Thu Apr 28 2011 16:56:47

Asterisk developer's documentation


netsock.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Kevin P. Fleming <kpfleming@digium.com>
00007  * Mark Spencer <markster@digium.com>
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief Network socket handling
00023  *
00024  * \author Kevin P. Fleming <kpfleming@digium.com>
00025  * \author Mark Spencer <markster@digium.com>
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 298050 $")
00031 
00032 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__)
00033 #include <net/if_dl.h>
00034 #endif
00035 
00036 #if defined (SOLARIS)
00037 #include <sys/sockio.h>
00038 #elif defined(HAVE_GETIFADDRS)
00039 #include <ifaddrs.h>
00040 #endif
00041 
00042 #include "asterisk/netsock.h"
00043 #include "asterisk/utils.h"
00044 #include "asterisk/astobj.h"
00045 
00046 struct ast_netsock {
00047    ASTOBJ_COMPONENTS(struct ast_netsock);
00048    struct sockaddr_in bindaddr;
00049    int sockfd;
00050    int *ioref;
00051    struct io_context *ioc;
00052    void *data;
00053 };
00054 
00055 struct ast_netsock_list {
00056    ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock);
00057    struct io_context *ioc;
00058 };
00059 
00060 static void ast_netsock_destroy(struct ast_netsock *netsock)
00061 {
00062    ast_io_remove(netsock->ioc, netsock->ioref);
00063    close(netsock->sockfd);
00064    ast_free(netsock);
00065 }
00066 
00067 struct ast_netsock_list *ast_netsock_list_alloc(void)
00068 {
00069    return ast_calloc(1, sizeof(struct ast_netsock_list));
00070 }
00071 
00072 int ast_netsock_init(struct ast_netsock_list *list)
00073 {
00074    memset(list, 0, sizeof(*list));
00075    ASTOBJ_CONTAINER_INIT(list);
00076 
00077    return 0;
00078 }
00079 
00080 int ast_netsock_release(struct ast_netsock_list *list)
00081 {
00082    ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
00083    ASTOBJ_CONTAINER_DESTROY(list);
00084    ast_free(list);
00085 
00086    return 0;
00087 }
00088 
00089 struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list,
00090                  struct sockaddr_in *sa)
00091 {
00092    struct ast_netsock *sock = NULL;
00093 
00094    ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
00095       ASTOBJ_RDLOCK(iterator);
00096       if (!inaddrcmp(&iterator->bindaddr, sa))
00097          sock = iterator;
00098       ASTOBJ_UNLOCK(iterator);
00099    });
00100 
00101    return sock;
00102 }
00103 
00104 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)
00105 {
00106    int netsocket = -1;
00107    int *ioref;
00108    
00109    struct ast_netsock *ns;
00110    const int reuseFlag = 1;
00111    
00112    /* Make a UDP socket */
00113    netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
00114    
00115    if (netsocket < 0) {
00116       ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
00117       return NULL;
00118    }
00119    if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
00120          ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
00121    }
00122    if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) {
00123       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));
00124       close(netsocket);
00125       return NULL;
00126    }
00127 
00128    ast_netsock_set_qos(netsocket, tos, cos, "IAX2");
00129       
00130    ast_enable_packet_fragmentation(netsocket);
00131 
00132    if (!(ns = ast_calloc(1, sizeof(*ns)))) {
00133       close(netsocket);
00134       return NULL;
00135    }
00136    
00137    /* Establish I/O callback for socket read */
00138    if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
00139       close(netsocket);
00140       ast_free(ns);
00141       return NULL;
00142    }  
00143    ASTOBJ_INIT(ns);
00144    ns->ioref = ioref;
00145    ns->ioc = ioc;
00146    ns->sockfd = netsocket;
00147    ns->data = data;
00148    memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
00149    ASTOBJ_CONTAINER_LINK(list, ns);
00150 
00151    return ns;
00152 }
00153 
00154 int ast_netsock_set_qos(int netsocket, int tos, int cos, const char *desc)
00155 {
00156    int res;
00157    
00158    if ((res = setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
00159       ast_log(LOG_WARNING, "Unable to set %s TOS to %d, may be you have no root privileges\n", desc, tos);
00160    else if (tos)
00161       ast_verb(2, "Using %s TOS bits %d\n", desc, tos);
00162 
00163 #if defined(linux)                        
00164    if (setsockopt(netsocket, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos)))
00165       ast_log(LOG_WARNING, "Unable to set %s CoS to %d\n", desc, cos);
00166    else if (cos)
00167       ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
00168 #endif
00169                      
00170    return res;
00171 }
00172                                        
00173 
00174 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)
00175 {
00176    struct sockaddr_in sin;
00177    char *tmp;
00178    char *host;
00179    char *port;
00180    int portno;
00181 
00182    memset(&sin, 0, sizeof(sin));
00183    sin.sin_family = AF_INET;
00184    sin.sin_port = htons(defaultport);
00185    tmp = ast_strdupa(bindinfo);
00186 
00187    host = strsep(&tmp, ":");
00188    port = tmp;
00189 
00190    if (port && ((portno = atoi(port)) > 0))
00191       sin.sin_port = htons(portno);
00192 
00193    inet_aton(host, &sin.sin_addr);
00194 
00195    return ast_netsock_bindaddr(list, ioc, &sin, tos, cos, callback, data);
00196 }
00197 
00198 int ast_netsock_sockfd(const struct ast_netsock *ns)
00199 {
00200    return ns ? ns-> sockfd : -1;
00201 }
00202 
00203 const struct sockaddr_in *ast_netsock_boundaddr(const struct ast_netsock *ns)
00204 {
00205    return &(ns->bindaddr);
00206 }
00207 
00208 void *ast_netsock_data(const struct ast_netsock *ns)
00209 {
00210    return ns->data;
00211 }
00212 
00213 void ast_netsock_unref(struct ast_netsock *ns)
00214 {
00215    ASTOBJ_UNREF(ns, ast_netsock_destroy);
00216 }
00217 
00218 char *ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
00219 {
00220    int x;
00221    char *os = s;
00222    if (maxlen < 18) {
00223       if (s && (maxlen > 0))
00224          *s = '\0';
00225    } else {
00226       for (x = 0; x < 5; x++) {
00227          sprintf(s, "%02x:", eid->eid[x]);
00228          s += 3;
00229       }
00230       sprintf(s, "%02x", eid->eid[5]);
00231    }
00232    return os;
00233 }
00234 
00235 void ast_set_default_eid(struct ast_eid *eid)
00236 {
00237 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_IFRU_IFRU_HWADDR)
00238    int s, x = 0;
00239    char eid_str[20];
00240    struct ifreq ifr;
00241 
00242    s = socket(AF_INET, SOCK_STREAM, 0);
00243    if (s < 0)
00244       return;
00245    for (x = 0; x < 10; x++) {
00246       memset(&ifr, 0, sizeof(ifr));
00247       snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth%d", x);
00248       if (ioctl(s, SIOCGIFHWADDR, &ifr))
00249          continue;
00250       memcpy(eid, ((unsigned char *)&ifr.ifr_hwaddr) + 2, sizeof(*eid));
00251       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);
00252       close(s);
00253       return;
00254    }
00255    close(s);
00256 #else
00257 #if defined(ifa_broadaddr) && !defined(SOLARIS)
00258    char eid_str[20];
00259    struct ifaddrs *ifap;
00260    
00261    if (getifaddrs(&ifap) == 0) {
00262       struct ifaddrs *p;
00263       for (p = ifap; p; p = p->ifa_next) {
00264          if ((p->ifa_addr->sa_family == AF_LINK) && !(p->ifa_flags & IFF_LOOPBACK) && (p->ifa_flags & IFF_RUNNING)) {
00265             struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr;
00266             memcpy(&(eid->eid), sdp->sdl_data + sdp->sdl_nlen, 6);
00267             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);
00268             freeifaddrs(ifap);
00269             return;
00270          }
00271       }
00272       freeifaddrs(ifap);
00273    }
00274 #endif
00275 #endif
00276    ast_debug(1, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
00277 }
00278 
00279 int ast_str_to_eid(struct ast_eid *eid, const char *s)
00280 {
00281    unsigned int eid_int[6];
00282    int x;
00283 
00284    if (sscanf(s, "%2x:%2x:%2x:%2x:%2x:%2x", &eid_int[0], &eid_int[1], &eid_int[2],
00285        &eid_int[3], &eid_int[4], &eid_int[5]) != 6)
00286          return -1;
00287    
00288    for (x = 0; x < 6; x++)
00289       eid->eid[x] = eid_int[x];
00290 
00291    return 0;
00292 }
00293 
00294 int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
00295 {
00296    return memcmp(eid1, eid2, sizeof(*eid1));
00297 }