1 /* Socket union related function.
2 * Copyright (c) 1997, 98 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
26 #include "sockunion.h"
32 #ifndef HAVE_INET_ATON
34 inet_aton (const char *cp, struct in_addr *inaddr)
37 register u_long addr = 0;
38 register u_long val = 0, base = 10;
42 register char c = *cp;
46 case '0': case '1': case '2': case '3': case '4': case '5':
47 case '6': case '7': case '8': case '9':
48 val = (val * base) + (c - '0');
56 addr = addr << 8 | val;
65 addr <<= 8 * (3 - dots);
67 inaddr->s_addr = htonl (addr);
70 #endif /* ! HAVE_INET_ATON */
73 #ifndef HAVE_INET_PTON
75 inet_pton (int family, const char *strptr, void *addrptr)
77 if (family == AF_INET)
79 struct in_addr in_val;
81 if (inet_aton (strptr, &in_val))
83 memcpy (addrptr, &in_val, sizeof (struct in_addr));
91 #endif /* ! HAVE_INET_PTON */
93 #ifndef HAVE_INET_NTOP
95 inet_ntop (int family, const void *addrptr, char *strptr, size_t len)
97 unsigned char *p = (unsigned char *) addrptr;
99 if (family == AF_INET)
101 char temp[INET_ADDRSTRLEN];
103 snprintf(temp, sizeof(temp), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
105 if (strlen(temp) >= len)
110 strcpy(strptr, temp);
114 errno = EAFNOSUPPORT;
117 #endif /* ! HAVE_INET_NTOP */
120 inet_sutop (const union sockunion *su, char *str)
122 switch (su->sa.sa_family)
125 inet_ntop (AF_INET, &su->sin.sin_addr, str, INET_ADDRSTRLEN);
129 inet_ntop (AF_INET6, &su->sin6.sin6_addr, str, INET6_ADDRSTRLEN);
131 #endif /* HAVE_IPV6 */
137 str2sockunion (const char *str, union sockunion *su)
141 memset (su, 0, sizeof (union sockunion));
143 ret = inet_pton (AF_INET, str, &su->sin.sin_addr);
144 if (ret > 0) /* Valid IPv4 address format. */
146 su->sin.sin_family = AF_INET;
147 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
148 su->sin.sin_len = sizeof(struct sockaddr_in);
149 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
153 ret = inet_pton (AF_INET6, str, &su->sin6.sin6_addr);
154 if (ret > 0) /* Valid IPv6 address format. */
156 su->sin6.sin6_family = AF_INET6;
158 su->sin6.sin6_len = sizeof(struct sockaddr_in6);
159 #endif /* SIN6_LEN */
162 #endif /* HAVE_IPV6 */
167 sockunion2str (const union sockunion *su, char *buf, size_t len)
169 switch (sockunion_family(su))
172 snprintf (buf, len, "(unspec)");
175 return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len);
178 return inet_ntop (AF_INET6, &su->sin6.sin6_addr, buf, len);
179 #endif /* HAVE_IPV6 */
181 snprintf (buf, len, "(af %d)", sockunion_family(su));
186 sockunion_str2su (const char *str)
188 union sockunion *su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
190 if (!str2sockunion (str, su))
193 XFREE (MTYPE_SOCKUNION, su);
197 /* Convert IPv4 compatible IPv6 address to IPv4 address. */
199 sockunion_normalise_mapped (union sockunion *su)
201 struct sockaddr_in sin;
204 if (su->sa.sa_family == AF_INET6
205 && IN6_IS_ADDR_V4MAPPED (&su->sin6.sin6_addr))
207 memset (&sin, 0, sizeof (struct sockaddr_in));
208 sin.sin_family = AF_INET;
209 sin.sin_port = su->sin6.sin6_port;
210 memcpy (&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
211 memcpy (su, &sin, sizeof (struct sockaddr_in));
213 #endif /* HAVE_IPV6 */
216 /* Return socket of sockunion. */
218 sockunion_socket (const union sockunion *su)
222 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
225 zlog (NULL, LOG_WARNING, "Can't make socket : %s", safe_strerror (errno));
232 /* Return accepted new socket file descriptor. */
234 sockunion_accept (int sock, union sockunion *su)
239 len = sizeof (union sockunion);
240 client_sock = accept (sock, (struct sockaddr *) su, &len);
242 sockunion_normalise_mapped (su);
246 /* Return sizeof union sockunion. */
248 sockunion_sizeof (const union sockunion *su)
253 switch (su->sa.sa_family)
256 ret = sizeof (struct sockaddr_in);
260 ret = sizeof (struct sockaddr_in6);
262 #endif /* AF_INET6 */
267 /* return sockunion structure : this function should be revised. */
269 sockunion_log (const union sockunion *su, char *buf, size_t len)
271 switch (su->sa.sa_family)
274 return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len);
278 return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len);
280 #endif /* HAVE_IPV6 */
283 snprintf (buf, len, "af_unknown %d ", su->sa.sa_family);
288 /* sockunion_connect returns
291 1 : connect is in progress */
293 sockunion_connect (int fd, const union sockunion *peersu, unsigned short port,
300 memcpy (&su, peersu, sizeof (union sockunion));
302 switch (su.sa.sa_family)
305 su.sin.sin_port = port;
309 su.sin6.sin6_port = port;
311 if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex)
313 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
314 /* su.sin6.sin6_scope_id = ifindex; */
315 #endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */
316 SET_IN6_LINKLOCAL_IFINDEX (su.sin6.sin6_addr, ifindex);
320 #endif /* HAVE_IPV6 */
323 /* Make socket non-block. */
324 val = fcntl (fd, F_GETFL, 0);
325 fcntl (fd, F_SETFL, val|O_NONBLOCK);
327 /* Call connect function. */
328 ret = connect (fd, (struct sockaddr *) &su, sockunion_sizeof (&su));
330 /* Immediate success */
333 fcntl (fd, F_SETFL, val);
334 return connect_success;
337 /* If connect is in progress then return 1 else it's real error. */
340 if (errno != EINPROGRESS)
342 char str[SU_ADDRSTRLEN];
343 zlog_info ("can't connect to %s fd %d : %s",
344 sockunion_log (&su, str, sizeof str),
345 fd, safe_strerror (errno));
346 return connect_error;
350 fcntl (fd, F_SETFL, val);
352 return connect_in_progress;
355 /* Make socket from sockunion union. */
357 sockunion_stream_socket (union sockunion *su)
361 if (su->sa.sa_family == 0)
362 su->sa.sa_family = AF_INET_UNION;
364 sock = socket (su->sa.sa_family, SOCK_STREAM, 0);
367 zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket");
372 /* Bind socket to specified address. */
374 sockunion_bind (int sock, union sockunion *su, unsigned short port,
375 union sockunion *su_addr)
380 if (su->sa.sa_family == AF_INET)
382 size = sizeof (struct sockaddr_in);
383 su->sin.sin_port = htons (port);
384 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
385 su->sin.sin_len = size;
386 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
388 sockunion2ip (su) = htonl (INADDR_ANY);
391 else if (su->sa.sa_family == AF_INET6)
393 size = sizeof (struct sockaddr_in6);
394 su->sin6.sin6_port = htons (port);
396 su->sin6.sin6_len = size;
397 #endif /* SIN6_LEN */
401 memset (&su->sin6.sin6_addr, 0, sizeof (struct in6_addr));
403 su->sin6.sin6_addr = in6addr_any;
404 #endif /* LINUX_IPV6 */
407 #endif /* HAVE_IPV6 */
410 ret = bind (sock, (struct sockaddr *)su, size);
412 zlog (NULL, LOG_WARNING, "can't bind socket : %s", safe_strerror (errno));
418 sockopt_reuseaddr (int sock)
423 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
424 (void *) &on, sizeof (on));
427 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock);
435 sockopt_reuseport (int sock)
440 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEPORT,
441 (void *) &on, sizeof (on));
444 zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEPORT to socket %d", sock);
451 sockopt_reuseport (int sock)
458 sockopt_ttl (int family, int sock, int ttl)
463 if (family == AF_INET)
465 ret = setsockopt (sock, IPPROTO_IP, IP_TTL,
466 (void *) &ttl, sizeof (int));
469 zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock);
476 if (family == AF_INET6)
478 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
479 (void *) &ttl, sizeof (int));
482 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
488 #endif /* HAVE_IPV6 */
493 sockopt_cork (int sock, int onoff)
496 return setsockopt (sock, IPPROTO_TCP, TCP_CORK, &onoff, sizeof(onoff));
503 sockopt_minttl (int family, int sock, int minttl)
506 if (family == AF_INET)
508 int ret = setsockopt (sock, IPPROTO_IP, IP_MINTTL, &minttl, sizeof(minttl));
510 zlog (NULL, LOG_WARNING,
511 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
512 minttl, sock, safe_strerror (errno));
515 #endif /* IP_MINTTL */
516 #ifdef IPV6_MINHOPCNT
517 if (family == AF_INET6)
519 int ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MINHOPCNT, &minttl, sizeof(minttl));
521 zlog (NULL, LOG_WARNING,
522 "can't set sockopt IPV6_MINHOPCNT to %d on socket %d: %s",
523 minttl, sock, safe_strerror (errno));
533 sockopt_v6only (int family, int sock)
539 if (family == AF_INET6)
541 ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
542 (void *) &on, sizeof (int));
545 zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_V6ONLY "
546 "to socket %d", sock);
551 #endif /* IPV6_V6ONLY */
552 #endif /* HAVE_IPV6 */
556 /* If same family and same prefix return 1. */
558 sockunion_same (const union sockunion *su1, const union sockunion *su2)
562 if (su1->sa.sa_family != su2->sa.sa_family)
565 switch (su1->sa.sa_family)
568 ret = memcmp (&su1->sin.sin_addr, &su2->sin.sin_addr,
569 sizeof (struct in_addr));
573 ret = memcmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
574 sizeof (struct in6_addr));
576 #endif /* HAVE_IPV6 */
585 sockunion_hash (const union sockunion *su)
587 switch (sockunion_family(su))
590 return jhash_1word(su->sin.sin_addr.s_addr, 0);
593 return jhash2(su->sin6.sin6_addr.s6_addr32, ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0);
594 #endif /* HAVE_IPV6 */
600 family2addrsize(int family)
605 return sizeof(struct in_addr);
608 return sizeof(struct in6_addr);
609 #endif /* HAVE_IPV6 */
615 sockunion_get_addrlen(const union sockunion *su)
617 return family2addrsize(sockunion_family(su));
621 sockunion_get_addr(const union sockunion *su)
623 switch (sockunion_family(su))
626 return (const u_char *) &su->sin.sin_addr.s_addr;
629 return (const u_char *) &su->sin6.sin6_addr;
630 #endif /* HAVE_IPV6 */
636 sockunion_get_port (const union sockunion *su)
638 switch (sockunion_family (su))
641 return ntohs(su->sin.sin_port);
644 return ntohs(su->sin6.sin6_port);
645 #endif /* HAVE_IPV6 */
651 sockunion_set(union sockunion *su, int family, const u_char *addr, size_t bytes)
653 if (family2addrsize(family) != bytes)
656 sockunion_family(su) = family;
660 memcpy(&su->sin.sin_addr.s_addr, addr, bytes);
664 memcpy(&su->sin6.sin6_addr, addr, bytes);
666 #endif /* HAVE_IPV6 */
670 /* After TCP connection is established. Get local address and port. */
672 sockunion_getsockname (int fd)
679 struct sockaddr_in sin;
681 struct sockaddr_in6 sin6;
682 #endif /* HAVE_IPV6 */
683 char tmp_buffer[128];
687 memset (&name, 0, sizeof name);
690 ret = getsockname (fd, (struct sockaddr *)&name, &len);
693 zlog_warn ("Can't get local address and port by getsockname: %s",
694 safe_strerror (errno));
698 if (name.sa.sa_family == AF_INET)
700 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
701 memcpy (su, &name, sizeof (struct sockaddr_in));
705 if (name.sa.sa_family == AF_INET6)
707 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
708 memcpy (su, &name, sizeof (struct sockaddr_in6));
709 sockunion_normalise_mapped (su);
712 #endif /* HAVE_IPV6 */
716 /* After TCP connection is established. Get remote address and port. */
718 sockunion_getpeername (int fd)
725 struct sockaddr_in sin;
727 struct sockaddr_in6 sin6;
728 #endif /* HAVE_IPV6 */
729 char tmp_buffer[128];
733 memset (&name, 0, sizeof name);
735 ret = getpeername (fd, (struct sockaddr *)&name, &len);
738 zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s",
739 safe_strerror (errno));
743 if (name.sa.sa_family == AF_INET)
745 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
746 memcpy (su, &name, sizeof (struct sockaddr_in));
750 if (name.sa.sa_family == AF_INET6)
752 su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
753 memcpy (su, &name, sizeof (struct sockaddr_in6));
754 sockunion_normalise_mapped (su);
757 #endif /* HAVE_IPV6 */
761 /* Print sockunion structure */
762 static void __attribute__ ((unused))
763 sockunion_print (const union sockunion *su)
768 switch (su->sa.sa_family)
771 printf ("%s\n", inet_ntoa (su->sin.sin_addr));
776 char buf [SU_ADDRSTRLEN];
778 printf ("%s\n", inet_ntop (AF_INET6, &(su->sin6.sin6_addr),
782 #endif /* HAVE_IPV6 */
787 struct sockaddr_dl *sdl;
789 sdl = (struct sockaddr_dl *)&(su->sa);
790 printf ("link#%d\n", sdl->sdl_index);
795 printf ("af_unknown %d\n", su->sa.sa_family);
802 in6addr_cmp (const struct in6_addr *addr1, const struct in6_addr *addr2)
807 p1 = (u_char *)addr1;
808 p2 = (u_char *)addr2;
810 for (i = 0; i < sizeof (struct in6_addr); i++)
814 else if (p1[i] < p2[i])
819 #endif /* HAVE_IPV6 */
822 sockunion_cmp (const union sockunion *su1, const union sockunion *su2)
824 if (su1->sa.sa_family > su2->sa.sa_family)
826 if (su1->sa.sa_family < su2->sa.sa_family)
829 if (su1->sa.sa_family == AF_INET)
831 if (ntohl (sockunion2ip (su1)) == ntohl (sockunion2ip (su2)))
833 if (ntohl (sockunion2ip (su1)) > ntohl (sockunion2ip (su2)))
839 if (su1->sa.sa_family == AF_INET6)
840 return in6addr_cmp (&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
841 #endif /* HAVE_IPV6 */
845 /* Duplicate sockunion. */
847 sockunion_dup (const union sockunion *su)
849 union sockunion *dup = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
850 memcpy (dup, su, sizeof (union sockunion));
855 sockunion_free (union sockunion *su)
857 XFREE (MTYPE_SOCKUNION, su);