xref: /csrg-svn/usr.bin/talk/look_up.c (revision 16365)
1 #ifndef lint
2 static char sccsid[] = "@(#)look_up.c	1.2 (Berkeley) 04/11/84";
3 #endif
4 
5 #include "talk_ctl.h"
6 
7 /*
8  * See if the local daemon has a invitation for us
9  */
10 check_local()
11 {
12 	CTL_RESPONSE response;
13 
14 	/* the rest of msg was set up in get_names */
15 	msg.ctl_addr = ctl_addr;
16 	if (!look_for_invite(&response))	/* must be initiating a talk */
17 		return (0);
18 	/*
19 	 * There was an invitation waiting for us,
20 	 * so connect with the other (hopefully waiting) party
21 	 */
22 	current_state = "Waiting to connect with caller";
23 again:
24 	if (connect(sockt, &response.addr, sizeof(response.addr)) != -1)
25 		return (1);
26 	if (errno == EINTR)
27 		goto again;
28 	if (errno == ECONNREFUSED) {
29 		/*
30 		 * The caller gave up, but his invitation somehow
31 		 * was not cleared. Clear it and initiate an
32 		 * invitation. (We know there are no newer invitations,
33 		 * the talkd works LIFO.)
34 		 */
35 		ctl_transact(his_machine_addr, msg, DELETE, &response);
36 		close(sockt);
37 		open_sockt();
38 		return (0);
39 	}
40 	p_error("Unable to connect with initiator");
41 	/*NOTREACHED*/
42 }
43 
44 /*
45  * Look for an invitation on 'machine'
46  */
47 look_for_invite(response)
48 	CTL_RESPONSE *response;
49 {
50 	struct in_addr machine_addr;
51 
52 	current_state = "Checking for invitation on caller's machine";
53 	ctl_transact(his_machine_addr, msg, LOOK_UP, response);
54 	/* the switch is for later options, such as multiple invitations */
55 	switch (response->answer) {
56 
57 	case SUCCESS:
58 		msg.id_num = response->id_num;
59 		return (1);
60 
61 	default :
62 		/* there wasn't an invitation waiting for us */
63 		return (0);
64 	}
65 }
66