-
Notifications
You must be signed in to change notification settings - Fork 0
/
construct_dhcp_packet.h
99 lines (85 loc) · 3.3 KB
/
construct_dhcp_packet.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
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
#ifndef CONSTRUCT_DHCP_PACKET_H
#define CONSTRUCT_DHCP_PACKET_H
#include <linux/ip.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <linux/udp.h>
#include <net/ethernet.h> /* the L2 protocols */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define PACKETMAXSIZE 1024
#define IP_LAYER 3
#define TRANSPORT_LAYER 4
#define APP_LAYER 5
#define LINK_LAYER 2
#define DHCP_DISCOVER 1
#define DHCP_OFFER 2
#define DHCP_REQUEST 3
#define DHCP_ACK 5
#define DHCP_RELEASE 7
struct dhcp_header { /* BOOTP packet format */
unsigned char op; /* 1=request, 2=reply */
unsigned char htype; /* HW address type */
unsigned char hlen; /* HW address length */
unsigned char hops; /* Used only by gateways */
unsigned int xid; /* Transaction ID */
unsigned short secs; /* Seconds since we started */
unsigned short broadcast:1, reserved:15; /* Just what it says */
unsigned int client_ip; /* Client's IP address if known */
unsigned int your_ip; /* Assigned IP address */
unsigned int server_ip; /* (Next, e.g. NFS) Server's IP address */
unsigned int relay_ip; /* IP address of BOOTP relay */
unsigned char hw_addr[16]; /* Client's HW address */
unsigned char serv_name[64]; /* Server host name */
unsigned char boot_file[128]; /* Name of boot file */
unsigned char exten[312]; /* DHCP options / BOOTP vendor extensions */
};
struct ip_h {
unsigned char type;
unsigned char type_of_service;
unsigned char total_len[2];
unsigned char id[2];
unsigned char flags[2];
unsigned char ttl;
unsigned char proto;
unsigned char checksum[2];
unsigned char srcIP[4];
unsigned char desIP[4];
};
struct l2_h {
unsigned char desMAC[6];
unsigned char srcMAC[6];
unsigned char type[2];
};
struct udp_h {
unsigned char srcPort[2];
unsigned char desPort[2];
unsigned char len[2];
unsigned char checksum[2];
};
typedef struct packet {
struct l2_h l2;
struct ip_h ip;
struct udp_h udp;
unsigned char data[PACKETMAXSIZE-sizeof(struct ethhdr)-sizeof(struct iphdr)-sizeof(struct udphdr)];
} Packet;
//pseudo hdr for computing udp checksum
struct pseudo_udp_hdr{
unsigned int source;
unsigned int dest;
unsigned char pad;
unsigned char proto;
unsigned short length;
struct udphdr udp_hdr;
unsigned char data[PACKETMAXSIZE-sizeof(struct ethhdr)-sizeof(struct iphdr)-sizeof(struct udphdr)];
};
//used to construct from layer 2 to udp layer.
int construct_dhcp_packet(unsigned int xid,unsigned int type,unsigned char hwmac[6],unsigned char dstmac[6],unsigned int *hip,unsigned int *dip,unsigned char *packet, unsigned char* fixed_mac,unsigned int*server_ip,unsigned int*req_ip);
static unsigned short checksum(void* addr,int count);
static void copy_macaddr(unsigned char *sll_addr,unsigned char first,unsigned char second,unsigned char third,unsigned char fourth,unsigned char fifth,unsigned char sixth);
//construct only dhcp payload
static int construct_dhcp_payload(unsigned int xid, unsigned int type,struct pseudo_udp_hdr* p_udp,unsigned char *hwmac,unsigned char *dstmac,unsigned int*hip,unsigned int*dip,unsigned int *server_ip,unsigned int *req_ip);
//used to add dhcp extension
static unsigned char*dhcp_add_exten(unsigned char*cursor,unsigned char code,unsigned char length,unsigned char* value);
#endif