Fri Feb 10 06:36:51 2012

Asterisk developer's documentation


netsock.h File Reference

Network socket handling. More...

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

Include dependency graph for netsock.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

struct 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)
struct 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)
struct sockaddr_in * ast_netsock_boundaddr (const struct ast_netsock *ns)
void * ast_netsock_data (const struct ast_netsock *ns)
struct ast_netsockast_netsock_find (struct ast_netsock_list *list, struct sockaddr_in *sa)
int ast_netsock_init (struct ast_netsock_list *list)
struct 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 
) [read]

Definition at line 176 of file netsock.c.

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

Referenced by peer_set_srcaddr(), and set_config().

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, cos, callback, data);
00198 }

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 
) [read]

Definition at line 106 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, ast_netsock::bindaddr, ast_netsock::data, errno, ast_netsock::ioc, ast_netsock::ioref, LOG_ERROR, LOG_WARNING, netsocket, and ast_netsock::sockfd.

Referenced by ast_netsock_bind().

00107 {
00108    int netsocket = -1;
00109    int *ioref;
00110    
00111    struct ast_netsock *ns;
00112    const int reuseFlag = 1;
00113    
00114    /* Make a UDP socket */
00115    netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
00116    
00117    if (netsocket < 0) {
00118       ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
00119       return NULL;
00120    }
00121    if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
00122          ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
00123    }
00124    if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) {
00125       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));
00126       close(netsocket);
00127       return NULL;
00128    }
00129 
00130    ast_netsock_set_qos(netsocket, tos, cos, "IAX2");
00131       
00132    ast_enable_packet_fragmentation(netsocket);
00133 
00134    if (!(ns = ast_calloc(1, sizeof(*ns)))) {
00135       close(netsocket);
00136       return NULL;
00137    }
00138    
00139    /* Establish I/O callback for socket read */
00140    if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
00141       close(netsocket);
00142       ast_free(ns);
00143       return NULL;
00144    }  
00145    ASTOBJ_INIT(ns);
00146    ns->ioref = ioref;
00147    ns->ioc = ioc;
00148    ns->sockfd = netsocket;
00149    ns->data = data;
00150    memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
00151    ASTOBJ_CONTAINER_LINK(list, ns);
00152 
00153    return ns;
00154 }

struct sockaddr_in* ast_netsock_boundaddr ( const struct ast_netsock ns  )  [read]

Definition at line 205 of file netsock.c.

References ast_netsock::bindaddr.

00206 {
00207    return &(ns->bindaddr);
00208 }

void* ast_netsock_data ( const struct ast_netsock ns  ) 

Definition at line 210 of file netsock.c.

References ast_netsock::data.

00211 {
00212    return ns->data;
00213 }

struct ast_netsock* ast_netsock_find ( struct ast_netsock_list list,
struct sockaddr_in *  sa 
) [read]

Definition at line 91 of file netsock.c.

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

Referenced by peer_set_srcaddr().

00093 {
00094    struct ast_netsock *sock = NULL;
00095 
00096    ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
00097       ASTOBJ_RDLOCK(iterator);
00098       if (!inaddrcmp(&iterator->bindaddr, sa))
00099          sock = iterator;
00100       ASTOBJ_UNLOCK(iterator);
00101    });
00102 
00103    return sock;
00104 }

int ast_netsock_init ( struct ast_netsock_list list  ) 

Definition at line 74 of file netsock.c.

References ASTOBJ_CONTAINER_INIT.

Referenced by load_module(), and set_config().

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

struct ast_netsock_list* ast_netsock_list_alloc ( void   )  [read]

Definition at line 69 of file netsock.c.

References ast_calloc.

Referenced by load_module(), and set_config().

00070 {
00071    return ast_calloc(1, sizeof(struct ast_netsock_list));
00072 }

int ast_netsock_release ( struct ast_netsock_list list  ) 

Definition at line 82 of file netsock.c.

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

Referenced by __unload_module(), and set_config().

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

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

Definition at line 156 of file netsock.c.

References ast_log(), ast_verb, and LOG_WARNING.

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

00157 {
00158    int res;
00159    
00160    if ((res = setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
00161       ast_log(LOG_WARNING, "Unable to set %s TOS to %d, may be you have no root privileges\n", desc, tos);
00162    else if (tos)
00163       ast_verb(2, "Using %s TOS bits %d\n", desc, tos);
00164 
00165 #if defined(linux)                        
00166    if (setsockopt(netsocket, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos)))
00167       ast_log(LOG_WARNING, "Unable to set %s CoS to %d\n", desc, cos);
00168    else if (cos)
00169       ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
00170 #endif
00171                      
00172    return res;
00173 }

int ast_netsock_sockfd ( const struct ast_netsock ns  ) 

Definition at line 200 of file netsock.c.

Referenced by peer_set_srcaddr(), and set_config().

00201 {
00202    return ns ? ns-> sockfd : -1;
00203 }

void ast_netsock_unref ( struct ast_netsock ns  ) 

Definition at line 215 of file netsock.c.

References ast_netsock_destroy(), and ASTOBJ_UNREF.

Referenced by peer_set_srcaddr(), and set_config().

00216 {
00217    ASTOBJ_UNREF(ns, ast_netsock_destroy);
00218 }


Generated on Fri Feb 10 06:36:51 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6