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 "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <sys/time.h>
00034 #include <signal.h>
00035 #include <errno.h>
00036 #include <unistd.h>
00037 #include <netinet/in.h>
00038 #include <arpa/inet.h>
00039 #include <sys/socket.h>
00040 #include <netdb.h>
00041 #include <net/if.h>
00042 #include <netinet/in.h>
00043 #include <netinet/in_systm.h>
00044 #include <netinet/ip.h>
00045 #include <sys/ioctl.h>
00046
00047 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
00048 #include <fcntl.h>
00049 #include <net/route.h>
00050 #endif
00051
00052 #if defined(SOLARIS)
00053 #include <sys/sockio.h>
00054 #endif
00055
00056
00057 #if !defined(IPTOS_LOWCOST)
00058 #define IPTOS_LOWCOST 0x02
00059 #endif
00060
00061 #if !defined(IPTOS_MINCOST)
00062 #define IPTOS_MINCOST IPTOS_LOWCOST
00063 #endif
00064
00065 #include "asterisk/acl.h"
00066 #include "asterisk/logger.h"
00067 #include "asterisk/channel.h"
00068 #include "asterisk/options.h"
00069 #include "asterisk/utils.h"
00070 #include "asterisk/lock.h"
00071 #include "asterisk/srv.h"
00072
00073 struct ast_ha {
00074
00075 struct in_addr netaddr;
00076 struct in_addr netmask;
00077 int sense;
00078 struct ast_ha *next;
00079 };
00080
00081
00082 static struct in_addr __ourip = { .s_addr = 0x00000000, };
00083
00084 struct my_ifreq {
00085 char ifrn_name[IFNAMSIZ];
00086 struct sockaddr_in ifru_addr;
00087 };
00088
00089
00090 void ast_free_ha(struct ast_ha *ha)
00091 {
00092 struct ast_ha *hal;
00093 while (ha) {
00094 hal = ha;
00095 ha = ha->next;
00096 free(hal);
00097 }
00098 }
00099
00100
00101 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
00102 {
00103 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
00104 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
00105 to->sense = from->sense;
00106 }
00107
00108
00109 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
00110 {
00111 struct ast_ha *new_ha;
00112
00113 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
00114
00115 ast_copy_ha(original, new_ha);
00116 }
00117
00118 return new_ha;
00119 }
00120
00121
00122
00123 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
00124 {
00125 struct ast_ha *start = original;
00126 struct ast_ha *ret = NULL;
00127 struct ast_ha *link, *prev = NULL;
00128
00129 while (start) {
00130 link = ast_duplicate_ha(start);
00131 if (prev)
00132 prev->next = link;
00133
00134 if (!ret)
00135 ret = link;
00136
00137 start = start->next;
00138 prev = link;
00139 }
00140 return ret;
00141 }
00142
00143 struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path)
00144 {
00145 struct ast_ha *ha;
00146 char *nm = "255.255.255.255";
00147 char tmp[256];
00148 struct ast_ha *prev = NULL;
00149 struct ast_ha *ret;
00150 int x, z;
00151 unsigned int y;
00152
00153 ret = path;
00154 while (path) {
00155 prev = path;
00156 path = path->next;
00157 }
00158 if ((ha = ast_malloc(sizeof(*ha)))) {
00159 ast_copy_string(tmp, stuff, sizeof(tmp));
00160 nm = strchr(tmp, '/');
00161 if (!nm) {
00162 nm = "255.255.255.255";
00163 } else {
00164 *nm = '\0';
00165 nm++;
00166 }
00167 if (!strchr(nm, '.')) {
00168 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32)) {
00169 y = 0;
00170 for (z = 0; z < x; z++) {
00171 y >>= 1;
00172 y |= 0x80000000;
00173 }
00174 ha->netmask.s_addr = htonl(y);
00175 }
00176 } else if (!inet_aton(nm, &ha->netmask)) {
00177 ast_log(LOG_WARNING, "%s is not a valid netmask\n", nm);
00178 free(ha);
00179 return ret;
00180 }
00181 if (!inet_aton(tmp, &ha->netaddr)) {
00182 ast_log(LOG_WARNING, "%s is not a valid IP\n", tmp);
00183 free(ha);
00184 return ret;
00185 }
00186 ha->netaddr.s_addr &= ha->netmask.s_addr;
00187 if (!strncasecmp(sense, "p", 1)) {
00188 ha->sense = AST_SENSE_ALLOW;
00189 } else {
00190 ha->sense = AST_SENSE_DENY;
00191 }
00192 ha->next = NULL;
00193 if (prev) {
00194 prev->next = ha;
00195 } else {
00196 ret = ha;
00197 }
00198 }
00199 if (option_debug)
00200 ast_log(LOG_DEBUG, "%s/%s appended to acl for peer\n", stuff, nm);
00201 return ret;
00202 }
00203
00204 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
00205 {
00206
00207 int res = AST_SENSE_ALLOW;
00208 while (ha) {
00209 char iabuf[INET_ADDRSTRLEN];
00210 char iabuf2[INET_ADDRSTRLEN];
00211
00212 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
00213 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
00214 if (option_debug)
00215 ast_log(LOG_DEBUG, "##### Testing %s with %s\n", iabuf, iabuf2);
00216
00217
00218 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
00219 res = ha->sense;
00220 ha = ha->next;
00221 }
00222 return res;
00223 }
00224
00225 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
00226 {
00227 struct hostent *hp;
00228 struct ast_hostent ahp;
00229 char srv[256];
00230 char host[256];
00231 int tportno = ntohs(sin->sin_port);
00232 if (inet_aton(value, &sin->sin_addr))
00233 return 0;
00234 if (service) {
00235 snprintf(srv, sizeof(srv), "%s.%s", service, value);
00236 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
00237 sin->sin_port = htons(tportno);
00238 value = host;
00239 }
00240 }
00241 hp = ast_gethostbyname(value, &ahp);
00242 if (hp) {
00243 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
00244 } else {
00245 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
00246 return -1;
00247 }
00248 return 0;
00249 }
00250
00251 struct dscp_codepoint {
00252 char *name;
00253 unsigned int space;
00254 };
00255
00256
00257
00258 static const struct dscp_codepoint dscp_pool1[] = {
00259 { "CS0", 0x00 },
00260 { "CS1", 0x08 },
00261 { "CS2", 0x10 },
00262 { "CS3", 0x18 },
00263 { "CS4", 0x20 },
00264 { "CS5", 0x28 },
00265 { "CS6", 0x30 },
00266 { "CS7", 0x38 },
00267 { "AF11", 0x0A },
00268 { "AF12", 0x0C },
00269 { "AF13", 0x0E },
00270 { "AF21", 0x12 },
00271 { "AF22", 0x14 },
00272 { "AF23", 0x16 },
00273 { "AF31", 0x1A },
00274 { "AF32", 0x1C },
00275 { "AF33", 0x1E },
00276 { "AF41", 0x22 },
00277 { "AF42", 0x24 },
00278 { "AF43", 0x26 },
00279 { "EF", 0x2E },
00280 };
00281
00282 int ast_str2tos(const char *value, unsigned int *tos)
00283 {
00284 int fval;
00285 unsigned int x;
00286
00287 if (sscanf(value, "%i", &fval) == 1) {
00288 *tos = fval & 0xFF;
00289 return 0;
00290 }
00291
00292 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
00293 if (!strcasecmp(value, dscp_pool1[x].name)) {
00294 *tos = dscp_pool1[x].space << 2;
00295 return 0;
00296 }
00297 }
00298
00299 if (!strcasecmp(value, "lowdelay"))
00300 *tos = IPTOS_LOWDELAY;
00301 else if (!strcasecmp(value, "throughput"))
00302 *tos = IPTOS_THROUGHPUT;
00303 else if (!strcasecmp(value, "reliability"))
00304 *tos = IPTOS_RELIABILITY;
00305 else if (!strcasecmp(value, "mincost"))
00306 *tos = IPTOS_MINCOST;
00307 else if (!strcasecmp(value, "none"))
00308 *tos = 0;
00309 else
00310 return -1;
00311
00312 ast_log(LOG_WARNING, "TOS value %s is deprecated. Please see doc/ip-tos.txt for more information.\n", value);
00313
00314 return 0;
00315 }
00316
00317 const char *ast_tos2str(unsigned int tos)
00318 {
00319 unsigned int x;
00320
00321 switch (tos) {
00322 case 0:
00323 return "none";
00324 case IPTOS_LOWDELAY:
00325 return "lowdelay";
00326 case IPTOS_THROUGHPUT:
00327 return "throughput";
00328 case IPTOS_RELIABILITY:
00329 return "reliability";
00330 case IPTOS_MINCOST:
00331 return "mincost";
00332 default:
00333 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
00334 if (dscp_pool1[x].space == (tos >> 2))
00335 return dscp_pool1[x].name;
00336 }
00337 }
00338
00339 return "unknown";
00340 }
00341
00342 int ast_get_ip(struct sockaddr_in *sin, const char *value)
00343 {
00344 return ast_get_ip_or_srv(sin, value, NULL);
00345 }
00346
00347
00348 int ast_lookup_iface(char *iface, struct in_addr *address)
00349 {
00350 int mysock, res = 0;
00351 struct my_ifreq ifreq;
00352
00353 memset(&ifreq, 0, sizeof(ifreq));
00354 ast_copy_string(ifreq.ifrn_name, iface, sizeof(ifreq.ifrn_name));
00355
00356 mysock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
00357 res = ioctl(mysock, SIOCGIFADDR, &ifreq);
00358
00359 close(mysock);
00360 if (res < 0) {
00361 ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
00362 memcpy((char *)address, (char *)&__ourip, sizeof(__ourip));
00363 return -1;
00364 } else {
00365 memcpy((char *)address, (char *)&ifreq.ifru_addr.sin_addr, sizeof(ifreq.ifru_addr.sin_addr));
00366 return 0;
00367 }
00368 }
00369
00370 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
00371 {
00372 int s;
00373 struct sockaddr_in sin;
00374 socklen_t slen;
00375
00376 s = socket(PF_INET, SOCK_DGRAM, 0);
00377 if (s < 0) {
00378 ast_log(LOG_WARNING, "Cannot create socket\n");
00379 return -1;
00380 }
00381 sin.sin_family = AF_INET;
00382 sin.sin_port = 5060;
00383 sin.sin_addr = *them;
00384 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
00385 ast_log(LOG_WARNING, "Cannot connect\n");
00386 close(s);
00387 return -1;
00388 }
00389 slen = sizeof(sin);
00390 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
00391 ast_log(LOG_WARNING, "Cannot get socket name\n");
00392 close(s);
00393 return -1;
00394 }
00395 close(s);
00396 *us = sin.sin_addr;
00397 return 0;
00398 }
00399
00400 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
00401 {
00402 char ourhost[MAXHOSTNAMELEN] = "";
00403 struct ast_hostent ahp;
00404 struct hostent *hp;
00405 struct in_addr saddr;
00406
00407
00408 if (ntohl(bindaddr.sin_addr.s_addr)) {
00409 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
00410 return 0;
00411 }
00412
00413 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
00414 ast_log(LOG_WARNING, "Unable to get hostname\n");
00415 } else {
00416 hp = ast_gethostbyname(ourhost, &ahp);
00417 if (hp) {
00418 memcpy(ourip, hp->h_addr, sizeof(*ourip));
00419 return 0;
00420 }
00421 }
00422
00423 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
00424 return 0;
00425 return -1;
00426 }
00427