temp files should be protected; bug report 4.3BSD/usr.bin/186
[unix-history] / usr / src / usr.bin / talk / look_up.c
CommitLineData
d0aeaf5a
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
f9ac90b4
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b36fc510
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
d0aeaf5a
DF
16 */
17
963ce42e 18#ifndef lint
b36fc510 19static char sccsid[] = "@(#)look_up.c 5.4 (Berkeley) %G%";
f9ac90b4 20#endif /* not lint */
9f704a96
MK
21
22#include "talk_ctl.h"
23
963ce42e 24/*
4fa2ac2d 25 * See if the local daemon has an invitation for us.
963ce42e 26 */
9f704a96
MK
27check_local()
28{
963ce42e 29 CTL_RESPONSE response;
4fa2ac2d 30 register CTL_RESPONSE *rp = &response;
9f704a96
MK
31
32 /* the rest of msg was set up in get_names */
4fa2ac2d
KM
33 msg.ctl_addr = *(struct sockaddr *)&ctl_addr;
34 msg.ctl_addr.sa_family = htons(msg.ctl_addr.sa_family);
9364b4ce 35 /* must be initiating a talk */
4fa2ac2d 36 if (!look_for_invite(rp))
963ce42e
MK
37 return (0);
38 /*
39 * There was an invitation waiting for us,
9f704a96
MK
40 * so connect with the other (hopefully waiting) party
41 */
963ce42e 42 current_state = "Waiting to connect with caller";
4fa2ac2d
KM
43 do {
44 if (rp->addr.sa_family != AF_INET)
45 p_error("Response uses invalid network address");
46 errno = 0;
47 if (connect(sockt, &rp->addr, sizeof (rp->addr)) != -1)
48 return (1);
49 } while (errno == EINTR);
9f704a96 50 if (errno == ECONNREFUSED) {
963ce42e
MK
51 /*
52 * The caller gave up, but his invitation somehow
9f704a96
MK
53 * was not cleared. Clear it and initiate an
54 * invitation. (We know there are no newer invitations,
55 * the talkd works LIFO.)
56 */
4fa2ac2d 57 ctl_transact(his_machine_addr, msg, DELETE, rp);
963ce42e
MK
58 close(sockt);
59 open_sockt();
60 return (0);
9f704a96 61 }
963ce42e
MK
62 p_error("Unable to connect with initiator");
63 /*NOTREACHED*/
9f704a96
MK
64}
65
963ce42e
MK
66/*
67 * Look for an invitation on 'machine'
68 */
4fa2ac2d
KM
69look_for_invite(rp)
70 CTL_RESPONSE *rp;
9f704a96 71{
963ce42e 72 struct in_addr machine_addr;
9f704a96 73
963ce42e 74 current_state = "Checking for invitation on caller's machine";
4fa2ac2d 75 ctl_transact(his_machine_addr, msg, LOOK_UP, rp);
963ce42e 76 /* the switch is for later options, such as multiple invitations */
4fa2ac2d 77 switch (rp->answer) {
9f704a96
MK
78
79 case SUCCESS:
4fa2ac2d 80 msg.id_num = htonl(rp->id_num);
963ce42e 81 return (1);
9f704a96 82
4fa2ac2d 83 default:
9f704a96 84 /* there wasn't an invitation waiting for us */
963ce42e
MK
85 return (0);
86 }
9f704a96 87}