xref: /original-bsd/usr.bin/talk/look_up.c (revision 241757c4)
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 this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)look_up.c	5.3 (Berkeley) 05/20/88";
15 #endif /* not lint */
16 
17 #include "talk_ctl.h"
18 
19 /*
20  * See if the local daemon has an invitation for us.
21  */
22 check_local()
23 {
24 	CTL_RESPONSE response;
25 	register CTL_RESPONSE *rp = &response;
26 
27 	/* the rest of msg was set up in get_names */
28 	msg.ctl_addr = *(struct sockaddr *)&ctl_addr;
29 	msg.ctl_addr.sa_family = htons(msg.ctl_addr.sa_family);
30 	/* must be initiating a talk */
31 	if (!look_for_invite(rp))
32 		return (0);
33 	/*
34 	 * There was an invitation waiting for us,
35 	 * so connect with the other (hopefully waiting) party
36 	 */
37 	current_state = "Waiting to connect with caller";
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);
45 	if (errno == ECONNREFUSED) {
46 		/*
47 		 * The caller gave up, but his invitation somehow
48 		 * was not cleared. Clear it and initiate an
49 		 * invitation. (We know there are no newer invitations,
50 		 * the talkd works LIFO.)
51 		 */
52 		ctl_transact(his_machine_addr, msg, DELETE, rp);
53 		close(sockt);
54 		open_sockt();
55 		return (0);
56 	}
57 	p_error("Unable to connect with initiator");
58 	/*NOTREACHED*/
59 }
60 
61 /*
62  * Look for an invitation on 'machine'
63  */
64 look_for_invite(rp)
65 	CTL_RESPONSE *rp;
66 {
67 	struct in_addr machine_addr;
68 
69 	current_state = "Checking for invitation on caller's machine";
70 	ctl_transact(his_machine_addr, msg, LOOK_UP, rp);
71 	/* the switch is for later options, such as multiple invitations */
72 	switch (rp->answer) {
73 
74 	case SUCCESS:
75 		msg.id_num = htonl(rp->id_num);
76 		return (1);
77 
78 	default:
79 		/* there wasn't an invitation waiting for us */
80 		return (0);
81 	}
82 }
83