Debian Codegraph @

talk/look_up.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1983, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include <string.h>
51
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <protocols/talkd.h>
56 #include <unistd.h>
57 #include <errno.h>
58 #include "talk_ctl.h"
59 #include "talk.h"
60
61 /*
62 * Look for an invitation on 'machine'
63 */
64 static int
65 look_for_invite (CTL_RESPONSE *rp)
66 {
67 current_state = "Checking for invitation on caller's machine";
68 ctl_transact (his_machine_addr, msg, LOOK_UP, rp);
69 /* the switch is for later options, such as multiple invitations */
70 switch (rp->answer)
71 {
72
73 case SUCCESS:
74 msg.id_num = htonl (rp->id_num);
75 return (1);
76
77 default:
78 /* there wasn't an invitation waiting for us */
79 return (0);
80 }
81 }
82
83 /*
84 * See if the local daemon has an invitation for us.
85 */
86 int
87 check_local (void)
88 {
89 CTL_RESPONSE response;
90 CTL_RESPONSE *rp = &response;
91
92 /* the rest of msg was set up in get_names */
93 msg.ctl_addr.sa_family = htons (ctl_addr.sin_family);
94 memcpy (msg.ctl_addr.sa_data,
95 ((struct sockaddr *) &ctl_addr)->sa_data,
96 sizeof ((struct sockaddr *) & ctl_addr)->sa_data);
97
98 /* must be initiating a talk */
99 if (!look_for_invite (rp))
100 return (0);
101 /*
102 * There was an invitation waiting for us,
103 * so connect with the other (hopefully waiting) party
104 */
105 current_state = "Waiting to connect with caller";
106 do
107 {
108 struct sockaddr addr;
109
110 if (rp->addr.sa_family != AF_INET)
111 p_error ("Response uses invalid network address");
112 errno = 0;
113 addr.sa_family = rp->addr.sa_family;
114 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
115 addr.sa_len = sizeof (struct sockaddr_in);
116 #endif
117 memcpy (&addr.sa_data, &rp->addr.sa_data, sizeof (addr.sa_data));
118
119 if (connect (sockt, &addr, sizeof (addr)) != -1)
120 return (1);
121 }
122 while (errno == EINTR);
123 if (errno == ECONNREFUSED)
124 {
125 /*
126 * The caller gave up, but his invitation somehow
127 * was not cleared. Clear it and initiate an
128 * invitation. (We know there are no newer invitations,
129 * the talkd works LIFO.)
130 */
131 ctl_transact (his_machine_addr, msg, DELETE, rp);
132 close (sockt);
133 open_sockt ();
134 return (0);
135 }
136 p_error ("Unable to connect with initiator");
137
138 return -1;
139 }