00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <sys/time.h>
00030 #include <signal.h>
00031 #include <errno.h>
00032 #include <unistd.h>
00033 #include <netinet/in.h>
00034 #include <arpa/inet.h>
00035 #include <sys/socket.h>
00036 #include <netdb.h>
00037 #include <net/if.h>
00038 #include <netinet/in_systm.h>
00039 #include <netinet/ip.h>
00040 #include <sys/ioctl.h>
00041
00042 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
00043 #include <fcntl.h>
00044 #include <net/route.h>
00045 #endif
00046
00047 #if defined (SOLARIS)
00048 #include <sys/sockio.h>
00049 #endif
00050
00051 #include "asterisk.h"
00052
00053 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 44955 $")
00054
00055 #include "asterisk/netsock.h"
00056 #include "asterisk/logger.h"
00057 #include "asterisk/channel.h"
00058 #include "asterisk/options.h"
00059 #include "asterisk/utils.h"
00060 #include "asterisk/lock.h"
00061 #include "asterisk/srv.h"
00062
00063 struct ast_netsock {
00064 ASTOBJ_COMPONENTS(struct ast_netsock);
00065 struct sockaddr_in bindaddr;
00066 int sockfd;
00067 int *ioref;
00068 struct io_context *ioc;
00069 void *data;
00070 };
00071
00072 struct ast_netsock_list {
00073 ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock);
00074 struct io_context *ioc;
00075 };
00076
00077 static void ast_netsock_destroy(struct ast_netsock *netsock)
00078 {
00079 ast_io_remove(netsock->ioc, netsock->ioref);
00080 close(netsock->sockfd);
00081 free(netsock);
00082 }
00083
00084 struct ast_netsock_list *ast_netsock_list_alloc(void)
00085 {
00086 struct ast_netsock_list *res;
00087
00088 res = calloc(1, sizeof(*res));
00089
00090 return res;
00091 }
00092
00093 int ast_netsock_init(struct ast_netsock_list *list)
00094 {
00095 memset(list, 0, sizeof(*list));
00096 ASTOBJ_CONTAINER_INIT(list);
00097
00098 return 0;
00099 }
00100
00101 int ast_netsock_release(struct ast_netsock_list *list)
00102 {
00103 ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
00104 ASTOBJ_CONTAINER_DESTROY(list);
00105
00106 return 0;
00107 }
00108
00109 struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list,
00110 struct sockaddr_in *sa)
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 }
00123
00124 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)
00125 {
00126 int netsocket = -1;
00127 int *ioref;
00128 char iabuf[INET_ADDRSTRLEN];
00129
00130 struct ast_netsock *ns;
00131
00132
00133 netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
00134
00135 if (netsocket < 0) {
00136 ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
00137 return NULL;
00138 }
00139 if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) {
00140 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));
00141 close(netsocket);
00142 return NULL;
00143 }
00144 if (option_verbose > 1)
00145 ast_verbose(VERBOSE_PREFIX_2 "Using TOS bits %d\n", tos);
00146
00147 if (setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))
00148 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
00149
00150 ast_enable_packet_fragmentation(netsocket);
00151
00152 ns = malloc(sizeof(struct ast_netsock));
00153 if (ns) {
00154
00155 ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns);
00156 if (!ioref) {
00157 ast_log(LOG_WARNING, "Out of memory!\n");
00158 close(netsocket);
00159 free(ns);
00160 return NULL;
00161 }
00162 ASTOBJ_INIT(ns);
00163 ns->ioref = ioref;
00164 ns->ioc = ioc;
00165 ns->sockfd = netsocket;
00166 ns->data = data;
00167 memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
00168 ASTOBJ_CONTAINER_LINK(list, ns);
00169 } else {
00170 ast_log(LOG_WARNING, "Out of memory!\n");
00171 close(netsocket);
00172 }
00173
00174 return ns;
00175 }
00176
00177 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)
00178 {
00179 struct sockaddr_in sin;
00180 char *tmp;
00181 char *host;
00182 char *port;
00183 int portno;
00184
00185 memset(&sin, 0, sizeof(sin));
00186 sin.sin_family = AF_INET;
00187 sin.sin_port = htons(defaultport);
00188 tmp = ast_strdupa(bindinfo);
00189 if (!tmp) {
00190 ast_log(LOG_WARNING, "Out of memory!\n");
00191 return NULL;
00192 }
00193
00194 host = strsep(&tmp, ":");
00195 port = tmp;
00196
00197 if (port && ((portno = atoi(port)) > 0))
00198 sin.sin_port = htons(portno);
00199
00200 inet_aton(host, &sin.sin_addr);
00201
00202 return ast_netsock_bindaddr(list, ioc, &sin, tos, callback, data);
00203 }
00204
00205 int ast_netsock_sockfd(const struct ast_netsock *ns)
00206 {
00207 return ns ? ns-> sockfd : -1;
00208 }
00209
00210 const struct sockaddr_in *ast_netsock_boundaddr(const struct ast_netsock *ns)
00211 {
00212 return &(ns->bindaddr);
00213 }
00214
00215 void *ast_netsock_data(const struct ast_netsock *ns)
00216 {
00217 return ns->data;
00218 }
00219
00220 void ast_netsock_unref(struct ast_netsock *ns)
00221 {
00222 ASTOBJ_UNREF(ns, ast_netsock_destroy);
00223 }