1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
/* Copyright (C) 2000-2004 Boris Wesslowski */
/* $Id: resolve.c,v 1.28 2004/03/21 09:42:55 bw Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ctype.h>
#include "resolve.h"
#include "main.h"
#include "utils.h"
struct dns_cache *dns_first = NULL;
extern struct options opt;
char * resolve_protocol(int proto)
{
struct protoent *protoent;
protoent = getprotobynumber(proto);
if (protoent != NULL) {
return (protoent->p_name);
} else {
char *number;
number = xmalloc(4);
snprintf(number, 4, "%d", proto);
return (number);
}
}
char * resolve_service(int port, char *proto)
{
struct servent *servent;
int p;
p = htons(port);
servent = getservbyport(p, proto);
if (servent != NULL) {
p = ntohs(servent->s_port);
if (p != port) {
fprintf(stderr, _("port mismatch: %d != %d\n"), p, port);
} else {
return (servent->s_name);
}
}
return ("-");
}
char * resolve_hostname(struct in_addr ip)
{
struct hostent *reverse, *forward;
struct dns_cache *dns;
char *pnt, fqdn[HOSTLEN];
dns = dns_first;
while(dns != NULL) {
if (ip.s_addr == dns->ip.s_addr) {
if(opt.verbose) {
fprintf(stderr, _("Resolving %s from cache\n"), inet_ntoa(ip));
}
return (dns->fqdn);
}
dns = dns->next;
}
if(opt.verbose)
fprintf(stderr, _("Resolving %s\n"), inet_ntoa(ip));
reverse = gethostbyaddr((void *)&ip.s_addr, sizeof(struct in_addr), AF_INET);
dns = xmalloc(sizeof(struct dns_cache));
dns->ip.s_addr = ip.s_addr;
if((reverse != NULL) && (reverse->h_name != NULL)) {
if ((unsigned int)reverse->h_length > sizeof(struct in_addr)) {
fprintf(stderr, _("Wrong host name size\n"));
reverse->h_length = sizeof(struct in_addr);
reverse->h_name[reverse->h_length] = '\0';
}
pnt = reverse->h_name;
while (*pnt != '\0') {
if (isalnum((int)*pnt) || *pnt == '.' || *pnt == '-') {
pnt++;
continue;
} else {
*pnt = '_';
pnt++;
}
}
if(opt.verbose)
fprintf(stderr, _("Resolving %s\n"), reverse->h_name);
forward = gethostbyname(reverse->h_name);
if ((forward != NULL) && (forward->h_addr_list[0]) != NULL) {
if (strncmp(inet_ntoa(ip), inet_ntoa(*(struct in_addr *)forward->h_addr_list[0]), IPLEN) == 0) {
xstrncpy(fqdn, reverse->h_name, HOSTLEN);
} else {
snprintf(fqdn, HOSTLEN, _("%s [forward lookup: %s]"), reverse->h_name, inet_ntoa(*(struct in_addr *)forward->h_addr_list[0]));
}
} else {
snprintf(fqdn, HOSTLEN, _("%s [forward lookup failed]"), reverse->h_name);
}
} else {
xstrncpy(fqdn, "-", HOSTLEN);
}
dns->fqdn = xmalloc(strlen(fqdn)+1);
xstrncpy(dns->fqdn, fqdn, strlen(fqdn)+1);
dns->next = dns_first;
dns_first = dns;
return (dns->fqdn);
}
|