| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Kevin P. Fleming <kpfleming@digium.com> | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief Network socket handling | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \author Kevin P. Fleming <kpfleming@digium.com> | 
					
						
							|  |  |  |  * \author Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-15 16:20:16 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 11:18:49 +02:00
										 |  |  | #if !defined (__linux__) && !defined (__GNU__)
 | 
					
						
							| 
									
										
										
										
											2010-08-15 13:08:45 +00:00
										 |  |  | #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__) || defined(__GLIBC__)
 | 
					
						
							| 
									
										
										
										
											2008-06-19 20:25:37 +00:00
										 |  |  | #include <net/if_dl.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-08-15 13:08:45 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-06-19 20:25:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | #if defined (SOLARIS)
 | 
					
						
							|  |  |  | #include <sys/sockio.h>
 | 
					
						
							| 
									
										
										
										
											2008-06-19 20:25:37 +00:00
										 |  |  | #elif defined(HAVE_GETIFADDRS)
 | 
					
						
							|  |  |  | #include <ifaddrs.h>
 | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk/netsock.h"
 | 
					
						
							| 
									
										
										
										
											2012-02-27 14:57:23 +00:00
										 |  |  | #include "asterisk/netsock2.h"
 | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | #include "asterisk/utils.h"
 | 
					
						
							| 
									
										
										
										
											2008-03-20 17:45:29 +00:00
										 |  |  | #include "asterisk/astobj.h"
 | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct ast_netsock { | 
					
						
							|  |  |  | 	ASTOBJ_COMPONENTS(struct ast_netsock); | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 	struct ast_sockaddr bindaddr; | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 	int sockfd; | 
					
						
							|  |  |  | 	int *ioref; | 
					
						
							|  |  |  | 	struct io_context *ioc; | 
					
						
							|  |  |  | 	void *data; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ast_netsock_list { | 
					
						
							|  |  |  | 	ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock); | 
					
						
							|  |  |  | 	struct io_context *ioc; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void ast_netsock_destroy(struct ast_netsock *netsock) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ast_io_remove(netsock->ioc, netsock->ioref); | 
					
						
							|  |  |  | 	close(netsock->sockfd); | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 	ast_free(netsock); | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ast_netsock_list *ast_netsock_list_alloc(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-05-10 19:45:45 +00:00
										 |  |  | 	return ast_calloc(1, sizeof(struct ast_netsock_list)); | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_netsock_init(struct ast_netsock_list *list) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	memset(list, 0, sizeof(*list)); | 
					
						
							|  |  |  | 	ASTOBJ_CONTAINER_INIT(list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_netsock_release(struct ast_netsock_list *list) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy); | 
					
						
							|  |  |  | 	ASTOBJ_CONTAINER_DESTROY(list); | 
					
						
							| 
									
										
										
										
											2009-10-08 19:35:30 +00:00
										 |  |  | 	ast_free(list); | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list, struct ast_sockaddr *addr) | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct ast_netsock *sock = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ASTOBJ_CONTAINER_TRAVERSE(list, !sock, { | 
					
						
							|  |  |  | 		ASTOBJ_RDLOCK(iterator); | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 		if (!ast_sockaddr_cmp(&iterator->bindaddr, addr)) { | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 			sock = iterator; | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 		ASTOBJ_UNLOCK(iterator); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return sock; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | struct ast_netsock *ast_netsock_bindaddr(struct ast_netsock_list *list, struct io_context *ioc, struct ast_sockaddr *bindaddr, int tos, int cos, ast_io_cb callback, void *data) | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int netsocket = -1; | 
					
						
							|  |  |  | 	int *ioref; | 
					
						
							| 
									
										
										
										
											2012-02-27 13:45:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 	struct ast_netsock *ns; | 
					
						
							| 
									
										
										
										
											2007-04-02 16:08:33 +00:00
										 |  |  | 	const int reuseFlag = 1; | 
					
						
							| 
									
										
										
										
											2012-02-27 13:45:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 	/* Make a UDP socket */ | 
					
						
							| 
									
										
										
										
											2013-10-04 21:41:58 +00:00
										 |  |  | 	netsocket = socket(ast_sockaddr_is_ipv6(bindaddr) ? AST_AF_INET6 : AST_AF_INET, SOCK_DGRAM, IPPROTO_IP); | 
					
						
							| 
									
										
										
										
											2012-02-27 13:45:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 	if (netsocket < 0) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno)); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-04-02 16:08:33 +00:00
										 |  |  | 	if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) { | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket); | 
					
						
							| 
									
										
										
										
											2007-04-02 16:08:33 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 	if (ast_bind(netsocket, bindaddr)) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, | 
					
						
							|  |  |  | 			"Unable to bind to %s: %s\n", | 
					
						
							|  |  |  | 			ast_sockaddr_stringify(bindaddr), | 
					
						
							|  |  |  | 			strerror(errno)); | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 		close(netsocket); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-27 14:57:23 +00:00
										 |  |  | 	ast_set_qos(netsocket, tos, cos, "IAX2"); | 
					
						
							| 
									
										
										
										
											2012-02-27 13:45:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-12 18:43:52 +00:00
										 |  |  | 	ast_enable_packet_fragmentation(netsocket); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 	if (!(ns = ast_calloc(1, sizeof(*ns)))) { | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 		close(netsocket); | 
					
						
							| 
									
										
										
										
											2006-05-10 19:45:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-02-27 13:45:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-10 19:45:45 +00:00
										 |  |  | 	/* Establish I/O callback for socket read */ | 
					
						
							|  |  |  | 	if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) { | 
					
						
							|  |  |  | 		close(netsocket); | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free(ns); | 
					
						
							| 
									
										
										
										
											2006-05-10 19:45:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2012-02-27 13:45:10 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-05-10 19:45:45 +00:00
										 |  |  | 	ASTOBJ_INIT(ns); | 
					
						
							|  |  |  | 	ns->ioref = ioref; | 
					
						
							|  |  |  | 	ns->ioc = ioc; | 
					
						
							|  |  |  | 	ns->sockfd = netsocket; | 
					
						
							|  |  |  | 	ns->data = data; | 
					
						
							| 
									
										
										
										
											2013-10-04 21:41:58 +00:00
										 |  |  | 	ast_sockaddr_copy(&ns->bindaddr, bindaddr); | 
					
						
							| 
									
										
										
										
											2006-05-10 19:45:45 +00:00
										 |  |  | 	ASTOBJ_CONTAINER_LINK(list, ns); | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return ns; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-27 14:57:23 +00:00
										 |  |  | int ast_netsock_set_qos(int sockfd, int tos, int cos, const char *desc) | 
					
						
							| 
									
										
										
										
											2007-04-30 16:16:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-02-27 14:57:23 +00:00
										 |  |  | 	return ast_set_qos(sockfd, tos, cos, desc); | 
					
						
							| 
									
										
										
										
											2007-04-30 16:16:26 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-02-27 13:45:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-30 16:16:26 +00:00
										 |  |  | 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) | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 	struct ast_sockaddr addr; | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 	if (ast_sockaddr_parse(&addr, bindinfo, 0)) { | 
					
						
							| 
									
										
										
										
											2012-02-27 17:03:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 		if (!ast_sockaddr_port(&addr)) { | 
					
						
							|  |  |  | 			ast_sockaddr_set_port(&addr, defaultport); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 		return ast_netsock_bindaddr(list, ioc, &addr, tos, cos, callback, data); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_netsock_sockfd(const struct ast_netsock *ns) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return ns ? ns-> sockfd : -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | const struct ast_sockaddr *ast_netsock_boundaddr(const struct ast_netsock *ns) | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-02-27 16:31:24 +00:00
										 |  |  | 	return &ns->bindaddr; | 
					
						
							| 
									
										
										
										
											2005-07-19 23:17:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void *ast_netsock_data(const struct ast_netsock *ns) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return ns->data; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2005-10-31 23:16:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void ast_netsock_unref(struct ast_netsock *ns) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ASTOBJ_UNREF(ns, ast_netsock_destroy); | 
					
						
							|  |  |  | } |