#include "asterisk.h"
#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/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_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) |
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) |
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_netsock * | ast_netsock_find (struct ast_netsock_list *list, struct sockaddr_in *sa) |
int | ast_netsock_init (struct ast_netsock_list *list) |
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) |
Mark Spencer <markster@digium.com>
Definition in file netsock.c.
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 176 of file netsock.c.
References ast_netsock_bindaddr(), ast_strdupa, and portno.
Referenced by peer_set_srcaddr(), and set_config().
00177 { 00178 struct sockaddr_in sin; 00179 char *tmp; 00180 char *host; 00181 char *port; 00182 int portno; 00183 00184 memset(&sin, 0, sizeof(sin)); 00185 sin.sin_family = AF_INET; 00186 sin.sin_port = htons(defaultport); 00187 tmp = ast_strdupa(bindinfo); 00188 00189 host = strsep(&tmp, ":"); 00190 port = tmp; 00191 00192 if (port && ((portno = atoi(port)) > 0)) 00193 sin.sin_port = htons(portno); 00194 00195 inet_aton(host, &sin.sin_addr); 00196 00197 return ast_netsock_bindaddr(list, ioc, &sin, tos, callback, data); 00198 }
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 123 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().
00124 { 00125 int netsocket = -1; 00126 int *ioref; 00127 00128 struct ast_netsock *ns; 00129 const int reuseFlag = 1; 00130 00131 /* Make a UDP socket */ 00132 netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); 00133 00134 if (netsocket < 0) { 00135 ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno)); 00136 return NULL; 00137 } 00138 if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) { 00139 ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket); 00140 } 00141 if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) { 00142 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)); 00143 close(netsocket); 00144 return NULL; 00145 } 00146 if (option_verbose > 1) 00147 ast_verbose(VERBOSE_PREFIX_2 "Using TOS bits %d\n", tos); 00148 00149 if (setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))) 00150 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos); 00151 00152 ast_enable_packet_fragmentation(netsocket); 00153 00154 if (!(ns = ast_calloc(1, sizeof(struct ast_netsock)))) { 00155 close(netsocket); 00156 return NULL; 00157 } 00158 00159 /* Establish I/O callback for socket read */ 00160 if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) { 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 00173 return ns; 00174 }
struct sockaddr_in* ast_netsock_boundaddr | ( | const struct ast_netsock * | ns | ) |
Definition at line 205 of file netsock.c.
References ast_netsock::bindaddr.
00206 { 00207 return &(ns->bindaddr); 00208 }
void* ast_netsock_data | ( | const struct ast_netsock * | ns | ) |
Definition at line 210 of file netsock.c.
References ast_netsock::data.
00211 { 00212 return ns->data; 00213 }
static void ast_netsock_destroy | ( | struct ast_netsock * | netsock | ) | [static] |
Definition at line 79 of file netsock.c.
References ast_io_remove(), free, ast_netsock_list::ioc, and netsock.
Referenced by ast_netsock_release(), and ast_netsock_unref().
00080 { 00081 ast_io_remove(netsock->ioc, netsock->ioref); 00082 close(netsock->sockfd); 00083 free(netsock); 00084 }
struct ast_netsock* ast_netsock_find | ( | struct ast_netsock_list * | list, | |
struct sockaddr_in * | sa | |||
) |
Definition at line 108 of file netsock.c.
References ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_RDLOCK, ASTOBJ_UNLOCK, and inaddrcmp().
Referenced by peer_set_srcaddr().
00110 { 00111 struct ast_netsock *sock = NULL; 00112 00113 ASTOBJ_CONTAINER_TRAVERSE(list, !sock, { 00114 ASTOBJ_RDLOCK(iterator); 00115 if (!inaddrcmp(&iterator->bindaddr, sa)) 00116 sock = iterator; 00117 ASTOBJ_UNLOCK(iterator); 00118 }); 00119 00120 return sock; 00121 }
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_free, 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 ast_free(list); 00104 00105 return 0; 00106 }
int ast_netsock_sockfd | ( | const struct ast_netsock * | ns | ) |
Definition at line 200 of file netsock.c.
Referenced by peer_set_srcaddr(), and set_config().
00201 { 00202 return ns ? ns-> sockfd : -1; 00203 }
void ast_netsock_unref | ( | struct ast_netsock * | ns | ) |
Definition at line 215 of file netsock.c.
References ast_netsock_destroy(), and ASTOBJ_UNREF.
Referenced by peer_set_srcaddr(), and set_config().
00216 { 00217 ASTOBJ_UNREF(ns, ast_netsock_destroy); 00218 }