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

📄 arpscan.c

📁 基于44b0的arpscan程序
💻 C
字号:
/* ARP address scan utility*/

#define VERSION "0.05"

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
//#include <conio.h>
//#include <signal.h>
#include "..\Target\44blib.h"
#include "ether.h"
#include "netutil.h"
#include "net.h"
#include "ip.h"

#define REMOTEIP    "192.168.253.3"
#define LOCALIP     "192.168.253.2"
#define IDCFG       "a"
#define NETCFG      "ether ne 0x08000000"
#define MASKCFG     "255.255.255.0"
#define GATECFG     "192.168.253.3"

#define MAXNETCFG   40              /* Max length of a net config string */

#define SCANCOUNT   5              /* Default number of addresses to scan */
#define DELTIME     100             /* Delay between transmissions (msec) */

GENFRAME genframe;                  /* Frame for network Tx/Rx */
ARPKT arpkt;
IPKT  ipkt;
ICMPKT icmpkt;
//char cfgfile[MAXPATH+5]=CFGFILE;    /* Config filename */
//char netcfg[MAXNETCFG+1]="??";      /* Network config string */
extern BYTE bcast[MACLEN];          /* Broadcast Ethernet addr */
extern BYTE myeth[MACLEN];          /* My Ethernet address */
NODE  locnode;                      /* My Ethernet and IP addresses */
NODE remnode;                       /* Remote Ethernet and IP addresses */
int scancount=SCANCOUNT;            /* Number of addresses to scan */
int breakflag;                      /* Flag to indicate ctrl-break pressed */


extern int netdebug;                /* Debug flag - net packet display */
extern int ndrivers;     

/* Function pointer: upcall from TCP/IP stack */
extern NODE *(*get_locnode_n)(int n);           /* Get local node */

/* Prototypes */
WORD read_netconfig( NODE *np);
NODE *locnode_n(int n);
void disp_usage(void);
void break_handler(int sig);

void ARPScan_test(void)
{
    int err=0;
    LWORD remip=0;
    WORD rxlen, txlen, dtype;
    GENFRAME *gfp;
    ARPKT *arp;
    char c,*p, temps[18];
	char argv[256];


    Uart_Printf("\nARPSCAN v" VERSION "");
    //signal(SIGINT, break_handler);              /* Trap ctrl-C */
    Uart_Printf("\nEnter Command line:(Example: arpscan 192.168.253.1)\n");
    p=argv;
    breakflag=0;
    remip=0;
	ndrivers=0;
    scancount=SCANCOUNT;
    Uart_GetString(p);
    p=argv;
    while (*p++!='\0')                       /* Process command-line args */
    {
        if ((c=*p)=='-')
        {
            switch (*(p+1))
            {
            
            case 'n':
			case 'N':                           /* -N: num of nodes to scan */
                scancount = atoi(p);
                break;
		    case 'v':
            case 'V':                           /* -V: verbose packet display */
                netdebug = 1;
                break;
            default:
                err = 1;
            }
        }
        else if ((c>=0x30)&&(c<=0x39)){           /* Starting IP address */
			remip = atoip(p);
			while(((*p++)!=' ')&&((*p++)!='\0'));
			p--;
        	}
    }
    if (err)                                    /* Prompt user if error */
        disp_usage();
    else if (!(dtype=read_netconfig(&locnode)))
        Uart_Printf("Invalid configuration '%s'\n");
    else
    {
        remnode.dtype = genframe.g.dtype = dtype;   /* Set frame driver type */
        gfp = &genframe;                            /* Get pointer to frame */
        Uart_Printf("Press EXINTn to exit\n");
        Uart_Printf("IP %s", ipstr(locnode.ip, temps));
        if (dtype & DTYPE_ETHER)
            Uart_Printf(" Ethernet %s (local)\n\n", ethstr(locnode.mac, temps));
        //mstimeout(&mstimer, 0);                 /* Refresh timer */
        while (!breakflag)
        {                                       /* If scanning & timeout.. */
            if (remip /*&& mstimeout(&mstimer, DELTIME)*/)
            {
                if (!scancount--)               /* ..stop looping if done */
                    break;
                remnode.ip = remip++;           /* Broadcast next IP adr */
                memcpy(remnode.mac, bcast, MACLEN);
                txlen = make_arp(gfp, &locnode, &remnode, ARPREQ);
                put_frame(gfp, txlen);
            }
            poll_net(gfp->g.dtype);             /* Keep network alive */
            if ((rxlen=get_frame(gfp)) > 0)     /* Check for incoming pkts */
            {
                if (is_arp(gfp, rxlen))
                {                               /* ARP response? */
                    //arp = getframe_datap(gfp);
                    arp = &arpkt;
                    if (arp->op==ARPRESP && arp->sip==remnode.ip)
                    {
                        Uart_Printf("\nIP %s ", ipstr(remnode.ip, temps));
                        Uart_Printf("Ethernet %s\n", ethstr(arp->smac, temps));
                    }
                    if (arp->op==ARPREQ && arp->dip==locnode.ip)
                    {                           /* ARP request? */
                        remnode.ip = arp->sip;  /* Make ARP response */
                        memcpy(remnode.mac, arp->smac, MACLEN);
                        txlen = make_arp(gfp, &locnode, &remnode, ARPRESP);
                        put_frame(gfp, txlen);
                    }
                }
            }
            if (c=Readkey())             /* if Any exintX pressed,break... */
           {
       	       breakflag = 1;                      /* Poll net drivers */
           }
        }
        close_net(dtype);                       /* Shut down net driver */
    }
}

/* Read network config file to get IP address
** Return driver type, 0 if error */
WORD read_netconfig(NODE *np)
{
    WORD dtype=0;
    BYTE b;
    
     np->ip=atoip(LOCALIP);    /* Get local node's IP*/
     dtype = open_net(NETCFG); /* read net type from tcplean.cfg */

     memcpy(np->mac, ether_addr(dtype), MACLEN);/* Get local MAC*/
     b = (BYTE)(np->ip >> 24);
     np->mask = atoip(MASKCFG);//temps);            /* Get netmask */
     
     np->gate = atoip(GATECFG);//temps);            /* Get gateway IP addr */
     
     return(dtype);
}

/* Return ptr to local node 'n' (n=0 for first), return 0 if doesn't exist
** Used by IP functions to get my netmask & gateway addresses */
NODE *locnode_n(int n)
{
    return(n==0 ? &locnode : 0);
}

/* Display usage help */
void disp_usage(void)
{
    printf("Usage:    ARPSCAN [options] [start_IP_addr]\n");
    printf("          If IP address is omitted, acts as server\n");
    //printf("Options:  -c name     Config filename (default %s)\n", cfgfile);
    printf("          -n count    Scan count (default %u)\n", SCANCOUNT);
    printf("Example:  ARPSCAN -c test.cfg 10.1.1.1\n");
}

/* Ctrl-break handler: set flag and return */
void break_handler(int sig)
{
    breakflag = sig;
}

/* EOF */

⌨️ 快捷键说明

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