📄 route.c
字号:
/***************************************************************** Copyright 1989, 1991, 1992 by Carnegie Mellon University All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of CMU not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLCMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************//* * Copyright (c) 1983,1988 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that this notice is preserved and that due credit is given * to the University of California at Berkeley. The name of the University * may not be used to endorse or promote products derived from this * software without specific prior written permission. This software * is provided ``as is'' without express or implied warranty. */#include <net-snmp/net-snmp-config.h>#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include <stdio.h>#include <ctype.h>#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#if HAVE_SYS_SELECT_H#include <sys/select.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#define LOOPBACKNET 127#if HAVE_WINSOCK_H#include <winsock.h>#include "winstub.h"#endif#if HAVE_SYS_SOCKET_H#include <sys/socket.h>#endif#if HAVE_NETDB_H#include <netdb.h>#endif#include "main.h"#include <net-snmp/net-snmp-includes.h>#include "netstat.h"struct route_entry { oid instance[4]; struct in_addr destination; int set_destination; struct in_addr mask; int set_mask; struct in_addr gateway; int set_gateway; int ifNumber; int set_ifNumber; int type; int set_type; int proto; int set_proto; char ifname[64]; int set_name;};#define RTDEST 1#define RTIFINDEX 2#define RTNEXTHOP 7#define RTTYPE 8#define RTPROTO 9#define RTMASK 11static oid oid_rttable[] = { 1, 3, 6, 1, 2, 1, 4, 21, 1 };static oid oid_rtdest[] = { 1, 3, 6, 1, 2, 1, 4, 21, 1, 1 };static oid oid_rtifindex[] = { 1, 3, 6, 1, 2, 1, 4, 21, 1, 2 };static oid oid_rtnexthop[] = { 1, 3, 6, 1, 2, 1, 4, 21, 1, 7 };static oid oid_rttype[] = { 1, 3, 6, 1, 2, 1, 4, 21, 1, 8 };static oid oid_rtproto[] = { 1, 3, 6, 1, 2, 1, 4, 21, 1, 9 };static oid oid_rtmask[] = { 1, 3, 6, 1, 2, 1, 4, 21, 1, 11 };static oid oid_ifdescr[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 2 };static oid oid_ipnoroutes[] = { 1, 3, 6, 1, 2, 1, 4, 12, 0 };/* * Print routing tables. */voidroutepr(void){ struct route_entry route, *rp = &route; netsnmp_pdu *request, *response; netsnmp_variable_list *vp; char name[16], *flags; oid *instance, type; int toloopback, status; char ch; printf("Routing tables\n"); printf("%-26.26s %-18.18s %-6.6s %s\n", "Destination", "Gateway", "Flags", "Interface"); request = snmp_pdu_create(SNMP_MSG_GETNEXT); snmp_add_null_var(request, oid_rtdest, sizeof(oid_rtdest) / sizeof(oid)); snmp_add_null_var(request, oid_rtmask, sizeof(oid_rtmask) / sizeof(oid)); snmp_add_null_var(request, oid_rtifindex, sizeof(oid_rtifindex) / sizeof(oid)); snmp_add_null_var(request, oid_rtnexthop, sizeof(oid_rtnexthop) / sizeof(oid)); snmp_add_null_var(request, oid_rttype, sizeof(oid_rttype) / sizeof(oid)); snmp_add_null_var(request, oid_rtproto, sizeof(oid_rtproto) / sizeof(oid)); while (request) { status = snmp_synch_response(Session, request, &response); if (status != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR) { fprintf(stderr, "SNMP request failed\n"); break; } instance = NULL; request = NULL; rp->set_destination = 0; rp->set_mask = 0; rp->set_ifNumber = 0; rp->set_gateway = 0; rp->set_type = 0; rp->set_proto = 0; for (vp = response->variables; vp; vp = vp->next_variable) { if (vp->name_length != 14 || memcmp(vp->name, oid_rttable, sizeof(oid_rttable))) { continue; /* if it isn't in this subtree, just continue */ } if (instance != NULL) { oid *ip, *op; int count; ip = instance; op = vp->name + 10; for (count = 0; count < 4; count++) { if (*ip++ != *op++) break; } if (count < 4) continue; /* not the right instance, ignore */ } else { instance = vp->name + 10; } /* * At this point, this variable is known to be in the routing table * subtree, and is of the right instance for this transaction. */ if (request == NULL) request = snmp_pdu_create(SNMP_MSG_GETNEXT); snmp_add_null_var(request, vp->name, vp->name_length); type = vp->name[9]; switch ((char) type) { case RTDEST: memmove(&rp->destination, vp->val.string, sizeof(u_long)); rp->set_destination = 1; break; case RTMASK: memmove(&rp->mask, vp->val.string, sizeof(u_long)); rp->set_mask = 1; break; case RTIFINDEX: rp->ifNumber = *vp->val.integer; rp->set_ifNumber = 1; break; case RTNEXTHOP: memmove(&rp->gateway, vp->val.string, sizeof(u_long)); rp->set_gateway = 1; break; case RTTYPE: rp->type = *vp->val.integer; rp->set_type = 1; break; case RTPROTO: rp->proto = *vp->val.integer; rp->set_proto = 1; break; } } snmp_free_pdu(response); if (!(rp->set_destination && rp->set_gateway && rp->set_type && rp->set_ifNumber)) { if (request) snmp_free_pdu(request); request = NULL; continue; } toloopback = *(char *) &rp->gateway == LOOPBACKNET; printf("%-26.26s ", (rp->destination.s_addr == INADDR_ANY) ? "default" : (toloopback) ? routename(rp->destination) : rp->set_mask ? netname(rp->destination, rp->mask.s_addr) : netname(rp->destination, 0L)); printf("%-18.18s ", routename(rp->gateway)); flags = name; *flags++ = 'U'; /* route is in use */ /* * this !toloopback shouldnt be necessary */ if (!toloopback && rp->type == MIB_IPROUTETYPE_REMOTE) *flags++ = 'G'; if (toloopback) *flags++ = 'H'; if (rp->proto == MIB_IPROUTEPROTO_ICMP) *flags++ = 'D'; /* redirect */ *flags = '\0'; printf("%-6.6s ", name); get_ifname(rp->ifname, rp->ifNumber); ch = rp->ifname[strlen(rp->ifname) - 1]; ch = '5'; /* force the if statement */ if (isdigit(ch)) printf(" %.32s\n", rp->ifname); else printf(" %.32s%d\n", rp->ifname, rp->ifNumber); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -