大家帮忙看一下
这是我用rtl8019as上网的程序 我想知道怎么样在这上面加TCP和UDP的程序进行解包和压包。可以的话请您发到我的邮箱tubage21@elong.com
_______________________________________________
#include "rtlregs.h"
#define NIC_PAGE_SIZE 256
#define NIC_START_PAGE 0x40
#define NIC_STOP_PAGE 0x80
#define NIC_TX_PAGES 6
#define NIC_TX_BUFFERS 2
#define NIC_FIRST_TX_PAGE NIC_START_PAGE
#define NIC_FIRST_RX_PAGE (NIC_FIRST_TX_PAGE + NIC_TX_PAGES * NIC_TX_BUFFERS)
#define TX_PAGES 12
static void NicCompleteDma(void)
{
u_char i;
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
for(i = 0; i <= 20; i++)
if(nic_read(NIC_PG0_ISR) & NIC_ISR_RDC)
break;
nic_write(NIC_PG0_ISR, NIC_ISR_RDC);
}
void NicRemovePacket(void)
{
struct nic_pkt_header_struct hdr;
u_short count;
u_char *buf;
u_char nextpg;
u_char bnry;
u_char curr;
u_short i;
u_char ict;
bnry = nic_read(NIC_PG0_BNRY) + 1;
if(bnry > = NIC_STOP_PAGE)
bnry = NIC_FIRST_RX_PAGE;
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2 | NIC_CR_PS0);
curr = nic_read(NIC_PG1_CURR);
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
if(bnry == curr)
return;
nic_write(NIC_PG0_RBCR0, sizeof(struct nic_pkt_header_struct));
nic_write(NIC_PG0_RBCR1, 0);
nic_write(NIC_PG0_RSAR0, 0);
nic_write(NIC_PG0_RSAR1, bnry);
buf = (u_char *)&hdr;
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD0);
for(i = 0; i < sizeof(struct nic_pkt_header_struct); i++)
*buf++ = nic_read(NIC_IOPORT);
NicCompleteDma();
count = hdr.ph_size - sizeof(struct nic_pkt_header_struct);
nic_write(NIC_PG0_RBCR0, count);
nic_write(NIC_PG0_RBCR1, count > > 8);
nic_write(NIC_PG0_RSAR0, sizeof(struct nic_pkt_header_struct));
nic_write(NIC_PG0_RSAR1, bnry);
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD0);
for(i = 0; i < count; i++)
ict = nic_read(NIC_IOPORT);
NicCompleteDma();
ict = ict;
nextpg = hdr.ph_nextpg - 1;
if(nextpg < NIC_FIRST_RX_PAGE)
nextpg = NIC_STOP_PAGE - 1;
nic_write(NIC_PG0_BNRY, nextpg);
}
unsigned int NicGetPacket(u_char *pkt1)
{
struct nic_pkt_header_struct hdr;
u_short count;
u_char *buf;
u_char nextpg;
u_char bnry;
u_char curr;
u_short i;
buf = (u_char *)&hdr;
bnry = nic_read(NIC_PG0_BNRY) + 1;
if(bnry > = NIC_STOP_PAGE)
bnry = NIC_FIRST_RX_PAGE;
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2 | NIC_CR_PS0);
curr = nic_read(NIC_PG1_CURR);
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
if(bnry == curr)
return 0;
nic_write(NIC_PG0_RBCR0, sizeof(struct nic_pkt_header_struct));
nic_write(NIC_PG0_RBCR1, 0);
nic_write(NIC_PG0_RSAR0, 0);
nic_write(NIC_PG0_RSAR1, bnry);
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD0);
for(i = 0; i < sizeof(struct nic_pkt_header_struct); i++)
*buf++ = nic_read(NIC_IOPORT);
NicCompleteDma();
count = hdr.ph_size - sizeof(struct nic_pkt_header_struct);
nic_write(NIC_PG0_RBCR0, count);
nic_write(NIC_PG0_RBCR1, count > > 8);
nic_write(NIC_PG0_RSAR0, sizeof(struct nic_pkt_header_struct));
nic_write(NIC_PG0_RSAR1, bnry);
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD0);
for (i = 0; i < count; i++)
*pkt1++ = nic_read(NIC_IOPORT);
NicCompleteDma();
nextpg = hdr.ph_nextpg - 1;
if(nextpg < NIC_FIRST_RX_PAGE)
nextpg = NIC_STOP_PAGE - 1;
nic_write(NIC_PG0_BNRY, nextpg);
return count;
}
int NicPutPacket(u_char *data, u_short sz)
{
u_short i;
u_char *p;
u_char padding = 0;
u_short sz1=sz;
if(sz1 > = 0x0600) return -1;
if(sz1 < 60) {
padding = (u_char)(60 - sz1);
sz1 = 60;
}
nic_write(NIC_PG0_RBCR0, sz1);
nic_write(NIC_PG0_RBCR1, sz1 > > 8);
nic_write(NIC_PG0_RSAR0, 0);
nic_write(NIC_PG0_RSAR1, NIC_FIRST_TX_PAGE);
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD1);
p = data;
for(i = 0; i < sz; i++)
nic_write(NIC_IOPORT, *p++);
for(i = 0; i < padding; i++)
nic_write(NIC_IOPORT, 0);
NicCompleteDma();
nic_write(NIC_PG0_TBCR0, (sz1 & 0xff));
nic_write(NIC_PG0_TBCR1, ((sz1 > > 8) & 0xff));
nic_write(NIC_PG0_TPSR, NIC_FIRST_TX_PAGE);
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_TXP | NIC_CR_RD2);
return 0;
}
int NicInit(unsigned char *mac)
{
u_char i;
u_char j;
for(j = 0; j < 10; j++) {
if(nic_read(NIC_PG0_ISR) & NIC_ISR_RST)
break;
nic_write(NIC_RESET, nic_read(NIC_RESET));
for(i = 0; i < 255; i++) {
if(nic_read(NIC_PG0_ISR) & NIC_ISR_RST)
break;
}
}
nic_write(NIC_PG0_IMR, 0);
nic_write(NIC_PG0_ISR, 0xff);
nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1);
nic_write(NIC_PG3_EECR, NIC_EECR_EEM0 | NIC_EECR_EEM1);
nic_write(NIC_PG3_CONFIG2, NIC_CONFIG2_BSELB);
nic_write(NIC_PG3_CONFIG3, NIC_CONFIG3_FUDUP);
nic_write(NIC_PG3_EECR, 0);
nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2);
nic_write(NIC_PG0_DCR, NIC_DCR_LS | NIC_DCR_FT1);
nic_write(NIC_PG0_RBCR0, 0);
nic_write(NIC_PG0_RBCR1, 0);
nic_write(NIC_PG0_RCR, NIC_RCR_MON);
nic_write(NIC_PG0_TCR, NIC_TCR_LB0);
nic_write(NIC_PG0_TPSR, NIC_FIRST_TX_PAGE);
nic_write(NIC_PG0_BNRY, NIC_STOP_PAGE - 1);
nic_write(NIC_PG0_PSTART, NIC_FIRST_RX_PAGE);
nic_write(NIC_PG0_PSTOP, NIC_STOP_PAGE);
nic_write(NIC_PG0_IMR, 0);
nic_write(NIC_PG0_ISR, 0xff);
nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0);
for(i = 0; i < 6; i++)
nic_write(NIC_PG1_PAR0 + i, mac[i]);
for(i = 0; i < 8; i++)
nic_write(NIC_PG1_MAR0 + i, 0);
nic_write(NIC_PG1_CURR, NIC_START_PAGE + TX_PAGES);
nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2);
nic_write(NIC_PG0_RCR, NIC_RCR_AB);
nic_write(NIC_PG0_ISR, 0xff);
nic_write(NIC_PG0_IMR, NIC_IMR_PRXE | NIC_IMR_PTXE);
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
nic_write(NIC_PG0_TCR, 0);
return 0;
}
unsigned char IsDataReady(void)
{
unsigned char rxdwrite,rxdread;
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2 | NIC_CR_PS0);
rxdwrite = nic_read(NIC_PG1_CURR);
nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2 );
rxdread = nic_read(NIC_PG0_BNRY) +1;
if(rxdread==NIC_STOP_PAGE) rxdread=NIC_FIRST_RX_PAGE;
if (rxdread!=rxdwrite) //at least recieved one packet;
return 1;
else
return 0;
}
_______________________________________________
#include <string.h>
#include "rtlregs.h"
#include <conio.h>
#include <stdio.h>
#include "NicRtl.h"
#include "NetFun.h"
extern unsigned char MyMac[6];
extern unsigned long MyIP;
unsigned char rece_buf[1560];
unsigned int len;
unsigned int CkSum(unsigned int *p, unsigned int len)
{
unsigned int i;
long int lsum;
unsigned int r,r1;
lsum = 0l;
for (i=0;i <len;i++)
lsum = lsum+(long int)(*(p+i));
r = (unsigned int)(lsum & 0xFFFF);
r1 = (unsigned int)((lsum -r)> > 16);
return ~(r+r1);
}
void NetReceiveEvent(void)
{
struct ethernet_struct *ethernet;
struct ether_arp_struct *etharp;
struct ether_ip_struct *ip;
struct icmp_struct *icmp;
ethernet = (struct ethernet_struct *)rece_buf;
len = NicGetPacket(rece_buf);
switch ( ntohs(ethernet-> ether_header.ether_type)) {
case 0x0806:
etharp = (struct ether_arp_struct *)ethernet-> packet;
if (ntohl(etharp-> arp_tpa)!=MyIP) break;
if (ntohs(etharp-> ea_hdr.ar_hrd) != 0x0001) break;
if (ntohs(etharp-> ea_hdr.ar_pro) != 0x0800) break;
if (etharp-> ea_hdr.ar_hln != 0x06) break;
if (etharp-> ea_hdr.ar_pln != 0x04) break;
switch (ntohs(etharp-> ea_hdr.ar_opc)) {
case 0x0001: // ARP request
memcpy(ethernet-> ether_header.ether_dhost, ethernet-> ether_header.ether_shost, 6);
memcpy(etharp-> arp_tha, ethernet-> ether_header.ether_shost, 6);
etharp-> arp_tpa = etharp-> arp_spa;
etharp-> arp_spa = htonl(MyIP);
memcpy(etharp-> arp_sha, MyMac, 6);
memcpy(ethernet-> ether_header.ether_shost, MyMac, 6);
etharp-> ea_hdr.ar_opc = htons(0x0002);
NicPutPacket(rece_buf, 0x3C);
break;
case 0x0002: // ARP answer
break;
case 0x0003: // RARP request
break;
case 0x0004: // RARP answer
break;
default:
break;
}
break;
case 0x0800:
ip = (struct ether_ip_struct *)ethernet-> packet;
icmp = (struct icmp_struct *)ip-> ip_dat;
switch (ip-> ei_hdr.ip_prot) {
case 0x01: // ICMP
memcpy(ethernet-> ether_header.ether_dhost, ethernet-> ether_header.ether_shost, 6);
memcpy(ethernet-> ether_header.ether_shost, MyMac, 6);
ip-> ei_hdr.ip_sum1=0;
ip-> ei_hdr.ip_dst1 = ip-> ei_hdr.ip_src1;
ip-> ei_hdr.ip_src1 = htonl(MyIP);
icmp-> icmp_cksum = 0;
icmp-> icmp_type = 0;
icmp-> icmp_code = 0;
ip-> ei_hdr.ip_sum1 = CkSum((unsigned int *)ip,10);
icmp-> icmp_cksum = CkSum((unsigned int *)icmp,ntohs(ip-> ei_hdr.ip_len1)-20);
NicPutPacket(rece_buf, ntohs(ip-> ei_hdr.ip_len1)+sizeof(struct ether_header_struct));
break;
case 0x06: // TCP
break;
case 0x11: // UDP
break;
case 0x08: // EGP
break;
default:
break;
}
break;
case 0x8035:
break;
default:
break;
}
}
_________________________________________________
发表时间:2003年4月28日16:25:43