1 /*-
2 * Copyright (c) 2003-2005 MAEKAWA Masahide <maekawa@cvsync.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/types.h>
31 #include <sys/socket.h>
32
33 #include <netdb.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36
37 #include <stdlib.h>
38
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include "compat_stdbool.h"
46
47 #include "logmsg.h"
48 #include "network.h"
49
50 int ipv4_listen_addr(in_addr_t, in_port_t);
51 int ipv4_connect_addr(in_addr_t, in_port_t);
52 in_port_t ipv4_port_pton(const char *);
53
54 int *
sock_listen(const char * host,const char * serv)55 sock_listen(const char *host, const char *serv)
56 {
57 struct hostent *h;
58 struct in_addr in;
59 in_addr_t addr;
60 in_port_t port;
61 int *socks, sock, nsocks, i;
62
63 if (serv == NULL)
64 return (NULL);
65 if ((port = ipv4_port_pton(serv)) == 0)
66 return (NULL);
67
68 if (host == NULL) {
69 if ((socks = malloc(2 * sizeof(*socks))) == NULL) {
70 logmsg_err("%s", strerror(errno));
71 return (NULL);
72 }
73 addr = htonl(INADDR_ANY);
74 if ((socks[0] = ipv4_listen_addr(addr, port)) == -1) {
75 free(socks);
76 return (NULL);
77 }
78
79 in.s_addr = addr;
80 logmsg_verbose("Listen on %s port %u", inet_ntoa(in), port);
81
82 socks[1] = -1;
83 return (socks);
84 }
85 if ((addr = inet_addr(host)) != (in_addr_t)-1) {
86 if ((socks = malloc(2 * sizeof(*socks))) == NULL) {
87 logmsg_err("%s", strerror(errno));
88 return (NULL);
89 }
90 if ((socks[0] = ipv4_listen_addr(addr, port)) == -1) {
91 free(socks);
92 return (NULL);
93 }
94 socks[1] = -1;
95 return (socks);
96 }
97
98 if ((h = gethostbyname(host)) == NULL)
99 return (NULL);
100
101 for (nsocks = 0 ; h->h_addr_list[nsocks] != NULL ; nsocks++)
102 /* Nothing to do. */;
103 if (nsocks == 0) {
104 logmsg_err("no address available");
105 return (NULL);
106 }
107
108 if ((socks = malloc((nsocks + 1) * sizeof(*socks))) == NULL) {
109 logmsg_err("%s", strerror(errno));
110 return (NULL);
111 }
112
113 nsocks = 0;
114 for (i = 0 ; h->h_addr_list[i] != NULL ; i++) {
115 addr = *(in_addr_t *)(void *)h->h_addr_list[i];
116 if ((sock = ipv4_listen_addr(addr, port)) == -1)
117 continue;
118
119 in.s_addr = addr;
120 logmsg_verbose("Listen on %s port %u", inet_ntoa(in), port);
121
122 socks[nsocks++] = sock;
123 }
124
125 if (nsocks == 0) {
126 logmsg_err("no listening socket");
127 free(socks);
128 return (NULL);
129 }
130
131 socks[nsocks] = -1;
132
133 return (socks);
134 }
135
136 int
sock_connect(int af,const char * host,const char * serv)137 sock_connect(int af, const char *host, const char *serv)
138 {
139 struct hostent *h;
140 struct in_addr in;
141 in_addr_t addr;
142 in_port_t port;
143 int sock = -1, i;
144
145 if ((af != AF_UNSPEC) && (af != AF_INET))
146 return (-1);
147 if ((host == NULL) || (serv == NULL))
148 return (-1);
149
150 logmsg("Connecting to %s port %s", host, serv);
151
152 if ((port = ipv4_port_pton(serv)) == 0)
153 return (-1);
154
155 if ((addr = inet_addr(host)) != (in_addr_t)-1) {
156 if ((sock = ipv4_connect_addr(addr, port)) == -1)
157 return (-1);
158 goto done;
159 }
160
161 if ((h = gethostbyname(host)) == NULL)
162 return (-1);
163
164 for (i = 0 ; h->h_addr_list[i] != NULL ; i++) {
165 addr = *(in_addr_t *)(void *)h->h_addr_list[i];
166 if ((sock = ipv4_connect_addr(addr, port)) == -1)
167 continue;
168
169 break;
170 }
171
172 if (sock == -1)
173 return (-1);
174
175 done:
176 in.s_addr = addr; /* already network byte order */
177 logmsg("Connected to %s port %u", inet_ntoa(in), port);
178
179 return (sock);
180 }
181
182 bool
sock_getpeeraddr(int sock,int * af,void * buffer,size_t bufsize)183 sock_getpeeraddr(int sock, int *af, void *buffer, size_t bufsize)
184 {
185 struct sockaddr_in sin4;
186 char *cp;
187 socklen_t sin4len = sizeof(sin4);
188 size_t len;
189
190 *af = AF_INET;
191
192 if (getpeername(sock, (struct sockaddr *)(void *)&sin4,
193 &sin4len) == -1) {
194 logmsg_err("%s", strerror(errno));
195 return (false);
196 }
197
198 if ((cp = inet_ntoa(sin4.sin_addr)) == NULL)
199 return (false);
200 if ((len = strlen(cp)) >= bufsize)
201 return (false);
202 (void)memcpy(buffer, cp, len);
203 ((char *)buffer)[len] = '\0';
204
205 return (true);
206 }
207
208 void
sock_resolv_addr(int af,const char * addr,char * buffer,size_t bufsize)209 sock_resolv_addr(int af, const char *addr, char *buffer, size_t bufsize)
210 {
211 struct hostent *h;
212 size_t len;
213
214 buffer[0] = '\0';
215
216 if (af != AF_INET)
217 return;
218
219 if ((h = gethostbyname(addr)) == NULL)
220 return;
221
222 if ((h->h_name == NULL) || ((len = strlen(h->h_name)) >= bufsize))
223 return;
224
225 (void)memcpy(buffer, h->h_name, len);
226 buffer[len] = '\0';
227 }
228
229 int
ipv4_listen_addr(in_addr_t addr,in_port_t port)230 ipv4_listen_addr(in_addr_t addr, in_port_t port)
231 {
232 static const int on = 1;
233 struct sockaddr_in sin4;
234 int sock, flags;
235
236 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
237 return (-1);
238
239 if ((flags = fcntl(sock, F_GETFL)) < 0) {
240 (void)close(sock);
241 return (-1);
242 }
243 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) {
244 (void)close(sock);
245 return (-1);
246 }
247
248 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
249 sizeof(on)) == -1) {
250 (void)close(sock);
251 return (-1);
252 }
253
254 sin4.sin_family = AF_INET;
255 sin4.sin_port = htons(port);
256 sin4.sin_addr.s_addr = addr; /* already network byte order */
257
258 if (bind(sock, (struct sockaddr *)(void *)&sin4,
259 sizeof(sin4)) == -1) {
260 (void)close(sock);
261 return (-1);
262 }
263
264 if (listen(sock, SOMAXCONN) == -1) {
265 (void)close(sock);
266 return (-1);
267 }
268
269 return (sock);
270 }
271
272 int
ipv4_connect_addr(in_addr_t addr,in_port_t port)273 ipv4_connect_addr(in_addr_t addr, in_port_t port)
274 {
275 struct sockaddr_in sin4;
276 int sock;
277
278 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
279 return (-1);
280
281 sin4.sin_family = AF_INET;
282 sin4.sin_port = htons(port);
283 sin4.sin_addr.s_addr = addr; /* already network byte order */
284
285 if (connect(sock, (struct sockaddr *)(void *)&sin4,
286 sizeof(sin4)) == -1) {
287 (void)close(sock);
288 return (-1);
289 }
290
291 return (sock);
292 }
293
294 in_port_t
ipv4_port_pton(const char * name)295 ipv4_port_pton(const char *name)
296 {
297 struct servent *serv;
298 char *ep;
299 unsigned long ul;
300
301 if ((serv = getservbyname(name, "tcp")) != NULL)
302 return (ntohs(serv->s_port));
303
304 errno = 0;
305 ul = strtoul(name, &ep, 10);
306 if ((ep == NULL) || (*ep != '\0') ||
307 ((ul == 0) && (errno == EINVAL)) ||
308 ((ul == ULONG_MAX) && (errno == ERANGE))) {
309 return (0);
310 }
311 if ((ul == 0) || (ul > 65535))
312 return (0);
313
314 return ((in_port_t)ul);
315 }
316