Thu Apr 28 2011 17:16:09

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

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

Referenced by peer_set_srcaddr(), and set_config().

{
   struct sockaddr_in sin;
   char *tmp;
   char *host;
   char *port;
   int portno;

   memset(&sin, 0, sizeof(sin));
   sin.sin_family = AF_INET;
   sin.sin_port = htons(defaultport);
   tmp = ast_strdupa(bindinfo);

   host = strsep(&tmp, ":");
   port = tmp;

   if (port && ((portno = atoi(port)) > 0))
      sin.sin_port = htons(portno);

   inet_aton(host, &sin.sin_addr);

   return ast_netsock_bindaddr(list, ioc, &sin, tos, cos, callback, data);
}
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 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, 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().

{
   int netsocket = -1;
   int *ioref;
   
   struct ast_netsock *ns;
   const int reuseFlag = 1;
   
   /* Make a UDP socket */
   netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
   
   if (netsocket < 0) {
      ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
      return NULL;
   }
   if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
         ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
   }
   if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) {
      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));
      close(netsocket);
      return NULL;
   }

   ast_netsock_set_qos(netsocket, tos, cos, "IAX2");
      
   ast_enable_packet_fragmentation(netsocket);

   if (!(ns = ast_calloc(1, sizeof(*ns)))) {
      close(netsocket);
      return NULL;
   }
   
   /* Establish I/O callback for socket read */
   if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
      close(netsocket);
      ast_free(ns);
      return NULL;
   }  
   ASTOBJ_INIT(ns);
   ns->ioref = ioref;
   ns->ioc = ioc;
   ns->sockfd = netsocket;
   ns->data = data;
   memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
   ASTOBJ_CONTAINER_LINK(list, ns);

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

Definition at line 203 of file netsock.c.

References ast_netsock::bindaddr.

{
   return &(ns->bindaddr);
}
void* ast_netsock_data ( const struct ast_netsock ns)

Definition at line 208 of file netsock.c.

References ast_netsock::data.

{
   return ns->data;
}
struct ast_netsock* ast_netsock_find ( struct ast_netsock_list list,
struct sockaddr_in *  sa 
) [read]

Definition at line 89 of file netsock.c.

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

Referenced by peer_set_srcaddr().

{
   struct ast_netsock *sock = NULL;

   ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
      ASTOBJ_RDLOCK(iterator);
      if (!inaddrcmp(&iterator->bindaddr, sa))
         sock = iterator;
      ASTOBJ_UNLOCK(iterator);
   });

   return sock;
}
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().

{
   memset(list, 0, sizeof(*list));
   ASTOBJ_CONTAINER_INIT(list);

   return 0;
}
struct ast_netsock_list* ast_netsock_list_alloc ( void  ) [read]

Definition at line 67 of file netsock.c.

References ast_calloc.

Referenced by load_module(), and set_config().

{
   return ast_calloc(1, sizeof(struct ast_netsock_list));
}
int ast_netsock_release ( struct ast_netsock_list list)
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_rtp_setqos(), ast_udptl_setqos(), config_load(), load_module(), and reload_config().

{
   int res;
   
   if ((res = setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
      ast_log(LOG_WARNING, "Unable to set %s TOS to %d, may be you have no root privileges\n", desc, tos);
   else if (tos)
      ast_verb(2, "Using %s TOS bits %d\n", desc, tos);

#if defined(linux)                        
   if (setsockopt(netsocket, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos)))
      ast_log(LOG_WARNING, "Unable to set %s CoS to %d\n", desc, cos);
   else if (cos)
      ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
#endif
                     
   return res;
}
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().

{
   return ns ? ns-> sockfd : -1;
}
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().