resolver.c 4.77 KB
Newer Older
pdw's avatar
iftop  
pdw committed
1
2
3
4
5
/*
 * resolver.c:
 *
 */

pdw's avatar
pdw committed
6
#include <sys/types.h>
pdw's avatar
iftop  
pdw committed
7
#include <sys/socket.h>
pdw's avatar
pdw committed
8
#include <netinet/in.h>
pdw's avatar
iftop  
pdw committed
9
10
11
12
13
14
15
16
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>

pdw's avatar
pdw committed
17
18
19
20
21
#ifdef USELIBRESOLV
#include <arpa/nameser.h>
#include <resolv.h>
#endif

pdw's avatar
iftop  
pdw committed
22
#include "ns_hash.h"
chris's avatar
""    
chris committed
23
#include "iftop.h"
pdw's avatar
iftop  
pdw committed
24

pdw's avatar
pdw committed
25
26
#include "threadprof.h"

pdw's avatar
pdw committed
27
28


pdw's avatar
iftop  
pdw committed
29
30
31
32
33
34
35
36
37
38
39
40
#define RESOLVE_QUEUE_LENGTH 20

struct in_addr resolve_queue[RESOLVE_QUEUE_LENGTH];

pthread_cond_t resolver_queue_cond;
pthread_mutex_t resolver_queue_mutex;

hash_type* ns_hash;

int head;
int tail;

pdw's avatar
pdw committed
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

#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

pdw's avatar
iftop  
pdw committed
118
119
void resolver_worker(void* ptr) {
    struct timespec delay;
chris's avatar
""    
chris committed
120
/*    int thread_number = *(int*)ptr;*/
pdw's avatar
iftop  
pdw committed
121
122
    delay.tv_sec = 0;
    delay.tv_nsec = 500;
123
    pthread_mutex_lock(&resolver_queue_mutex);
chris's avatar
""    
chris committed
124
    sethostent(1);
pdw's avatar
iftop  
pdw committed
125
126
    while(1) {
        /* Wait until we are told that an address has been added to the 
chris's avatar
""    
chris committed
127
         * queue. */
pdw's avatar
iftop  
pdw committed
128
129
130
131
        pthread_cond_wait(&resolver_queue_cond, &resolver_queue_mutex);

        /* Keep resolving until the queue is empty */
        while(head != tail) {
pdw's avatar
pdw committed
132
            char * hostname;
pdw's avatar
iftop  
pdw committed
133
134
135
136
137
138
            struct in_addr addr = resolve_queue[tail];

            /* mutex always locked at this point */

            tail = (tail + 1) % RESOLVE_QUEUE_LENGTH;

139
140
            pthread_mutex_unlock(&resolver_queue_mutex);

pdw's avatar
pdw committed
141
            hostname = do_resolve(&addr);
pdw's avatar
iftop  
pdw committed
142
143
144
145

            /*
             * Store the result in ns_hash
             */
146
            pthread_mutex_lock(&resolver_queue_mutex);
pdw's avatar
iftop  
pdw committed
147

pdw's avatar
pdw committed
148
149
150
            if(hostname != NULL) {
                char* old;
                if(hash_find(ns_hash, &addr, (void**)&old) == HASH_STATUS_OK) {
pdw's avatar
iftop  
pdw committed
151
                    hash_delete(ns_hash, &addr);
pdw's avatar
pdw committed
152
                    xfree(old);
pdw's avatar
iftop  
pdw committed
153
154
155
                }
                hash_insert(ns_hash, &addr, (void*)hostname);
            }
pdw's avatar
pdw committed
156

pdw's avatar
iftop  
pdw committed
157
158
159
160
161
        }
    }
}

void resolver_initialise() {
162
163
    int* n;
    int i;
pdw's avatar
iftop  
pdw committed
164
165
166
167
168
169
170
171
    pthread_t thread;
    head = tail = 0;

    ns_hash = ns_hash_create();
    
    pthread_mutex_init(&resolver_queue_mutex, NULL);
    pthread_cond_init(&resolver_queue_cond, NULL);

172
    for(i = 0; i < 2; i++) {
chris's avatar
""    
chris committed
173
        n = (int*)xmalloc(sizeof *n);
174
175
176
        *n = i;
        pthread_create(&thread, NULL, (void*)&resolver_worker, (void*)n);
    }
pdw's avatar
iftop  
pdw committed
177
178
179
180
181

}

void resolve(struct in_addr* addr, char* result, int buflen) {
    char* hostname;
182
    int added = 0;
pdw's avatar
iftop  
pdw committed
183

184
    pthread_mutex_lock(&resolver_queue_mutex);
pdw's avatar
iftop  
pdw committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198

    if(hash_find(ns_hash, addr, (void**)&hostname) == HASH_STATUS_OK) {
        /* Found => already resolved, or on the queue */
    }
    else {
        hostname = strdup(inet_ntoa(*addr));
        hash_insert(ns_hash, addr, hostname);

        if(((head + 1) % RESOLVE_QUEUE_LENGTH) == tail) {
            /* queue full */
        }
        else {
            resolve_queue[head] = *addr;
            head = (head + 1) % RESOLVE_QUEUE_LENGTH;
199
            added = 1;
pdw's avatar
iftop  
pdw committed
200
201
        }
    }
202
203
204
205
206
    pthread_mutex_unlock(&resolver_queue_mutex);

    if(added == 1) {
        pthread_cond_signal(&resolver_queue_cond);
    }
pdw's avatar
iftop  
pdw committed
207
208
209
210
211
212

    if(result != NULL && buflen > 1) {
        strncpy(result, hostname, buflen - 1);
        result[buflen - 1] = '\0';
    }
}