forked from TaraHoleInIt/SLIP8266
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipv4.cpp
More file actions
242 lines (196 loc) · 7.45 KB
/
ipv4.cpp
File metadata and controls
242 lines (196 loc) · 7.45 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <ESP8266WiFi.h>
#include <lwip/netif.h>
#include <lwip/err.h>
#include <stdarg.h>
#include "ether.h"
#include "ipv4.h"
#include "util.h"
#include "slip.h"
#include "mydebug.h"
extern "C" {
#include <netif/wlan_lwip_if.h>
#include <lwip/inet_chksum.h>
#include <user_interface.h>
}
int UDP_BuildOutgoingPacket( uint32_t SourceIP, uint32_t TargetIP, uint16_t Port, const uint8_t* Data, int DataLength ) {
uint8_t TempBuffer[ 2048 ];
uint8_t DestinationMACAddress[ MACAddressLen ];
uint8_t* BufferPtr = TempBuffer;
int BytesToWrite = 0;
if ( Route( TargetIP, DestinationMACAddress ) ) {
BytesToWrite+= PrepareEthernetHeader( ( struct EtherFrame* ) TempBuffer, OurMACAddress, DestinationMACAddress, EtherType_IPv4 );
BytesToWrite+= PrepareTCPHeader( ( struct ip_packet* ) ( TempBuffer + BytesToWrite ), SourceIP, TargetIP, DataLength, 0, IP_PROTO_UDP );
BytesToWrite+= PrepareUDPHeader( ( struct udp_packet* ) ( TempBuffer + BytesToWrite ), Port, DataLength );
memcpy( ( TempBuffer + BytesToWrite ), Data, DataLength );
BytesToWrite+= DataLength;
EtherWrite( TempBuffer, BytesToWrite );
return 1;
} else {
DebugPrintf( "UDP_BuildOutgoingPacket: No route to host.\n" );
}
return 0;
}
int PrepareTCPHeader( struct ip_packet* IPHeader, const uint32_t SourceIP, const uint32_t DestIP, int DataLength, int DontFragment, int Protocol ) {
IPHeader->HeaderLengthInWords = 5;
IPHeader->Version = 4;
IPHeader->TypeOfService = 0;
IPHeader->Length = htons( DataLength + sizeof( struct ip_packet ) + sizeof( struct udp_packet ) );
IPHeader->Identification = millis( ) & 0xFFFF;
IPHeader->Fragment = htons( DontFragment ? 2 : 0 );
IPHeader->TimeToLive = 64;
IPHeader->Protocol = Protocol;
IPHeader->HeaderChecksum = 0;
IPHeader->SourceIP = SourceIP;
IPHeader->DestIP = DestIP;
IPHeader->HeaderChecksum = inet_chksum( ( void* ) IPHeader, sizeof( struct ip_packet ) );
return sizeof( struct ip_packet );
}
int PrepareUDPHeader( struct udp_packet* UDPHeader, uint16_t Port, int DataLength ) {
UDPHeader->SourcePort = htons( Port - 11 );
UDPHeader->DestPort = htons( Port );
UDPHeader->Length = htons( sizeof( struct udp_packet ) + DataLength );
UDPHeader->Checksum = 0;
return sizeof( struct udp_packet );
}
int TCP_EtherEncapsulate( const uint8_t* Packet, int Length ) {
const struct ip_packet* IPHeader = ( const struct ip_packet* ) Packet;
uint8_t Buffer[ 2048 ];
struct EtherFrame* FrameHeader = ( struct EtherFrame* ) Buffer;
int Local = 0;
Local = AreWeOnTheSameSubnet( IPHeader->DestIP );
if ( ARP_RequestMACFromIP_Blocking( Local ? IPHeader->DestIP : ( uint32_t ) OurGateway, FrameHeader->DestMAC ) ) {
if ( ( Length + sizeof( struct EtherFrame ) ) > sizeof( Buffer ) ) {
DebugPrintf( "FATAL: EtherWrite packet overrun!\n" );
Length = 0;
}
memcpy( &Buffer[ sizeof( struct EtherFrame ) ], Packet, Length );
memcpy( FrameHeader->SourceMAC, OurMACAddress, MACAddressLen );
FrameHeader->LengthOrType = htons( EtherType_IPv4 );
Length+= sizeof( struct EtherFrame );
EtherWrite( Buffer, Length );
return 1;
} else {
DebugPrintf( "Timeout or didn't get target MAC\n" );
}
return 0;
}
/*
* Returns 1 if the given IP address is a broadcast address.
* NOTE:
* This expects the IP and Mask to be in HOST BYTE ORDER.
*/
int IsBroadcastIP( uint32_t IP, uint32_t Mask ) {
return ( IP & ~Mask ) == 255 ? 1 : 0;
}
int Route( uint32_t IPAddr, uint8_t* MACAddress ) {
int IsLocalAddress = 0;
int Result = 0;
if ( IsBroadcastIP( ntohl( IPAddr ), ntohl( OurNetmask ) ) ) {
memset( MACAddress, 0xFF, MACAddressLen );
Result = 1;
}
else {
IsLocalAddress = AreWeOnTheSameSubnet( IPAddr );
Result = ARP_RequestMACFromIP_Blocking( IsLocalAddress ? IPAddr : ( uint32_t ) OurGateway, MACAddress );
}
return Result;
}
#define DHCP_PORT 67
#define DHCP_Opcode_Request 1
#define DHCP_Opcode_Reply 2
enum {
DHCP_Field_Pad = 0,
DHCP_Field_Hostname = 12,
DHCP_Field_Request = 53,
DHCP_Field_ClientIdentifier = 61,
DHCP_Field_End = 255
};
enum {
DHCPDISCOVER = 1,
DHCPOFFER,
DHCPREQUEST,
DHCPDECLINE,
DHCPACK,
DHCPNACK,
DHCPRELEASE,
DHCPINFORM
};
struct dhcp_option {
uint8_t Opcode;
uint8_t Length;
uint8_t Data[ 1 ];
};
struct dhcp_packet {
uint8_t Opcode;
uint8_t HardwareType;
uint8_t HardwareAddressLen;
uint8_t Hops;
uint32_t TransactionID;
uint16_t Seconds;
uint16_t Flags;
uint32_t ClientIPAddress;
uint32_t YourIPAddress;
uint32_t ServerIPAddress;
uint32_t GatewayIPAddress;
uint8_t ClientHWAddress[ 16 ];
signed char ServerName[ 64 ];
signed char BootFileName[ 128 ];
uint8_t Options[ 64 ];
} __attribute__( ( packed ) );
void DHCPRequest( void ) {
static uint8_t Buffer[ 512 ];
struct dhcp_packet* Request = ( struct dhcp_packet* ) Buffer;
struct dhcp_option* Option = NULL;
int BytesToWrite = 0;
memset( Buffer, 0, sizeof( Buffer ) );
Request->Opcode = DHCP_Opcode_Request;
Request->HardwareType = 1;
Request->HardwareAddressLen = MACAddressLen;
Request->Hops = 0;
Request->TransactionID = millis( );
Request->Seconds = 0;
Request->Flags = htons( 0 );
Request->ClientIPAddress = 0;
Request->YourIPAddress = 0;
Request->ServerIPAddress = 0;
Request->GatewayIPAddress = 0;
memcpy( Request->ClientHWAddress, OurMACAddress, MACAddressLen );
memset( Request->ServerName, 0, sizeof( Request->ServerName ) );
memset( Request->BootFileName, 0, sizeof( Request->BootFileName ) );
BytesToWrite = sizeof( struct dhcp_packet ) - 64;
Buffer[ BytesToWrite++ ] = 99;
Buffer[ BytesToWrite++ ] = 130;
Buffer[ BytesToWrite++ ] = 83;
Buffer[ BytesToWrite++ ] = 99;
Option = ( struct dhcp_option* ) &Buffer[ BytesToWrite ];
Option->Opcode = DHCP_Field_Request;
Option->Length = 1;
Option->Data[ 0 ] = DHCPDISCOVER;
BytesToWrite+= sizeof( struct dhcp_option ) + Option->Length - sizeof( Option->Data );
Option = ( struct dhcp_option* ) &Buffer[ BytesToWrite ];
Option->Opcode = DHCP_Field_Hostname;
Option->Length = 9;
memcpy( Option->Data, "Buttslol\0", 9 );
BytesToWrite+= sizeof( struct dhcp_option ) + Option->Length - sizeof( Option->Data );
Buffer[ BytesToWrite++ ] = DHCP_Field_End;
UDP_BuildOutgoingPacket( IPAddress( 0, 0, 0, 0 ), IPAddress( 255, 255, 255, 255 ), DHCP_PORT, Buffer, BytesToWrite );
}
void OnIPv4Packet( const uint8_t* Data, int Length, const struct EtherFrame* FrameHeader ) {
struct ip_packet* IPHeader = ( struct ip_packet* ) Data;
struct udp_packet* UDPHeader = NULL;
char SourceMACStr[ 32 ];
char DestMACStr[ 32 ];
char SourceIPStr[ 32 ];
char DestIPStr[ 32 ];
MACsprintf( FrameHeader->SourceMAC, SourceMACStr, sizeof( SourceMACStr ) );
MACsprintf( FrameHeader->DestMAC, DestMACStr, sizeof( DestMACStr ) );
IPsprintf( IPHeader->SourceIP, SourceIPStr, sizeof( SourceIPStr ) );
IPsprintf( IPHeader->DestIP, DestIPStr, sizeof( DestIPStr ) );
if ( IPHeader->Protocol == IP_PROTO_UDP ) {
UDPHeader = ( struct udp_packet* ) &Data[ IPHeader->HeaderLengthInWords * 4 ];
DebugPrintf( "UDP: %d/%d\n", ( int ) ntohs( UDPHeader->SourcePort ), ( int ) ntohs( UDPHeader->DestPort ) );
}
else {
DebugPrintf( "%s: [Src: %s] [Dst: %s] [Len: %d] [Proto:%02X]\n", __FUNCTION__, SourceMACStr, DestMACStr, Length, IPHeader->Protocol );
}
}