blob: 8fbe345026f431664b45de7f1df65c30e5b6259c [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"
30
31#ifndef HAVE_INET_ATON
32int
33inet_aton (const char *cp, struct in_addr *inaddr)
34{
35 int dots = 0;
36 register u_long addr = 0;
37 register u_long val = 0, base = 10;
38
39 do
40 {
41 register char c = *cp;
42
43 switch (c)
44 {
45 case '0': case '1': case '2': case '3': case '4': case '5':
46 case '6': case '7': case '8': case '9':
47 val = (val * base) + (c - '0');
48 break;
49 case '.':
50 if (++dots > 3)
51 return 0;
52 case '\0':
53 if (val > 255)
54 return 0;
55 addr = addr << 8 | val;
56 val = 0;
57 break;
58 default:
59 return 0;
60 }
61 } while (*cp++) ;
62
63 if (dots < 3)
64 addr <<= 8 * (3 - dots);
65 if (inaddr)
66 inaddr->s_addr = htonl (addr);
67 return 1;
68}
69#endif /* ! HAVE_INET_ATON */
70
71
72#ifndef HAVE_INET_PTON
73int
74inet_pton (int family, const char *strptr, void *addrptr)
75{
76 if (family == AF_INET)
77 {
78 struct in_addr in_val;
79
80 if (inet_aton (strptr, &in_val))
81 {
82 memcpy (addrptr, &in_val, sizeof (struct in_addr));
83 return 1;
84 }
85 return 0;
86 }
87 errno = EAFNOSUPPORT;
88 return -1;
89}
90#endif /* ! HAVE_INET_PTON */
91
92#ifndef HAVE_INET_NTOP
93const char *
94inet_ntop (int family, const void *addrptr, char *strptr, size_t len)
95{
96 unsigned char *p = (unsigned char *) addrptr;
97
98 if (family == AF_INET)
99 {
100 char temp[INET_ADDRSTRLEN];
101
102 snprintf(temp, sizeof(temp), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
103
104 if (strlen(temp) >= len)
105 {
106 errno = ENOSPC;
107 return NULL;
108 }
109 strcpy(strptr, temp);
110 return strptr;
111 }
112
113 errno = EAFNOSUPPORT;
114 return NULL;
115}
116#endif /* ! HAVE_INET_NTOP */
117
118const char *
119inet_sutop (union sockunion *su, char *str)
120{
121 switch (su->sa.sa_family)
122 {
123 case AF_INET:
124 inet_ntop (AF_INET, &su->sin.sin_addr, str, INET_ADDRSTRLEN);
125 break;
126#ifdef HAVE_IPV6
127 case AF_INET6:
128 inet_ntop (AF_INET6, &su->sin6.sin6_addr, str, INET6_ADDRSTRLEN);
129 break;
130#endif /* HAVE_IPV6 */
131 }
132 return str;
133}
134
135int
hassoa1494112004-10-11 12:53:17 +0000136str2sockunion (const char *str, union sockunion *su)
paul718e3742002-12-13 20:15:29 +0000137{
138 int ret;
139
140 memset (su, 0, sizeof (union sockunion));
141
142 ret = inet_pton (AF_INET, str, &su->sin.sin_addr);
143 if (ret > 0) /* Valid IPv4 address format. */
144 {
145 su->sin.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000146#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000147 su->sin.sin_len = sizeof(struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000148#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000149 return 0;
150 }
151#ifdef HAVE_IPV6
152 ret = inet_pton (AF_INET6, str, &su->sin6.sin6_addr);
153 if (ret > 0) /* Valid IPv6 address format. */
154 {
155 su->sin6.sin6_family = AF_INET6;
156#ifdef SIN6_LEN
157 su->sin6.sin6_len = sizeof(struct sockaddr_in6);
158#endif /* SIN6_LEN */
159 return 0;
160 }
161#endif /* HAVE_IPV6 */
162 return -1;
163}
164
165const char *
166sockunion2str (union sockunion *su, char *buf, size_t len)
167{
168 if (su->sa.sa_family == AF_INET)
169 return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len);
170#ifdef HAVE_IPV6
171 else if (su->sa.sa_family == AF_INET6)
172 return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len);
173#endif /* HAVE_IPV6 */
174 return NULL;
175}
176
177union sockunion *
paul42d49862004-10-13 05:22:18 +0000178sockunion_str2su (const char *str)
paul718e3742002-12-13 20:15:29 +0000179{
180 int ret;
181 union sockunion *su;
182
183 su = XMALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
184 memset (su, 0, sizeof (union sockunion));
185
186 ret = inet_pton (AF_INET, str, &su->sin.sin_addr);
187 if (ret > 0) /* Valid IPv4 address format. */
188 {
189 su->sin.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000190#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000191 su->sin.sin_len = sizeof(struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000192#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000193 return su;
194 }
195#ifdef HAVE_IPV6
196 ret = inet_pton (AF_INET6, str, &su->sin6.sin6_addr);
197 if (ret > 0) /* Valid IPv6 address format. */
198 {
199 su->sin6.sin6_family = AF_INET6;
200#ifdef SIN6_LEN
201 su->sin6.sin6_len = sizeof(struct sockaddr_in6);
202#endif /* SIN6_LEN */
203 return su;
204 }
205#endif /* HAVE_IPV6 */
206
207 XFREE (MTYPE_SOCKUNION, su);
208 return NULL;
209}
210
211char *
212sockunion_su2str (union sockunion *su)
213{
paul42d49862004-10-13 05:22:18 +0000214 char str[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +0000215
216 switch (su->sa.sa_family)
217 {
218 case AF_INET:
219 inet_ntop (AF_INET, &su->sin.sin_addr, str, sizeof (str));
220 break;
221#ifdef HAVE_IPV6
222 case AF_INET6:
223 inet_ntop (AF_INET6, &su->sin6.sin6_addr, str, sizeof (str));
224 break;
225#endif /* HAVE_IPV6 */
226 }
Paul Jakma5a54df92006-02-21 01:37:14 +0000227 return XSTRDUP (MTYPE_TMP, str);
paul718e3742002-12-13 20:15:29 +0000228}
229
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100230/* Convert IPv4 compatible IPv6 address to IPv4 address. */
231static void
232sockunion_normalise_mapped (union sockunion *su)
233{
234 struct sockaddr_in sin;
235
236#ifdef HAVE_IPV6
237 if (su->sa.sa_family == AF_INET6
238 && IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
239 {
240 memset (&sin, 0, sizeof (struct sockaddr_in));
241 sin.sin_family = AF_INET;
242 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
243 memcpy (su, &sin, sizeof (struct sockaddr_in));
244 }
245#endif /* HAVE_IPV6 */
246}
247
paul718e3742002-12-13 20:15:29 +0000248/* Return socket of sockunion. */
249int
250sockunion_socket (union sockunion *su)
251{
252 int sock;
253
254 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
255 if (sock < 0)
256 {
ajs6099b3b2004-11-20 02:06:59 +0000257 zlog (NULL, LOG_WARNING, "Can't make socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000258 return -1;
259 }
260
261 return sock;
262}
263
264/* Return accepted new socket file descriptor. */
265int
266sockunion_accept (int sock, union sockunion *su)
267{
268 socklen_t len;
269 int client_sock;
270
271 len = sizeof (union sockunion);
272 client_sock = accept (sock, (struct sockaddr *) su, &len);
273
paul718e3742002-12-13 20:15:29 +0000274 return client_sock;
275}
276
277/* Return sizeof union sockunion. */
paul8cc41982005-05-06 21:25:49 +0000278static int
paul718e3742002-12-13 20:15:29 +0000279sockunion_sizeof (union sockunion *su)
280{
281 int ret;
282
283 ret = 0;
284 switch (su->sa.sa_family)
285 {
286 case AF_INET:
287 ret = sizeof (struct sockaddr_in);
288 break;
289#ifdef HAVE_IPV6
290 case AF_INET6:
291 ret = sizeof (struct sockaddr_in6);
292 break;
293#endif /* AF_INET6 */
294 }
295 return ret;
296}
297
298/* return sockunion structure : this function should be revised. */
paul8cc41982005-05-06 21:25:49 +0000299static char *
paul718e3742002-12-13 20:15:29 +0000300sockunion_log (union sockunion *su)
301{
302 static char buf[SU_ADDRSTRLEN];
303
304 switch (su->sa.sa_family)
305 {
306 case AF_INET:
paul239a6712003-05-24 11:50:50 +0000307 snprintf (buf, SU_ADDRSTRLEN, "%s", inet_ntoa (su->sin.sin_addr));
paul718e3742002-12-13 20:15:29 +0000308 break;
309#ifdef HAVE_IPV6
310 case AF_INET6:
paul239a6712003-05-24 11:50:50 +0000311 snprintf (buf, SU_ADDRSTRLEN, "%s",
312 inet_ntop (AF_INET6, &(su->sin6.sin6_addr), buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +0000313 break;
314#endif /* HAVE_IPV6 */
315 default:
paul239a6712003-05-24 11:50:50 +0000316 snprintf (buf, SU_ADDRSTRLEN, "af_unknown %d ", su->sa.sa_family);
paul718e3742002-12-13 20:15:29 +0000317 break;
318 }
Paul Jakma5a54df92006-02-21 01:37:14 +0000319 return (XSTRDUP (MTYPE_TMP, buf));
paul718e3742002-12-13 20:15:29 +0000320}
321
322/* sockunion_connect returns
323 -1 : error occured
324 0 : connect success
325 1 : connect is in progress */
326enum connect_result
327sockunion_connect (int fd, union sockunion *peersu, unsigned short port,
328 unsigned int ifindex)
329{
330 int ret;
331 int val;
332 union sockunion su;
333
334 memcpy (&su, peersu, sizeof (union sockunion));
335
336 switch (su.sa.sa_family)
337 {
338 case AF_INET:
339 su.sin.sin_port = port;
340 break;
341#ifdef HAVE_IPV6
342 case AF_INET6:
343 su.sin6.sin6_port = port;
344#ifdef KAME
345 if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex)
346 {
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000347#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
paul718e3742002-12-13 20:15:29 +0000348 /* su.sin6.sin6_scope_id = ifindex; */
hasso726f9b22003-05-25 21:04:54 +0000349#ifdef MUSICA
350 su.sin6.sin6_scope_id = ifindex;
351#endif
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000352#endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */
hasso726f9b22003-05-25 21:04:54 +0000353#ifndef MUSICA
paul718e3742002-12-13 20:15:29 +0000354 SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex);
hasso726f9b22003-05-25 21:04:54 +0000355#endif
paul718e3742002-12-13 20:15:29 +0000356 }
357#endif /* KAME */
358 break;
359#endif /* HAVE_IPV6 */
360 }
361
362 /* Make socket non-block. */
363 val = fcntl (fd, F_GETFL, 0);
364 fcntl (fd, F_SETFL, val|O_NONBLOCK);
365
366 /* Call connect function. */
367 ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su));
368
369 /* Immediate success */
370 if (ret == 0)
371 {
372 fcntl (fd, F_SETFL, val);
373 return connect_success;
374 }
375
376 /* If connect is in progress then return 1 else it's real error. */
377 if (ret < 0)
378 {
379 if (errno != EINPROGRESS)
380 {
381 zlog_info ("can't connect to %s fd %d : %s",
ajs6099b3b2004-11-20 02:06:59 +0000382 sockunion_log (&su), fd, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000383 return connect_error;
384 }
385 }
386
387 fcntl (fd, F_SETFL, val);
388
389 return connect_in_progress;
390}
391
392/* Make socket from sockunion union. */
393int
394sockunion_stream_socket (union sockunion *su)
395{
396 int sock;
397
398 if (su->sa.sa_family == 0)
399 su->sa.sa_family = AF_INET_UNION;
400
401 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
402
403 if (sock < 0)
404 zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket");
405
406 return sock;
407}
408
409/* Bind socket to specified address. */
410int
411sockunion_bind (int sock, union sockunion *su, unsigned short port,
412 union sockunion *su_addr)
413{
414 int size = 0;
415 int ret;
416
417 if (su->sa.sa_family == AF_INET)
418 {
419 size = sizeof (struct sockaddr_in);
420 su->sin.sin_port = htons (port);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000421#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000422 su->sin.sin_len = size;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000423#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000424 if (su_addr == NULL)
425 su->sin.sin_addr.s_addr = htonl (INADDR_ANY);
426 }
427#ifdef HAVE_IPV6
428 else if (su->sa.sa_family == AF_INET6)
429 {
430 size = sizeof (struct sockaddr_in6);
431 su->sin6.sin6_port = htons (port);
432#ifdef SIN6_LEN
433 su->sin6.sin6_len = size;
434#endif /* SIN6_LEN */
435 if (su_addr == NULL)
436 {
437#if defined(LINUX_IPV6) || defined(NRL)
438 memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr));
439#else
440 su->sin6.sin6_addr = in6addr_any;
441#endif /* LINUX_IPV6 */
442 }
443 }
444#endif /* HAVE_IPV6 */
445
446
447 ret = bind (sock, (struct sockaddr *)su, size);
448 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +0000449 zlog (NULL, LOG_WARNING, "can't bind socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000450
451 return ret;
452}
453
454int
455sockopt_reuseaddr (int sock)
456{
457 int ret;
458 int on = 1;
459
460 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
461 (void *) &on, sizeof (on));
462 if (ret < 0)
463 {
464 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock);
465 return -1;
466 }
467 return 0;
468}
469
470#ifdef SO_REUSEPORT
471int
472sockopt_reuseport (int sock)
473{
474 int ret;
475 int on = 1;
476
477 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT,
478 (void *) &on, sizeof (on));
479 if (ret < 0)
480 {
hassoe7fe8c82005-05-06 19:33:35 +0000481 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEPORT to socket %d", sock);
paul718e3742002-12-13 20:15:29 +0000482 return -1;
483 }
484 return 0;
485}
486#else
487int
488sockopt_reuseport (int sock)
489{
490 return 0;
491}
492#endif /* 0 */
493
494int
495sockopt_ttl (int family, int sock, int ttl)
496{
497 int ret;
498
499#ifdef IP_TTL
500 if (family == AF_INET)
501 {
502 ret = setsockopt (sock, IPPROTO_IP, IP_TTL,
503 (void *) &ttl, sizeof (int));
504 if (ret < 0)
505 {
506 zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock);
507 return -1;
508 }
509 return 0;
510 }
511#endif /* IP_TTL */
512#ifdef HAVE_IPV6
513 if (family == AF_INET6)
514 {
515 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
516 (void *) &ttl, sizeof (int));
517 if (ret < 0)
518 {
519 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
520 ttl, sock);
521 return -1;
522 }
523 return 0;
524 }
525#endif /* HAVE_IPV6 */
526 return 0;
527}
528
529/* If same family and same prefix return 1. */
530int
531sockunion_same (union sockunion *su1, union sockunion *su2)
532{
533 int ret = 0;
534
535 if (su1->sa.sa_family != su2->sa.sa_family)
536 return 0;
537
538 switch (su1->sa.sa_family)
539 {
540 case AF_INET:
541 ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr,
542 sizeof (struct in_addr));
543 break;
544#ifdef HAVE_IPV6
545 case AF_INET6:
546 ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
547 sizeof (struct in6_addr));
548 break;
549#endif /* HAVE_IPV6 */
550 }
551 if (ret == 0)
552 return 1;
553 else
554 return 0;
555}
556
557/* After TCP connection is established. Get local address and port. */
558union sockunion *
559sockunion_getsockname (int fd)
560{
561 int ret;
paul22528292004-05-08 05:10:38 +0000562 socklen_t len;
paul718e3742002-12-13 20:15:29 +0000563 union
564 {
565 struct sockaddr sa;
566 struct sockaddr_in sin;
567#ifdef HAVE_IPV6
568 struct sockaddr_in6 sin6;
569#endif /* HAVE_IPV6 */
570 char tmp_buffer[128];
571 } name;
572 union sockunion *su;
573
574 memset (&name, 0, sizeof name);
575 len = sizeof name;
576
577 ret = getsockname (fd, (struct sockaddr *)&name, &len);
578 if (ret < 0)
579 {
580 zlog_warn ("Can't get local address and port by getsockname: %s",
ajs6099b3b2004-11-20 02:06:59 +0000581 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000582 return NULL;
583 }
584
585 if (name.sa.sa_family == AF_INET)
586 {
paul2ba9a372005-05-19 01:37:50 +0000587 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000588 memcpy (su, &name, sizeof (struct sockaddr_in));
589 return su;
590 }
591#ifdef HAVE_IPV6
592 if (name.sa.sa_family == AF_INET6)
593 {
paul2ba9a372005-05-19 01:37:50 +0000594 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000595 memcpy (su, &name, sizeof (struct sockaddr_in6));
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100596 sockunion_normalise_mapped (su);
paul718e3742002-12-13 20:15:29 +0000597 return su;
598 }
599#endif /* HAVE_IPV6 */
600 return NULL;
601}
602
603/* After TCP connection is established. Get remote address and port. */
604union sockunion *
605sockunion_getpeername (int fd)
606{
607 int ret;
paul22528292004-05-08 05:10:38 +0000608 socklen_t len;
paul718e3742002-12-13 20:15:29 +0000609 union
610 {
611 struct sockaddr sa;
612 struct sockaddr_in sin;
613#ifdef HAVE_IPV6
614 struct sockaddr_in6 sin6;
615#endif /* HAVE_IPV6 */
616 char tmp_buffer[128];
617 } name;
618 union sockunion *su;
619
620 memset (&name, 0, sizeof name);
621 len = sizeof name;
622 ret = getpeername (fd, (struct sockaddr *)&name, &len);
623 if (ret < 0)
624 {
625 zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s",
ajs6099b3b2004-11-20 02:06:59 +0000626 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000627 return NULL;
628 }
629
630 if (name.sa.sa_family == AF_INET)
631 {
paul2ba9a372005-05-19 01:37:50 +0000632 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000633 memcpy (su, &name, sizeof (struct sockaddr_in));
634 return su;
635 }
636#ifdef HAVE_IPV6
637 if (name.sa.sa_family == AF_INET6)
638 {
paul2ba9a372005-05-19 01:37:50 +0000639 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000640 memcpy (su, &name, sizeof (struct sockaddr_in6));
Paul Jakma1a7dcf42008-09-09 21:17:04 +0100641 sockunion_normalise_mapped (su);
paul718e3742002-12-13 20:15:29 +0000642 return su;
643 }
644#endif /* HAVE_IPV6 */
645 return NULL;
646}
647
648/* Print sockunion structure */
paul8cc41982005-05-06 21:25:49 +0000649static void __attribute__ ((unused))
paul718e3742002-12-13 20:15:29 +0000650sockunion_print (union sockunion *su)
651{
652 if (su == NULL)
653 return;
654
655 switch (su->sa.sa_family)
656 {
657 case AF_INET:
658 printf ("%s\n", inet_ntoa (su->sin.sin_addr));
659 break;
660#ifdef HAVE_IPV6
661 case AF_INET6:
662 {
paul42d49862004-10-13 05:22:18 +0000663 char buf [SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +0000664
665 printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr),
666 buf, sizeof (buf)));
667 }
668 break;
669#endif /* HAVE_IPV6 */
670
671#ifdef AF_LINK
672 case AF_LINK:
673 {
674 struct sockaddr_dl *sdl;
675
676 sdl = (struct sockaddr_dl *)&(su->sa);
677 printf ("link#%d\n", sdl->sdl_index);
678 }
679 break;
680#endif /* AF_LINK */
681 default:
682 printf ("af_unknown %d\n", su->sa.sa_family);
683 break;
684 }
685}
686
687#ifdef HAVE_IPV6
paul8cc41982005-05-06 21:25:49 +0000688static int
paul718e3742002-12-13 20:15:29 +0000689in6addr_cmp (struct in6_addr *addr1, struct in6_addr *addr2)
690{
hasso8c328f12004-10-05 21:01:23 +0000691 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000692 u_char *p1, *p2;
693
694 p1 = (u_char *)addr1;
695 p2 = (u_char *)addr2;
696
697 for (i = 0; i < sizeof (struct in6_addr); i++)
698 {
699 if (p1[i] > p2[i])
700 return 1;
701 else if (p1[i] < p2[i])
702 return -1;
703 }
704 return 0;
705}
706#endif /* HAVE_IPV6 */
707
708int
709sockunion_cmp (union sockunion *su1, union sockunion *su2)
710{
711 if (su1->sa.sa_family > su2->sa.sa_family)
712 return 1;
713 if (su1->sa.sa_family < su2->sa.sa_family)
714 return -1;
715
716 if (su1->sa.sa_family == AF_INET)
717 {
718 if (ntohl (su1->sin.sin_addr.s_addr) == ntohl (su2->sin.sin_addr.s_addr))
719 return 0;
720 if (ntohl (su1->sin.sin_addr.s_addr) > ntohl (su2->sin.sin_addr.s_addr))
721 return 1;
722 else
723 return -1;
724 }
725#ifdef HAVE_IPV6
726 if (su1->sa.sa_family == AF_INET6)
727 return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
728#endif /* HAVE_IPV6 */
729 return 0;
730}
731
732/* Duplicate sockunion. */
733union sockunion *
734sockunion_dup (union sockunion *su)
735{
736 union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
737 memcpy (dup, su, sizeof (union sockunion));
738 return dup;
739}
740
741void
742sockunion_free (union sockunion *su)
743{
744 XFREE (MTYPE_SOCKUNION, su);
745}