Mon Mar 12 2012 21:21:40

Asterisk developer's documentation


netsock2.h
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2010, Digium, Inc.
00005  *
00006  * Viagénie <asteriskv6@viagenie.ca>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief Network socket handling
00021  */
00022 
00023 #ifndef _ASTERISK_NETSOCK2_H
00024 #define _ASTERISK_NETSOCK2_H
00025 
00026 #if defined(__cplusplus) || defined(c_plusplus)
00027 extern "C" {
00028 #endif
00029 
00030 #include <sys/socket.h>
00031 
00032 #include <netinet/in.h>
00033 
00034 /*!
00035  * Values for address families that we support. This is reproduced from socket.h
00036  * because we do not want users to include that file. Only netsock2.c should
00037  * ever include socket.h.
00038  */
00039 enum {
00040    AST_AF_UNSPEC  = 0,
00041    AST_AF_INET = 2,
00042    AST_AF_INET6   = 10,
00043 };
00044 
00045 /*!
00046  * \brief Socket address structure.
00047  *
00048  * \details
00049  * The first member is big enough to contain addresses of any
00050  * family. The second member contains the length (in bytes) used
00051  * in the first member.
00052  *
00053  * \note
00054  * Some BSDs have the length embedded in sockaddr structs. We
00055  * ignore them. (This is the right thing to do.)
00056  *
00057  * \note
00058  * It is important to always initialize ast_sockaddr before use
00059  * -- even if they are passed to ast_sockaddr_copy() as the
00060  * underlying storage could be bigger than what ends up being
00061  * copied -- leaving part of the data unitialized.
00062  */
00063 struct ast_sockaddr {
00064    struct sockaddr_storage  ss;
00065    socklen_t len;
00066 };
00067 
00068 /*!
00069  * \brief
00070  * Convert an IPv4-mapped IPv6 address into an IPv4 address.
00071  *
00072  * \warning You should rarely need this function. Only call this
00073  * if you know what you're doing.
00074  *
00075  * \param addr The IPv4-mapped address to convert
00076  * \param mapped_addr The resulting IPv4 address
00077  * \retval 0 Unable to make the conversion
00078  * \retval 1 Successful conversion
00079  */
00080 int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped);
00081 
00082 /*!
00083  * \since 1.8
00084  *
00085  * \brief
00086  * Checks if the ast_sockaddr is null. "null" in this sense essentially
00087  * means uninitialized, or having a 0 length.
00088  *
00089  * \param addr Pointer to the ast_sockaddr we wish to check
00090  * \retval 1 \a addr is null
00091  * \retval 0 \a addr is non-null.
00092  */
00093 static inline int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
00094 {
00095    return !addr || addr->len == 0;
00096 }
00097 
00098 /*!
00099  * \since 1.8
00100  *
00101  * \brief
00102  * Sets address \a addr to null.
00103  *
00104  * \retval void
00105  */
00106 static inline void ast_sockaddr_setnull(struct ast_sockaddr *addr)
00107 {
00108    addr->len = 0;
00109 }
00110 
00111 /*!
00112  * \since 1.8
00113  *
00114  * \brief
00115  * Copies the data from one ast_sockaddr to another
00116  *
00117  * \param dst The destination ast_sockaddr
00118  * \param src The source ast_sockaddr
00119  * \retval void
00120  */
00121 static inline void ast_sockaddr_copy(struct ast_sockaddr *dst,
00122       const struct ast_sockaddr *src)
00123 {
00124    memcpy(dst, src, src->len);
00125    dst->len = src->len;
00126 };
00127 
00128 /*!
00129  * \since 1.8
00130  *
00131  * \brief
00132  * Compares two ast_sockaddr structures
00133  *
00134  * \retval -1 \a a is lexicographically smaller than \a b
00135  * \retval 0 \a a is equal to \a b
00136  * \retval 1 \a b is lexicographically smaller than \a a
00137  */
00138 int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b);
00139 
00140 /*!
00141  * \since 1.8
00142  *
00143  * \brief
00144  * Compares the addresses of two ast_sockaddr structures.
00145  *
00146  * \retval -1 \a a is lexicographically smaller than \a b
00147  * \retval 0 \a a is equal to \a b
00148  * \retval 1 \a b is lexicographically smaller than \a a
00149  */
00150 int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b);
00151 
00152 #define AST_SOCKADDR_STR_ADDR    (1 << 0)
00153 #define AST_SOCKADDR_STR_PORT    (1 << 1)
00154 #define AST_SOCKADDR_STR_BRACKETS   (1 << 2)
00155 #define AST_SOCKADDR_STR_REMOTE     (1 << 3)
00156 #define AST_SOCKADDR_STR_HOST    (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_BRACKETS)
00157 #define AST_SOCKADDR_STR_DEFAULT (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT)
00158 #define AST_SOCKADDR_STR_ADDR_REMOTE     (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_REMOTE)
00159 #define AST_SOCKADDR_STR_HOST_REMOTE     (AST_SOCKADDR_STR_HOST | AST_SOCKADDR_STR_REMOTE)
00160 #define AST_SOCKADDR_STR_DEFAULT_REMOTE  (AST_SOCKADDR_STR_DEFAULT | AST_SOCKADDR_STR_REMOTE)
00161 #define AST_SOCKADDR_STR_FORMAT_MASK     (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT | AST_SOCKADDR_STR_BRACKETS)
00162 
00163 /*!
00164  * \since 1.8
00165  *
00166  * \brief
00167  * Convert a socket address to a string.
00168  *
00169  * \details
00170  * This will be of the form a.b.c.d:xyz
00171  * for IPv4 and [a:b:c:...:d]:xyz for IPv6.
00172  *
00173  * This function is thread-safe. The returned string is on static
00174  * thread-specific storage.
00175  *
00176  * \param addr The input to be stringified
00177  * \param format one of the following:
00178  * AST_SOCKADDR_STR_DEFAULT:
00179  *    a.b.c.d:xyz for IPv4
00180  *    [a:b:c:...:d]:xyz for IPv6.
00181  * AST_SOCKADDR_STR_ADDR: address only
00182  *    a.b.c.d for IPv4
00183  *    a:b:c:...:d for IPv6.
00184  * AST_SOCKADDR_STR_HOST: address only, suitable for a URL
00185  *    a.b.c.d for IPv4
00186  *    [a:b:c:...:d] for IPv6.
00187  * AST_SOCKADDR_STR_PORT: port only
00188  * \retval "(null)" \a addr is null
00189  * \retval "" An error occurred during processing
00190  * \retval string The stringified form of the address
00191  */
00192 char *ast_sockaddr_stringify_fmt(const struct ast_sockaddr *addr, int format);
00193 
00194 /*!
00195  * \since 1.8
00196  *
00197  * \brief
00198  * Wrapper around ast_sockaddr_stringify_fmt() with default format
00199  *
00200  * \return same as ast_sockaddr_stringify_fmt()
00201  */
00202 static inline char *ast_sockaddr_stringify(const struct ast_sockaddr *addr)
00203 {
00204    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_DEFAULT);
00205 }
00206 
00207 /*!
00208  * \since 1.8
00209  *
00210  * \brief
00211  * Wrapper around ast_sockaddr_stringify_fmt() with default format
00212  *
00213  * \note This address will be suitable for passing to a remote machine via the
00214  * application layer. For example, the scope-id on a link-local IPv6 address
00215  * will be stripped.
00216  *
00217  * \return same as ast_sockaddr_stringify_fmt()
00218  */
00219 static inline char *ast_sockaddr_stringify_remote(const struct ast_sockaddr *addr)
00220 {
00221    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_DEFAULT_REMOTE);
00222 }
00223 
00224 /*!
00225  * \since 1.8
00226  *
00227  * \brief
00228  * Wrapper around ast_sockaddr_stringify_fmt() to return an address only
00229  *
00230  * \return same as ast_sockaddr_stringify_fmt()
00231  */
00232 static inline char *ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
00233 {
00234    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_ADDR);
00235 }
00236 
00237 /*!
00238  * \since 1.8
00239  *
00240  * \brief
00241  * Wrapper around ast_sockaddr_stringify_fmt() to return an address only
00242  *
00243  * \note This address will be suitable for passing to a remote machine via the
00244  * application layer. For example, the scope-id on a link-local IPv6 address
00245  * will be stripped.
00246  *
00247  * \return same as ast_sockaddr_stringify_fmt()
00248  */
00249 static inline char *ast_sockaddr_stringify_addr_remote(const struct ast_sockaddr *addr)
00250 {
00251    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_ADDR_REMOTE);
00252 }
00253 
00254 /*!
00255  * \since 1.8
00256  *
00257  * \brief
00258  * Wrapper around ast_sockaddr_stringify_fmt() to return an address only,
00259  * suitable for a URL (with brackets for IPv6).
00260  *
00261  * \return same as ast_sockaddr_stringify_fmt()
00262  */
00263 static inline char *ast_sockaddr_stringify_host(const struct ast_sockaddr *addr)
00264 {
00265    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_HOST);
00266 }
00267 
00268 /*!
00269  * \since 1.8
00270  *
00271  * \brief
00272  * Wrapper around ast_sockaddr_stringify_fmt() to return an address only,
00273  * suitable for a URL (with brackets for IPv6).
00274  *
00275  * \note This address will be suitable for passing to a remote machine via the
00276  * application layer. For example, the scope-id on a link-local IPv6 address
00277  * will be stripped.
00278  *
00279  * \return same as ast_sockaddr_stringify_fmt()
00280  */
00281 static inline char *ast_sockaddr_stringify_host_remote(const struct ast_sockaddr *addr)
00282 {
00283    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_HOST_REMOTE);
00284 }
00285 
00286 /*!
00287  * \since 1.8
00288  *
00289  * \brief
00290  * Wrapper around ast_sockaddr_stringify_fmt() to return a port only
00291  *
00292  * \return same as ast_sockaddr_stringify_fmt()
00293  */
00294 static inline char *ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
00295 {
00296    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_PORT);
00297 }
00298 
00299 /*!
00300  * \since 1.8
00301  *
00302  * \brief
00303  * Splits a string into its host and port components
00304  *
00305  * \param str[in]   The string to parse. May be modified by writing a NUL at the end of
00306  *                  the host part.
00307  * \param host[out] Pointer to the host component within \a str.
00308  * \param port[out] Pointer to the port component within \a str.
00309  * \param flags     If set to zero, a port MAY be present. If set to PARSE_PORT_IGNORE, a
00310  *                  port MAY be present but will be ignored. If set to PARSE_PORT_REQUIRE,
00311  *                  a port MUST be present. If set to PARSE_PORT_FORBID, a port MUST NOT
00312  *                  be present.
00313  *
00314  * \retval 1 Success
00315  * \retval 0 Failure
00316  */
00317 int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags);
00318 
00319 /*!
00320  * \since 1.8
00321  *
00322  * \brief
00323  * Parse an IPv4 or IPv6 address string.
00324  *
00325  * \details
00326  * Parses a string containing an IPv4 or IPv6 address followed by an optional
00327  * port (separated by a colon) into a struct ast_sockaddr. The allowed formats
00328  * are the following:
00329  *
00330  * a.b.c.d
00331  * a.b.c.d:port
00332  * a:b:c:...:d
00333  * [a:b:c:...:d]
00334  * [a:b:c:...:d]:port
00335  *
00336  * Host names are NOT allowed.
00337  *
00338  * \param[out] addr The resulting ast_sockaddr
00339  * \param str The string to parse
00340  * \param flags If set to zero, a port MAY be present. If set to
00341  * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
00342  * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
00343  * port MUST NOT be present.
00344  *
00345  * \retval 1 Success
00346  * \retval 0 Failure
00347  */
00348 int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags);
00349 
00350 /*!
00351  * \since 1.8
00352  *
00353  * \brief
00354  * Parses a string with an IPv4 or IPv6 address and place results into an array
00355  *
00356  * \details
00357  * Parses a string containing a host name or an IPv4 or IPv6 address followed
00358  * by an optional port (separated by a colon).  The result is returned into a
00359  * array of struct ast_sockaddr. Allowed formats for str are the following:
00360  *
00361  * hostname:port
00362  * host.example.com:port
00363  * a.b.c.d
00364  * a.b.c.d:port
00365  * a:b:c:...:d
00366  * [a:b:c:...:d]
00367  * [a:b:c:...:d]:port
00368  *
00369  * \param[out] addrs The resulting array of ast_sockaddrs
00370  * \param str The string to parse
00371  * \param flags If set to zero, a port MAY be present. If set to
00372  * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
00373  * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
00374  * port MUST NOT be present.
00375  *
00376  * \param family Only addresses of the given family will be returned. Use 0 or
00377  * AST_SOCKADDR_UNSPEC to get addresses of all families.
00378  *
00379  * \retval 0 Failure
00380  * \retval non-zero The number of elements in addrs array.
00381  */
00382 int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
00383           int flags, int family);
00384 
00385 /*!
00386  * \since 1.8
00387  *
00388  * \brief
00389  * Get the port number of a socket address.
00390  *
00391  * \warning Do not use this function unless you really know what you are doing.
00392  * And "I want the port number" is not knowing what you are doing.
00393  *
00394  * \retval 0 Address is null
00395  * \retval non-zero The port number of the ast_sockaddr
00396  */
00397 #define ast_sockaddr_port(addr)  _ast_sockaddr_port(addr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
00398 uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func);
00399 
00400 /*!
00401  * \since 1.8
00402  *
00403  * \brief
00404  * Sets the port number of a socket address.
00405  *
00406  * \warning Do not use this function unless you really know what you are doing.
00407  * And "I want the port number" is not knowing what you are doing.
00408  *
00409  * \param addr Address on which to set the port
00410  * \param port The port you wish to set the address to use
00411  * \retval void
00412  */
00413 #define ast_sockaddr_set_port(addr,port)  _ast_sockaddr_set_port(addr,port,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00414 void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func);
00415 
00416 /*!
00417  * \since 1.8
00418  *
00419  * \brief
00420  * Get an IPv4 address of an ast_sockaddr
00421  *
00422  * \warning You should rarely need this function. Only use if you know what
00423  * you're doing.
00424  * \return IPv4 address in network byte order
00425  */
00426 uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr);
00427 
00428 /*!
00429  * \since 1.8
00430  *
00431  * \brief
00432  * Determine if the address is an IPv4 address
00433  *
00434  * \warning You should rarely need this function. Only use if you know what
00435  * you're doing.
00436  * \retval 1 This is an IPv4 address
00437  * \retval 0 This is an IPv6 or IPv4-mapped IPv6 address
00438  */
00439 int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr);
00440 
00441 /*!
00442  * \since 1.8
00443  *
00444  * \brief
00445  * Determine if this is an IPv4-mapped IPv6 address
00446  *
00447  * \warning You should rarely need this function. Only use if you know what
00448  * you're doing.
00449  *
00450  * \retval 1 This is an IPv4-mapped IPv6 address.
00451  * \retval 0 This is not an IPv4-mapped IPv6 address.
00452  */
00453 int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr);
00454 
00455 /*!
00456  * \since 1.8
00457  *
00458  * \brief
00459  * Determine if this is a link-local IPv6 address
00460  *
00461  * \warning You should rarely need this function. Only use if you know what
00462  * you're doing.
00463  *
00464  * \retval 1 This is a link-local IPv6 address.
00465  * \retval 0 This is link-local IPv6 address.
00466  */
00467 int ast_sockaddr_is_ipv6_link_local(const struct ast_sockaddr *addr);
00468 
00469 /*!
00470  * \since 1.8
00471  *
00472  * \brief
00473  * Determine if this is an IPv6 address
00474  *
00475  * \warning You should rarely need this function. Only use if you know what
00476  * you're doing.
00477  *
00478  * \retval 1 This is an IPv6 or IPv4-mapped IPv6 address.
00479  * \retval 0 This is an IPv4 address.
00480  */
00481 int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr);
00482 
00483 /*!
00484  * \since 1.8
00485  *
00486  * \brief
00487  * Determine if the address type is unspecified, or "any" address.
00488  *
00489  * \details
00490  * For IPv4, this would be the address 0.0.0.0, and for IPv6,
00491  * this would be the address ::. The port number is ignored.
00492  *
00493  * \retval 1 This is an "any" address
00494  * \retval 0 This is not an "any" address
00495  */
00496 int ast_sockaddr_is_any(const struct ast_sockaddr *addr);
00497 
00498 /*!
00499  * \since 1.8
00500  *
00501  * \brief
00502  * Computes a hash value from the address. The port is ignored.
00503  *
00504  * \retval 0 Unknown address family
00505  * \retval other A 32-bit hash derived from the address
00506  */
00507 int ast_sockaddr_hash(const struct ast_sockaddr *addr);
00508 
00509 /*!
00510  * \since 1.8
00511  *
00512  * \brief
00513  * Wrapper around accept(2) that uses struct ast_sockaddr.
00514  *
00515  * \details
00516  * For parameter and return information, see the man page for
00517  * accept(2).
00518  */
00519 int ast_accept(int sockfd, struct ast_sockaddr *addr);
00520 
00521 /*!
00522  * \since 1.8
00523  *
00524  * \brief
00525  * Wrapper around bind(2) that uses struct ast_sockaddr.
00526  *
00527  * \details
00528  * For parameter and return information, see the man page for
00529  * bind(2).
00530  */
00531 int ast_bind(int sockfd, const struct ast_sockaddr *addr);
00532 
00533 /*!
00534  * \since 1.8
00535  *
00536  * \brief
00537  * Wrapper around connect(2) that uses struct ast_sockaddr.
00538  *
00539  * \details
00540  * For parameter and return information, see the man page for
00541  * connect(2).
00542  */
00543 int ast_connect(int sockfd, const struct ast_sockaddr *addr);
00544 
00545 /*!
00546  * \since 1.8
00547  *
00548  * \brief
00549  * Wrapper around getsockname(2) that uses struct ast_sockaddr.
00550  *
00551  * \details
00552  * For parameter and return information, see the man page for
00553  * getsockname(2).
00554  */
00555 int ast_getsockname(int sockfd, struct ast_sockaddr *addr);
00556 
00557 /*!
00558  * \since 1.8
00559  *
00560  * \brief
00561  * Wrapper around recvfrom(2) that uses struct ast_sockaddr.
00562  *
00563  * \details
00564  * For parameter and return information, see the man page for
00565  * recvfrom(2).
00566  */
00567 ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags,
00568            struct ast_sockaddr *src_addr);
00569 
00570 /*!
00571  * \since 1.8
00572  *
00573  * \brief
00574  * Wrapper around sendto(2) that uses ast_sockaddr.
00575  *
00576  * \details
00577  * For parameter and
00578  * return information, see the man page for sendto(2)
00579  */
00580 ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags,
00581          const struct ast_sockaddr *dest_addr);
00582 
00583 /*!
00584  * \since 1.8
00585  *
00586  * \brief
00587  * Set type of service
00588  *
00589  * \details
00590  * Set ToS ("Type of Service for IPv4 and "Traffic Class for IPv6) and
00591  * CoS (Linux's SO_PRIORITY)
00592  *
00593  * \param sockfd File descriptor for socket on which to set the parameters
00594  * \param tos The type of service for the socket
00595  * \param cos The cost of service for the socket
00596  * \param desc A text description of the socket in question.
00597  * \retval 0 Success
00598  * \retval -1 Error, with errno set to an appropriate value
00599  */
00600 int ast_set_qos(int sockfd, int tos, int cos, const char *desc);
00601 
00602 /*!
00603  * These are backward compatibility functions that may be used by subsystems
00604  * that have not yet been converted to IPv6. They will be removed when all
00605  * subsystems are IPv6-ready.
00606  */
00607 /*@{*/
00608 
00609 /*!
00610  * \since 1.8
00611  *
00612  * \brief
00613  * Converts a struct ast_sockaddr to a struct sockaddr_in.
00614  *
00615  * \param addr The ast_sockaddr to convert
00616  * \param[out] sin The resulting sockaddr_in struct
00617  * \retval nonzero Success
00618  * \retval zero Failure
00619  */
00620 #define ast_sockaddr_to_sin(addr,sin)  _ast_sockaddr_to_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
00621 int _ast_sockaddr_to_sin(const struct ast_sockaddr *addr,
00622          struct sockaddr_in *sin, const char *file, int line, const char *func);
00623 
00624 /*!
00625  * \since 1.8
00626  *
00627  * \brief
00628  * Converts a struct sockaddr_in to a struct ast_sockaddr.
00629  *
00630  * \param sin The sockaddr_in to convert
00631  * \return an ast_sockaddr structure
00632  */
00633 #define ast_sockaddr_from_sin(addr,sin)   _ast_sockaddr_from_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
00634 void _ast_sockaddr_from_sin(struct ast_sockaddr *addr, const struct sockaddr_in *sin,
00635       const char *file, int line, const char *func);
00636 
00637 /*@}*/
00638 
00639 #if defined(__cplusplus) || defined(c_plusplus)
00640 }
00641 #endif
00642 
00643 #endif /* _ASTERISK_NETSOCK2_H */