new copyright notice
[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 *
cb956e54 5 * %sccs.include.redist.c%
d0aeaf5a
DF
6 */
7
963ce42e 8#ifndef lint
cb956e54 9static char sccsid[] = "@(#)look_up.c 5.6 (Berkeley) %G%";
f9ac90b4 10#endif /* not lint */
9f704a96
MK
11
12#include "talk_ctl.h"
13
963ce42e 14/*
4fa2ac2d 15 * See if the local daemon has an invitation for us.
963ce42e 16 */
9f704a96
MK
17check_local()
18{
963ce42e 19 CTL_RESPONSE response;
4fa2ac2d 20 register CTL_RESPONSE *rp = &response;
9f704a96
MK
21
22 /* the rest of msg was set up in get_names */
7038e9c5
MK
23#ifdef MSG_EOR
24 /* copy new style sockaddr to old, swap family (short in old) */
25 msg.ctl_addr = *(struct osockaddr *)&ctl_addr;
26 msg.ctl_addr.sa_family = htons(ctl_addr.sin_family);
27#else
4fa2ac2d 28 msg.ctl_addr = *(struct sockaddr *)&ctl_addr;
7038e9c5 29#endif
9364b4ce 30 /* must be initiating a talk */
4fa2ac2d 31 if (!look_for_invite(rp))
963ce42e
MK
32 return (0);
33 /*
34 * There was an invitation waiting for us,
9f704a96
MK
35 * so connect with the other (hopefully waiting) party
36 */
963ce42e 37 current_state = "Waiting to connect with caller";
4fa2ac2d
KM
38 do {
39 if (rp->addr.sa_family != AF_INET)
40 p_error("Response uses invalid network address");
41 errno = 0;
42 if (connect(sockt, &rp->addr, sizeof (rp->addr)) != -1)
43 return (1);
44 } while (errno == EINTR);
9f704a96 45 if (errno == ECONNREFUSED) {
963ce42e
MK
46 /*
47 * The caller gave up, but his invitation somehow
9f704a96
MK
48 * was not cleared. Clear it and initiate an
49 * invitation. (We know there are no newer invitations,
50 * the talkd works LIFO.)
51 */
4fa2ac2d 52 ctl_transact(his_machine_addr, msg, DELETE, rp);
963ce42e
MK
53 close(sockt);
54 open_sockt();
55 return (0);
9f704a96 56 }
963ce42e
MK
57 p_error("Unable to connect with initiator");
58 /*NOTREACHED*/
9f704a96
MK
59}
60
963ce42e
MK
61/*
62 * Look for an invitation on 'machine'
63 */
4fa2ac2d
KM
64look_for_invite(rp)
65 CTL_RESPONSE *rp;
9f704a96 66{
963ce42e 67 struct in_addr machine_addr;
9f704a96 68
963ce42e 69 current_state = "Checking for invitation on caller's machine";
4fa2ac2d 70 ctl_transact(his_machine_addr, msg, LOOK_UP, rp);
963ce42e 71 /* the switch is for later options, such as multiple invitations */
4fa2ac2d 72 switch (rp->answer) {
9f704a96
MK
73
74 case SUCCESS:
4fa2ac2d 75 msg.id_num = htonl(rp->id_num);
963ce42e 76 return (1);
9f704a96 77
4fa2ac2d 78 default:
9f704a96 79 /* there wasn't an invitation waiting for us */
963ce42e
MK
80 return (0);
81 }
9f704a96 82}