]> git.proxmox.com Git - mirror_frr.git/blame - lib/sockunion.c
*: make DEFUN installations file-local
[mirror_frr.git] / lib / sockunion.c
CommitLineData
718e3744 1/* 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"
718e3744 28#include "log.h"
7cb5cdbb 29#include "jhash.h"
718e3744 30
4a1ab8e4
DL
31DEFINE_MTYPE_STATIC(LIB, SOCKUNION, "Socket union")
32
718e3744 33const char *
c0d8db4d 34inet_sutop (const union sockunion *su, char *str)
718e3744 35{
36 switch (su->sa.sa_family)
37 {
38 case AF_INET:
39 inet_ntop (AF_INET, &su->sin.sin_addr, str, INET_ADDRSTRLEN);
40 break;
41#ifdef HAVE_IPV6
42 case AF_INET6:
43 inet_ntop (AF_INET6, &su->sin6.sin6_addr, str, INET6_ADDRSTRLEN);
44 break;
45#endif /* HAVE_IPV6 */
46 }
47 return str;
48}
49
50int
a149411b 51str2sockunion (const char *str, union sockunion *su)
718e3744 52{
53 int ret;
54
55 memset (su, 0, sizeof (union sockunion));
56
57 ret = inet_pton (AF_INET, str, &su->sin.sin_addr);
58 if (ret > 0) /* Valid IPv4 address format. */
59 {
60 su->sin.sin_family = AF_INET;
6f0e3f6e 61#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 62 su->sin.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 63#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 64 return 0;
65 }
66#ifdef HAVE_IPV6
67 ret = inet_pton (AF_INET6, str, &su->sin6.sin6_addr);
68 if (ret > 0) /* Valid IPv6 address format. */
69 {
70 su->sin6.sin6_family = AF_INET6;
71#ifdef SIN6_LEN
72 su->sin6.sin6_len = sizeof(struct sockaddr_in6);
73#endif /* SIN6_LEN */
74 return 0;
75 }
76#endif /* HAVE_IPV6 */
77 return -1;
78}
79
80const char *
c0d8db4d 81sockunion2str (const union sockunion *su, char *buf, size_t len)
718e3744 82{
67e2b6f0
TT
83 switch (sockunion_family(su))
84 {
85 case AF_UNSPEC:
86 snprintf (buf, len, "(unspec)");
87 return buf;
88 case AF_INET:
89 return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len);
718e3744 90#ifdef HAVE_IPV6
67e2b6f0
TT
91 case AF_INET6:
92 return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len);
718e3744 93#endif /* HAVE_IPV6 */
67e2b6f0
TT
94 }
95 snprintf (buf, len, "(af %d)", sockunion_family(su));
96 return buf;
718e3744 97}
98
2fb2a455
PJ
99union sockunion *
100sockunion_str2su (const char *str)
101{
102 union sockunion *su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
103
104 if (!str2sockunion (str, su))
105 return su;
106
107 XFREE (MTYPE_SOCKUNION, su);
108 return NULL;
109}
110
1a7dcf42
PJ
111/* Convert IPv4 compatible IPv6 address to IPv4 address. */
112static void
113sockunion_normalise_mapped (union sockunion *su)
114{
115 struct sockaddr_in sin;
116
117#ifdef HAVE_IPV6
118 if (su->sa.sa_family == AF_INET6
119 && IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
120 {
121 memset (&sin, 0, sizeof (struct sockaddr_in));
122 sin.sin_family = AF_INET;
3fa3f957 123 sin.sin_port = su->sin6.sin6_port;
1a7dcf42
PJ
124 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
125 memcpy (su, &sin, sizeof (struct sockaddr_in));
126 }
127#endif /* HAVE_IPV6 */
128}
129
fc9707c3
DW
130/* return sockunion structure : this function should be revised. */
131static const char *
c0d8db4d 132sockunion_log (const union sockunion *su, char *buf, size_t len)
fc9707c3
DW
133{
134 switch (su->sa.sa_family)
135 {
136 case AF_INET:
137 return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len);
138
139 case AF_INET6:
140 return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len);
141 break;
142
143 default:
144 snprintf (buf, len, "af_unknown %d ", su->sa.sa_family);
145 return buf;
146 }
147}
148
718e3744 149/* Return socket of sockunion. */
150int
c0d8db4d 151sockunion_socket (const union sockunion *su)
718e3744 152{
153 int sock;
154
155 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
156 if (sock < 0)
157 {
fc9707c3
DW
158 char buf[SU_ADDRSTRLEN];
159 zlog (NULL, LOG_WARNING, "Can't make socket for %s : %s",
160 sockunion_log (su, buf, SU_ADDRSTRLEN), safe_strerror (errno));
718e3744 161 return -1;
162 }
163
164 return sock;
165}
166
167/* Return accepted new socket file descriptor. */
168int
169sockunion_accept (int sock, union sockunion *su)
170{
171 socklen_t len;
172 int client_sock;
173
174 len = sizeof (union sockunion);
175 client_sock = accept (sock, (struct sockaddr *) su, &len);
176
84152ee6 177 sockunion_normalise_mapped (su);
718e3744 178 return client_sock;
179}
180
181/* Return sizeof union sockunion. */
8cc4198f 182static int
c0d8db4d 183sockunion_sizeof (const union sockunion *su)
718e3744 184{
185 int ret;
186
187 ret = 0;
188 switch (su->sa.sa_family)
189 {
190 case AF_INET:
191 ret = sizeof (struct sockaddr_in);
192 break;
193#ifdef HAVE_IPV6
194 case AF_INET6:
195 ret = sizeof (struct sockaddr_in6);
196 break;
197#endif /* AF_INET6 */
198 }
199 return ret;
200}
201
718e3744 202/* sockunion_connect returns
203 -1 : error occured
204 0 : connect success
205 1 : connect is in progress */
206enum connect_result
c0d8db4d 207sockunion_connect (int fd, const union sockunion *peersu, unsigned short port,
b892f1dd 208 ifindex_t ifindex)
718e3744 209{
210 int ret;
211 int val;
212 union sockunion su;
213
214 memcpy (&su, peersu, sizeof (union sockunion));
215
216 switch (su.sa.sa_family)
217 {
218 case AF_INET:
219 su.sin.sin_port = port;
220 break;
221#ifdef HAVE_IPV6
222 case AF_INET6:
223 su.sin6.sin6_port = port;
224#ifdef KAME
225 if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex)
226 {
f2345335 227 su.sin6.sin6_scope_id = ifindex;
718e3744 228 SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex);
229 }
230#endif /* KAME */
231 break;
232#endif /* HAVE_IPV6 */
233 }
234
235 /* Make socket non-block. */
236 val = fcntl (fd, F_GETFL, 0);
237 fcntl (fd, F_SETFL, val|O_NONBLOCK);
238
239 /* Call connect function. */
240 ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su));
241
242 /* Immediate success */
243 if (ret == 0)
244 {
245 fcntl (fd, F_SETFL, val);
246 return connect_success;
247 }
248
249 /* If connect is in progress then return 1 else it's real error. */
250 if (ret < 0)
251 {
252 if (errno != EINPROGRESS)
253 {
b24b19f7 254 char str[SU_ADDRSTRLEN];
718e3744 255 zlog_info ("can't connect to %s fd %d : %s",
b24b19f7
SH
256 sockunion_log (&su, str, sizeof str),
257 fd, safe_strerror (errno));
718e3744 258 return connect_error;
259 }
260 }
261
262 fcntl (fd, F_SETFL, val);
263
264 return connect_in_progress;
265}
266
267/* Make socket from sockunion union. */
268int
269sockunion_stream_socket (union sockunion *su)
270{
271 int sock;
272
273 if (su->sa.sa_family == 0)
274 su->sa.sa_family = AF_INET_UNION;
275
276 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
277
278 if (sock < 0)
279 zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket");
280
281 return sock;
282}
283
284/* Bind socket to specified address. */
285int
286sockunion_bind (int sock, union sockunion *su, unsigned short port,
287 union sockunion *su_addr)
288{
289 int size = 0;
290 int ret;
291
292 if (su->sa.sa_family == AF_INET)
293 {
294 size = sizeof (struct sockaddr_in);
295 su->sin.sin_port = htons (port);
6f0e3f6e 296#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 297 su->sin.sin_len = size;
6f0e3f6e 298#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 299 if (su_addr == NULL)
12829328 300 sockunion2ip (su) = htonl (INADDR_ANY);
718e3744 301 }
302#ifdef HAVE_IPV6
303 else if (su->sa.sa_family == AF_INET6)
304 {
305 size = sizeof (struct sockaddr_in6);
306 su->sin6.sin6_port = htons (port);
307#ifdef SIN6_LEN
308 su->sin6.sin6_len = size;
309#endif /* SIN6_LEN */
310 if (su_addr == NULL)
311 {
1cbb5dfc 312#ifdef LINUX_IPV6
718e3744 313 memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr));
314#else
315 su->sin6.sin6_addr = in6addr_any;
316#endif /* LINUX_IPV6 */
317 }
318 }
319#endif /* HAVE_IPV6 */
320
321
322 ret = bind (sock, (struct sockaddr *)su, size);
323 if (ret < 0)
fc9707c3
DW
324 {
325 char buf[SU_ADDRSTRLEN];
326 zlog (NULL, LOG_WARNING, "can't bind socket for %s : %s",
327 sockunion_log (su, buf, SU_ADDRSTRLEN), safe_strerror (errno));
328 }
718e3744 329
330 return ret;
331}
332
333int
334sockopt_reuseaddr (int sock)
335{
336 int ret;
337 int on = 1;
338
339 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
340 (void *) &on, sizeof (on));
341 if (ret < 0)
342 {
343 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock);
344 return -1;
345 }
346 return 0;
347}
348
349#ifdef SO_REUSEPORT
350int
351sockopt_reuseport (int sock)
352{
353 int ret;
354 int on = 1;
355
356 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT,
357 (void *) &on, sizeof (on));
358 if (ret < 0)
359 {
e7fe8c88 360 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEPORT to socket %d", sock);
718e3744 361 return -1;
362 }
363 return 0;
364}
365#else
366int
367sockopt_reuseport (int sock)
368{
369 return 0;
370}
371#endif /* 0 */
372
373int
374sockopt_ttl (int family, int sock, int ttl)
375{
376 int ret;
377
378#ifdef IP_TTL
379 if (family == AF_INET)
380 {
381 ret = setsockopt (sock, IPPROTO_IP, IP_TTL,
382 (void *) &ttl, sizeof (int));
383 if (ret < 0)
384 {
385 zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock);
386 return -1;
387 }
388 return 0;
389 }
390#endif /* IP_TTL */
391#ifdef HAVE_IPV6
392 if (family == AF_INET6)
393 {
394 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
395 (void *) &ttl, sizeof (int));
396 if (ret < 0)
397 {
398 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
399 ttl, sock);
400 return -1;
401 }
402 return 0;
403 }
404#endif /* HAVE_IPV6 */
405 return 0;
406}
407
58192df7
SH
408int
409sockopt_cork (int sock, int onoff)
410{
411#ifdef TCP_CORK
412 return setsockopt (sock, IPPROTO_TCP, TCP_CORK, &onoff, sizeof(onoff));
413#else
414 return 0;
ed40466a
DS
415#endif
416}
417
418int sockopt_mark_default(int sock, int mark, struct zebra_privs_t *cap)
419{
420#ifdef SO_MARK
421 int ret;
422
423 if ( cap->change (ZPRIVS_RAISE) )
424 zlog_err ("routing_socket: Can't raise privileges");
425
426 ret = setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
427
428 if ( cap->change (ZPRIVS_LOWER) )
429 zlog_err ("routing_socket: Can't lower privileges");
430
431 return ret;
432#else
433 return 0;
58192df7
SH
434#endif
435}
436
fa411a21
NH
437int
438sockopt_minttl (int family, int sock, int minttl)
439{
89b6d1f8 440#ifdef IP_MINTTL
d876bdf4 441 if (family == AF_INET)
fa411a21 442 {
d876bdf4
SH
443 int ret = setsockopt (sock, IPPROTO_IP, IP_MINTTL, &minttl, sizeof(minttl));
444 if (ret < 0)
445 zlog (NULL, LOG_WARNING,
446 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
447 minttl, sock, safe_strerror (errno));
448 return ret;
fa411a21 449 }
d876bdf4 450#endif /* IP_MINTTL */
d8dc5257 451#ifdef IPV6_MINHOPCOUNT
d876bdf4
SH
452 if (family == AF_INET6)
453 {
d8dc5257 454 int ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MINHOPCOUNT, &minttl, sizeof(minttl));
d876bdf4
SH
455 if (ret < 0)
456 zlog (NULL, LOG_WARNING,
d8dc5257 457 "can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s",
d876bdf4
SH
458 minttl, sock, safe_strerror (errno));
459 return ret;
460 }
461#endif
fa411a21 462
89b6d1f8
SH
463 errno = EOPNOTSUPP;
464 return -1;
fa411a21
NH
465}
466
ca051269
DL
467int
468sockopt_v6only (int family, int sock)
469{
470 int ret, on = 1;
471
472#ifdef HAVE_IPV6
473#ifdef IPV6_V6ONLY
474 if (family == AF_INET6)
475 {
476 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
477 (void *) &on, sizeof (int));
478 if (ret < 0)
479 {
480 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_V6ONLY "
481 "to socket %d", sock);
482 return -1;
483 }
484 return 0;
485 }
486#endif /* IPV6_V6ONLY */
487#endif /* HAVE_IPV6 */
488 return 0;
489}
490
718e3744 491/* If same family and same prefix return 1. */
492int
3f9c7369 493sockunion_same (const union sockunion *su1, const union sockunion *su2)
718e3744 494{
495 int ret = 0;
496
497 if (su1->sa.sa_family != su2->sa.sa_family)
498 return 0;
499
500 switch (su1->sa.sa_family)
501 {
502 case AF_INET:
503 ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr,
504 sizeof (struct in_addr));
505 break;
506#ifdef HAVE_IPV6
507 case AF_INET6:
508 ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
509 sizeof (struct in6_addr));
f2345335
DS
510 if ((ret == 0) && IN6_IS_ADDR_LINKLOCAL(&su1->sin6.sin6_addr))
511 {
512 /* compare interface indices */
513 if (su1->sin6.sin6_scope_id && su2->sin6.sin6_scope_id)
514 ret = (su1->sin6.sin6_scope_id == su2->sin6.sin6_scope_id) ? 0 : 1;
515 }
718e3744 516 break;
517#endif /* HAVE_IPV6 */
518 }
519 if (ret == 0)
520 return 1;
521 else
522 return 0;
523}
524
7cb5cdbb
TT
525unsigned int
526sockunion_hash (const union sockunion *su)
527{
528 switch (sockunion_family(su))
529 {
530 case AF_INET:
531 return jhash_1word(su->sin.sin_addr.s_addr, 0);
532#ifdef HAVE_IPV6
533 case AF_INET6:
534 return jhash2(su->sin6.sin6_addr.s6_addr32, ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0);
535#endif /* HAVE_IPV6 */
536 }
537 return 0;
538}
539
95e0999c
TT
540size_t
541family2addrsize(int family)
542{
543 switch (family)
544 {
545 case AF_INET:
546 return sizeof(struct in_addr);
547#ifdef HAVE_IPV6
548 case AF_INET6:
549 return sizeof(struct in6_addr);
550#endif /* HAVE_IPV6 */
551 }
552 return 0;
553}
554
555size_t
556sockunion_get_addrlen(const union sockunion *su)
557{
558 return family2addrsize(sockunion_family(su));
559}
560
561const u_char *
562sockunion_get_addr(const union sockunion *su)
563{
564 switch (sockunion_family(su))
565 {
566 case AF_INET:
567 return (const u_char *) &su->sin.sin_addr.s_addr;
568#ifdef HAVE_IPV6
569 case AF_INET6:
570 return (const u_char *) &su->sin6.sin6_addr;
571#endif /* HAVE_IPV6 */
572 }
573 return NULL;
574}
575
576void
577sockunion_set(union sockunion *su, int family, const u_char *addr, size_t bytes)
578{
579 if (family2addrsize(family) != bytes)
580 return;
581
582 sockunion_family(su) = family;
583 switch (family)
584 {
585 case AF_INET:
586 memcpy(&su->sin.sin_addr.s_addr, addr, bytes);
587 break;
588#ifdef HAVE_IPV6
589 case AF_INET6:
590 memcpy(&su->sin6.sin6_addr, addr, bytes);
591 break;
592#endif /* HAVE_IPV6 */
593 }
594}
595
718e3744 596/* After TCP connection is established. Get local address and port. */
597union sockunion *
598sockunion_getsockname (int fd)
599{
600 int ret;
22528299 601 socklen_t len;
718e3744 602 union
603 {
604 struct sockaddr sa;
605 struct sockaddr_in sin;
606#ifdef HAVE_IPV6
607 struct sockaddr_in6 sin6;
608#endif /* HAVE_IPV6 */
609 char tmp_buffer[128];
610 } name;
611 union sockunion *su;
612
613 memset (&name, 0, sizeof name);
614 len = sizeof name;
615
616 ret = getsockname (fd, (struct sockaddr *)&name, &len);
617 if (ret < 0)
618 {
619 zlog_warn ("Can't get local address and port by getsockname: %s",
6099b3b5 620 safe_strerror (errno));
718e3744 621 return NULL;
622 }
623
624 if (name.sa.sa_family == AF_INET)
625 {
2ba9a37a 626 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
718e3744 627 memcpy (su, &name, sizeof (struct sockaddr_in));
628 return su;
629 }
630#ifdef HAVE_IPV6
631 if (name.sa.sa_family == AF_INET6)
632 {
2ba9a37a 633 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
718e3744 634 memcpy (su, &name, sizeof (struct sockaddr_in6));
1a7dcf42 635 sockunion_normalise_mapped (su);
718e3744 636 return su;
637 }
638#endif /* HAVE_IPV6 */
639 return NULL;
640}
641
642/* After TCP connection is established. Get remote address and port. */
643union sockunion *
644sockunion_getpeername (int fd)
645{
646 int ret;
22528299 647 socklen_t len;
718e3744 648 union
649 {
650 struct sockaddr sa;
651 struct sockaddr_in sin;
652#ifdef HAVE_IPV6
653 struct sockaddr_in6 sin6;
654#endif /* HAVE_IPV6 */
655 char tmp_buffer[128];
656 } name;
657 union sockunion *su;
658
659 memset (&name, 0, sizeof name);
660 len = sizeof name;
661 ret = getpeername (fd, (struct sockaddr *)&name, &len);
662 if (ret < 0)
663 {
664 zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s",
6099b3b5 665 safe_strerror (errno));
718e3744 666 return NULL;
667 }
668
669 if (name.sa.sa_family == AF_INET)
670 {
2ba9a37a 671 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
718e3744 672 memcpy (su, &name, sizeof (struct sockaddr_in));
673 return su;
674 }
675#ifdef HAVE_IPV6
676 if (name.sa.sa_family == AF_INET6)
677 {
2ba9a37a 678 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
718e3744 679 memcpy (su, &name, sizeof (struct sockaddr_in6));
1a7dcf42 680 sockunion_normalise_mapped (su);
718e3744 681 return su;
682 }
683#endif /* HAVE_IPV6 */
684 return NULL;
685}
686
687/* Print sockunion structure */
8cc4198f 688static void __attribute__ ((unused))
c0d8db4d 689sockunion_print (const union sockunion *su)
718e3744 690{
691 if (su == NULL)
692 return;
693
694 switch (su->sa.sa_family)
695 {
696 case AF_INET:
697 printf ("%s\n", inet_ntoa (su->sin.sin_addr));
698 break;
699#ifdef HAVE_IPV6
700 case AF_INET6:
701 {
42d49865 702 char buf [SU_ADDRSTRLEN];
718e3744 703
704 printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr),
705 buf, sizeof (buf)));
706 }
707 break;
708#endif /* HAVE_IPV6 */
709
710#ifdef AF_LINK
711 case AF_LINK:
712 {
713 struct sockaddr_dl *sdl;
714
715 sdl = (struct sockaddr_dl *)&(su->sa);
716 printf ("link#%d\n", sdl->sdl_index);
717 }
718 break;
719#endif /* AF_LINK */
720 default:
721 printf ("af_unknown %d\n", su->sa.sa_family);
722 break;
723 }
724}
725
726#ifdef HAVE_IPV6
8cc4198f 727static int
c0d8db4d 728in6addr_cmp (const struct in6_addr *addr1, const struct in6_addr *addr2)
718e3744 729{
8c328f11 730 unsigned int i;
35dece84 731 const u_char *p1, *p2;
718e3744 732
35dece84
DS
733 p1 = (const u_char *)addr1;
734 p2 = (const u_char *)addr2;
718e3744 735
736 for (i = 0; i < sizeof (struct in6_addr); i++)
737 {
738 if (p1[i] > p2[i])
739 return 1;
740 else if (p1[i] < p2[i])
741 return -1;
742 }
743 return 0;
744}
745#endif /* HAVE_IPV6 */
746
747int
c0d8db4d 748sockunion_cmp (const union sockunion *su1, const union sockunion *su2)
718e3744 749{
750 if (su1->sa.sa_family > su2->sa.sa_family)
751 return 1;
752 if (su1->sa.sa_family < su2->sa.sa_family)
753 return -1;
754
755 if (su1->sa.sa_family == AF_INET)
756 {
12829328 757 if (ntohl (sockunion2ip (su1)) == ntohl (sockunion2ip (su2)))
718e3744 758 return 0;
12829328 759 if (ntohl (sockunion2ip (su1)) > ntohl (sockunion2ip (su2)))
718e3744 760 return 1;
761 else
762 return -1;
763 }
764#ifdef HAVE_IPV6
765 if (su1->sa.sa_family == AF_INET6)
766 return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
767#endif /* HAVE_IPV6 */
768 return 0;
769}
770
771/* Duplicate sockunion. */
772union sockunion *
c0d8db4d 773sockunion_dup (const union sockunion *su)
718e3744 774{
775 union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
776 memcpy (dup, su, sizeof (union sockunion));
777 return dup;
778}
779
780void
781sockunion_free (union sockunion *su)
782{
783 XFREE (MTYPE_SOCKUNION, su);
784}
dd793e4a
DW
785
786void
787sockunion_init (union sockunion *su)
788{
789 memset(su, 0, sizeof(union sockunion));
790}