哈工大 计算机网络 实验4 ipv4收发实验 附两份代码

在INET上编译执行

注意的是,该软件只支持32位系统

一般在win7-32位

1. 默认值安装 WinPcap_4_0_2.exe 软件。

 2. 双击 SimplePAD-客户端 v2.exe。将客户端解压到本地文件夹。

 3. 进入解压目录中的\expsys\bin 目录下,双击运行客户端 INEP.exe。 

4. 初次登录请选择菜单中“系统”—“系统设置”配置服务器 IP 地址 

  
 
5. 服务器访问地址       

  实验控制设备地址:201.118.249.113 协议测试设备地址:202.118.249.116 


6. 选择菜单中“系统”—“登录”输入学号,密码进行登录 

 
7. 选择实验内容进行实验 


 选择好实验内容后,还要把下面的测试内容全部选上 
 
8. 代码编写 点击工具栏的“新建”,系统会自动生成一个模板,包含了所需的头文件、可以调用的系 统函数以及学生需要实现的代码框架。 
 
9. 调试运行 

 
10. 提交结果 在运行的同时,系统会弹出程序执行过程的窗口(见上图) ,在运行结束后该窗口不会 自动关闭,此时需要用户按一下回车键,会出现下面的测试结果窗口,用户可以看到程序运 行的测试结果,并可以将测试结果递交到服务器。 
 

调试代码:

样例1:

/*
* THIS FILE IS FOR IP TEST
*/
// system support
#include "sysInclude.h"

extern void ip_DiscardPkt(char* pBuffer,int type);

extern void ip_SendtoLower(char*pBuffer,int length);

extern void ip_SendtoUp(char *pBuffer,int length);

extern unsigned int getIpv4Address();

// implemented by students

struct Ipv4
{
  char version_ihl;
  char type_of_service;
  short total_length;
  short identification;
  short fragment_offset;
  char time_to_live;
  char protocol;
  short header_checksum;
  unsigned int source_address;
  unsigned int destination_address;
  Ipv4() {
    memset(this,0,sizeof(Ipv4));
  }
  Ipv4(unsigned int len,unsigned int srcAddr,unsigned int dstAddr,
    byte _protocol,byte ttl) {
    memset(this,0,sizeof(Ipv4));
    version_ihl = 0x45;
    total_length = htons(len+20);
    time_to_live = ttl;
    protocol = _protocol;
    source_address = htonl(srcAddr);
    destination_address = htonl(dstAddr);
    
    char *pBuffer;
    memcpy(pBuffer,this,sizeof(Ipv4));
    int sum = 0;
    for(int i = 0; i < 10; i++) {
        if(i != 5) {
          sum += (int)((unsigned char)pBuffer[i*2] << 8);
          sum += (int)((unsigned char)pBuffer[i*2+1]);
        }
    }
    while((sum & 0xffff0000) != 0) {
      sum = (sum & 0xffff) + ((sum >> 16) & 0xffff);
    }
    unsigned short int ssum = sum;
    header_checksum = htons(~ssum);
  }
};

int stud_ip_recv(char *pBuffer,unsigned short length)
{
  Ipv4 *ipv4 = new Ipv4();
  *ipv4 = *(Ipv4*)pBuffer;
  int version = 0xf & ((ipv4->version_ihl)>> 4);
  if(version != 4)  {
    ip_DiscardPkt(pBuffer,STUD_IP_TEST_VERSION_ERROR);
    return 1;
  }
  int ihl = 0xf & ipv4->version_ihl;
  if(ihl < 5) {
    ip_DiscardPkt(pBuffer,STUD_IP_TEST_HEADLEN_ERROR);
    return 1;
  }
  int ttl = (int)ipv4->time_to_live;
  if(ttl == 0) {
    ip_DiscardPkt(pBuffer,STUD_IP_TEST_TTL_ERROR);
    return 1;
  }
  int destination_address = ntohl(ipv4->destination_address);  
  if(destination_address != getIpv4Address() && destination_address != 0xffffffff) {
    ip_DiscardPkt(pBuffer,STUD_IP_TEST_DESTINATION_ERROR);
    return 1;
  }
  int header_checksum = ntohs(ipv4->header_checksum);
  int sum = 0;
  for(int i = 0; i < ihl*2; i++) {
    if(i!=5)
    {
      sum += (int)((unsigned char)pBuffer[i*2] << 8);
      sum += (int)((unsigned char)pBuffer[i*2+1]);
    }
  }

  while((sum & 0xffff0000) != 0) {
    sum = (sum & 0xffff) + ((sum >> 16) & 0xffff);
  }
  unsigned short int ssum = (~sum) & 0xffff;
  if(ssum != header_checksum) {
    ip_DiscardPkt(pBuffer,STUD_IP_TEST_CHECKSUM_ERROR);
    return 1;
  }
  ip_SendtoUp(pBuffer,length);
  return 0;
}

int stud_ip_Upsend(char *pBuffer,unsigned short len,unsigned int srcAddr,
           unsigned int dstAddr,byte protocol,byte ttl)
{
  char *pack_to_sent = new char[len+20];
  memset(pack_to_sent,0,len+20);
  *((Ipv4*)pack_to_sent) = Ipv4(len,srcAddr,dstAddr,protocol,ttl);
  memcpy(pack_to_sent+20,pBuffer,len);
  ip_SendtoLower(pack_to_sent,len+20);
  delete[] pack_to_sent;
  
  return 0;
}

代码样例2:

/*
* THIS FILE IS FOR IP TEST
*/
// system support
#include "sysInclude.h"
#include <stdio.h>
#include <malloc.h>

extern void ip_DiscardPkt(char* pBuffer,int type);

extern void ip_SendtoLower(char*pBuffer,int length);

extern void ip_SendtoUp(char *pBuffer,int length);

extern unsigned int getIpv4Address();

// implemented by students

int stud_ip_recv(char *pBuffer,unsigned short length)
{
    	int version = pBuffer[0] >> 4;  
    	int headLength = pBuffer[0] & 0xf; 
	int TTL = (unsigned short)pBuffer[8]; 
	int headCheckSum = ntohs(*(unsigned short *)(pBuffer + 10));
	int dstAddr = ntohl(*(unsigned int*)(pBuffer + 16));
	
	//TTL值错误
	if (TTL <= 0){
		ip_DiscardPkt(pBuffer, STUD_IP_TEST_TTL_ERROR);
		return 1;
	}
	
	//IP版本号错
	if (version != 4){
		ip_DiscardPkt(pBuffer, STUD_IP_TEST_VERSION_ERROR);
		return 1;
	}
	
	//头部长度错
	if (headLength < 5){
		ip_DiscardPkt(pBuffer, STUD_IP_TEST_HEADLEN_ERROR);
		return 1;
	}
	
	//目的地址错
	if (dstAddr != getIpv4Address() && dstAddr != 0xffff){
		ip_DiscardPkt(pBuffer,STUD_IP_TEST_DESTINATION_ERROR);  
		return 1;
	}
	
	//校验和错应该最后检验错误
	unsigned short sum = 0; 
	unsigned short tempNum = 0; 
	for (int i = 0; i < headLength * 2; i++){
		tempNum = ((unsigned char)pBuffer[i*2]<<8) + (unsigned char)pBuffer[i*2 + 1];
		if (0xffff - sum < tempNum)
			sum = sum + tempNum + 1;
		else
			sum = sum + tempNum;
	}
	if (sum != 0xffff){
		ip_DiscardPkt(pBuffer, STUD_IP_TEST_CHECKSUM_ERROR);
		return 1;
	}

	//成功接受
 	ip_SendtoUp(pBuffer,length); 
	return 0;
}

int stud_ip_Upsend(char *pBuffer,unsigned short len,unsigned int srcAddr,
				   unsigned int dstAddr,byte protocol,byte ttl)
{
	char *IPBuffer = (char *)malloc((20 + len) * sizeof(char));
	memset(IPBuffer, 0, len+20);  
	IPBuffer[0] = 0x45;	//版本号+头长度
	unsigned short totalLength =  htons(len + 20);	//分组总长度
	memcpy(IPBuffer + 2, &totalLength, 2);
	IPBuffer[8] = ttl;  	//ttl
	IPBuffer[9] = protocol; //协议
      
	unsigned int src = htonl(srcAddr);  
	unsigned int dis = htonl(dstAddr);  
	memcpy(IPBuffer + 12, &src, 4);  //源与目的IP地址
	memcpy(IPBuffer + 16, &dis, 4);  
      
	unsigned short sum = 0; 
	unsigned short tempNum = 0; 
	unsigned short headCheckSum = 0;

	//计算checksum
	for (int i = 0; i < 10; i++){
		tempNum = ((unsigned char)IPBuffer[i*2]<<8) + (unsigned char)IPBuffer[i*2 + 1];
		if (0xffff - sum < tempNum)
			sum = sum + tempNum + 1;
		else
			sum = sum + tempNum;
	}
	headCheckSum = htons(0xffff - sum);  
	memcpy(IPBuffer + 10, &headCheckSum, 2);  
	memcpy(IPBuffer + 20, pBuffer, len);    
	ip_SendtoLower(IPBuffer,len+20);  
	return 0;  
}


猜你喜欢

转载自blog.csdn.net/junruitian/article/details/80291631