2 $Id: inet_gr.c,v 1.13 2000/10/08 01:00:44 ecki Exp $
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 1999-10-07 - Kurt Garloff <garloff@suse.de> - do host (instead of network) name
8 lookup for gws and hosts
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <arpa/nameser.h>
19 /* #include <net/route.h> realy broken */
29 #include "net-support.h"
30 #include "pathnames.h"
32 #include "net-features.h"
34 extern struct aftype inet_aftype
;
36 extern char *INET_sprintmask(struct sockaddr
*sap
, int numeric
,
37 unsigned int netmask
);
39 int rprint_fib(int ext
, int numeric
)
41 char buff
[1024], iface
[16], flags
[64];
42 char gate_addr
[128], net_addr
[128];
44 int num
, iflags
, metric
, refcnt
, use
, mss
, window
, irtt
;
45 FILE *fp
= fopen(_PATH_PROCNET_ROUTE
, "r");
49 perror(_PATH_PROCNET_ROUTE
);
50 printf(_("INET (IPv4) not configured in this system.\n"));
53 printf(_("Kernel IP routing table\n"));
56 printf(_("Destination Gateway Genmask "
57 "Flags Metric Ref Use Iface\n"));
59 printf(_("Destination Gateway Genmask "
60 "Flags MSS Window irtt Iface\n"));
62 printf(_("Destination Gateway Genmask "
63 "Flags Metric Ref Use Iface "
64 "MSS Window irtt\n"));
70 fmt
= proc_gen_fmt(_PATH_PROCNET_ROUTE
, 0, fp
,
72 "Destination", "%128s",
83 /* "%16s %128s %128s %X %d %d %d %128s %d %d %d\n" */
88 while (fgets(buff
, 1023, fp
)) {
89 struct sockaddr snet_target
, snet_gateway
, snet_mask
;
90 struct sockaddr_in
*sin_netmask
;
92 num
= sscanf(buff
, fmt
,
93 iface
, net_addr
, gate_addr
,
94 &iflags
, &refcnt
, &use
, &metric
, mask_addr
,
95 &mss
, &window
, &irtt
);
96 if (num
< 10 || !(iflags
& RTF_UP
))
99 /* Fetch and resolve the target address. */
100 (void) inet_aftype
.input(1, net_addr
, &snet_target
);
102 /* Fetch and resolve the gateway address. */
103 (void) inet_aftype
.input(1, gate_addr
, &snet_gateway
);
105 /* Fetch and resolve the genmask. */
106 (void) inet_aftype
.input(1, mask_addr
, &snet_mask
);
108 sin_netmask
= (struct sockaddr_in
*)&snet_mask
;
109 strcpy(net_addr
, INET_sprintmask(&snet_target
,
110 (numeric
| 0x8000 | (iflags
& RTF_HOST
? 0x4000: 0)),
111 sin_netmask
->sin_addr
.s_addr
));
114 strcpy(gate_addr
, inet_aftype
.sprint(&snet_gateway
, numeric
| 0x4000));
115 gate_addr
[15] = '\0';
117 strcpy(mask_addr
, inet_aftype
.sprint(&snet_mask
, 1));
118 mask_addr
[15] = '\0';
120 /* Decode the flags. */
124 if (iflags
& RTF_GATEWAY
)
127 if (iflags
& RTF_REJECT
)
130 if (iflags
& RTF_HOST
)
132 if (iflags
& RTF_REINSTATE
)
134 if (iflags
& RTF_DYNAMIC
)
136 if (iflags
& RTF_MODIFIED
)
138 if (iflags
& RTF_DEFAULT
)
140 if (iflags
& RTF_ALLONLINK
)
142 if (iflags
& RTF_ADDRCONF
)
144 if (iflags
& RTF_NONEXTHOP
)
146 if (iflags
& RTF_EXPIRES
)
148 if (iflags
& RTF_CACHE
)
150 if (iflags
& RTF_FLOW
)
152 if (iflags
& RTF_POLICY
)
154 if (iflags
& RTF_LOCAL
)
156 if (iflags
& RTF_MTU
)
158 if (iflags
& RTF_WINDOW
)
160 if (iflags
& RTF_IRTT
)
162 if (iflags
& RTF_NOTCACHED
) /* 2.0.36 */
165 /* Print the info. */
168 if (iflags
& RTF_REJECT
)
169 printf("%-15s - %-15s %-5s %-6d - %7d -\n",
170 net_addr
, mask_addr
, flags
, metric
, use
);
173 printf("%-15s %-15s %-15s %-5s %-6d %-2d %7d %s\n",
174 net_addr
, gate_addr
, mask_addr
, flags
,
175 metric
, refcnt
, use
, iface
);
179 if (iflags
& RTF_REJECT
)
180 printf("%-15s - %-15s %-5s - - - -\n",
181 net_addr
, mask_addr
, flags
);
184 printf("%-15s %-15s %-15s %-5s %5d %-5d %6d %s\n",
185 net_addr
, gate_addr
, mask_addr
, flags
,
186 mss
, window
, irtt
, iface
);
190 if (iflags
& RTF_REJECT
)
191 printf("%-15s - %-15s %-5s %-6d - %7d - - - -\n",
192 net_addr
, mask_addr
, flags
, metric
, use
);
195 printf("%-15s %-15s %-15s %-5s %-6d %-3d %6d %-6.6s %-5d %-6d %d\n",
196 net_addr
, gate_addr
, mask_addr
, flags
,
197 metric
, refcnt
, use
, iface
, mss
, window
, irtt
);
206 int rprint_cache(int ext
, int numeric
)
208 char buff
[1024], iface
[16], flags
[64];
209 char gate_addr
[128], dest_addr
[128], specdst
[128];
211 struct sockaddr snet
;
213 int num
, format
, metric
, refcnt
, use
, mss
, window
, irtt
, hh
, hhref
, hhuptod
, arp
, tos
;
216 FILE *fp
= fopen(_PATH_PROCNET_RTCACHE
, "r");
219 perror(_PATH_PROCNET_RTCACHE
);
220 printf(_("INET (IPv4) not configured in this system.\n"));
224 /* Okay, first thing we need to know is the format of the rt_cache.
225 * I am aware of two possible layouts:
227 * "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\tMetric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\tHHUptod\tSpecDst"
228 * "%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"
231 * "Iface\tDestination\tGateway \tFlags\tRefCnt\tUse\tMetric\tSource\t\tMTU\tWindow\tIRTT\tHH\tARP"
232 * "%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"
235 format
= proc_guess_fmt(_PATH_PROCNET_RTCACHE
, fp
, "IRTT",1,"TOS",2,"HHRef",4,"HHUptod",8,"SpecDst",16,"HH",32,"ARP",64,NULL
);
237 printf(_("Kernel IP routing cache\n"));
240 case -1: /* I/O Error */
241 perror(_PATH_PROCNET_RTCACHE
);
244 case 63: /* 2.2.0 Format */
247 case 97: /* 2.0.36 Format */
251 printf("ERROR: proc_guess_fmt(%s,... returned: %d\n",_PATH_PROCNET_RTCACHE
, format
);
258 printf(_("Source Destination Gateway "
259 "Flags Metric Ref Use Iface\n"));
261 printf(_("Source Destination Gateway "
262 "Flags MSS Window irtt Iface\n"));
266 printf(_("Source Destination Gateway "
267 "Flags Metric Ref Use Iface "
268 "MSS Window irtt HH Arp\n"));
270 fmt
= proc_gen_fmt(_PATH_PROCNET_RTCACHE
, 0, fp
,
272 "Destination", "%128s",
285 /* "%16s %128s %128s %X %d %d %d %128s %d %d %d %d %d\n" */
290 printf(_("Source Destination Gateway "
291 "Flags Metric Ref Use Iface "
292 "MSS Window irtt TOS HHRef HHUptod SpecDst\n"));
293 fmt
= proc_gen_fmt(_PATH_PROCNET_RTCACHE
, 0, fp
,
295 "Destination", "%128s",
310 /* "%16s %128s %128s %X %d %d %d %128s %d %d %d %d %d %128s\n" */
317 hh
= 0; hhref
= 0; hhuptod
= 0;
319 while (fgets(buff
, 1023, fp
)) {
321 num
= sscanf(buff
, fmt
,
322 iface
, dest_addr
, gate_addr
,
323 &iflags
, &refcnt
, &use
, &metric
, src_addr
,
324 &mss
, &window
, &irtt
, &hh
, &arp
);
329 num
= sscanf(buff
, fmt
,
330 iface
, dest_addr
, gate_addr
,
331 &iflags
, &refcnt
, &use
, &metric
, src_addr
,
332 &mss
, &window
, &irtt
, &tos
, &hhref
, &hhuptod
, &specdst
);
338 /* Fetch and resolve the target address. */
339 (void) inet_aftype
.input(1, dest_addr
, &snet
);
340 strcpy(dest_addr
, inet_aftype
.sprint(&snet
, numeric
));
341 dest_addr
[15] = '\0';
343 /* Fetch and resolve the gateway address. */
344 (void) inet_aftype
.input(1, gate_addr
, &snet
);
345 strcpy(gate_addr
, inet_aftype
.sprint(&snet
, numeric
));
346 gate_addr
[15] = '\0';
348 /* Fetch and resolve the source. */
349 (void) inet_aftype
.input(1, src_addr
, &snet
);
350 strcpy(src_addr
, inet_aftype
.sprint(&snet
, numeric
));
353 /* Fetch and resolve the SpecDst addrerss. */
354 (void) inet_aftype
.input(1, specdst
, &snet
);
355 strcpy(specdst
, inet_aftype
.sprint(&snet
, numeric
));
358 /* Decode the flags. */
363 if (iflags
& RTF_HOST
)
366 if (iflags
& RTF_GATEWAY
)
369 if (iflags
& RTF_REJECT
)
372 if (iflags
& RTF_REINSTATE
)
374 if (iflags
& RTF_DYNAMIC
)
376 if (iflags
& RTF_MODIFIED
)
379 /* possible collision with 2.0 flags U and H */
381 if (iflags
& RTCF_DEAD
)
383 if (iflags
& RTCF_ONLINK
)
386 if (iflags
& RTCF_NOTIFY
)
388 if (iflags
& RTCF_DIRECTDST
)
390 if (iflags
& RTCF_TPROXY
)
392 if (iflags
& RTCF_FAST
)
394 if (iflags
& RTCF_MASQ
)
396 if (iflags
& RTCF_SNAT
)
398 if (iflags
& RTCF_DOREDIRECT
)
400 if (iflags
& RTCF_DIRECTSRC
)
402 if (iflags
& RTCF_DNAT
)
404 if (iflags
& RTCF_BROADCAST
)
406 if (iflags
& RTCF_MULTICAST
)
408 if (iflags
& RTCF_REJECT
)
410 if (iflags
& RTCF_LOCAL
)
412 /* Print the info. */
414 printf("%-15s %-15s %-15s %-5s %-6d %-2d %7d %s\n",
415 src_addr
, dest_addr
, gate_addr
, flags
,
416 metric
, refcnt
, use
, iface
);
419 printf("%-15s %-15s %-15s %-5s %5d %-5d %6d %s\n",
420 src_addr
, dest_addr
, gate_addr
, flags
,
421 mss
, window
, irtt
, iface
);
425 printf("%-15s %-15s %-15s %-5s %-6d %-3d %6d %-6.6s %-5d %-6d %-5d %-3d %d\n",
426 src_addr
, dest_addr
, gate_addr
, flags
,
427 metric
, refcnt
, use
, iface
, mss
, window
, irtt
, hh
, arp
);
432 printf("%-15s %-15s %-15s %-5s %-6d %-3d %6d %-6.6s %-5d %-6d %-5d %-3d %-3d %-3d %15s\n",
433 src_addr
, dest_addr
, gate_addr
, flags
,
434 metric
, refcnt
, use
, iface
, mss
, window
, irtt
, tos
, hhref
, hhuptod
, specdst
);
444 int INET_rprint(int options
)
446 int ext
= options
& FLAG_EXT
;
447 int numeric
= options
& (FLAG_NUM_HOST
| FLAG_SYM
);
450 if (options
& FLAG_FIB
)
451 if ((rc
= rprint_fib(ext
, numeric
)))
453 if (options
& FLAG_CACHE
)
454 rc
= rprint_cache(ext
, numeric
);
459 #endif /* HAVE_AFINET */