Debian Codegraph @

channels/iax2/netsock.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Kevin P. Fleming <[email protected]>
7 * Mark Spencer <[email protected]>
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
20 /*! \file
21 *
22 * \brief Network socket handling
23 *
24 * \author Kevin P. Fleming <[email protected]>
25 * \author Mark Spencer <[email protected]>
26 */
27
28 /*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32 #include "asterisk.h"
33
34 #if !defined (__linux__) && !defined (__GNU__)
35 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__) || defined(__GLIBC__)
36 #include <net/if_dl.h>
37 #endif
38 #endif
39
40 #if defined (SOLARIS)
41 #include <sys/sockio.h>
42 #elif defined(HAVE_GETIFADDRS)
43 #include <ifaddrs.h>
44 #endif
45
46 #include "include/netsock.h"
47 #include "asterisk/netsock2.h"
48 #include "asterisk/utils.h"
49 #include "include/astobj.h"
50
51 struct ast_netsock {
52 ASTOBJ_COMPONENTS(struct ast_netsock);
53 struct ast_sockaddr bindaddr;
54 int sockfd;
55 int *ioref;
56 struct io_context *ioc;
57 void *data;
58 };
59
60 struct ast_netsock_list {
61 ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock);
62 struct io_context *ioc;
63 };
64
65 static void ast_netsock_destroy(struct ast_netsock *netsock)
66 {
67 ast_io_remove(netsock->ioc, netsock->ioref);
68 close(netsock->sockfd);
69 ast_free(netsock);
70 }
71
72 struct ast_netsock_list *ast_netsock_list_alloc(void)
73 {
74 return ast_calloc(1, sizeof(struct ast_netsock_list));
75 }
76
77 int ast_netsock_init(struct ast_netsock_list *list)
78 {
79 memset(list, 0, sizeof(*list));
80 ASTOBJ_CONTAINER_INIT(list);
81
82 return 0;
83 }
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 ast_free(list);
90
91 return 0;
92 }
93
94 struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list, struct ast_sockaddr *addr)
95 {
96 struct ast_netsock *sock = NULL;
97
98 ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
99 ASTOBJ_RDLOCK(iterator);
100 if (!ast_sockaddr_cmp(&iterator->bindaddr, addr)) {
101 sock = iterator;
102 }
103 ASTOBJ_UNLOCK(iterator);
104 });
105
106 return sock;
107 }
108
109 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)
110 {
111 int netsocket = -1;
112 int *ioref;
113
114 struct ast_netsock *ns;
115 const int reuseFlag = 1;
116
117 /* Make a UDP socket */
118 netsocket = socket(ast_sockaddr_is_ipv6(bindaddr) ? AST_AF_INET6 : AST_AF_INET, SOCK_DGRAM, IPPROTO_IP);
119
120 if (netsocket < 0) {
121 ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
122 return NULL;
123 }
124 if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
125 ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
126 }
127 if (ast_bind(netsocket, bindaddr)) {
128 ast_log(LOG_ERROR,
129 "Unable to bind to %s: %s\n",
130 ast_sockaddr_stringify(bindaddr),
131 strerror(errno));
132 close(netsocket);
133 return NULL;
134 }
135
136 ast_set_qos(netsocket, tos, cos, "IAX2");
137
138 ast_enable_packet_fragmentation(netsocket);
139
140 if (!(ns = ast_calloc(1, sizeof(*ns)))) {
141 close(netsocket);
142 return NULL;
143 }
144
145 /* Establish I/O callback for socket read */
146 if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
147 close(netsocket);
148 ast_free(ns);
149 return NULL;
150 }
151 ASTOBJ_INIT(ns);
152 ns->ioref = ioref;
153 ns->ioc = ioc;
154 ns->sockfd = netsocket;
155 ns->data = data;
156 ast_sockaddr_copy(&ns->bindaddr, bindaddr);
157 ASTOBJ_CONTAINER_LINK(list, ns);
158
159 return ns;
160 }
161
162 int ast_netsock_set_qos(int sockfd, int tos, int cos, const char *desc)
163 {
164 return ast_set_qos(sockfd, tos, cos, desc);
165 }
166
167 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)
168 {
169 struct ast_sockaddr addr;
170
171 if (ast_sockaddr_parse(&addr, bindinfo, 0)) {
172
173 if (!ast_sockaddr_port(&addr)) {
174 ast_sockaddr_set_port(&addr, defaultport);
175 }
176
177 return ast_netsock_bindaddr(list, ioc, &addr, tos, cos, callback, data);
178 }
179
180 return NULL;
181 }
182
183 int ast_netsock_sockfd(const struct ast_netsock *ns)
184 {
185 return ns ? ns-> sockfd : -1;
186 }
187
188 const struct ast_sockaddr *ast_netsock_boundaddr(const struct ast_netsock *ns)
189 {
190 return &ns->bindaddr;
191 }
192
193 void *ast_netsock_data(const struct ast_netsock *ns)
194 {
195 return ns->data;
196 }
197
198 void ast_netsock_unref(struct ast_netsock *ns)
199 {
200 ASTOBJ_UNREF(ns, ast_netsock_destroy);
201 }