File: netutils.c

package info (click to toggle)
ippl 1.4.14-15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 864 kB
  • sloc: ansic: 1,683; yacc: 443; sh: 320; lex: 163; makefile: 93
file content (258 lines) | stat: -rw-r--r-- 6,442 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 *  netutils.c - functions common to all the protocols
 *
 *  Copyright (C) 1998-1999 Hugo Haas
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <netdb.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <netinet/ip.h>
#include <stdlib.h>
#include <ctype.h>

#include <pthread.h>

#include "defines.h"
#include "netutils.h"
#include "log.h"

extern struct loginfo logi;

/* Mutexes */
pthread_mutex_t service_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t dns_mutex = PTHREAD_MUTEX_INITIALIZER;

/*
 * host_lookup
 *
 * Performs a DNS query with caching
 *
 * Cache uses a hastable with open adressing and double hashing.
 * It has LEVELS levels. Each entry has a dirty flag to help to decide
 * which entry should be discarded if all the levels are occupied.
 *
 * Some results when run on a LAN:
 * Tue Feb  2 19:00:36 DEBUG cache: 910 calls - 86 misses
 *
 * Yes, I know, it is incredible! :-)
 * -- Hugo (2/2/99)
 */

#ifdef CACHE_DEBUG
unsigned int dbg_calls = 0, dbg_missed = 0;
#define DUMP_EVERY 10
#endif

/* Hashtable */

struct ht_entry_struct {
  __u32 ip;
  char *name;
  short dirty;
};
typedef struct ht_entry_struct ht_entry;
ht_entry table[TABLE_SIZE];

void initialize_dns_cache(void) {
  int i;
#ifdef CACHE_DEBUG
  int dbg_freed = 0;
#endif

  /* Tell that we should use UDP for resolving, and not TCP */
  sethostent(0);

  pthread_mutex_lock(&dns_mutex);

  for(i = 0; i < TABLE_SIZE; i++) {
    table[i].ip = 0;
    if (table[i].name != NULL) {
      free(table[i].name);
      table[i].name = NULL;
#ifdef CACHE_DEBUG
      dbg_freed++;
#endif
    }
  }

  pthread_mutex_unlock(&dns_mutex);

#ifdef CACHE_DEBUG
  log.log(log.level_or_fd, "DEBUG cache: cleared %d entries", dbg_freed);
#endif
}

int perform_lookup(char *host, __u32 in) {
  struct in_addr i;
  struct hostent *he;

  i.s_addr = in;
  he = gethostbyaddr((char *) &i, sizeof(struct in_addr), AF_INET);
  if (he) {
    strncpy(host, he->h_name, HOST_LENGTH-24);
    return TRUE;
  }
  return FALSE;
}

int host_lookup(char *host, __u32 in) {
  __u16 key, key1, key2, level;
  int res;

  pthread_mutex_lock(&dns_mutex);

#ifdef CACHE_DEBUG
  if (dbg_calls % DUMP_EVERY == 0)
    log.log(log.level_or_fd, "DEBUG cache: %d calls - %d misses", dbg_calls, dbg_missed);

  dbg_calls++;
#endif

  /* First pass: see if the value is stored or do a lookup if a slot is empty */

  key1 = HASH1(in);
  key2 = HASH2(in);
  key = key1;
  level = 0;
  do {
    if (table[key].ip == 0) {
      goto lookup;
    }
    if (table[key].ip == in) {
      strcpy(host, table[key].name);
      table[key].dirty = FALSE;
      goto exit_hl;
    }
    key = (key + key2) % TABLE_SIZE;
  } while(++level == LEVELS);

  /* If we are here, it means that all the slots are full and none of them contains the searched item */
  /* Second pass: look for an entry to replace; take the first dirty one */
  key = key1;
  level = 0;
  do {
    if (table[key].dirty == TRUE) {
      goto lookup;
    }
    else {
      table[key].dirty = TRUE;
    }
    key = (key + key2) % TABLE_SIZE;
  } while(++level == LEVELS);

  /* Do a lookup and store the result in */
 lookup:
#ifdef CACHE_DEBUG
  dbg_missed++;
#endif
  res = perform_lookup(host, in);
#ifdef CACHE_DEBUG
  log.log(log.level_or_fd, "DEBUG cache: result of lookup - %s", host);
  log.log(log.level_or_fd, "DEBUG cache: replacing (%d; %d; %d; %s)", key, table[key].ip, table[key].dirty, table[key].name);
#endif
  /* Now host contains the correct value; store it in the table */
  table[key].ip = in;
  table[key].dirty = FALSE;
  if (table[key].name != NULL)
    free(table[key].name);
  table[key].name = strdup(host);

  pthread_mutex_unlock(&dns_mutex);
  return res;

 exit_hl:
  pthread_mutex_unlock(&dns_mutex);
  return TRUE;
}

/*
 * host_print
 *
 * Prints into hostname IP and optionaly DNS name
 */
void host_print(char *hostname, __u32 in, int resolve) {
  char remote_hostname[HOST_LENGTH];
  struct in_addr i;

  i.s_addr = in;
  *remote_hostname = '\0';

  if (resolve && host_lookup(remote_hostname, in)) {
    snprintf(hostname, HOST_LENGTH, "%s [%.20s]",
             remote_hostname, inet_ntoa(i));
  } else {
    snprintf(hostname, 21, "%.20s", inet_ntoa(i));
  }
}

/*
 * get_details
 *
 * Display details about a packet header (source & destination port & address)
 * Based on Steffen Ullrich's (ccrlphr@xensei.com) code
 * " (xxx.xxx.xxx.xxx:xxxxx->xxx.xxx.xxx.xxx:xxxxx)" if ports are non-zero
 * " (xxx.xxx.xxx.xxx->xxx.xxx.xxx.xxx)" if ports are zero
 */

void get_details(char *details,
                 const __u32 src_addr, const __u16 src_port,
                 const __u32 dst_addr, const __u16 dst_port) {
  int sz;
  struct in_addr i;

  i.s_addr = src_addr;
  if ((src_port == 0) && (dst_port == 0)) {
    sz = snprintf(details, DETAILS_LENGTH, " (%s->", inet_ntoa(i));
    i.s_addr = dst_addr;
    snprintf(details+sz, DETAILS_LENGTH-sz, "%s)", inet_ntoa(i));
  }
  else {
    sz = snprintf(details, DETAILS_LENGTH, " (%s:%d->", inet_ntoa(i), ntohs(src_port));
    i.s_addr = dst_addr;
    snprintf(details+sz, DETAILS_LENGTH-sz, "%s:%d)", inet_ntoa(i), ntohs(dst_port));
  }
}

/*
 * service_lookup
 *
 * Get a service name for a specified protocol
 */

void service_lookup(char *proto, char *service, __u16 port, int portresolve) {
  struct servent *se;

  pthread_mutex_lock(&service_mutex);
  if (portresolve)
  {
    se = getservbyport(port, proto);
    if (se == NULL)
      snprintf(service, SERVICE_LENGTH, "port %d", ntohs(port));
    else {
      snprintf(service, SERVICE_LENGTH, "%s", se->s_name);
    }
  }
  else {
    snprintf(service, SERVICE_LENGTH, "port %d", ntohs(port));
  }
  pthread_mutex_unlock(&service_mutex);
}