]> git.proxmox.com Git - mirror_frr.git/blob - lib/sockunion.h
bgpd: Zebra lib for Graceful Restart.
[mirror_frr.git] / lib / sockunion.h
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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifndef _ZEBRA_SOCKUNION_H
23 #define _ZEBRA_SOCKUNION_H
24
25 #include "privs.h"
26 #include "if.h"
27 #ifdef __OpenBSD__
28 #include <netmpls/mpls.h>
29 #endif
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 union sockunion {
36 struct sockaddr sa;
37 struct sockaddr_in sin;
38 struct sockaddr_in6 sin6;
39 #ifdef __OpenBSD__
40 struct sockaddr_mpls smpls;
41 struct sockaddr_rtlabel rtlabel;
42 #endif
43 };
44
45 enum connect_result { connect_error, connect_success, connect_in_progress };
46
47 /* Default address family. */
48 #define AF_INET_UNION AF_INET6
49
50 /* Sockunion address string length. Same as INET6_ADDRSTRLEN. */
51 #define SU_ADDRSTRLEN 46
52
53 /* Macro to set link local index to the IPv6 address. For KAME IPv6
54 stack. */
55 #ifdef KAME
56 #define IN6_LINKLOCAL_IFINDEX(a) ((a).s6_addr[2] << 8 | (a).s6_addr[3])
57 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
58 do { \
59 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
60 (a).s6_addr[3] = (i)&0xff; \
61 } while (0)
62 #else
63 #define IN6_LINKLOCAL_IFINDEX(a)
64 #define SET_IN6_LINKLOCAL_IFINDEX(a, i)
65 #endif /* KAME */
66
67 #define sockunion_family(X) (X)->sa.sa_family
68
69 #define sockunion2ip(X) (X)->sin.sin_addr.s_addr
70
71 /* Prototypes. */
72 extern int str2sockunion(const char *, union sockunion *);
73 extern const char *sockunion2str(const union sockunion *, char *, size_t);
74 extern int sockunion_cmp(const union sockunion *, const union sockunion *);
75 extern int sockunion_same(const union sockunion *, const union sockunion *);
76 extern unsigned int sockunion_hash(const union sockunion *);
77
78 extern size_t family2addrsize(int family);
79 extern size_t sockunion_get_addrlen(const union sockunion *);
80 extern const uint8_t *sockunion_get_addr(const union sockunion *);
81 extern void sockunion_set(union sockunion *, int family, const uint8_t *addr,
82 size_t bytes);
83
84 extern union sockunion *sockunion_str2su(const char *str);
85 extern int sockunion_accept(int sock, union sockunion *);
86 extern int sockunion_sizeof(const union sockunion *su);
87 extern int sockunion_stream_socket(union sockunion *);
88 extern int sockopt_reuseaddr(int);
89 extern int sockopt_reuseport(int);
90 extern int sockopt_v6only(int family, int sock);
91 extern int sockunion_bind(int sock, union sockunion *, unsigned short,
92 union sockunion *);
93 extern int sockopt_ttl(int family, int sock, int ttl);
94 extern int sockopt_minttl(int family, int sock, int minttl);
95 extern int sockopt_cork(int sock, int onoff);
96 extern int sockunion_socket(const union sockunion *su);
97 extern const char *inet_sutop(const union sockunion *su, char *str);
98 extern enum connect_result sockunion_connect(int fd, const union sockunion *su,
99 unsigned short port, ifindex_t);
100 extern union sockunion *sockunion_getsockname(int);
101 extern union sockunion *sockunion_getpeername(int);
102 extern union sockunion *sockunion_dup(const union sockunion *);
103 extern void sockunion_free(union sockunion *);
104 extern void sockunion_init(union sockunion *);
105
106 #ifdef __cplusplus
107 }
108 #endif
109
110 #endif /* _ZEBRA_SOCKUNION_H */