File: net.c

package info (click to toggle)
mtr 0.28-0.2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 404 kB
  • ctags: 368
  • sloc: ansic: 2,905; sh: 282; makefile: 60
file content (443 lines) | stat: -rw-r--r-- 9,760 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
    mtr  --  a network diagnostic tool
    Copyright (C) 1997,1998  Matt Kimball

    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 <config.h>

#if defined(HAVE_SYS_XTI_H)
#include <sys/xti.h>
#endif

#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <memory.h>
#include <unistd.h>
#include <stdio.h>
#include <math.h>

#include "net.h"


extern float WaitTime, DeltaTime;

#define MaxTransit 4

/*  We can't rely on header files to provide this information, because
    the fields have different names between, for instance, Linux and 
    Solaris  */
struct ICMPHeader {
  unsigned char type;
  unsigned char code;
  unsigned short checksum;
  unsigned short id;
  unsigned short sequence;
};

/*  Structure of an IP header.  */
struct IPHeader {
  uint8 version;
  uint8 tos;
  uint16 len;
  uint16 id;
  uint16 frag;
  uint8 ttl;
  uint8 protocol;
  uint16 check;
  uint32 saddr;
  uint32 daddr;
};
  
#define ICMP_ECHO		8
#define ICMP_ECHOREPLY		0
#define ICMP_TIME_EXCEEDED	11
  
#ifndef SOL_IP
#define SOL_IP 0
#endif
  
struct packetdata {
    int index;
    int ttl;
    int sec;
    int msec;
};

struct nethost {
  int addr;
  int xmit;
  int returned;
  int total;
  int best;
  int worst;
  int transit;
};

static struct nethost host[MaxHost];
static struct timeval reset = { 0, 0 };

int sendsock;
int recvsock;
struct sockaddr_in remoteaddress;

int checksum(void *data, int sz) {
  unsigned short *ch;
  unsigned int sum;

  sum = 0;
  ch = data;
  sz = sz / 2;
  while(sz--) {
    sum += *(ch++);
  }
  
  sum = (sum >> 16) + (sum & 0xffff);  

  return (~sum & 0xffff);  
}

void net_send_ping(int index) {
  char packet[sizeof(struct IPHeader) + sizeof(struct ICMPHeader) 
	     + sizeof(struct packetdata)];
  struct IPHeader *ip;
  struct ICMPHeader *icmp;
  struct packetdata *data;
  int packetsize = sizeof(struct IPHeader) + sizeof(struct ICMPHeader) + sizeof(struct packetdata);
  struct sockaddr_in addr;
  struct timeval now;

  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = host[index].addr;
  host[index].xmit++;
  host[index].transit = 1;

  memset(packet, 0, packetsize);

  ip = (struct IPHeader *)packet;
  icmp = (struct ICMPHeader *)(packet + sizeof(struct IPHeader));
  data = (struct packetdata *)(packet + sizeof(struct IPHeader) + sizeof(struct ICMPHeader));

  ip->version = 0x45;
  ip->tos = 0;
  ip->len = htons (packetsize);
  ip->id = 0;
  ip->frag = 0;
  ip->ttl = 127;
  ip->protocol = IPPROTO_ICMP;
  ip->saddr = 0;
  ip->daddr = host[index].addr;
  
  icmp->type = ICMP_ECHO;
  icmp->id = getpid();
  icmp->sequence = 0;

  data->ttl = 0;
  data->index = index;

  gettimeofday(&now, NULL);
  data->sec = now.tv_sec;
  data->msec = now.tv_usec / 1000;

  icmp->checksum = checksum(icmp, packetsize - sizeof(struct IPHeader));
  ip->check = checksum(ip, packetsize);

  sendto(sendsock, packet, packetsize, 0, 
	 (struct sockaddr *)&addr, sizeof(addr));
}

/*  Attempt to find the host at a particular number of hops away  */
void net_send_query(int hops) {
  char packet[sizeof(struct IPHeader) + sizeof(struct ICMPHeader) + sizeof(struct packetdata)];
  struct IPHeader *ip;
  struct ICMPHeader *icmp;
  struct packetdata *data;
  int packetsize = sizeof(struct IPHeader) + sizeof(struct ICMPHeader) + sizeof(struct packetdata);

  memset(packet, 0, packetsize);

  ip = (struct IPHeader *)packet;
  icmp = (struct ICMPHeader *)(packet + sizeof(struct IPHeader));
  data = (struct packetdata *)(packet + sizeof(struct IPHeader) + sizeof(struct ICMPHeader));

  ip->version = 0x45;
  ip->tos = 0;
  ip->len = htons (packetsize);
  ip->id = 0;
  ip->frag = 0;
  ip->ttl = hops;
  ip->protocol = IPPROTO_ICMP;
  ip->saddr = 0;
  ip->daddr = remoteaddress.sin_addr.s_addr;

  icmp->type = ICMP_ECHO;
  icmp->id = getpid();
  icmp->sequence = hops;

  data->ttl = hops;
  data->index = -1;

  icmp->checksum = checksum(icmp, packetsize - sizeof(struct IPHeader));
  ip->check = checksum(ip, packetsize);

  sendto(sendsock, packet, packetsize, 0, 
	 (struct sockaddr *)&remoteaddress, sizeof(remoteaddress));
}

void net_process_ping(struct packetdata *data, struct sockaddr_in *addr) {
  int at;
  struct timeval now;
  int totmsec;

  if(data->index >= 0) {
    gettimeofday(&now, NULL);

    if(data->sec < reset.tv_sec
       || (data->sec == reset.tv_sec && (1000*data->msec) < reset.tv_usec))
      /* discard this data point, stats were reset after it was generated */
      return;
    
    totmsec = (now.tv_sec - data->sec) * 1000 +
              ((now.tv_usec/1000) - data->msec);

    if(host[data->index].returned <= 0) {
      host[data->index].best = host[data->index].worst = totmsec;
    }

    if(totmsec < host[data->index].best)
      host[data->index].best = totmsec;

    if(totmsec > host[data->index].worst)
      host[data->index].worst = totmsec;

    display_rawping (data->index, totmsec);

    host[data->index].total += totmsec;
    host[data->index].returned++;
    host[data->index].transit = 0;
  } else {
    at = data->ttl - 1;
    if(at < 0 || at > MaxHost)
      return;

    host[at].addr = addr->sin_addr.s_addr;
    display_rawhost (at, host[at].addr);
  }
}

void net_process_return() {
  char packet[2048];
  struct sockaddr_in fromaddr;
  int fromaddrsize;
  int num;
  int at;
  struct ICMPHeader *header;

  fromaddrsize = sizeof(fromaddr);
  num = recvfrom(recvsock, packet, 2048, 0, 
		 (struct sockaddr *)&fromaddr, &fromaddrsize);

  if(num < sizeof(struct IPHeader) + sizeof(struct ICMPHeader) + sizeof(struct packetdata))
    return;

  header = (struct ICMPHeader *)(packet + sizeof(struct IPHeader));
  if(header->type == ICMP_ECHOREPLY) {
    if(header->id != getpid())
      return;

    net_process_ping((struct packetdata *)(packet + sizeof(struct IPHeader) + 
					   sizeof(struct ICMPHeader)),
		     &fromaddr);
  } else if(header->type == ICMP_TIME_EXCEEDED) {
    if(num < sizeof(struct IPHeader) + sizeof(struct ICMPHeader) + 
             sizeof(struct IPHeader) + sizeof(struct ICMPHeader))
      return;
    
    header = (struct ICMPHeader *)(packet + sizeof(struct IPHeader) + 
				sizeof(struct ICMPHeader) + sizeof(struct IPHeader));
    if(header->id != getpid())
      return;
    
    at = header->sequence - 1;
    if(at < 0 || at > MaxHost)
      return;

    host[at].addr = fromaddr.sin_addr.s_addr;
    display_rawhost (at, net_addr(at));
  }
}

int net_addr(int at) {
  return ntohl(host[at].addr);
}

int net_percent(int at) {
  if(host[at].xmit == 0) 
    return 0;

  return 100 - (100 * (host[at].returned + host[at].transit) / host[at].xmit);
}

int net_best(int at) {
  return host[at].best;
}

int net_worst(int at) {
  return host[at].worst;
}

int net_avg(int at) {
  if(host[at].returned == 0)
    return 0;

  return host[at].total / host[at].returned;
}

int net_max() {
  int at;
  int max;

  max = 0;
  for(at = 0; at < MaxHost; at++) {
    if(host[at].addr == remoteaddress.sin_addr.s_addr) {
      return at + 1;
    } else if(host[at].addr != 0) {
      max = at + 2;
    }
  }

  return max;
}


/* Added by Brian Casey December 1997 bcasey@imagiware.com*/
int net_returned(int at) { 
   return host[at].returned;
}
int net_xmit(int at) { 
   return host[at].xmit;
}
int net_transit(int at) { 
   return host[at].transit;
}

void net_end_transit() {
  int at;

  for(at = 0; at < MaxHost; at++) {
    host[at].transit = 0;
  }
}



int net_send_batch() {
  static int n_unknown = 10;
  static int at;

  if(host[at].addr == 0) {
    net_send_query(at + 1);
    n_unknown--;
  } else {
    net_send_ping(at);
  }
  
  if ((host[at].addr == remoteaddress.sin_addr.s_addr) ||
      (n_unknown == 0)) {
    DeltaTime = WaitTime / (float) (at+1);
    at = 0;
    n_unknown = 10;
    return 1;
  }

  at++;
  return 0;
}


int net_preopen() {
  int trueopt = 1;

  sendsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  if(sendsock < 0)
    return -1;

#ifdef IP_HDRINCL
  /*  FreeBSD wants this to avoid sending out packets with protocol type RAW
      to the network.  */
  if(setsockopt(sendsock, SOL_IP, IP_HDRINCL, &trueopt, sizeof(trueopt)))
  {
    perror("setsockopt(IP_HDRINCL,1)");
    return -1;
  }
#endif

  recvsock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  if(recvsock < 0)
    return -1;

  return 0;
}
 
int net_open(int addr) {
  remoteaddress.sin_family = AF_INET;
  remoteaddress.sin_addr.s_addr = addr;

  net_send_batch();

  return 0;
}

void net_reopen(int addr) {
  int at;

  for(at = 0; at < MaxHost; at++) {
    memset(&host[at], 0, sizeof(host[at]));
  }

  remoteaddress.sin_family = AF_INET;
  remoteaddress.sin_addr.s_addr = addr;

  net_reset ();
  net_send_batch();
}

void net_reset() {
  int at;

  for(at = 0; at < MaxHost; at++) {
    host[at].xmit = host[at].transit;
    host[at].returned = 0;
    host[at].total = 0;
    host[at].best = 0;
    host[at].worst = 0;
  }
  gettimeofday(&reset, NULL);
}

void net_close() {
  close(sendsock);
  close(recvsock);
}

int net_waitfd() {
  return recvsock;
}