blob: 2137162464026c77178d0f9cce3c23e2c12763ba [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
136str2sockunion (char *str, union sockunion *su)
137{
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 *
178sockunion_str2su (char *str)
179{
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{
214 char str[INET6_ADDRSTRLEN];
215
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 {
239 zlog (NULL, LOG_WARNING, "Can't make socket : %s", strerror (errno));
240 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. */
276int
277sockunion_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. */
297char *
298sockunion_log (union sockunion *su)
299{
300 static char buf[SU_ADDRSTRLEN];
301
302 switch (su->sa.sa_family)
303 {
304 case AF_INET:
305 snprintf (buf, BUFSIZ, "%s", inet_ntoa (su->sin.sin_addr));
306 break;
307#ifdef HAVE_IPV6
308 case AF_INET6:
309 snprintf (buf, BUFSIZ, "%s",
310 inet_ntop (AF_INET6, &(su->sin6.sin6_addr), buf, BUFSIZ));
311 break;
312#endif /* HAVE_IPV6 */
313 default:
314 snprintf (buf, BUFSIZ, "af_unknown %d ", su->sa.sa_family);
315 break;
316 }
317 return buf;
318}
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; */
347#endif /* HAVE_SIN6_SCOPE_ID */
348 SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex);
349 }
350#endif /* KAME */
351 break;
352#endif /* HAVE_IPV6 */
353 }
354
355 /* Make socket non-block. */
356 val = fcntl (fd, F_GETFL, 0);
357 fcntl (fd, F_SETFL, val|O_NONBLOCK);
358
359 /* Call connect function. */
360 ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su));
361
362 /* Immediate success */
363 if (ret == 0)
364 {
365 fcntl (fd, F_SETFL, val);
366 return connect_success;
367 }
368
369 /* If connect is in progress then return 1 else it's real error. */
370 if (ret < 0)
371 {
372 if (errno != EINPROGRESS)
373 {
374 zlog_info ("can't connect to %s fd %d : %s",
375 sockunion_log (&su), fd, strerror (errno));
376 return connect_error;
377 }
378 }
379
380 fcntl (fd, F_SETFL, val);
381
382 return connect_in_progress;
383}
384
385/* Make socket from sockunion union. */
386int
387sockunion_stream_socket (union sockunion *su)
388{
389 int sock;
390
391 if (su->sa.sa_family == 0)
392 su->sa.sa_family = AF_INET_UNION;
393
394 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
395
396 if (sock < 0)
397 zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket");
398
399 return sock;
400}
401
402/* Bind socket to specified address. */
403int
404sockunion_bind (int sock, union sockunion *su, unsigned short port,
405 union sockunion *su_addr)
406{
407 int size = 0;
408 int ret;
409
410 if (su->sa.sa_family == AF_INET)
411 {
412 size = sizeof (struct sockaddr_in);
413 su->sin.sin_port = htons (port);
414#ifdef HAVE_SIN_LEN
415 su->sin.sin_len = size;
416#endif /* HAVE_SIN_LEN */
417 if (su_addr == NULL)
418 su->sin.sin_addr.s_addr = htonl (INADDR_ANY);
419 }
420#ifdef HAVE_IPV6
421 else if (su->sa.sa_family == AF_INET6)
422 {
423 size = sizeof (struct sockaddr_in6);
424 su->sin6.sin6_port = htons (port);
425#ifdef SIN6_LEN
426 su->sin6.sin6_len = size;
427#endif /* SIN6_LEN */
428 if (su_addr == NULL)
429 {
430#if defined(LINUX_IPV6) || defined(NRL)
431 memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr));
432#else
433 su->sin6.sin6_addr = in6addr_any;
434#endif /* LINUX_IPV6 */
435 }
436 }
437#endif /* HAVE_IPV6 */
438
439
440 ret = bind (sock, (struct sockaddr *)su, size);
441 if (ret < 0)
442 zlog (NULL, LOG_WARNING, "can't bind socket : %s", strerror (errno));
443
444 return ret;
445}
446
447int
448sockopt_reuseaddr (int sock)
449{
450 int ret;
451 int on = 1;
452
453 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
454 (void *) &on, sizeof (on));
455 if (ret < 0)
456 {
457 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock);
458 return -1;
459 }
460 return 0;
461}
462
463#ifdef SO_REUSEPORT
464int
465sockopt_reuseport (int sock)
466{
467 int ret;
468 int on = 1;
469
470 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT,
471 (void *) &on, sizeof (on));
472 if (ret < 0)
473 {
474 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock);
475 return -1;
476 }
477 return 0;
478}
479#else
480int
481sockopt_reuseport (int sock)
482{
483 return 0;
484}
485#endif /* 0 */
486
487int
488sockopt_ttl (int family, int sock, int ttl)
489{
490 int ret;
491
492#ifdef IP_TTL
493 if (family == AF_INET)
494 {
495 ret = setsockopt (sock, IPPROTO_IP, IP_TTL,
496 (void *) &ttl, sizeof (int));
497 if (ret < 0)
498 {
499 zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock);
500 return -1;
501 }
502 return 0;
503 }
504#endif /* IP_TTL */
505#ifdef HAVE_IPV6
506 if (family == AF_INET6)
507 {
508 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
509 (void *) &ttl, sizeof (int));
510 if (ret < 0)
511 {
512 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
513 ttl, sock);
514 return -1;
515 }
516 return 0;
517 }
518#endif /* HAVE_IPV6 */
519 return 0;
520}
521
522/* If same family and same prefix return 1. */
523int
524sockunion_same (union sockunion *su1, union sockunion *su2)
525{
526 int ret = 0;
527
528 if (su1->sa.sa_family != su2->sa.sa_family)
529 return 0;
530
531 switch (su1->sa.sa_family)
532 {
533 case AF_INET:
534 ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr,
535 sizeof (struct in_addr));
536 break;
537#ifdef HAVE_IPV6
538 case AF_INET6:
539 ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
540 sizeof (struct in6_addr));
541 break;
542#endif /* HAVE_IPV6 */
543 }
544 if (ret == 0)
545 return 1;
546 else
547 return 0;
548}
549
550/* After TCP connection is established. Get local address and port. */
551union sockunion *
552sockunion_getsockname (int fd)
553{
554 int ret;
555 int len;
556 union
557 {
558 struct sockaddr sa;
559 struct sockaddr_in sin;
560#ifdef HAVE_IPV6
561 struct sockaddr_in6 sin6;
562#endif /* HAVE_IPV6 */
563 char tmp_buffer[128];
564 } name;
565 union sockunion *su;
566
567 memset (&name, 0, sizeof name);
568 len = sizeof name;
569
570 ret = getsockname (fd, (struct sockaddr *)&name, &len);
571 if (ret < 0)
572 {
573 zlog_warn ("Can't get local address and port by getsockname: %s",
574 strerror (errno));
575 return NULL;
576 }
577
578 if (name.sa.sa_family == AF_INET)
579 {
580 su = XCALLOC (MTYPE_TMP, sizeof (union sockunion));
581 memcpy (su, &name, sizeof (struct sockaddr_in));
582 return su;
583 }
584#ifdef HAVE_IPV6
585 if (name.sa.sa_family == AF_INET6)
586 {
587 su = XCALLOC (MTYPE_TMP, sizeof (union sockunion));
588 memcpy (su, &name, sizeof (struct sockaddr_in6));
589
590 if (IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
591 {
592 struct sockaddr_in sin;
593
594 sin.sin_family = AF_INET;
595 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
596 sin.sin_port = su->sin6.sin6_port;
597 memcpy (su, &sin, sizeof (struct sockaddr_in));
598 }
599 return su;
600 }
601#endif /* HAVE_IPV6 */
602 return NULL;
603}
604
605/* After TCP connection is established. Get remote address and port. */
606union sockunion *
607sockunion_getpeername (int fd)
608{
609 int ret;
610 int len;
611 union
612 {
613 struct sockaddr sa;
614 struct sockaddr_in sin;
615#ifdef HAVE_IPV6
616 struct sockaddr_in6 sin6;
617#endif /* HAVE_IPV6 */
618 char tmp_buffer[128];
619 } name;
620 union sockunion *su;
621
622 memset (&name, 0, sizeof name);
623 len = sizeof name;
624 ret = getpeername (fd, (struct sockaddr *)&name, &len);
625 if (ret < 0)
626 {
627 zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s",
628 strerror (errno));
629 return NULL;
630 }
631
632 if (name.sa.sa_family == AF_INET)
633 {
634 su = XCALLOC (MTYPE_TMP, sizeof (union sockunion));
635 memcpy (su, &name, sizeof (struct sockaddr_in));
636 return su;
637 }
638#ifdef HAVE_IPV6
639 if (name.sa.sa_family == AF_INET6)
640 {
641 su = XCALLOC (MTYPE_TMP, sizeof (union sockunion));
642 memcpy (su, &name, sizeof (struct sockaddr_in6));
643
644 if (IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
645 {
646 struct sockaddr_in sin;
647
648 sin.sin_family = AF_INET;
649 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
650 sin.sin_port = su->sin6.sin6_port;
651 memcpy (su, &sin, sizeof (struct sockaddr_in));
652 }
653 return su;
654 }
655#endif /* HAVE_IPV6 */
656 return NULL;
657}
658
659/* Print sockunion structure */
660void
661sockunion_print (union sockunion *su)
662{
663 if (su == NULL)
664 return;
665
666 switch (su->sa.sa_family)
667 {
668 case AF_INET:
669 printf ("%s\n", inet_ntoa (su->sin.sin_addr));
670 break;
671#ifdef HAVE_IPV6
672 case AF_INET6:
673 {
674 char buf [64];
675
676 printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr),
677 buf, sizeof (buf)));
678 }
679 break;
680#endif /* HAVE_IPV6 */
681
682#ifdef AF_LINK
683 case AF_LINK:
684 {
685 struct sockaddr_dl *sdl;
686
687 sdl = (struct sockaddr_dl *)&(su->sa);
688 printf ("link#%d\n", sdl->sdl_index);
689 }
690 break;
691#endif /* AF_LINK */
692 default:
693 printf ("af_unknown %d\n", su->sa.sa_family);
694 break;
695 }
696}
697
698#ifdef HAVE_IPV6
699int
700in6addr_cmp (struct in6_addr *addr1, struct in6_addr *addr2)
701{
702 int i;
703 u_char *p1, *p2;
704
705 p1 = (u_char *)addr1;
706 p2 = (u_char *)addr2;
707
708 for (i = 0; i < sizeof (struct in6_addr); i++)
709 {
710 if (p1[i] > p2[i])
711 return 1;
712 else if (p1[i] < p2[i])
713 return -1;
714 }
715 return 0;
716}
717#endif /* HAVE_IPV6 */
718
719int
720sockunion_cmp (union sockunion *su1, union sockunion *su2)
721{
722 if (su1->sa.sa_family > su2->sa.sa_family)
723 return 1;
724 if (su1->sa.sa_family < su2->sa.sa_family)
725 return -1;
726
727 if (su1->sa.sa_family == AF_INET)
728 {
729 if (ntohl (su1->sin.sin_addr.s_addr) == ntohl (su2->sin.sin_addr.s_addr))
730 return 0;
731 if (ntohl (su1->sin.sin_addr.s_addr) > ntohl (su2->sin.sin_addr.s_addr))
732 return 1;
733 else
734 return -1;
735 }
736#ifdef HAVE_IPV6
737 if (su1->sa.sa_family == AF_INET6)
738 return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
739#endif /* HAVE_IPV6 */
740 return 0;
741}
742
743/* Duplicate sockunion. */
744union sockunion *
745sockunion_dup (union sockunion *su)
746{
747 union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
748 memcpy (dup, su, sizeof (union sockunion));
749 return dup;
750}
751
752void
753sockunion_free (union sockunion *su)
754{
755 XFREE (MTYPE_SOCKUNION, su);
756}