<
帖一下代码:IP层
unsigned char
IPv4DatagramSend(unsigned char* pucDestIP,
unsigned char ucUpperProtocol,
unsigned short usDatagramLen)
{
ARPTableEntry* DestAddrPTR;
EthFrameHead* EthSendFrameHeadPTR;
/*Search the ARP Table for the MAC address*/
DestAddrPTR=pucARPEntry(pucDestIP);
if (DestAddrPTR==NULL)
{
/* If no IP address is found, send a ARP request and return 0*/
ARPPacketOut(pucDestIP,
pucBroadcastMACAddr,
ARP_REQUEST);
return 0;
}
else/* Find the IP address in the ARP Table and send the datagram*/
{
/* Ethernet Frame Header Set*/
EthSendFrameHeadPTR=(EthFrameHead *)(&(pucSendBuffer[0]));
memcpy(EthSendFrameHeadPTR->pucDestMACAddr,
DestAddrPTR->pucMACAddr,6);
/* Source MAC is in the Frame.*/
/* Upper protocol is IPv4 protocol*/
EthSendFrameHeadPTR->usUpperProtocol=RFC894_IPv4;
/* IP Datagram header Set*/
/* 1 version and header length*/
SENDIPv4HeaderPTR->ucVerHL=IPv4_nOP;
/* 2 Type of Service */
SENDIPv4HeaderPTR->ucTOS=DEFAULT_TOS_SETTING;
/* Compute the length of the data to be transmitted*/
ulTxLen=(unsigned long)usDatagramLen
+(unsigned long)IPV4_HEADER_LEN
+(unsigned long)ETH_HEADER_LEN;
EthernetPacketPut(ETH_BASE,pucSendBuffer,(long)ulTxLen);
ulTxLen=0;
return 1;
}
}
unsigned char
IPv4DatagramSend(unsigned char* pucDestIP,
unsigned char ucUpperProtocol,
unsigned short usDatagramLen)
{
ARPTableEntry* DestAddrPTR;
EthFrameHead* EthSendFrameHeadPTR;
/*Search the ARP Table for the MAC address*/
DestAddrPTR=pucARPEntry(pucDestIP);
if (DestAddrPTR==NULL)
{
/* If no IP address is found, send a ARP request and return 0*/
ARPPacketOut(pucDestIP,
pucBroadcastMACAddr,
ARP_REQUEST);
return 0;
}
else/* Find the IP address in the ARP Table and send the datagram*/
{
/* Ethernet Frame Header Set*/
EthSendFrameHeadPTR=(EthFrameHead *)(&(pucSendBuffer[0]));
memcpy(EthSendFrameHeadPTR->pucDestMACAddr,
DestAddrPTR->pucMACAddr,6);
/* Source MAC is in the Frame.*/
/* Upper protocol is IPv4 protocol*/
EthSendFrameHeadPTR->usUpperProtocol=RFC894_IPv4;
/* IP Datagram header Set*/
/* 1 version and header length*/
SENDIPv4HeaderPTR->ucVerHL=IPv4_nOP;
/* 2 Type of Service */
SENDIPv4HeaderPTR->ucTOS=DEFAULT_TOS_SETTING;
/* Datagram Length (IPv4 datagram Header included)*/
if (usDatagramLen+IPV4_HEADER_LEN<46)
{
usDatagramLen=46-IPV4_HEADER_LEN;
}
#ifdef Little_Endian
SENDIPv4HeaderPTR->usDataLen=usDatagramLen+IPV4_HEADER_LEN;
#endif
#ifdef Big_Endian
SENDIPv4HeaderPTR->usDataLen=ByteReverseShort(usDatagramLen+IPV4_HEADER_LEN);
#endif
// Identification
usIPID++;
SENDIPv4HeaderPTR->usId=usIPID;
/* Flag Fragment offset
fragment when necessary
Fragment offset is zero*/
SENDIPv4HeaderPTR->usFlagFragment=0x00;
/* TTL hops=128 in this system */
SENDIPv4HeaderPTR->ucTTL=128;
/* Upper Protocol*/
SENDIPv4HeaderPTR->ucUpperProtocol=ucUpperProtocol;
/* Source IP address set:pucSourceIPAddr*/
memcpy(SENDIPv4HeaderPTR->pucSourceIPAddr,pucHostIPAddr,4);
/* Destination IP address set:pucDestIPAddr*/
memcpy(SENDIPv4HeaderPTR->pucDestIPAddr,pucDestIP,4);
/* Header checksum*/
IPv4HeaderChecksumWrite(SENDIPv4HeaderPTR);
/* Compute the length of the data to be transmitted*/
ulTxLen=(unsigned long)usDatagramLen
+(unsigned long)IPV4_HEADER_LEN
+(unsigned long)ETH_HEADER_LEN;
EthernetPacketPut(ETH_BASE,pucSendBuffer,(long)ulTxLen);
ulTxLen=0;
return 1;
}
}
应该是 MFC Winsock 的问题吧.
对上位机多做几次测试看看.
后续上位机,我打算用 QT .
一周热门 更多>