resolver.c 8.03 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
17
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>

#include "ns_hash.h"
chris's avatar
""    
chris committed
18
#include "iftop.h"
pdw's avatar
iftop  
pdw committed
19

pdw's avatar
pdw committed
20
21
#include "threadprof.h"

pdw's avatar
pdw committed
22
23


pdw's avatar
iftop  
pdw committed
24
25
26
27
28
29
30
31
32
33
34
35
#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
36

chris's avatar
""    
chris committed
37
38
39
40
41
42
43
44
/* 
 * We have a choice of resolver methods. Real computers have gethostbyaddr_r,
 * which is reentrant and therefore thread safe. Other machines don't, and so
 * we can use non-reentrant gethostbyaddr and have only one resolver thread.
 * Alternatively, we can use the MIT ares asynchronous DNS library to do this.
 */

#if defined(USE_GETHOSTBYADDR_R)
pdw's avatar
pdw committed
45
46
/**
 * Implementation of do_resolve for platforms with working gethostbyaddr_r
pdw's avatar
pdw committed
47
48
49
 *
 * Some implementations of libc choose to implement gethostbyaddr_r as
 * a non thread-safe wrapper to gethostbyaddr.  An interesting choice...
pdw's avatar
pdw committed
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
 */
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;
}

chris's avatar
""    
chris committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#elif defined(USE_GETHOSTBYADDR)

/**
 * Implementation using gethostbyname. Since this is nonreentrant, we have to
 * wrap it in a mutex, losing all benefit of multithreaded resolution.
 */
char *do_resolve(struct in_addr *addr) {
    static pthread_mutex_t ghba_mtx = PTHREAD_MUTEX_INITIALIZER;
    char *s = NULL;
    struct hostent *he;
    pthread_mutex_lock(&ghba_mtx);
    he = gethostbyaddr((char*)addr, sizeof *addr, AF_INET);
    if (he)
        s = xstrdup(he->h_name);
    pthread_mutex_unlock(&ghba_mtx);
    return s;
}


#elif defined(USE_LIBRESOLV)

#include <arpa/nameser.h>
#include <resolv.h>
pdw's avatar
pdw committed
106
107

/**
pdw's avatar
pdw committed
108
109
 * libresolv implementation 
 * resolver functions may not be thread safe
pdw's avatar
pdw committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 */
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;
}
chris's avatar
""    
chris committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

#elif defined(USE_ARES)

/**
 * ares implementation
 */

#include <sys/time.h>
#include <ares.h>
#include <arpa/nameser.h>

/* callback function for ares */
struct ares_callback_comm {
    struct in_addr *addr;
    int result;
    char *name;
};

static void do_resolve_ares_callback(void *arg, int status, unsigned char *abuf, int alen) {
    struct hostent *he;
    struct ares_callback_comm *C;
    C = (struct ares_callback_comm*)arg;

    if (status == ARES_SUCCESS) {
        C->result = 1;
        ares_parse_ptr_reply(abuf, alen, C->addr, sizeof *C->addr, AF_INET, &he);
        C->name = xstrdup(he->h_name);;
        ares_free_hostent(he);
    } else {
        C->result = -1;
    }
}

char *do_resolve(struct in_addr * addr) {
    struct ares_callback_comm C;
    char s[35];
    unsigned char *a;
    ares_channel *chan;
    static pthread_mutex_t ares_init_mtx = PTHREAD_MUTEX_INITIALIZER;
    static pthread_key_t ares_key;
    static int gotkey;

    /* Make sure we have an ARES channel for this thread. */
    pthread_mutex_lock(&ares_init_mtx);
    if (!gotkey) {
        pthread_key_create(&ares_key, NULL);
        gotkey = 1;
        
    }
    pthread_mutex_unlock(&ares_init_mtx);
    
    chan = pthread_getspecific(ares_key);
    if (!chan) {
        chan = xmalloc(sizeof *chan);
        pthread_setspecific(ares_key, chan);
        if (ares_init(chan) != ARES_SUCCESS) return NULL;
    }
    
    a = (unsigned char*)addr;
    sprintf(s, "%d.%d.%d.%d.in-addr.arpa.", a[3], a[2], a[1], a[0]);
    
    C.result = 0;
    C.addr = addr;
    ares_query(*chan, s, C_IN, T_PTR, do_resolve_ares_callback, &C);
    while (C.result == 0) {
        int n;
        fd_set readfds, writefds;
        struct timeval tv;
        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        n = ares_fds(*chan, &readfds, &writefds);
        ares_timeout(*chan, NULL, &tv);
        select(n, &readfds, &writefds, NULL, &tv);
        ares_process(*chan, &readfds, &writefds);
    }

    /* At this stage, the query should be complete. */
    switch (C.result) {
        case -1:
        case 0:     /* shouldn't happen */
            return NULL;

        default:
            return C.name;
    }
}

#else

#   warning No name resolution method specified; name resolution will not work

char *do_resolve(struct in_addr *addr) {
    return NULL;
}

pdw's avatar
pdw committed
238
239
#endif

pdw's avatar
iftop  
pdw committed
240
void resolver_worker(void* ptr) {
chris's avatar
""    
chris committed
241
/*    int thread_number = *(int*)ptr;*/
242
    pthread_mutex_lock(&resolver_queue_mutex);
chris's avatar
""    
chris committed
243
    sethostent(1);
pdw's avatar
iftop  
pdw committed
244
245
    while(1) {
        /* Wait until we are told that an address has been added to the 
chris's avatar
""    
chris committed
246
         * queue. */
pdw's avatar
iftop  
pdw committed
247
248
249
250
        pthread_cond_wait(&resolver_queue_cond, &resolver_queue_mutex);

        /* Keep resolving until the queue is empty */
        while(head != tail) {
pdw's avatar
pdw committed
251
            char * hostname;
pdw's avatar
iftop  
pdw committed
252
253
254
255
256
257
            struct in_addr addr = resolve_queue[tail];

            /* mutex always locked at this point */

            tail = (tail + 1) % RESOLVE_QUEUE_LENGTH;

258
259
            pthread_mutex_unlock(&resolver_queue_mutex);

pdw's avatar
pdw committed
260
            hostname = do_resolve(&addr);
pdw's avatar
iftop  
pdw committed
261
262
263
264

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

pdw's avatar
pdw committed
267
268
269
            if(hostname != NULL) {
                char* old;
                if(hash_find(ns_hash, &addr, (void**)&old) == HASH_STATUS_OK) {
pdw's avatar
iftop  
pdw committed
270
                    hash_delete(ns_hash, &addr);
pdw's avatar
pdw committed
271
                    xfree(old);
pdw's avatar
iftop  
pdw committed
272
273
274
                }
                hash_insert(ns_hash, &addr, (void*)hostname);
            }
pdw's avatar
pdw committed
275

pdw's avatar
iftop  
pdw committed
276
277
278
279
280
        }
    }
}

void resolver_initialise() {
281
282
    int* n;
    int i;
pdw's avatar
iftop  
pdw committed
283
284
285
286
287
288
289
290
    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);

291
    for(i = 0; i < 2; i++) {
chris's avatar
""    
chris committed
292
        n = (int*)xmalloc(sizeof *n);
293
294
295
        *n = i;
        pthread_create(&thread, NULL, (void*)&resolver_worker, (void*)n);
    }
pdw's avatar
iftop  
pdw committed
296
297
298
299
300

}

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

303
    pthread_mutex_lock(&resolver_queue_mutex);
pdw's avatar
iftop  
pdw committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317

    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;
318
            added = 1;
pdw's avatar
iftop  
pdw committed
319
320
        }
    }
321
322
323
324
325
    pthread_mutex_unlock(&resolver_queue_mutex);

    if(added == 1) {
        pthread_cond_signal(&resolver_queue_cond);
    }
pdw's avatar
iftop  
pdw committed
326
327
328
329
330
331

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