paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Socket union header. |
| 3 | * Copyright (c) 1997 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifndef _ZEBRA_SOCKUNION_H |
| 24 | #define _ZEBRA_SOCKUNION_H |
| 25 | |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 26 | #include "if.h" |
| 27 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | #if 0 |
| 29 | union sockunion { |
| 30 | struct sockinet { |
| 31 | u_char si_len; |
| 32 | u_char si_family; |
| 33 | u_short si_port; |
| 34 | } su_si; |
| 35 | struct sockaddr_in su_sin; |
| 36 | struct sockaddr_in6 su_sin6; |
| 37 | }; |
| 38 | #define su_len su_si.si_len |
| 39 | #define su_family su_si.si_family |
| 40 | #define su_port su_si.si_port |
| 41 | #endif /* 0 */ |
| 42 | |
| 43 | union sockunion |
| 44 | { |
| 45 | struct sockaddr sa; |
| 46 | struct sockaddr_in sin; |
| 47 | #ifdef HAVE_IPV6 |
| 48 | struct sockaddr_in6 sin6; |
| 49 | #endif /* HAVE_IPV6 */ |
| 50 | }; |
| 51 | |
| 52 | enum connect_result |
| 53 | { |
| 54 | connect_error, |
| 55 | connect_success, |
| 56 | connect_in_progress |
| 57 | }; |
| 58 | |
| 59 | /* Default address family. */ |
| 60 | #ifdef HAVE_IPV6 |
| 61 | #define AF_INET_UNION AF_INET6 |
| 62 | #else |
| 63 | #define AF_INET_UNION AF_INET |
| 64 | #endif |
| 65 | |
| 66 | /* Sockunion address string length. Same as INET6_ADDRSTRLEN. */ |
| 67 | #define SU_ADDRSTRLEN 46 |
| 68 | |
| 69 | /* Macro to set link local index to the IPv6 address. For KAME IPv6 |
| 70 | stack. */ |
| 71 | #ifdef KAME |
| 72 | #define IN6_LINKLOCAL_IFINDEX(a) ((a).s6_addr[2] << 8 | (a).s6_addr[3]) |
| 73 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \ |
| 74 | do { \ |
| 75 | (a).s6_addr[2] = ((i) >> 8) & 0xff; \ |
| 76 | (a).s6_addr[3] = (i) & 0xff; \ |
| 77 | } while (0) |
| 78 | #else |
| 79 | #define IN6_LINKLOCAL_IFINDEX(a) |
| 80 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) |
| 81 | #endif /* KAME */ |
| 82 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 83 | #define sockunion_family(X) (X)->sa.sa_family |
| 84 | |
Jorge Boncompte [DTI2] | 0c5ed3e | 2012-04-10 16:57:22 +0200 | [diff] [blame] | 85 | #define sockunion2ip(X) (X)->sin.sin_addr.s_addr |
| 86 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 87 | /* Prototypes. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 88 | extern int str2sockunion (const char *, union sockunion *); |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 89 | extern const char *sockunion2str (const union sockunion *, char *, size_t); |
| 90 | extern int sockunion_cmp (const union sockunion *, const union sockunion *); |
| 91 | extern int sockunion_same (const union sockunion *, const union sockunion *); |
Timo Teräs | 9196caf | 2015-04-29 09:43:05 +0300 | [diff] [blame] | 92 | extern unsigned int sockunion_hash (const union sockunion *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 93 | |
Timo Teräs | 483abc0 | 2015-05-22 13:40:59 +0300 | [diff] [blame] | 94 | extern size_t family2addrsize(int family); |
| 95 | extern size_t sockunion_get_addrlen(const union sockunion *); |
| 96 | extern const u_char *sockunion_get_addr(const union sockunion *); |
Paul Jakma | f408fa2 | 2016-04-25 15:50:31 +0100 | [diff] [blame] | 97 | extern unsigned short sockunion_get_port (const union sockunion *); |
Timo Teräs | 483abc0 | 2015-05-22 13:40:59 +0300 | [diff] [blame] | 98 | extern void sockunion_set(union sockunion *, int family, const u_char *addr, size_t bytes); |
| 99 | |
Paul Jakma | 2fb2a45 | 2012-06-14 10:37:40 +0100 | [diff] [blame] | 100 | extern union sockunion *sockunion_str2su (const char *str); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 101 | extern int sockunion_accept (int sock, union sockunion *); |
| 102 | extern int sockunion_stream_socket (union sockunion *); |
| 103 | extern int sockopt_reuseaddr (int); |
| 104 | extern int sockopt_reuseport (int); |
David Lamparter | ca05126 | 2009-10-04 16:21:49 +0200 | [diff] [blame] | 105 | extern int sockopt_v6only (int family, int sock); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 106 | extern int sockunion_bind (int sock, union sockunion *, |
| 107 | unsigned short, union sockunion *); |
| 108 | extern int sockopt_ttl (int family, int sock, int ttl); |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 109 | extern int sockopt_minttl (int family, int sock, int minttl); |
Stephen Hemminger | 58192df | 2010-08-05 10:26:24 -0700 | [diff] [blame] | 110 | extern int sockopt_cork (int sock, int onoff); |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 111 | extern int sockunion_socket (const union sockunion *su); |
| 112 | extern const char *inet_sutop (const union sockunion *su, char *str); |
| 113 | extern enum connect_result sockunion_connect (int fd, const union sockunion *su, |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 114 | unsigned short port, |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 115 | ifindex_t); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 116 | extern union sockunion *sockunion_getsockname (int); |
| 117 | extern union sockunion *sockunion_getpeername (int); |
Timo Teräs | 81b139b | 2015-04-29 09:43:01 +0300 | [diff] [blame] | 118 | extern union sockunion *sockunion_dup (const union sockunion *); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 119 | extern void sockunion_free (union sockunion *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 120 | |
| 121 | #ifndef HAVE_INET_NTOP |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 122 | extern const char * inet_ntop (int family, const void *addrptr, |
| 123 | char *strptr, size_t len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | #endif /* HAVE_INET_NTOP */ |
| 125 | |
| 126 | #ifndef HAVE_INET_PTON |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 127 | extern int inet_pton (int family, const char *strptr, void *addrptr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | #endif /* HAVE_INET_PTON */ |
| 129 | |
| 130 | #ifndef HAVE_INET_ATON |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 131 | extern int inet_aton (const char *cp, struct in_addr *inaddr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
| 134 | #endif /* _ZEBRA_SOCKUNION_H */ |