]>
git.saurik.com Git - apple/network_cmds.git/blob - talkd.tproj/process.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1983, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 static char sccsid
[] = "@(#)process.c 8.2 (Berkeley) 11/16/93";
63 * process.c handles the requests, which can be of three types:
64 * ANNOUNCE - announce to a user that a talk is wanted
65 * LEAVE_INVITE - insert the request into the table
66 * LOOK_UP - look up to see if a request is waiting in
67 * in the table for the local user
68 * DELETE - delete invitation
70 #include <sys/param.h>
72 #include <sys/socket.h>
73 #include <netinet/in.h>
74 #include <protocols/talkd.h>
82 CTL_MSG
*find_request();
83 CTL_MSG
*find_match();
86 process_request(mp
, rp
)
88 register CTL_RESPONSE
*rp
;
90 register CTL_MSG
*ptr
;
93 rp
->vers
= TALK_VERSION
;
95 rp
->id_num
= htonl(0);
96 if (mp
->vers
!= TALK_VERSION
) {
97 syslog(LOG_WARNING
, "Bad protocol version %d", mp
->vers
);
98 rp
->answer
= BADVERSION
;
101 mp
->id_num
= ntohl(mp
->id_num
);
102 mp
->addr
.sa_family
= ntohs(mp
->addr
.sa_family
);
103 if (mp
->addr
.sa_family
!= AF_INET
) {
104 syslog(LOG_WARNING
, "Bad address, family %d",
106 rp
->answer
= BADADDR
;
109 mp
->ctl_addr
.sa_family
= ntohs(mp
->ctl_addr
.sa_family
);
110 if (mp
->ctl_addr
.sa_family
!= AF_INET
) {
111 syslog(LOG_WARNING
, "Bad control address, family %d",
112 mp
->ctl_addr
.sa_family
);
113 rp
->answer
= BADCTLADDR
;
116 mp
->pid
= ntohl(mp
->pid
);
118 print_request("process_request", mp
);
126 ptr
= find_request(mp
);
127 if (ptr
!= (CTL_MSG
*)0) {
128 rp
->id_num
= htonl(ptr
->id_num
);
129 rp
->answer
= SUCCESS
;
131 insert_table(mp
, rp
);
135 ptr
= find_match(mp
);
136 if (ptr
!= (CTL_MSG
*)0) {
137 rp
->id_num
= htonl(ptr
->id_num
);
138 rp
->addr
= ptr
->addr
;
139 rp
->addr
.sa_family
= htons(ptr
->addr
.sa_family
);
140 rp
->answer
= SUCCESS
;
142 rp
->answer
= NOT_HERE
;
146 rp
->answer
= delete_invite(mp
->id_num
);
150 rp
->answer
= UNKNOWN_REQUEST
;
154 print_response("process_request", rp
);
159 register CTL_MSG
*mp
;
166 /* see if the user is logged */
167 result
= find_user(mp
->r_name
, mp
->r_tty
);
168 if (result
!= SUCCESS
) {
172 #define satosin(sa) ((struct sockaddr_in *)(sa))
173 hp
= gethostbyaddr((char *)&satosin(&mp
->ctl_addr
)->sin_addr
,
174 sizeof (struct in_addr
), AF_INET
);
175 if (hp
== (struct hostent
*)0) {
176 rp
->answer
= MACHINE_UNKNOWN
;
179 ptr
= find_request(mp
);
180 if (ptr
== (CTL_MSG
*) 0) {
181 insert_table(mp
, rp
);
182 rp
->answer
= announce(mp
, hp
->h_name
);
185 if (mp
->id_num
> ptr
->id_num
) {
187 * This is an explicit re-announce, so update the id_num
188 * field to avoid duplicates and re-announce the talk.
190 ptr
->id_num
= new_id();
191 rp
->id_num
= htonl(ptr
->id_num
);
192 rp
->answer
= announce(mp
, hp
->h_name
);
194 /* a duplicated request, so ignore it */
195 rp
->id_num
= htonl(ptr
->id_num
);
196 rp
->answer
= SUCCESS
;
203 * Search utmp for the local user
213 char line
[sizeof(ubuf
.ut_line
) + 1];
214 char ftty
[sizeof(_PATH_DEV
) - 1 + sizeof(line
)];
216 if ((fd
= fopen(_PATH_UTMP
, "r")) == NULL
) {
217 fprintf(stderr
, "talkd: can't read %s.\n", _PATH_UTMP
);
220 #define SCMPN(a, b) strncmp(a, b, sizeof (a))
222 (void) strcpy(ftty
, _PATH_DEV
);
223 while (fread((char *) &ubuf
, sizeof ubuf
, 1, fd
) == 1)
224 if (SCMPN(ubuf
.ut_name
, name
) == 0) {
225 strncpy(line
, ubuf
.ut_line
, sizeof(ubuf
.ut_line
));
226 line
[sizeof(ubuf
.ut_line
)] = '\0';
228 status
= PERMISSION_DENIED
;
229 /* no particular tty was requested */
230 (void) strcpy(ftty
+ sizeof(_PATH_DEV
) - 1,
232 if (stat(ftty
, &statb
) == 0) {
233 if (!(statb
.st_mode
& 020))
235 (void) strcpy(tty
, line
);
240 if (strcmp(line
, tty
) == 0) {