blob: cfd3bf9ad395b6fb952e6cb094056b878dd90d2e [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
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. */
Paul Jakma0df7c912008-07-21 21:02:49 +0000257#if 0
paul718e3742002-12-13 20:15:29 +0000258#ifdef HAVE_IPV6
259 if (su->sa.sa_family == AF_INET6)
260 {
261 if (IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
262 {
263 struct sockaddr_in sin;
264
265 memset (&sin, 0, sizeof (struct sockaddr_in));
266 sin.sin_family = AF_INET;
267 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
268 memcpy (su, &sin, sizeof (struct sockaddr_in));
269 }
270 }
271#endif /* HAVE_IPV6 */
Paul Jakma0df7c912008-07-21 21:02:49 +0000272#endif
paul718e3742002-12-13 20:15:29 +0000273 return client_sock;
274}
275
276/* Return sizeof union sockunion. */
paul8cc41982005-05-06 21:25:49 +0000277static int
paul718e3742002-12-13 20:15:29 +0000278sockunion_sizeof (union sockunion *su)
279{
280 int ret;
281
282 ret = 0;
283 switch (su->sa.sa_family)
284 {
285 case AF_INET:
286 ret = sizeof (struct sockaddr_in);
287 break;
288#ifdef HAVE_IPV6
289 case AF_INET6:
290 ret = sizeof (struct sockaddr_in6);
291 break;
292#endif /* AF_INET6 */
293 }
294 return ret;
295}
296
297/* return sockunion structure : this function should be revised. */
paul8cc41982005-05-06 21:25:49 +0000298static char *
paul718e3742002-12-13 20:15:29 +0000299sockunion_log (union sockunion *su)
300{
301 static char buf[SU_ADDRSTRLEN];
302
303 switch (su->sa.sa_family)
304 {
305 case AF_INET:
paul239a6712003-05-24 11:50:50 +0000306 snprintf (buf, SU_ADDRSTRLEN, "%s", inet_ntoa (su->sin.sin_addr));
paul718e3742002-12-13 20:15:29 +0000307 break;
308#ifdef HAVE_IPV6
309 case AF_INET6:
paul239a6712003-05-24 11:50:50 +0000310 snprintf (buf, SU_ADDRSTRLEN, "%s",
311 inet_ntop (AF_INET6, &(su->sin6.sin6_addr), buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +0000312 break;
313#endif /* HAVE_IPV6 */
314 default:
paul239a6712003-05-24 11:50:50 +0000315 snprintf (buf, SU_ADDRSTRLEN, "af_unknown %d ", su->sa.sa_family);
paul718e3742002-12-13 20:15:29 +0000316 break;
317 }
Paul Jakma5a54df92006-02-21 01:37:14 +0000318 return (XSTRDUP (MTYPE_TMP, buf));
paul718e3742002-12-13 20:15:29 +0000319}
320
321/* sockunion_connect returns
322 -1 : error occured
323 0 : connect success
324 1 : connect is in progress */
325enum connect_result
326sockunion_connect (int fd, union sockunion *peersu, unsigned short port,
327 unsigned int ifindex)
328{
329 int ret;
330 int val;
331 union sockunion su;
332
333 memcpy (&su, peersu, sizeof (union sockunion));
334
335 switch (su.sa.sa_family)
336 {
337 case AF_INET:
338 su.sin.sin_port = port;
339 break;
340#ifdef HAVE_IPV6
341 case AF_INET6:
342 su.sin6.sin6_port = port;
343#ifdef KAME
344 if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex)
345 {
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000346#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
paul718e3742002-12-13 20:15:29 +0000347 /* su.sin6.sin6_scope_id = ifindex; */
hasso726f9b22003-05-25 21:04:54 +0000348#ifdef MUSICA
349 su.sin6.sin6_scope_id = ifindex;
350#endif
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000351#endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */
hasso726f9b22003-05-25 21:04:54 +0000352#ifndef MUSICA
paul718e3742002-12-13 20:15:29 +0000353 SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex);
hasso726f9b22003-05-25 21:04:54 +0000354#endif
paul718e3742002-12-13 20:15:29 +0000355 }
356#endif /* KAME */
357 break;
358#endif /* HAVE_IPV6 */
359 }
360
361 /* Make socket non-block. */
362 val = fcntl (fd, F_GETFL, 0);
363 fcntl (fd, F_SETFL, val|O_NONBLOCK);
364
365 /* Call connect function. */
366 ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su));
367
368 /* Immediate success */
369 if (ret == 0)
370 {
371 fcntl (fd, F_SETFL, val);
372 return connect_success;
373 }
374
375 /* If connect is in progress then return 1 else it's real error. */
376 if (ret < 0)
377 {
378 if (errno != EINPROGRESS)
379 {
380 zlog_info ("can't connect to %s fd %d : %s",
ajs6099b3b2004-11-20 02:06:59 +0000381 sockunion_log (&su), fd, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000382 return connect_error;
383 }
384 }
385
386 fcntl (fd, F_SETFL, val);
387
388 return connect_in_progress;
389}
390
391/* Make socket from sockunion union. */
392int
393sockunion_stream_socket (union sockunion *su)
394{
395 int sock;
396
397 if (su->sa.sa_family == 0)
398 su->sa.sa_family = AF_INET_UNION;
399
400 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
401
402 if (sock < 0)
403 zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket");
404
405 return sock;
406}
407
408/* Bind socket to specified address. */
409int
410sockunion_bind (int sock, union sockunion *su, unsigned short port,
411 union sockunion *su_addr)
412{
413 int size = 0;
414 int ret;
415
416 if (su->sa.sa_family == AF_INET)
417 {
418 size = sizeof (struct sockaddr_in);
419 su->sin.sin_port = htons (port);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000420#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000421 su->sin.sin_len = size;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000422#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000423 if (su_addr == NULL)
424 su->sin.sin_addr.s_addr = htonl (INADDR_ANY);
425 }
426#ifdef HAVE_IPV6
427 else if (su->sa.sa_family == AF_INET6)
428 {
429 size = sizeof (struct sockaddr_in6);
430 su->sin6.sin6_port = htons (port);
431#ifdef SIN6_LEN
432 su->sin6.sin6_len = size;
433#endif /* SIN6_LEN */
434 if (su_addr == NULL)
435 {
436#if defined(LINUX_IPV6) || defined(NRL)
437 memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr));
438#else
439 su->sin6.sin6_addr = in6addr_any;
440#endif /* LINUX_IPV6 */
441 }
442 }
443#endif /* HAVE_IPV6 */
444
445
446 ret = bind (sock, (struct sockaddr *)su, size);
447 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +0000448 zlog (NULL, LOG_WARNING, "can't bind socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000449
450 return ret;
451}
452
453int
454sockopt_reuseaddr (int sock)
455{
456 int ret;
457 int on = 1;
458
459 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
460 (void *) &on, sizeof (on));
461 if (ret < 0)
462 {
463 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock);
464 return -1;
465 }
466 return 0;
467}
468
469#ifdef SO_REUSEPORT
470int
471sockopt_reuseport (int sock)
472{
473 int ret;
474 int on = 1;
475
476 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT,
477 (void *) &on, sizeof (on));
478 if (ret < 0)
479 {
hassoe7fe8c82005-05-06 19:33:35 +0000480 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEPORT to socket %d", sock);
paul718e3742002-12-13 20:15:29 +0000481 return -1;
482 }
483 return 0;
484}
485#else
486int
487sockopt_reuseport (int sock)
488{
489 return 0;
490}
491#endif /* 0 */
492
493int
494sockopt_ttl (int family, int sock, int ttl)
495{
496 int ret;
497
498#ifdef IP_TTL
499 if (family == AF_INET)
500 {
501 ret = setsockopt (sock, IPPROTO_IP, IP_TTL,
502 (void *) &ttl, sizeof (int));
503 if (ret < 0)
504 {
505 zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock);
506 return -1;
507 }
508 return 0;
509 }
510#endif /* IP_TTL */
511#ifdef HAVE_IPV6
512 if (family == AF_INET6)
513 {
514 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
515 (void *) &ttl, sizeof (int));
516 if (ret < 0)
517 {
518 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
519 ttl, sock);
520 return -1;
521 }
522 return 0;
523 }
524#endif /* HAVE_IPV6 */
525 return 0;
526}
527
528/* If same family and same prefix return 1. */
529int
530sockunion_same (union sockunion *su1, union sockunion *su2)
531{
532 int ret = 0;
533
534 if (su1->sa.sa_family != su2->sa.sa_family)
535 return 0;
536
537 switch (su1->sa.sa_family)
538 {
539 case AF_INET:
540 ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr,
541 sizeof (struct in_addr));
542 break;
543#ifdef HAVE_IPV6
544 case AF_INET6:
545 ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
546 sizeof (struct in6_addr));
547 break;
548#endif /* HAVE_IPV6 */
549 }
550 if (ret == 0)
551 return 1;
552 else
553 return 0;
554}
555
556/* After TCP connection is established. Get local address and port. */
557union sockunion *
558sockunion_getsockname (int fd)
559{
560 int ret;
paul22528292004-05-08 05:10:38 +0000561 socklen_t len;
paul718e3742002-12-13 20:15:29 +0000562 union
563 {
564 struct sockaddr sa;
565 struct sockaddr_in sin;
566#ifdef HAVE_IPV6
567 struct sockaddr_in6 sin6;
568#endif /* HAVE_IPV6 */
569 char tmp_buffer[128];
570 } name;
571 union sockunion *su;
572
573 memset (&name, 0, sizeof name);
574 len = sizeof name;
575
576 ret = getsockname (fd, (struct sockaddr *)&name, &len);
577 if (ret < 0)
578 {
579 zlog_warn ("Can't get local address and port by getsockname: %s",
ajs6099b3b2004-11-20 02:06:59 +0000580 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000581 return NULL;
582 }
583
584 if (name.sa.sa_family == AF_INET)
585 {
paul2ba9a372005-05-19 01:37:50 +0000586 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000587 memcpy (su, &name, sizeof (struct sockaddr_in));
588 return su;
589 }
590#ifdef HAVE_IPV6
591 if (name.sa.sa_family == AF_INET6)
592 {
paul2ba9a372005-05-19 01:37:50 +0000593 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000594 memcpy (su, &name, sizeof (struct sockaddr_in6));
595
Paul Jakma0df7c912008-07-21 21:02:49 +0000596#if 0
paul718e3742002-12-13 20:15:29 +0000597 if (IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
598 {
599 struct sockaddr_in sin;
600
601 sin.sin_family = AF_INET;
602 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
603 sin.sin_port = su->sin6.sin6_port;
604 memcpy (su, &sin, sizeof (struct sockaddr_in));
605 }
Paul Jakma0df7c912008-07-21 21:02:49 +0000606#endif
paul718e3742002-12-13 20:15:29 +0000607 return su;
608 }
609#endif /* HAVE_IPV6 */
610 return NULL;
611}
612
613/* After TCP connection is established. Get remote address and port. */
614union sockunion *
615sockunion_getpeername (int fd)
616{
617 int ret;
paul22528292004-05-08 05:10:38 +0000618 socklen_t len;
paul718e3742002-12-13 20:15:29 +0000619 union
620 {
621 struct sockaddr sa;
622 struct sockaddr_in sin;
623#ifdef HAVE_IPV6
624 struct sockaddr_in6 sin6;
625#endif /* HAVE_IPV6 */
626 char tmp_buffer[128];
627 } name;
628 union sockunion *su;
629
630 memset (&name, 0, sizeof name);
631 len = sizeof name;
632 ret = getpeername (fd, (struct sockaddr *)&name, &len);
633 if (ret < 0)
634 {
635 zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s",
ajs6099b3b2004-11-20 02:06:59 +0000636 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000637 return NULL;
638 }
639
640 if (name.sa.sa_family == AF_INET)
641 {
paul2ba9a372005-05-19 01:37:50 +0000642 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000643 memcpy (su, &name, sizeof (struct sockaddr_in));
644 return su;
645 }
646#ifdef HAVE_IPV6
647 if (name.sa.sa_family == AF_INET6)
648 {
paul2ba9a372005-05-19 01:37:50 +0000649 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
paul718e3742002-12-13 20:15:29 +0000650 memcpy (su, &name, sizeof (struct sockaddr_in6));
Paul Jakma0df7c912008-07-21 21:02:49 +0000651#if 0
paul718e3742002-12-13 20:15:29 +0000652 if (IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
653 {
654 struct sockaddr_in sin;
655
656 sin.sin_family = AF_INET;
657 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
658 sin.sin_port = su->sin6.sin6_port;
659 memcpy (su, &sin, sizeof (struct sockaddr_in));
660 }
Paul Jakma0df7c912008-07-21 21:02:49 +0000661#endif
paul718e3742002-12-13 20:15:29 +0000662 return su;
663 }
664#endif /* HAVE_IPV6 */
665 return NULL;
666}
667
668/* Print sockunion structure */
paul8cc41982005-05-06 21:25:49 +0000669static void __attribute__ ((unused))
paul718e3742002-12-13 20:15:29 +0000670sockunion_print (union sockunion *su)
671{
672 if (su == NULL)
673 return;
674
675 switch (su->sa.sa_family)
676 {
677 case AF_INET:
678 printf ("%s\n", inet_ntoa (su->sin.sin_addr));
679 break;
680#ifdef HAVE_IPV6
681 case AF_INET6:
682 {
paul42d49862004-10-13 05:22:18 +0000683 char buf [SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +0000684
685 printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr),
686 buf, sizeof (buf)));
687 }
688 break;
689#endif /* HAVE_IPV6 */
690
691#ifdef AF_LINK
692 case AF_LINK:
693 {
694 struct sockaddr_dl *sdl;
695
696 sdl = (struct sockaddr_dl *)&(su->sa);
697 printf ("link#%d\n", sdl->sdl_index);
698 }
699 break;
700#endif /* AF_LINK */
701 default:
702 printf ("af_unknown %d\n", su->sa.sa_family);
703 break;
704 }
705}
706
707#ifdef HAVE_IPV6
paul8cc41982005-05-06 21:25:49 +0000708static int
paul718e3742002-12-13 20:15:29 +0000709in6addr_cmp (struct in6_addr *addr1, struct in6_addr *addr2)
710{
hasso8c328f12004-10-05 21:01:23 +0000711 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000712 u_char *p1, *p2;
713
714 p1 = (u_char *)addr1;
715 p2 = (u_char *)addr2;
716
717 for (i = 0; i < sizeof (struct in6_addr); i++)
718 {
719 if (p1[i] > p2[i])
720 return 1;
721 else if (p1[i] < p2[i])
722 return -1;
723 }
724 return 0;
725}
726#endif /* HAVE_IPV6 */
727
728int
729sockunion_cmp (union sockunion *su1, union sockunion *su2)
730{
731 if (su1->sa.sa_family > su2->sa.sa_family)
732 return 1;
733 if (su1->sa.sa_family < su2->sa.sa_family)
734 return -1;
735
736 if (su1->sa.sa_family == AF_INET)
737 {
738 if (ntohl (su1->sin.sin_addr.s_addr) == ntohl (su2->sin.sin_addr.s_addr))
739 return 0;
740 if (ntohl (su1->sin.sin_addr.s_addr) > ntohl (su2->sin.sin_addr.s_addr))
741 return 1;
742 else
743 return -1;
744 }
745#ifdef HAVE_IPV6
746 if (su1->sa.sa_family == AF_INET6)
747 return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
748#endif /* HAVE_IPV6 */
749 return 0;
750}
751
752/* Duplicate sockunion. */
753union sockunion *
754sockunion_dup (union sockunion *su)
755{
756 union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
757 memcpy (dup, su, sizeof (union sockunion));
758 return dup;
759}
760
761void
762sockunion_free (union sockunion *su)
763{
764 XFREE (MTYPE_SOCKUNION, su);
765}