1.题目描述
参考计算机网络教材 188 页内容,模拟 NAT 路由器的工作过程,主要有 2 个步骤的工作:1、将收到的来自 内网报文中的私有源 IP 地址转换为 NAT 的外部合法 IP 地址,同时将传输层源端口号转换为 NAT 路 由器分配的端口号,建立转换映射表;2、将收到的来自外网的应答报文提取其目的 IP 地址及端口号, 查找映射表,找到其对应的内网机器的 IP 地址及端口号并替换。 转换表如下图所示:
因此我们只需要实现转换功能就可以,可以用if else来实现(doge)。
这里我们可以用两个Map表来维护,保证查询速度
2.运行Demo
图2.1主页面
图2.2 转换结果
其他功能自行探索 ,doge
3.程序代码
//time:2024.1.4
//coder:MGJ
#include <iostream>
#include<map>
#include <conio.h>
#include <windows.h>
using namespace std;
//功能数量
#define MENUNUMS1 4
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//函数声明
void cls();
void Init_data();
int Menuchoose1();
void Menushow1(int i);
void Function_Select(int select);
void Net_address_translation();
void Net_address_add();
void Net_address_delect();
void Net_address_revise();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
map<string, string> Maps_In;
map<string, string> Maps_Out;
//----------------------------------------------------------------------------------
//主函数
int main() {
int out = 0;
Init_data();
while (true) {
out = Menuchoose1();
Function_Select(out);
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//初始化函数
void Init_data() {
Maps_In["172.38.1.5:40001"] = "192.168.0.3:30000";
Maps_In["172.38.1.5:40002"] = "192.168.0.4:30000";
Maps_Out["192.168.0.3:30000"] = "172.38.1.5:40001";
Maps_Out["192.168.0.4:30000"] = "172.38.1.5:40002";
}
//选择函数
int Menuchoose1() {
int ch, i = 0;
Menushow1(0);
while (true) {
if (_kbhit()) {
ch = _getch();
if (ch == 80) {
//循环
i = (++i) % MENUNUMS1;
Menushow1(i);
} else if (ch == 72) {
//循环防止出现负数
i = ((--i) + MENUNUMS1) % MENUNUMS1;
Menushow1(i);
} else if (ch == 13) return i + 1;
}
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//菜单显示
void Menushow1(int i) {
cls();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << "\n *=====模拟 NAT 网络地址转换Demo=====*\n";
if (i == 0)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
cout << " ->>>转换网络地址<<<- " << endl;
if (i == 1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
if (i == 0)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << " ->>>增加网络地址<<<- " << endl;
if (i == 2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
if (i == 1)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << " ->>>删除网络地址<<<- " << endl;
if (i == 3)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
if (i == 2)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << " ->>>修改网络地址<<<- " << endl;
if (i == 3)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << " -------------------------------------\n";
}
//功能函数
void Function_Select(int select) {
switch (select) {
case 1: {
Net_address_translation();
break;
}
case 2: {
Net_address_add();
break;
}
case 3: {
Net_address_delect();
break;
}
case 4: {
Net_address_revise();
break;
}
}
system("cls");
}
void Net_address_translation() {
string Search_addrs;
cout << "请输入IP与端口(例如:172.38.1.5:40001):" << endl;
cin >> Search_addrs;
cout << endl << " 转换结果为:--------------------------------" << endl << endl;
if (Maps_In.find(Search_addrs) == Maps_In.end() && Maps_Out.find(Search_addrs) == Maps_Out.end()) {
cout << " * 系统没有此地址IP *" << endl;
} else if (Maps_In.find(Search_addrs) != Maps_In.end()) {
cout << " 内部地址: " << Maps_In[Search_addrs] << endl;
} else if (Maps_Out.find(Search_addrs) != Maps_Out.end()) {
cout << " 外部地址:" << Maps_Out[Search_addrs] << endl;
}
cout << endl << " -------------------------------------------" << endl;
system("pause");
}
void Net_address_add() {
string Input_interior_addrs;
string Input_exterior_addrs;
cout << "请输入内部地址(例如:172.38.1.5:40001):" << endl;
cin >> Input_interior_addrs;
cout << "请输入外部地址(例如:172.38.1.5:40001):" << endl;
cin >> Input_exterior_addrs;
cout << endl << " 保存结果为:--------------------------------" << endl << endl;
cout << " 内部地址:" << Input_interior_addrs << endl;
cout << " 外部地址:" << Input_exterior_addrs << endl;
cout << endl << " -------------------------------------------" << endl;
Maps_In[Input_exterior_addrs] = Input_interior_addrs;
Maps_Out[Input_interior_addrs] = Input_exterior_addrs;
system("pause");
}
void Net_address_delect() {
string Search_addrs;
cout << "请输入需要删除的IP与端口(例如:172.38.1.5:40001):" << endl;
cin >> Search_addrs;
cout << endl << " 删除结果为:--------------------------------" << endl << endl;
if (Maps_In.find(Search_addrs) == Maps_In.end() && Maps_Out.find(Search_addrs) == Maps_Out.end()) {
cout << " * 系统没有此地址IP *" << endl;
} else if (Maps_In.find(Search_addrs) != Maps_In.end()) {
cout << " 内部地址: " << Maps_In[Search_addrs] << endl;
cout << " 外部地址: " << Search_addrs<< endl;
Maps_Out.erase(Maps_In[Search_addrs]);
Maps_In.erase(Search_addrs);
} else if (Maps_Out.find(Search_addrs) != Maps_Out.end()) {
cout << " 内部地址: " << Search_addrs<< endl;
cout << " 外部地址: " << Maps_Out[Search_addrs] << endl;
Maps_In.erase(Maps_Out[Search_addrs]);
Maps_Out.erase(Search_addrs);
}
cout << endl << " -------------------------------------------" << endl;
system("pause");
}
void Net_address_revise() {
string Search_addrs;
string Input__addrs;
cout << "请输入需要修改的IP与端口(例如:172.38.1.5:40001):" << endl;
cin >> Search_addrs;
cout << endl << " 当前结果为:--------------------------------" << endl << endl;
if (Maps_In.find(Search_addrs) == Maps_In.end() && Maps_Out.find(Search_addrs) == Maps_Out.end()) {
cout << " * 系统没有此地址IP *" << endl;
cout << endl << " -------------------------------------------" << endl;
system("pause");
return;
} else if (Maps_In.find(Search_addrs) != Maps_In.end()) {
cout << " 内部地址: " << Maps_In[Search_addrs] << endl;
cout << " 外部地址: " << Search_addrs<< endl;
cout << endl << " -------------------------------------------" << endl;
cout << "请修改内部的IP与端口(例如:172.38.1.5:40001):" << endl,cin>>Input__addrs;;
Maps_In[Search_addrs]=Input__addrs;
} else if (Maps_Out.find(Search_addrs) != Maps_Out.end()) {
cout << " 内部地址: " << Search_addrs<< endl;
cout << " 外部地址: " << Maps_Out[Search_addrs] << endl;
cout << endl << " -------------------------------------------" << endl;
cout << "请修改外部的IP与端口(例如:172.38.1.5:40001):" << endl,cin>>Input__addrs;;
Maps_Out[Search_addrs]=Input__addrs;
}
cout << endl << " -------------------------------------------" << endl;
system("pause");
}
//清屏函数
void cls() {
COORD pos;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
pos.Y = pos.X = 0;
SetConsoleCursorPosition(hOut, pos);
}
点个关注,哈哈
2024 HNUST计算机网络课程设计-(ᕑᗢᓫ∗)˒芒果酱-参考文章
(代码可以参考,૮₍ ˃ ⤙ ˂ ₎ა 但同学们要认真编写哦)
-------------------------------------------------------------------------
1、网络聊天程序的设计与实现
C++ Socket 多线程 网络聊天室 支持用户端双向交流(2023)-CSDN博客
2、Tracert 与 Ping 程序设计与实现
Tracert 与 Ping 程序设计与实现(2024)-CSDN博客
3、滑动窗口协议仿真
滑动窗口协议仿真(2024)-CSDN博客
4、OSPF 路由协议原型系统设计与实现
OSPF 路由协议原型系统设计与实现-CSDN博客
5、基于 IP 多播的网络会议程序
基于 IP 多播的网络会议程序(2024)-CSDN博客
6、编程模拟 NAT 网络地址转换
编程模拟 NAT 网络地址转换(2024)-CSDN博客
7、网络嗅探器的设计与实现
网络嗅探器的设计与实现(2024)-转载-CSDN博客
8、网络报文分析程序的设计与实现
网络报文分析程序的设计与实现(2024)-CSDN博客
9、简单 Web Server 程序的设计与实现
简单 Web Server 程序的设计与实现 (2024)-CSDN博客
10、路由器查表过程模拟计算机网络 - 路由器查表过程模拟 C++(2024)-CSDN博客