#include "asterisk.h"
#include "asterisk/netsock.h"
#include "asterisk/utils.h"
Go to the source code of this file.
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, | |||
int | cos, | |||
ast_io_cb | callback, | |||
void * | data | |||
) |
Definition at line 166 of file netsock.c.
References ast_netsock_bindaddr(), ast_strdupa, inet_aton(), and strsep().
Referenced by peer_set_srcaddr(), and set_config().
00167 { 00168 struct sockaddr_in sin; 00169 char *tmp; 00170 char *host; 00171 char *port; 00172 int portno; 00173 00174 memset(&sin, 0, sizeof(sin)); 00175 sin.sin_family = AF_INET; 00176 sin.sin_port = htons(defaultport); 00177 tmp = ast_strdupa(bindinfo); 00178 00179 host = strsep(&tmp, ":"); 00180 port = tmp; 00181 00182 if (port && ((portno = atoi(port)) > 0)) 00183 sin.sin_port = htons(portno); 00184 00185 inet_aton(host, &sin.sin_addr); 00186 00187 return ast_netsock_bindaddr(list, ioc, &sin, tos, cos, callback, data); 00188 }
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 96 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().
00097 { 00098 int netsocket = -1; 00099 int *ioref; 00100 00101 struct ast_netsock *ns; 00102 const int reuseFlag = 1; 00103 00104 /* Make a UDP socket */ 00105 netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); 00106 00107 if (netsocket < 0) { 00108 ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno)); 00109 return NULL; 00110 } 00111 if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) { 00112 ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket); 00113 } 00114 if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) { 00115 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)); 00116 close(netsocket); 00117 return NULL; 00118 } 00119 00120 ast_netsock_set_qos(netsocket, tos, cos, "IAX2"); 00121 00122 ast_enable_packet_fragmentation(netsocket); 00123 00124 if (!(ns = ast_calloc(1, sizeof(*ns)))) { 00125 close(netsocket); 00126 return NULL; 00127 } 00128 00129 /* Establish I/O callback for socket read */ 00130 if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) { 00131 close(netsocket); 00132 ast_free(ns); 00133 return NULL; 00134 } 00135 ASTOBJ_INIT(ns); 00136 ns->ioref = ioref; 00137 ns->ioc = ioc; 00138 ns->sockfd = netsocket; 00139 ns->data = data; 00140 memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr)); 00141 ASTOBJ_CONTAINER_LINK(list, ns); 00142 00143 return ns; 00144 }
struct sockaddr_in* ast_netsock_boundaddr | ( | const struct ast_netsock * | ns | ) |
Definition at line 195 of file netsock.c.
References ast_netsock::bindaddr.
00196 { 00197 return &(ns->bindaddr); 00198 }
void* ast_netsock_data | ( | const struct ast_netsock * | ns | ) |
Definition at line 200 of file netsock.c.
References ast_netsock::data.
00201 { 00202 return ns->data; 00203 }
static void ast_netsock_destroy | ( | struct ast_netsock * | netsock | ) | [static] |
Definition at line 53 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().
00054 { 00055 ast_io_remove(netsock->ioc, netsock->ioref); 00056 close(netsock->sockfd); 00057 ast_free(netsock); 00058 }
struct ast_netsock* ast_netsock_find | ( | struct ast_netsock_list * | list, | |
struct sockaddr_in * | sa | |||
) |
Definition at line 81 of file netsock.c.
References ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_RDLOCK, ASTOBJ_UNLOCK, and inaddrcmp().
Referenced by peer_set_srcaddr().
00083 { 00084 struct ast_netsock *sock = NULL; 00085 00086 ASTOBJ_CONTAINER_TRAVERSE(list, !sock, { 00087 ASTOBJ_RDLOCK(iterator); 00088 if (!inaddrcmp(&iterator->bindaddr, sa)) 00089 sock = iterator; 00090 ASTOBJ_UNLOCK(iterator); 00091 }); 00092 00093 return sock; 00094 }
int ast_netsock_init | ( | struct ast_netsock_list * | list | ) |
Definition at line 65 of file netsock.c.
References ASTOBJ_CONTAINER_INIT.
Referenced by load_module(), and set_config().
00066 { 00067 memset(list, 0, sizeof(*list)); 00068 ASTOBJ_CONTAINER_INIT(list); 00069 00070 return 0; 00071 }
struct ast_netsock_list* ast_netsock_list_alloc | ( | void | ) |
Definition at line 60 of file netsock.c.
References ast_calloc.
Referenced by load_module(), and set_config().
00061 { 00062 return ast_calloc(1, sizeof(struct ast_netsock_list)); 00063 }
int ast_netsock_release | ( | struct ast_netsock_list * | list | ) |
Definition at line 73 of file netsock.c.
References ast_netsock_destroy(), ASTOBJ_CONTAINER_DESTROY, and ASTOBJ_CONTAINER_DESTROYALL.
Referenced by __unload_module(), and set_config().
00074 { 00075 ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy); 00076 ASTOBJ_CONTAINER_DESTROY(list); 00077 00078 return 0; 00079 }
int ast_netsock_set_qos | ( | int | netsocket, | |
int | tos, | |||
int | cos, | |||
const char * | desc | |||
) |
Definition at line 146 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().
00147 { 00148 int res; 00149 00150 if ((res = setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 00151 ast_log(LOG_WARNING, "Unable to set %s TOS to %d, may be you have no root privileges\n", desc, tos); 00152 else if (tos) 00153 ast_verb(2, "Using %s TOS bits %d\n", desc, tos); 00154 00155 #if defined(linux) 00156 if (setsockopt(netsocket, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos))) 00157 ast_log(LOG_WARNING, "Unable to set %s CoS to %d\n", desc, cos); 00158 else if (cos) 00159 ast_verb(2, "Using %s CoS mark %d\n", desc, cos); 00160 #endif 00161 00162 return res; 00163 }
int ast_netsock_sockfd | ( | const struct ast_netsock * | ns | ) |
Definition at line 190 of file netsock.c.
Referenced by peer_set_srcaddr(), and set_config().
00191 { 00192 return ns ? ns-> sockfd : -1; 00193 }
void ast_netsock_unref | ( | struct ast_netsock * | ns | ) |
Definition at line 205 of file netsock.c.
References ast_netsock_destroy(), and ASTOBJ_UNREF.
Referenced by peer_set_srcaddr(), and set_config().
00206 { 00207 ASTOBJ_UNREF(ns, ast_netsock_destroy); 00208 }