blob: e79f034c8133c9dfb28829ca737848d1001d477b [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;
146#ifdef HAVE_SIN_LEN
147 su->sin.sin_len = sizeof(struct sockaddr_in);
148#endif /* HAVE_SIN_LEN */
149 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;
190#ifdef HAVE_SIN_LEN
191 su->sin.sin_len = sizeof(struct sockaddr_in);
192#endif /* HAVE_SIN_LEN */
193 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 }
227 return strdup (str);
228}
229
230/* Return socket of sockunion. */
231int
232sockunion_socket (union sockunion *su)
233{
234 int sock;
235
236 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
237 if (sock < 0)
238 {
ajs6099b3b2004-11-20 02:06:59 +0000239 zlog (NULL, LOG_WARNING, "Can't make socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000240 return -1;
241 }
242
243 return sock;
244}
245
246/* Return accepted new socket file descriptor. */
247int
248sockunion_accept (int sock, union sockunion *su)
249{
250 socklen_t len;
251 int client_sock;
252
253 len = sizeof (union sockunion);
254 client_sock = accept (sock, (struct sockaddr *) su, &len);
255
256 /* Convert IPv4 compatible IPv6 address to IPv4 address. */
257#ifdef HAVE_IPV6
258 if (su->sa.sa_family == AF_INET6)
259 {
260 if (IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
261 {
262 struct sockaddr_in sin;
263
264 memset (&sin, 0, sizeof (struct sockaddr_in));
265 sin.sin_family = AF_INET;
266 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
267 memcpy (su, &sin, sizeof (struct sockaddr_in));
268 }
269 }
270#endif /* HAVE_IPV6 */
271
272 return client_sock;
273}
274
275/* Return sizeof union sockunion. */
paul8cc41982005-05-06 21:25:49 +0000276static int
paul718e3742002-12-13 20:15:29 +0000277sockunion_sizeof (union sockunion *su)
278{
279 int ret;
280
281 ret = 0;
282 switch (su->sa.sa_family)
283 {
284 case AF_INET:
285 ret = sizeof (struct sockaddr_in);
286 break;
287#ifdef HAVE_IPV6
288 case AF_INET6:
289 ret = sizeof (struct sockaddr_in6);
290 break;
291#endif /* AF_INET6 */
292 }
293 return ret;
294}
295
296/* return sockunion structure : this function should be revised. */
paul8cc41982005-05-06 21:25:49 +0000297static char *
paul718e3742002-12-13 20:15:29 +0000298sockunion_log (union sockunion *su)
299{
300 static char buf[SU_ADDRSTRLEN];
301
302 switch (su->sa.sa_family)
303 {
304 case AF_INET:
paul239a6712003-05-24 11:50:50 +0000305 snprintf (buf, SU_ADDRSTRLEN, "%s", inet_ntoa (su->sin.sin_addr));
paul718e3742002-12-13 20:15:29 +0000306 break;
307#ifdef HAVE_IPV6
308 case AF_INET6:
paul239a6712003-05-24 11:50:50 +0000309 snprintf (buf, SU_ADDRSTRLEN, "%s",
310 inet_ntop (AF_INET6, &(su->sin6.sin6_addr), buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +0000311 break;
312#endif /* HAVE_IPV6 */
313 default:
paul239a6712003-05-24 11:50:50 +0000314 snprintf (buf, SU_ADDRSTRLEN, "af_unknown %d ", su->sa.sa_family);
paul718e3742002-12-13 20:15:29 +0000315 break;
316 }
paul42d49862004-10-13 05:22:18 +0000317 return (strdup (buf));
paul718e3742002-12-13 20:15:29 +0000318}
319
320/* sockunion_connect returns
321 -1 : error occured
322 0 : connect success
323 1 : connect is in progress */
324enum connect_result
325sockunion_connect (int fd, union sockunion *peersu, unsigned short port,
326 unsigned int ifindex)
327{
328 int ret;
329 int val;
330 union sockunion su;
331
332 memcpy (&su, peersu, sizeof (union sockunion));
333
334 switch (su.sa.sa_family)
335 {
336 case AF_INET:
337 su.sin.sin_port = port;
338 break;
339#ifdef HAVE_IPV6
340 case AF_INET6:
341 su.sin6.sin6_port = port;
342#ifdef KAME
343 if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex)
344 {
345#ifdef HAVE_SIN6_SCOPE_ID
346 /* su.sin6.sin6_scope_id = ifindex; */
hasso726f9b22003-05-25 21:04:54 +0000347#ifdef MUSICA
348 su.sin6.sin6_scope_id = ifindex;
349#endif
paul718e3742002-12-13 20:15:29 +0000350#endif /* HAVE_SIN6_SCOPE_ID */
hasso726f9b22003-05-25 21:04:54 +0000351#ifndef MUSICA
paul718e3742002-12-13 20:15:29 +0000352 SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex);
hasso726f9b22003-05-25 21:04:54 +0000353#endif
paul718e3742002-12-13 20:15:29 +0000354 }
355#endif /* KAME */
356 break;
357#endif /* HAVE_IPV6 */
358 }
359
360 /* Make socket non-block. */
361 val = fcntl (fd, F_GETFL, 0);
362 fcntl (fd, F_SETFL, val|O_NONBLOCK);
363
364 /* Call connect function. */
365 ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su));
366
367 /* Immediate success */
368 if (ret == 0)
369 {
370 fcntl (fd, F_SETFL, val);
371 return connect_success;
372 }
373
374 /* If connect is in progress then return 1 else it's real error. */
375 if (ret < 0)
376 {
377 if (errno != EINPROGRESS)
378 {
379 zlog_info ("can't connect to %s fd %d : %s",
ajs6099b3b2004-11-20 02:06:59 +0000380 sockunion_log (&su), fd, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000381 return connect_error;
382 }
383 }
384
385 fcntl (fd, F_SETFL, val);
386
387 return connect_in_progress;
388}
389
390/* Make socket from sockunion union. */
391int
392sockunion_stream_socket (union sockunion *su)
393{
394 int sock;
395
396 if (su->sa.sa_family == 0)
397 su->sa.sa_family = AF_INET_UNION;
398
399 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
400
401 if (sock < 0)
402 zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket");
403
404 return sock;
405}
406
407/* Bind socket to specified address. */
408int
409sockunion_bind (int sock, union sockunion *su, unsigned short port,
410 union sockunion *su_addr)
411{
412 int size = 0;
413 int ret;
414
415 if (su->sa.sa_family == AF_INET)
416 {
417 size = sizeof (struct sockaddr_in);
418 su->sin.sin_port = htons (port);
419#ifdef HAVE_SIN_LEN
420 su->sin.sin_len = size;
421#endif /* HAVE_SIN_LEN */
422 if (su_addr == NULL)
423 su->sin.sin_addr.s_addr = htonl (INADDR_ANY);
424 }
425#ifdef HAVE_IPV6
426 else if (su->sa.sa_family == AF_INET6)
427 {
428 size = sizeof (struct sockaddr_in6);
429 su->sin6.sin6_port = htons (port);
430#ifdef SIN6_LEN
431 su->sin6.sin6_len = size;
432#endif /* SIN6_LEN */
433 if (su_addr == NULL)
434 {
435#if defined(LINUX_IPV6) || defined(NRL)
436 memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr));
437#else
438 su->sin6.sin6_addr = in6addr_any;
439#endif /* LINUX_IPV6 */
440 }
441 }
442#endif /* HAVE_IPV6 */
443
444
445 ret = bind (sock, (struct sockaddr *)su, size);
446 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +0000447 zlog (NULL, LOG_WARNING, "can't bind socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000448
449 return ret;
450}
451
452int
453sockopt_reuseaddr (int sock)
454{
455 int ret;
456 int on = 1;
457
458 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
459 (void *) &on, sizeof (on));
460 if (ret < 0)
461 {
462 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock);
463 return -1;
464 }
465 return 0;
466}
467
468#ifdef SO_REUSEPORT
469int
470sockopt_reuseport (int sock)
471{
472 int ret;
473 int on = 1;
474
475 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT,
476 (void *) &on, sizeof (on));
477 if (ret < 0)
478 {
hassoe7fe8c82005-05-06 19:33:35 +0000479 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEPORT to socket %d", sock);
paul718e3742002-12-13 20:15:29 +0000480 return -1;
481 }
482 return 0;
483}
484#else
485int
486sockopt_reuseport (int sock)
487{
488 return 0;
489}
490#endif /* 0 */
491
492int
493sockopt_ttl (int family, int sock, int ttl)
494{
495 int ret;
496
497#ifdef IP_TTL
498 if (family == AF_INET)
499 {
500 ret = setsockopt (sock, IPPROTO_IP, IP_TTL,
501 (void *) &ttl, sizeof (int));
502 if (ret < 0)
503 {
504 zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock);
505 return -1;
506 }
507 return 0;
508 }
509#endif /* IP_TTL */
510#ifdef HAVE_IPV6
511 if (family == AF_INET6)
512 {
513 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
514 (void *) &ttl, sizeof (int));
515 if (ret < 0)
516 {
517 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
518 ttl, sock);
519 return -1;
520 }
521 return 0;
522 }
523#endif /* HAVE_IPV6 */
524 return 0;
525}
526
527/* If same family and same prefix return 1. */
528int
529sockunion_same (union sockunion *su1, union sockunion *su2)
530{
531 int ret = 0;
532
533 if (su1->sa.sa_family != su2->sa.sa_family)
534 return 0;
535
536 switch (su1->sa.sa_family)
537 {
538 case AF_INET:
539 ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr,
540 sizeof (struct in_addr));
541 break;
542#ifdef HAVE_IPV6
543 case AF_INET6:
544 ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
545 sizeof (struct in6_addr));
546 break;
547#endif /* HAVE_IPV6 */
548 }
549 if (ret == 0)
550 return 1;
551 else
552 return 0;
553}
554
555/* After TCP connection is established. Get local address and port. */
556union sockunion *
557sockunion_getsockname (int fd)
558{
559 int ret;
paul22528292004-05-08 05:10:38 +0000560 socklen_t len;
paul718e3742002-12-13 20:15:29 +0000561 union
562 {
563 struct sockaddr sa;
564 struct sockaddr_in sin;
565#ifdef HAVE_IPV6
566 struct sockaddr_in6 sin6;
567#endif /* HAVE_IPV6 */
568 char tmp_buffer[128];
569 } name;
570 union sockunion *su;
571
572 memset (&name, 0, sizeof name);
573 len = sizeof name;
574
575 ret = getsockname (fd, (struct sockaddr *)&name, &len);
576 if (ret < 0)
577 {
578 zlog_warn ("Can't get local address and port by getsockname: %s",
ajs6099b3b2004-11-20 02:06:59 +0000579 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000580 return NULL;
581 }
582
583 if (name.sa.sa_family == AF_INET)
584 {
paul2ba9a372005-05-19 01:37:50 +0000585 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000586 memcpy (su, &name, sizeof (struct sockaddr_in));
587 return su;
588 }
589#ifdef HAVE_IPV6
590 if (name.sa.sa_family == AF_INET6)
591 {
paul2ba9a372005-05-19 01:37:50 +0000592 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000593 memcpy (su, &name, sizeof (struct sockaddr_in6));
594
595 if (IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
596 {
597 struct sockaddr_in sin;
598
599 sin.sin_family = AF_INET;
600 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
601 sin.sin_port = su->sin6.sin6_port;
602 memcpy (su, &sin, sizeof (struct sockaddr_in));
603 }
604 return su;
605 }
606#endif /* HAVE_IPV6 */
607 return NULL;
608}
609
610/* After TCP connection is established. Get remote address and port. */
611union sockunion *
612sockunion_getpeername (int fd)
613{
614 int ret;
paul22528292004-05-08 05:10:38 +0000615 socklen_t len;
paul718e3742002-12-13 20:15:29 +0000616 union
617 {
618 struct sockaddr sa;
619 struct sockaddr_in sin;
620#ifdef HAVE_IPV6
621 struct sockaddr_in6 sin6;
622#endif /* HAVE_IPV6 */
623 char tmp_buffer[128];
624 } name;
625 union sockunion *su;
626
627 memset (&name, 0, sizeof name);
628 len = sizeof name;
629 ret = getpeername (fd, (struct sockaddr *)&name, &len);
630 if (ret < 0)
631 {
632 zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s",
ajs6099b3b2004-11-20 02:06:59 +0000633 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000634 return NULL;
635 }
636
637 if (name.sa.sa_family == AF_INET)
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_in));
641 return su;
642 }
643#ifdef HAVE_IPV6
644 if (name.sa.sa_family == AF_INET6)
645 {
paul2ba9a372005-05-19 01:37:50 +0000646 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000647 memcpy (su, &name, sizeof (struct sockaddr_in6));
648
649 if (IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
650 {
651 struct sockaddr_in sin;
652
653 sin.sin_family = AF_INET;
654 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
655 sin.sin_port = su->sin6.sin6_port;
656 memcpy (su, &sin, sizeof (struct sockaddr_in));
657 }
658 return su;
659 }
660#endif /* HAVE_IPV6 */
661 return NULL;
662}
663
664/* Print sockunion structure */
paul8cc41982005-05-06 21:25:49 +0000665static void __attribute__ ((unused))
paul718e3742002-12-13 20:15:29 +0000666sockunion_print (union sockunion *su)
667{
668 if (su == NULL)
669 return;
670
671 switch (su->sa.sa_family)
672 {
673 case AF_INET:
674 printf ("%s\n", inet_ntoa (su->sin.sin_addr));
675 break;
676#ifdef HAVE_IPV6
677 case AF_INET6:
678 {
paul42d49862004-10-13 05:22:18 +0000679 char buf [SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +0000680
681 printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr),
682 buf, sizeof (buf)));
683 }
684 break;
685#endif /* HAVE_IPV6 */
686
687#ifdef AF_LINK
688 case AF_LINK:
689 {
690 struct sockaddr_dl *sdl;
691
692 sdl = (struct sockaddr_dl *)&(su->sa);
693 printf ("link#%d\n", sdl->sdl_index);
694 }
695 break;
696#endif /* AF_LINK */
697 default:
698 printf ("af_unknown %d\n", su->sa.sa_family);
699 break;
700 }
701}
702
703#ifdef HAVE_IPV6
paul8cc41982005-05-06 21:25:49 +0000704static int
paul718e3742002-12-13 20:15:29 +0000705in6addr_cmp (struct in6_addr *addr1, struct in6_addr *addr2)
706{
hasso8c328f12004-10-05 21:01:23 +0000707 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000708 u_char *p1, *p2;
709
710 p1 = (u_char *)addr1;
711 p2 = (u_char *)addr2;
712
713 for (i = 0; i < sizeof (struct in6_addr); i++)
714 {
715 if (p1[i] > p2[i])
716 return 1;
717 else if (p1[i] < p2[i])
718 return -1;
719 }
720 return 0;
721}
722#endif /* HAVE_IPV6 */
723
724int
725sockunion_cmp (union sockunion *su1, union sockunion *su2)
726{
727 if (su1->sa.sa_family > su2->sa.sa_family)
728 return 1;
729 if (su1->sa.sa_family < su2->sa.sa_family)
730 return -1;
731
732 if (su1->sa.sa_family == AF_INET)
733 {
734 if (ntohl (su1->sin.sin_addr.s_addr) == ntohl (su2->sin.sin_addr.s_addr))
735 return 0;
736 if (ntohl (su1->sin.sin_addr.s_addr) > ntohl (su2->sin.sin_addr.s_addr))
737 return 1;
738 else
739 return -1;
740 }
741#ifdef HAVE_IPV6
742 if (su1->sa.sa_family == AF_INET6)
743 return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
744#endif /* HAVE_IPV6 */
745 return 0;
746}
747
748/* Duplicate sockunion. */
749union sockunion *
750sockunion_dup (union sockunion *su)
751{
752 union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
753 memcpy (dup, su, sizeof (union sockunion));
754 return dup;
755}
756
757void
758sockunion_free (union sockunion *su)
759{
760 XFREE (MTYPE_SOCKUNION, su);
761}