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 * \brief Socket address structure.
49 * The first member is big enough to contain addresses of any
50 * family. The second member contains the length (in bytes) used
51 * in the first member.
54 * Some BSDs have the length embedded in sockaddr structs. We
55 * ignore them. (This is the right thing to do.)
58 * It is important to always initialize ast_sockaddr before use
59 * -- even if they are passed to ast_sockaddr_copy() as the
60 * underlying storage could be bigger than what ends up being
61 * copied -- leaving part of the data unitialized.
64 struct sockaddr_storage ss;
70 * Convert an IPv4-mapped IPv6 address into an IPv4 address.
72 * \warning You should rarely need this function. Only call this
73 * if you know what you're doing.
75 * \param addr The IPv4-mapped address to convert
76 * \param mapped_addr The resulting IPv4 address
77 * \retval 0 Unable to make the conversion
78 * \retval 1 Successful conversion
80 int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped);
86 * Checks if the ast_sockaddr is null. "null" in this sense essentially
87 * means uninitialized, or having a 0 length.
89 * \param addr Pointer to the ast_sockaddr we wish to check
90 * \retval 1 \a addr is null
91 * \retval 0 \a addr is non-null.
93 static inline int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
95 return !addr || addr->len == 0;
102 * Sets address \a addr to null.
106 static inline void ast_sockaddr_setnull(struct ast_sockaddr *addr)
115 * Copies the data from one ast_sockaddr to another
117 * \param dst The destination ast_sockaddr
118 * \param src The source ast_sockaddr
121 static inline void ast_sockaddr_copy(struct ast_sockaddr *dst,
122 const struct ast_sockaddr *src)
124 memcpy(dst, src, src->len);
132 * Compares two ast_sockaddr structures
134 * \retval -1 \a a is lexicographically smaller than \a b
135 * \retval 0 \a a is equal to \a b
136 * \retval 1 \a b is lexicographically smaller than \a a
138 int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b);
144 * Compares the addresses of two ast_sockaddr structures.
146 * \retval -1 \a a is lexicographically smaller than \a b
147 * \retval 0 \a a is equal to \a b
148 * \retval 1 \a b is lexicographically smaller than \a a
150 int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b);
152 #define AST_SOCKADDR_STR_ADDR (1 << 0)
153 #define AST_SOCKADDR_STR_PORT (1 << 1)
154 #define AST_SOCKADDR_STR_BRACKETS (1 << 2)
155 #define AST_SOCKADDR_STR_REMOTE (1 << 3)
156 #define AST_SOCKADDR_STR_HOST (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_BRACKETS)
157 #define AST_SOCKADDR_STR_DEFAULT (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT)
158 #define AST_SOCKADDR_STR_ADDR_REMOTE (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_REMOTE)
159 #define AST_SOCKADDR_STR_HOST_REMOTE (AST_SOCKADDR_STR_HOST | AST_SOCKADDR_STR_REMOTE)
160 #define AST_SOCKADDR_STR_DEFAULT_REMOTE (AST_SOCKADDR_STR_DEFAULT | AST_SOCKADDR_STR_REMOTE)
161 #define AST_SOCKADDR_STR_FORMAT_MASK (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT | AST_SOCKADDR_STR_BRACKETS)
167 * Convert a socket address to a string.
170 * This will be of the form a.b.c.d:xyz
171 * for IPv4 and [a:b:c:...:d]:xyz for IPv6.
173 * This function is thread-safe. The returned string is on static
174 * thread-specific storage.
176 * \param addr The input to be stringified
177 * \param format one of the following:
178 * AST_SOCKADDR_STR_DEFAULT:
179 * a.b.c.d:xyz for IPv4
180 * [a:b:c:...:d]:xyz for IPv6.
181 * AST_SOCKADDR_STR_ADDR: address only
183 * a:b:c:...:d for IPv6.
184 * AST_SOCKADDR_STR_HOST: address only, suitable for a URL
186 * [a:b:c:...:d] for IPv6.
187 * AST_SOCKADDR_STR_PORT: port only
188 * \retval "(null)" \a addr is null
189 * \retval "" An error occurred during processing
190 * \retval string The stringified form of the address
192 char *ast_sockaddr_stringify_fmt(const struct ast_sockaddr *addr, int format);
198 * Wrapper around ast_sockaddr_stringify_fmt() with default format
200 * \return same as ast_sockaddr_stringify_fmt()
202 static inline char *ast_sockaddr_stringify(const struct ast_sockaddr *addr)
204 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_DEFAULT);
211 * Wrapper around ast_sockaddr_stringify_fmt() with default format
213 * \note This address will be suitable for passing to a remote machine via the
214 * application layer. For example, the scope-id on a link-local IPv6 address
217 * \return same as ast_sockaddr_stringify_fmt()
219 static inline char *ast_sockaddr_stringify_remote(const struct ast_sockaddr *addr)
221 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_DEFAULT_REMOTE);
228 * Wrapper around ast_sockaddr_stringify_fmt() to return an address only
230 * \return same as ast_sockaddr_stringify_fmt()
232 static inline char *ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
234 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_ADDR);
241 * Wrapper around ast_sockaddr_stringify_fmt() to return an address only
243 * \note This address will be suitable for passing to a remote machine via the
244 * application layer. For example, the scope-id on a link-local IPv6 address
247 * \return same as ast_sockaddr_stringify_fmt()
249 static inline char *ast_sockaddr_stringify_addr_remote(const struct ast_sockaddr *addr)
251 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_ADDR_REMOTE);
258 * Wrapper around ast_sockaddr_stringify_fmt() to return an address only,
259 * suitable for a URL (with brackets for IPv6).
261 * \return same as ast_sockaddr_stringify_fmt()
263 static inline char *ast_sockaddr_stringify_host(const struct ast_sockaddr *addr)
265 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_HOST);
272 * Wrapper around ast_sockaddr_stringify_fmt() to return an address only,
273 * suitable for a URL (with brackets for IPv6).
275 * \note This address will be suitable for passing to a remote machine via the
276 * application layer. For example, the scope-id on a link-local IPv6 address
279 * \return same as ast_sockaddr_stringify_fmt()
281 static inline char *ast_sockaddr_stringify_host_remote(const struct ast_sockaddr *addr)
283 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_HOST_REMOTE);
290 * Wrapper around ast_sockaddr_stringify_fmt() to return a port only
292 * \return same as ast_sockaddr_stringify_fmt()
294 static inline char *ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
296 return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_PORT);
303 * Splits a string into its host and port components
305 * \param str[in] The string to parse. May be modified by writing a NUL at the end of
307 * \param host[out] Pointer to the host component within \a str.
308 * \param port[out] Pointer to the port component within \a str.
309 * \param flags If set to zero, a port MAY be present. If set to PARSE_PORT_IGNORE, a
310 * port MAY be present but will be ignored. If set to PARSE_PORT_REQUIRE,
311 * a port MUST be present. If set to PARSE_PORT_FORBID, a port MUST NOT
317 int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags);
323 * Parse an IPv4 or IPv6 address string.
326 * Parses a string containing an IPv4 or IPv6 address followed by an optional
327 * port (separated by a colon) into a struct ast_sockaddr. The allowed formats
336 * Host names are NOT allowed.
338 * \param[out] addr The resulting ast_sockaddr
339 * \param str The string to parse
340 * \param flags If set to zero, a port MAY be present. If set to
341 * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
342 * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
343 * port MUST NOT be present.
348 int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags);
354 * Parses a string with an IPv4 or IPv6 address and place results into an array
357 * Parses a string containing a host name or an IPv4 or IPv6 address followed
358 * by an optional port (separated by a colon). The result is returned into a
359 * array of struct ast_sockaddr. Allowed formats for str are the following:
362 * host.example.com:port
369 * \param[out] addrs The resulting array of ast_sockaddrs
370 * \param str The string to parse
371 * \param flags If set to zero, a port MAY be present. If set to
372 * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
373 * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
374 * port MUST NOT be present.
376 * \param family Only addresses of the given family will be returned. Use 0 or
377 * AST_SOCKADDR_UNSPEC to get addresses of all families.
380 * \retval non-zero The number of elements in addrs array.
382 int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
383 int flags, int family);
389 * Get the port number of a socket address.
391 * \warning Do not use this function unless you really know what you are doing.
392 * And "I want the port number" is not knowing what you are doing.
394 * \retval 0 Address is null
395 * \retval non-zero The port number of the ast_sockaddr
397 #define ast_sockaddr_port(addr) _ast_sockaddr_port(addr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
398 uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func);
404 * Sets the port number of a socket address.
406 * \warning Do not use this function unless you really know what you are doing.
407 * And "I want the port number" is not knowing what you are doing.
409 * \param addr Address on which to set the port
410 * \param port The port you wish to set the address to use
413 #define ast_sockaddr_set_port(addr,port) _ast_sockaddr_set_port(addr,port,__FILE__,__LINE__,__PRETTY_FUNCTION__)
414 void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func);
420 * Get an IPv4 address of an ast_sockaddr
422 * \warning You should rarely need this function. Only use if you know what
424 * \return IPv4 address in network byte order
426 uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr);
432 * Determine if the address is an IPv4 address
434 * \warning You should rarely need this function. Only use if you know what
436 * \retval 1 This is an IPv4 address
437 * \retval 0 This is an IPv6 or IPv4-mapped IPv6 address
439 int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr);
445 * Determine if this is an IPv4-mapped IPv6 address
447 * \warning You should rarely need this function. Only use if you know what
450 * \retval 1 This is an IPv4-mapped IPv6 address.
451 * \retval 0 This is not an IPv4-mapped IPv6 address.
453 int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr);
459 * Determine if an IPv4 address is a multicast address
461 * \parm addr the address to check
463 * This function checks if an address is in the 224.0.0.0/4 network block.
465 * \return non-zero if this is a multicast address
467 int ast_sockaddr_is_ipv4_multicast(const struct ast_sockaddr *addr);
473 * Determine if this is a link-local IPv6 address
475 * \warning You should rarely need this function. Only use if you know what
478 * \retval 1 This is a link-local IPv6 address.
479 * \retval 0 This is link-local IPv6 address.
481 int ast_sockaddr_is_ipv6_link_local(const struct ast_sockaddr *addr);
487 * Determine if this is an IPv6 address
489 * \warning You should rarely need this function. Only use if you know what
492 * \retval 1 This is an IPv6 or IPv4-mapped IPv6 address.
493 * \retval 0 This is an IPv4 address.
495 int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr);
501 * Determine if the address type is unspecified, or "any" address.
504 * For IPv4, this would be the address 0.0.0.0, and for IPv6,
505 * this would be the address ::. The port number is ignored.
507 * \retval 1 This is an "any" address
508 * \retval 0 This is not an "any" address
510 int ast_sockaddr_is_any(const struct ast_sockaddr *addr);
516 * Computes a hash value from the address. The port is ignored.
518 * \retval 0 Unknown address family
519 * \retval other A 32-bit hash derived from the address
521 int ast_sockaddr_hash(const struct ast_sockaddr *addr);
527 * Wrapper around accept(2) that uses struct ast_sockaddr.
530 * For parameter and return information, see the man page for
533 int ast_accept(int sockfd, struct ast_sockaddr *addr);
539 * Wrapper around bind(2) that uses struct ast_sockaddr.
542 * For parameter and return information, see the man page for
545 int ast_bind(int sockfd, const struct ast_sockaddr *addr);
551 * Wrapper around connect(2) that uses struct ast_sockaddr.
554 * For parameter and return information, see the man page for
557 int ast_connect(int sockfd, const struct ast_sockaddr *addr);
563 * Wrapper around getsockname(2) that uses struct ast_sockaddr.
566 * For parameter and return information, see the man page for
569 int ast_getsockname(int sockfd, struct ast_sockaddr *addr);
575 * Wrapper around recvfrom(2) that uses struct ast_sockaddr.
578 * For parameter and return information, see the man page for
581 ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags,
582 struct ast_sockaddr *src_addr);
588 * Wrapper around sendto(2) that uses ast_sockaddr.
592 * return information, see the man page for sendto(2)
594 ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags,
595 const struct ast_sockaddr *dest_addr);
601 * Set type of service
604 * Set ToS ("Type of Service for IPv4 and "Traffic Class for IPv6) and
605 * CoS (Linux's SO_PRIORITY)
607 * \param sockfd File descriptor for socket on which to set the parameters
608 * \param tos The type of service for the socket
609 * \param cos The cost of service for the socket
610 * \param desc A text description of the socket in question.
612 * \retval -1 Error, with errno set to an appropriate value
614 int ast_set_qos(int sockfd, int tos, int cos, const char *desc);
617 * These are backward compatibility functions that may be used by subsystems
618 * that have not yet been converted to IPv6. They will be removed when all
619 * subsystems are IPv6-ready.
627 * Converts a struct ast_sockaddr to a struct sockaddr_in.
629 * \param addr The ast_sockaddr to convert
630 * \param[out] sin The resulting sockaddr_in struct
631 * \retval nonzero Success
632 * \retval zero Failure
634 #define ast_sockaddr_to_sin(addr,sin) _ast_sockaddr_to_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
635 int _ast_sockaddr_to_sin(const struct ast_sockaddr *addr,
636 struct sockaddr_in *sin, const char *file, int line, const char *func);
642 * Converts a struct sockaddr_in to a struct ast_sockaddr.
644 * \param sin The sockaddr_in to convert
645 * \return an ast_sockaddr structure
647 #define ast_sockaddr_from_sin(addr,sin) _ast_sockaddr_from_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
648 void _ast_sockaddr_from_sin(struct ast_sockaddr *addr, const struct sockaddr_in *sin,
649 const char *file, int line, const char *func);
653 #if defined(__cplusplus) || defined(c_plusplus)
657 #endif /* _ASTERISK_NETSOCK2_H */