Thu Feb 5 16:26:18 2009

Asterisk developer's documentation


netsock.h File Reference

Network socket handling. More...

#include <netinet/in.h>
#include "asterisk/io.h"
#include "asterisk/astobj.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, 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, 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_free (struct ast_netsock_list *list, struct ast_netsock *netsock)
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_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,
ast_io_cb  callback,
void *  data 
)

Definition at line 175 of file netsock.c.

References ast_netsock_bindaddr(), ast_strdupa, and portno.

Referenced by peer_set_srcaddr(), and set_config().

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

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 
)

Definition at line 122 of file netsock.c.

References ast_calloc, ast_enable_packet_fragmentation(), ast_inet_ntoa(), ast_io_add(), AST_IO_IN, ast_log(), ast_verbose(), ASTOBJ_CONTAINER_LINK, ASTOBJ_INIT, ast_netsock::bindaddr, ast_netsock::data, errno, free, ast_netsock::ioc, ast_netsock::ioref, LOG_ERROR, LOG_WARNING, netsocket, option_verbose, ast_netsock::sockfd, and VERBOSE_PREFIX_2.

Referenced by ast_netsock_bind().

00123 {
00124    int netsocket = -1;
00125    int *ioref;
00126    
00127    struct ast_netsock *ns;
00128    const int reuseFlag = 1;
00129    
00130    /* Make a UDP socket */
00131    netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
00132    
00133    if (netsocket < 0) {
00134       ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
00135       return NULL;
00136    }
00137    if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
00138          ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
00139    }
00140    if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) {
00141       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));
00142       close(netsocket);
00143       return NULL;
00144    }
00145    if (option_verbose > 1)
00146       ast_verbose(VERBOSE_PREFIX_2 "Using TOS bits %d\n", tos);
00147 
00148    if (setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) 
00149       ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
00150 
00151    ast_enable_packet_fragmentation(netsocket);
00152 
00153    if (!(ns = ast_calloc(1, sizeof(struct ast_netsock)))) {
00154       close(netsocket);
00155       return NULL;
00156    }
00157    
00158    /* Establish I/O callback for socket read */
00159    if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
00160       close(netsocket);
00161       free(ns);
00162       return NULL;
00163    }  
00164    ASTOBJ_INIT(ns);
00165    ns->ioref = ioref;
00166    ns->ioc = ioc;
00167    ns->sockfd = netsocket;
00168    ns->data = data;
00169    memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
00170    ASTOBJ_CONTAINER_LINK(list, ns);
00171 
00172    return ns;
00173 }

struct sockaddr_in* ast_netsock_boundaddr ( const struct ast_netsock ns  ) 

Definition at line 204 of file netsock.c.

References ast_netsock::bindaddr.

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

void* ast_netsock_data ( const struct ast_netsock ns  ) 

Definition at line 209 of file netsock.c.

References ast_netsock::data.

00210 {
00211    return ns->data;
00212 }

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

Definition at line 107 of file netsock.c.

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

Referenced by peer_set_srcaddr().

00109 {
00110    struct ast_netsock *sock = NULL;
00111 
00112    ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
00113       ASTOBJ_RDLOCK(iterator);
00114       if (!inaddrcmp(&iterator->bindaddr, sa))
00115          sock = iterator;
00116       ASTOBJ_UNLOCK(iterator);
00117    });
00118 
00119    return sock;
00120 }

int ast_netsock_free ( struct ast_netsock_list list,
struct ast_netsock netsock 
)

int ast_netsock_init ( struct ast_netsock_list list  ) 

Definition at line 91 of file netsock.c.

References ASTOBJ_CONTAINER_INIT.

Referenced by load_module(), and set_config().

00092 {
00093    memset(list, 0, sizeof(*list));
00094    ASTOBJ_CONTAINER_INIT(list);
00095 
00096    return 0;
00097 }

struct ast_netsock_list* ast_netsock_list_alloc ( void   ) 

Definition at line 86 of file netsock.c.

References ast_calloc.

Referenced by load_module(), and set_config().

00087 {
00088    return ast_calloc(1, sizeof(struct ast_netsock_list));
00089 }

int ast_netsock_release ( struct ast_netsock_list list  ) 

Definition at line 99 of file netsock.c.

References ast_netsock_destroy(), ASTOBJ_CONTAINER_DESTROY, and ASTOBJ_CONTAINER_DESTROYALL.

Referenced by __unload_module(), load_module(), and set_config().

00100 {
00101    ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
00102    ASTOBJ_CONTAINER_DESTROY(list);
00103 
00104    return 0;
00105 }

int ast_netsock_sockfd ( const struct ast_netsock ns  ) 

Definition at line 199 of file netsock.c.

Referenced by peer_set_srcaddr(), and set_config().

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

void ast_netsock_unref ( struct ast_netsock ns  ) 

Definition at line 214 of file netsock.c.

References ast_netsock_destroy(), and ASTOBJ_UNREF.

Referenced by peer_set_srcaddr(), and set_config().

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


Generated on Thu Feb 5 16:26:18 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7