|
687 | 687 | sprintf(eh.ether_shost, "\x00\x00\x00\x00\x00\x00"); |
688 | 688 | eh.ether_type = (u_int16_t)8; |
689 | 689 |
|
690 | | - GenerateEthernetPacket(&packet, &eh); |
| 690 | + InitBaseEthernetPacket(&packet, &eh); |
691 | 691 |
|
692 | 692 | PrintEthernet(&packet); |
693 | 693 | PrintRawPacket(&packet); |
|
716 | 716 | lib/generate.c |
717 | 717 |
|
718 | 718 | ```c |
719 | | -void GenerateEthernetPacket(Packet *packet, struct ether_header *eh) { |
| 719 | +void InitBaseEthernetPacket(Packet *packet, struct ether_header *eh) { |
720 | 720 | size_t size = sizeof(struct ether_header); |
721 | 721 | packet -> ptr = (unsigned char *)malloc(size); |
722 | 722 | packet -> eh = (struct ether_header *)packet -> ptr; |
|
1262 | 1262 | ip.check = checksum2((unsigned char *)&ip, sizeof(struct iphdr), NULL, 0); |
1263 | 1263 |
|
1264 | 1264 | // パケットの実態の生成 |
1265 | | - GenerateEthernetPacket(&packet, &eh); |
1266 | | - AddIPHeader(&packet, &ip); |
| 1265 | + InitBaseEthernetPacket(&packet, &eh); |
| 1266 | + CarryProtocolHeader(iphdr, packet, ip); |
1267 | 1267 |
|
1268 | 1268 | // パケットの表示 |
1269 | 1269 | PrintEthernet(&packet); |
|
1294 | 1294 | lib/generate.c |
1295 | 1295 |
|
1296 | 1296 | ```c |
1297 | | -void GenerateEthernetPacket(Packet *packet, struct ether_header *eh) { |
| 1297 | +void InitBaseEthernetPacket(Packet *packet, struct ether_header *eh) { |
1298 | 1298 | size_t size = sizeof(struct ether_header); |
1299 | 1299 | packet -> ptr = (unsigned char *)malloc(size); |
1300 | 1300 | packet -> eh = (struct ether_header *)packet -> ptr; |
1301 | 1301 | memcpy(packet -> eh, eh, sizeof(struct ether_header)); |
1302 | 1302 | packet -> size = sizeof(struct ether_header); |
1303 | 1303 | } |
| 1304 | +``` |
1304 | 1305 |
|
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 | +} \ |
1312 | 1317 | ``` |
1313 | 1318 |
|
1314 | 1319 | >>> |
|
1360 | 1365 | ip.check = checksum2((unsigned char *)&ip, sizeof(struct iphdr), NULL, 0); |
1361 | 1366 |
|
1362 | 1367 | // パケットの実態の生成 |
1363 | | -GenerateEthernetPacket(&packet, &eh); |
1364 | | -AddIPHeader(&packet, &ip); |
| 1368 | +InitBaseEthernetPacket(&packet, &eh); |
| 1369 | +CarryProtocolHeader(iphdr, packet, ip); |
1365 | 1370 |
|
1366 | 1371 | ``` |
1367 | 1372 |
|
|
2013 | 2018 | }; |
2014 | 2019 |
|
2015 | 2020 | // 実体の生成 |
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)); |
2020 | 2025 |
|
2021 | 2026 | // 表示 |
2022 | 2027 | printf("text:\n"); |
|
2053 | 2058 | lib/generate.c |
2054 | 2059 |
|
2055 | 2060 | ```c |
2056 | | -void GenerateEthernetPacket(Packet *packet, struct ether_header *eh) { |
| 2061 | +void InitBaseEthernetPacket(Packet *packet, struct ether_header *eh) { |
2057 | 2062 | size_t size = sizeof(struct ether_header); |
2058 | 2063 | packet -> ptr = (unsigned char *)malloc(size); |
2059 | 2064 | packet -> eh = (struct ether_header *)packet -> ptr; |
2060 | 2065 | memcpy(packet -> eh, eh, sizeof(struct ether_header)); |
2061 | 2066 | packet -> size = sizeof(struct ether_header); |
2062 | 2067 | } |
| 2068 | +``` |
2063 | 2069 |
|
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 |
2071 | 2071 |
|
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 | +} \ |
2079 | 2081 | ``` |
2080 | 2082 |
|
2081 | 2083 | >>> |
|
2887 | 2889 |
|
2888 | 2890 |
|
2889 | 2891 | // 実体の生成 |
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)); |
2894 | 2896 |
|
2895 | 2897 | // チェックサム |
2896 | 2898 | int len = ntohs(packet.ip -> tot_len) - packet.ip -> ihl * 4; |
|
2931 | 2933 | lib/generate.c |
2932 | 2934 |
|
2933 | 2935 | ```c |
2934 | | -void GenerateEthernetPacket(Packet *packet, struct ether_header *eh) { |
| 2936 | +void InitBaseEthernetPacket(Packet *packet, struct ether_header *eh) { |
2935 | 2937 | size_t size = sizeof(struct ether_header); |
2936 | 2938 | packet -> ptr = (unsigned char *)malloc(size); |
2937 | 2939 | packet -> eh = (struct ether_header *)packet -> ptr; |
2938 | 2940 | memcpy(packet -> eh, eh, sizeof(struct ether_header)); |
2939 | 2941 | packet -> size = sizeof(struct ether_header); |
2940 | 2942 | } |
2941 | 2943 |
|
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) { |
2959 | 2945 | packet -> ptr = (unsigned char *)realloc(packet -> ptr, packet -> size + size); |
2960 | 2946 | packet -> data = (unsigned char *)(packet -> ptr + packet -> size); |
2961 | 2947 | packet -> data_size = size; |
2962 | 2948 | memcpy(packet -> data, data, size); |
2963 | 2949 | packet -> size = size + packet -> size; |
2964 | 2950 | } |
| 2951 | +``` |
2965 | 2952 |
|
| 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 | +} \ |
2966 | 2964 | ``` |
2967 | 2965 |
|
| 2966 | + |
2968 | 2967 | >>> |
2969 | 2968 |
|
2970 | 2969 | ### メモリ開放 |
|
3045 | 3044 |
|
3046 | 3045 |
|
3047 | 3046 | // 実体の生成 |
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)); |
3052 | 3051 |
|
3053 | 3052 | // チェックサム |
3054 | 3053 | int len = ntohs(packet.ip -> tot_len) - packet.ip -> ihl * 4; |
|
0 commit comments