rttest.c

来自「在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LE」· C语言 代码 · 共 732 行 · 第 1/2 页

C
732
字号
/*
 * FILENAME: rttest.c
 *
 * Copyright  2001 By InterNiche Technologies Inc. All rights reserved
 *
 * Pseudo driver for speed testing routing & routing and 
 * NAT. This works by by installing itself as an InterNiche MAC 
 * driver which allows the user to generate IP or NAT routing 
 * traffic. 
 *
 * MODULE: MISCLIB
 *
 * ROUTINES: rt_help(), rtcreate_device(), rt_init(), 
 * ROUTINES: rt_pkt_send(), rt_close(), rt_reg_type(), rt_stats(), rt_send(), 
 * ROUTINES: nt_send(), rt_setprot(), rt_statistics(), rt_startup(), rt_done(),
 *
 * PORTABLE: yes
 */


#include "ipport.h"

#ifdef ROUTE_TEST    /* Whole file can be ifdeffed */

#ifndef IP_ROUTING
#error Must define IP_ROUTING to use ROUTE_TEST
#endif   /* IP_ROUTING */

#ifndef DYNAMIC_IFACES
#error Must define DYNAMIC_IFACES to use ROUTE_TEST
#endif   /* DYNAMIC_IFACES */

#ifdef NATRT
#include "../natrt/natport.h"
#endif /* NATRT */

#include "q.h"
#include "netbuf.h"
#include "net.h"
#include "ether.h"
#include "arp.h"
#include "ip.h"
#include "udp.h"
#include "tcp.h"

#include "nvparms.h"
#include "menu.h"

NET      nt_ifp;     /* this driver (the creatyed test driver) */
int      lb_index;   /* index of loopback iface */
int      rt_index;   /* index of this test iface */

int   rtdrv_prep(int ifaces);

int   rt_init(int iface);           /* net initialization routine */
int   rt_pkt_send(struct netbuf *); /* send packet on media */
int   rt_close(int  iface);         /* net close routine */
int   rt_reg_type(unshort type, struct net*);   /* register a MAC type */
void  rt_stats(void * pio, int iface);   

struct nt_stats   {
   long  udp_pkts_sent;
   long  udp_bytes_sent;
   long  tcp_pkts_sent;
   long  tcp_bytes_sent;
   long  udp_pkts_recv;
   long  udp_bytes_recv;
   long  tcp_pkts_recv;
   long  tcp_bytes_recv;
   long  other_pkts;
};

struct nt_stats   ntstat;

int rt_send(void * pio);
int rt_startup(void * pio);
int rt_done(void * pio);
int nt_send(void * pio);
int rt_setprot(void * pio);
int rt_statistics(void * pio);
int rt_help(void * pio);

struct menu_op rt_menu[]   =
{
   "rtest",    stooges,          "Route test",      /* menu ID */
   "rstart",   rt_startup,       "Create route test device",
   "rdone",    rt_done,          "Delete route test device",
   "rsend",    rt_send,          "send packets through route code",
#ifdef NATRT
   "nsend",    nt_send,          "send packets through Nat",
#endif
   "rprot",    rt_setprot,       "set route test protocol (tcp or udp)",
   "rstat",    rt_statistics,    "show/clear route route test statistics",
   "rthelp",   rt_help,          "help with route TEST",
   NULL, 0, NULL,
};



/* FUNCTION: rt_help()
 * 
 * menu routine
 * 
 * PARAM1: void * pio
 *
 * RETURNS: 
 */

int
rt_help(void * pio)
{
   ns_printf(pio, "Use \"rstart\" to create a test iface first, then:\n");
   ns_printf(pio, "\"rsend\" to start a packet burst through router\n");
   ns_printf(pio, "\"nsend\" to start a packet burst through NAT\n");
   ns_printf(pio, "\"rstat\" to display the resulting statstics\n");
   ns_printf(pio, "\"rstat 0\" to reset statistics\n");
   ns_printf(pio, "\"rprot\" to set protocol as 6(TCP) or 17(UDP)\n");
   ns_printf(pio, "\"length\" to set length of test packets\n");
   ns_printf(pio, "\"rdone\" to delete the test iface\n");
   return 0;
}

char *   nat_mac  = "RTRTST";    /* fake MAC address */

u_char   ntprot   =  6;       /* protocol of test pkts, 6==tcp, 17==udp */
u_short  nt_sport =  1200; /* source and dest ports */
u_short  nt_dport =  80;
ip_addr   nt_srcaddr;
ip_addr   nt_destaddr;

extern   int      deflength;     /* use ping packet length */
extern   void     ip_bldhead(PACKET p, 
   unsigned pid, u_char prot, unshort fragword);
extern   void     nat_re_init(void);

static  int    save_nat_inet;
static  int    save_nat_localnet;

/* test driver routines */

/* FUNCTION: rtcreate_device()
 * 
 * the "ni_create()" entry to create a dynamic rttest device.
 * 
 * PARAM1: NET ifp
 * PARAM2: void * bindinfo
 *
 * RETURNS: 0 if OK or an ENP error code
 */

int
rtcreate_device(NET ifp, void * bindinfo)
{
   USE_VOID(bindinfo);

   /* set up  an InterNiche net structure for a fake ethernet interface */
   ifp->n_lnh = ETHHDR_SIZE;        /* space reserved for ethernet header */
   ifp->n_hal = 6;                  /* hardware address length */
   ifp->n_mtu = 1514;               /* max frame size */
   ifp->n_mib->ifAdminStatus = 2;   /* status = down */
   ifp->n_mib->ifOperStatus = 2;    /* will be set up in init()  */
   ifp->n_mib->ifLastChange = cticks * (100/TPS);
   ifp->n_mib->ifType = ETHERNET;
   ifp->n_mib->ifDescr = (u_char*)"Routing/NAT test driver";
   ifp->n_mib->ifPhysAddress = (u_char*)nat_mac;

   /* install our virtual driver routines */
   ifp->n_init = rt_init;
   ifp->pkt_send = rt_pkt_send;
   ifp->n_close = rt_close;
   ifp->n_stats = rt_stats;
   ifp->n_reg_type = rt_reg_type;

   /* set a custom name for ndis device */
   ifp->name[0] = 'r';
   ifp->name[1] = 't';
   ifp->name[2] = 's';

#ifdef INCLUDE_NVPARMS   /* only if using NV parms */
   /* search the NV parameters for iface setup for our name. If this
    * fails we just default to what's already in the ifp.
    */
   if_configbyname(ifp);
#endif   /* INCLUDE_NVPARMS */

   nt_ifp = ifp;     /* save our iface for later use */

   rt_index = if_netnumber(nt_ifp);    /* get numeric index */

   return(rt_init(rt_index));
}


/* interface initialization routine */



/* FUNCTION: rt_init()
 * 
 * PARAM1: int iface
 *
 * RETURNS:  0 if OK or an ENP error code
 */

int
rt_init(int iface)
{
   if (nets[iface] != nt_ifp)
   {
      dtrap("rttest 0\n"); /* serious code error */
      return ENP_LOGIC;
   }
   return 0;
}





/* FUNCTION: rt_pkt_send()
 * 
 * MAC interface send packet call for test device. In this case
 * it is called whenever a packet sent fromt his device has been
 * routed through the loopback driver and back to this interface.
 * All we need to do is count them and free the buffer.
 *
 * PARAM1: struct netbuf * pkt
 *
 * RETURNS:  0 if OK or an ENP error code
 */

int
rt_pkt_send(struct netbuf * pkt)
{
   struct ip * pip;
   struct IfMib * mib;

   pip = (struct ip *)(pkt->nb_prot + ETHHDR_SIZE);

   if (pip->ip_prot == 6)
   {
      ntstat.tcp_pkts_recv++;
      ntstat.tcp_bytes_recv += pkt->nb_plen;
   }
   else if(pip->ip_prot == 17)
   {
      ntstat.udp_pkts_recv++;
      ntstat.udp_bytes_recv += pkt->nb_plen;
   }
   else
   {
      ntstat.other_pkts++;
   }

   mib = nt_ifp->n_mib;
   mib->ifOutOctets += pkt->nb_plen;
   mib->ifOutUcastPkts++;

   pk_free(pkt);
   return 0;
}



/* FUNCTION: rt_close()
 * 
 * PARAM1: int  iface
 *
 * RETURNS:   0 if OK or an ENP error code
 */

int
rt_close(int  iface)
{
   USE_ARG(iface);
   return 0;
}


/* FUNCTION: rt_reg_type()
 * 
 * register a MAC type - no-op on this device.
 *
 * PARAM1: unshort type
 * PARAM2: struct net * ifp
 *
 * RETURNS:   0 if OK or an ENP error code
 */

int
rt_reg_type(unshort type, struct net * ifp)
{
   USE_ARG(type);
   USE_ARG(ifp);
   return 0;
}




/* FUNCTION: rt_stats()
 * 
 * PARAM1: void * pio
 * PARAM2: int iface
 *
 * RETURNS: void
 */

void
rt_stats(void * pio, int iface)
{
   rt_statistics(pio);
   USE_ARG(iface);
}


/* NAT test menu routines */

#define  TESTIP      1
#define  TESTNAT     2

static int rttest = TESTNAT;



/* FUNCTION: rt_send()
 *
 * Menu routine to generate the stream of test packets into the IP code. This is
 * just a wrapper which sets a flag and call nt_send().
 *
 * PARAM1: void * pio
 *
 * RETURNS: 0 if OK or an ENP error code
 */

int
rt_send(void * pio)
{
   int   err;

#ifdef NATRT
   /* Since test defaults to NAT, we have to unNAT it here. */
   /* stuff the indexes into nvparms structure */
   natrt_nvparms.nat_inet = save_nat_inet;
   natrt_nvparms.nat_localnet = save_nat_localnet;
   nat_re_init();       /* restart NAT with original ifaces */
#endif   /* NATRT */

   rttest = TESTIP;     /* set for IP */
   err = nt_send(pio);
   rttest = TESTNAT;

#ifdef NATRT
   /* stuff the indexes into nvparms structure */
   natrt_nvparms.nat_inet = lb_index;
   natrt_nvparms.nat_localnet = rt_index;
   nat_re_init();       /* restart NAT with original ifaces */
#endif   /* NATRT */

   return err;
}




/* FUNCTION: nt_send()

⌨️ 快捷键说明

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