2 * Copyright (c) 2005-2008 Zmanda Inc. All Rights Reserved.
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.
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
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
17 * Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
18 * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 * Author: Dustin J. Mitchell <dustin@zmanda.com>
23 * Utility routines for handling sockaddrs
26 #ifndef SOCKADDR_UTIL_H
27 #define SOCKADDR_UTIL_H
31 /* Dump a sockaddr_union using dbprintf
33 * @param sa: the sockaddr to dump
35 void dump_sockaddr(sockaddr_union * sa);
37 /* Convert a sockaddr_union to a string.
39 * NOTE: this function is not threadsafe!
41 * @param sa: the sockaddr_union to dump
42 * @returns: pointer to statically allocated string
44 char * str_sockaddr(sockaddr_union *sa);
46 /* Compare two sockaddr_union objects, optionally comparing
47 * only the address (and thus ignoring port, flow info, etc.).
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
54 int cmp_sockaddr(sockaddr_union *su1,
58 /* Copy a sockaddr object.
60 * @param dest: destination
63 #define copy_sockaddr(dest, src) memcpy((dest), (src), SS_LEN((src)))
65 /* The "best" address family we support.
69 #define AF_NATIVE AF_INET6
71 #define AF_NATIVE AF_INET
74 /* Get the family for a sockaddr_union.
76 * @param su: the sockaddr_union to examine
78 #define SU_GET_FAMILY(su) ((su)->sa.sa_family)
79 /* Calculate the length of the data in a sockaddr_union.
81 * @param su: the sockaddr_union to examine
82 * @returns: length of the data in the object
86 # define SS_LEN(su) (SU_GET_FAMILY(su)==AF_INET6? sizeof(struct sockaddr_in6):sizeof(struct sockaddr_in))
88 # define SS_LEN(su) (sizeof(struct sockaddr_in))
91 /* Initialize a sockaddr_union to all zeroes (as directed by RFC),
92 * and set its address family as specified
94 * @param su: sockaddr_union object to initialize
95 * @param family: an AF_* constant
97 /* SU_INIT(su, family) */
98 #define SU_INIT(su, family) do { \
99 memset((su), 0, sizeof(*(su))); \
100 (su)->sa.sa_family = (family); \
103 /* set a sockaddr_union to the family-appropriate equivalent of
104 * INADDR_ANY -- a wildcard address and port. Call SU_INIT(su)
105 * first to initialize the object and set the family.
107 * @param su: the sockaddr_union to set
109 /* SU_SET_INADDR_ANY(su) */
111 #define SU_SET_INADDR_ANY(su) do { \
112 switch (SU_GET_FAMILY(su)) { \
114 (su)->sin6.sin6_flowinfo = 0; \
115 (su)->sin6.sin6_addr = in6addr_any; \
118 (su)->sin.sin_addr.s_addr = INADDR_ANY; \
123 #define SU_SET_INADDR_ANY(su) do { \
124 (su)->sin.sin_addr.s_addr = INADDR_ANY; \
128 /* Set the port in a sockaddr_union that already has an family
130 * @param su: the sockaddr_union to manipulate
131 * @param port: the port to insert (in host byte order)
133 /* SU_SET_PORT(su, port) */
135 #define SU_SET_PORT(su, port) \
136 switch (SU_GET_FAMILY(su)) { \
138 (su)->sin.sin_port = (in_port_t)htons((port)); \
141 (su)->sin6.sin6_port = (in_port_t)htons((port)); \
143 default: assert(0); \
146 #define SU_SET_PORT(su, port) \
147 (su)->sin.sin_port = (in_port_t)htons((port));
150 /* Get the port in a sockaddr_union object
152 * @param su: the sockaddr_union to manipulate
153 * @return: the port, in host byte horder
155 /* SU_GET_PORT(su) */
157 #define SU_GET_PORT(su) (ntohs(SU_GET_FAMILY(su) == AF_INET6? (su)->sin6.sin6_port:(su)->sin.sin_port))
159 #define SU_GET_PORT(su) (ntohs((su)->sin.sin_port))
162 #endif /* SOCKADDR_UTIL_H */