-
Notifications
You must be signed in to change notification settings - Fork 46
/
debug.c
460 lines (394 loc) · 10.9 KB
/
debug.c
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*****************************************************************************
*
* Copyright (C) 2001 Uppsala University & Ericsson AB.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors: Erik Nordström, <[email protected]>
*
*
*****************************************************************************/
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#ifdef NS_PORT
#include "ns-2/aodv-uu.h"
#else
#include <net/if.h>
#include "aodv_rreq.h"
#include "aodv_rrep.h"
#include "aodv_rerr.h"
#include "defs.h"
#include "debug.h"
#include "params.h"
#include "timer_queue.h"
#include "routing_table.h"
#endif
#ifndef NS_PORT
extern int log_to_file, rt_log_interval;
extern char *progname;
int log_file_fd = -1;
int log_rt_fd = -1;
int log_nmsgs = 0;
int debug = 0;
struct timer rt_log_timer;
#endif
void NS_CLASS log_init()
{
/* NS_PORT: Log filename is prefix + IP address + suffix */
#ifdef NS_PORT
char AODV_LOG_PATH[strlen(AODV_LOG_PATH_PREFIX) +
strlen(AODV_LOG_PATH_SUFFIX) + 16];
char AODV_RT_LOG_PATH[strlen(AODV_LOG_PATH_PREFIX) +
strlen(AODV_RT_LOG_PATH_SUFFIX) + 16];
sprintf(AODV_LOG_PATH, "%s%d%s", AODV_LOG_PATH_PREFIX, node_id,
AODV_LOG_PATH_SUFFIX);
sprintf(AODV_RT_LOG_PATH, "%s%d%s", AODV_LOG_PATH_PREFIX, node_id,
AODV_RT_LOG_PATH_SUFFIX);
#endif /* NS_PORT */
if (log_to_file) {
if ((log_file_fd =
open(AODV_LOG_PATH, O_RDWR | O_CREAT | O_TRUNC,
S_IROTH | S_IWUSR | S_IRUSR | S_IRGRP)) < 0) {
perror("open log file failed!");
exit(-1);
}
}
if (rt_log_interval) {
if ((log_rt_fd =
open(AODV_RT_LOG_PATH, O_RDWR | O_CREAT | O_TRUNC,
S_IROTH | S_IWUSR | S_IRUSR | S_IRGRP)) < 0) {
perror("open rt log file failed!");
exit(-1);
}
}
openlog(progname, 0, LOG_DAEMON);
}
void NS_CLASS log_rt_table_init()
{
timer_init(&rt_log_timer, &NS_CLASS print_rt_table, NULL);
timer_set_timeout(&rt_log_timer, rt_log_interval);
}
void NS_CLASS log_cleanup()
{
if (log_to_file && log_file_fd) {
if (NS_OUTSIDE_CLASS close(log_file_fd) < 0)
fprintf(stderr, "Could not close log_file_fd!\n");
}
}
void NS_CLASS write_to_log_file(char *msg, int len)
{
if (!log_file_fd) {
fprintf(stderr, "Could not write to log file\n");
return;
}
if (len <= 0) {
fprintf(stderr, "len=0\n");
return;
}
if (write(log_file_fd, msg, len) < 0)
perror("Could not write to log file");
}
char *packet_type(u_int type)
{
static char temp[50];
switch (type) {
case AODV_RREQ:
return "AODV_RREQ";
case AODV_RREP:
return "AODV_RREP";
case AODV_RERR:
return "AODV_RERR";
default:
sprintf(temp, "Unknown packet type %d", type);
return temp;
}
}
void NS_CLASS alog(int type, int errnum, const char *function, char *format,
...)
{
va_list ap;
static char buffer[256] = "";
static char log_buf[1024];
char *msg;
struct timeval now;
struct tm *time;
int len = 0;
/* NS_PORT: Include IP address in log */
#ifdef NS_PORT
if (DEV_NR(NS_DEV_NR).enabled == 1) {
len += sprintf(log_buf + len, "%s: ",
ip_to_str(DEV_NR(NS_DEV_NR).ipaddr));
}
#endif /* NS_PORT */
va_start(ap, format);
if (type == LOG_WARNING)
msg = &buffer[9];
else
msg = buffer;
vsprintf(msg, format, ap);
va_end(ap);
if (!debug && !log_to_file)
goto syslog;
gettimeofday(&now, NULL);
#ifdef NS_PORT
time = gmtime(&now.tv_sec);
#else
time = localtime(&now.tv_sec);
#endif
/* if (type <= LOG_NOTICE) */
/* len += sprintf(log_buf + len, "%s: ", progname); */
len += sprintf(log_buf + len, "%02d:%02d:%02d.%03ld %s: %s", time->tm_hour,
time->tm_min, time->tm_sec, now.tv_usec / 1000, function,
msg);
if (errnum == 0)
len += sprintf(log_buf + len, "\n");
else
len += sprintf(log_buf + len, ": %s\n", strerror(errnum));
if (len > 1024) {
fprintf(stderr, "alog(): buffer to small! len = %d\n", len);
goto syslog;
}
/* OK, we are clear to write the buffer to the aodv log file... */
if (log_to_file)
write_to_log_file(log_buf, len);
/* If we have the debug option set, also write to stdout */
if (debug)
printf("%s", log_buf);
/* Syslog all messages that are of severity LOG_NOTICE or worse */
syslog:
if (type <= LOG_NOTICE) {
if (errnum != 0) {
errno = errnum;
syslog(type, "%s: %s: %m", function, msg);
} else
syslog(type, "%s: %s", function, msg);
}
/* Exit on error */
if (type <= LOG_ERR)
exit(-1);
}
char *NS_CLASS rreq_flags_to_str(RREQ * rreq)
{
static char buf[5];
int len = 0;
char *str;
if (rreq->j)
buf[len++] = 'J';
if (rreq->r)
buf[len++] = 'R';
if (rreq->g)
buf[len++] = 'G';
if (rreq->d)
buf[len++] = 'D';
buf[len] = '\0';
str = buf;
return str;
}
char *NS_CLASS rrep_flags_to_str(RREP * rrep)
{
static char buf[3];
int len = 0;
char *str;
if (rrep->r)
buf[len++] = 'R';
if (rrep->a)
buf[len++] = 'A';
buf[len] = '\0';
str = buf;
return str;
}
void NS_CLASS log_pkt_fields(AODV_msg * msg)
{
RREQ *rreq;
RREP *rrep;
RERR *rerr;
struct in_addr dest, orig;
switch (msg->type) {
case AODV_RREQ:
rreq = (RREQ *) msg;
dest.s_addr = rreq->dest_addr;
orig.s_addr = rreq->orig_addr;
DEBUG(LOG_DEBUG, 0,
"rreq->flags:%s rreq->hopcount=%d rreq->rreq_id=%ld",
rreq_flags_to_str(rreq), rreq->hcnt, ntohl(rreq->rreq_id));
DEBUG(LOG_DEBUG, 0, "rreq->dest_addr:%s rreq->dest_seqno=%lu",
ip_to_str(dest), ntohl(rreq->dest_seqno));
DEBUG(LOG_DEBUG, 0, "rreq->orig_addr:%s rreq->orig_seqno=%ld",
ip_to_str(orig), ntohl(rreq->orig_seqno));
break;
case AODV_RREP:
rrep = (RREP *) msg;
dest.s_addr = rrep->dest_addr;
orig.s_addr = rrep->orig_addr;
DEBUG(LOG_DEBUG, 0, "rrep->flags:%s rrep->hcnt=%d",
rrep_flags_to_str(rrep), rrep->hcnt);
DEBUG(LOG_DEBUG, 0, "rrep->dest_addr:%s rrep->dest_seqno=%d",
ip_to_str(dest), ntohl(rrep->dest_seqno));
DEBUG(LOG_DEBUG, 0, "rrep->orig_addr:%s rrep->lifetime=%d",
ip_to_str(orig), ntohl(rrep->lifetime));
break;
case AODV_RERR:
rerr = (RERR *) msg;
DEBUG(LOG_DEBUG, 0, "rerr->dest_count:%d rerr->flags=%s",
rerr->dest_count, rerr->n ? "N" : "-");
break;
}
}
char *NS_CLASS rt_flags_to_str(u_int16_t flags)
{
static char buf[5];
int len = 0;
char *str;
if (flags & RT_UNIDIR)
buf[len++] = 'U';
if (flags & RT_REPAIR)
buf[len++] = 'R';
if (flags & RT_INET_DEST)
buf[len++] = 'I';
if (flags & RT_GATEWAY)
buf[len++] = 'G';
buf[len] = '\0';
str = buf;
return str;
}
char *NS_CLASS state_to_str(u_int8_t state)
{
if (state == VALID)
return "VAL";
else if (state == INVALID)
return "INV";
else
return "?";
}
char *NS_CLASS devs_ip_to_str()
{
static char buf[MAX_NR_INTERFACES * IFNAMSIZ];
char *str;
int i, index = 0;
for (i = 0; i < MAX_NR_INTERFACES; i++) {
if (!DEV_NR(i).enabled)
continue;
index += sprintf(buf + index, "%s,", ip_to_str(DEV_NR(i).ipaddr));
}
str = buf;
return str;
}
void NS_CLASS print_rt_table(void *arg)
{
char rt_buf[2048], ifname[64], seqno_str[11];
int len = 0;
int i = 0;
struct timeval now;
struct tm *time;
ssize_t written;
if (rt_tbl.num_entries == 0)
goto schedule;
gettimeofday(&now, NULL);
#ifdef NS_PORT
time = gmtime(&now.tv_sec);
#else
time = localtime(&now.tv_sec);
#endif
len +=
sprintf(rt_buf,
"# Time: %02d:%02d:%02d.%03ld IP: %s seqno: %u entries/active: %u/%u\n",
time->tm_hour, time->tm_min, time->tm_sec, now.tv_usec / 1000,
devs_ip_to_str(), this_host.seqno, rt_tbl.num_entries,
rt_tbl.num_active);
len +=
sprintf(rt_buf + len,
"%-15s %-15s %-3s %-3s %-5s %-6s %-5s %-5s %-15s\n",
"Destination", "Next hop", "HC", "St.", "Seqno", "Expire",
"Flags", "Iface", "Precursors");
written = write(log_rt_fd, rt_buf, len);
len = 0;
for (i = 0; i < RT_TABLESIZE; i++) {
list_t *pos;
list_foreach(pos, &rt_tbl.tbl[i]) {
rt_table_t *rt = (rt_table_t *) pos;
if (rt->dest_seqno == 0)
sprintf(seqno_str, "-");
else
sprintf(seqno_str, "%u", rt->dest_seqno);
/* Print routing table entries one by one... */
if (list_empty(&rt->precursors))
len += sprintf(rt_buf + len,
"%-15s %-15s %-3d %-3s %-5s %-6lu %-5s %-5s\n",
ip_to_str(rt->dest_addr),
ip_to_str(rt->next_hop), rt->hcnt,
state_to_str(rt->state), seqno_str,
(rt->hcnt == 255) ? 0 :
timeval_diff(&rt->rt_timer.timeout, &now),
rt_flags_to_str(rt->flags),
if_indextoname(rt->ifindex, ifname));
else {
list_t *pos2;
len += sprintf(rt_buf + len,
"%-15s %-15s %-3d %-3s %-5s %-6lu %-5s %-5s %-15s\n",
ip_to_str(rt->dest_addr),
ip_to_str(rt->next_hop), rt->hcnt,
state_to_str(rt->state), seqno_str,
(rt->hcnt == 255) ? 0 :
timeval_diff(&rt->rt_timer.timeout, &now),
rt_flags_to_str(rt->flags),
if_indextoname(rt->ifindex, ifname),
ip_to_str(((precursor_t *) rt->precursors.next)->
neighbor));
/* Print all precursors for the current routing entry */
list_foreach(pos2, &rt->precursors) {
precursor_t *pr = (precursor_t *) pos2;
/* Skip first entry since it is already printed */
if (pos2->prev == &rt->precursors)
continue;
len += sprintf(rt_buf + len, "%64s %-15s\n", " ",
ip_to_str(pr->neighbor));
/* Since the precursor list is grown dynamically
* the write buffer should be flushed for every
* entry to avoid buffer overflows */
written = write(log_rt_fd, rt_buf, len);
len = 0;
}
}
if (len > 0) {
written = write(log_rt_fd, rt_buf, len);
len = 0;
}
}
}
/* Schedule a new printing of routing table... */
schedule:
timer_set_timeout(&rt_log_timer, rt_log_interval);
}
/* This function lets you print more than one IP address at the same time */
char *NS_CLASS ip_to_str(struct in_addr addr)
{
static char buf[16 * 4];
static int index = 0;
char *str;
#ifdef NS_PORT
#undef htonl
addr.s_addr = htonl(addr.s_addr);
#endif
strcpy(&buf[index], inet_ntoa(addr));
str = &buf[index];
index += 16;
index %= 64;
return str;
}