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 #include <sys/sockio.h>
36 #include "asterisk/netsock.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/astobj.h"
41 ASTOBJ_COMPONENTS(struct ast_netsock);
42 struct sockaddr_in bindaddr;
45 struct io_context *ioc;
49 struct ast_netsock_list {
50 ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock);
51 struct io_context *ioc;
54 static void ast_netsock_destroy(struct ast_netsock *netsock)
56 ast_io_remove(netsock->ioc, netsock->ioref);
57 close(netsock->sockfd);
61 struct ast_netsock_list *ast_netsock_list_alloc(void)
63 return ast_calloc(1, sizeof(struct ast_netsock_list));
66 int ast_netsock_init(struct ast_netsock_list *list)
68 memset(list, 0, sizeof(*list));
69 ASTOBJ_CONTAINER_INIT(list);
74 int ast_netsock_release(struct ast_netsock_list *list)
76 ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
77 ASTOBJ_CONTAINER_DESTROY(list);
82 struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list,
83 struct sockaddr_in *sa)
85 struct ast_netsock *sock = NULL;
87 ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
88 ASTOBJ_RDLOCK(iterator);
89 if (!inaddrcmp(&iterator->bindaddr, sa))
91 ASTOBJ_UNLOCK(iterator);
97 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)
102 struct ast_netsock *ns;
103 const int reuseFlag = 1;
105 /* Make a UDP socket */
106 netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
109 ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
112 if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
113 ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
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(bindaddr->sin_addr), ntohs(bindaddr->sin_port), strerror(errno));
121 ast_netsock_set_qos(netsocket, tos, cos, "IAX2");
123 ast_enable_packet_fragmentation(netsocket);
125 if (!(ns = ast_calloc(1, sizeof(*ns)))) {
130 /* Establish I/O callback for socket read */
131 if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
139 ns->sockfd = netsocket;
141 memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
142 ASTOBJ_CONTAINER_LINK(list, ns);
147 int ast_netsock_set_qos(int netsocket, int tos, int cos, const char *desc)
151 if ((res = setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
152 ast_log(LOG_WARNING, "Unable to set %s TOS to %d, may be you have no root privileges\n", desc, tos);
154 ast_verb(2, "Using %s TOS bits %d\n", desc, tos);
157 if (setsockopt(netsocket, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos)))
158 ast_log(LOG_WARNING, "Unable to set %s CoS to %d\n", desc, cos);
160 ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
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)
169 struct sockaddr_in sin;
175 memset(&sin, 0, sizeof(sin));
176 sin.sin_family = AF_INET;
177 sin.sin_port = htons(defaultport);
178 tmp = ast_strdupa(bindinfo);
180 host = strsep(&tmp, ":");
183 if (port && ((portno = atoi(port)) > 0))
184 sin.sin_port = htons(portno);
186 inet_aton(host, &sin.sin_addr);
188 return ast_netsock_bindaddr(list, ioc, &sin, tos, cos, callback, data);
191 int ast_netsock_sockfd(const struct ast_netsock *ns)
193 return ns ? ns-> sockfd : -1;
196 const struct sockaddr_in *ast_netsock_boundaddr(const struct ast_netsock *ns)
198 return &(ns->bindaddr);
201 void *ast_netsock_data(const struct ast_netsock *ns)
206 void ast_netsock_unref(struct ast_netsock *ns)
208 ASTOBJ_UNREF(ns, ast_netsock_destroy);
211 char *ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
216 if (s && (maxlen > 0))
219 for (x = 0; x < 5; x++) {
220 sprintf(s, "%02x:", eid->eid[x]);
223 sprintf(s, "%02x", eid->eid[5]);
228 void ast_set_default_eid(struct ast_eid *eid)
230 #if defined(SIOCGIFHWADDR)
235 s = socket(AF_INET, SOCK_STREAM, 0);
238 for (x = 0; x < 10; x++) {
239 memset(&ifr, 0, sizeof(ifr));
240 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth%d", x);
241 if (ioctl(s, SIOCGIFHWADDR, &ifr))
243 memcpy(eid, ((unsigned char *)&ifr.ifr_hwaddr) + 2, sizeof(*eid));
244 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);
250 #if defined(ifa_broadaddr) && !defined(SOLARIS)
252 struct ifaddrs *ifap;
254 if (getifaddrs(&ifap) == 0) {
256 for (p = ifap; p; p = p->ifa_next) {
257 if ((p->ifa_addr->sa_family == AF_LINK) && !(p->ifa_flags & IFF_LOOPBACK) && (p->ifa_flags & IFF_RUNNING)) {
258 struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr;
259 memcpy(&(eid->eid), sdp->sdl_data + sdp->sdl_nlen, 6);
260 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);
269 ast_debug(1, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
272 int ast_str_to_eid(struct ast_eid *eid, const char *s)
274 unsigned int eid_int[6];
277 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &eid_int[0], &eid_int[1], &eid_int[2],
278 &eid_int[3], &eid_int[4], &eid_int[5]) != 6)
281 for (x = 0; x < 6; x++)
282 eid->eid[x] = eid_int[x];
287 int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
289 return memcmp(eid1, eid2, sizeof(*eid1));