Sun May 20 06:33:55 2012

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: 360190 $")
00031 
00032 #ifndef __linux__
00033 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__) || defined(__GLIBC__)
00034 #include <net/if_dl.h>
00035 #endif
00036 #endif
00037 
00038 #if defined (SOLARIS)
00039 #include <sys/sockio.h>
00040 #elif defined(HAVE_GETIFADDRS)
00041 #include <ifaddrs.h>
00042 #endif
00043 
00044 #include "asterisk/netsock.h"
00045 #include "asterisk/netsock2.h"
00046 #include "asterisk/utils.h"
00047 #include "asterisk/astobj.h"
00048 
00049 struct ast_netsock {
00050    ASTOBJ_COMPONENTS(struct ast_netsock);
00051    struct ast_sockaddr bindaddr;
00052    int sockfd;
00053    int *ioref;
00054    struct io_context *ioc;
00055    void *data;
00056 };
00057 
00058 struct ast_netsock_list {
00059    ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock);
00060    struct io_context *ioc;
00061 };
00062 
00063 static void ast_netsock_destroy(struct ast_netsock *netsock)
00064 {
00065    ast_io_remove(netsock->ioc, netsock->ioref);
00066    close(netsock->sockfd);
00067    ast_free(netsock);
00068 }
00069 
00070 struct ast_netsock_list *ast_netsock_list_alloc(void)
00071 {
00072    return ast_calloc(1, sizeof(struct ast_netsock_list));
00073 }
00074 
00075 int ast_netsock_init(struct ast_netsock_list *list)
00076 {
00077    memset(list, 0, sizeof(*list));
00078    ASTOBJ_CONTAINER_INIT(list);
00079 
00080    return 0;
00081 }
00082 
00083 int ast_netsock_release(struct ast_netsock_list *list)
00084 {
00085    ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
00086    ASTOBJ_CONTAINER_DESTROY(list);
00087    ast_free(list);
00088 
00089    return 0;
00090 }
00091 
00092 struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list, struct ast_sockaddr *addr)
00093 {
00094    struct ast_netsock *sock = NULL;
00095 
00096    ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
00097       ASTOBJ_RDLOCK(iterator);
00098       if (!ast_sockaddr_cmp(&iterator->bindaddr, addr)) {
00099          sock = iterator;
00100       }
00101       ASTOBJ_UNLOCK(iterator);
00102    });
00103 
00104    return sock;
00105 }
00106 
00107 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)
00108 {
00109    int netsocket = -1;
00110    int *ioref;
00111 
00112    struct ast_netsock *ns;
00113    const int reuseFlag = 1;
00114 
00115    /* Make a UDP socket */
00116    netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
00117 
00118    if (netsocket < 0) {
00119       ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
00120       return NULL;
00121    }
00122    if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
00123       ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
00124    }
00125    if (ast_bind(netsocket, bindaddr)) {
00126       ast_log(LOG_ERROR,
00127          "Unable to bind to %s: %s\n",
00128          ast_sockaddr_stringify(bindaddr),
00129          strerror(errno));
00130       close(netsocket);
00131       return NULL;
00132    }
00133 
00134    ast_set_qos(netsocket, tos, cos, "IAX2");
00135 
00136    ast_enable_packet_fragmentation(netsocket);
00137 
00138    if (!(ns = ast_calloc(1, sizeof(*ns)))) {
00139       close(netsocket);
00140       return NULL;
00141    }
00142 
00143    /* Establish I/O callback for socket read */
00144    if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
00145       close(netsocket);
00146       ast_free(ns);
00147       return NULL;
00148    }
00149    ASTOBJ_INIT(ns);
00150    ns->ioref = ioref;
00151    ns->ioc = ioc;
00152    ns->sockfd = netsocket;
00153    ns->data = data;
00154    memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
00155    ASTOBJ_CONTAINER_LINK(list, ns);
00156 
00157    return ns;
00158 }
00159 
00160 int ast_netsock_set_qos(int sockfd, int tos, int cos, const char *desc)
00161 {
00162    return ast_set_qos(sockfd, tos, cos, desc);
00163 }
00164 
00165 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)
00166 {
00167    struct ast_sockaddr addr;
00168 
00169    if (ast_sockaddr_parse(&addr, bindinfo, 0)) {
00170       if (!ast_sockaddr_is_ipv4(&addr)) {
00171          ast_log(LOG_WARNING, "Only IPv4 addresses are supported at this time.\n");
00172          return NULL;
00173       }
00174 
00175       if (!ast_sockaddr_port(&addr)) {
00176          ast_sockaddr_set_port(&addr, defaultport);
00177       }
00178 
00179       return ast_netsock_bindaddr(list, ioc, &addr, tos, cos, callback, data);
00180    }
00181 
00182    return NULL;
00183 }
00184 
00185 int ast_netsock_sockfd(const struct ast_netsock *ns)
00186 {
00187    return ns ? ns-> sockfd : -1;
00188 }
00189 
00190 const struct ast_sockaddr *ast_netsock_boundaddr(const struct ast_netsock *ns)
00191 {
00192    return &ns->bindaddr;
00193 }
00194 
00195 void *ast_netsock_data(const struct ast_netsock *ns)
00196 {
00197    return ns->data;
00198 }
00199 
00200 void ast_netsock_unref(struct ast_netsock *ns)
00201 {
00202    ASTOBJ_UNREF(ns, ast_netsock_destroy);
00203 }
00204 
00205 char *ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
00206 {
00207    int x;
00208    char *os = s;
00209    if (maxlen < 18) {
00210       if (s && (maxlen > 0))
00211          *s = '\0';
00212    } else {
00213       for (x = 0; x < 5; x++) {
00214          sprintf(s, "%02x:", eid->eid[x]);
00215          s += 3;
00216       }
00217       sprintf(s, "%02x", eid->eid[5]);
00218    }
00219    return os;
00220 }
00221 
00222 void ast_set_default_eid(struct ast_eid *eid)
00223 {
00224 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_IFRU_IFRU_HWADDR)
00225    int s, x = 0;
00226    char eid_str[20];
00227    struct ifreq ifr;
00228    static const unsigned int MAXIF = 10;
00229 
00230    s = socket(AF_INET, SOCK_STREAM, 0);
00231    if (s < 0)
00232       return;
00233    for (x = 0; x < MAXIF; x++) {
00234       static const char *prefixes[] = { "eth", "em" };
00235       unsigned int i;
00236 
00237       for (i = 0; i < ARRAY_LEN(prefixes); i++) {
00238          memset(&ifr, 0, sizeof(ifr));
00239          snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", prefixes[i], x);
00240          if (!ioctl(s, SIOCGIFHWADDR, &ifr)) {
00241             break;
00242          }
00243       }
00244 
00245       if (i == ARRAY_LEN(prefixes)) {
00246          /* Try pciX#[1..N] */
00247          for (i = 0; i < MAXIF; i++) {
00248             memset(&ifr, 0, sizeof(ifr));
00249             snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "pci%u#%u", x, i);
00250             if (!ioctl(s, SIOCGIFHWADDR, &ifr)) {
00251                break;
00252             }
00253          }
00254          if (i == MAXIF) {
00255             continue;
00256          }
00257       }
00258 
00259       memcpy(eid, ((unsigned char *)&ifr.ifr_hwaddr) + 2, sizeof(*eid));
00260       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);
00261       close(s);
00262       return;
00263    }
00264    close(s);
00265 #else
00266 #if defined(ifa_broadaddr) && !defined(SOLARIS)
00267    char eid_str[20];
00268    struct ifaddrs *ifap;
00269 
00270    if (getifaddrs(&ifap) == 0) {
00271       struct ifaddrs *p;
00272       for (p = ifap; p; p = p->ifa_next) {
00273          if ((p->ifa_addr->sa_family == AF_LINK) && !(p->ifa_flags & IFF_LOOPBACK) && (p->ifa_flags & IFF_RUNNING)) {
00274             struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr;
00275             memcpy(&(eid->eid), sdp->sdl_data + sdp->sdl_nlen, 6);
00276             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);
00277             freeifaddrs(ifap);
00278             return;
00279          }
00280       }
00281       freeifaddrs(ifap);
00282    }
00283 #endif
00284 #endif
00285    ast_debug(1, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
00286 }
00287 
00288 int ast_str_to_eid(struct ast_eid *eid, const char *s)
00289 {
00290    unsigned int eid_int[6];
00291    int x;
00292 
00293    if (sscanf(s, "%2x:%2x:%2x:%2x:%2x:%2x", &eid_int[0], &eid_int[1], &eid_int[2],
00294        &eid_int[3], &eid_int[4], &eid_int[5]) != 6)
00295          return -1;
00296 
00297    for (x = 0; x < 6; x++)
00298       eid->eid[x] = eid_int[x];
00299 
00300    return 0;
00301 }
00302 
00303 int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
00304 {
00305    return memcmp(eid1, eid2, sizeof(*eid1));
00306 }

Generated on Sun May 20 06:33:55 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6