This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacketGenerator.h
56 lines (44 loc) · 1.82 KB
/
PacketGenerator.h
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
//
// Created by brendan on 25/10/18.
//
#ifndef LITTLE_SHARK_PACKETGENERATOR_H
#define LITTLE_SHARK_PACKETGENERATOR_H
#include <cstdint>
#include <cstdlib>
struct tcpPseudoHeader {
uint32_t src_ip;
uint32_t dst_ip;
u_char rsv;
u_char proto;
uint16_t tcp_len;
};
class PacketGenerator {
public:
static const unsigned int WITH_IPV4 = 0b00001;
// static const unsigned int WITH_IPV6 = 0b00010;
static const unsigned int WITH_ICMP = 0b00100;
static const unsigned int WITH_UDP = 0b01000;
static const unsigned int WITH_TCP = 0b10000;
ssize_t getCreatedPacketSize() const;
PacketGenerator() = default;
~PacketGenerator();
void setTarget(const char *, const char *, const char *, const char *, uint16_t = 4567, uint16_t = 7654);
unsigned char *createPacket(const unsigned char *buffer, ssize_t packet_len, unsigned int = PacketGenerator::WITH_IPV4 | PacketGenerator::WITH_UDP);
private:
void fillEthernetHeader(struct ethhdr *, const char *, const char *);
void fillIPV4Header(struct iphdr *, const char *, const char *, uint16_t, unsigned char);
unsigned short IPTCPChecksum(unsigned char *, int);
void fillICMPv4Header(struct icmphdr *, ssize_t);
unsigned short icmpChecksum(unsigned short *ptr, int nbytes);
void fillTCPHeader(struct tcphdr *, struct iphdr *, const unsigned char *, ssize_t);
void fillUDPHeader(struct udphdr *, struct iphdr *, ssize_t);
unsigned char *m_pPacket = nullptr;
const char *src_adr_mac = nullptr; // xx:xx:xx:xx:xx:xx
const char *dst_adr_mac = nullptr; // yy:yy:yy:yy:yy:yy
const char *src_adr_ip_v4 = nullptr; // 01.23.45.67
const char *dst_adr_ip_v4 = nullptr; // 76.54.32.10
uint16_t src_port = 4567;
uint16_t dst_port = 7654;
ssize_t createdPacketSize = 0;
};
#endif //LITTLE_SHARK_PACKETGENERATOR_H