# firewall-libhv
**Repository Path**: zyanishere/firewall-group8
## Basic Information
- **Project Name**: firewall-libhv
- **Description**: 课程项目防火墙 特点
- 工作在网络层,代理模式,构想是可以部署在路由器上(包括软硬)。
- 状态检测
- **Primary Language**: C++
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2022-05-06
- **Last Updated**: 2023-06-12
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## SimpleFirewall-Group8
### 1. 介绍
课程项目防火墙的一种构想。个人学习,仍在努力学习跟进中。
##### 特点
- 工作在网络层,代理模式,构想是可以部署在路由器上(包括软硬)。
- 基于强大的 [libhv: 🔥 比libevent、libuv更易用的国产网络库](https://gitee.com/libhv/libhv ) 网络事件库,采用事件循环和reactor两种模式
### 2. 软件架构
#### 2.1 事件驱动模式
都在同一线程中工作,充分利用资源。事件触发后,处理的先后有所区别(大致为一下的结构)

#### 2.2 多线程模式(面向对象)

### 3. TODO
#### 3.1 PacketResolver包解析器模快!!!(重点)
- [ ] #### 数据报的解析
- [ ] ##### 1.1 网络层数据包IP地址的解析
- [ ] ##### IP数据报格式(字节数,内容)

```c++
// https://gitee.com/dekuan/VwFirewall
//============IP==============
// sizeof = 20
typedef struct tagIPHeader
{
UCHAR ucVersion : 4; // Version -> V4 V6
UCHAR ucHeaderLength : 4; // Header length ->4字节->15bytes max
USHORT uTotalLength; // Total length
// 整个 IP 数据包的长度,包括 IP 头后面的数据
// 因为是 16bit 所以最大是 65535,但实际没有这么大
USHORT uId; // Identification //分片?
USHORT uOffset; // Fragment offset field
UCHAR ucTimeToLive; // Time to live
UCHAR ucProtocol; // Protocol
USHORT uChecksum; // Checksum
// IP 首部校验和,仅仅对 IP 包的首部
ULONG ulSourceAddress; // Source address
ULONG ulDestinationAddress; // Destination address
}STIPHEADER, * LPSTIPHEADER;
```
#### 参考:
- 从文件中读取数据报,解析头部信息,存入上述结构体【tagIPHeader】中
- 灵活使用指针(根据不同的字节数,转换指针,读取数据)
- ```c++
//windows下
//参考示例
#include
//由于是ip数据报,所有数据开头就是ip头部
//获取ip地址的方法
//ipv4
char src_ip[16]="";
char dst_ip[16]="";
sprintf(src_ip,"%d.%d.%d.%d",ip_adrr[0],ip_adrr[1],ip_adrr[2],ip_adrr[3]);
sprintf(dst_ip,"%d.%d.%d.%d",ip_adrr[4],ip_adrr[5],ip_adrr[6],ip_adrr[7]);
printf("%s---->%s",src_ip,dst_ip);
//当然更简洁方便的办法有
//inet_ntop 将16字节整型转换位点分十进制的字符串
//Family --> 一般填AF_INET 指IPV4
//pAddr --> 地址的指针
//PSTR --> 结果(点分十进制字符串)
//StringBufSize --> 结果数组的大小
inet_ntop(INT Family, const void *pAddr, PSTR pStringBuf, size_t StringBufSize)
```
- [ ] ICMP数据报
```c++
typedef struct tagICMPHeader
{
UCHAR type; // Type of message
UCHAR code; // Code
USHORT checksum; // Checksum
}STICMPHEADER, * LPSTICMPHEADER;
```
- [ ] ##### 1.2 传输层
- [ ] UDP/TCP头部
```c++
// NetHeaders.h
//===============UDP================
typedef struct tagUDPHeader
{
USHORT sourcePort; // Source port
USHORT destinationPort; // Destination port
USHORT length; // Length
USHORT checksum; // Checksum
}STUDPHEADER, * LPSTUDPHEADER;
//==============TCP=================
typedef struct tagTCPHeader
{
USHORT sourcePort; // 16 位源端口(Source Port)
USHORT destinationPort; // 16 位目的端口(Destination Port)
ULONG nSequence; // 32 位序列号(Sequence number)
ULONG nAck; // 32 位确认号(Acknowledgement number)
UCHAR unused : 4; // 6 位保留字(Reserved for future use. Must be zero.)
UCHAR offset : 4; // Data offset
// The number of 32 bit words in the TCP Header. This indicates where
// the data begins. The TCP header (even one including options) is an
// integral number of 32 bits long.
// TCP数据开始位置 = offset * 4;
UCHAR flags; // 6 位标志位(控制用)(Flags)
// URG: Urgent Pointer field significant
// ACK: Acknowledgment field significant
// PSH: Push Function
// RST: Reset the connection
// SYN: Synchronize sequence numbers
// FIN: No more data from sender
USHORT window; // 16 位窗口大小(Window size)
USHORT checksum; // 16 位 TCP 校验和(Checksum)
USHORT urp; // 16 位紧急数据偏移量(Urgent Pointer)
//u_short hlen_sv_control;//HLEN(4 bit)+保留(6 bit)+控制字段(6 bit,URG+ACK+PSH+PST+SYN+FIN)
//Options: no options included here (we assume that the MSS (max.
//segment size) has the default value of 536 bytes)
//Padding: not included here
}STTCPHEADER, * LPSTTCPHEADER;
```
- [ ] [Git (git-scm.com)](https://git-scm.com/) -- 代码下载和提交
- [ ] 由网络层的ip数据包解析出信息(编写函数)
#### 3 Router模块(丢弃/转发)
- [ ] ##### 数据类的确认(需要哪些信息)
- [ ] ##### 数据特征提取?
- [ ] ##### 实现类似于【NAT】功能
```
struct nat_mapping
{
uint8_t originator_hw_addr[6];
uint32_t originator_ip_addr;
uint16_t originator_src_port;
};
typedef struct nat_mapping* nat_mapping_t;
```
- [ ] ###### raw socket包的发送
- [ ] ###### mac地址是否需要更改
#### 4 http服务器&网页交互界面
- [ ] 可以使用libhv的httpServer模块
- [ ] 网页的编写
- [ ] 数据结构的确定
### 4. 编译与安装
- #### Windows
1. [CMake](https://cmake.org/)https://cmake.org/) -- 协助编译
1. 前往cmake下载最新版本并安装
2. [Visual Studio: IDE and Code Editor for Software Developers and Teams (microsoft.com)](https://visualstudio.microsoft.com/) -- for 【MSVC开发工具】
1. 要求版本【大于VS2015】,并安装了C++依赖
3. [Visual Studio Code - Code Editing. Redefined](https://code.visualstudio.com/) -- 主要的文本编辑器和IDE(比较简洁)
1. ### 【git clone】本项目(一定要使用git clone!!,直接下载不知道为什么会乱码,不能编译】
2. 安装C/C++扩展 [C++ programming with Visual Studio Code](https://code.visualstudio.com/docs/languages/cpp)
3. 安装CMake扩展
4. 运行
1. 项目中写有CMake和预编译的【hv.dll】和【hv.lib】(windows平台),因此可以直接编译运行
- 选择好【Debug】【MSVC构建工具】-->点击【build】即可
2. 如果lib目录下的hv.dll或者hv.lib,不匹配系统,无法正常运行,参照[libhv教程02--编译与安装--libhv 编译](https://hewei.blog.csdn.net/article/details/113704737),使用【CMake】【Visual Studio】进行编译
3. 编译完成后,前往【build/bin/firewall.exe】,复制【lib/hv.dll】到exe目录下,【右击】,【以管理员方式运行】
- version1 (包监听)
- #### Linux / Mac
- 待开发 ~
### 6. 模块说明
1. #### PacketListener--- 包监听器
```c++
//基本成员
PacketListener(const char* ip , SafeQueue& queue);
//构造函数,引用了消息队列
SOCKET setupRawSocket()
//初始化工作在网络层的raw socket
EventLoopThread listeningThread;
//工作的线程
Packet_t copy(unsigned char* src, int len) ;
//复制数据包(申请内存,写入堆),并返回引索
inline void produce(Packet_t packet);
//将上述包写入队列,供Resolver读取
```
2. #### PacketResolver---包解析器
3. #### PacketSniffer ---- 抓包器
- 封装了上述的监听器和解析器,和router在同一层级。
```c++
class PacketSniffer :public PacketListener, public PacketResolver
```
4. #### PacketRouter --- 包路由器
- [ ] TODO
- [ ] 根据Configuration转发或者丢弃包
- [ ] NAT ??
5. #### Queue(SafeQueue)--- 线程安全的消息队列
- PacketQueue
```c++
struct Packet_t {
int len; // 包长度
unsigned char* data; //包在堆中的引索(指针)
};
//存入队列中的数据包引索
SafeQueue& packetQueue;
//线程安全的队列
```
- PacketInfoQueue
- [ ] struct PacketInfo
6. libhv的简单使用
1. [libhv接口手册](https://hewei.blog.csdn.net/article/details/103976875)
### 7. 参与贡献
组员:ZhuYao
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request