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