4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
28 * All Rights Reserved.
32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 * The Regents of the University of California.
34 * All Rights Reserved.
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
41 #pragma ident "%Z%%M% %I% %E% SMI"
44 * Invoked by the Internet daemon to handle talk requests
45 * Processes talk requests until MAX_LIFE seconds go by with
46 * no action, then dies.
51 #include <sys/ioctl.h>
53 #include <sys/systeminfo.h>
58 #include "talkd_impl.h"
60 static CTL_MSG request
;
61 static CTL_RESPONSE response
;
63 char hostname
[HOST_NAME_LENGTH
];
66 static CTL_MSG
swapmsg(CTL_MSG req
);
71 struct sockaddr_in from
;
72 socklen_t from_size
= (socklen_t
)sizeof (from
);
74 int name_length
= sizeof (hostname
);
78 (void) sysinfo(SI_HOSTNAME
, hostname
, name_length
);
85 if (select(1, &rfds
, 0, 0, &tv
) <= 0)
87 cc
= recvfrom(0, (char *)&request
, sizeof (request
), 0,
88 (struct sockaddr
*)&from
, &from_size
);
90 if (cc
!= sizeof (request
)) {
91 if (cc
< 0 && errno
!= EINTR
) {
92 print_error("receive");
97 (void) printf("Request received : \n");
98 (void) print_request(&request
);
101 request
= swapmsg(request
);
102 process_request(&request
, &response
);
105 (void) printf("Response sent : \n");
106 print_response(&response
);
110 * Can block here, is this what I want?
112 cc
= sendto(0, (char *)&response
, sizeof (response
), 0,
113 (struct sockaddr
*)&request
.ctl_addr
,
114 (socklen_t
)sizeof (request
.ctl_addr
));
116 if (cc
!= sizeof (response
)) {
124 print_error(char *string
)
127 char *err_dev
= "/dev/console";
132 err_dev
= "/dev/tty";
134 if ((sys
= strerror(errno
)) == NULL
)
135 sys
= "Unknown error";
137 /* don't ever open tty's directly, let a child do it */
138 if ((pid
= fork()) == 0) {
139 cons
= fopen(err_dev
, "a");
141 (void) fprintf(cons
, "Talkd : %s : %s(%d)\n\r", string
,
147 /* wait for the child process to return */
150 if (val
== (pid_t
)-1) {
151 if (errno
== EINTR
) {
153 } else if (errno
== ECHILD
) {
157 } while (val
!= pid
);
161 #define swapshort(a) (((a << 8) | ((unsigned short) a >> 8)) & 0xffff)
162 #define swaplong(a) ((swapshort(a) << 16) | (swapshort(((unsigned)a >> 16))))
165 * Heuristic to detect if need to swap bytes.
173 if (req
.ctl_addr
.sin_family
== swapshort(AF_INET
)) {
175 swapreq
.id_num
= swaplong(req
.id_num
);
176 swapreq
.pid
= swaplong(req
.pid
);
177 swapreq
.addr
.sin_family
= swapshort(req
.addr
.sin_family
);
178 swapreq
.ctl_addr
.sin_family
=
179 swapshort(req
.ctl_addr
.sin_family
);