📄 print_route6.c
字号:
/***************************************************************************** * * Copyright (C) 2003 Simon Fraser University and NewMIC * * 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 * * Author: Peter Lee <peter.lee@shaw.ca> * *****************************************************************************/#include "print_route6.h"#include <asm/types.h>#include <asm/param.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <arpa/nameser.h>#include <net/route.h>#include <ctype.h>#include <errno.h>#include <netdb.h>#include <resolv.h>#include <stdlib.h>#include <string.h>#include <stdio.h>#include <unistd.h>#define _PATH_PROCNET_ROUTE6 "/proc/net/ipv6_route"#define E_INTERN 2#define FLAG_EXT 3 /* AND-Mask */#define FLAG_NUM_HOST 4#define FLAG_SYM 32#define FLAG_FIB 128/* Like strncpy but make sure the resulting string is always 0 terminated. */ char *safe_strncpy(char *dst, const char *src, size_t size){ dst[size-1] = '\0'; return strncpy(dst,src,size-1); }static int INET6_resolve(char *name, struct sockaddr_in6 *sin6){ struct addrinfo req, *ai; int s; memset (&req, '\0', sizeof req); req.ai_family = AF_INET6; if ((s = getaddrinfo(name, NULL, &req, &ai))) { fprintf(stderr, "getaddrinfo: %s: %d\n", name, s); return -1; } memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6)); freeaddrinfo(ai); return (0);}static int INET6_rresolve(char *name, struct sockaddr_in6 *sin6, int numeric){ int s; /* Grmpf. -FvK */ if (sin6->sin6_family != AF_INET6) {#ifdef DEBUG fprintf(stderr, ("rresolve: unsupport address family %d !\n"), sin6->sin6_family);#endif errno = EAFNOSUPPORT; return (-1); } if (numeric & 0x7FFF) { inet_ntop(AF_INET6, &sin6->sin6_addr, name, 80); return (0); } if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { if (numeric & 0x8000) strcpy(name, "default"); else strcpy(name, "*"); return (0); } if ((s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, 255 /* !! */ , NULL, 0, 0))) { fputs("getnameinfo failed\n", stderr); return -1; } return (0);}/* Display an Internet socket address. *//* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */static char *INET6_sprint(struct sockaddr *sap, int numeric){ static char buff[128]; if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return safe_strncpy(buff, ("[NONE SET]"), sizeof(buff)); if (INET6_rresolve(buff, (struct sockaddr_in6 *) sap, numeric) != 0) return safe_strncpy(buff, ("[UNKNOWN]"), sizeof(buff)); return (buff);}static int INET6_getsock(char *bufp, struct sockaddr *sap){ struct sockaddr_in6 *sin6; sin6 = (struct sockaddr_in6 *) sap; sin6->sin6_family = AF_INET6; sin6->sin6_port = 0; if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0) return (-1); return 16; /* ?;) */}static int INET6_input(int type, char *bufp, struct sockaddr *sap){ switch (type) { case 1: return (INET6_getsock(bufp, sap)); default: return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap)); }}int rprint_fib6(int ext, int numeric){ char buff[4096], iface[16], flags[16]; char addr6[128], naddr6[128]; struct sockaddr_in6 saddr6, snaddr6; int num, iflags, metric, refcnt, use, prefix_len, slen; FILE *fp = fopen(_PATH_PROCNET_ROUTE6, "r"); char addr6p[8][5], saddr6p[8][5], naddr6p[8][5]; if (!fp) { perror(_PATH_PROCNET_ROUTE6); printf(("INET6 (IPv6) not configured in this system.\n")); return 1; } printf(("Kernel IPv6 routing table\n")); printf(("Destination " "Next Hop " "Flags Metric Ref Use Iface\n")); while (fgets(buff, 1023, fp)) { num = sscanf(buff, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %4s%4s%4s%4s%4s%4s%4s%4s %02x %4s%4s%4s%4s%4s%4s%4s%4s %08x %08x %08x %08x %s\n", addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7], &prefix_len, saddr6p[0], saddr6p[1], saddr6p[2], saddr6p[3], saddr6p[4], saddr6p[5], saddr6p[6], saddr6p[7], &slen, naddr6p[0], naddr6p[1], naddr6p[2], naddr6p[3], naddr6p[4], naddr6p[5], naddr6p[6], naddr6p[7], &metric, &use, &refcnt, &iflags, iface);#if 0 if (num < 23) continue;#endif if (!(iflags & RTF_UP)) continue; /* Fetch and resolve the target address. */ snprintf(addr6, sizeof(addr6), "%s:%s:%s:%s:%s:%s:%s:%s", addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7]); //inet6_aftype.input(1, addr6, (struct sockaddr *) &saddr6); INET6_input(1, addr6, (struct sockaddr *) &saddr6); //PL: /* snprintf(addr6, sizeof(addr6), "%s/%d", inet6_aftype.sprint((struct sockaddr *) &saddr6, 1), prefix_len); */ snprintf(addr6, sizeof(addr6), "%s/%d", INET6_sprint((struct sockaddr *) &saddr6, 1), prefix_len); /* Fetch and resolve the nexthop address. */ snprintf(naddr6, sizeof(naddr6), "%s:%s:%s:%s:%s:%s:%s:%s", naddr6p[0], naddr6p[1], naddr6p[2], naddr6p[3], naddr6p[4], naddr6p[5], naddr6p[6], naddr6p[7]); //inet6_aftype.input(1, naddr6, (struct sockaddr *) &snaddr6); INET6_input(1, naddr6, (struct sockaddr *) &snaddr6); /* snprintf(naddr6, sizeof(naddr6), "%s", inet6_aftype.sprint((struct sockaddr *) &snaddr6, 1)); */ snprintf(naddr6, sizeof(naddr6), "%s", INET6_sprint((struct sockaddr *) &snaddr6, 1)); /* Decode the flags. */ strcpy(flags, "U"); if (iflags & RTF_GATEWAY) strcat(flags, "G"); if (iflags & RTF_HOST) strcat(flags, "H"); if (iflags & RTF_DEFAULT) strcat(flags, "D"); if (iflags & RTF_ADDRCONF) strcat(flags, "A"); if (iflags & RTF_CACHE) strcat(flags, "C"); /* Print the info. */ printf("%-43s %-39s %-5s %-6d %-2d %7d %-8s\n", addr6, naddr6, flags, metric, refcnt, use, iface); } (void) fclose(fp); return (0);}void print_route6(){ int ext = FLAG_FIB & FLAG_EXT; int numeric = FLAG_FIB & (FLAG_NUM_HOST | FLAG_SYM); int rc = E_INTERN; printf("\n"); rc = rprint_fib6(ext, numeric); printf("\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -