Skip to content

Commit ee42b6c

Browse files
committed
[fix] 資料の修正
1 parent 59afd70 commit ee42b6c

1 file changed

Lines changed: 58 additions & 59 deletions

File tree

‎docs/index.html‎

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@
687687
sprintf(eh.ether_shost, "\x00\x00\x00\x00\x00\x00");
688688
eh.ether_type = (u_int16_t)8;
689689

690-
GenerateEthernetPacket(&packet, &eh);
690+
InitBaseEthernetPacket(&packet, &eh);
691691

692692
PrintEthernet(&packet);
693693
PrintRawPacket(&packet);
@@ -716,7 +716,7 @@
716716
lib/generate.c
717717

718718
```c
719-
void GenerateEthernetPacket(Packet *packet, struct ether_header *eh) {
719+
void InitBaseEthernetPacket(Packet *packet, struct ether_header *eh) {
720720
size_t size = sizeof(struct ether_header);
721721
packet -> ptr = (unsigned char *)malloc(size);
722722
packet -> eh = (struct ether_header *)packet -> ptr;
@@ -1262,8 +1262,8 @@
12621262
ip.check = checksum2((unsigned char *)&ip, sizeof(struct iphdr), NULL, 0);
12631263

12641264
// パケットの実態の生成
1265-
GenerateEthernetPacket(&packet, &eh);
1266-
AddIPHeader(&packet, &ip);
1265+
InitBaseEthernetPacket(&packet, &eh);
1266+
CarryProtocolHeader(iphdr, packet, ip);
12671267

12681268
// パケットの表示
12691269
PrintEthernet(&packet);
@@ -1294,21 +1294,26 @@
12941294
lib/generate.c
12951295

12961296
```c
1297-
void GenerateEthernetPacket(Packet *packet, struct ether_header *eh) {
1297+
void InitBaseEthernetPacket(Packet *packet, struct ether_header *eh) {
12981298
size_t size = sizeof(struct ether_header);
12991299
packet -> ptr = (unsigned char *)malloc(size);
13001300
packet -> eh = (struct ether_header *)packet -> ptr;
13011301
memcpy(packet -> eh, eh, sizeof(struct ether_header));
13021302
packet -> size = sizeof(struct ether_header);
13031303
}
1304+
```
13041305

1305-
void AddIPHeader(Packet *packet, struct iphdr *ip) {
1306-
size_t size = sizeof(struct iphdr);
1307-
packet -> ptr = (unsigned char *)realloc(packet -> ptr, packet -> size + size);
1308-
packet -> ip = (struct iphdr *)(packet -> ptr + packet -> size);
1309-
memcpy(packet -> ip, ip, sizeof(struct iphdr));
1310-
packet -> size = size + packet -> size;
1311-
}
1306+
lib/generate.h
1307+
1308+
```c
1309+
#define CarryProtocolHeader(headerType, packet, header) \
1310+
{ \
1311+
size_t size = sizeof(struct headerType); \
1312+
packet.ptr = (unsigned char *)realloc(packet.ptr, packet.size + size);\
1313+
packet.header = (struct headerType *)(packet.ptr + packet.size); \
1314+
memcpy(packet.header, &header, sizeof(struct headerType)); \
1315+
packet.size = size + packet.size; \
1316+
} \
13121317
```
13131318

13141319
>>>
@@ -1360,8 +1365,8 @@
13601365
ip.check = checksum2((unsigned char *)&ip, sizeof(struct iphdr), NULL, 0);
13611366

13621367
// パケットの実態の生成
1363-
GenerateEthernetPacket(&packet, &eh);
1364-
AddIPHeader(&packet, &ip);
1368+
InitBaseEthernetPacket(&packet, &eh);
1369+
CarryProtocolHeader(iphdr, packet, ip);
13651370

13661371
```
13671372

@@ -2013,10 +2018,10 @@
20132018
};
20142019

20152020
// 実体の生成
2016-
GenerateEthernetPacket(&packet, &eh);
2017-
AddIPHeader(&packet, &ip);
2018-
AddUDPHeader(&packet, &udp);
2019-
AddData(&packet, data, sizeof(data));
2021+
InitBaseEthernetPacket(&packet, &eh);
2022+
CarryProtocolHeader(iphdr, packet, ip);
2023+
CarryProtocolHeader(udphdr, packet, udp);
2024+
CarryData(&packet, data, sizeof(data));
20202025

20212026
// 表示
20222027
printf("text:\n");
@@ -2053,29 +2058,26 @@
20532058
lib/generate.c
20542059

20552060
```c
2056-
void GenerateEthernetPacket(Packet *packet, struct ether_header *eh) {
2061+
void InitBaseEthernetPacket(Packet *packet, struct ether_header *eh) {
20572062
size_t size = sizeof(struct ether_header);
20582063
packet -> ptr = (unsigned char *)malloc(size);
20592064
packet -> eh = (struct ether_header *)packet -> ptr;
20602065
memcpy(packet -> eh, eh, sizeof(struct ether_header));
20612066
packet -> size = sizeof(struct ether_header);
20622067
}
2068+
```
20632069

2064-
void AddIPHeader(Packet *packet, struct iphdr *ip) {
2065-
size_t size = sizeof(struct iphdr);
2066-
packet -> ptr = (unsigned char *)realloc(packet -> ptr, packet -> size + size);
2067-
packet -> ip = (struct iphdr *)(packet -> ptr + packet -> size);
2068-
memcpy(packet -> ip, ip, sizeof(struct iphdr));
2069-
packet -> size = size + packet -> size;
2070-
}
2070+
lib/generate.h
20712071

2072-
void AddUDPHeader(Packet *packet, struct udphdr *udp) {
2073-
size_t size = sizeof(struct udphdr);
2074-
packet -> ptr = (unsigned char *)realloc(packet -> ptr, packet -> size + size);
2075-
packet -> udp = (struct udphdr *)(packet -> ptr + packet -> size);
2076-
memcpy(packet -> udp, udp, sizeof(struct udphdr));
2077-
packet -> size = size + packet -> size;
2078-
}
2072+
```c
2073+
#define CarryProtocolHeader(headerType, packet, header) \
2074+
{ \
2075+
size_t size = sizeof(struct headerType); \
2076+
packet.ptr = (unsigned char *)realloc(packet.ptr, packet.size + size);\
2077+
packet.header = (struct headerType *)(packet.ptr + packet.size); \
2078+
memcpy(packet.header, &header, sizeof(struct headerType)); \
2079+
packet.size = size + packet.size; \
2080+
} \
20792081
```
20802082

20812083
>>>
@@ -2887,10 +2889,10 @@
28872889

28882890

28892891
// 実体の生成
2890-
GenerateEthernetPacket(&packet, &eh);
2891-
AddIPHeader(&packet, &ip);
2892-
AddTCPHeader(&packet, &tcp);
2893-
AddData(&packet, data, sizeof(data));
2892+
InitBaseEthernetPacket(&packet, &eh);
2893+
CarryProtocolHeader(iphdr, packet, ip);
2894+
CarryProtocolHeader(tcphdr, packet, tcp);
2895+
CarryData(&packet, data, sizeof(data));
28942896

28952897
// チェックサム
28962898
int len = ntohs(packet.ip -> tot_len) - packet.ip -> ihl * 4;
@@ -2931,40 +2933,37 @@
29312933
lib/generate.c
29322934

29332935
```c
2934-
void GenerateEthernetPacket(Packet *packet, struct ether_header *eh) {
2936+
void InitBaseEthernetPacket(Packet *packet, struct ether_header *eh) {
29352937
size_t size = sizeof(struct ether_header);
29362938
packet -> ptr = (unsigned char *)malloc(size);
29372939
packet -> eh = (struct ether_header *)packet -> ptr;
29382940
memcpy(packet -> eh, eh, sizeof(struct ether_header));
29392941
packet -> size = sizeof(struct ether_header);
29402942
}
29412943

2942-
void AddIPHeader(Packet *packet, struct iphdr *ip) {
2943-
size_t size = sizeof(struct iphdr);
2944-
packet -> ptr = (unsigned char *)realloc(packet -> ptr, packet -> size + size);
2945-
packet -> ip = (struct iphdr *)(packet -> ptr + packet -> size);
2946-
memcpy(packet -> ip, ip, sizeof(struct iphdr));
2947-
packet -> size = size + packet -> size;
2948-
}
2949-
2950-
void AddTCPHeader(Packet *packet, struct tcphdr *tcp) {
2951-
size_t size = sizeof(struct tcphdr);
2952-
packet -> ptr = (unsigned char *)realloc(packet -> ptr, packet -> size + size);
2953-
packet -> tcp = (struct tcphdr *)(packet -> ptr + packet -> size);
2954-
memcpy(packet -> tcp, tcp, sizeof(struct tcphdr));
2955-
packet -> size = size + packet -> size;
2956-
}
2957-
2958-
void AddData(Packet *packet, unsigned char *data, size_t size) {
2944+
void CarryData(Packet *packet, unsigned char *data, size_t size) {
29592945
packet -> ptr = (unsigned char *)realloc(packet -> ptr, packet -> size + size);
29602946
packet -> data = (unsigned char *)(packet -> ptr + packet -> size);
29612947
packet -> data_size = size;
29622948
memcpy(packet -> data, data, size);
29632949
packet -> size = size + packet -> size;
29642950
}
2951+
```
29652952

2953+
lib/generate.h
2954+
2955+
```c
2956+
#define CarryProtocolHeader(headerType, packet, header) \
2957+
{ \
2958+
size_t size = sizeof(struct headerType); \
2959+
packet.ptr = (unsigned char *)realloc(packet.ptr, packet.size + size);\
2960+
packet.header = (struct headerType *)(packet.ptr + packet.size); \
2961+
memcpy(packet.header, &header, sizeof(struct headerType)); \
2962+
packet.size = size + packet.size; \
2963+
} \
29662964
```
29672965

2966+
29682967
>>>
29692968

29702969
### メモリ開放
@@ -3045,10 +3044,10 @@
30453044

30463045

30473046
// 実体の生成
3048-
GenerateEthernetPacket(&packet, &eh);
3049-
AddIPHeader(&packet, &ip);
3050-
AddTCPHeader(&packet, &tcp);
3051-
AddData(&packet, data, sizeof(data));
3047+
InitBaseEthernetPacket(&packet, &eh);
3048+
CarryProtocolHeader(iphdr, packet, ip);
3049+
CarryProtocolHeader(tcphdr, packet, tcp);
3050+
CarryData(&packet, data, sizeof(data));
30523051

30533052
// チェックサム
30543053
int len = ntohs(packet.ip -> tot_len) - packet.ip -> ihl * 4;

0 commit comments

Comments
 (0)