Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:49:07 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"`

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_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)
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)
struct sockaddr_in * | ast_netsock_boundaddr (const struct ast_netsock *ns)
void * | ast_netsock_data (const struct ast_netsock *ns)
struct ast_netsock * | ast_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)
struct ast_netsock_list * | ast_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 |
| ) |  |  | `[read]`

Definition at line 180 of file netsock.c.

References ast_log(), ast_netsock_bindaddr(), ast_strdupa, host, LOG_WARNING, portno, and strsep().

Referenced by peer_set_srcaddr(), and set_config().

    00181 {
    00182    struct sockaddr_in sin;
    00183    char *tmp;
    00184    char *host;
    00185    char *port;
    00186    int portno;
    00187
    00188    memset(&sin, 0, sizeof(sin));
    00189    sin.sin_family = AF_INET;
    00190    sin.sin_port = htons(defaultport);
    00191    tmp = ast_strdupa(bindinfo);
    00192    if (!tmp) {
    00193       ast_log(LOG_WARNING, "Out of memory!\n");
    00194       return NULL;
    00195    }
    00196
    00197    host = strsep(&tmp, ":");
    00198    port = tmp;
    00199
    00200    if (port && ((portno = atoi(port)) > 0))
    00201       sin.sin_port = htons(portno);
    00202
    00203    inet_aton(host, &sin.sin_addr);
    00204
    00205    return ast_netsock_bindaddr(list, ioc, &sin, tos, callback, data);
    00206 }

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 |
| ) |  |  | `[read]`

Definition at line 124 of file netsock.c.

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

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

Definition at line 213 of file netsock.c.

References ast_netsock::bindaddr.

    00214 {
    00215    return &(ns->bindaddr);
    00216 }

void* ast_netsock_data | ( | const struct ast_netsock * | ns | ) |

Definition at line 218 of file netsock.c.

References ast_netsock::data.

    00219 {
    00220    return ns->data;
    00221 }

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

Definition at line 109 of file netsock.c.

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

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_free | ( | struct ast_netsock_list * | list,
|  | struct ast_netsock * | netsock |
| ) |  |  |

int ast_netsock_init | ( | struct ast_netsock_list * | list | ) |

Definition at line 93 of file netsock.c.

References ASTOBJ_CONTAINER_INIT.

Referenced by load_module(), and set_config().

    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 |  | ) | `[read]`

Definition at line 84 of file netsock.c.

References calloc.

Referenced by load_module(), and set_config().

    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, and ASTOBJ_CONTAINER_DESTROYALL.

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

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

Referenced by peer_set_srcaddr(), and set_config().

    00209 {
    00210    return ns ? ns-> sockfd : -1;
    00211 }

void ast_netsock_unref | ( | struct ast_netsock * | ns | ) |

Definition at line 223 of file netsock.c.

References ast_netsock_destroy(), and ASTOBJ_UNREF.

Referenced by peer_set_srcaddr(), and set_config().

    00224 {
    00225    ASTOBJ_UNREF(ns, ast_netsock_destroy);
    00226 }