Changeset b097945 for lib/misc.c
- Timestamp:
- 2017-04-06T20:25:08Z (8 years ago)
- Branches:
- master
- Children:
- 0515063
- Parents:
- 0156c42
- git-author:
- Wilmer van der Gaast <wilmer@…> (05-04-17 22:38:08)
- git-committer:
- Wilmer van der Gaast <github@…> (06-04-17 20:25:08)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/misc.c
r0156c42 rb097945 1 /********************************************************************\1 /********************************************************************\ 2 2 * BitlBee -- An IRC to other IM-networks gateway * 3 3 * * … … 504 504 } 505 505 506 /* From OpenSSH 7.4p1 canohost.c" */507 char *reverse_lookup(const struct sockaddr *from_, const socklen_t fromlen_)508 {509 struct sockaddr_storage from;510 socklen_t fromlen;511 struct addrinfo hints, *ai, *aitop;512 char name[NI_MAXHOST], ntop2[NI_MAXHOST];513 char ntop[INET6_ADDRSTRLEN];514 515 fromlen = sizeof(from);516 memset(&from, 0, sizeof(from));517 memcpy(&from, from_, fromlen_);518 ipv64_normalise_mapped(&from, &fromlen);519 if (from.ss_family == AF_INET6) {520 fromlen = sizeof(struct sockaddr_in6);521 }522 523 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),524 NULL, 0, NI_NUMERICHOST) != 0) {525 return NULL;526 }527 528 /* Map the IP address to a host name. */529 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),530 NULL, 0, NI_NAMEREQD) != 0) {531 /* Host name not found. Use ip address. */532 return g_strdup(ntop);533 }534 535 /*536 * if reverse lookup result looks like a numeric hostname,537 * someone is trying to trick us by PTR record like following:538 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5539 */540 memset(&hints, 0, sizeof(hints));541 hints.ai_socktype = SOCK_DGRAM; /*dummy*/542 hints.ai_flags = AI_NUMERICHOST;543 if (getaddrinfo(name, NULL, &hints, &ai) == 0) {544 freeaddrinfo(ai);545 return g_strdup(ntop);546 }547 548 /* Names are stored in lowercase. */549 char *tolower = g_utf8_strdown(name, -1);550 g_snprintf(name, sizeof(name), "%s", tolower);551 g_free(tolower);552 553 /*554 * Map it back to an IP address and check that the given555 * address actually is an address of this host. This is556 * necessary because anyone with access to a name server can557 * define arbitrary names for an IP address. Mapping from558 * name to IP address can be trusted better (but can still be559 * fooled if the intruder has access to the name server of560 * the domain).561 */562 memset(&hints, 0, sizeof(hints));563 hints.ai_family = from.ss_family;564 hints.ai_socktype = SOCK_STREAM;565 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {566 return g_strdup(ntop);567 }568 /* Look for the address from the list of addresses. */569 for (ai = aitop; ai; ai = ai->ai_next) {570 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,571 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&572 (strcmp(ntop, ntop2) == 0))573 break;574 }575 freeaddrinfo(aitop);576 /* If we reached the end of the list, the address was not there. */577 if (ai == NULL) {578 /* Address not found for the host name. */579 return g_strdup(ntop);580 }581 return g_strdup(name);582 }583 584 void585 ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)586 {587 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;588 struct sockaddr_in *a4 = (struct sockaddr_in *)addr;589 struct in_addr inaddr;590 u_int16_t port;591 592 if (addr->ss_family != AF_INET6 ||593 !IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr))594 return;595 596 memcpy(&inaddr, ((char *)&a6->sin6_addr) + 12, sizeof(inaddr));597 port = a6->sin6_port;598 599 memset(a4, 0, sizeof(*a4));600 601 a4->sin_family = AF_INET;602 *len = sizeof(*a4);603 memcpy(&a4->sin_addr, &inaddr, sizeof(inaddr));604 a4->sin_port = port;605 }606 607 506 char *word_wrap(const char *msg, int line_len) 608 507 {
Note: See TracChangeset
for help on using the changeset viewer.