Wed Jan 27 20:02:42 2016

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

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 178 of file netsock.c.

References ast_netsock_bindaddr(), and ast_strdupa.

Referenced by peer_set_srcaddr(), and set_config().

00179 {
00180    struct sockaddr_in sin;
00181    char *tmp;
00182    char *host;
00183    char *port;
00184    int portno;
00185 
00186    memset(&sin, 0, sizeof(sin));
00187    sin.sin_family = AF_INET;
00188    sin.sin_port = htons(defaultport);
00189    tmp = ast_strdupa(bindinfo);
00190 
00191    host = strsep(&tmp, ":");
00192    port = tmp;
00193 
00194    if (port && ((portno = atoi(port)) > 0))
00195       sin.sin_port = htons(portno);
00196 
00197    inet_aton(host, &sin.sin_addr);
00198 
00199    return ast_netsock_bindaddr(list, ioc, &sin, tos, cos, callback, data);
00200 }

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 108 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().

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

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

Definition at line 207 of file netsock.c.

References ast_netsock::bindaddr.

00208 {
00209    return &(ns->bindaddr);
00210 }

void* ast_netsock_data ( const struct ast_netsock ns  ) 

Definition at line 212 of file netsock.c.

References ast_netsock::data.

00213 {
00214    return ns->data;
00215 }

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

Definition at line 93 of file netsock.c.

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

Referenced by peer_set_srcaddr().

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

int ast_netsock_init ( struct ast_netsock_list list  ) 

Definition at line 76 of file netsock.c.

References ASTOBJ_CONTAINER_INIT.

Referenced by load_module(), and set_config().

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

struct ast_netsock_list* ast_netsock_list_alloc ( void   )  [read]

Definition at line 71 of file netsock.c.

References ast_calloc.

Referenced by load_module(), and set_config().

00072 {
00073    return ast_calloc(1, sizeof(struct ast_netsock_list));
00074 }

int ast_netsock_release ( struct ast_netsock_list list  ) 

Definition at line 84 of file netsock.c.

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

Referenced by __unload_module(), and set_config().

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

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

Definition at line 158 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().

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

int ast_netsock_sockfd ( const struct ast_netsock ns  ) 

Definition at line 202 of file netsock.c.

Referenced by peer_set_srcaddr(), and set_config().

00203 {
00204    return ns ? ns-> sockfd : -1;
00205 }

void ast_netsock_unref ( struct ast_netsock ns  ) 

Definition at line 217 of file netsock.c.

References ast_netsock_destroy(), and ASTOBJ_UNREF.

Referenced by peer_set_srcaddr(), and set_config().

00218 {
00219    ASTOBJ_UNREF(ns, ast_netsock_destroy);
00220 }


Generated on 27 Jan 2016 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1