Fri Jun 19 12:10:42 2009

Asterisk developer's documentation


netsock.c File Reference

Network socket handling. More...

#include "asterisk.h"
#include <ifaddrs.h>
#include "asterisk/netsock.h"
#include "asterisk/utils.h"
#include "asterisk/astobj.h"

Go to the source code of this file.

Data Structures

struct  ast_netsock
struct  ast_netsock_list

Functions

int ast_eid_cmp (const struct ast_eid *eid1, const struct ast_eid *eid2)
 Compare two EIDs.
char * ast_eid_to_str (char *s, int maxlen, struct ast_eid *eid)
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)
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)
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_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)
void ast_set_default_eid (struct ast_eid *eid)
 Fill in an ast_eid with the default eid of this machine.
int ast_str_to_eid (struct ast_eid *eid, const char *s)
 Convert a string into an EID.


Detailed Description

Network socket handling.

Author:
Kevin P. Fleming <kpfleming@digium.com>

Mark Spencer <markster@digium.com>

Definition in file netsock.c.


Function Documentation

int ast_eid_cmp ( const struct ast_eid eid1,
const struct ast_eid eid2 
)

Compare two EIDs.

Returns:
0 if the two are the same, non-zero otherwise
Since:
1.6.1

Definition at line 293 of file netsock.c.

Referenced by build_peer(), build_transactions(), destroy_trans(), dundi_answer_entity(), dundi_answer_query(), dundi_ie_append_eid_appropriately(), dundi_lookup_internal(), dundi_query_thread(), find_peer(), optimize_transactions(), and register_request().

00294 {
00295    return memcmp(eid1, eid2, sizeof(*eid1));
00296 }

char* ast_eid_to_str ( char *  s,
int  maxlen,
struct ast_eid eid 
)

/brief Convert an EID to a string

Since:
1.6.1

Definition at line 217 of file netsock.c.

References ast_eid::eid.

Referenced by append_transaction(), ast_set_default_eid(), build_transactions(), cache_lookup(), cache_lookup_internal(), check_key(), complete_peer_helper(), destroy_trans(), do_autokill(), do_register(), do_register_expire(), dump_answer(), dump_eid(), dundi_answer_entity(), dundi_lookup_internal(), dundi_lookup_local(), dundi_lookup_thread(), dundi_precache_thread(), dundi_prop_precache(), dundi_query_thread(), dundi_send(), dundi_show_entityid(), dundi_show_peer(), dundi_show_peers(), dundi_show_requests(), handle_command_response(), handle_show_settings(), pbx_retrieve_variable(), populate_addr(), register_request(), and update_key().

00218 {
00219    int x;
00220    char *os = s;
00221    if (maxlen < 18) {
00222       if (s && (maxlen > 0))
00223          *s = '\0';
00224    } else {
00225       for (x = 0; x < 5; x++) {
00226          sprintf(s, "%02x:", eid->eid[x]);
00227          s += 3;
00228       }
00229       sprintf(s, "%02x", eid->eid[5]);
00230    }
00231    return os;
00232 }

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 
)

Definition at line 173 of file netsock.c.

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

Referenced by peer_set_srcaddr(), and set_config().

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

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 
)

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

Referenced by ast_netsock_bind().

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

struct sockaddr_in* ast_netsock_boundaddr ( const struct ast_netsock ns  ) 

Definition at line 202 of file netsock.c.

References ast_netsock::bindaddr.

00203 {
00204    return &(ns->bindaddr);
00205 }

void* ast_netsock_data ( const struct ast_netsock ns  ) 

Definition at line 207 of file netsock.c.

References ast_netsock::data.

00208 {
00209    return ns->data;
00210 }

static void ast_netsock_destroy ( struct ast_netsock netsock  )  [static]

Definition at line 60 of file netsock.c.

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

Referenced by ast_netsock_release(), and ast_netsock_unref().

00061 {
00062    ast_io_remove(netsock->ioc, netsock->ioref);
00063    close(netsock->sockfd);
00064    ast_free(netsock);
00065 }

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

Definition at line 88 of file netsock.c.

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

Referenced by peer_set_srcaddr().

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

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

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

struct ast_netsock_list* ast_netsock_list_alloc ( void   ) 

Definition at line 67 of file netsock.c.

References ast_calloc.

Referenced by load_module(), and set_config().

00068 {
00069    return ast_calloc(1, sizeof(struct ast_netsock_list));
00070 }

int ast_netsock_release ( struct ast_netsock_list list  ) 

Definition at line 80 of file netsock.c.

References ast_netsock_destroy(), ASTOBJ_CONTAINER_DESTROY, and ASTOBJ_CONTAINER_DESTROYALL.

Referenced by __unload_module(), and set_config().

00081 {
00082    ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
00083    ASTOBJ_CONTAINER_DESTROY(list);
00084 
00085    return 0;
00086 }

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

Definition at line 153 of file netsock.c.

References ast_log(), ast_verb, and LOG_WARNING.

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

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

int ast_netsock_sockfd ( const struct ast_netsock ns  ) 

Definition at line 197 of file netsock.c.

Referenced by peer_set_srcaddr(), and set_config().

00198 {
00199    return ns ? ns-> sockfd : -1;
00200 }

void ast_netsock_unref ( struct ast_netsock ns  ) 

Definition at line 212 of file netsock.c.

References ast_netsock_destroy(), and ASTOBJ_UNREF.

Referenced by peer_set_srcaddr(), and set_config().

00213 {
00214    ASTOBJ_UNREF(ns, ast_netsock_destroy);
00215 }

void ast_set_default_eid ( struct ast_eid eid  ) 

Fill in an ast_eid with the default eid of this machine.

Since:
1.6.1

Definition at line 234 of file netsock.c.

References ast_debug, ast_eid_to_str(), ast_eid::eid, and s.

Referenced by ast_readconfig().

00235 {
00236 #if defined(SIOCGIFHWADDR)
00237    int s, x = 0;
00238    char eid_str[20];
00239    struct ifreq ifr;
00240 
00241    s = socket(AF_INET, SOCK_STREAM, 0);
00242    if (s < 0)
00243       return;
00244    for (x = 0; x < 10; x++) {
00245       memset(&ifr, 0, sizeof(ifr));
00246       snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth%d", x);
00247       if (ioctl(s, SIOCGIFHWADDR, &ifr))
00248          continue;
00249       memcpy(eid, ((unsigned char *)&ifr.ifr_hwaddr) + 2, sizeof(*eid));
00250       ast_debug(1, "Seeding global EID '%s' from '%s' using 'siocgifhwaddr'\n", ast_eid_to_str(eid_str, sizeof(eid_str), eid), ifr.ifr_name);
00251       close(s);
00252       return;
00253    }
00254    close(s);
00255 #else
00256 #if defined(ifa_broadaddr) && !defined(SOLARIS)
00257    char eid_str[20];
00258    struct ifaddrs *ifap;
00259    
00260    if (getifaddrs(&ifap) == 0) {
00261       struct ifaddrs *p;
00262       for (p = ifap; p; p = p->ifa_next) {
00263          if ((p->ifa_addr->sa_family == AF_LINK) && !(p->ifa_flags & IFF_LOOPBACK) && (p->ifa_flags & IFF_RUNNING)) {
00264             struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr;
00265             memcpy(&(eid->eid), sdp->sdl_data + sdp->sdl_nlen, 6);
00266             ast_debug(1, "Seeding global EID '%s' from '%s' using 'getifaddrs'\n", ast_eid_to_str(eid_str, sizeof(eid_str), eid), p->ifa_name);
00267             freeifaddrs(ifap);
00268             return;
00269          }
00270       }
00271       freeifaddrs(ifap);
00272    }
00273 #endif
00274 #endif
00275    ast_debug(1, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
00276 }

int ast_str_to_eid ( struct ast_eid eid,
const char *  s 
)

Convert a string into an EID.

This function expects an EID in the format: 00:11:22:33:44:55

Returns:
0 success, non-zero failure
Since:
1.6.1

Definition at line 278 of file netsock.c.

References ast_eid::eid.

Referenced by dundi_do_query(), and set_config().

00279 {
00280    unsigned int eid_int[6];
00281    int x;
00282 
00283    if (sscanf(s, "%x:%x:%x:%x:%x:%x", &eid_int[0], &eid_int[1], &eid_int[2],
00284        &eid_int[3], &eid_int[4], &eid_int[5]) != 6)
00285          return -1;
00286    
00287    for (x = 0; x < 6; x++)
00288       eid->eid[x] = eid_int[x];
00289 
00290    return 0;
00291 }


Generated on Fri Jun 19 12:10:42 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7