blob: c7315f28b35d7e9665e997c101654aa1fd9e11ee [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{
169 if (su->sa.sa_family == AF_INET)
170 return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len);
171#ifdef HAVE_IPV6
172 else if (su->sa.sa_family == AF_INET6)
173 return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len);
174#endif /* HAVE_IPV6 */
175 return NULL;
176}
177
Paul Jakma2fb2a452012-06-14 10:37:40 +0100178union sockunion *
179sockunion_str2su (const char *str)
180{
181 union sockunion *su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
182
183 if (!str2sockunion (str, su))
184 return su;
185
186 XFREE (MTYPE_SOCKUNION, su);
187 return NULL;
188}
189
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100190/* Convert IPv4 compatible IPv6 address to IPv4 address. */
191static void
192sockunion_normalise_mapped (union sockunion *su)
193{
194 struct sockaddr_in sin;
195
196#ifdef HAVE_IPV6
197 if (su->sa.sa_family == AF_INET6
198 && IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
199 {
200 memset (&sin, 0, sizeof (struct sockaddr_in));
201 sin.sin_family = AF_INET;
Stephen Hemminger3fa3f952009-07-11 21:27:51 -0700202 sin.sin_port = su->sin6.sin6_port;
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100203 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
204 memcpy (su, &sin, sizeof (struct sockaddr_in));
205 }
206#endif /* HAVE_IPV6 */
207}
208
paul718e3742002-12-13 20:15:29 +0000209/* Return socket of sockunion. */
210int
Timo Teräs81b139b2015-04-29 09:43:01 +0300211sockunion_socket (const union sockunion *su)
paul718e3742002-12-13 20:15:29 +0000212{
213 int sock;
214
215 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
216 if (sock < 0)
217 {
ajs6099b3b2004-11-20 02:06:59 +0000218 zlog (NULL, LOG_WARNING, "Can't make socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000219 return -1;
220 }
221
222 return sock;
223}
224
225/* Return accepted new socket file descriptor. */
226int
227sockunion_accept (int sock, union sockunion *su)
228{
229 socklen_t len;
230 int client_sock;
231
232 len = sizeof (union sockunion);
233 client_sock = accept (sock, (struct sockaddr *) su, &len);
234
Paul Jakma84152ee2008-11-24 22:25:16 +0000235 sockunion_normalise_mapped (su);
paul718e3742002-12-13 20:15:29 +0000236 return client_sock;
237}
238
239/* Return sizeof union sockunion. */
paul8cc41982005-05-06 21:25:49 +0000240static int
Timo Teräs81b139b2015-04-29 09:43:01 +0300241sockunion_sizeof (const union sockunion *su)
paul718e3742002-12-13 20:15:29 +0000242{
243 int ret;
244
245 ret = 0;
246 switch (su->sa.sa_family)
247 {
248 case AF_INET:
249 ret = sizeof (struct sockaddr_in);
250 break;
251#ifdef HAVE_IPV6
252 case AF_INET6:
253 ret = sizeof (struct sockaddr_in6);
254 break;
255#endif /* AF_INET6 */
256 }
257 return ret;
258}
259
260/* return sockunion structure : this function should be revised. */
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400261static const char *
Timo Teräs81b139b2015-04-29 09:43:01 +0300262sockunion_log (const union sockunion *su, char *buf, size_t len)
paul718e3742002-12-13 20:15:29 +0000263{
paul718e3742002-12-13 20:15:29 +0000264 switch (su->sa.sa_family)
265 {
266 case AF_INET:
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400267 return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len);
268
paul718e3742002-12-13 20:15:29 +0000269#ifdef HAVE_IPV6
270 case AF_INET6:
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400271 return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len);
paul718e3742002-12-13 20:15:29 +0000272 break;
273#endif /* HAVE_IPV6 */
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400274
paul718e3742002-12-13 20:15:29 +0000275 default:
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400276 snprintf (buf, len, "af_unknown %d ", su->sa.sa_family);
277 return buf;
paul718e3742002-12-13 20:15:29 +0000278 }
paul718e3742002-12-13 20:15:29 +0000279}
280
281/* sockunion_connect returns
282 -1 : error occured
283 0 : connect success
284 1 : connect is in progress */
285enum connect_result
Timo Teräs81b139b2015-04-29 09:43:01 +0300286sockunion_connect (int fd, const union sockunion *peersu, unsigned short port,
paul718e3742002-12-13 20:15:29 +0000287 unsigned int ifindex)
288{
289 int ret;
290 int val;
291 union sockunion su;
292
293 memcpy (&su, peersu, sizeof (union sockunion));
294
295 switch (su.sa.sa_family)
296 {
297 case AF_INET:
298 su.sin.sin_port = port;
299 break;
300#ifdef HAVE_IPV6
301 case AF_INET6:
302 su.sin6.sin6_port = port;
303#ifdef KAME
304 if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex)
305 {
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000306#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
paul718e3742002-12-13 20:15:29 +0000307 /* su.sin6.sin6_scope_id = ifindex; */
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000308#endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */
paul718e3742002-12-13 20:15:29 +0000309 SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex);
paul718e3742002-12-13 20:15:29 +0000310 }
311#endif /* KAME */
312 break;
313#endif /* HAVE_IPV6 */
314 }
315
316 /* Make socket non-block. */
317 val = fcntl (fd, F_GETFL, 0);
318 fcntl (fd, F_SETFL, val|O_NONBLOCK);
319
320 /* Call connect function. */
321 ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su));
322
323 /* Immediate success */
324 if (ret == 0)
325 {
326 fcntl (fd, F_SETFL, val);
327 return connect_success;
328 }
329
330 /* If connect is in progress then return 1 else it's real error. */
331 if (ret < 0)
332 {
333 if (errno != EINPROGRESS)
334 {
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400335 char str[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +0000336 zlog_info ("can't connect to %s fd %d : %s",
Stephen Hemmingerb24b19f2011-12-06 14:09:18 +0400337 sockunion_log (&su, str, sizeof str),
338 fd, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000339 return connect_error;
340 }
341 }
342
343 fcntl (fd, F_SETFL, val);
344
345 return connect_in_progress;
346}
347
348/* Make socket from sockunion union. */
349int
350sockunion_stream_socket (union sockunion *su)
351{
352 int sock;
353
354 if (su->sa.sa_family == 0)
355 su->sa.sa_family = AF_INET_UNION;
356
357 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
358
359 if (sock < 0)
360 zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket");
361
362 return sock;
363}
364
365/* Bind socket to specified address. */
366int
367sockunion_bind (int sock, union sockunion *su, unsigned short port,
368 union sockunion *su_addr)
369{
370 int size = 0;
371 int ret;
372
373 if (su->sa.sa_family == AF_INET)
374 {
375 size = sizeof (struct sockaddr_in);
376 su->sin.sin_port = htons (port);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000377#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000378 su->sin.sin_len = size;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000379#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000380 if (su_addr == NULL)
Jorge Boncompte [DTI2]12829322012-04-10 16:57:25 +0200381 sockunion2ip (su) = htonl (INADDR_ANY);
paul718e3742002-12-13 20:15:29 +0000382 }
383#ifdef HAVE_IPV6
384 else if (su->sa.sa_family == AF_INET6)
385 {
386 size = sizeof (struct sockaddr_in6);
387 su->sin6.sin6_port = htons (port);
388#ifdef SIN6_LEN
389 su->sin6.sin6_len = size;
390#endif /* SIN6_LEN */
391 if (su_addr == NULL)
392 {
David Lamparter6d6df302014-06-28 21:12:37 +0200393#ifdef LINUX_IPV6
paul718e3742002-12-13 20:15:29 +0000394 memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr));
395#else
396 su->sin6.sin6_addr = in6addr_any;
397#endif /* LINUX_IPV6 */
398 }
399 }
400#endif /* HAVE_IPV6 */
401
402
403 ret = bind (sock, (struct sockaddr *)su, size);
404 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +0000405 zlog (NULL, LOG_WARNING, "can't bind socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000406
407 return ret;
408}
409
410int
411sockopt_reuseaddr (int sock)
412{
413 int ret;
414 int on = 1;
415
416 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
417 (void *) &on, sizeof (on));
418 if (ret < 0)
419 {
420 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock);
421 return -1;
422 }
423 return 0;
424}
425
426#ifdef SO_REUSEPORT
427int
428sockopt_reuseport (int sock)
429{
430 int ret;
431 int on = 1;
432
433 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT,
434 (void *) &on, sizeof (on));
435 if (ret < 0)
436 {
hassoe7fe8c82005-05-06 19:33:35 +0000437 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEPORT to socket %d", sock);
paul718e3742002-12-13 20:15:29 +0000438 return -1;
439 }
440 return 0;
441}
442#else
443int
444sockopt_reuseport (int sock)
445{
446 return 0;
447}
448#endif /* 0 */
449
450int
451sockopt_ttl (int family, int sock, int ttl)
452{
453 int ret;
454
455#ifdef IP_TTL
456 if (family == AF_INET)
457 {
458 ret = setsockopt (sock, IPPROTO_IP, IP_TTL,
459 (void *) &ttl, sizeof (int));
460 if (ret < 0)
461 {
462 zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock);
463 return -1;
464 }
465 return 0;
466 }
467#endif /* IP_TTL */
468#ifdef HAVE_IPV6
469 if (family == AF_INET6)
470 {
471 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
472 (void *) &ttl, sizeof (int));
473 if (ret < 0)
474 {
475 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
476 ttl, sock);
477 return -1;
478 }
479 return 0;
480 }
481#endif /* HAVE_IPV6 */
482 return 0;
483}
484
Stephen Hemminger58192df2010-08-05 10:26:24 -0700485int
486sockopt_cork (int sock, int onoff)
487{
488#ifdef TCP_CORK
489 return setsockopt (sock, IPPROTO_TCP, TCP_CORK, &onoff, sizeof(onoff));
490#else
491 return 0;
492#endif
493}
494
Nick Hilliardfa411a22011-03-23 15:33:17 +0000495int
496sockopt_minttl (int family, int sock, int minttl)
497{
Stephen Hemminger89b6d1f2011-03-24 10:51:59 +0000498#ifdef IP_MINTTL
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700499 if (family == AF_INET)
Nick Hilliardfa411a22011-03-23 15:33:17 +0000500 {
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700501 int ret = setsockopt (sock, IPPROTO_IP, IP_MINTTL, &minttl, sizeof(minttl));
502 if (ret < 0)
503 zlog (NULL, LOG_WARNING,
504 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
505 minttl, sock, safe_strerror (errno));
506 return ret;
Nick Hilliardfa411a22011-03-23 15:33:17 +0000507 }
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700508#endif /* IP_MINTTL */
David Lamparter05a69d22015-05-27 22:15:37 +0200509#ifdef IPV6_MINHOPCNT
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700510 if (family == AF_INET6)
511 {
David Lamparter05a69d22015-05-27 22:15:37 +0200512 int ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MINHOPCNT, &minttl, sizeof(minttl));
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700513 if (ret < 0)
514 zlog (NULL, LOG_WARNING,
David Lamparter05a69d22015-05-27 22:15:37 +0200515 "can't set sockopt IPV6_MINHOPCNT to %d on socket %d: %s",
Stephen Hemmingerd876bdf2010-08-05 10:26:27 -0700516 minttl, sock, safe_strerror (errno));
517 return ret;
518 }
519#endif
Nick Hilliardfa411a22011-03-23 15:33:17 +0000520
Stephen Hemminger89b6d1f2011-03-24 10:51:59 +0000521 errno = EOPNOTSUPP;
522 return -1;
Nick Hilliardfa411a22011-03-23 15:33:17 +0000523}
524
David Lamparterca051262009-10-04 16:21:49 +0200525int
526sockopt_v6only (int family, int sock)
527{
528 int ret, on = 1;
529
530#ifdef HAVE_IPV6
531#ifdef IPV6_V6ONLY
532 if (family == AF_INET6)
533 {
534 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
535 (void *) &on, sizeof (int));
536 if (ret < 0)
537 {
538 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_V6ONLY "
539 "to socket %d", sock);
540 return -1;
541 }
542 return 0;
543 }
544#endif /* IPV6_V6ONLY */
545#endif /* HAVE_IPV6 */
546 return 0;
547}
548
paul718e3742002-12-13 20:15:29 +0000549/* If same family and same prefix return 1. */
550int
Timo Teräs81b139b2015-04-29 09:43:01 +0300551sockunion_same (const union sockunion *su1, const union sockunion *su2)
paul718e3742002-12-13 20:15:29 +0000552{
553 int ret = 0;
554
555 if (su1->sa.sa_family != su2->sa.sa_family)
556 return 0;
557
558 switch (su1->sa.sa_family)
559 {
560 case AF_INET:
561 ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr,
562 sizeof (struct in_addr));
563 break;
564#ifdef HAVE_IPV6
565 case AF_INET6:
566 ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
567 sizeof (struct in6_addr));
568 break;
569#endif /* HAVE_IPV6 */
570 }
571 if (ret == 0)
572 return 1;
573 else
574 return 0;
575}
576
Timo Teräs9196caf2015-04-29 09:43:05 +0300577unsigned int
578sockunion_hash (const union sockunion *su)
579{
580 switch (sockunion_family(su))
581 {
582 case AF_INET:
583 return jhash_1word(su->sin.sin_addr.s_addr, 0);
584#ifdef HAVE_IPV6
585 case AF_INET6:
586 return jhash2(su->sin6.sin6_addr.s6_addr32, ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0);
587#endif /* HAVE_IPV6 */
588 }
589 return 0;
590}
591
paul718e3742002-12-13 20:15:29 +0000592/* After TCP connection is established. Get local address and port. */
593union sockunion *
594sockunion_getsockname (int fd)
595{
596 int ret;
paul22528292004-05-08 05:10:38 +0000597 socklen_t len;
paul718e3742002-12-13 20:15:29 +0000598 union
599 {
600 struct sockaddr sa;
601 struct sockaddr_in sin;
602#ifdef HAVE_IPV6
603 struct sockaddr_in6 sin6;
604#endif /* HAVE_IPV6 */
605 char tmp_buffer[128];
606 } name;
607 union sockunion *su;
608
609 memset (&name, 0, sizeof name);
610 len = sizeof name;
611
612 ret = getsockname (fd, (struct sockaddr *)&name, &len);
613 if (ret < 0)
614 {
615 zlog_warn ("Can't get local address and port by getsockname: %s",
ajs6099b3b2004-11-20 02:06:59 +0000616 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000617 return NULL;
618 }
619
620 if (name.sa.sa_family == AF_INET)
621 {
paul2ba9a372005-05-19 01:37:50 +0000622 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000623 memcpy (su, &name, sizeof (struct sockaddr_in));
624 return su;
625 }
626#ifdef HAVE_IPV6
627 if (name.sa.sa_family == AF_INET6)
628 {
paul2ba9a372005-05-19 01:37:50 +0000629 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000630 memcpy (su, &name, sizeof (struct sockaddr_in6));
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100631 sockunion_normalise_mapped (su);
paul718e3742002-12-13 20:15:29 +0000632 return su;
633 }
634#endif /* HAVE_IPV6 */
635 return NULL;
636}
637
638/* After TCP connection is established. Get remote address and port. */
639union sockunion *
640sockunion_getpeername (int fd)
641{
642 int ret;
paul22528292004-05-08 05:10:38 +0000643 socklen_t len;
paul718e3742002-12-13 20:15:29 +0000644 union
645 {
646 struct sockaddr sa;
647 struct sockaddr_in sin;
648#ifdef HAVE_IPV6
649 struct sockaddr_in6 sin6;
650#endif /* HAVE_IPV6 */
651 char tmp_buffer[128];
652 } name;
653 union sockunion *su;
654
655 memset (&name, 0, sizeof name);
656 len = sizeof name;
657 ret = getpeername (fd, (struct sockaddr *)&name, &len);
658 if (ret < 0)
659 {
660 zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s",
ajs6099b3b2004-11-20 02:06:59 +0000661 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000662 return NULL;
663 }
664
665 if (name.sa.sa_family == AF_INET)
666 {
paul2ba9a372005-05-19 01:37:50 +0000667 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000668 memcpy (su, &name, sizeof (struct sockaddr_in));
669 return su;
670 }
671#ifdef HAVE_IPV6
672 if (name.sa.sa_family == AF_INET6)
673 {
paul2ba9a372005-05-19 01:37:50 +0000674 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000675 memcpy (su, &name, sizeof (struct sockaddr_in6));
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100676 sockunion_normalise_mapped (su);
paul718e3742002-12-13 20:15:29 +0000677 return su;
678 }
679#endif /* HAVE_IPV6 */
680 return NULL;
681}
682
683/* Print sockunion structure */
paul8cc41982005-05-06 21:25:49 +0000684static void __attribute__ ((unused))
Timo Teräs81b139b2015-04-29 09:43:01 +0300685sockunion_print (const union sockunion *su)
paul718e3742002-12-13 20:15:29 +0000686{
687 if (su == NULL)
688 return;
689
690 switch (su->sa.sa_family)
691 {
692 case AF_INET:
693 printf ("%s\n", inet_ntoa (su->sin.sin_addr));
694 break;
695#ifdef HAVE_IPV6
696 case AF_INET6:
697 {
paul42d49862004-10-13 05:22:18 +0000698 char buf [SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +0000699
700 printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr),
701 buf, sizeof (buf)));
702 }
703 break;
704#endif /* HAVE_IPV6 */
705
706#ifdef AF_LINK
707 case AF_LINK:
708 {
709 struct sockaddr_dl *sdl;
710
711 sdl = (struct sockaddr_dl *)&(su->sa);
712 printf ("link#%d\n", sdl->sdl_index);
713 }
714 break;
715#endif /* AF_LINK */
716 default:
717 printf ("af_unknown %d\n", su->sa.sa_family);
718 break;
719 }
720}
721
722#ifdef HAVE_IPV6
paul8cc41982005-05-06 21:25:49 +0000723static int
Timo Teräs81b139b2015-04-29 09:43:01 +0300724in6addr_cmp (const struct in6_addr *addr1, const struct in6_addr *addr2)
paul718e3742002-12-13 20:15:29 +0000725{
hasso8c328f12004-10-05 21:01:23 +0000726 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000727 u_char *p1, *p2;
728
729 p1 = (u_char *)addr1;
730 p2 = (u_char *)addr2;
731
732 for (i = 0; i < sizeof (struct in6_addr); i++)
733 {
734 if (p1[i] > p2[i])
735 return 1;
736 else if (p1[i] < p2[i])
737 return -1;
738 }
739 return 0;
740}
741#endif /* HAVE_IPV6 */
742
743int
Timo Teräs81b139b2015-04-29 09:43:01 +0300744sockunion_cmp (const union sockunion *su1, const union sockunion *su2)
paul718e3742002-12-13 20:15:29 +0000745{
746 if (su1->sa.sa_family > su2->sa.sa_family)
747 return 1;
748 if (su1->sa.sa_family < su2->sa.sa_family)
749 return -1;
750
751 if (su1->sa.sa_family == AF_INET)
752 {
Jorge Boncompte [DTI2]12829322012-04-10 16:57:25 +0200753 if (ntohl (sockunion2ip (su1)) == ntohl (sockunion2ip (su2)))
paul718e3742002-12-13 20:15:29 +0000754 return 0;
Jorge Boncompte [DTI2]12829322012-04-10 16:57:25 +0200755 if (ntohl (sockunion2ip (su1)) > ntohl (sockunion2ip (su2)))
paul718e3742002-12-13 20:15:29 +0000756 return 1;
757 else
758 return -1;
759 }
760#ifdef HAVE_IPV6
761 if (su1->sa.sa_family == AF_INET6)
762 return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
763#endif /* HAVE_IPV6 */
764 return 0;
765}
766
767/* Duplicate sockunion. */
768union sockunion *
Timo Teräs81b139b2015-04-29 09:43:01 +0300769sockunion_dup (const union sockunion *su)
paul718e3742002-12-13 20:15:29 +0000770{
771 union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
772 memcpy (dup, su, sizeof (union sockunion));
773 return dup;
774}
775
776void
777sockunion_free (union sockunion *su)
778{
779 XFREE (MTYPE_SOCKUNION, su);
780}