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 | |
| 26 | #if 0 |
| 27 | union sockunion { |
| 28 | struct sockinet { |
| 29 | u_char si_len; |
| 30 | u_char si_family; |
| 31 | u_short si_port; |
| 32 | } su_si; |
| 33 | struct sockaddr_in su_sin; |
| 34 | struct sockaddr_in6 su_sin6; |
| 35 | }; |
| 36 | #define su_len su_si.si_len |
| 37 | #define su_family su_si.si_family |
| 38 | #define su_port su_si.si_port |
| 39 | #endif /* 0 */ |
| 40 | |
| 41 | union sockunion |
| 42 | { |
| 43 | struct sockaddr sa; |
| 44 | struct sockaddr_in sin; |
| 45 | #ifdef HAVE_IPV6 |
| 46 | struct sockaddr_in6 sin6; |
| 47 | #endif /* HAVE_IPV6 */ |
| 48 | }; |
| 49 | |
| 50 | enum connect_result |
| 51 | { |
| 52 | connect_error, |
| 53 | connect_success, |
| 54 | connect_in_progress |
| 55 | }; |
| 56 | |
| 57 | /* Default address family. */ |
| 58 | #ifdef HAVE_IPV6 |
| 59 | #define AF_INET_UNION AF_INET6 |
| 60 | #else |
| 61 | #define AF_INET_UNION AF_INET |
| 62 | #endif |
| 63 | |
| 64 | /* Sockunion address string length. Same as INET6_ADDRSTRLEN. */ |
| 65 | #define SU_ADDRSTRLEN 46 |
| 66 | |
| 67 | /* Macro to set link local index to the IPv6 address. For KAME IPv6 |
| 68 | stack. */ |
| 69 | #ifdef KAME |
| 70 | #define IN6_LINKLOCAL_IFINDEX(a) ((a).s6_addr[2] << 8 | (a).s6_addr[3]) |
| 71 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \ |
| 72 | do { \ |
| 73 | (a).s6_addr[2] = ((i) >> 8) & 0xff; \ |
| 74 | (a).s6_addr[3] = (i) & 0xff; \ |
| 75 | } while (0) |
| 76 | #else |
| 77 | #define IN6_LINKLOCAL_IFINDEX(a) |
| 78 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) |
| 79 | #endif /* KAME */ |
| 80 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 81 | #define sockunion_family(X) (X)->sa.sa_family |
| 82 | |
Jorge Boncompte [DTI2] | 0c5ed3e | 2012-04-10 16:57:22 +0200 | [diff] [blame] | 83 | #define sockunion2ip(X) (X)->sin.sin_addr.s_addr |
| 84 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | /* Prototypes. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 86 | extern int str2sockunion (const char *, union sockunion *); |
| 87 | extern const char *sockunion2str (union sockunion *, char *, size_t); |
| 88 | extern int sockunion_cmp (union sockunion *, union sockunion *); |
| 89 | extern int sockunion_same (union sockunion *, union sockunion *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 90 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 91 | extern int sockunion_accept (int sock, union sockunion *); |
| 92 | extern int sockunion_stream_socket (union sockunion *); |
| 93 | extern int sockopt_reuseaddr (int); |
| 94 | extern int sockopt_reuseport (int); |
David Lamparter | ca05126 | 2009-10-04 16:21:49 +0200 | [diff] [blame] | 95 | extern int sockopt_v6only (int family, int sock); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 96 | extern int sockunion_bind (int sock, union sockunion *, |
| 97 | unsigned short, union sockunion *); |
| 98 | extern int sockopt_ttl (int family, int sock, int ttl); |
Nick Hilliard | fa411a2 | 2011-03-23 15:33:17 +0000 | [diff] [blame] | 99 | extern int sockopt_minttl (int family, int sock, int minttl); |
Stephen Hemminger | 58192df | 2010-08-05 10:26:24 -0700 | [diff] [blame] | 100 | extern int sockopt_cork (int sock, int onoff); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 101 | extern int sockunion_socket (union sockunion *su); |
| 102 | extern const char *inet_sutop (union sockunion *su, char *str); |
| 103 | extern enum connect_result sockunion_connect (int fd, union sockunion *su, |
| 104 | unsigned short port, |
| 105 | unsigned int); |
| 106 | extern union sockunion *sockunion_getsockname (int); |
| 107 | extern union sockunion *sockunion_getpeername (int); |
| 108 | extern union sockunion *sockunion_dup (union sockunion *); |
| 109 | extern void sockunion_free (union sockunion *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 110 | |
| 111 | #ifndef HAVE_INET_NTOP |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 112 | extern const char * inet_ntop (int family, const void *addrptr, |
| 113 | char *strptr, size_t len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | #endif /* HAVE_INET_NTOP */ |
| 115 | |
| 116 | #ifndef HAVE_INET_PTON |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 117 | extern int inet_pton (int family, const char *strptr, void *addrptr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 118 | #endif /* HAVE_INET_PTON */ |
| 119 | |
| 120 | #ifndef HAVE_INET_ATON |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 121 | extern int inet_aton (const char *cp, struct in_addr *inaddr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
| 124 | #endif /* _ZEBRA_SOCKUNION_H */ |