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: 248948 $")
00029
00030 #include "asterisk/network.h"
00031
00032 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__)
00033 #include <fcntl.h>
00034 #include <net/route.h>
00035 #endif
00036
00037 #if defined(SOLARIS)
00038 #include <sys/sockio.h>
00039 #include <net/if.h>
00040 #elif defined(HAVE_GETIFADDRS)
00041 #include <ifaddrs.h>
00042 #endif
00043
00044 #include "asterisk/acl.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/utils.h"
00047 #include "asterisk/lock.h"
00048 #include "asterisk/srv.h"
00049
00050 #if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS))
00051 static int get_local_address(struct in_addr *ourip)
00052 {
00053 return -1;
00054 }
00055 #else
00056 static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score)
00057 {
00058 const char *address;
00059 int score;
00060
00061 address = ast_inet_ntoa(sin->sin_addr);
00062
00063
00064 if (address[0] == '0')
00065 score = -25;
00066
00067 else if (strncmp(address, "127", 3) == 0)
00068 score = -20;
00069
00070 else if (strncmp(address, "10.", 3) == 0)
00071 score = -5;
00072
00073 else if (strncmp(address, "172", 3) == 0) {
00074
00075 if (address[4] == '1' && address[5] >= '6' && address[6] == '.')
00076 score = -5;
00077
00078 else if (address[4] == '2' && address[6] == '.')
00079 score = -5;
00080
00081 else if (address[4] == '3' && address[5] <= '1')
00082 score = -5;
00083
00084 else
00085 score = 0;
00086
00087 } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.')
00088 score = -10;
00089
00090 else if (strncmp(address, "192.168", 7) == 0)
00091 score = -5;
00092
00093 else if (strncmp(address, "169.254", 7) == 0)
00094
00095
00096
00097
00098
00099 score = -10;
00100
00101 else if (strncmp(address, "192.0.2.", 8) == 0)
00102 score = -15;
00103
00104 else
00105 score = 0;
00106
00107 if (score > *best_score) {
00108 *best_score = score;
00109 memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr));
00110 }
00111 }
00112
00113 static int get_local_address(struct in_addr *ourip)
00114 {
00115 int s, res = -1;
00116 #ifdef SOLARIS
00117 struct lifreq *ifr = NULL;
00118 struct lifnum ifn;
00119 struct lifconf ifc;
00120 struct sockaddr_in *sa;
00121 char *buf = NULL;
00122 int bufsz, x;
00123 #endif
00124 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
00125 struct ifaddrs *ifap, *ifaphead;
00126 int rtnerr;
00127 const struct sockaddr_in *sin;
00128 #endif
00129 struct in_addr best_addr;
00130 int best_score = -100;
00131 memset(&best_addr, 0, sizeof(best_addr));
00132
00133 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
00134 rtnerr = getifaddrs(&ifaphead);
00135 if (rtnerr) {
00136 perror(NULL);
00137 return -1;
00138 }
00139 #endif
00140
00141 s = socket(AF_INET, SOCK_STREAM, 0);
00142
00143 if (s > 0) {
00144 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
00145 for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
00146
00147 if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET) {
00148 sin = (const struct sockaddr_in *) ifap->ifa_addr;
00149 score_address(sin, &best_addr, &best_score);
00150 res = 0;
00151
00152 if (best_score == 0)
00153 break;
00154 }
00155 }
00156 #endif
00157
00158
00159 #ifdef SOLARIS
00160
00161 ifn.lifn_family = AF_INET;
00162 ifn.lifn_flags = 0;
00163 ifn.lifn_count = 0;
00164 if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) {
00165 close(s);
00166 return -1;
00167 }
00168
00169 bufsz = ifn.lifn_count * sizeof(struct lifreq);
00170 if (!(buf = malloc(bufsz))) {
00171 close(s);
00172 return -1;
00173 }
00174 memset(buf, 0, bufsz);
00175
00176
00177 ifc.lifc_len = bufsz;
00178 ifc.lifc_buf = buf;
00179 ifc.lifc_family = AF_INET;
00180 ifc.lifc_flags = 0;
00181 if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) {
00182 close(s);
00183 free(buf);
00184 return -1;
00185 }
00186
00187 for (ifr = ifc.lifc_req, x = 0; x < ifn.lifn_count; ifr++, x++) {
00188 sa = (struct sockaddr_in *)&(ifr->lifr_addr);
00189 score_address(sa, &best_addr, &best_score);
00190 res = 0;
00191
00192 if (best_score == 0)
00193 break;
00194 }
00195
00196 free(buf);
00197 #endif
00198
00199 close(s);
00200 }
00201 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
00202 freeifaddrs(ifaphead);
00203 #endif
00204
00205 if (res == 0 && ourip)
00206 memcpy(ourip, &best_addr, sizeof(*ourip));
00207 return res;
00208 }
00209 #endif
00210
00211
00212 void ast_free_ha(struct ast_ha *ha)
00213 {
00214 struct ast_ha *hal;
00215 while (ha) {
00216 hal = ha;
00217 ha = ha->next;
00218 ast_free(hal);
00219 }
00220 }
00221
00222
00223 void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to)
00224 {
00225 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
00226 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
00227 to->sense = from->sense;
00228 }
00229
00230
00231 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
00232 {
00233 struct ast_ha *new_ha;
00234
00235 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
00236
00237 ast_copy_ha(original, new_ha);
00238 }
00239
00240 return new_ha;
00241 }
00242
00243
00244
00245 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
00246 {
00247 struct ast_ha *start = original;
00248 struct ast_ha *ret = NULL;
00249 struct ast_ha *current, *prev = NULL;
00250
00251 while (start) {
00252 current = ast_duplicate_ha(start);
00253 if (prev)
00254 prev->next = current;
00255
00256 if (!ret)
00257 ret = current;
00258
00259 start = start->next;
00260 prev = current;
00261 }
00262 return ret;
00263 }
00264
00265 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
00266 {
00267 struct ast_ha *ha;
00268 char *nm;
00269 struct ast_ha *prev = NULL;
00270 struct ast_ha *ret;
00271 int x;
00272 char *tmp = ast_strdupa(stuff);
00273
00274 ret = path;
00275 while (path) {
00276 prev = path;
00277 path = path->next;
00278 }
00279
00280 ha = ast_malloc(sizeof(*ha));
00281 if (!ha)
00282 return ret;
00283
00284 nm = strchr(tmp, '/');
00285 if (!nm) {
00286
00287
00288 ha->netmask.s_addr = htonl(0xFFFFFFFF);
00289 } else {
00290 *nm = '\0';
00291 nm++;
00292
00293 if (!strchr(nm, '.')) {
00294 if ((sscanf(nm, "%30d", &x) == 1) && (x >= 0) && (x <= 32))
00295 if (x == 0) {
00296
00297
00298
00299 ha->netmask.s_addr = 0;
00300 } else {
00301 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
00302 }
00303 else {
00304 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
00305 ast_free(ha);
00306 if (error)
00307 *error = 1;
00308 return ret;
00309 }
00310 } else if (!inet_aton(nm, &ha->netmask)) {
00311 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
00312 ast_free(ha);
00313 if (error)
00314 *error = 1;
00315 return ret;
00316 }
00317 }
00318
00319 if (!inet_aton(tmp, &ha->netaddr)) {
00320 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
00321 ast_free(ha);
00322 if (error)
00323 *error = 1;
00324 return ret;
00325 }
00326
00327 ha->netaddr.s_addr &= ha->netmask.s_addr;
00328
00329 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
00330
00331 ha->next = NULL;
00332 if (prev) {
00333 prev->next = ha;
00334 } else {
00335 ret = ha;
00336 }
00337
00338 ast_debug(1, "%s/%s sense %d appended to acl for peer\n", ast_strdupa(ast_inet_ntoa(ha->netaddr)), ast_strdupa(ast_inet_ntoa(ha->netmask)), ha->sense);
00339
00340 return ret;
00341 }
00342
00343 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
00344 {
00345
00346 int res = AST_SENSE_ALLOW;
00347 while (ha) {
00348 #if 0
00349 char iabuf[INET_ADDRSTRLEN];
00350 char iabuf2[INET_ADDRSTRLEN];
00351
00352 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
00353 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
00354 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
00355 #endif
00356
00357
00358 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
00359 res = ha->sense;
00360 ha = ha->next;
00361 }
00362 return res;
00363 }
00364
00365 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
00366 {
00367 struct hostent *hp;
00368 struct ast_hostent ahp;
00369 char srv[256];
00370 char host[256];
00371 int tportno = ntohs(sin->sin_port);
00372 if (service) {
00373 snprintf(srv, sizeof(srv), "%s.%s", service, value);
00374 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
00375 sin->sin_port = htons(tportno);
00376 value = host;
00377 }
00378 }
00379 hp = ast_gethostbyname(value, &ahp);
00380 if (hp) {
00381 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
00382 } else {
00383 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
00384 return -1;
00385 }
00386 return 0;
00387 }
00388
00389 struct dscp_codepoint {
00390 char *name;
00391 unsigned int space;
00392 };
00393
00394
00395
00396 static const struct dscp_codepoint dscp_pool1[] = {
00397 { "CS0", 0x00 },
00398 { "CS1", 0x08 },
00399 { "CS2", 0x10 },
00400 { "CS3", 0x18 },
00401 { "CS4", 0x20 },
00402 { "CS5", 0x28 },
00403 { "CS6", 0x30 },
00404 { "CS7", 0x38 },
00405 { "AF11", 0x0A },
00406 { "AF12", 0x0C },
00407 { "AF13", 0x0E },
00408 { "AF21", 0x12 },
00409 { "AF22", 0x14 },
00410 { "AF23", 0x16 },
00411 { "AF31", 0x1A },
00412 { "AF32", 0x1C },
00413 { "AF33", 0x1E },
00414 { "AF41", 0x22 },
00415 { "AF42", 0x24 },
00416 { "AF43", 0x26 },
00417 { "EF", 0x2E },
00418 };
00419
00420 int ast_str2cos(const char *value, unsigned int *cos)
00421 {
00422 int fval;
00423
00424 if (sscanf(value, "%30d", &fval) == 1) {
00425 if (fval < 8) {
00426 *cos = fval;
00427 return 0;
00428 }
00429 }
00430
00431 return -1;
00432 }
00433
00434 int ast_str2tos(const char *value, unsigned int *tos)
00435 {
00436 int fval;
00437 unsigned int x;
00438
00439 if (sscanf(value, "%30i", &fval) == 1) {
00440 *tos = fval & 0xFF;
00441 return 0;
00442 }
00443
00444 for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
00445 if (!strcasecmp(value, dscp_pool1[x].name)) {
00446 *tos = dscp_pool1[x].space << 2;
00447 return 0;
00448 }
00449 }
00450
00451 return -1;
00452 }
00453
00454 const char *ast_tos2str(unsigned int tos)
00455 {
00456 unsigned int x;
00457
00458 for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
00459 if (dscp_pool1[x].space == (tos >> 2))
00460 return dscp_pool1[x].name;
00461 }
00462
00463 return "unknown";
00464 }
00465
00466 int ast_get_ip(struct sockaddr_in *sin, const char *value)
00467 {
00468 return ast_get_ip_or_srv(sin, value, NULL);
00469 }
00470
00471 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
00472 {
00473 int s;
00474 struct sockaddr_in sin;
00475 socklen_t slen;
00476
00477 s = socket(PF_INET, SOCK_DGRAM, 0);
00478 if (s < 0) {
00479 ast_log(LOG_ERROR, "Cannot create socket\n");
00480 return -1;
00481 }
00482 sin.sin_family = AF_INET;
00483 sin.sin_port = htons(5060);
00484 sin.sin_addr = *them;
00485 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
00486 ast_log(LOG_WARNING, "Cannot connect\n");
00487 close(s);
00488 return -1;
00489 }
00490 slen = sizeof(sin);
00491 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
00492 ast_log(LOG_WARNING, "Cannot get socket name\n");
00493 close(s);
00494 return -1;
00495 }
00496 close(s);
00497 ast_debug(3, "Found IP address for this socket\n");
00498 *us = sin.sin_addr;
00499 return 0;
00500 }
00501
00502 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
00503 {
00504 char ourhost[MAXHOSTNAMELEN] = "";
00505 struct ast_hostent ahp;
00506 struct hostent *hp;
00507 struct in_addr saddr;
00508
00509
00510 if (ntohl(bindaddr.sin_addr.s_addr)) {
00511 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
00512 ast_debug(3, "Attached to given IP address\n");
00513 return 0;
00514 }
00515
00516 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
00517 ast_log(LOG_WARNING, "Unable to get hostname\n");
00518 } else {
00519 hp = ast_gethostbyname(ourhost, &ahp);
00520 if (hp) {
00521 memcpy(ourip, hp->h_addr, sizeof(*ourip));
00522 ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost);
00523 return 0;
00524 }
00525 }
00526 ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
00527
00528 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
00529 return 0;
00530 return get_local_address(ourip);
00531 }
00532