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 * Splits a string into its host and port components
238 * \param str[in] The string to parse. May be modified by writing a NUL at the end of
240 * \param host[out] Pointer to the host component within \a str.
241 * \param port[out] Pointer to the port component within \a str.
242 * \param flags If set to zero, a port MAY be present. If set to PARSE_PORT_IGNORE, a
243 * port MAY be present but will be ignored. If set to PARSE_PORT_REQUIRE,
244 * a port MUST be present. If set to PARSE_PORT_FORBID, a port MUST NOT
250 int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags);
256 * Parse an IPv4 or IPv6 address string.
259 * Parses a string containing an IPv4 or IPv6 address followed by an optional
260 * port (separated by a colon) into a struct ast_sockaddr. The allowed formats
269 * Host names are NOT allowed.
271 * \param[out] addr The resulting ast_sockaddr
272 * \param str The string to parse
273 * \param flags If set to zero, a port MAY be present. If set to
274 * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
275 * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
276 * port MUST NOT be present.
281 int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags);
287 * Parses a string with an IPv4 or IPv6 address and place results into an array
290 * Parses a string containing a host name or an IPv4 or IPv6 address followed
291 * by an optional port (separated by a colon). The result is returned into a
292 * array of struct ast_sockaddr. Allowed formats for str are the following:
295 * host.example.com:port
302 * \param[out] addrs The resulting array of ast_sockaddrs
303 * \param str The string to parse
304 * \param flags If set to zero, a port MAY be present. If set to
305 * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
306 * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
307 * port MUST NOT be present.
309 * \param family Only addresses of the given family will be returned. Use 0 or
310 * AST_SOCKADDR_UNSPEC to get addresses of all families.
313 * \retval non-zero The number of elements in addrs array.
315 int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
316 int flags, int family);
322 * Get the port number of a socket address.
324 * \warning Do not use this function unless you really know what you are doing.
325 * And "I want the port number" is not knowing what you are doing.
327 * \retval 0 Address is null
328 * \retval non-zero The port number of the ast_sockaddr
330 #define ast_sockaddr_port(addr) _ast_sockaddr_port(addr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
331 uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func);
337 * Sets the port number of a socket address.
339 * \warning Do not use this function unless you really know what you are doing.
340 * And "I want the port number" is not knowing what you are doing.
342 * \param addr Address on which to set the port
343 * \param port The port you wish to set the address to use
346 #define ast_sockaddr_set_port(addr,port) _ast_sockaddr_set_port(addr,port,__FILE__,__LINE__,__PRETTY_FUNCTION__)
347 void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func);
353 * Get an IPv4 address of an ast_sockaddr
355 * \warning You should rarely need this function. Only use if you know what
357 * \return IPv4 address in network byte order
359 uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr);
365 * Determine if the address is an IPv4 address
367 * \warning You should rarely need this function. Only use if you know what
369 * \retval 1 This is an IPv4 address
370 * \retval 0 This is an IPv6 or IPv4-mapped IPv6 address
372 int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr);
378 * Determine if this is an IPv4-mapped IPv6 address
380 * \warning You should rarely need this function. Only use if you know what
383 * \retval 1 This is an IPv4-mapped IPv6 address.
384 * \retval 0 This is not an IPv4-mapped IPv6 address.
386 int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr);
392 * Determine if an IPv4 address is a multicast address
394 * \parm addr the address to check
396 * This function checks if an address is in the 224.0.0.0/4 network block.
398 * \return non-zero if this is a multicast address
400 int ast_sockaddr_is_ipv4_multicast(const struct ast_sockaddr *addr);
406 * Determine if this is an IPv6 address
408 * \warning You should rarely need this function. Only use if you know what
411 * \retval 1 This is an IPv6 or IPv4-mapped IPv6 address.
412 * \retval 0 This is an IPv4 address.
414 int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr);
420 * Determine if the address type is unspecified, or "any" address.
423 * For IPv4, this would be the address 0.0.0.0, and for IPv6,
424 * this would be the address ::. The port number is ignored.
426 * \retval 1 This is an "any" address
427 * \retval 0 This is not an "any" address
429 int ast_sockaddr_is_any(const struct ast_sockaddr *addr);
435 * Computes a hash value from the address. The port is ignored.
437 * \retval 0 Unknown address family
438 * \retval other A 32-bit hash derived from the address
440 int ast_sockaddr_hash(const struct ast_sockaddr *addr);
446 * Wrapper around accept(2) that uses struct ast_sockaddr.
449 * For parameter and return information, see the man page for
452 int ast_accept(int sockfd, struct ast_sockaddr *addr);
458 * Wrapper around bind(2) that uses struct ast_sockaddr.
461 * For parameter and return information, see the man page for
464 int ast_bind(int sockfd, const struct ast_sockaddr *addr);
470 * Wrapper around connect(2) that uses struct ast_sockaddr.
473 * For parameter and return information, see the man page for
476 int ast_connect(int sockfd, const struct ast_sockaddr *addr);
482 * Wrapper around getsockname(2) that uses struct ast_sockaddr.
485 * For parameter and return information, see the man page for
488 int ast_getsockname(int sockfd, struct ast_sockaddr *addr);
494 * Wrapper around recvfrom(2) that uses struct ast_sockaddr.
497 * For parameter and return information, see the man page for
500 ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags,
501 struct ast_sockaddr *src_addr);
507 * Wrapper around sendto(2) that uses ast_sockaddr.
511 * return information, see the man page for sendto(2)
513 ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags,
514 const struct ast_sockaddr *dest_addr);
520 * Set type of service
523 * Set ToS ("Type of Service for IPv4 and "Traffic Class for IPv6) and
524 * CoS (Linux's SO_PRIORITY)
526 * \param sockfd File descriptor for socket on which to set the parameters
527 * \param tos The type of service for the socket
528 * \param cos The cost of service for the socket
529 * \param desc A text description of the socket in question.
531 * \retval -1 Error, with errno set to an appropriate value
533 int ast_set_qos(int sockfd, int tos, int cos, const char *desc);
536 * These are backward compatibility functions that may be used by subsystems
537 * that have not yet been converted to IPv6. They will be removed when all
538 * subsystems are IPv6-ready.
546 * Converts a struct ast_sockaddr to a struct sockaddr_in.
548 * \param addr The ast_sockaddr to convert
549 * \param[out] sin The resulting sockaddr_in struct
550 * \retval nonzero Success
551 * \retval zero Failure
553 #define ast_sockaddr_to_sin(addr,sin) _ast_sockaddr_to_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
554 int _ast_sockaddr_to_sin(const struct ast_sockaddr *addr,
555 struct sockaddr_in *sin, const char *file, int line, const char *func);
561 * Converts a struct sockaddr_in to a struct ast_sockaddr.
563 * \param sin The sockaddr_in to convert
564 * \return an ast_sockaddr structure
566 #define ast_sockaddr_from_sin(addr,sin) _ast_sockaddr_from_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
567 void _ast_sockaddr_from_sin(struct ast_sockaddr *addr, const struct sockaddr_in *sin,
568 const char *file, int line, const char *func);
572 #if defined(__cplusplus) || defined(c_plusplus)
576 #endif /* _ASTERISK_NETSOCK2_H */