Wed Apr 6 11:30:06 2011

Asterisk developer's documentation


netsock.h File Reference

Network socket handling. More...

#include "asterisk/network.h"
#include "asterisk/io.h"

Go to the source code of this file.

Functions

ast_netsockast_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)
ast_netsockast_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)
sockaddr_in * ast_netsock_boundaddr (const struct ast_netsock *ns)
void * ast_netsock_data (const struct ast_netsock *ns)
ast_netsockast_netsock_find (struct ast_netsock_list *list, struct sockaddr_in *sa)
int ast_netsock_init (struct ast_netsock_list *list)
ast_netsock_listast_netsock_list_alloc (void)
int ast_netsock_release (struct ast_netsock_list *list)
int ast_netsock_set_qos (int netsocket, int tos, int cos, const char *desc)
int ast_netsock_sockfd (const struct ast_netsock *ns)
void ast_netsock_unref (struct ast_netsock *ns)


Detailed Description

Network socket handling.

Definition in file netsock.h.


Function Documentation

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 
)

Definition at line 174 of file netsock.c.

References ast_netsock_bindaddr(), ast_strdupa, inet_aton(), and strsep().

Referenced by peer_set_srcaddr(), and set_config().

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 }

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 
)

Definition at line 104 of file netsock.c.

References ast_calloc, ast_enable_packet_fragmentation(), ast_free, ast_inet_ntoa(), ast_io_add(), AST_IO_IN, ast_log(), ast_netsock_set_qos(), ASTOBJ_CONTAINER_LINK, ASTOBJ_INIT, errno, ast_netsock::ioc, ast_netsock::ioref, LOG_ERROR, LOG_WARNING, and netsocket.

Referenced by ast_netsock_bind().

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 }

struct sockaddr_in* ast_netsock_boundaddr ( const struct ast_netsock ns  ) 

Definition at line 203 of file netsock.c.

References ast_netsock::bindaddr.

00204 {
00205    return &(ns->bindaddr);
00206 }

void* ast_netsock_data ( const struct ast_netsock ns  ) 

Definition at line 208 of file netsock.c.

References ast_netsock::data.

00209 {
00210    return ns->data;
00211 }

struct ast_netsock* ast_netsock_find ( struct ast_netsock_list list,
struct sockaddr_in *  sa 
)

Definition at line 89 of file netsock.c.

References ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_RDLOCK, ASTOBJ_UNLOCK, and inaddrcmp().

Referenced by peer_set_srcaddr().

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 }

int ast_netsock_init ( struct ast_netsock_list list  ) 

Definition at line 72 of file netsock.c.

References ASTOBJ_CONTAINER_INIT.

Referenced by load_module(), and set_config().

00073 {
00074    memset(list, 0, sizeof(*list));
00075    ASTOBJ_CONTAINER_INIT(list);
00076 
00077    return 0;
00078 }

struct ast_netsock_list* ast_netsock_list_alloc ( void   ) 

Definition at line 67 of file netsock.c.

References ast_calloc.

Referenced by load_module(), and set_config().

00068 {
00069    return ast_calloc(1, sizeof(struct ast_netsock_list));
00070 }

int ast_netsock_release ( struct ast_netsock_list list  ) 

Definition at line 80 of file netsock.c.

References ast_free, ast_netsock_destroy(), ASTOBJ_CONTAINER_DESTROY, and ASTOBJ_CONTAINER_DESTROYALL.

Referenced by __unload_module(), and set_config().

00081 {
00082    ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
00083    ASTOBJ_CONTAINER_DESTROY(list);
00084    ast_free(list);
00085 
00086    return 0;
00087 }

int ast_netsock_set_qos ( int  netsocket,
int  tos,
int  cos,
const char *  desc 
)

Definition at line 154 of file netsock.c.

References ast_log(), ast_verb, and LOG_WARNING.

Referenced by ast_netsock_bindaddr(), ast_udptl_setqos(), config_load(), and load_module().

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 }

int ast_netsock_sockfd ( const struct ast_netsock ns  ) 

Definition at line 198 of file netsock.c.

Referenced by peer_set_srcaddr(), and set_config().

00199 {
00200    return ns ? ns-> sockfd : -1;
00201 }

void ast_netsock_unref ( struct ast_netsock ns  ) 

Definition at line 213 of file netsock.c.

References ast_netsock_destroy(), and ASTOBJ_UNREF.

Referenced by peer_set_srcaddr(), and set_config().

00214 {
00215    ASTOBJ_UNREF(ns, ast_netsock_destroy);
00216 }


Generated on Wed Apr 6 11:30:06 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7