blob: 4a22c6386c7976b724556bf7a1ba5822282f6fc3 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Socket union related function.
2 * Copyright (c) 1997, 98 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include "prefix.h"
25#include "vty.h"
26#include "sockunion.h"
27#include "memory.h"
28#include "str.h"
29#include "log.h"
Timo Teräs9196caf2015-04-29 09:43:05 +030030#include "jhash.h"
paul718e3742002-12-13 20:15:29 +000031
32#ifndef HAVE_INET_ATON
33int
34inet_aton (const char *cp, struct in_addr *inaddr)
35{
36 int dots = 0;
37 register u_long addr = 0;
38 register u_long val = 0, base = 10;
39
40 do
41 {
42 register char c = *cp;
43
44 switch (c)
45 {
46 case '0': case '1': case '2': case '3': case '4': case '5':
47 case '6': case '7': case '8': case '9':
48 val = (val * base) + (c - '0');
49 break;
50 case '.':
51 if (++dots > 3)
52 return 0;
53 case '\0':
54 if (val > 255)
55 return 0;
56 addr = addr << 8 | val;
57 val = 0;
58 break;
59 default:
60 return 0;
61 }
62 } while (*cp++) ;
63
64 if (dots < 3)
65 addr <<= 8 * (3 - dots);
66 if (inaddr)
67 inaddr->s_addr = htonl (addr);
68 return 1;
69}
70#endif /* ! HAVE_INET_ATON */
71
72
73#ifndef HAVE_INET_PTON
74int
75inet_pton (int family, const char *strptr, void *addrptr)
76{
77 if (family == AF_INET)
78 {
79 struct in_addr in_val;
80
81 if (inet_aton (strptr, &in_val))
82 {
83 memcpy (addrptr, &in_val, sizeof (struct in_addr));
84 return 1;
85 }
86 return 0;
87 }
88 errno = EAFNOSUPPORT;
89 return -1;
90}
91#endif /* ! HAVE_INET_PTON */
92
93#ifndef HAVE_INET_NTOP
94const char *
95inet_ntop (int family, const void *addrptr, char *strptr, size_t len)
96{
97 unsigned char *p = (unsigned char *) addrptr;
98
99 if (family == AF_INET)
100 {
101 char temp[INET_ADDRSTRLEN];
102
103 snprintf(temp, sizeof(temp), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
104
105 if (strlen(temp) >= len)
106 {
107 errno = ENOSPC;
108 return NULL;
109 }
110 strcpy(strptr, temp);
111 return strptr;
112 }
113
114 errno = EAFNOSUPPORT;
115 return NULL;
116}
117#endif /* ! HAVE_INET_NTOP */
118
119const char *
Timo Teräs81b139b2015-04-29 09:43:01 +0300120inet_sutop (const union sockunion *su, char *str)
paul718e3742002-12-13 20:15:29 +0000121{
122 switch (su->sa.sa_family)
123 {
124 case AF_INET:
125 inet_ntop (AF_INET, &su->sin.sin_addr, str, INET_ADDRSTRLEN);
126 break;
127#ifdef HAVE_IPV6
128 case AF_INET6:
129 inet_ntop (AF_INET6, &su->sin6.sin6_addr, str, INET6_ADDRSTRLEN);
130 break;
131#endif /* HAVE_IPV6 */
132 }
133 return str;
134}
135
136int
hassoa1494112004-10-11 12:53:17 +0000137str2sockunion (const char *str, union sockunion *su)
paul718e3742002-12-13 20:15:29 +0000138{
139 int ret;
140
141 memset (su, 0, sizeof (union sockunion));
142
143 ret = inet_pton (AF_INET, str, &su->sin.sin_addr);
144 if (ret > 0) /* Valid IPv4 address format. */
145 {
146 su->sin.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000147#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000148 su->sin.sin_len = sizeof(struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000149#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000150 return 0;
151 }
152#ifdef HAVE_IPV6
153 ret = inet_pton (AF_INET6, str, &su->sin6.sin6_addr);
154 if (ret > 0) /* Valid IPv6 address format. */
155 {
156 su->sin6.sin6_family = AF_INET6;
157#ifdef SIN6_LEN
158 su->sin6.sin6_len = sizeof(struct sockaddr_in6);
159#endif /* SIN6_LEN */
160 return 0;
161 }
162#endif /* HAVE_IPV6 */
163 return -1;
164}
165
166const char *
Timo Teräs81b139b2015-04-29 09:43:01 +0300167sockunion2str (const union sockunion *su, char *buf, size_t len)
paul718e3742002-12-13 20:15:29 +0000168{
Timo Teräs53009d32015-05-23 11:08:38 +0300169 switch (sockunion_family(su))
170 {
171 case AF_UNSPEC:
172 snprintf (buf, len, "(unspec)");
173 return buf;
174 case AF_INET:
175 return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len);
paul718e3742002-12-13 20:15:29 +0000176#ifdef HAVE_IPV6
Timo Teräs53009d32015-05-23 11:08:38 +0300177 case AF_INET6:
178 return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len);
paul718e3742002-12-13 20:15:29 +0000179#endif /* HAVE_IPV6 */
Timo Teräs53009d32015-05-23 11:08:38 +0300180 }
181 snprintf (buf, len, "(af %d)", sockunion_family(su));
182 return buf;
paul718e3742002-12-13 20:15:29 +0000183}
184
Paul Jakma2fb2a452012-06-14 10:37:40 +0100185union sockunion *
186sockunion_str2su (const char *str)
187{
188 union sockunion *su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
189
190 if (!str2sockunion (str, su))
191 return su;
192
193 XFREE (MTYPE_SOCKUNION, su);
194 return NULL;
195}
196
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100197/* Convert IPv4 compatible IPv6 address to IPv4 address. */
198static void
199sockunion_normalise_mapped (union sockunion *su)
200{
201 struct sockaddr_in sin;
202
203#ifdef HAVE_IPV6
204 if (su->sa.sa_family == AF_INET6
205 && IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
206 {
207 memset (&sin, 0, sizeof (struct sockaddr_in));
208 sin.sin_family = AF_INET;
Stephen Hemminger3fa3f952009-07-11 21:27:51 -0700209 sin.sin_port = su->sin6.sin6_port;
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100210 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
211 memcpy (su, &sin, sizeof (struct sockaddr_in));
212 }
213#endif /* HAVE_IPV6 */
214}
215
paul718e3742002-12-13 20:15:29 +0000216/* Return socket of sockunion. */
217int
Timo Teräs81b139b2015-04-29 09:43:01 +0300218sockunion_socket (const union sockunion *su)
paul718e3742002-12-13 20:15:29 +0000219{
220 int sock;
221
222 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
223 if (sock < 0)
224 {
ajs6099b3b2004-11-20 02:06:59 +0000225 zlog (NULL, LOG_WARNING, "Can't make socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000226 return -1;
227 }
228
229 return sock;
230}
231
232/* Return accepted new socket file descriptor. */
233int
234sockunion_accept (int sock, union sockunion *su)
235{
236 socklen_t len;
237 int client_sock;
238
239 len = sizeof (union sockunion);
240 client_sock = accept (sock, (struct sockaddr *) su, &len);
241
Paul Jakma84152ee2008-11-24 22:25:16 +0000242 sockunion_normalise_mapped (su);
paul718e3742002-12-13 20:15:29 +0000243 return client_sock;
244}
245
246/* Return sizeof union sockunion. */
paul8cc41982005-05-06 21:25:49 +0000247static int
Timo Teräs81b139b2015-04-29 09:43:01 +0300248sockunion_sizeof (const union sockunion *su)
paul718e3742002-12-13 20:15:29 +0000249{
250 int ret;
251
252 ret = 0;
253 switch (su->sa.sa_family)
254 {
255 case AF_INET:
256 ret = sizeof (struct sockaddr_in);
257 break;
258#ifdef HAVE_IPV6
259 case AF_INET6:
260 ret = sizeof (struct sockaddr_in6);
261 break;
262#endif /* AF_INET6 */
263 }
264 return ret;
265}
266
267/* return sockunion structure : this function should be revised. */
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400268static const char *
Timo Teräs81b139b2015-04-29 09:43:01 +0300269sockunion_log (const union sockunion *su, char *buf, size_t len)
paul718e3742002-12-13 20:15:29 +0000270{
paul718e3742002-12-13 20:15:29 +0000271 switch (su->sa.sa_family)
272 {
273 case AF_INET:
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400274 return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len);
275
paul718e3742002-12-13 20:15:29 +0000276#ifdef HAVE_IPV6
277 case AF_INET6:
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400278 return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len);
paul718e3742002-12-13 20:15:29 +0000279 break;
280#endif /* HAVE_IPV6 */
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400281
paul718e3742002-12-13 20:15:29 +0000282 default:
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400283 snprintf (buf, len, "af_unknown %d ", su->sa.sa_family);
284 return buf;
paul718e3742002-12-13 20:15:29 +0000285 }
paul718e3742002-12-13 20:15:29 +0000286}
287
288/* sockunion_connect returns
289 -1 : error occured
290 0 : connect success
291 1 : connect is in progress */
292enum connect_result
Timo Teräs81b139b2015-04-29 09:43:01 +0300293sockunion_connect (int fd, const union sockunion *peersu, unsigned short port,
Paul Jakma9099f9b2016-01-18 10:12:10 +0000294 ifindex_t ifindex)
paul718e3742002-12-13 20:15:29 +0000295{
296 int ret;
297 int val;
298 union sockunion su;
299
300 memcpy (&su, peersu, sizeof (union sockunion));
301
302 switch (su.sa.sa_family)
303 {
304 case AF_INET:
305 su.sin.sin_port = port;
306 break;
307#ifdef HAVE_IPV6
308 case AF_INET6:
309 su.sin6.sin6_port = port;
310#ifdef KAME
311 if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex)
312 {
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000313#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
paul718e3742002-12-13 20:15:29 +0000314 /* su.sin6.sin6_scope_id = ifindex; */
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000315#endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */
paul718e3742002-12-13 20:15:29 +0000316 SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex);
paul718e3742002-12-13 20:15:29 +0000317 }
318#endif /* KAME */
319 break;
320#endif /* HAVE_IPV6 */
321 }
322
323 /* Make socket non-block. */
324 val = fcntl (fd, F_GETFL, 0);
325 fcntl (fd, F_SETFL, val|O_NONBLOCK);
326
327 /* Call connect function. */
328 ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su));
329
330 /* Immediate success */
331 if (ret == 0)
332 {
333 fcntl (fd, F_SETFL, val);
334 return connect_success;
335 }
336
337 /* If connect is in progress then return 1 else it's real error. */
338 if (ret < 0)
339 {
340 if (errno != EINPROGRESS)
341 {
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400342 char str[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +0000343 zlog_info ("can't connect to %s fd %d : %s",
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400344 sockunion_log (&su, str, sizeof str),
345 fd, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000346 return connect_error;
347 }
348 }
349
350 fcntl (fd, F_SETFL, val);
351
352 return connect_in_progress;
353}
354
355/* Make socket from sockunion union. */
356int
357sockunion_stream_socket (union sockunion *su)
358{
359 int sock;
360
361 if (su->sa.sa_family == 0)
362 su->sa.sa_family = AF_INET_UNION;
363
364 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
365
366 if (sock < 0)
367 zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket");
368
369 return sock;
370}
371
372/* Bind socket to specified address. */
373int
374sockunion_bind (int sock, union sockunion *su, unsigned short port,
375 union sockunion *su_addr)
376{
377 int size = 0;
378 int ret;
379
380 if (su->sa.sa_family == AF_INET)
381 {
382 size = sizeof (struct sockaddr_in);
383 su->sin.sin_port = htons (port);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000384#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000385 su->sin.sin_len = size;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000386#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000387 if (su_addr == NULL)
Jorge Boncompte [DTI2]12829322012-04-10 16:57:25 +0200388 sockunion2ip (su) = htonl (INADDR_ANY);
paul718e3742002-12-13 20:15:29 +0000389 }
390#ifdef HAVE_IPV6
391 else if (su->sa.sa_family == AF_INET6)
392 {
393 size = sizeof (struct sockaddr_in6);
394 su->sin6.sin6_port = htons (port);
395#ifdef SIN6_LEN
396 su->sin6.sin6_len = size;
397#endif /* SIN6_LEN */
398 if (su_addr == NULL)
399 {
David Lamparter6d6df302014-06-28 21:12:37 +0200400#ifdef LINUX_IPV6
paul718e3742002-12-13 20:15:29 +0000401 memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr));
402#else
403 su->sin6.sin6_addr = in6addr_any;
404#endif /* LINUX_IPV6 */
405 }
406 }
407#endif /* HAVE_IPV6 */
408
409
410 ret = bind (sock, (struct sockaddr *)su, size);
411 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +0000412 zlog (NULL, LOG_WARNING, "can't bind socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000413
414 return ret;
415}
416
417int
418sockopt_reuseaddr (int sock)
419{
420 int ret;
421 int on = 1;
422
423 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
424 (void *) &on, sizeof (on));
425 if (ret < 0)
426 {
427 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock);
428 return -1;
429 }
430 return 0;
431}
432
433#ifdef SO_REUSEPORT
434int
435sockopt_reuseport (int sock)
436{
437 int ret;
438 int on = 1;
439
440 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT,
441 (void *) &on, sizeof (on));
442 if (ret < 0)
443 {
hassoe7fe8c82005-05-06 19:33:35 +0000444 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEPORT to socket %d", sock);
paul718e3742002-12-13 20:15:29 +0000445 return -1;
446 }
447 return 0;
448}
449#else
450int
451sockopt_reuseport (int sock)
452{
453 return 0;
454}
455#endif /* 0 */
456
457int
458sockopt_ttl (int family, int sock, int ttl)
459{
460 int ret;
461
462#ifdef IP_TTL
463 if (family == AF_INET)
464 {
465 ret = setsockopt (sock, IPPROTO_IP, IP_TTL,
466 (void *) &ttl, sizeof (int));
467 if (ret < 0)
468 {
469 zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock);
470 return -1;
471 }
472 return 0;
473 }
474#endif /* IP_TTL */
475#ifdef HAVE_IPV6
476 if (family == AF_INET6)
477 {
478 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
479 (void *) &ttl, sizeof (int));
480 if (ret < 0)
481 {
482 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
483 ttl, sock);
484 return -1;
485 }
486 return 0;
487 }
488#endif /* HAVE_IPV6 */
489 return 0;
490}
491
Stephen Hemminger58192df2010-08-05 10:26:24 -0700492int
493sockopt_cork (int sock, int onoff)
494{
495#ifdef TCP_CORK
496 return setsockopt (sock, IPPROTO_TCP, TCP_CORK, &onoff, sizeof(onoff));
497#else
498 return 0;
499#endif
500}
501
Nick Hilliardfa411a22011-03-23 15:33:17 +0000502int
503sockopt_minttl (int family, int sock, int minttl)
504{
Stephen Hemminger89b6d1f2011-03-24 10:51:59 +0000505#ifdef IP_MINTTL
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700506 if (family == AF_INET)
Nick Hilliardfa411a22011-03-23 15:33:17 +0000507 {
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700508 int ret = setsockopt (sock, IPPROTO_IP, IP_MINTTL, &minttl, sizeof(minttl));
509 if (ret < 0)
510 zlog (NULL, LOG_WARNING,
511 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
512 minttl, sock, safe_strerror (errno));
513 return ret;
Nick Hilliardfa411a22011-03-23 15:33:17 +0000514 }
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700515#endif /* IP_MINTTL */
David Lamparter05a69d22015-05-27 22:15:37 +0200516#ifdef IPV6_MINHOPCNT
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700517 if (family == AF_INET6)
518 {
David Lamparter05a69d22015-05-27 22:15:37 +0200519 int ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MINHOPCNT, &minttl, sizeof(minttl));
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700520 if (ret < 0)
521 zlog (NULL, LOG_WARNING,
David Lamparter05a69d22015-05-27 22:15:37 +0200522 "can't set sockopt IPV6_MINHOPCNT to %d on socket %d: %s",
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700523 minttl, sock, safe_strerror (errno));
524 return ret;
525 }
526#endif
Nick Hilliardfa411a22011-03-23 15:33:17 +0000527
Stephen Hemminger89b6d1f2011-03-24 10:51:59 +0000528 errno = EOPNOTSUPP;
529 return -1;
Nick Hilliardfa411a22011-03-23 15:33:17 +0000530}
531
David Lamparterca051262009-10-04 16:21:49 +0200532int
533sockopt_v6only (int family, int sock)
534{
535 int ret, on = 1;
536
537#ifdef HAVE_IPV6
538#ifdef IPV6_V6ONLY
539 if (family == AF_INET6)
540 {
541 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
542 (void *) &on, sizeof (int));
543 if (ret < 0)
544 {
545 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_V6ONLY "
546 "to socket %d", sock);
547 return -1;
548 }
549 return 0;
550 }
551#endif /* IPV6_V6ONLY */
552#endif /* HAVE_IPV6 */
553 return 0;
554}
555
paul718e3742002-12-13 20:15:29 +0000556/* If same family and same prefix return 1. */
557int
Timo Teräs81b139b2015-04-29 09:43:01 +0300558sockunion_same (const union sockunion *su1, const union sockunion *su2)
paul718e3742002-12-13 20:15:29 +0000559{
560 int ret = 0;
561
562 if (su1->sa.sa_family != su2->sa.sa_family)
563 return 0;
564
565 switch (su1->sa.sa_family)
566 {
567 case AF_INET:
568 ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr,
569 sizeof (struct in_addr));
570 break;
571#ifdef HAVE_IPV6
572 case AF_INET6:
573 ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
574 sizeof (struct in6_addr));
575 break;
576#endif /* HAVE_IPV6 */
577 }
578 if (ret == 0)
579 return 1;
580 else
581 return 0;
582}
583
Timo Teräs9196caf2015-04-29 09:43:05 +0300584unsigned int
585sockunion_hash (const union sockunion *su)
586{
587 switch (sockunion_family(su))
588 {
589 case AF_INET:
590 return jhash_1word(su->sin.sin_addr.s_addr, 0);
591#ifdef HAVE_IPV6
592 case AF_INET6:
593 return jhash2(su->sin6.sin6_addr.s6_addr32, ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0);
594#endif /* HAVE_IPV6 */
595 }
596 return 0;
597}
598
Timo Teräs483abc02015-05-22 13:40:59 +0300599size_t
600family2addrsize(int family)
601{
602 switch (family)
603 {
604 case AF_INET:
605 return sizeof(struct in_addr);
606#ifdef HAVE_IPV6
607 case AF_INET6:
608 return sizeof(struct in6_addr);
609#endif /* HAVE_IPV6 */
610 }
611 return 0;
612}
613
614size_t
615sockunion_get_addrlen(const union sockunion *su)
616{
617 return family2addrsize(sockunion_family(su));
618}
619
620const u_char *
621sockunion_get_addr(const union sockunion *su)
622{
623 switch (sockunion_family(su))
624 {
625 case AF_INET:
626 return (const u_char *) &su->sin.sin_addr.s_addr;
627#ifdef HAVE_IPV6
628 case AF_INET6:
629 return (const u_char *) &su->sin6.sin6_addr;
630#endif /* HAVE_IPV6 */
631 }
632 return NULL;
633}
634
635void
636sockunion_set(union sockunion *su, int family, const u_char *addr, size_t bytes)
637{
638 if (family2addrsize(family) != bytes)
639 return;
640
641 sockunion_family(su) = family;
642 switch (family)
643 {
644 case AF_INET:
645 memcpy(&su->sin.sin_addr.s_addr, addr, bytes);
646 break;
647#ifdef HAVE_IPV6
648 case AF_INET6:
649 memcpy(&su->sin6.sin6_addr, addr, bytes);
650 break;
651#endif /* HAVE_IPV6 */
652 }
653}
654
paul718e3742002-12-13 20:15:29 +0000655/* After TCP connection is established. Get local address and port. */
656union sockunion *
657sockunion_getsockname (int fd)
658{
659 int ret;
paul22528292004-05-08 05:10:38 +0000660 socklen_t len;
paul718e3742002-12-13 20:15:29 +0000661 union
662 {
663 struct sockaddr sa;
664 struct sockaddr_in sin;
665#ifdef HAVE_IPV6
666 struct sockaddr_in6 sin6;
667#endif /* HAVE_IPV6 */
668 char tmp_buffer[128];
669 } name;
670 union sockunion *su;
671
672 memset (&name, 0, sizeof name);
673 len = sizeof name;
674
675 ret = getsockname (fd, (struct sockaddr *)&name, &len);
676 if (ret < 0)
677 {
678 zlog_warn ("Can't get local address and port by getsockname: %s",
ajs6099b3b2004-11-20 02:06:59 +0000679 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000680 return NULL;
681 }
682
683 if (name.sa.sa_family == AF_INET)
684 {
paul2ba9a372005-05-19 01:37:50 +0000685 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000686 memcpy (su, &name, sizeof (struct sockaddr_in));
687 return su;
688 }
689#ifdef HAVE_IPV6
690 if (name.sa.sa_family == AF_INET6)
691 {
paul2ba9a372005-05-19 01:37:50 +0000692 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000693 memcpy (su, &name, sizeof (struct sockaddr_in6));
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100694 sockunion_normalise_mapped (su);
paul718e3742002-12-13 20:15:29 +0000695 return su;
696 }
697#endif /* HAVE_IPV6 */
698 return NULL;
699}
700
701/* After TCP connection is established. Get remote address and port. */
702union sockunion *
703sockunion_getpeername (int fd)
704{
705 int ret;
paul22528292004-05-08 05:10:38 +0000706 socklen_t len;
paul718e3742002-12-13 20:15:29 +0000707 union
708 {
709 struct sockaddr sa;
710 struct sockaddr_in sin;
711#ifdef HAVE_IPV6
712 struct sockaddr_in6 sin6;
713#endif /* HAVE_IPV6 */
714 char tmp_buffer[128];
715 } name;
716 union sockunion *su;
717
718 memset (&name, 0, sizeof name);
719 len = sizeof name;
720 ret = getpeername (fd, (struct sockaddr *)&name, &len);
721 if (ret < 0)
722 {
723 zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s",
ajs6099b3b2004-11-20 02:06:59 +0000724 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000725 return NULL;
726 }
727
728 if (name.sa.sa_family == AF_INET)
729 {
paul2ba9a372005-05-19 01:37:50 +0000730 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000731 memcpy (su, &name, sizeof (struct sockaddr_in));
732 return su;
733 }
734#ifdef HAVE_IPV6
735 if (name.sa.sa_family == AF_INET6)
736 {
paul2ba9a372005-05-19 01:37:50 +0000737 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000738 memcpy (su, &name, sizeof (struct sockaddr_in6));
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100739 sockunion_normalise_mapped (su);
paul718e3742002-12-13 20:15:29 +0000740 return su;
741 }
742#endif /* HAVE_IPV6 */
743 return NULL;
744}
745
746/* Print sockunion structure */
paul8cc41982005-05-06 21:25:49 +0000747static void __attribute__ ((unused))
Timo Teräs81b139b2015-04-29 09:43:01 +0300748sockunion_print (const union sockunion *su)
paul718e3742002-12-13 20:15:29 +0000749{
750 if (su == NULL)
751 return;
752
753 switch (su->sa.sa_family)
754 {
755 case AF_INET:
756 printf ("%s\n", inet_ntoa (su->sin.sin_addr));
757 break;
758#ifdef HAVE_IPV6
759 case AF_INET6:
760 {
paul42d49862004-10-13 05:22:18 +0000761 char buf [SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +0000762
763 printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr),
764 buf, sizeof (buf)));
765 }
766 break;
767#endif /* HAVE_IPV6 */
768
769#ifdef AF_LINK
770 case AF_LINK:
771 {
772 struct sockaddr_dl *sdl;
773
774 sdl = (struct sockaddr_dl *)&(su->sa);
775 printf ("link#%d\n", sdl->sdl_index);
776 }
777 break;
778#endif /* AF_LINK */
779 default:
780 printf ("af_unknown %d\n", su->sa.sa_family);
781 break;
782 }
783}
784
785#ifdef HAVE_IPV6
paul8cc41982005-05-06 21:25:49 +0000786static int
Timo Teräs81b139b2015-04-29 09:43:01 +0300787in6addr_cmp (const struct in6_addr *addr1, const struct in6_addr *addr2)
paul718e3742002-12-13 20:15:29 +0000788{
hasso8c328f12004-10-05 21:01:23 +0000789 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000790 u_char *p1, *p2;
791
792 p1 = (u_char *)addr1;
793 p2 = (u_char *)addr2;
794
795 for (i = 0; i < sizeof (struct in6_addr); i++)
796 {
797 if (p1[i] > p2[i])
798 return 1;
799 else if (p1[i] < p2[i])
800 return -1;
801 }
802 return 0;
803}
804#endif /* HAVE_IPV6 */
805
806int
Timo Teräs81b139b2015-04-29 09:43:01 +0300807sockunion_cmp (const union sockunion *su1, const union sockunion *su2)
paul718e3742002-12-13 20:15:29 +0000808{
809 if (su1->sa.sa_family > su2->sa.sa_family)
810 return 1;
811 if (su1->sa.sa_family < su2->sa.sa_family)
812 return -1;
813
814 if (su1->sa.sa_family == AF_INET)
815 {
Jorge Boncompte [DTI2]12829322012-04-10 16:57:25 +0200816 if (ntohl (sockunion2ip (su1)) == ntohl (sockunion2ip (su2)))
paul718e3742002-12-13 20:15:29 +0000817 return 0;
Jorge Boncompte [DTI2]12829322012-04-10 16:57:25 +0200818 if (ntohl (sockunion2ip (su1)) > ntohl (sockunion2ip (su2)))
paul718e3742002-12-13 20:15:29 +0000819 return 1;
820 else
821 return -1;
822 }
823#ifdef HAVE_IPV6
824 if (su1->sa.sa_family == AF_INET6)
825 return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
826#endif /* HAVE_IPV6 */
827 return 0;
828}
829
830/* Duplicate sockunion. */
831union sockunion *
Timo Teräs81b139b2015-04-29 09:43:01 +0300832sockunion_dup (const union sockunion *su)
paul718e3742002-12-13 20:15:29 +0000833{
834 union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
835 memcpy (dup, su, sizeof (union sockunion));
836 return dup;
837}
838
839void
840sockunion_free (union sockunion *su)
841{
842 XFREE (MTYPE_SOCKUNION, su);
843}