Sat Aug 6 00:39:30 2011

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: 222877 $")
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <sys/time.h>
00036 #include <signal.h>
00037 #include <errno.h>
00038 #include <unistd.h>
00039 #include <netinet/in.h>
00040 #include <arpa/inet.h>
00041 #include <sys/socket.h>
00042 #include <netdb.h>
00043 #include <net/if.h>
00044 #include <netinet/in_systm.h>
00045 #include <netinet/ip.h>
00046 #include <sys/ioctl.h>
00047 
00048 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
00049 #include <fcntl.h>
00050 #include <net/route.h>
00051 #endif
00052 
00053 #if defined (SOLARIS)
00054 #include <sys/sockio.h>
00055 #endif
00056 
00057 #include "asterisk/netsock.h"
00058 #include "asterisk/logger.h"
00059 #include "asterisk/channel.h"
00060 #include "asterisk/options.h"
00061 #include "asterisk/utils.h"
00062 #include "asterisk/lock.h"
00063 #include "asterisk/srv.h"
00064 
00065 struct ast_netsock {
00066    ASTOBJ_COMPONENTS(struct ast_netsock);
00067    struct sockaddr_in bindaddr;
00068    int sockfd;
00069    int *ioref;
00070    struct io_context *ioc;
00071    void *data;
00072 };
00073 
00074 struct ast_netsock_list {
00075    ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock);
00076    struct io_context *ioc;
00077 };
00078 
00079 static void ast_netsock_destroy(struct ast_netsock *netsock)
00080 {
00081    ast_io_remove(netsock->ioc, netsock->ioref);
00082    close(netsock->sockfd);
00083    free(netsock);
00084 }
00085 
00086 struct ast_netsock_list *ast_netsock_list_alloc(void)
00087 {
00088    return ast_calloc(1, sizeof(struct ast_netsock_list));
00089 }
00090 
00091 int ast_netsock_init(struct ast_netsock_list *list)
00092 {
00093    memset(list, 0, sizeof(*list));
00094    ASTOBJ_CONTAINER_INIT(list);
00095 
00096    return 0;
00097 }
00098 
00099 int ast_netsock_release(struct ast_netsock_list *list)
00100 {
00101    ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
00102    ASTOBJ_CONTAINER_DESTROY(list);
00103    ast_free(list);
00104 
00105    return 0;
00106 }
00107 
00108 struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list,
00109                  struct sockaddr_in *sa)
00110 {
00111    struct ast_netsock *sock = NULL;
00112 
00113    ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
00114       ASTOBJ_RDLOCK(iterator);
00115       if (!inaddrcmp(&iterator->bindaddr, sa))
00116          sock = iterator;
00117       ASTOBJ_UNLOCK(iterator);
00118    });
00119 
00120    return sock;
00121 }
00122 
00123 struct ast_netsock *ast_netsock_bindaddr(struct ast_netsock_list *list, struct io_context *ioc, struct sockaddr_in *bindaddr, int tos, ast_io_cb callback, void *data)
00124 {
00125    int netsocket = -1;
00126    int *ioref;
00127    
00128    struct ast_netsock *ns;
00129    const int reuseFlag = 1;
00130    
00131    /* Make a UDP socket */
00132    netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
00133    
00134    if (netsocket < 0) {
00135       ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
00136       return NULL;
00137    }
00138    if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
00139          ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
00140    }
00141    if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) {
00142       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));
00143       close(netsocket);
00144       return NULL;
00145    }
00146    if (option_verbose > 1)
00147       ast_verbose(VERBOSE_PREFIX_2 "Using TOS bits %d\n", tos);
00148 
00149    if (setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) 
00150       ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
00151 
00152    ast_enable_packet_fragmentation(netsocket);
00153 
00154    if (!(ns = ast_calloc(1, sizeof(struct ast_netsock)))) {
00155       close(netsocket);
00156       return NULL;
00157    }
00158    
00159    /* Establish I/O callback for socket read */
00160    if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
00161       close(netsocket);
00162       free(ns);
00163       return NULL;
00164    }  
00165    ASTOBJ_INIT(ns);
00166    ns->ioref = ioref;
00167    ns->ioc = ioc;
00168    ns->sockfd = netsocket;
00169    ns->data = data;
00170    memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
00171    ASTOBJ_CONTAINER_LINK(list, ns);
00172 
00173    return ns;
00174 }
00175 
00176 struct ast_netsock *ast_netsock_bind(struct ast_netsock_list *list, struct io_context *ioc, const char *bindinfo, int defaultport, int tos, ast_io_cb callback, void *data)
00177 {
00178    struct sockaddr_in sin;
00179    char *tmp;
00180    char *host;
00181    char *port;
00182    int portno;
00183 
00184    memset(&sin, 0, sizeof(sin));
00185    sin.sin_family = AF_INET;
00186    sin.sin_port = htons(defaultport);
00187    tmp = ast_strdupa(bindinfo);
00188 
00189    host = strsep(&tmp, ":");
00190    port = tmp;
00191 
00192    if (port && ((portno = atoi(port)) > 0))
00193       sin.sin_port = htons(portno);
00194 
00195    inet_aton(host, &sin.sin_addr);
00196 
00197    return ast_netsock_bindaddr(list, ioc, &sin, tos, callback, data);
00198 }
00199 
00200 int ast_netsock_sockfd(const struct ast_netsock *ns)
00201 {
00202    return ns ? ns-> sockfd : -1;
00203 }
00204 
00205 const struct sockaddr_in *ast_netsock_boundaddr(const struct ast_netsock *ns)
00206 {
00207    return &(ns->bindaddr);
00208 }
00209 
00210 void *ast_netsock_data(const struct ast_netsock *ns)
00211 {
00212    return ns->data;
00213 }
00214 
00215 void ast_netsock_unref(struct ast_netsock *ns)
00216 {
00217    ASTOBJ_UNREF(ns, ast_netsock_destroy);
00218 }

Generated on Sat Aug 6 00:39:30 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7