#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_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) |
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) |
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_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. |
Mark Spencer <markster@digium.com>
Definition in file netsock.c.
Compare two EIDs.
Definition at line 320 of file netsock.c.
Referenced by aji_devstate_cb(), aji_handle_pubsub_event(), aji_mwi_cb(), ast_event_cb(), build_peer(), build_transactions(), destroy_trans(), dundi_answer_entity(), dundi_answer_query(), dundi_ie_append_eid_appropriately(), dundi_lookup_internal(), dundi_query_thread(), evt_event_deliver_cb(), find_peer(), optimize_transactions(), and register_request().
char* ast_eid_to_str | ( | char * | s, | |
int | maxlen, | |||
struct ast_eid * | eid | |||
) |
/brief Convert an EID to a string
Definition at line 222 of file netsock.c.
References ast_eid::eid.
Referenced by aji_publish_device_state(), aji_publish_mwi(), append_transaction(), ast_set_default_eid(), ast_str_retrieve_variable(), 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(), dump_raw_ie(), 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(), populate_addr(), register_request(), and update_key().
00223 { 00224 int x; 00225 char *os = s; 00226 if (maxlen < 18) { 00227 if (s && (maxlen > 0)) 00228 *s = '\0'; 00229 } else { 00230 for (x = 0; x < 5; x++) { 00231 sprintf(s, "%02x:", eid->eid[x]); 00232 s += 3; 00233 } 00234 sprintf(s, "%02x", eid->eid[5]); 00235 } 00236 return os; 00237 }
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 178 of file netsock.c.
References ast_netsock_bindaddr(), ast_strdupa, inet_aton(), and strsep().
Referenced by peer_set_srcaddr(), and set_config().
00179 { 00180 struct sockaddr_in sin; 00181 char *tmp; 00182 char *host; 00183 char *port; 00184 int portno; 00185 00186 memset(&sin, 0, sizeof(sin)); 00187 sin.sin_family = AF_INET; 00188 sin.sin_port = htons(defaultport); 00189 tmp = ast_strdupa(bindinfo); 00190 00191 host = strsep(&tmp, ":"); 00192 port = tmp; 00193 00194 if (port && ((portno = atoi(port)) > 0)) 00195 sin.sin_port = htons(portno); 00196 00197 inet_aton(host, &sin.sin_addr); 00198 00199 return ast_netsock_bindaddr(list, ioc, &sin, tos, cos, callback, data); 00200 }
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 108 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().
00109 { 00110 int netsocket = -1; 00111 int *ioref; 00112 00113 struct ast_netsock *ns; 00114 const int reuseFlag = 1; 00115 00116 /* Make a UDP socket */ 00117 netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); 00118 00119 if (netsocket < 0) { 00120 ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno)); 00121 return NULL; 00122 } 00123 if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) { 00124 ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket); 00125 } 00126 if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) { 00127 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)); 00128 close(netsocket); 00129 return NULL; 00130 } 00131 00132 ast_netsock_set_qos(netsocket, tos, cos, "IAX2"); 00133 00134 ast_enable_packet_fragmentation(netsocket); 00135 00136 if (!(ns = ast_calloc(1, sizeof(*ns)))) { 00137 close(netsocket); 00138 return NULL; 00139 } 00140 00141 /* Establish I/O callback for socket read */ 00142 if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) { 00143 close(netsocket); 00144 ast_free(ns); 00145 return NULL; 00146 } 00147 ASTOBJ_INIT(ns); 00148 ns->ioref = ioref; 00149 ns->ioc = ioc; 00150 ns->sockfd = netsocket; 00151 ns->data = data; 00152 memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr)); 00153 ASTOBJ_CONTAINER_LINK(list, ns); 00154 00155 return ns; 00156 }
struct sockaddr_in* ast_netsock_boundaddr | ( | const struct ast_netsock * | ns | ) |
Definition at line 207 of file netsock.c.
References ast_netsock::bindaddr.
00208 { 00209 return &(ns->bindaddr); 00210 }
void* ast_netsock_data | ( | const struct ast_netsock * | ns | ) |
Definition at line 212 of file netsock.c.
References ast_netsock::data.
00213 { 00214 return ns->data; 00215 }
static void ast_netsock_destroy | ( | struct ast_netsock * | netsock | ) | [static] |
Definition at line 64 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().
00065 { 00066 ast_io_remove(netsock->ioc, netsock->ioref); 00067 close(netsock->sockfd); 00068 ast_free(netsock); 00069 }
struct ast_netsock* ast_netsock_find | ( | struct ast_netsock_list * | list, | |
struct sockaddr_in * | sa | |||
) |
Definition at line 93 of file netsock.c.
References ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_RDLOCK, ASTOBJ_UNLOCK, and inaddrcmp().
Referenced by peer_set_srcaddr().
00095 { 00096 struct ast_netsock *sock = NULL; 00097 00098 ASTOBJ_CONTAINER_TRAVERSE(list, !sock, { 00099 ASTOBJ_RDLOCK(iterator); 00100 if (!inaddrcmp(&iterator->bindaddr, sa)) 00101 sock = iterator; 00102 ASTOBJ_UNLOCK(iterator); 00103 }); 00104 00105 return sock; 00106 }
int ast_netsock_init | ( | struct ast_netsock_list * | list | ) |
Definition at line 76 of file netsock.c.
References ASTOBJ_CONTAINER_INIT.
Referenced by load_module(), and set_config().
00077 { 00078 memset(list, 0, sizeof(*list)); 00079 ASTOBJ_CONTAINER_INIT(list); 00080 00081 return 0; 00082 }
struct ast_netsock_list* ast_netsock_list_alloc | ( | void | ) |
Definition at line 71 of file netsock.c.
References ast_calloc.
Referenced by load_module(), and set_config().
00072 { 00073 return ast_calloc(1, sizeof(struct ast_netsock_list)); 00074 }
int ast_netsock_release | ( | struct ast_netsock_list * | list | ) |
Definition at line 84 of file netsock.c.
References ast_free, ast_netsock_destroy(), ASTOBJ_CONTAINER_DESTROY, and ASTOBJ_CONTAINER_DESTROYALL.
Referenced by __unload_module(), and set_config().
00085 { 00086 ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy); 00087 ASTOBJ_CONTAINER_DESTROY(list); 00088 ast_free(list); 00089 00090 return 0; 00091 }
int ast_netsock_set_qos | ( | int | netsocket, | |
int | tos, | |||
int | cos, | |||
const char * | desc | |||
) |
Definition at line 158 of file netsock.c.
References ast_log(), ast_verb, and LOG_WARNING.
Referenced by ast_netsock_bindaddr(), ast_udptl_setqos(), config_load(), and load_module().
00159 { 00160 int res; 00161 00162 if ((res = setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 00163 ast_log(LOG_WARNING, "Unable to set %s TOS to %d, may be you have no root privileges\n", desc, tos); 00164 else if (tos) 00165 ast_verb(2, "Using %s TOS bits %d\n", desc, tos); 00166 00167 #if defined(linux) 00168 if (setsockopt(netsocket, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos))) 00169 ast_log(LOG_WARNING, "Unable to set %s CoS to %d\n", desc, cos); 00170 else if (cos) 00171 ast_verb(2, "Using %s CoS mark %d\n", desc, cos); 00172 #endif 00173 00174 return res; 00175 }
int ast_netsock_sockfd | ( | const struct ast_netsock * | ns | ) |
Definition at line 202 of file netsock.c.
Referenced by peer_set_srcaddr(), and set_config().
00203 { 00204 return ns ? ns-> sockfd : -1; 00205 }
void ast_netsock_unref | ( | struct ast_netsock * | ns | ) |
Definition at line 217 of file netsock.c.
References ast_netsock_destroy(), and ASTOBJ_UNREF.
Referenced by peer_set_srcaddr(), and set_config().
00218 { 00219 ASTOBJ_UNREF(ns, ast_netsock_destroy); 00220 }
void ast_set_default_eid | ( | struct ast_eid * | eid | ) |
Fill in an ast_eid with the default eid of this machine.
Definition at line 239 of file netsock.c.
References ARRAY_LEN, ast_debug, ast_eid_to_str(), and ast_eid::eid.
Referenced by ast_readconfig().
00240 { 00241 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_IFRU_IFRU_HWADDR) 00242 int s, x = 0; 00243 char eid_str[20]; 00244 struct ifreq ifr; 00245 static const unsigned int MAXIF = 10; 00246 00247 s = socket(AF_INET, SOCK_STREAM, 0); 00248 if (s < 0) 00249 return; 00250 for (x = 0; x < MAXIF; x++) { 00251 static const char *prefixes[] = { "eth", "em" }; 00252 unsigned int i; 00253 00254 for (i = 0; i < ARRAY_LEN(prefixes); i++) { 00255 memset(&ifr, 0, sizeof(ifr)); 00256 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", prefixes[i], x); 00257 if (!ioctl(s, SIOCGIFHWADDR, &ifr)) { 00258 break; 00259 } 00260 } 00261 00262 if (i == ARRAY_LEN(prefixes)) { 00263 /* Try pciX#[1..N] */ 00264 for (i = 0; i < MAXIF; i++) { 00265 memset(&ifr, 0, sizeof(ifr)); 00266 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "pci%u#%u", x, i); 00267 if (!ioctl(s, SIOCGIFHWADDR, &ifr)) { 00268 break; 00269 } 00270 } 00271 if (i == MAXIF) { 00272 continue; 00273 } 00274 } 00275 00276 memcpy(eid, ((unsigned char *)&ifr.ifr_hwaddr) + 2, sizeof(*eid)); 00277 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); 00278 close(s); 00279 return; 00280 } 00281 close(s); 00282 #else 00283 #if defined(ifa_broadaddr) && !defined(SOLARIS) 00284 char eid_str[20]; 00285 struct ifaddrs *ifap; 00286 00287 if (getifaddrs(&ifap) == 0) { 00288 struct ifaddrs *p; 00289 for (p = ifap; p; p = p->ifa_next) { 00290 if ((p->ifa_addr->sa_family == AF_LINK) && !(p->ifa_flags & IFF_LOOPBACK) && (p->ifa_flags & IFF_RUNNING)) { 00291 struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr; 00292 memcpy(&(eid->eid), sdp->sdl_data + sdp->sdl_nlen, 6); 00293 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); 00294 freeifaddrs(ifap); 00295 return; 00296 } 00297 } 00298 freeifaddrs(ifap); 00299 } 00300 #endif 00301 #endif 00302 ast_debug(1, "No ethernet interface found for seeding global EID. You will have to set it manually.\n"); 00303 }
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
Definition at line 305 of file netsock.c.
References ast_eid::eid.
Referenced by aji_handle_pubsub_event(), dundi_do_query(), and set_config().
00306 { 00307 unsigned int eid_int[6]; 00308 int x; 00309 00310 if (sscanf(s, "%2x:%2x:%2x:%2x:%2x:%2x", &eid_int[0], &eid_int[1], &eid_int[2], 00311 &eid_int[3], &eid_int[4], &eid_int[5]) != 6) 00312 return -1; 00313 00314 for (x = 0; x < 6; x++) 00315 eid->eid[x] = eid_int[x]; 00316 00317 return 0; 00318 }