?? net_fun.c
字號:
#include "netcom.h"
#include "net_fun.h"
#include "sys.h"
extern u8_t UdpDestEthAddr[6];
extern u8_t UdpDestIpAddr[4];
extern u16_t UdpDestPort;
extern u16_t UdpSrcPort;
extern u8_t MyEthAddr[6];
extern u8_t MyIpAddr[4];
extern u8_t ArpDestEthAddr[6];
extern u8_t ArpDestIpAddr[4];
extern u16_t AppDataLen;
extern u16_t SendDataLen;
void udp_hdr_make(u16_t len,u16_t offset,u16_t *pdata,u16_t datalen);
/*
* ************************************************************
* makehdr
* ok,no depend extern
* ***********************************************************
*/
void makehdr(u16_t proto,u16_t len,u16_t offset,u16_t *pdata,u16_t datalen)
{
switch(proto){
case PROTO_UDP:
udp_hdr_make(len,offset,pdata,datalen);
break;
case ETH_P_ARP:
break;
default:
break;
}
}
/*
* **********************************************************
* udp_hdr_make
* ok,no depend extern
* len -->the eth_ip_udp header length
* offset-->offset of where store the header
* pdata--->store where
* datalen-->app data len
* no return
* **********************************************************
*/
void udp_hdr_make(u16_t len,u16_t offset,u16_t *pdata,u16_t datalen)
{
int DATAID = 0x1111;
eth_ip_udp_hdr *phdr = ((eth_ip_udp_hdr *)&pdata[offset]);
/* ethhdr */
phdr->ehdr.destaddr0 = UdpDestEthAddr[0];
phdr->ehdr.destaddr1 = UdpDestEthAddr[1];
phdr->ehdr.destaddr2 = UdpDestEthAddr[2];
phdr->ehdr.destaddr3 = UdpDestEthAddr[3];
phdr->ehdr.destaddr4 = UdpDestEthAddr[4];
phdr->ehdr.destaddr5 = UdpDestEthAddr[5];
phdr->ehdr.srcaddr0 = MyEthAddr[0];
phdr->ehdr.srcaddr1 = MyEthAddr[1];
phdr->ehdr.srcaddr2 = MyEthAddr[2];
phdr->ehdr.srcaddr3 = MyEthAddr[3];
phdr->ehdr.srcaddr4 = MyEthAddr[4];
phdr->ehdr.srcaddr5 = MyEthAddr[5];
phdr->ehdr.proto = ETH_P_IP;
/* iphdr */
phdr->ihdr.ver = 0x4;
phdr->ihdr.hdrlen = 0x5;
phdr->ihdr.tos = 0x00;
phdr->ihdr.totlen = IPHDR_LEN + UDPHDR_LEN + (datalen << 1);
phdr->ihdr.id = DATAID;
phdr->ihdr.frag_offset = 0x0;
phdr->ihdr.ttl = 0x40;
phdr->ihdr.proto = 0x11;
phdr->ihdr.checksum = 0x0000; //left later calc it
phdr->ihdr.srcipaddr0 = MyIpAddr[0];
phdr->ihdr.srcipaddr1 = MyIpAddr[1];
phdr->ihdr.srcipaddr2 = MyIpAddr[2];
phdr->ihdr.srcipaddr3 = MyIpAddr[3];
phdr->ihdr.destipaddr0 = UdpDestIpAddr[0];
phdr->ihdr.destipaddr1 = UdpDestIpAddr[1];
phdr->ihdr.destipaddr2 = UdpDestIpAddr[2];
phdr->ihdr.destipaddr3 = UdpDestIpAddr[3];
/* udphdr */
phdr->uhdr.srcport = UdpSrcPort;
phdr->uhdr.destport = UdpDestPort;
phdr->uhdr.len = UDPHDR_LEN + (datalen << 1);
phdr->uhdr.checksum = 0x0000; //later calc it
}
/*
* *********************************************************
* checksum
* independ
* *********************************************************
*/
u16_t checksum(u16_t len,u16_t offset,u16_t *ppp)
{
u16_t _checksum = 0;
long sum = 0;
u16_t *p = &ppp[offset];
while(len > 1){
sum += *p;
p++;
if(sum & 0x80000000)
sum = (sum & 0xffff) + (sum>>16);
len -= 1;
}
if(len)
sum += *p;
while(sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
_checksum = ~sum;
return _checksum;
}
/*
* **********************************************************
* arp
* return the ethframe len
* *********************************************************
*/
u16_t arp(u16_t opcode,u8_t dhdaddr[6],u8_t tgtipaddr[4],u16_t *pdata)
{
int i;
eth_arp_hdr *hdr = ((eth_arp_hdr *)&pdata[0]);
/* ethhdr */
hdr->ehdr.destaddr0 = 0xff;
hdr->ehdr.destaddr1 = 0xff;
hdr->ehdr.destaddr2 = 0xff;
hdr->ehdr.destaddr3 = 0xff;
hdr->ehdr.destaddr4 = 0xff;
hdr->ehdr.destaddr5 = 0xff;
hdr->ehdr.srcaddr0 = MyEthAddr[0];
hdr->ehdr.srcaddr1 = MyEthAddr[1];
hdr->ehdr.srcaddr2 = MyEthAddr[2];
hdr->ehdr.srcaddr3 = MyEthAddr[3];
hdr->ehdr.srcaddr4 = MyEthAddr[4];
hdr->ehdr.srcaddr5 = MyEthAddr[5];
hdr->ehdr.proto = ETH_P_ARP;
/* arphdr */
hdr->ahdr.hdtype = 0x0001;
hdr->ahdr.proto = ETH_P_IP;
hdr->ahdr.hdlen = 0x06;
hdr->ahdr.protolen = 0x04;
hdr->ahdr.opcode = opcode;
hdr->ahdr.send_hd_addr0 = MyEthAddr[0];
hdr->ahdr.send_hd_addr1 = MyEthAddr[1];
hdr->ahdr.send_hd_addr2 = MyEthAddr[2];
hdr->ahdr.send_hd_addr3 = MyEthAddr[3];
hdr->ahdr.send_hd_addr4 = MyEthAddr[4];
hdr->ahdr.send_hd_addr5 = MyEthAddr[5];
hdr->ahdr.send_ip_addr0 = MyIpAddr[0];
hdr->ahdr.send_ip_addr1 = MyIpAddr[1];
hdr->ahdr.send_ip_addr2 = MyIpAddr[2];
hdr->ahdr.send_ip_addr3 = MyIpAddr[3];
if(opcode == 0x0001){//request
hdr->ahdr.target_hd_addr0 = 0x00;
hdr->ahdr.target_hd_addr1 = 0x00;
hdr->ahdr.target_hd_addr2 = 0x00;
hdr->ahdr.target_hd_addr3 = 0x00;
hdr->ahdr.target_hd_addr4 = 0x00;
hdr->ahdr.target_hd_addr5 = 0x00;
}else if(opcode == 0x0002){//reply
hdr->ahdr.target_hd_addr0 = ArpDestEthAddr[0];
hdr->ahdr.target_hd_addr1 = ArpDestEthAddr[1];
hdr->ahdr.target_hd_addr2 = ArpDestEthAddr[2];
hdr->ahdr.target_hd_addr3 = ArpDestEthAddr[3];
hdr->ahdr.target_hd_addr4 = ArpDestEthAddr[4];
hdr->ahdr.target_hd_addr5 = ArpDestEthAddr[5];
}else
Msg("arp() opcode error!");
hdr->ahdr.target_ip_addr0 = ArpDestIpAddr[0];
hdr->ahdr.target_ip_addr1 = ArpDestIpAddr[1];
hdr->ahdr.target_ip_addr2 = ArpDestIpAddr[2];
hdr->ahdr.target_ip_addr3 = ArpDestIpAddr[3];
for(i = 0;i < 21;i++){
pdata[i] = htons(pdata[i]);
}
//AppDataLen = 18 >> 1;//add it
//SendDataLen = 60 >> 1;
return 60 >> 1;
}
/*
* **************************************************************
* totalmake,construct the UDP protocoled data,
* independ
* data --->where store the ethnet frame
* pappdata ----->the application data will send
* appdatalen -----> applicatiln data len,16 bit counted
* pappenddata ------> this is what I will append before the application data
* appenddatalen--->16bit counted,append data length
* return:ethernet frame length,16bit counted
* ************************************************************
*/
u16_t totalmake(u16_t *data,u16_t *pappdata,u16_t appdatalen,u16_t *pappenddata,u16_t appenddatalen)
{
int i,j;
u16_t ttl;
eth_ip_udp_hdr *hdr = (eth_ip_udp_hdr *)data;
u16_t ip_checksum;
u16_t ethframelen;
//make protocol header
makehdr(PROTO_UDP,21,0,data,appdatalen + appenddatalen);
//copy the append data to data
for(i = 0,j = 21; i <appenddatalen;i++,j++){
data[j] = pappenddata[i];
}
//copy the application data to data
for(i = 0,j = 21 + appenddatalen;i < appdatalen;i++,j++){
data[j] = pappdata[i];
}
////now append data add,so the appdatalen => appdatalen + appenddatalen
appdatalen += appenddatalen;
//ip checksum
ip_checksum = checksum(IPHDR_LEN >> 1,ETHHDR_LEN >> 1,data);
hdr->ihdr.checksum = ip_checksum;
/* make psedu udp hdr */
ttl = hdr->ihdr.ttl;//save ttl
hdr->ihdr.checksum = (appdatalen << 1) + UDPHDR_LEN;
hdr->ihdr.ttl = 0;
//udp checksum
hdr->uhdr.checksum=checksum((PSEUD_UDPHDR_LEN >> 1) + \
(UDPHDR_LEN >> 1) + (appdatalen),\
(ETHHDR_LEN >> 1) + ((IPHDR_LEN - PSEUD_UDPHDR_LEN) >> 1),\
data);
//restore the ip_checksum and ttl
hdr->ihdr.checksum = ip_checksum;
hdr->ihdr.ttl = ttl;
ethframelen = appdatalen + ((ETHHDR_LEN + IPHDR_LEN + UDPHDR_LEN) >> 1);
// host to net byte order
for(i = 0;i < ethframelen;i++){
data[i] = htons(data[i]);
}
if(ethframelen < (ETH_ZLEN >> 1))
ethframelen = ETH_ZLEN>>1;
return ethframelen;
}
/*
***********************************************************
* two nic communication
* depend on SendDataLen,SendDataBuf,AppDataBuf,AppDataLen
***********************************************************
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -