-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
175 lines (156 loc) · 5.04 KB
/
Copy pathutils.c
File metadata and controls
175 lines (156 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "utils.h"
#include "udp.h"
#include "tcp.h"
#include "net.h"
#include <stdio.h>
#include <string.h>
/**
* @brief ip转字符串
*
* @param ip ip地址
* @return char* 生成的字符串
*/
char *iptos(uint8_t *ip) {
static char output[3 * 4 + 3 + 1];
sprintf(output, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
return output;
}
/**
* @brief mac转字符串
*
* @param mac mac地址
* @return char* 生成的字符串
*/
char *mactos(uint8_t *mac) {
static char output[2 * 6 + 5 + 1];
sprintf(output, "%02X-%02X-%02X-%02X-%02X-%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return output;
}
/**
* @brief 时间戳转字符串
*
* @param timestamp 时间戳
* @return char* 生成的字符串
*/
char *timetos(time_t timestamp) {
static char output[20];
struct tm *utc_time = gmtime(×tamp);
sprintf(output, "%04d-%02d-%02d %02d:%02d:%02d", utc_time->tm_year + 1900, utc_time->tm_mon + 1, utc_time->tm_mday, utc_time->tm_hour, utc_time->tm_min, utc_time->tm_sec);
return output;
}
/**
* @brief ip前缀匹配
*
* @param ipa 第一个ip
* @param ipb 第二个ip
* @return uint8_t 两个ip相同的前缀长度
*/
uint8_t ip_prefix_match(uint8_t *ipa, uint8_t *ipb) {
uint8_t count = 0;
for (size_t i = 0; i < 4; i++) {
uint8_t flag = ipa[i] ^ ipb[i];
for (size_t j = 0; j < 8; j++) {
if (flag & (1 << 7))
return count;
else
count++, flag <<= 1;
}
}
return count;
}
/**
* @brief 计算16位校验和
*
* @param buf 要计算的数据包
* @param len 要计算的长度
* @return uint16_t 校验和
*/
uint16_t checksum16(uint16_t *data, size_t len) {
// add by 16 bits
// 防止溢出
uint32_t sum = 0;
// len - 1 为了防止后面的奇数字节处理越界
for (int i = 0; i < len - 1; i += 2) {
sum += data[i / 2];
}
// deal with remain byte
if (len % 2 == 1) {
sum += ((uint8_t *)data)[len - 1];
}
// deal with the high bits
while ((sum >> 16) > 0) {
sum = (sum >> 16) + (sum & 0xFFFF);
}
// get the checksum
return (uint16_t)(~sum);
}
#pragma pack(1)
typedef struct peso_hdr {
uint8_t src_ip[4]; // 源IP地址
uint8_t dst_ip[4]; // 目的IP地址
uint8_t placeholder; // 必须置0,用于填充对齐
uint8_t protocol; // 协议号
uint16_t total_len16; // 整个数据包的长度
} peso_hdr_t;
#pragma pack()
/**
* @brief 计算传输层协议(如TCP/UDP)的校验和
*
* @param protocol 传输层协议号(如NET_PROTOCOL_UDP、NET_PROTOCOL_TCP)
* @param buf 待计算的数据包缓冲区
* @param src_ip 源IP地址
* @param dst_ip 目的IP地址
* @return uint16_t 计算得到的16位校验和
*/
uint16_t transport_checksum(uint8_t protocol, buf_t *buf, uint8_t *src_ip, uint8_t *dst_ip) {
if(protocol == NET_PROTOCOL_UDP){
udp_hdr_t *udp_hdr = (udp_hdr_t *)(buf->data);
// add the pesudo header
buf_add_header(buf, sizeof(peso_hdr_t));
// stash the ip header
peso_hdr_t stash_ip_header;
memcpy(&stash_ip_header, buf->data, sizeof(peso_hdr_t));
// fill the pesudo header
peso_hdr_t *peso_hdr = (peso_hdr_t *)buf->data;
memcpy(peso_hdr->src_ip, src_ip, NET_IP_LEN);
memcpy(peso_hdr->dst_ip, dst_ip, NET_IP_LEN);
peso_hdr->placeholder = 0;
peso_hdr->protocol = protocol;
// 将要发送出去,所以应该swap16
peso_hdr->total_len16 = swap16(buf->len - sizeof(peso_hdr_t));
// calculate the checksum
udp_hdr->checksum16 = 0;
udp_hdr->checksum16 = checksum16((uint16_t *)buf->data, buf->len);
// restore the ip header
memcpy(buf->data, &stash_ip_header, sizeof(peso_hdr_t));
// remove the pesudo header
buf_remove_header(buf, sizeof(peso_hdr_t));
//return the checksum
return udp_hdr->checksum16;
}
else if(protocol == NET_PROTOCOL_TCP){
tcp_hdr_t *tcp_hdr = (tcp_hdr_t *)(buf->data);
// add the pesudo header
buf_add_header(buf, sizeof(peso_hdr_t));
// stash the ip header
peso_hdr_t stash_ip_header;
memcpy(&stash_ip_header, buf->data, sizeof(peso_hdr_t));
// fill the pesudo header
peso_hdr_t *peso_hdr = (peso_hdr_t *)buf->data;
memcpy(peso_hdr->src_ip, src_ip, NET_IP_LEN);
memcpy(peso_hdr->dst_ip, dst_ip, NET_IP_LEN);
peso_hdr->placeholder = 0;
peso_hdr->protocol = protocol;
// 将要发送出去,所以应该swap16
peso_hdr->total_len16 = swap16(buf->len - sizeof(peso_hdr_t));
// calculate the checksum
tcp_hdr->checksum16 = 0;
tcp_hdr->checksum16 = checksum16((uint16_t *)buf->data, buf->len);
// restore the ip header
memcpy(buf->data, &stash_ip_header, sizeof(peso_hdr_t));
// remove the pesudo header
buf_remove_header(buf, sizeof(peso_hdr_t));
//return the checksum
return tcp_hdr->checksum16;
}
}