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

📄 c_stpvr2_ip_tests_ngsetup.c

📁 Lan9118以太网芯片linux驱动程序
💻 C
字号:
#include <ngos.h>
#include <ngip.h>
#include <ngip/ethernet.h>
#include <ngudp.h>
#include <ngeth.h>
#include <ngsocket.h>
#include <ngtcp.h>
#include "stddefs.h"
#include "myAlloc.h"
#include "ostime.h" 
#include "develop.h"
#include "mainconf.h"
#include "HARD.h"
#include "ngping.h"
#include <ngdhcpc.h>
#include <ngboot/net_dc.h>
#include <ngresolv.h>

#define NET_NETMASK   
#define MAXBUF 64
#define INMULTI_MAX   16
#define STACK_SIZE    8192
#define SOCK_MAX 32
#define DEV_PRIO         MAX_USER_PRIORITY-5
#define TIMER_PRIO       MAX_USER_PRIORITY-7
#define INPUT_PRIO       MAX_USER_PRIORITY-6
#if defined(STI5514)
#define NET_ETHADDR "\xc0\xff\xee\x78\x05\x05"
#else
#define NET_ETHADDR "\xc0\xff\xee\x78\x05\x07"
#endif 

/* NGREsolver VAR */
/* Maximum number of simultaneous application pending queries */ 
#define NG_RSLV_QUER_MAX 8  
/* Size of cache (must be greater than the maximum number of pending queries) */ 
#define NG_RSLV_CACHE_MAX NG_RSLV_QUER_MAX+8   /* NexGenRESOLV variables */
static NGslvpquer rsv_quer[ NG_RSLV_QUER_MAX]; /* Pending query table */ 
static NGslvcaent rsv_ca[ NG_RSLV_CACHE_MAX];  /* Cache entry table */
#ifdef NG_RTOS 
static NGOSsem rsv_sem[ NG_RSLV_QUER_MAX];    /* Semaphore table */
#endif

/* call back function */
void dhcpCallbackProc(int event, NGubyte incoming_mask, NGdhcpclient*dcp);

/* DHCP id client */
U8 dhcpc_cid[7] = { 0xc0, 0xff, 0xee, 0x78, 0x05, 0x07, 0x99 };

/* Some buffers to store retrieved option from BOOTP server */
  static NGuint   subnet;
  static NGuint   gateway;
  static NGuint   dns;                         /* we want dns address */  

/* A static array to store requested options (last arg of ngBootpQuery) */
NGdhcpoption requestedOptions[]={
    TAG_SUBNET_MASK,     	4, (NGubyte*)&subnet,
    TAG_GATEWAY,         	4, (NGubyte*)&gateway,       
    TAG_DOMAIN_SERVER, 		4, (NGubyte*)&dns,      /* we want 3 dns address */
    TAG_END
  };

/* tcp control blocks */
NGtcpcb     tcpcb[SOCK_MAX];   

/* dhcp protocol */
NGdhcpclient dhcpc0;

/* multicast */
static NGinmulti   inmtable[INMULTI_MAX];

/* Ethernet interface */
static NGethifnet  ifnet0;
static NGifnet     ifnet_loop;
extern const NGnetdrv ngNetDrv_smsclan9118;  
/*extern const NGnetdrv ngNetDrv_CS8900A;*/
NGarpent arptable[8];


/* sockets */
NGsock socks[SOCK_MAX]; 

/* stuff for rtos mode */
NGOSsem     socks_sem[SOCK_MAX];
NGubyte     input_stk[STACK_SIZE];
NGubyte     timer_stk[STACK_SIZE];


/* stack configuration table */
static NGcfgent nginit_conf[] = {
/* General */
	NG_BUFO_MAX, 					NG_CFG_INT( MAXBUF),   /* 32*/ 
	NG_BUFO_ALLOC_F, 				NG_CFG_PTR( my_buf_alloc),  /* in myalloc.h*/
	NG_BUFO_HEADER_SIZE, 		NG_CFG_INT( sizeof( NGetherhdr)),
	NG_BUFO_DATA_SIZE, 			NG_CFG_INT( ETHERMTU),
	NG_BUFO_INPQ_MAX,       	NG_CFG_INT( 16),
	
	NG_SOCKO_MAX, 					NG_CFG_INT( sizeof( socks)/sizeof( socks[0])),
	NG_SOCKO_TABLE, 				NG_CFG_PTR( &socks),
	NG_SOCKO_SEMTABLE,      	NG_CFG_PTR( socks_sem),
		
  	NG_RTO_INPUT_PRIO,        	NG_CFG_INT( 6),
  	NG_RTO_INPUT_STACK_SIZE,  	NG_CFG_INT( STACK_SIZE),
  	NG_RTO_INPUT_STACK,       	NG_CFG_PTR( input_stk),
  	NG_RTO_TIMER_PRIO,        	NG_CFG_INT( 5),
  	NG_RTO_TIMER_STACK_SIZE,  	NG_CFG_INT( STACK_SIZE),
  	NG_RTO_TIMER_STACK,       	NG_CFG_PTR( timer_stk),	 	
	
	/* TCP */
	NG_CFG_PROTOADD,          	NG_CFG_PTR( &ngProto_TCP),
	NG_TCPO_TCB_MAX,          	NG_CFG_INT( sizeof( tcpcb)/sizeof( tcpcb[0])),
	NG_TCPO_TCB_TABLE,        	NG_CFG_PTR( &tcpcb),

	/* UDP */
	NG_CFG_PROTOADD, 		NG_CFG_PTR( &ngProto_UDP),
	
	/* RAWIP */
  	NG_CFG_PROTOADD,        	NG_CFG_PTR( &ngProto_RAWIP),
	
	/* IP */
	NG_CFG_PROTOADD, 		      NG_CFG_PTR( &ngProto_IP),
	NG_IPO_INMULTI_MAX,     	NG_CFG_INT( sizeof( inmtable)/sizeof( inmtable[0])),
  	NG_IPO_INMULTI_TABLE,   	NG_CFG_PTR( &inmtable),
	NG_IPO_ROUTE_DEFAULT,		NG_CFG_ADR( 192, 168,1,120),

	/* ARP */
	NG_CFG_PROTOADD, 		NG_CFG_PTR( &ngProto_ARP),
	NG_ARPO_MAX, 			NG_CFG_INT( sizeof( arptable)/sizeof( arptable[0])),
	NG_ARPO_TABLE, 			NG_CFG_PTR( &arptable),
	
	/* Loopback Interface */
	NG_CFG_IFADD,          	 	NG_CFG_PTR( &ifnet_loop),
	NG_CFG_DRIVER,          	NG_CFG_PTR( &ngNetDrv_LOOPBACK),
	NG_IFO_NAME,            	NG_CFG_PTR( "lo0"),

 	/* Ethernet Interface */
 	NG_CFG_IFADD,     		NG_CFG_PTR( &ifnet0),
   	NG_CFG_DRIVER, 			NG_CFG_PTR( &ngNetDrv_smsclan9118),
	                  	
	NG_IFO_NAME, 			NG_CFG_PTR( "le0"),
/*#if defined(STI5514) 	
	NG_IFO_ADDR, 			NG_CFG_ADR( 164, 130,60,146),
#else                	  
	NG_IFO_ADDR, 			NG_CFG_ADR( 164, 130,60,146),
#endif*/
	NG_IFO_OUTQ_MAX,     		NG_CFG_INT( 16),
/*#ifdef NET_NETMASK
  	NG_IFO_NETMASK,      		NG_CFG_ADR( 255,255,255,192),
#endif*/
#ifdef NET_ALLMULTI
  	NG_IFO_ALLMULTI,     		NG_CFG_TRUE,
#endif  
	    
	NG_ETHIFO_IRQ, 			NG_CFG_INT( 48),
	NG_ETHIFO_IOBASE, 		NG_CFG_INT( 0x50010300),
	NG_ETHIFO_FULLDUPLEX,   	NG_CFG_TRUE,
#ifdef NET_ETHADDR
	NG_ETHIFO_ADDR,         	NG_CFG_PTR( NET_ETHADDR),
#endif	
	/* adding DHCP support */           
	NG_CFG_PROTOADD, 					NG_CFG_PTR( &ngProto_DHCPC),          
	NG_DHCPCO_CLIENTADD, 	 		NG_CFG_PTR( &dhcpc0),
	NG_DHCPCO_SET, 	 				NG_CFG_PTR( &dhcpc0),
	NG_DHCPCO_CLIENTID,     		NG_CFG_PTR( dhcpc_cid),         
  	NG_DHCPCO_CLIENTID_LEN, 		NG_CFG_INT( sizeof(dhcpc_cid)),           	 	 
	NG_DHCPCO_IF, 		 				NG_CFG_PTR( &ifnet0),
	NG_DHCPCO_MODE, 					NG_CFG_INT( DHCPC_MATCHFIRST), 
	NG_DHCPCO_RANDOMSEED,   		NG_CFG_INT( 600),	 
	NG_DHCPCO_OPTLIST,           	NG_CFG_PTR( requestedOptions),
	NG_DHCPCO_CALLBACK, 				NG_CFG_PTR( dhcpCallbackProc),	

	/* adding NGResoler protocole */
	NG_CFG_PROTOADD, NG_CFG_PTR( &ngProto_RESOLV), 
	NG_RSLVO_SERV1_IPADDR, NG_CFG_ADR( 164, 129, 62, 2), 	
	NG_RSLVO_QUERY_MAX, NG_CFG_INT( sizeof(rsv_quer)/sizeof(rsv_quer[0])), 
	NG_RSLVO_QUERY, NG_CFG_PTR( rsv_quer), 
	#ifdef NG_RTOS
	NG_RSLVO_SEM, NG_CFG_PTR( rsv_sem), 
	#endif 
	NG_RSLVO_CACHE_MAX, 	NG_CFG_INT( sizeof(rsv_ca)/sizeof(rsv_ca[0])), 
	NG_RSLVO_CACHE_ENT, NG_CFG_PTR( rsv_ca),
	
	NG_CFG_END
};

U32 nginitialization(void)         
{ 
	 
	static int err;
		
	/* debug code by Phong - init multicast group */
	inmtable [0].inm_next = &inmtable [1];
	inmtable [0].inm_ifnetp = &ifnet0.eif_net;
	inmtable [0].inm_addr = 0x010000e0;
	inmtable [0].inm_refcount = 1;
	inmtable [0].inm_timer = 10;

	inmtable [1].inm_next = NULL;
	inmtable [1].inm_ifnetp = &ifnet0.eif_net;
	inmtable [1].inm_addr = 0x070000e0;
	inmtable [1].inm_refcount = 1;
	inmtable [1].inm_timer = 10;

	/* initialise stack */
	err=ngInit( (NGcfgent *) nginit_conf);		
	
	return((U32)err);	
}

int ngDhcpInit(void)
{
	static int Error;	
	static NGifnet * netptr;		
	
	netptr = ngIfGetPtr("le0");	
	
	Error=ngDhcpcStart(netptr);	 	
	
	return(Error);
}

int ngDhcpRelease(void)
{
	static int Error;	
	static NGifnet * netptr;		
	
	netptr = ngIfGetPtr("le0");	
	
	Error=ngDhcpcRelease(netptr);	 	
	
	return(Error);
}


void ipchange(char* ipaddr)
{
	NGifnet * netptr;
	static U32  err; 
	NGsockaddr myaddr;
	netptr = ngIfGetPtr("le0");
	ngInetATON( ipaddr, &myaddr.sin_addr);
	if ((err=ngIfSetOption(netptr, NG_IFO_ADDR,(void*)&myaddr.sin_addr)) != NG_EOK) 
	{
		print("error changing ip address :%d",err);
		return;
	}
}


void netmask_change(char* ipaddr)
{
	NGifnet * netptr;
	static U32  err; 
	NGsockaddr myaddr;
	netptr = ngIfGetPtr("le0");
	ngInetATON( ipaddr, &myaddr.sin_addr);	
	if ((err=ngIfSetOption(netptr, NG_IFO_NETMASK,(void*)&myaddr.sin_addr)) != NG_EOK) 
	{
		print("erro changing netmask address :%d",err);
		return;
	}
}


void gateway_change(char* ipaddr)
{
	NGifnet * netptr;
	static U32  err; 
	NGsockaddr myaddr;
	netptr = ngIfGetPtr("le0");
	ngInetATON( ipaddr, &myaddr.sin_addr);
	if ((err=ngRouteDefault( myaddr.sin_addr)) != NG_EOK) 
	{
		print("erro changing gateway address :%d",err);
		return;
	}
}

void dns_change(char* dns_server)
{
	NGuint addr;
	static int error;	

	ngInetATON( dns_server, &addr);	

	/* Change primary server address */ 
	error=ngResolvSetOption( NG_RSLVO_SERV1_IPADDR, dns_server);
}


void ip_config(void)
{
	NGifnet * netptr;
	static U32  err; 
	static NGuint netmask;
	static NGuint ipaddr;
	char ipaddrS[40];
	char netmaskS[40];
	
	netptr = ngIfGetPtr("le0");
	if ((err=ngIfGetOption(netptr, NG_IFO_ADDR,&ipaddr)) != NG_EOK) 
	{
		print("erro reading ip  address :%d\n",err);
		return;
	}
	else
	{
		ngInetNTOA(ipaddr,ipaddrS,40);
		print("ip address	: 	%s\n",ipaddrS);
	}
	if ((err=ngIfGetOption(netptr, NG_IFO_NETMASK,&netmask)) != NG_EOK) 
	{
		print("erro reading netmask address :%d\n",err);
		return;
	}
	else
	{
		ngInetNTOA(netmask,netmaskS,40);
		print("netmask		: 	%s\n",netmaskS);
	}
	
}

BOOL ip_configwait(void)
{
	NGifnet * netptr;
	static U32  err; 
	static NGuint netmask;
	static NGuint ipaddr;
	char ipaddrS[40];
	char netmaskS[40];
	int i=100;
	
	netptr = ngIfGetPtr("le0");
	do
	{
		i--;
		task_delay(1000);
		if ((err=ngIfGetOption(netptr, NG_IFO_ADDR,&ipaddr)) != NG_EOK) 
		{
			print("error reading ip  address :%d\n",err);
			return FALSE;
		}
    } while ( (ipaddr==0) && (i!=0));
    if (i==0)
    {
		print("error cannot get an IP address via DHCP\n");
		return FALSE;
    }
	else
	{
		ngInetNTOA(ipaddr,ipaddrS,40);
		print("ip address	: 	%s\n",ipaddrS);
	}
	if ((err=ngIfGetOption(netptr, NG_IFO_NETMASK,&netmask)) != NG_EOK) 
	{
		print("erro reading netmask address :%d\n",err);
		return FALSE;
	}
	else
	{
		ngInetNTOA(netmask,netmaskS,40);
		print("netmask		: 	%s\n",netmaskS);
	}
	return TRUE;
	
}


static void display_ping( int seq, int err, int datasize, int ttl, int time,
                          NGsockaddr *saddr, void *data)
{
	char ipaddr[40];

	ngMemSet(ipaddr, 0, sizeof(ipaddr));
	ngInetNTOA(saddr->sin_addr,ipaddr,40);
	/*ipaddr[16]='\0';*/
    if(err==NG_EOK)
        print( "%d bytes from %s: icmp_seq=%d ttl=%d time=%d ms\n",datasize,ipaddr, seq, ttl, time);
    else
        print( "Host not responding\n");
}



void ping(char* ipaddr)
{
	NGsockaddr myaddr;
	NGcfgping cfgping;
	ngMemSet( &cfgping, 0, sizeof(cfgping));
    	cfgping.repeat       = 4;
    	cfgping.data_len     = 0;
    	cfgping.display      = display_ping;
    	ngInetATON( ipaddr, &myaddr.sin_addr);
	ngPing(&myaddr,&cfgping);
	
}

void ngexit(void) 
{
	 
	/* initialise stack */
	ngExit( 0);

}

void dhcpCallbackProc(int event, NGubyte incoming_mask, NGdhcpclient*dcp)
{
	
	static U32 err;	
		
	
	/* at index 0 in reqOpt	: subnet */
 	if (incoming_mask & (1<<0)) 
 	{
 		if((err = (dcp->dc_netp)->if_cntl_f(dcp->dc_netp, NG_CNTL_SET,NG_IFO_NETMASK,&(subnet))))
 		{
			print("Error on setting netmask:%d",err);
			return;
		}
	}
		
	/* at index 1 in reqOpt	: gateway */ 
	if (incoming_mask & (1<<1)) 
	{	
	
	}
	
	/* at index 2 in reqOpt : DNS server */ 
	if (incoming_mask & (1<<2)) 
	{
	
	}
}

⌨️ 快捷键说明

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