netsock2.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * Viagénie <asteriskv6@viagenie.ca>
  7. *
  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.
  13. *
  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.
  17. */
  18. /*! \file
  19. * \brief Network socket handling
  20. */
  21. #ifndef _ASTERISK_NETSOCK2_H
  22. #define _ASTERISK_NETSOCK2_H
  23. #if defined(__cplusplus) || defined(c_plusplus)
  24. extern "C" {
  25. #endif
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. /*
  29. * String buffer size that can accommodate a fully stringified representation of a
  30. * supported IP address & port:
  31. *
  32. * - 45 bytes for an IPv6 address
  33. * - 2 bytes for brackets around an IPv6 address
  34. * - 1 byte for the port separator (a colon)
  35. * - 5 bytes for the port
  36. * - 1 byte for the zero-terminator
  37. */
  38. #define AST_SOCKADDR_BUFLEN (45 + 2 + 1 + 5 + 1)
  39. /*!
  40. * Values for address families that we support. This is reproduced from socket.h
  41. * because we do not want users to include that file. Only netsock2.c should
  42. * ever include socket.h.
  43. */
  44. enum {
  45. AST_AF_UNSPEC = AF_UNSPEC,
  46. AST_AF_INET = AF_INET,
  47. AST_AF_INET6 = AF_INET6,
  48. };
  49. enum ast_transport {
  50. AST_TRANSPORT_UDP = 1,
  51. AST_TRANSPORT_TCP = 1 << 1,
  52. AST_TRANSPORT_TLS = 1 << 2,
  53. AST_TRANSPORT_WS = 1 << 3,
  54. AST_TRANSPORT_WSS = 1 << 4,
  55. };
  56. /*!
  57. * \brief
  58. * Isolate a 32-bit section of an IPv6 address
  59. *
  60. * An IPv6 address can be divided into 4 32-bit chunks. This gives
  61. * easy access to one of these chunks.
  62. *
  63. * \param sin6 A pointer to a struct sockaddr_in6
  64. * \param index Which 32-bit chunk to operate on. Must be in the range 0-3.
  65. */
  66. #define V6_WORD(sin6, index) ((uint32_t *)&((sin6)->sin6_addr))[(index)]
  67. /*!
  68. * \brief Socket address structure.
  69. *
  70. * \details
  71. * The first member is big enough to contain addresses of any
  72. * family. The second member contains the length (in bytes) used
  73. * in the first member.
  74. *
  75. * \note
  76. * Some BSDs have the length embedded in sockaddr structs. We
  77. * ignore them. (This is the right thing to do.)
  78. *
  79. * \note
  80. * It is important to always initialize ast_sockaddr before use
  81. * -- even if they are passed to ast_sockaddr_copy() as the
  82. * underlying storage could be bigger than what ends up being
  83. * copied -- leaving part of the data unitialized.
  84. */
  85. struct ast_sockaddr {
  86. struct sockaddr_storage ss;
  87. socklen_t len;
  88. };
  89. /*!
  90. * \brief
  91. * Convert an IPv4-mapped IPv6 address into an IPv4 address.
  92. *
  93. * \warning You should rarely need this function. Only call this
  94. * if you know what you're doing.
  95. *
  96. * \param addr The IPv4-mapped address to convert
  97. * \param ast_mapped The resulting IPv4 address
  98. * \retval 0 Unable to make the conversion
  99. * \retval 1 Successful conversion
  100. */
  101. int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped);
  102. /*!
  103. * \since 1.8
  104. *
  105. * \brief
  106. * Checks if the ast_sockaddr is null. "null" in this sense essentially
  107. * means uninitialized, or having a 0 length.
  108. *
  109. * \param addr Pointer to the ast_sockaddr we wish to check
  110. * \retval 1 \a addr is null
  111. * \retval 0 \a addr is non-null.
  112. */
  113. static inline int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
  114. {
  115. return !addr || addr->len == 0;
  116. }
  117. /*!
  118. * \since 1.8
  119. *
  120. * \brief
  121. * Sets address \a addr to null.
  122. *
  123. * \retval void
  124. */
  125. static inline void ast_sockaddr_setnull(struct ast_sockaddr *addr)
  126. {
  127. addr->len = 0;
  128. }
  129. /*!
  130. * \since 1.8
  131. *
  132. * \brief
  133. * Copies the data from one ast_sockaddr to another
  134. *
  135. * \param dst The destination ast_sockaddr
  136. * \param src The source ast_sockaddr
  137. * \retval void
  138. */
  139. static inline void ast_sockaddr_copy(struct ast_sockaddr *dst,
  140. const struct ast_sockaddr *src)
  141. {
  142. memcpy(dst, src, src->len);
  143. dst->len = src->len;
  144. };
  145. /*!
  146. * \since 1.8
  147. *
  148. * \brief
  149. * Compares two ast_sockaddr structures
  150. *
  151. * \retval -1 \a a is lexicographically smaller than \a b
  152. * \retval 0 \a a is equal to \a b
  153. * \retval 1 \a b is lexicographically smaller than \a a
  154. */
  155. int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b);
  156. /*!
  157. * \since 1.8
  158. *
  159. * \brief
  160. * Compares the addresses of two ast_sockaddr structures.
  161. *
  162. * \retval -1 \a a is lexicographically smaller than \a b
  163. * \retval 0 \a a is equal to \a b
  164. * \retval 1 \a b is lexicographically smaller than \a a
  165. */
  166. int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b);
  167. #define AST_SOCKADDR_STR_ADDR (1 << 0)
  168. #define AST_SOCKADDR_STR_PORT (1 << 1)
  169. #define AST_SOCKADDR_STR_BRACKETS (1 << 2)
  170. #define AST_SOCKADDR_STR_REMOTE (1 << 3)
  171. #define AST_SOCKADDR_STR_HOST (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_BRACKETS)
  172. #define AST_SOCKADDR_STR_DEFAULT (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT)
  173. #define AST_SOCKADDR_STR_ADDR_REMOTE (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_REMOTE)
  174. #define AST_SOCKADDR_STR_HOST_REMOTE (AST_SOCKADDR_STR_HOST | AST_SOCKADDR_STR_REMOTE)
  175. #define AST_SOCKADDR_STR_DEFAULT_REMOTE (AST_SOCKADDR_STR_DEFAULT | AST_SOCKADDR_STR_REMOTE)
  176. #define AST_SOCKADDR_STR_FORMAT_MASK (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT | AST_SOCKADDR_STR_BRACKETS)
  177. /*!
  178. * \since 1.8
  179. *
  180. * \brief
  181. * Convert a socket address to a string.
  182. *
  183. * \details
  184. * This will be of the form a.b.c.d:xyz
  185. * for IPv4 and [a:b:c:...:d]:xyz for IPv6.
  186. *
  187. * This function is thread-safe. The returned string is on static
  188. * thread-specific storage.
  189. *
  190. * \param addr The input to be stringified
  191. * \param format one of the following:
  192. * AST_SOCKADDR_STR_DEFAULT:
  193. * a.b.c.d:xyz for IPv4
  194. * [a:b:c:...:d]:xyz for IPv6.
  195. * AST_SOCKADDR_STR_ADDR: address only
  196. * a.b.c.d for IPv4
  197. * a:b:c:...:d for IPv6.
  198. * AST_SOCKADDR_STR_HOST: address only, suitable for a URL
  199. * a.b.c.d for IPv4
  200. * [a:b:c:...:d] for IPv6.
  201. * AST_SOCKADDR_STR_PORT: port only
  202. *
  203. * \note The string pointer returned by this function will point to a string that
  204. * will be changed whenever any form of ast_sockaddr_stringify_fmt is called on that
  205. * thread. Because of this, it is important that if you use this function, you use the
  206. * string before another use of this function is made elsewhere in the same thread.
  207. * The easiest way to accomplish this is by immediately copying the string to a buffer
  208. * with something like ast_strdupa.
  209. *
  210. * \retval "(null)" \a addr is null
  211. * \retval "" An error occurred during processing
  212. * \retval string The stringified form of the address
  213. */
  214. char *ast_sockaddr_stringify_fmt(const struct ast_sockaddr *addr, int format);
  215. /*!
  216. * \since 1.8
  217. *
  218. * \brief
  219. * Wrapper around ast_sockaddr_stringify_fmt() with default format
  220. *
  221. * \return same as ast_sockaddr_stringify_fmt()
  222. */
  223. static inline char *ast_sockaddr_stringify(const struct ast_sockaddr *addr)
  224. {
  225. return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_DEFAULT);
  226. }
  227. /*!
  228. * \since 1.8
  229. *
  230. * \brief
  231. * Wrapper around ast_sockaddr_stringify_fmt() with default format
  232. *
  233. * \note This address will be suitable for passing to a remote machine via the
  234. * application layer. For example, the scope-id on a link-local IPv6 address
  235. * will be stripped.
  236. *
  237. * \return same as ast_sockaddr_stringify_fmt()
  238. */
  239. static inline char *ast_sockaddr_stringify_remote(const struct ast_sockaddr *addr)
  240. {
  241. return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_DEFAULT_REMOTE);
  242. }
  243. /*!
  244. * \since 1.8
  245. *
  246. * \brief
  247. * Wrapper around ast_sockaddr_stringify_fmt() to return an address only
  248. *
  249. * \return same as ast_sockaddr_stringify_fmt()
  250. */
  251. static inline char *ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
  252. {
  253. return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_ADDR);
  254. }
  255. /*!
  256. * \since 12.4
  257. *
  258. * \brief
  259. * Count the 1 bits in a netmask
  260. *
  261. * \return number of 1 bits
  262. */
  263. int ast_sockaddr_cidr_bits(const struct ast_sockaddr *sa);
  264. /*!
  265. * \since 1.8
  266. *
  267. * \brief
  268. * Wrapper around ast_sockaddr_stringify_fmt() to return an address only
  269. *
  270. * \note This address will be suitable for passing to a remote machine via the
  271. * application layer. For example, the scope-id on a link-local IPv6 address
  272. * will be stripped.
  273. *
  274. * \return same as ast_sockaddr_stringify_fmt()
  275. */
  276. static inline char *ast_sockaddr_stringify_addr_remote(const struct ast_sockaddr *addr)
  277. {
  278. return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_ADDR_REMOTE);
  279. }
  280. /*!
  281. * \since 1.8
  282. *
  283. * \brief
  284. * Wrapper around ast_sockaddr_stringify_fmt() to return an address only,
  285. * suitable for a URL (with brackets for IPv6).
  286. *
  287. * \return same as ast_sockaddr_stringify_fmt()
  288. */
  289. static inline char *ast_sockaddr_stringify_host(const struct ast_sockaddr *addr)
  290. {
  291. return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_HOST);
  292. }
  293. /*!
  294. * \since 1.8
  295. *
  296. * \brief
  297. * Wrapper around ast_sockaddr_stringify_fmt() to return an address only,
  298. * suitable for a URL (with brackets for IPv6).
  299. *
  300. * \note This address will be suitable for passing to a remote machine via the
  301. * application layer. For example, the scope-id on a link-local IPv6 address
  302. * will be stripped.
  303. *
  304. * \return same as ast_sockaddr_stringify_fmt()
  305. */
  306. static inline char *ast_sockaddr_stringify_host_remote(const struct ast_sockaddr *addr)
  307. {
  308. return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_HOST_REMOTE);
  309. }
  310. /*!
  311. * \since 1.8
  312. *
  313. * \brief
  314. * Wrapper around ast_sockaddr_stringify_fmt() to return a port only
  315. *
  316. * \return same as ast_sockaddr_stringify_fmt()
  317. */
  318. static inline char *ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
  319. {
  320. return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_PORT);
  321. }
  322. /*!
  323. * \since 1.8
  324. *
  325. * \brief
  326. * Splits a string into its host and port components
  327. *
  328. * \param[in] str The string to parse. May be modified by writing a NUL at the end of
  329. * the host part.
  330. * \param[out] host Pointer to the host component within \a str.
  331. * \param[out] port Pointer to the port component within \a str.
  332. * \param flags If set to zero, a port MAY be present. If set to PARSE_PORT_IGNORE, a
  333. * port MAY be present but will be ignored. If set to PARSE_PORT_REQUIRE,
  334. * a port MUST be present. If set to PARSE_PORT_FORBID, a port MUST NOT
  335. * be present.
  336. *
  337. * \retval 1 Success
  338. * \retval 0 Failure
  339. */
  340. int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags);
  341. /*!
  342. * \since 1.8
  343. *
  344. * \brief
  345. * Parse an IPv4 or IPv6 address string.
  346. *
  347. * \details
  348. * Parses a string containing an IPv4 or IPv6 address followed by an optional
  349. * port (separated by a colon) into a struct ast_sockaddr. The allowed formats
  350. * are the following:
  351. *
  352. * a.b.c.d
  353. * a.b.c.d:port
  354. * a:b:c:...:d
  355. * [a:b:c:...:d]
  356. * [a:b:c:...:d]:port
  357. *
  358. * Host names are NOT allowed.
  359. *
  360. * \param[out] addr The resulting ast_sockaddr. This MAY be NULL from
  361. * functions that are performing validity checks only, e.g. ast_parse_arg().
  362. * \param str The string to parse
  363. * \param flags If set to zero, a port MAY be present. If set to
  364. * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
  365. * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
  366. * port MUST NOT be present.
  367. *
  368. * \retval 1 Success
  369. * \retval 0 Failure
  370. */
  371. int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags);
  372. /*!
  373. * \since 1.8
  374. *
  375. * \brief
  376. * Parses a string with an IPv4 or IPv6 address and place results into an array
  377. *
  378. * \details
  379. * Parses a string containing a host name or an IPv4 or IPv6 address followed
  380. * by an optional port (separated by a colon). The result is returned into a
  381. * array of struct ast_sockaddr. Allowed formats for str are the following:
  382. *
  383. * hostname:port
  384. * host.example.com:port
  385. * a.b.c.d
  386. * a.b.c.d:port
  387. * a:b:c:...:d
  388. * [a:b:c:...:d]
  389. * [a:b:c:...:d]:port
  390. *
  391. * \param[out] addrs The resulting array of ast_sockaddrs
  392. * \param str The string to parse
  393. * \param flags If set to zero, a port MAY be present. If set to
  394. * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
  395. * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
  396. * port MUST NOT be present.
  397. *
  398. * \param family Only addresses of the given family will be returned. Use 0 or
  399. * AST_AF_UNSPEC to get addresses of all families.
  400. *
  401. * \retval 0 Failure
  402. * \retval non-zero The number of elements in addrs array.
  403. */
  404. int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
  405. int flags, int family);
  406. /*!
  407. * \brief
  408. * Apply a netmask to an address and store the result in a separate structure.
  409. *
  410. * When dealing with IPv6 addresses, one cannot apply a netmask with a simple
  411. * logical AND operation. Futhermore, the incoming address may be an IPv4
  412. * address and needs to be mapped properly before attempting to apply a rule.
  413. *
  414. * \param addr The IP address to apply the mask to.
  415. * \param netmask The netmask configured in the host access rule.
  416. * \param result The resultant address after applying the netmask to the given address
  417. * \retval 0 Successfully applied netmask
  418. * \retval -1 Failed to apply netmask
  419. */
  420. int ast_sockaddr_apply_netmask(const struct ast_sockaddr *addr, const struct ast_sockaddr *netmask,
  421. struct ast_sockaddr *result);
  422. /*!
  423. * \since 1.8
  424. *
  425. * \brief
  426. * Get the port number of a socket address.
  427. *
  428. * \warning Do not use this function unless you really know what you are doing.
  429. * And "I want the port number" is not knowing what you are doing.
  430. *
  431. * \retval 0 Address is null
  432. * \retval non-zero The port number of the ast_sockaddr
  433. */
  434. #define ast_sockaddr_port(addr) _ast_sockaddr_port(addr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  435. uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func);
  436. /*!
  437. * \since 1.8
  438. *
  439. * \brief
  440. * Sets the port number of a socket address.
  441. *
  442. * \warning Do not use this function unless you really know what you are doing.
  443. * And "I want the port number" is not knowing what you are doing.
  444. *
  445. * \param addr Address on which to set the port
  446. * \param port The port you wish to set the address to use
  447. * \retval void
  448. */
  449. #define ast_sockaddr_set_port(addr,port) _ast_sockaddr_set_port(addr,port,__FILE__,__LINE__,__PRETTY_FUNCTION__)
  450. void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func);
  451. /*!
  452. * \since 1.8
  453. *
  454. * \brief
  455. * Get an IPv4 address of an ast_sockaddr
  456. *
  457. * \warning You should rarely need this function. Only use if you know what
  458. * you're doing.
  459. * \return IPv4 address in network byte order
  460. */
  461. uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr);
  462. /*!
  463. * \since 1.8
  464. *
  465. * \brief
  466. * Determine if the address is an IPv4 address
  467. *
  468. * \warning You should rarely need this function. Only use if you know what
  469. * you're doing.
  470. * \retval 1 This is an IPv4 address
  471. * \retval 0 This is an IPv6 or IPv4-mapped IPv6 address
  472. */
  473. int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr);
  474. /*!
  475. * \since 1.8
  476. *
  477. * \brief
  478. * Determine if this is an IPv4-mapped IPv6 address
  479. *
  480. * \warning You should rarely need this function. Only use if you know what
  481. * you're doing.
  482. *
  483. * \retval 1 This is an IPv4-mapped IPv6 address.
  484. * \retval 0 This is not an IPv4-mapped IPv6 address.
  485. */
  486. int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr);
  487. /*!
  488. * \since 10.0
  489. *
  490. * \brief
  491. * Determine if an IPv4 address is a multicast address
  492. *
  493. * \param addr the address to check
  494. *
  495. * This function checks if an address is in the 224.0.0.0/4 network block.
  496. *
  497. * \return non-zero if this is a multicast address
  498. */
  499. int ast_sockaddr_is_ipv4_multicast(const struct ast_sockaddr *addr);
  500. /*!
  501. * \since 1.8
  502. *
  503. * \brief
  504. * Determine if this is a link-local IPv6 address
  505. *
  506. * \warning You should rarely need this function. Only use if you know what
  507. * you're doing.
  508. *
  509. * \retval 1 This is a link-local IPv6 address.
  510. * \retval 0 This is link-local IPv6 address.
  511. */
  512. int ast_sockaddr_is_ipv6_link_local(const struct ast_sockaddr *addr);
  513. /*!
  514. * \since 1.8
  515. *
  516. * \brief
  517. * Determine if this is an IPv6 address
  518. *
  519. * \warning You should rarely need this function. Only use if you know what
  520. * you're doing.
  521. *
  522. * \retval 1 This is an IPv6 or IPv4-mapped IPv6 address.
  523. * \retval 0 This is an IPv4 address.
  524. */
  525. int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr);
  526. /*!
  527. * \since 1.8
  528. *
  529. * \brief
  530. * Determine if the address type is unspecified, or "any" address.
  531. *
  532. * \details
  533. * For IPv4, this would be the address 0.0.0.0, and for IPv6,
  534. * this would be the address ::. The port number is ignored.
  535. *
  536. * \retval 1 This is an "any" address
  537. * \retval 0 This is not an "any" address
  538. */
  539. int ast_sockaddr_is_any(const struct ast_sockaddr *addr);
  540. /*!
  541. * \since 1.8
  542. *
  543. * \brief
  544. * Computes a hash value from the address. The port is ignored.
  545. *
  546. * \retval 0 Unknown address family
  547. * \retval other A 32-bit hash derived from the address
  548. */
  549. int ast_sockaddr_hash(const struct ast_sockaddr *addr);
  550. /*!
  551. * \since 12.3
  552. *
  553. * \brief
  554. * Returns a string representation of an ast_transport
  555. *
  556. * \retval Name of the tranpsort if it is defined
  557. * \retval Undefined if the transport is undefined
  558. */
  559. const char *ast_transport2str(enum ast_transport transport);
  560. /*!
  561. * \since 1.8
  562. *
  563. * \brief
  564. * Wrapper around accept(2) that uses struct ast_sockaddr.
  565. *
  566. * \details
  567. * For parameter and return information, see the man page for
  568. * accept(2).
  569. */
  570. int ast_accept(int sockfd, struct ast_sockaddr *addr);
  571. /*!
  572. * \since 1.8
  573. *
  574. * \brief
  575. * Wrapper around bind(2) that uses struct ast_sockaddr.
  576. *
  577. * \details
  578. * For parameter and return information, see the man page for
  579. * bind(2).
  580. */
  581. int ast_bind(int sockfd, const struct ast_sockaddr *addr);
  582. /*!
  583. * \since 1.8
  584. *
  585. * \brief
  586. * Wrapper around connect(2) that uses struct ast_sockaddr.
  587. *
  588. * \details
  589. * For parameter and return information, see the man page for
  590. * connect(2).
  591. */
  592. int ast_connect(int sockfd, const struct ast_sockaddr *addr);
  593. /*!
  594. * \since 1.8
  595. *
  596. * \brief
  597. * Wrapper around getsockname(2) that uses struct ast_sockaddr.
  598. *
  599. * \details
  600. * For parameter and return information, see the man page for
  601. * getsockname(2).
  602. */
  603. int ast_getsockname(int sockfd, struct ast_sockaddr *addr);
  604. /*!
  605. * \since 1.8
  606. *
  607. * \brief
  608. * Wrapper around recvfrom(2) that uses struct ast_sockaddr.
  609. *
  610. * \details
  611. * For parameter and return information, see the man page for
  612. * recvfrom(2).
  613. */
  614. ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags,
  615. struct ast_sockaddr *src_addr);
  616. /*!
  617. * \since 1.8
  618. *
  619. * \brief
  620. * Wrapper around sendto(2) that uses ast_sockaddr.
  621. *
  622. * \details
  623. * For parameter and
  624. * return information, see the man page for sendto(2)
  625. */
  626. ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags,
  627. const struct ast_sockaddr *dest_addr);
  628. /*!
  629. * \since 1.8
  630. *
  631. * \brief
  632. * Set type of service
  633. *
  634. * \details
  635. * Set ToS ("Type of Service for IPv4 and "Traffic Class for IPv6) and
  636. * CoS (Linux's SO_PRIORITY)
  637. *
  638. * \param sockfd File descriptor for socket on which to set the parameters
  639. * \param tos The type of service for the socket
  640. * \param cos The cost of service for the socket
  641. * \param desc A text description of the socket in question.
  642. * \retval 0 Success
  643. * \retval -1 Error, with errno set to an appropriate value
  644. */
  645. int ast_set_qos(int sockfd, int tos, int cos, const char *desc);
  646. /*!
  647. * These are backward compatibility functions that may be used by subsystems
  648. * that have not yet been converted to IPv6. They will be removed when all
  649. * subsystems are IPv6-ready.
  650. */
  651. /*@{*/
  652. /*!
  653. * \since 1.8
  654. *
  655. * \brief
  656. * Converts a struct ast_sockaddr to a struct sockaddr_in.
  657. *
  658. * \param addr The ast_sockaddr to convert
  659. * \param[out] sin The resulting sockaddr_in struct
  660. * \retval nonzero Success
  661. * \retval zero Failure
  662. */
  663. #define ast_sockaddr_to_sin(addr,sin) _ast_sockaddr_to_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  664. int _ast_sockaddr_to_sin(const struct ast_sockaddr *addr,
  665. struct sockaddr_in *sin, const char *file, int line, const char *func);
  666. /*!
  667. * \since 1.8
  668. *
  669. * \brief Converts a struct sockaddr_in to a struct ast_sockaddr.
  670. *
  671. * \param addr
  672. * \param sin The sockaddr_in to convert
  673. * \return an ast_sockaddr structure
  674. */
  675. #define ast_sockaddr_from_sin(addr,sin) _ast_sockaddr_from_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  676. void _ast_sockaddr_from_sin(struct ast_sockaddr *addr, const struct sockaddr_in *sin,
  677. const char *file, int line, const char *func);
  678. /*@}*/
  679. #if defined(__cplusplus) || defined(c_plusplus)
  680. }
  681. #endif
  682. #endif /* _ASTERISK_NETSOCK2_H */