⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drt.c

📁 wm PNE 3.3 source code, running at more than vxworks6.x version.
💻 C
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/snmptalk/drt.c,v 1.3 2002/03/11 19:17:08 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved.  Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** *  Copyright 1991-1997 Epilogue Technology Corporation. *  Copyright 1998 Integrated Systems, Inc. *  All rights reserved. ****************************************************************************//* dump the IP routing table *//* * $Log: drt.c,v $ * Revision 1.3  2002/03/11 19:17:08  josh * snmptalk's destination address is now stored and passed around * as an ipaddr_t.  This required changes to a lot of internal function * calls that used to expect an inaddr_t to be passed around.  Also, * snmptalk is now capable of using IPv6 sockets * * Revision 1.2  2001/11/08 16:47:23  tneale * Updated for newset file layout * * Revision 1.1.1.1  2001/11/05 17:49:11  tneale * Tornado shuffle * * Revision 7.10  2001/01/19 22:24:43  paul * Update copyright. * * Revision 7.9  2000/03/17 00:14:32  meister * Update copyright message * * Revision 7.8  1998/06/23 20:56:43  sar * Moved parse.h to snark/h/parse.h * * Revision 7.7  1998/02/25 04:57:54  sra * Update copyrights. * * Revision 7.6  1997/03/20 06:53:44  sra * DFARS-safe copyright text.  Zap! * * Revision 7.5  1997/02/25 10:58:16  sra * Update copyright notice, dust under the bed. * * Revision 7.4  1997/02/19 08:10:29  sra * More fun merging snmptalk into snark, general snark cleanup. * * Revision 7.3  1997/01/09 22:44:19  sar * Modified the command calling sequence again, this time to mathc * what snark was using. * * Revision 7.2  1997/01/08  23:26:53  sar * Updated include files to use envoy/h as appropriate after * move from envoy/utils to snark/snmptalk * * Revision 7.1  1997/01/08  01:55:00  sar * Modified to use the snark sty driver for in/output * * Revision 7.0  1996/03/18  20:19:20  sar * Updated rev to 7.0 and copyright to 96 * * Revision 6.1  1995/10/20  22:39:59  sar * Modified call to snmptalk_create_request * * Revision 6.0  1995/05/31  21:50:00  sra * Release 6.0. * * Revision 5.1  1994/12/20  22:46:38  dab * Print just the number of bits in the netmask rather than printing the * mask like an IP address. * * Revision 5.0  1994/05/16  15:57:40  sar * Updated revision to 5.0 and copyright to include 1994 * * Revision 4.0  1993/06/24  17:14:56  sar * Updated rev to 4.0 and copyright to 93 * * Revision 3.6  1993/05/19  20:38:22  sar * Added procedure prototypes to some of the routines. (#if !defined(NO_PP)...) * * Revision 3.5  1993/02/22  21:04:10  dab * Changed to use SNMPTalk_Create_Request so does either v1 or v2. * * Revision 3.4  1993/02/05  18:47:56  dab * Changed "system.h" to <snmptalk.h> * * Revision 3.3  1992/07/31  12:57:40  dab * Non-error output should go to outfile not stdout. * * Revision 3.2  92/06/17  12:55:00  dab * Added the ability to look at a single IP route or a subset of the routes. *  *//* [clearcase]modification history-------------------01a,19apr05,job  update copyright notices*/#include <snmptalk.h>#include <stdio.h>#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/localio.h>#include <wrn/wm/snmp/engine/buffer.h>#include <wrn/wm/snmp/engine/objectid.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/auxfuncs.h>#include <wrn/wm/snmp/engine/buildpkt.h>#include "md.h"#include "snmpint.h"#include "mibutils.h"#include <wrn/wm/demo/snarklib.h>#include <wrn/wm/demo/snmpconf.h>enum help_level { help_none, help_short, help_long };#define OIDSZ(oid) (sizeof(oid)/sizeof(OIDC_T) - 4)#define INSTANCE(oid) (oid + OIDSZ(oid))/* Make the typing a bit easier */#define MIB2 1, 3, 6, 1, 2, 1#define ip MIB2, 4#define rtTable ip, 21#define rtEntry rtTable, 1/* instance space may or may not be used */OIDC_T rtDest[] =               { rtEntry, 1, 0, 0, 0, 0 };OIDC_T rtIfIndex[] =            { rtEntry, 2, 0, 0, 0, 0 };OIDC_T rtMetric1[] =            { rtEntry, 3, 0, 0, 0, 0 };OIDC_T rtMetric2[] =            { rtEntry, 4, 0, 0, 0, 0 };OIDC_T rtMetric3[] =            { rtEntry, 5, 0, 0, 0, 0 };OIDC_T rtMetric4[] =            { rtEntry, 6, 0, 0, 0, 0 };OIDC_T rtNextHop[] =            { rtEntry, 7, 0, 0, 0, 0 };OIDC_T rtType[] =               { rtEntry, 8, 0, 0, 0, 0 };OIDC_T rtProto[] =              { rtEntry, 9, 0, 0, 0, 0 };OIDC_T rtMask[] =               { rtEntry, 11, 0, 0, 0, 0 };OIDC_T rtMetric5[] =            { rtEntry, 12, 0, 0, 0, 0 };static int exact;               /* doing a get instead of a get-next */static int initial_oid_length;  /* oid length for doing end-of-table check */void sprint_ip_and_mask __((char *buffer,                            inaddr_t addr,                            inaddr_t mask));extern int req_id;static void  drt_error(struct sty  *sty,	    char *err_msg){  sty_printf(sty, "%s\n", err_msg);  command_next(sty);}/* Handle responses while dumping the routing table.  If we're still in the * routing table then print what we learned and go on to the next. */static void  drt_response(struct sty *sty,	       SNMP_PKT_T *rpkt){  SNMP_PKT_T *tpkt;  VB_T *vbl;  static char bad_type[] = "<type>";  char pbuf[40];  OIDC_T *oid;  int i;  char *err_msg;  int indx;    /* If we get an error back from the agent. */  if (rpkt->pdu.std_pdu.error_status != NO_ERROR) {    print_snmp_error(sty, rpkt);    command_next(sty);  }  else {    vbl = rpkt->pdu.std_pdu.std_vbl.vblist;    if (vbl == 0) {      sty_puts(sty, "varbind list is empty\n");      command_next(sty);    }    else {      /* Check that we're still in the routing table */      oid = vbl->vb_obj_id.component_list;      if ((vbl->vb_obj_id.num_components < initial_oid_length) ||	  (oidorder(rtDest, oid, initial_oid_length) != 0)) {	command_next(sty);      }      else {	/* Print out the contents of the packet we've just received while	 * simultaneously building the next packet */		indx = 0;		/* Packet for next row */	tpkt = SNMPTalk_Create_Request(GET_NEXT_REQUEST_PDU, 10, 0, 0);		/* rtDest and rtMask */	if (vbl->vb_data_flags_n_type != VT_IPADDRESS)	  sprintf(pbuf, "dest %-14s ", bad_type);	else if ((vbl+1)->vb_data_flags_n_type != VT_IPADDRESS)	  sprintf(pbuf, "mask %-14s ", bad_type);	else {	  sprint_ip_and_mask(pbuf,			     *(inaddr_t *)(vbl->value_u.v_network_address),			     *(inaddr_t *)((vbl+1)->value_u.v_network_address));	  sty_printf(sty, "%-19s ", pbuf);	}	SNMP_Bind_Null(tpkt, indx++, vbl->vb_obj_id.num_components,		       vbl->vb_obj_id.component_list);	vbl++;		SNMP_Bind_Null(tpkt, indx++, vbl->vb_obj_id.num_components,		       vbl->vb_obj_id.component_list);	vbl++;		/* rtIfIndex */	if (vbl->vb_data_flags_n_type != VT_NUMBER)	  sty_printf(sty, "%s/", bad_type);	else	  sty_printf(sty, "%lu/", vbl->value_u.v_number);	SNMP_Bind_Null(tpkt, indx++, vbl->vb_obj_id.num_components,		       vbl->vb_obj_id.component_list);	vbl++;		/* rtNextHop */	if (vbl->vb_data_flags_n_type != VT_IPADDRESS)	  sty_printf(sty, "%-15s   ", bad_type);	else {	  sprintf(pbuf, "%u.%u.%u.%u",		  vbl->value_u.v_network_address[0],		  vbl->value_u.v_network_address[1],		  vbl->value_u.v_network_address[2],		  vbl->value_u.v_network_address[3]);	  sty_printf(sty, "%-15s   ", pbuf);	}	SNMP_Bind_Null(tpkt, indx++, vbl->vb_obj_id.num_components,		       vbl->vb_obj_id.component_list);	vbl++;		/* rtType */	if (vbl->vb_data_flags_n_type != VT_NUMBER)	  sty_printf(sty, "%-10s   ", bad_type);	else {	  int2string(rtType, OIDSZ(rtType), vbl->value_u.v_number,		     pbuf, sizeof(pbuf), 1);	  sty_printf(sty, "%-10s   ", pbuf);	}	SNMP_Bind_Null(tpkt, indx++, vbl->vb_obj_id.num_components,		       vbl->vb_obj_id.component_list);	vbl++;		/* rtProto */	if (vbl->vb_data_flags_n_type != VT_NUMBER)	  sty_printf(sty, "%-15s\n", bad_type);	else {	  int2string(rtProto, OIDSZ(rtProto), vbl->value_u.v_number,		     pbuf, sizeof(pbuf), 1);	  sty_printf(sty, "%-15s\n", pbuf);	}	SNMP_Bind_Null(tpkt, indx++, vbl->vb_obj_id.num_components,		       vbl->vb_obj_id.component_list);	vbl++;		/* rtMetrics */	sty_printf(sty, "%29s ", "Metrics:");	pbuf[0] = '\0';	for (i = 0; i < 4; i++) {	  if (vbl->vb_data_flags_n_type != VT_NUMBER)	    sprintf(pbuf+strlen(pbuf), "--     ");	  else	    sprintf(pbuf+strlen(pbuf), "%6ld ", vbl->value_u.v_number);	  SNMP_Bind_Null(tpkt, indx++, vbl->vb_obj_id.num_components,			 vbl->vb_obj_id.component_list);	  vbl++;	}	sty_printf(sty, "%s\n", pbuf);		/* Get next row */	if (!exact) {	  err_msg = snmp_send_rec(sty, tpkt, &dest_addr, drt_response,				  drt_error);	  if (err_msg) {	    sty_printf(sty, "%s\n", err_msg);	    command_next(sty);	  }	}	else	  command_next(sty);      }    }  }    SNMP_Free(rpkt);}boolean_t  do_drt(struct sty        *sty,	 enum   help_level  help,	        int         argc,	        char       *argv[]){SNMP_PKT_T *tpkt;char *err_msg;switch (help) { case help_short:  sty_puts(sty, "drt - dump IP routing table\n");  break; case help_long:  sty_puts(sty, "\The 'drt' command gets data from the destination's IP routing\n\table and prints it.  May be given an IP address or partial IP\n\address and will get the routing information for that address or\n\set of addresses.\n");  break; case help_none:  if (!destination) {    sty_puts(sty, "Need to specify destination first\n");    return 1;    }  if (argc > 2) {    sty_puts(sty, "at most one argument to 'drt'\n");    return 1;    }  else if (argc == 1) {       /* no arguments */    exact = 0;    initial_oid_length = OIDSZ(rtDest);    }  else {                      /* argc == 2 */    int count;    char *cp;    /* Parse out IP address or partial IP address */    cp = argv[1];    for (count = 0; count < 4; count++) {      if ((*cp < '0') || (*cp > '9')) {	sty_puts(sty, "Bad format\n");	return 1;        }      if ((rtDest[OIDSZ(rtDest) + count] = atoi(cp)) > 255) {	sty_puts(sty, "IP address component greater than 255\n");	return 1;        }      while ((*cp >= '0') && (*cp <= '9'))	cp++;      if (*cp == '\0')	break;      if (*cp != '.') {	sty_puts(sty, "Bad format\n");	return 1;        }      else	cp++;      }    /* Copy instance to all variables */    initial_oid_length = OIDSZ(rtDest) + count + 1;    if (count == 3)      exact = 1;    else      exact = 0;    do      rtIfIndex[OIDSZ(rtIfIndex) + count] =	rtMetric1[OIDSZ(rtMetric1) + count] =	  rtMetric2[OIDSZ(rtMetric2) + count] =	    rtMetric3[OIDSZ(rtMetric3) + count] =	      rtMetric4[OIDSZ(rtMetric4) + count] =		rtNextHop[OIDSZ(rtNextHop) + count] =		  rtType[OIDSZ(rtType) + count] =		    rtProto[OIDSZ(rtProto) + count] =		      rtMask[OIDSZ(rtMask) + count] =			rtMetric5[OIDSZ(rtMetric5) + count] =			  rtDest[OIDSZ(rtDest) + count];    while (count--);    }  /* Create the first request. */  tpkt = SNMPTalk_Create_Request(exact ? GET_REQUEST_PDU : GET_NEXT_REQUEST_PDU,			       10, 0, 0);  SNMP_Bind_Null(tpkt, /* index */ 0, initial_oid_length, rtDest);  SNMP_Bind_Null(tpkt, /* index */ 1, initial_oid_length, rtMask);  SNMP_Bind_Null(tpkt, /* index */ 2, initial_oid_length, rtIfIndex);  SNMP_Bind_Null(tpkt, /* index */ 3, initial_oid_length, rtNextHop);  SNMP_Bind_Null(tpkt, /* index */ 4, initial_oid_length, rtType);  SNMP_Bind_Null(tpkt, /* index */ 5, initial_oid_length, rtProto);  SNMP_Bind_Null(tpkt, /* index */ 6, initial_oid_length, rtMetric1);  SNMP_Bind_Null(tpkt, /* index */ 7, initial_oid_length, rtMetric2);  SNMP_Bind_Null(tpkt, /* index */ 8, initial_oid_length, rtMetric3);  SNMP_Bind_Null(tpkt, /* index */ 9, initial_oid_length, rtMetric4);  err_msg = snmp_send_rec(sty, tpkt, &dest_addr, drt_response, drt_error);  if (err_msg) {    sty_printf(sty, "%s\n", err_msg);    return(1);    }  return(0);  }return(1);}/* * These next few routines implement a new way of printing IP * addresses and their associated masks.  The new way should be * shorter and clearer. *//* Return the number of bits on at the front of the mask (in network * byte order) */int  ip_mask_to_count(inaddr_t mask){union { inaddr_t addr;	unsigned char array[4];      } nmask;int i;int count;unsigned char cmask;nmask.addr = mask;count = 0;for (i = 0; i < 4; i++) {    cmask = 0x80;    while (cmask) {        if ((cmask & nmask.array[i]) == 0)	    return count;	cmask >>= 1;	count++;        }    }return count;}/* Prints the interesting part of the IP address and netmask length * into the buffer provided.  The maximum length of the printed string * is 18 byes plus 1 byte for the null termination. */void  sprint_ip_and_mask(char *buffer,		     inaddr_t addr,		     inaddr_t mask){union { inaddr_t addr;	unsigned char array[4];      } naddr;int mask_len;mask_len = ip_mask_to_count(mask);naddr.addr = addr & mask;sprintf(buffer, "%u", naddr.array[0]);if (mask_len > 8)    sprintf(buffer+strlen(buffer), ".%u", naddr.array[1]);if (mask_len > 16)    sprintf(buffer+strlen(buffer), ".%u", naddr.array[2]);if (mask_len > 24)    sprintf(buffer+strlen(buffer), ".%u", naddr.array[3]);sprintf(buffer+strlen(buffer), "/%u", mask_len);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -