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