netsock.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #ifndef __linux__
  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(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. memcpy(&ns->bindaddr, bindaddr, sizeof(ns->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_is_ipv4(&addr)) {
  145. ast_log(LOG_WARNING, "Only IPv4 addresses are supported at this time.\n");
  146. return NULL;
  147. }
  148. if (!ast_sockaddr_port(&addr)) {
  149. ast_sockaddr_set_port(&addr, defaultport);
  150. }
  151. return ast_netsock_bindaddr(list, ioc, &addr, tos, cos, callback, data);
  152. }
  153. return NULL;
  154. }
  155. int ast_netsock_sockfd(const struct ast_netsock *ns)
  156. {
  157. return ns ? ns-> sockfd : -1;
  158. }
  159. const struct ast_sockaddr *ast_netsock_boundaddr(const struct ast_netsock *ns)
  160. {
  161. return &ns->bindaddr;
  162. }
  163. void *ast_netsock_data(const struct ast_netsock *ns)
  164. {
  165. return ns->data;
  166. }
  167. void ast_netsock_unref(struct ast_netsock *ns)
  168. {
  169. ASTOBJ_UNREF(ns, ast_netsock_destroy);
  170. }
  171. char *ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
  172. {
  173. int x;
  174. char *os = s;
  175. if (maxlen < 18) {
  176. if (s && (maxlen > 0))
  177. *s = '\0';
  178. } else {
  179. for (x = 0; x < 5; x++) {
  180. sprintf(s, "%02hhx:", eid->eid[x]);
  181. s += 3;
  182. }
  183. sprintf(s, "%02hhx", eid->eid[5]);
  184. }
  185. return os;
  186. }
  187. void ast_set_default_eid(struct ast_eid *eid)
  188. {
  189. #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_IFRU_IFRU_HWADDR)
  190. int s, x = 0;
  191. char eid_str[20];
  192. struct ifreq ifr;
  193. static const unsigned int MAXIF = 10;
  194. s = socket(AF_INET, SOCK_STREAM, 0);
  195. if (s < 0)
  196. return;
  197. for (x = 0; x < MAXIF; x++) {
  198. static const char *prefixes[] = { "eth", "em" };
  199. unsigned int i;
  200. for (i = 0; i < ARRAY_LEN(prefixes); i++) {
  201. memset(&ifr, 0, sizeof(ifr));
  202. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", prefixes[i], x);
  203. if (!ioctl(s, SIOCGIFHWADDR, &ifr)) {
  204. break;
  205. }
  206. }
  207. if (i == ARRAY_LEN(prefixes)) {
  208. /* Try pciX#[1..N] */
  209. for (i = 0; i < MAXIF; i++) {
  210. memset(&ifr, 0, sizeof(ifr));
  211. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "pci%d#%u", x, i);
  212. if (!ioctl(s, SIOCGIFHWADDR, &ifr)) {
  213. break;
  214. }
  215. }
  216. if (i == MAXIF) {
  217. continue;
  218. }
  219. }
  220. memcpy(eid, ((unsigned char *)&ifr.ifr_hwaddr) + 2, sizeof(*eid));
  221. ast_debug(1, "Seeding global EID '%s' from '%s' using 'siocgifhwaddr'\n", ast_eid_to_str(eid_str, sizeof(eid_str), eid), ifr.ifr_name);
  222. close(s);
  223. return;
  224. }
  225. close(s);
  226. #else
  227. #if defined(ifa_broadaddr) && !defined(SOLARIS)
  228. char eid_str[20];
  229. struct ifaddrs *ifap;
  230. if (getifaddrs(&ifap) == 0) {
  231. struct ifaddrs *p;
  232. for (p = ifap; p; p = p->ifa_next) {
  233. if ((p->ifa_addr->sa_family == AF_LINK) && !(p->ifa_flags & IFF_LOOPBACK) && (p->ifa_flags & IFF_RUNNING)) {
  234. struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr;
  235. memcpy(&(eid->eid), sdp->sdl_data + sdp->sdl_nlen, 6);
  236. ast_debug(1, "Seeding global EID '%s' from '%s' using 'getifaddrs'\n", ast_eid_to_str(eid_str, sizeof(eid_str), eid), p->ifa_name);
  237. freeifaddrs(ifap);
  238. return;
  239. }
  240. }
  241. freeifaddrs(ifap);
  242. }
  243. #endif
  244. #endif
  245. ast_debug(1, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
  246. }
  247. int ast_str_to_eid(struct ast_eid *eid, const char *s)
  248. {
  249. unsigned int eid_int[6];
  250. int x;
  251. if (sscanf(s, "%2x:%2x:%2x:%2x:%2x:%2x", &eid_int[0], &eid_int[1], &eid_int[2],
  252. &eid_int[3], &eid_int[4], &eid_int[5]) != 6)
  253. return -1;
  254. for (x = 0; x < 6; x++)
  255. eid->eid[x] = eid_int[x];
  256. return 0;
  257. }
  258. int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
  259. {
  260. return memcmp(eid1, eid2, sizeof(*eid1));
  261. }