2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2010, Digium, Inc.
6 * Viagénie <asteriskv6@viagenie.ca>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 * \brief Network socket handling
23 #ifndef _ASTERISK_NETSOCK2_H
24 #define _ASTERISK_NETSOCK2_H
26 #if defined(__cplusplus) || defined(c_plusplus)
30 #include <sys/socket.h>
32 #include <netinet/in.h>
35 * Values for address families that we support. This is reproduced from socket.h
36 * because we do not want users to include that file. Only netsock2.c should
37 * ever include socket.h.
46 * Socket address structure. The first member is big enough to contain addresses
47 * of any family. The second member contains the length (in bytes) used in the
50 * Some BSDs have the length embedded in sockaddr structs. We ignore them.
51 * (This is the right thing to do.)
54 struct sockaddr_storage ss;
60 * Convert an IPv4-mapped IPv6 address into an IPv4 address.
62 * \warning You should rarely need this function. Only call this
63 * if you know what you're doing.
65 * \param addr The IPv4-mapped address to convert
66 * \param mapped_addr The resulting IPv4 address
67 * \retval 0 Unable to make the conversion
68 * \retval 1 Successful conversion
70 int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped);
76 * Checks if the ast_sockaddr is null. "null" in this sense essentially
77 * means uninitialized, or having a 0 length.
79 * \param addr Pointer to the ast_sockaddr we wish to check
80 * \retval 1 \a addr is null
81 * \retval 0 \a addr is non-null.
83 static inline int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
85 return !addr || addr->len == 0;
92 * Sets address \a addr to null.
96 static inline void ast_sockaddr_setnull(struct ast_sockaddr *addr)
105 * Copies the data from one ast_sockaddr to another
107 * \param dst The destination ast_sockaddr
108 * \param src The source ast_sockaddr
111 static inline void ast_sockaddr_copy(struct ast_sockaddr *dst,
112 const struct ast_sockaddr *src)
114 memcpy(dst, src, src->len);
122 * Compares two ast_sockaddr structures
124 * \retval -1 \a a is lexicographically smaller than \a b
125 * \retval 0 \a a is equal to \a b
126 * \retval 1 \a b is lexicographically smaller than \a a
128 int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b);
134 * Compares the addresses of two ast_sockaddr structures.
136 * \retval -1 \a a is lexicographically smaller than \a b
137 * \retval 0 \a a is equal to \a b
138 * \retval 1 \a b is lexicographically smaller than \a a
140 int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b);
142 #define AST_SOCKADDR_STR_ADDR (1 << 0)
143 #define AST_SOCKADDR_STR_PORT (1 << 1)
144 #define AST_SOCKADDR_STR_BRACKETS (1 << 2)
145 #define AST_SOCKADDR_STR_HOST AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_BRACKETS
146 #define AST_SOCKADDR_STR_DEFAULT AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT
152 * Convert a socket address to a string.
155 * This will be of the form a.b.c.d:xyz
156 * for IPv4 and [a:b:c:...:d]:xyz for IPv6.
158 * This function is thread-safe. The returned string is on static
159 * thread-specific storage.
161 * \param addr The input to be stringified
162 * \param format one of the following:
163 * AST_SOCKADDR_STR_DEFAULT:
164 * a.b.c.d:xyz for IPv4
165 * [a:b:c:...:d]:xyz for IPv6.
166 * AST_SOCKADDR_STR_ADDR: address only
168 * a:b:c:...:d for IPv6.
169 * AST_SOCKADDR_STR_HOST: address only, suitable for a URL
171 * [a:b:c:...:d] for IPv6.
172 * AST_SOCKADDR_STR_PORT: port only
173 * \retval "(null)" \a addr is null
174 * \retval "" An error occurred during processing
175 * \retval string The stringified form of the address
177 char *ast_sockaddr_stringify_fmt(const struct ast_sockaddr *addr, int format);
183 * Wrapper around ast_sockaddr_stringify_fmt() with default format
185 * \return same as ast_sockaddr_stringify_fmt()
187 static inline char *ast_sockaddr_stringify(const struct ast_sockaddr *addr)
189 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_DEFAULT);
196 * Wrapper around ast_sockaddr_stringify_fmt() to return an address only
198 * \return same as ast_sockaddr_stringify_fmt()
200 static inline char *ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
202 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_ADDR);
209 * Wrapper around ast_sockaddr_stringify_fmt() to return an address only,
210 * suitable for a URL (with brackets for IPv6).
212 * \return same as ast_sockaddr_stringify_fmt()
214 static inline char *ast_sockaddr_stringify_host(const struct ast_sockaddr *addr)
216 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_HOST);
223 * Wrapper around ast_sockaddr_stringify_fmt() to return a port only
225 * \return same as ast_sockaddr_stringify_fmt()
227 static inline char *ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
229 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_PORT);
236 * Parse an IPv4 or IPv6 address string.
239 * Parses a string containing an IPv4 or IPv6 address followed by an optional
240 * port (separated by a colon) into a struct ast_sockaddr. The allowed formats
249 * Host names are NOT allowed.
251 * \param[out] addr The resulting ast_sockaddr
252 * \param str The string to parse
253 * \param flags If set to zero, a port MAY be present. If set to
254 * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
255 * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
256 * port MUST NOT be present.
261 int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags);
267 * Parses a string with an IPv4 or IPv6 address and place results into an array
270 * Parses a string containing a host name or an IPv4 or IPv6 address followed
271 * by an optional port (separated by a colon). The result is returned into a
272 * array of struct ast_sockaddr. Allowed formats for str are the following:
275 * host.example.com:port
282 * \param[out] addrs The resulting array of ast_sockaddrs
283 * \param str The string to parse
284 * \param flags If set to zero, a port MAY be present. If set to
285 * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
286 * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
287 * port MUST NOT be present.
289 * \param family Only addresses of the given family will be returned. Use 0 or
290 * AST_SOCKADDR_UNSPEC to get addresses of all families.
293 * \retval non-zero The number of elements in addrs array.
295 int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
296 int flags, int family);
302 * Get the port number of a socket address.
304 * \warning Do not use this function unless you really know what you are doing.
305 * And "I want the port number" is not knowing what you are doing.
307 * \retval 0 Address is null
308 * \retval non-zero The port number of the ast_sockaddr
310 #define ast_sockaddr_port(addr) _ast_sockaddr_port(addr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
311 uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func);
317 * Sets the port number of a socket address.
319 * \warning Do not use this function unless you really know what you are doing.
320 * And "I want the port number" is not knowing what you are doing.
322 * \param addr Address on which to set the port
323 * \param port The port you wish to set the address to use
326 #define ast_sockaddr_set_port(addr,port) _ast_sockaddr_set_port(addr,port,__FILE__,__LINE__,__PRETTY_FUNCTION__)
327 void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func);
333 * Get an IPv4 address of an ast_sockaddr
335 * \warning You should rarely need this function. Only use if you know what
337 * \return IPv4 address in network byte order
339 uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr);
345 * Determine if the address is an IPv4 address
347 * \warning You should rarely need this function. Only use if you know what
349 * \retval 1 This is an IPv4 address
350 * \retval 0 This is an IPv6 or IPv4-mapped IPv6 address
352 int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr);
358 * Determine if this is an IPv4-mapped IPv6 address
360 * \warning You should rarely need this function. Only use if you know what
363 * \retval 1 This is an IPv4-mapped IPv6 address.
364 * \retval 0 This is not an IPv4-mapped IPv6 address.
366 int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr);
372 * Determine if this is an IPv6 address
374 * \warning You should rarely need this function. Only use if you know what
377 * \retval 1 This is an IPv6 or IPv4-mapped IPv6 address.
378 * \retval 0 This is an IPv4 address.
380 int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr);
386 * Determine if the address type is unspecified, or "any" address.
389 * For IPv4, this would be the address 0.0.0.0, and for IPv6,
390 * this would be the address ::. The port number is ignored.
392 * \retval 1 This is an "any" address
393 * \retval 0 This is not an "any" address
395 int ast_sockaddr_is_any(const struct ast_sockaddr *addr);
401 * Computes a hash value from the address. The port is ignored.
403 * \retval 0 Unknown address family
404 * \retval other A 32-bit hash derived from the address
406 int ast_sockaddr_hash(const struct ast_sockaddr *addr);
412 * Wrapper around accept(2) that uses struct ast_sockaddr.
415 * For parameter and return information, see the man page for
418 int ast_accept(int sockfd, struct ast_sockaddr *addr);
424 * Wrapper around bind(2) that uses struct ast_sockaddr.
427 * For parameter and return information, see the man page for
430 int ast_bind(int sockfd, const struct ast_sockaddr *addr);
436 * Wrapper around connect(2) that uses struct ast_sockaddr.
439 * For parameter and return information, see the man page for
442 int ast_connect(int sockfd, const struct ast_sockaddr *addr);
448 * Wrapper around getsockname(2) that uses struct ast_sockaddr.
451 * For parameter and return information, see the man page for
454 int ast_getsockname(int sockfd, struct ast_sockaddr *addr);
460 * Wrapper around recvfrom(2) that uses struct ast_sockaddr.
463 * For parameter and return information, see the man page for
466 ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags,
467 struct ast_sockaddr *src_addr);
473 * Wrapper around sendto(2) that uses ast_sockaddr.
477 * return information, see the man page for sendto(2)
479 ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags,
480 const struct ast_sockaddr *dest_addr);
486 * Set type of service
489 * Set ToS ("Type of Service for IPv4 and "Traffic Class for IPv6) and
490 * CoS (Linux's SO_PRIORITY)
492 * \param sockfd File descriptor for socket on which to set the parameters
493 * \param tos The type of service for the socket
494 * \param cos The cost of service for the socket
495 * \param desc A text description of the socket in question.
497 * \retval -1 Error, with errno set to an appropriate value
499 int ast_set_qos(int sockfd, int tos, int cos, const char *desc);
502 * These are backward compatibility functions that may be used by subsystems
503 * that have not yet been converted to IPv6. They will be removed when all
504 * subsystems are IPv6-ready.
512 * Converts a struct ast_sockaddr to a struct sockaddr_in.
514 * \param addr The ast_sockaddr to convert
515 * \param[out] sin The resulting sockaddr_in struct
516 * \retval nonzero Success
517 * \retval zero Failure
519 #define ast_sockaddr_to_sin(addr,sin) _ast_sockaddr_to_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
520 int _ast_sockaddr_to_sin(const struct ast_sockaddr *addr,
521 struct sockaddr_in *sin, const char *file, int line, const char *func);
527 * Converts a struct sockaddr_in to a struct ast_sockaddr.
529 * \param sin The sockaddr_in to convert
530 * \return an ast_sockaddr structure
532 #define ast_sockaddr_from_sin(addr,sin) _ast_sockaddr_from_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
533 void _ast_sockaddr_from_sin(struct ast_sockaddr *addr, const struct sockaddr_in *sin,
534 const char *file, int line, const char *func);
538 #if defined(__cplusplus) || defined(c_plusplus)
542 #endif /* _ASTERISK_NETSOCK2_H */