/*
* THIS FILE IS FOR IPv6 TEST
*/
// system support
#include "sysinclude.h"
extern void ipv6_DiscardPkt(char* pBuffer,int type);
extern void ipv6_SendtoLower(char*pBuffer,int length);
extern void ipv6_SendtoUp(char *pBuffer,int length);
extern void getIpv6Address(ipv6_addr *paddr);
// implemented by students
int stud_ipv6_recv(char *pBuffer, unsigned short length)
{
int version = (int)pBuffer[0] >> 4;
unsigned int payloadLen = *(short unsigned int*)(pBuffer+4);
unsigned int hopLimit = (unsigned int)pBuffer[7];
ipv6_addr *dstAddr = (ipv6_addr*)( pBuffer+24);
if(version != 6)
{
ipv6_DiscardPkt(pBuffer, STUD_IPV6_TEST_VERSION_ERROR);
return 1;
}
if(hopLimit <= 0)
{
ipv6_DiscardPkt(pBuffer, STUD_IPV6_TEST_HOPLIMIT_ERROR);
return 1;
}
ipv6_addr *paddr;
printf(">>>>1\n");
getIpv6Address(paddr);
printf(">>>>2\n");
for(int i=0; i<4; i++)
{
if(paddr->dwAddr[i] != dstAddr->dwAddr[i])
{
ipv6_DiscardPkt(pBuffer, STUD_IPV6_TEST_DESTINATION_ERROR);
return 1;
}
}
ipv6_SendtoUp(pBuffer, length);
return 0;
}
int stud_ipv6_Upsend(char *pData, unsigned short len,
ipv6_addr *srcAddr, ipv6_addr *dstAddr,
char hoplimit, char nexthead)
{
char send[len+40];
short int sendLen = htons(len);
send[0] = 0x60;
memcpy(send+4, &sendLen, sizeof(short int));
memcpy(send+6, &nexthead, sizeof(char));
memcpy(send+7, &hoplimit, sizeof(char));
memcpy(send+8, srcAddr, sizeof(ipv6_addr));
memcpy(send+24, dstAddr, sizeof(ipv6_addr));
memcpy(send+40, pData, len);
ipv6_SendtoLower(send, len+40);
return 0;
}
网络实验 ipv6协议收发实验
最新推荐文章于 2022-05-27 15:36:19 发布