Imported Upstream version 3.2.0
[debian/amanda] / common-src / sockaddr-util.h
1 /*
2  * Copyright (c) 2007, 2008, 2010 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19  *
20  * Author: Dustin J. Mitchell <dustin@zmanda.com>
21  */
22 /*
23  * Utility routines for handling sockaddrs
24  */
25
26 #ifndef SOCKADDR_UTIL_H
27 #define SOCKADDR_UTIL_H
28
29 #include "amanda.h"
30
31 /* Dump a sockaddr_union using dbprintf
32  *
33  * @param sa: the sockaddr to dump
34  */
35 void    dump_sockaddr(sockaddr_union *  sa);
36
37 /* Convert a sockaddr_union to a string.
38  *
39  * NOTE: this function is not threadsafe!
40  *
41  * @param sa: the sockaddr_union to dump
42  * @returns: pointer to statically allocated string
43  */
44 char *  str_sockaddr(sockaddr_union *sa);
45
46 /* Compare two sockaddr_union objects, optionally comparing
47  * only the address (and thus ignoring port, flow info, etc.).
48  *
49  * @param su1: one sockaddr_union to compare
50  * @param su2: the other sockaddr_union to compare
51  * @param addr_only: if true, ignore port, flow info, etc.
52  * @returns: -1, 0, or 1 for <, ==, >, respectively
53  */
54 int     cmp_sockaddr(sockaddr_union *su1,
55                      sockaddr_union *su2,
56                      int addr_only);
57
58 /* Parse a string into a sockaddr.  This will try all available address
59  * families.
60  *
61  * @param src: the string representation of the address
62  * @param dst: the sockaddr_union in which to store the result
63  * @returns: 1 on success, -1 on error, or 0 if unparseable
64  */
65 int     str_to_sockaddr(
66         const char *src,
67         sockaddr_union *dst);
68
69 /* Copy a sockaddr object.
70  *
71  * @param dest: destination
72  * @param src: source
73  */
74 #define copy_sockaddr(dest, src) memcpy((dest), (src), SS_LEN((src)))
75
76 /* The "best" address family we support.
77  */
78 /* AF_NATIVE */
79 #ifdef WORKING_IPV6
80 #define AF_NATIVE AF_INET6
81 #else
82 #define AF_NATIVE AF_INET
83 #endif
84
85 /* Get the family for a sockaddr_union.
86  *
87  * @param su: the sockaddr_union to examine
88  */
89 #define SU_GET_FAMILY(su) ((su)->sa.sa_family)
90 /* Calculate the length of the data in a sockaddr_union.
91  *
92  * @param su: the sockaddr_union to examine
93  * @returns: length of the data in the object
94  */
95 /* SS_LEN(su) */
96 #ifdef WORKING_IPV6
97 # define SS_LEN(su) (SU_GET_FAMILY(su)==AF_INET6? sizeof(struct sockaddr_in6):sizeof(struct sockaddr_in))
98 #else
99 # define SS_LEN(su) (sizeof(struct sockaddr_in))
100 #endif
101
102 /* Initialize a sockaddr_union to all zeroes (as directed by RFC),
103  * and set its address family as specified
104  *
105  * @param su: sockaddr_union object to initialize
106  * @param family: an AF_* constant
107  */
108 /* SU_INIT(su, family) */
109 #define SU_INIT(su, family) do { \
110     memset((su), 0, sizeof(*(su))); \
111     (su)->sa.sa_family = (family); \
112 } while (0);
113
114 /* set a sockaddr_union to the family-appropriate equivalent of
115  * INADDR_ANY -- a wildcard address and port.  Call SU_INIT(su)
116  * first to initialize the object and set the family.
117  *
118  * @param su: the sockaddr_union to set
119  */
120 /* SU_SET_INADDR_ANY(su) */
121 #ifdef WORKING_IPV6
122 #define SU_SET_INADDR_ANY(su) do { \
123     switch (SU_GET_FAMILY(su)) { \
124         case AF_INET6: \
125             (su)->sin6.sin6_flowinfo = 0; \
126             (su)->sin6.sin6_addr = in6addr_any; \
127             break; \
128         case AF_INET: \
129             (su)->sin.sin_addr.s_addr = INADDR_ANY; \
130             break; \
131     } \
132 } while (0);
133 #else
134 #define SU_SET_INADDR_ANY(su) do { \
135     (su)->sin.sin_addr.s_addr = INADDR_ANY; \
136 } while (0);
137 #endif
138
139 /* Set the port in a sockaddr_union that already has an family
140  *
141  * @param su: the sockaddr_union to manipulate
142  * @param port: the port to insert (in host byte order)
143  */
144 /* SU_SET_PORT(su, port) */
145 #ifdef WORKING_IPV6
146 #define SU_SET_PORT(su, port) \
147 switch (SU_GET_FAMILY(su)) { \
148     case AF_INET: \
149         (su)->sin.sin_port = (in_port_t)htons((port)); \
150         break; \
151     case AF_INET6: \
152         (su)->sin6.sin6_port = (in_port_t)htons((port)); \
153         break; \
154     default: assert(0); \
155 }
156 #else
157 #define SU_SET_PORT(su, port) \
158         (su)->sin.sin_port = (in_port_t)htons((port));
159 #endif
160
161 /* Get the port in a sockaddr_union object
162  *
163  * @param su: the sockaddr_union to manipulate
164  * @return: the port, in host byte horder
165  */
166 /* SU_GET_PORT(su) */
167 #ifdef WORKING_IPV6
168 #define SU_GET_PORT(su) (ntohs(SU_GET_FAMILY(su) == AF_INET6? (su)->sin6.sin6_port:(su)->sin.sin_port))
169 #else
170 #define SU_GET_PORT(su) (ntohs((su)->sin.sin_port))
171 #endif
172
173 #endif  /* SOCKADDR_UTIL_H */