OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [userland/] [route/] [lib/] [inet_gr.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 745 simons
/*
2
   $Id: inet_gr.c,v 1.1 2002-03-17 19:58:53 simons Exp $
3
 
4
   Modifications:
5
   1998-07-01 - Arnaldo Carvalho de Melo - GNU gettext instead of catgets
6
   1999-01-01 - Bernd Eckenfels          - fixed the routing cache printouts
7
 */
8
 
9
#include "config.h"
10
 
11
#if HAVE_AFINET
12
#include <sys/types.h>
13
#include <sys/socket.h>
14
#include <netinet/in.h>
15
#include <arpa/inet.h>
16
#include <arpa/nameser.h>
17
/* #include <net/route.h> realy broken */
18
#include <ctype.h>
19
#include <errno.h>
20
#include <netdb.h>
21
#include <resolv.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <stdio.h>
25
#include <unistd.h>
26
#include "version.h"
27
#include "net-support.h"
28
#include "pathnames.h"
29
#include "intl.h"
30
#include "net-features.h"
31
#include "proc.h"
32
extern struct aftype inet_aftype;
33
 
34
extern char *INET_sprintmask(struct sockaddr *sap, int numeric,
35
                             unsigned int netmask);
36
 
37
int rprint_fib(int ext, int numeric)
38
{
39
    char buff[1024], iface[16], flags[64];
40
    char gate_addr[128], net_addr[128];
41
    char mask_addr[128];
42
    int num, iflags, metric, refcnt, use, mss, window, irtt;
43
    FILE *fp = fopen(_PATH_PROCNET_ROUTE, "r");
44
    char *fmt;
45
 
46
    if (!fp) {
47
        perror(_PATH_PROCNET_ROUTE);
48
        printf(_("INET (IPv4) not configured in this system.\n"));
49
        return 1;
50
    }
51
    printf(_("Kernel IP routing table\n"));
52
 
53
    if (ext == 1)
54
        printf(_("Destination     Gateway         Genmask         "
55
                 "Flags Metric Ref    Use Iface\n"));
56
    if (ext == 2)
57
        printf(_("Destination     Gateway         Genmask         "
58
                 "Flags   MSS Window  irtt Iface\n"));
59
    if (ext >= 3)
60
        printf(_("Destination     Gateway         Genmask         "
61
                 "Flags Metric Ref    Use Iface    "
62
                 "MSS   Window irtt\n"));
63
 
64
    irtt = 0;
65
    window = 0;
66
    mss = 0;
67
 
68
    fmt = proc_gen_fmt(_PATH_PROCNET_ROUTE, 0, fp,
69
                       "Iface", "%16s",
70
                       "Destination", "%128s",
71
                       "Gateway", "%128s",
72
                       "Flags", "%X",
73
                       "RefCnt", "%d",
74
                       "Use", "%d",
75
                       "Metric", "%d",
76
                       "Mask", "%128s",
77
                       "MTU", "%d",
78
                       "Window", "%d",
79
                       "IRTT", "%d",
80
                       NULL);
81
    /* "%16s %128s %128s %X %d %d %d %128s %d %d %d\n" */
82
 
83
    if (!fmt)
84
        return 1;
85
 
86
    while (fgets(buff, 1023, fp)) {
87
        struct sockaddr snet_target, snet_gateway, snet_mask;
88
        struct sockaddr_in *sin_netmask;
89
 
90
        num = sscanf(buff, fmt,
91
                     iface, net_addr, gate_addr,
92
                     &iflags, &refcnt, &use, &metric, mask_addr,
93
                     &mss, &window, &irtt);
94
        if (num < 10 || !(iflags & RTF_UP))
95
            continue;
96
 
97
        /* Fetch and resolve the target address. */
98
        (void) inet_aftype.input(1, net_addr, &snet_target);
99
 
100
        /* Fetch and resolve the gateway address. */
101
        (void) inet_aftype.input(1, gate_addr, &snet_gateway);
102
 
103
        /* Fetch and resolve the genmask. */
104
        (void) inet_aftype.input(1, mask_addr, &snet_mask);
105
 
106
        sin_netmask = (struct sockaddr_in *)&snet_mask;
107
        strcpy(net_addr, INET_sprintmask(&snet_target,
108
                                         (numeric | 0x8000),
109
                                         sin_netmask->sin_addr.s_addr));
110
        net_addr[15] = '\0';
111
 
112
        strcpy(gate_addr, inet_aftype.sprint(&snet_gateway, numeric));
113
        gate_addr[15] = '\0';
114
 
115
        strcpy(mask_addr, inet_aftype.sprint(&snet_mask, 1));
116
        mask_addr[15] = '\0';
117
 
118
        /* Decode the flags. */
119
        flags[0] = '\0';
120
        if (iflags & RTF_UP)
121
            strcat(flags, "U");
122
        if (iflags & RTF_GATEWAY)
123
            strcat(flags, "G");
124
#if HAVE_RTF_REJECT
125
        if (iflags & RTF_REJECT)
126
            strcpy(flags, "!");
127
#endif
128
        if (iflags & RTF_HOST)
129
            strcat(flags, "H");
130
        if (iflags & RTF_REINSTATE)
131
            strcat(flags, "R");
132
        if (iflags & RTF_DYNAMIC)
133
            strcat(flags, "D");
134
        if (iflags & RTF_MODIFIED)
135
            strcat(flags, "M");
136
        if (iflags & RTF_DEFAULT)
137
            strcat(flags, "d");
138
        if (iflags & RTF_ALLONLINK)
139
            strcat(flags, "a");
140
        if (iflags & RTF_ADDRCONF)
141
            strcat(flags, "c");
142
        if (iflags & RTF_NONEXTHOP)
143
            strcat(flags, "o");
144
        if (iflags & RTF_EXPIRES)
145
            strcat(flags, "e");
146
        if (iflags & RTF_CACHE)
147
            strcat(flags, "c");
148
        if (iflags & RTF_FLOW)
149
            strcat(flags, "f");
150
        if (iflags & RTF_POLICY)
151
            strcat(flags, "p");
152
        if (iflags & RTF_LOCAL)
153
            strcat(flags, "l");
154
        if (iflags & RTF_MTU)
155
            strcat(flags, "u");
156
        if (iflags & RTF_WINDOW)
157
            strcat(flags, "w");
158
        if (iflags & RTF_IRTT)
159
            strcat(flags, "i");
160
        if (iflags & RTF_NOTCACHED) /* 2.0.36 */
161
            strcat(flags, "n");
162
 
163
        /* Print the info. */
164
        if (ext == 1) {
165
#if HAVE_RTF_REJECT
166
            if (iflags & RTF_REJECT)
167
                printf("%-15s -               %-15s %-5s %-6d -  %7d -\n",
168
                       net_addr, mask_addr, flags, metric, use);
169
            else
170
#endif
171
                printf("%-15s %-15s %-15s %-5s %-6d %-2d %7d %s\n",
172
                       net_addr, gate_addr, mask_addr, flags,
173
                       metric, refcnt, use, iface);
174
        }
175
        if (ext == 2) {
176
#if HAVE_RTF_REJECT
177
            if (iflags & RTF_REJECT)
178
                printf("%-15s -               %-15s %-5s     - -          - -\n",
179
                       net_addr, mask_addr, flags);
180
            else
181
#endif
182
                printf("%-15s %-15s %-15s %-5s %5d %-5d %6d %s\n",
183
                       net_addr, gate_addr, mask_addr, flags,
184
                       mss, window, irtt, iface);
185
        }
186
        if (ext >= 3) {
187
#if HAVE_RTF_REJECT
188
            if (iflags & RTF_REJECT)
189
                printf("%-15s -               %-15s %-5s %-6d -  %7d -        -     -      -\n",
190
                       net_addr, mask_addr, flags, metric, use);
191
            else
192
#endif
193
                printf("%-15s %-15s %-15s %-5s %-6d %-3d %6d %-6.6s   %-5d %-6d %d\n",
194
                       net_addr, gate_addr, mask_addr, flags,
195
                       metric, refcnt, use, iface, mss, window, irtt);
196
        }
197
    }
198
 
199
    free(fmt);
200
    (void) fclose(fp);
201
    return (0);
202
}
203
 
204
int rprint_cache(int ext, int numeric)
205
{
206
    char buff[1024], iface[16], flags[64];
207
    char gate_addr[128], dest_addr[128], specdst[128];
208
    char src_addr[128];
209
    struct sockaddr snet;
210
    unsigned int iflags;
211
    int num, format, metric, refcnt, use, mss, window, irtt, hh, hhref, hhuptod, arp, tos;
212
    char *fmt = NULL;
213
 
214
    FILE *fp = fopen(_PATH_PROCNET_RTCACHE, "r");
215
 
216
    if (!fp) {
217
        perror(_PATH_PROCNET_RTCACHE);
218
        printf(_("INET (IPv4) not configured in this system.\n"));
219
        return 1;
220
    }
221
 
222
   /* Okay, first thing we need to know is the format of the rt_cache.
223
    * I am aware of two possible layouts:
224
    * 2.2.0
225
    * "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\tMetric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\tHHUptod\tSpecDst"
226
    * "%s\t%08lX\t%08lX\t%8X\t%d\t%u\t%d\t%08lX\t%d\t%u\t%u\t%02X\t%d\t%1d\t%08X"
227
    *
228
    * 2.0.36
229
    * "Iface\tDestination\tGateway \tFlags\tRefCnt\tUse\tMetric\tSource\t\tMTU\tWindow\tIRTT\tHH\tARP"
230
    * "%s\t%08lX\t%08lX\t%02X\t%d\t%u\t%d\t%08lX\t%d\t%lu\t%u\t%d\t%1d"
231
    */
232
 
233
    format = proc_guess_fmt(_PATH_PROCNET_RTCACHE, fp, "IRTT",1,"TOS",2,"HHRef",4,"HHUptod",8,"SpecDst",16,"HH",32,"ARP",64,NULL);
234
 
235
    printf(_("Kernel IP routing cache\n"));
236
 
237
    switch(format) {
238
        case -1: /* I/O Error */
239
          perror(_PATH_PROCNET_RTCACHE);
240
          exit(-1);
241
          break;
242
        case 63: /* 2.2.0 Format */
243
          format = 2;
244
          break;
245
        case 97: /* 2.0.36 Format */
246
          format = 1;
247
          break;
248
        default:
249
          printf("ERROR: proc_guess_fmt(%s,... returned: %d\n",_PATH_PROCNET_RTCACHE, format);
250
          break;
251
    }
252
 
253
    rewind(fp);
254
 
255
    if (ext == 1)
256
      printf(_("Source          Destination     Gateway         "
257
               "Flags Metric Ref    Use Iface\n"));
258
    if (ext == 2)
259
      printf(_("Source          Destination     Gateway         "
260
               "Flags   MSS Window  irtt Iface\n"));
261
 
262
    if (format == 1) {
263
      if (ext >= 3)
264
        printf(_("Source          Destination     Gateway         "
265
                 "Flags Metric Ref    Use Iface    "
266
                 "MSS   Window irtt  HH  Arp\n"));
267
 
268
      fmt = proc_gen_fmt(_PATH_PROCNET_RTCACHE, 0, fp,
269
                       "Iface", "%16s",
270
                       "Destination", "%128s",
271
                       "Gateway", "%128s",
272
                       "Flags", "%X",
273
                       "RefCnt", "%d",
274
                       "Use", "%d",
275
                       "Metric", "%d",
276
                       "Source", "%128s",
277
                       "MTU", "%d",
278
                       "Window", "%d",
279
                       "IRTT", "%d",
280
                       "HH", "%d",
281
                       "ARP", "%d",
282
                       NULL);
283
      /* "%16s %128s %128s %X %d %d %d %128s %d %d %d %d %d\n" */
284
    }
285
 
286
    if (format == 2) {
287
      if (ext >= 3)
288
        printf(_("Source          Destination     Gateway         "
289
                 "Flags Metric Ref    Use Iface    "
290
                 "MSS   Window irtt  TOS HHRef HHUptod     SpecDst\n"));
291
        fmt = proc_gen_fmt(_PATH_PROCNET_RTCACHE, 0, fp,
292
                       "Iface", "%16s",
293
                       "Destination", "%128s",
294
                       "Gateway", "%128s",
295
                       "Flags", "%X",
296
                       "RefCnt", "%d",
297
                       "Use", "%d",
298
                       "Metric", "%d",
299
                       "Source", "%128s",
300
                       "MTU", "%d",
301
                       "Window", "%d",
302
                       "IRTT", "%d",
303
                       "TOS", "%d",
304
                       "HHRef", "%d",
305
                       "HHUptod", "%d",
306
                       "SpecDst", "%128s",
307
                       NULL);
308
      /* "%16s %128s %128s %X %d %d %d %128s %d %d %d %d %d %128s\n" */
309
    }
310
 
311
 
312
    irtt = 0;
313
    window = 0;
314
    mss = 0;
315
    hh = 0; hhref = 0; hhuptod = 0;
316
    arp = 0; tos = 0;
317
    while (fgets(buff, 1023, fp)) {
318
        if (format == 1) {
319
          num = sscanf(buff, fmt,
320
                     iface, dest_addr, gate_addr,
321
                     &iflags, &refcnt, &use, &metric, src_addr,
322
                     &mss, &window, &irtt, &hh, &arp);
323
          if (num < 12)
324
            continue;
325
        }
326
        if (format == 2) {
327
          num = sscanf(buff, fmt,
328
                     iface, dest_addr, gate_addr,
329
                     &iflags, &refcnt, &use, &metric, src_addr,
330
                     &mss, &window, &irtt, &tos, &hhref, &hhuptod, &specdst);
331
          if (num < 12)
332
            continue;
333
        }
334
 
335
 
336
        /* Fetch and resolve the target address. */
337
        (void) inet_aftype.input(1, dest_addr, &snet);
338
        strcpy(dest_addr, inet_aftype.sprint(&snet, numeric));
339
        dest_addr[15] = '\0';
340
 
341
        /* Fetch and resolve the gateway address. */
342
        (void) inet_aftype.input(1, gate_addr, &snet);
343
        strcpy(gate_addr, inet_aftype.sprint(&snet, numeric));
344
        gate_addr[15] = '\0';
345
 
346
        /* Fetch and resolve the source. */
347
        (void) inet_aftype.input(1, src_addr, &snet);
348
        strcpy(src_addr, inet_aftype.sprint(&snet, numeric));
349
        src_addr[15] = '\0';
350
 
351
        /* Fetch and resolve the SpecDst addrerss. */
352
        (void) inet_aftype.input(1, specdst, &snet);
353
        strcpy(specdst, inet_aftype.sprint(&snet, numeric));
354
        specdst[15] = '\0';
355
 
356
        /* Decode the flags. */
357
        flags[0] = '\0';
358
if (format == 1) {
359
        if (iflags & RTF_UP)
360
            strcat(flags, "U");
361
        if (iflags & RTF_HOST)
362
            strcat(flags, "H");
363
}
364
        if (iflags & RTF_GATEWAY)
365
            strcat(flags, "G");
366
#if HAVE_RTF_REJECT
367
        if (iflags & RTF_REJECT)
368
            strcpy(flags, "!");
369
#endif
370
        if (iflags & RTF_REINSTATE)
371
            strcat(flags, "R");
372
        if (iflags & RTF_DYNAMIC)
373
            strcat(flags, "D");
374
        if (iflags & RTF_MODIFIED)
375
            strcat(flags, "M");
376
 
377
/* possible collision with 2.0 flags U and H */
378
if (format == 2) {
379
        if (iflags & RTCF_DEAD)
380
            strcat(flags, "-");
381
        if (iflags & RTCF_ONLINK)
382
            strcat(flags, "o");
383
}
384
        if (iflags & RTCF_NOTIFY)
385
            strcat(flags, "n");
386
        if (iflags & RTCF_DIRECTDST)
387
            strcat(flags, "d");
388
        if (iflags & RTCF_TPROXY)
389
            strcat(flags, "t");
390
        if (iflags & RTCF_FAST)
391
            strcat(flags, "f");
392
        if (iflags & RTCF_MASQ)
393
            strcat(flags, "q");
394
        if (iflags & RTCF_SNAT)
395
            strcat(flags, "Ns");
396
        if (iflags & RTCF_DOREDIRECT)
397
            strcat(flags, "r");
398
        if (iflags & RTCF_DIRECTSRC)
399
            strcat(flags, "i");
400
        if (iflags & RTCF_DNAT)
401
            strcat(flags, "Nd");
402
        if (iflags & RTCF_BROADCAST)
403
            strcat(flags, "b");
404
        if (iflags & RTCF_MULTICAST)
405
            strcat(flags, "m");
406
        if (iflags & RTCF_REJECT)
407
            strcat(flags, "#");
408
        if (iflags & RTCF_LOCAL)
409
            strcat(flags, "l");
410
        /* Print the info. */
411
        if (ext == 1) {
412
                printf("%-15s %-15s %-15s %-5s %-6d %-2d %7d %s\n",
413
                       src_addr, dest_addr, gate_addr, flags,
414
                       metric, refcnt, use, iface);
415
        }
416
        if (ext == 2) {
417
                printf("%-15s %-15s %-15s %-5s %5d %-5d %6d %s\n",
418
                       src_addr, dest_addr, gate_addr, flags,
419
                       mss, window, irtt, iface);
420
        }
421
        if (format == 1) {
422
          if (ext >= 3) {
423
                printf("%-15s %-15s %-15s %-5s %-6d %-3d %6d %-6.6s   %-5d %-6d %-5d %-3d %d\n",
424
                       src_addr, dest_addr, gate_addr, flags,
425
                 metric, refcnt, use, iface, mss, window, irtt, hh, arp);
426
          }
427
        }
428
        if (format == 2) {
429
          if (ext >= 3) {
430
                printf("%-15s %-15s %-15s %-5s %-6d %-3d %6d %-6.6s   %-5d %-6d %-5d %-3d %-3d   %-3d %15s\n",
431
                       src_addr, dest_addr, gate_addr, flags,
432
                 metric, refcnt, use, iface, mss, window, irtt, tos, hhref, hhuptod, specdst);
433
          }
434
        }
435
    }
436
 
437
    free(fmt);
438
    (void) fclose(fp);
439
    return (0);
440
}
441
 
442
int INET_rprint(int options)
443
{
444
    int ext = options & FLAG_EXT;
445
    int numeric = options & (FLAG_NUM | FLAG_SYM);
446
    int rc = E_INTERN;
447
 
448
    if (options & FLAG_FIB)
449
        if ((rc = rprint_fib(ext, numeric)))
450
            return (rc);
451
    if (options & FLAG_CACHE)
452
        rc = rprint_cache(ext, numeric);
453
 
454
    return (rc);
455
}
456
 
457
#endif                          /* HAVE_AFINET */

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.