summaryrefslogtreecommitdiff
path: root/resolver.c
diff options
context:
space:
mode:
authorpdw <>2002-10-24 13:42:11 +0000
committerpdw <>2002-10-24 13:42:11 +0000
commit4ef789bed10a53c5aa69d1c069a97d6c3900a80c (patch)
tree98350a572f04d2119ce072e80781e83fec8e76c2 /resolver.c
parent0d9a9538ddbc207081bec7d355fedfeac2a4afd5 (diff)
downloadiftop-4ef789bed10a53c5aa69d1c069a97d6c3900a80c.zip
Added alternative resolver.
Altered display of totals.
Diffstat (limited to 'resolver.c')
-rw-r--r--resolver.c120
1 files changed, 91 insertions, 29 deletions
diff --git a/resolver.c b/resolver.c
index 8602421..aa30910 100644
--- a/resolver.c
+++ b/resolver.c
@@ -14,11 +14,18 @@
#include <errno.h>
#include <string.h>
+#ifdef USELIBRESOLV
+#include <arpa/nameser.h>
+#include <resolv.h>
+#endif
+
#include "ns_hash.h"
#include "iftop.h"
#include "threadprof.h"
+
+
#define RESOLVE_QUEUE_LENGTH 20
struct in_addr resolve_queue[RESOLVE_QUEUE_LENGTH];
@@ -31,6 +38,83 @@ hash_type* ns_hash;
int head;
int tail;
+
+#ifndef USELIBRESOLV
+/**
+ * Implementation of do_resolve for platforms with working gethostbyaddr_r
+ */
+char* do_resolve(struct in_addr * addr) {
+ struct hostent hostbuf, *hp;
+ size_t hstbuflen = 1024;
+ char *tmphstbuf;
+ int res;
+ int herr;
+ char * ret = NULL;
+
+ /* Allocate buffer, remember to free it to avoid memory leakage. */
+ tmphstbuf = xmalloc (hstbuflen);
+
+ while ((res = gethostbyaddr_r ((char*)addr, sizeof(struct in_addr), AF_INET,
+ &hostbuf, tmphstbuf, hstbuflen,
+ &hp, &herr)) == ERANGE) {
+ /* Enlarge the buffer. */
+ hstbuflen *= 2;
+ tmphstbuf = realloc (tmphstbuf, hstbuflen);
+ }
+
+ /* Check for errors. */
+ if (res || hp == NULL) {
+ /* failed */
+ /* Leave the unresolved IP in the hash */
+ }
+ else {
+ ret = xstrdup(hp->h_name);
+
+ }
+ xfree(tmphstbuf);
+ return ret;
+}
+
+#else
+
+/**
+ * libresolv implementation
+ */
+char* do_resolve(struct in_addr * addr) {
+ char msg[PACKETSZ];
+ char s[35];
+ int l;
+ unsigned char* a;
+ char * ret = NULL;
+
+ a = (unsigned char*)addr;
+
+ snprintf(s, 35, "%d.%d.%d.%d.in-addr.arpa.",a[3], a[2], a[1], a[0]);
+
+ l = res_search(s, C_IN, T_PTR, msg, PACKETSZ);
+ if(l != -1) {
+ ns_msg nsmsg;
+ ns_rr rr;
+ if(ns_initparse(msg, l, &nsmsg) != -1) {
+ int c;
+ int i;
+ c = ns_msg_count(nsmsg, ns_s_an);
+ for(i = 0; i < c; i++) {
+ if(ns_parserr(&nsmsg, ns_s_an, i, &rr) == 0){
+ if(ns_rr_type(rr) == T_PTR) {
+ char buf[256];
+ ns_name_uncompress(msg, msg + l, ns_rr_rdata(rr), buf, 256);
+ ret = xstrdup(buf);
+ }
+
+ }
+ }
+ }
+ }
+ return ret;
+}
+#endif
+
void resolver_worker(void* ptr) {
struct timespec delay;
/* int thread_number = *(int*)ptr;*/
@@ -45,12 +129,8 @@ void resolver_worker(void* ptr) {
/* Keep resolving until the queue is empty */
while(head != tail) {
+ char * hostname;
struct in_addr addr = resolve_queue[tail];
- struct hostent hostbuf, *hp;
- size_t hstbuflen;
- char *tmphstbuf;
- int res;
- int herr;
/* mutex always locked at this point */
@@ -58,40 +138,22 @@ void resolver_worker(void* ptr) {
pthread_mutex_unlock(&resolver_queue_mutex);
- hstbuflen = 1024;
- /* Allocate buffer, remember to free it to avoid memory leakage. */
- tmphstbuf = xmalloc (hstbuflen);
-
- while ((res = gethostbyaddr_r ((char*)&addr, sizeof(addr), AF_INET,
- &hostbuf, tmphstbuf, hstbuflen,
- &hp, &herr)) == ERANGE) {
- /* Enlarge the buffer. */
- hstbuflen *= 2;
- tmphstbuf = realloc (tmphstbuf, hstbuflen);
- }
+ hostname = do_resolve(&addr);
/*
* Store the result in ns_hash
*/
pthread_mutex_lock(&resolver_queue_mutex);
- /* Check for errors. */
- if (res || hp == NULL) {
- /* failed */
- /* Leave the unresolved IP in the hash */
- }
- else {
- /* success */
- char* hostname;
- if(hash_find(ns_hash, &addr, (void**)&hostname) == HASH_STATUS_OK) {
+ if(hostname != NULL) {
+ char* old;
+ if(hash_find(ns_hash, &addr, (void**)&old) == HASH_STATUS_OK) {
hash_delete(ns_hash, &addr);
- xfree(hostname);
+ xfree(old);
}
- hostname = strdup(hp->h_name);
hash_insert(ns_hash, &addr, (void*)hostname);
-
}
- xfree(tmphstbuf);
+
}
}
}