【计网实验——prj12】网络地址转换实验

本文详细介绍了网络地址转换(NAT)的三个实验,包括SNAT、DNAT和复杂NAT场景。实验涵盖了配置config文件、区分数据包方向、处理合法数据包及NAT老化操作。通过实验结果展示,验证了NAT转换的成功。同时探讨了NAT如何支持UDP和ICMP协议,以及在不同内网主机间建立TCP连接的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【计网实验——prj12】网络地址转换实验

实验要求

实验一

SNAT实验

  • 运行给定网络拓扑(nat_topo.py)
  • 在n1, h1, h2, h3上运行相应脚本
    • n1: disable_arp.sh, disable_icmp.sh, disable_ip_forward.sh, disable_ipv6.sh
    • h1-h3: disable_offloading.sh, disable_ipv6.sh
  • 在n1上运行nat程序: n1# ./nat exp1.conf
  • 在h3上运行HTTP服务:h3# python ./http_server.py
  • 在h1, h2上分别访问h3的HTTP服务
    • h1# wget http://159.226.39.123:8000
    • h2# wget http://159.226.39.123:8000

实验二

DNAT实验

  • 运行给定网络拓扑(nat_topo.py)
  • 在n1, h1, h2, h3上运行相应脚本
    • n1: disable_arp.sh, disable_icmp.sh, disable_ip_forward.sh, disable_ipv6.sh
    • h1-h3: disable_offloading.sh, disable_ipv6.sh
  • 在n1上运行nat程序: n1# ./nat exp2.conf
  • 在h1, h2上分别运行HTTP Server: h1/h2# python ./http_server.py
  • 在h3上分别请求h1, h2页面
    • h3# wget http://159.226.39.43:8000
    • h3# wget http://159.226.39.43:8001

实验三

  • 手动构造一个包含两个nat的拓扑
    • h1 <-> n1 <-> n2 <-> h2
  • 节点n1作为SNAT, n2作为DNAT,主机h2提供HTTP服务,主机h1穿过两个nat连接到h2并获取相应页面

实现方案

配置config文件

int parse_config(const char *filename) 函数

  负责根据config文件中读取的每一行字符串,分别配置external-ifaceinternal-iface以及DNAT Rules。 具体实现如下:

int parse_config(const char *filename)
{
   
	fprintf(stdout, "TODO: parse config file, including i-iface, e-iface (and dnat-rules if existing).\n");
	FILE * fd = fopen(filename, "r");
	if (fd == NULL) {
   
		return 0;
	}
	char *line = (char *)malloc(MAX_LINE);
	while (fgets(line, MAX_LINE, fd) != NULL) {
   
		char *type = strtok(line, ": ");
		char *content = strtok(NULL, " ");
		content[strlen(content)-1] = '\0';
		if (strstr(type, "internal-iface")) {
   
			//parse internal iface
			iface_info_t *iface = if_name_to_iface(content);
			nat.internal_iface = iface;
		} 
		else if (strstr(type, "external-iface")) {
   
			//parse external iface
			iface_info_t *iface = if_name_to_iface(content);
			nat.external_iface = iface;
		} 
		else if (strstr(type, "dnat-rules")) {
   
			//parse dnat rules
			char *ip1 = strtok(content, " ")
			char *ip2 = strtok(NULL, "-> ")
			ip1 = strtok(ip1, ":");
			char *port1 = strtok(NULL, " ");
			ip2 = strtok(ip2, ":");
			char *port2 = strtok(NULL, " ");

			int external_ip = ip_to_number(ip1);
			int external_port = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值