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
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 222876 $")
00031
00032 #if defined (SOLARIS)
00033 #include <sys/sockio.h>
00034 #endif
00035
00036 #include "asterisk/netsock.h"
00037 #include "asterisk/utils.h"
00038
00039 struct ast_netsock {
00040 ASTOBJ_COMPONENTS(struct ast_netsock);
00041 struct sockaddr_in bindaddr;
00042 int sockfd;
00043 int *ioref;
00044 struct io_context *ioc;
00045 void *data;
00046 };
00047
00048 struct ast_netsock_list {
00049 ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock);
00050 struct io_context *ioc;
00051 };
00052
00053 static void ast_netsock_destroy(struct ast_netsock *netsock)
00054 {
00055 ast_io_remove(netsock->ioc, netsock->ioref);
00056 close(netsock->sockfd);
00057 ast_free(netsock);
00058 }
00059
00060 struct ast_netsock_list *ast_netsock_list_alloc(void)
00061 {
00062 return ast_calloc(1, sizeof(struct ast_netsock_list));
00063 }
00064
00065 int ast_netsock_init(struct ast_netsock_list *list)
00066 {
00067 memset(list, 0, sizeof(*list));
00068 ASTOBJ_CONTAINER_INIT(list);
00069
00070 return 0;
00071 }
00072
00073 int ast_netsock_release(struct ast_netsock_list *list)
00074 {
00075 ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
00076 ASTOBJ_CONTAINER_DESTROY(list);
00077 ast_free(list);
00078
00079 return 0;
00080 }
00081
00082 struct ast_netsock *ast_netsock_find(struct ast_netsock_list *list,
00083 struct sockaddr_in *sa)
00084 {
00085 struct ast_netsock *sock = NULL;
00086
00087 ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
00088 ASTOBJ_RDLOCK(iterator);
00089 if (!inaddrcmp(&iterator->bindaddr, sa))
00090 sock = iterator;
00091 ASTOBJ_UNLOCK(iterator);
00092 });
00093
00094 return sock;
00095 }
00096
00097 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)
00098 {
00099 int netsocket = -1;
00100 int *ioref;
00101
00102 struct ast_netsock *ns;
00103 const int reuseFlag = 1;
00104
00105
00106 netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
00107
00108 if (netsocket < 0) {
00109 ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
00110 return NULL;
00111 }
00112 if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
00113 ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
00114 }
00115 if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) {
00116 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));
00117 close(netsocket);
00118 return NULL;
00119 }
00120
00121 ast_netsock_set_qos(netsocket, tos, cos, "IAX2");
00122
00123 ast_enable_packet_fragmentation(netsocket);
00124
00125 if (!(ns = ast_calloc(1, sizeof(*ns)))) {
00126 close(netsocket);
00127 return NULL;
00128 }
00129
00130
00131 if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
00132 close(netsocket);
00133 ast_free(ns);
00134 return NULL;
00135 }
00136 ASTOBJ_INIT(ns);
00137 ns->ioref = ioref;
00138 ns->ioc = ioc;
00139 ns->sockfd = netsocket;
00140 ns->data = data;
00141 memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
00142 ASTOBJ_CONTAINER_LINK(list, ns);
00143
00144 return ns;
00145 }
00146
00147 int ast_netsock_set_qos(int netsocket, int tos, int cos, const char *desc)
00148 {
00149 int res;
00150
00151 if ((res = setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
00152 ast_log(LOG_WARNING, "Unable to set %s TOS to %d, may be you have no root privileges\n", desc, tos);
00153 else if (tos)
00154 ast_verb(2, "Using %s TOS bits %d\n", desc, tos);
00155
00156 #if defined(linux)
00157 if (setsockopt(netsocket, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos)))
00158 ast_log(LOG_WARNING, "Unable to set %s CoS to %d\n", desc, cos);
00159 else if (cos)
00160 ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
00161 #endif
00162
00163 return res;
00164 }
00165
00166
00167 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)
00168 {
00169 struct sockaddr_in sin;
00170 char *tmp;
00171 char *host;
00172 char *port;
00173 int portno;
00174
00175 memset(&sin, 0, sizeof(sin));
00176 sin.sin_family = AF_INET;
00177 sin.sin_port = htons(defaultport);
00178 tmp = ast_strdupa(bindinfo);
00179
00180 host = strsep(&tmp, ":");
00181 port = tmp;
00182
00183 if (port && ((portno = atoi(port)) > 0))
00184 sin.sin_port = htons(portno);
00185
00186 inet_aton(host, &sin.sin_addr);
00187
00188 return ast_netsock_bindaddr(list, ioc, &sin, tos, cos, callback, data);
00189 }
00190
00191 int ast_netsock_sockfd(const struct ast_netsock *ns)
00192 {
00193 return ns ? ns-> sockfd : -1;
00194 }
00195
00196 const struct sockaddr_in *ast_netsock_boundaddr(const struct ast_netsock *ns)
00197 {
00198 return &(ns->bindaddr);
00199 }
00200
00201 void *ast_netsock_data(const struct ast_netsock *ns)
00202 {
00203 return ns->data;
00204 }
00205
00206 void ast_netsock_unref(struct ast_netsock *ns)
00207 {
00208 ASTOBJ_UNREF(ns, ast_netsock_destroy);
00209 }