Debian Codegraph @

talk/ctl.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 /*
49 * This file handles haggling with the various talk daemons to
50 * get a socket to talk to. sockt is opened and connected in
51 * the progress
52 */
53
54 #include <config.h>
55
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <protocols/talkd.h>
59 #include <netinet/in.h>
60 #include "talk.h"
61 #include "talk_ctl.h"
62
63 struct sockaddr_in daemon_addr;
64 struct sockaddr_in ctl_addr;
65 struct sockaddr_in my_addr;
66
67 /* inet addresses of the two machines */
68 struct in_addr my_machine_addr;
69 struct in_addr his_machine_addr;
70
71 unsigned short daemon_port; /* port number of the talk daemon */
72
73 int ctl_sockt;
74 int sockt;
75 int invitation_waiting = 0;
76
77 CTL_MSG msg;
78
79 int
80 open_sockt (void)
81 {
82 socklen_t length;
83
84 my_addr.sin_family = AF_INET;
85 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
86 my_addr.sin_len = sizeof (my_addr);
87 #endif
88 my_addr.sin_addr = my_machine_addr;
89 my_addr.sin_port = 0;
90 sockt = socket (AF_INET, SOCK_STREAM, 0);
91 if (sockt <= 0)
92 p_error ("Bad socket");
93 if (bind (sockt, (struct sockaddr *) &my_addr, sizeof (my_addr)) != 0)
94 p_error ("Binding local socket");
95 length = sizeof (my_addr);
96 if (getsockname (sockt, (struct sockaddr *) &my_addr, &length) == -1)
97 p_error ("Bad address for socket");
98
99 return 0;
100 }
101
102 /* open the ctl socket */
103 int
104 open_ctl (void)
105 {
106 socklen_t length;
107
108 ctl_addr.sin_family = AF_INET;
109 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
110 ctl_addr.sin_len = sizeof (ctl_addr);
111 #endif
112 ctl_addr.sin_port = 0;
113 ctl_addr.sin_addr = my_machine_addr;
114 ctl_sockt = socket (AF_INET, SOCK_DGRAM, 0);
115 if (ctl_sockt <= 0)
116 p_error ("Bad socket");
117 if (bind (ctl_sockt, (struct sockaddr *) &ctl_addr, sizeof (ctl_addr)) != 0)
118 p_error ("Couldn't bind to control socket");
119 length = sizeof (ctl_addr);
120 if (getsockname (ctl_sockt, (struct sockaddr *) &ctl_addr, &length) == -1)
121 p_error ("Bad address for ctl socket");
122
123 return 0;
124 }