Mon Sep 18 09:15:19 2006

Asterisk developer's documentation


netsock.c File Reference

Network socket handling. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <net/if.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <sys/ioctl.h>
#include "asterisk.h"
#include "asterisk/netsock.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/srv.h"

Go to the source code of this file.

Data Structures

struct  ast_netsock
struct  ast_netsock_list

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)
static void ast_netsock_destroy (struct ast_netsock *netsock)
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_sockfd (const struct ast_netsock *ns)
void ast_netsock_unref (struct ast_netsock *ns)


Detailed Description

Network socket handling.

Definition in file netsock.c.


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_log(), ast_netsock_bindaddr(), ast_strdupa, host, list, LOG_WARNING, portno, and strsep().

Referenced by 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    if (!tmp) {
00188       ast_log(LOG_WARNING, "Out of memory!\n");
00189       return NULL;
00190    }
00191 
00192    host = strsep(&tmp, ":");
00193    port = tmp;
00194 
00195    if (port && ((portno = atoi(port)) > 0))
00196       sin.sin_port = htons(portno);
00197 
00198    inet_aton(host, &sin.sin_addr);
00199 
00200    return ast_netsock_bindaddr(list, ioc, &sin, tos, callback, data);
00201 }

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

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

Referenced by ast_netsock_bind().

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

struct sockaddr_in* ast_netsock_boundaddr ( const struct ast_netsock ns  ) 

Definition at line 208 of file netsock.c.

References ast_netsock::bindaddr.

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

void* ast_netsock_data ( const struct ast_netsock ns  ) 

Definition at line 213 of file netsock.c.

References ast_netsock::data.

00214 {
00215    return ns->data;
00216 }

static void ast_netsock_destroy ( struct ast_netsock netsock  )  [static]

Definition at line 77 of file netsock.c.

References ast_io_remove(), free, ast_netsock_list::ioc, and netsock.

Referenced by ast_netsock_release(), and ast_netsock_unref().

00078 {
00079    ast_io_remove(netsock->ioc, netsock->ioref);
00080    close(netsock->sockfd);
00081    free(netsock);
00082 }

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

Definition at line 109 of file netsock.c.

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

Referenced by peer_set_srcaddr().

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

int ast_netsock_init ( struct ast_netsock_list list  ) 

Definition at line 93 of file netsock.c.

References ASTOBJ_CONTAINER_INIT, and list.

Referenced by load_module().

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

struct ast_netsock_list* ast_netsock_list_alloc ( void   ) 

Definition at line 84 of file netsock.c.

References calloc.

Referenced by load_module().

00085 {
00086    struct ast_netsock_list *res;
00087 
00088    res = calloc(1, sizeof(*res));
00089 
00090    return res;
00091 }

int ast_netsock_release ( struct ast_netsock_list list  ) 

Definition at line 101 of file netsock.c.

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

Referenced by __unload_module(), and load_module().

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

int ast_netsock_sockfd ( const struct ast_netsock ns  ) 

Definition at line 203 of file netsock.c.

Referenced by peer_set_srcaddr(), and set_config().

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

void ast_netsock_unref ( struct ast_netsock ns  ) 

Definition at line 218 of file netsock.c.

References ast_netsock_destroy(), and ASTOBJ_UNREF.

Referenced by set_config().

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


Generated on Mon Sep 18 09:15:19 2006 for Asterisk - the Open Source PBX by  doxygen 1.4.7