netsock.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/time.h>
  28. #include <signal.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <sys/socket.h>
  34. #include <netdb.h>
  35. #include <net/if.h>
  36. #include <netinet/in_systm.h>
  37. #include <netinet/ip.h>
  38. #include <sys/ioctl.h>
  39. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
  40. #include <fcntl.h>
  41. #include <net/route.h>
  42. #endif
  43. #if defined (SOLARIS)
  44. #include <sys/sockio.h>
  45. #endif
  46. #include "asterisk.h"
  47. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  48. #include "asterisk/netsock.h"
  49. #include "asterisk/logger.h"
  50. #include "asterisk/channel.h"
  51. #include "asterisk/options.h"
  52. #include "asterisk/utils.h"
  53. #include "asterisk/lock.h"
  54. #include "asterisk/srv.h"
  55. struct ast_netsock {
  56. ASTOBJ_COMPONENTS(struct ast_netsock);
  57. struct sockaddr_in bindaddr;
  58. int sockfd;
  59. int *ioref;
  60. struct io_context *ioc;
  61. void *data;
  62. };
  63. struct ast_netsock_list {
  64. ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock);
  65. struct io_context *ioc;
  66. };
  67. static void ast_netsock_destroy(struct ast_netsock *netsock)
  68. {
  69. ast_io_remove(netsock->ioc, netsock->ioref);
  70. close(netsock->sockfd);
  71. free(netsock);
  72. }
  73. struct ast_netsock_list *ast_netsock_list_alloc(void)
  74. {
  75. struct ast_netsock_list *res;
  76. res = calloc(1, sizeof(*res));
  77. return res;
  78. }
  79. int ast_netsock_init(struct ast_netsock_list *list)
  80. {
  81. memset(list, 0, sizeof(*list));
  82. ASTOBJ_CONTAINER_INIT(list);
  83. return 0;
  84. }
  85. int ast_netsock_release(struct ast_netsock_list *list)
  86. {
  87. ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
  88. ASTOBJ_CONTAINER_DESTROY(list);
  89. return 0;
  90. }
  91. struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list,
  92. struct sockaddr_in *sa)
  93. {
  94. struct ast_netsock *sock = NULL;
  95. ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
  96. ASTOBJ_RDLOCK(iterator);
  97. if (!inaddrcmp(&iterator->bindaddr, sa))
  98. sock = iterator;
  99. ASTOBJ_UNLOCK(iterator);
  100. });
  101. return sock;
  102. }
  103. struct ast_netsock *ast_netsock_bindaddr(struct ast_netsock_list *list, struct io_context *ioc, struct sockaddr_in *bindaddr, int tos, ast_io_cb callback, void *data)
  104. {
  105. int netsocket = -1;
  106. int *ioref;
  107. char iabuf[INET_ADDRSTRLEN];
  108. struct ast_netsock *ns;
  109. /* Make a UDP socket */
  110. netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  111. if (netsocket < 0) {
  112. ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
  113. return NULL;
  114. }
  115. if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) {
  116. ast_log(LOG_ERROR, "Unable to bind to %s port %d: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr->sin_addr), ntohs(bindaddr->sin_port), strerror(errno));
  117. close(netsocket);
  118. return NULL;
  119. }
  120. if (option_verbose > 1)
  121. ast_verbose(VERBOSE_PREFIX_2 "Using TOS bits %d\n", tos);
  122. if (setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))
  123. ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
  124. ns = malloc(sizeof(struct ast_netsock));
  125. if (ns) {
  126. /* Establish I/O callback for socket read */
  127. ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns);
  128. if (!ioref) {
  129. ast_log(LOG_WARNING, "Out of memory!\n");
  130. close(netsocket);
  131. free(ns);
  132. return NULL;
  133. }
  134. ASTOBJ_INIT(ns);
  135. ns->ioref = ioref;
  136. ns->ioc = ioc;
  137. ns->sockfd = netsocket;
  138. ns->data = data;
  139. memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
  140. ASTOBJ_CONTAINER_LINK(list, ns);
  141. } else {
  142. ast_log(LOG_WARNING, "Out of memory!\n");
  143. close(netsocket);
  144. }
  145. return ns;
  146. }
  147. struct ast_netsock *ast_netsock_bind(struct ast_netsock_list *list, struct io_context *ioc, const char *bindinfo, int defaultport, int tos, ast_io_cb callback, void *data)
  148. {
  149. struct sockaddr_in sin;
  150. char *tmp;
  151. char *host;
  152. char *port;
  153. int portno;
  154. memset(&sin, 0, sizeof(sin));
  155. sin.sin_family = AF_INET;
  156. sin.sin_port = htons(defaultport);
  157. tmp = ast_strdupa(bindinfo);
  158. if (!tmp) {
  159. ast_log(LOG_WARNING, "Out of memory!\n");
  160. return NULL;
  161. }
  162. host = strsep(&tmp, ":");
  163. port = tmp;
  164. if (port && ((portno = atoi(port)) > 0))
  165. sin.sin_port = htons(portno);
  166. inet_aton(host, &sin.sin_addr);
  167. return ast_netsock_bindaddr(list, ioc, &sin, tos, callback, data);
  168. }
  169. int ast_netsock_sockfd(const struct ast_netsock *ns)
  170. {
  171. return ns ? ns-> sockfd : -1;
  172. }
  173. const struct sockaddr_in *ast_netsock_boundaddr(const struct ast_netsock *ns)
  174. {
  175. return &(ns->bindaddr);
  176. }
  177. void *ast_netsock_data(const struct ast_netsock *ns)
  178. {
  179. return ns->data;
  180. }
  181. void ast_netsock_unref(struct ast_netsock *ns)
  182. {
  183. ASTOBJ_UNREF(ns, ast_netsock_destroy);
  184. }