【计网实验——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-iface
,internal-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 =