2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Kevin P. Fleming <kpfleming@digium.com>
7 * Mark Spencer <markster@digium.com>
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.
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.
22 * \brief Network socket handling
24 * \author Kevin P. Fleming <kpfleming@digium.com>
25 * \author Mark Spencer <markster@digium.com>
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__) || defined(__GLIBC__)
34 #include <net/if_dl.h>
39 #include <sys/sockio.h>
40 #elif defined(HAVE_GETIFADDRS)
44 #include "asterisk/netsock.h"
45 #include "asterisk/utils.h"
46 #include "asterisk/astobj.h"
49 ASTOBJ_COMPONENTS(struct ast_netsock);
50 struct sockaddr_in bindaddr;
53 struct io_context *ioc;
57 struct ast_netsock_list {
58 ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock);
59 struct io_context *ioc;
62 static void ast_netsock_destroy(struct ast_netsock *netsock)
64 ast_io_remove(netsock->ioc, netsock->ioref);
65 close(netsock->sockfd);
69 struct ast_netsock_list *ast_netsock_list_alloc(void)
71 return ast_calloc(1, sizeof(struct ast_netsock_list));
74 int ast_netsock_init(struct ast_netsock_list *list)
76 memset(list, 0, sizeof(*list));
77 ASTOBJ_CONTAINER_INIT(list);
82 int ast_netsock_release(struct ast_netsock_list *list)
84 ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
85 ASTOBJ_CONTAINER_DESTROY(list);
91 struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list,
92 struct sockaddr_in *sa)
94 struct ast_netsock *sock = NULL;
96 ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
97 ASTOBJ_RDLOCK(iterator);
98 if (!inaddrcmp(&iterator->bindaddr, sa))
100 ASTOBJ_UNLOCK(iterator);
106 struct ast_netsock *ast_netsock_bindaddr(struct ast_netsock_list *list, struct io_context *ioc, struct sockaddr_in *bindaddr, int tos, int cos, ast_io_cb callback, void *data)
111 struct ast_netsock *ns;
112 const int reuseFlag = 1;
114 /* Make a UDP socket */
115 netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
118 ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
121 if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
122 ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
124 if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) {
125 ast_log(LOG_ERROR, "Unable to bind to %s port %d: %s\n", ast_inet_ntoa(bindaddr->sin_addr), ntohs(bindaddr->sin_port), strerror(errno));
130 ast_netsock_set_qos(netsocket, tos, cos, "IAX2");
132 ast_enable_packet_fragmentation(netsocket);
134 if (!(ns = ast_calloc(1, sizeof(*ns)))) {
139 /* Establish I/O callback for socket read */
140 if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
148 ns->sockfd = netsocket;
150 memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
151 ASTOBJ_CONTAINER_LINK(list, ns);
156 int ast_netsock_set_qos(int netsocket, int tos, int cos, const char *desc)
160 if ((res = setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
161 ast_log(LOG_WARNING, "Unable to set %s TOS to %d, may be you have no root privileges\n", desc, tos);
163 ast_verb(2, "Using %s TOS bits %d\n", desc, tos);
166 if (setsockopt(netsocket, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos)))
167 ast_log(LOG_WARNING, "Unable to set %s CoS to %d\n", desc, cos);
169 ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
176 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)
178 struct sockaddr_in sin;
184 memset(&sin, 0, sizeof(sin));
185 sin.sin_family = AF_INET;
186 sin.sin_port = htons(defaultport);
187 tmp = ast_strdupa(bindinfo);
189 host = strsep(&tmp, ":");
192 if (port && ((portno = atoi(port)) > 0))
193 sin.sin_port = htons(portno);
195 inet_aton(host, &sin.sin_addr);
197 return ast_netsock_bindaddr(list, ioc, &sin, tos, cos, callback, data);
200 int ast_netsock_sockfd(const struct ast_netsock *ns)
202 return ns ? ns-> sockfd : -1;
205 const struct sockaddr_in *ast_netsock_boundaddr(const struct ast_netsock *ns)
207 return &(ns->bindaddr);
210 void *ast_netsock_data(const struct ast_netsock *ns)
215 void ast_netsock_unref(struct ast_netsock *ns)
217 ASTOBJ_UNREF(ns, ast_netsock_destroy);
220 char *ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
225 if (s && (maxlen > 0))
228 for (x = 0; x < 5; x++) {
229 sprintf(s, "%02x:", eid->eid[x]);
232 sprintf(s, "%02x", eid->eid[5]);
237 void ast_set_default_eid(struct ast_eid *eid)
239 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_IFRU_IFRU_HWADDR)
244 s = socket(AF_INET, SOCK_STREAM, 0);
247 for (x = 0; x < 10; x++) {
248 memset(&ifr, 0, sizeof(ifr));
249 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth%d", x);
250 if (ioctl(s, SIOCGIFHWADDR, &ifr))
252 memcpy(eid, ((unsigned char *)&ifr.ifr_hwaddr) + 2, sizeof(*eid));
253 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);
259 #if defined(ifa_broadaddr) && !defined(SOLARIS)
261 struct ifaddrs *ifap;
263 if (getifaddrs(&ifap) == 0) {
265 for (p = ifap; p; p = p->ifa_next) {
266 if ((p->ifa_addr->sa_family == AF_LINK) && !(p->ifa_flags & IFF_LOOPBACK) && (p->ifa_flags & IFF_RUNNING)) {
267 struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr;
268 memcpy(&(eid->eid), sdp->sdl_data + sdp->sdl_nlen, 6);
269 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);
278 ast_debug(1, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
281 int ast_str_to_eid(struct ast_eid *eid, const char *s)
283 unsigned int eid_int[6];
286 if (sscanf(s, "%2x:%2x:%2x:%2x:%2x:%2x", &eid_int[0], &eid_int[1], &eid_int[2],
287 &eid_int[3], &eid_int[4], &eid_int[5]) != 6)
290 for (x = 0; x < 6; x++)
291 eid->eid[x] = eid_int[x];
296 int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
298 return memcmp(eid1, eid2, sizeof(*eid1));