Thu Jul 9 13:40:37 2009

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

Generated on Thu Jul 9 13:40:37 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7