netsock.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Kevin P. Fleming <kpfleming@digium.com>
  7. * Mark Spencer <markster@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Network socket handling
  22. *
  23. * \author Kevin P. Fleming <kpfleming@digium.com>
  24. * \author Mark Spencer <markster@digium.com>
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #if !defined (__linux__) && !defined (__GNU__)
  32. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__) || defined(__GLIBC__)
  33. #include <net/if_dl.h>
  34. #endif
  35. #endif
  36. #if defined (SOLARIS)
  37. #include <sys/sockio.h>
  38. #elif defined(HAVE_GETIFADDRS)
  39. #include <ifaddrs.h>
  40. #endif
  41. #include "asterisk/netsock.h"
  42. #include "asterisk/netsock2.h"
  43. #include "asterisk/utils.h"
  44. #include "asterisk/astobj.h"
  45. struct ast_netsock {
  46. ASTOBJ_COMPONENTS(struct ast_netsock);
  47. struct ast_sockaddr bindaddr;
  48. int sockfd;
  49. int *ioref;
  50. struct io_context *ioc;
  51. void *data;
  52. };
  53. struct ast_netsock_list {
  54. ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock);
  55. struct io_context *ioc;
  56. };
  57. static void ast_netsock_destroy(struct ast_netsock *netsock)
  58. {
  59. ast_io_remove(netsock->ioc, netsock->ioref);
  60. close(netsock->sockfd);
  61. ast_free(netsock);
  62. }
  63. struct ast_netsock_list *ast_netsock_list_alloc(void)
  64. {
  65. return ast_calloc(1, sizeof(struct ast_netsock_list));
  66. }
  67. int ast_netsock_init(struct ast_netsock_list *list)
  68. {
  69. memset(list, 0, sizeof(*list));
  70. ASTOBJ_CONTAINER_INIT(list);
  71. return 0;
  72. }
  73. int ast_netsock_release(struct ast_netsock_list *list)
  74. {
  75. ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
  76. ASTOBJ_CONTAINER_DESTROY(list);
  77. ast_free(list);
  78. return 0;
  79. }
  80. struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list, struct ast_sockaddr *addr)
  81. {
  82. struct ast_netsock *sock = NULL;
  83. ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
  84. ASTOBJ_RDLOCK(iterator);
  85. if (!ast_sockaddr_cmp(&iterator->bindaddr, addr)) {
  86. sock = iterator;
  87. }
  88. ASTOBJ_UNLOCK(iterator);
  89. });
  90. return sock;
  91. }
  92. struct ast_netsock *ast_netsock_bindaddr(struct ast_netsock_list *list, struct io_context *ioc, struct ast_sockaddr *bindaddr, int tos, int cos, ast_io_cb callback, void *data)
  93. {
  94. int netsocket = -1;
  95. int *ioref;
  96. struct ast_netsock *ns;
  97. const int reuseFlag = 1;
  98. /* Make a UDP socket */
  99. netsocket = socket(ast_sockaddr_is_ipv6(bindaddr) ? AST_AF_INET6 : AST_AF_INET, SOCK_DGRAM, IPPROTO_IP);
  100. if (netsocket < 0) {
  101. ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
  102. return NULL;
  103. }
  104. if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
  105. ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
  106. }
  107. if (ast_bind(netsocket, bindaddr)) {
  108. ast_log(LOG_ERROR,
  109. "Unable to bind to %s: %s\n",
  110. ast_sockaddr_stringify(bindaddr),
  111. strerror(errno));
  112. close(netsocket);
  113. return NULL;
  114. }
  115. ast_set_qos(netsocket, tos, cos, "IAX2");
  116. ast_enable_packet_fragmentation(netsocket);
  117. if (!(ns = ast_calloc(1, sizeof(*ns)))) {
  118. close(netsocket);
  119. return NULL;
  120. }
  121. /* Establish I/O callback for socket read */
  122. if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
  123. close(netsocket);
  124. ast_free(ns);
  125. return NULL;
  126. }
  127. ASTOBJ_INIT(ns);
  128. ns->ioref = ioref;
  129. ns->ioc = ioc;
  130. ns->sockfd = netsocket;
  131. ns->data = data;
  132. ast_sockaddr_copy(&ns->bindaddr, bindaddr);
  133. ASTOBJ_CONTAINER_LINK(list, ns);
  134. return ns;
  135. }
  136. int ast_netsock_set_qos(int sockfd, int tos, int cos, const char *desc)
  137. {
  138. return ast_set_qos(sockfd, tos, cos, desc);
  139. }
  140. struct ast_netsock *ast_netsock_bind(struct ast_netsock_list *list, struct io_context *ioc, const char *bindinfo, int defaultport, int tos, int cos, ast_io_cb callback, void *data)
  141. {
  142. struct ast_sockaddr addr;
  143. if (ast_sockaddr_parse(&addr, bindinfo, 0)) {
  144. if (!ast_sockaddr_port(&addr)) {
  145. ast_sockaddr_set_port(&addr, defaultport);
  146. }
  147. return ast_netsock_bindaddr(list, ioc, &addr, tos, cos, callback, data);
  148. }
  149. return NULL;
  150. }
  151. int ast_netsock_sockfd(const struct ast_netsock *ns)
  152. {
  153. return ns ? ns-> sockfd : -1;
  154. }
  155. const struct ast_sockaddr *ast_netsock_boundaddr(const struct ast_netsock *ns)
  156. {
  157. return &ns->bindaddr;
  158. }
  159. void *ast_netsock_data(const struct ast_netsock *ns)
  160. {
  161. return ns->data;
  162. }
  163. void ast_netsock_unref(struct ast_netsock *ns)
  164. {
  165. ASTOBJ_UNREF(ns, ast_netsock_destroy);
  166. }