xref: /original-bsd/usr.bin/talk/look_up.c (revision 7717c4d4)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
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.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)look_up.c	5.5 (Berkeley) 08/16/89";
20 #endif /* not lint */
21 
22 #include "talk_ctl.h"
23 
24 /*
25  * See if the local daemon has an invitation for us.
26  */
27 check_local()
28 {
29 	CTL_RESPONSE response;
30 	register CTL_RESPONSE *rp = &response;
31 
32 	/* the rest of msg was set up in get_names */
33 #ifdef MSG_EOR
34 	/* copy new style sockaddr to old, swap family (short in old) */
35 	msg.ctl_addr = *(struct osockaddr *)&ctl_addr;
36 	msg.ctl_addr.sa_family = htons(ctl_addr.sin_family);
37 #else
38 	msg.ctl_addr = *(struct sockaddr *)&ctl_addr;
39 #endif
40 	/* must be initiating a talk */
41 	if (!look_for_invite(rp))
42 		return (0);
43 	/*
44 	 * There was an invitation waiting for us,
45 	 * so connect with the other (hopefully waiting) party
46 	 */
47 	current_state = "Waiting to connect with caller";
48 	do {
49 		if (rp->addr.sa_family != AF_INET)
50 			p_error("Response uses invalid network address");
51 		errno = 0;
52 		if (connect(sockt, &rp->addr, sizeof (rp->addr)) != -1)
53 			return (1);
54 	} while (errno == EINTR);
55 	if (errno == ECONNREFUSED) {
56 		/*
57 		 * The caller gave up, but his invitation somehow
58 		 * was not cleared. Clear it and initiate an
59 		 * invitation. (We know there are no newer invitations,
60 		 * the talkd works LIFO.)
61 		 */
62 		ctl_transact(his_machine_addr, msg, DELETE, rp);
63 		close(sockt);
64 		open_sockt();
65 		return (0);
66 	}
67 	p_error("Unable to connect with initiator");
68 	/*NOTREACHED*/
69 }
70 
71 /*
72  * Look for an invitation on 'machine'
73  */
74 look_for_invite(rp)
75 	CTL_RESPONSE *rp;
76 {
77 	struct in_addr machine_addr;
78 
79 	current_state = "Checking for invitation on caller's machine";
80 	ctl_transact(his_machine_addr, msg, LOOK_UP, rp);
81 	/* the switch is for later options, such as multiple invitations */
82 	switch (rp->answer) {
83 
84 	case SUCCESS:
85 		msg.id_num = htonl(rp->id_num);
86 		return (1);
87 
88 	default:
89 		/* there wasn't an invitation waiting for us */
90 		return (0);
91 	}
92 }
93