-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtocol.h
704 lines (529 loc) · 17.6 KB
/
Protocol.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
* The MIT License(MIT)
* Copyright(c) 2016 Lorenzo Delana, https://searchathing.com
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef _SEARCHATHING_ARDUINO_NET_PROTOCOL_H
#define _SEARCHATHING_ARDUINO_NET_PROTOCOL_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif
#include <SearchAThing.Arduino.Utils\DebugMacros.h>
#include <SearchAThing.Arduino.Utils\BufferInfo.h>
namespace SearchAThing
{
namespace Arduino
{
namespace Net
{
class EthNet;
class EthDriver;
//----------------------------------------------------------
// Utils
//----------------------------------------------------------
// Timeout to receive a respond after a request issued before to
// try a new request again.
// See also: DynamicTimeout
const uint16_t REQUEST_TIMEOUT_MS = 500;
// After each timeout reach the timeout value increase of the
// initial timeout value until max value, then it will reset
// to the initial timeout value.
// See also: DynamicTimeout
const uint32_t REQUEST_TIMEOUT_MAX = 1L * 1000;
// Creates a private MAC address 00:00:6C:00:00:xx using xx=lsb.
// See: https://en.wikipedia.org/wiki/MAC_address
RamData PrivateMACAddress(byte lsb);
const byte IPV4_TTL = 128;
const byte IPV4_MACSIZE = 6;
const byte IPV4_IPSIZE = 4;
//----------------------------------------------------------
// Eth2
//----------------------------------------------------------
// EthernetII header.
// https://en.wikipedia.org/wiki/Ethernet_frame
typedef struct Eth2Header
{
// Destination MAC address.
byte dstMAC[IPV4_MACSIZE];
// Source MAC address.
byte srcMAC[IPV4_MACSIZE];
// EtherType. Use Eth2Type enum to address the type.
// http://www.cavebear.com/archive/cavebear/Ethernet/type.html
byte type[2];
};
// EtherType enum.
typedef enum
{
Eth2Type_UNK = -1,
// ARP EtherType.
Eth2Type_ARP = 0x0806,
// IP EtherType.
Eth2Type_IP = 0x0800
} Eth2Type;
// Retrieve EtherType from the given EthII header.
Eth2Type Eth2GetType(Eth2Header *eth2);
// Retrieve EthII header from the given packet buffer.
Eth2Header *Eth2GetHeader(byte *pkt);
// Prepare the current packet Eth2 header filling with
// SourceMAC = network card MAC
// EtherType = given `type'
void Eth2Fill(const EthNet *net, Eth2Header *eth2, Eth2Type type);
// Prints Eth2 Header fields.
void Eth2Print(Eth2Header *eth2);
//----------------------------------------------------------
// ARP
//----------------------------------------------------------
// ARP Header.
// http://www.networksorcery.com/enp/protocol/arp.htm
typedef struct ARPHeader
{
// Hardware type. See: ARPType.
byte hwType[2];
// Protocol type. See: ARPProtocolType.
byte protoType[2];
// Hardware address length. Usually MAC 6 bytes.
byte hwAddrLength;
// Protocol address length. For IPv4 this value is 4.
byte protoAddrLength;
// ARP OpCode. See: ARPOpcodeType.
byte opCode[2];
};
// ARP Type enu.
typedef enum
{
// ARP Type : Ethernet
ARPType_Ethernet = 1
} ARPType;
// ARP Protocol type enum.
typedef enum
{
// ARP Protocol type : IP
ARPProtocolType_IP = 0x800
} ARPProtocolType;
// ARP Opcode type enum.
typedef enum
{
// ARP Opcode : Request
ARPOpCodeType_Request = 1,
// ARP Opcode : Reply
ARPOpCodeType_Reply = 2
} ARPOpcodeType;
// Retrieve the ARP Header from the given Eth2 header.
ARPHeader *ARPGetHeader(Eth2Header *eth2);
// Set the ARP opcode.
void ARPSetOpCodeType(ARPHeader *arp, ARPOpcodeType type);
// Retrieve the ARP data packet pointer to the source MAC.
byte *ARPSourceHardwareAddress(ARPHeader *arp);
// Retrieve the ARP data packet pointer to the source IP.
byte *ARPSourceProtocolAddress(ARPHeader *arp);
// Retrieve the ARP data packet pointer to the destination MAC.
byte *ARPDestinationHardwareAddress(ARPHeader *arp);
// Retrieve the ARP data packet pointer to the destination IP.
byte *ARPDestinationProtocolAddress(ARPHeader *arp);
// Retrieve the ARP Size ( Header + Data ).
uint16_t ARPSize(ARPHeader *arp);
// Fills out the ARP packet with the given opCode and following
// settings:
// - Hardware type : Ethernet
// - Protocol type : IP
// - Hardware length : 6 ( == IPV4_MACSIZE )
// - Protocol length : 4 ( == IPV4_IPSIZE )
// - Source MAC : ethernet card MAC
// - Source IP : ethernet subsystem IP
void ARPFill(const EthNet *net, ARPHeader *arp, ARPOpcodeType opCode);
// Prints ARP Header fields.
void ARPPrint(ARPHeader *arp);
//----------------------------------------------------------
// IPv4
//----------------------------------------------------------
// Internet Protocol Header.
// http://www.networksorcery.com/enp/protocol/ip.htm
typedef struct IPv4Header
{
// Internet Header Length.
// Its express a length with a number which units is 4bytes
// (eg. ihl=5 -> 5 x 4 = 20 bytes )
// ( 4 bit lower )
byte ihl : 4;
// IP protocol version.
// ( 4 bit higher )
byte version : 4;
// Services.
byte services;
// Total length of the packet including encapsulated
// protocols and data. Excluded previous Eth2 header.
byte totalLength[2];
// Used for sequence identification.
byte identification[2];
// Flags.
// ( 3 bit higher )
byte flags : 3;
// Fragment Offset
// ( 5 bits higher )
byte fragmentOffsetH : 5;
// Fragment Offset (Low)
// ( 8 bits lower )
byte fragmentOffsetL : 8;
// Time to live.
byte ttl;
// Protocol type. See: IPv4Type.
byte protocol;
// IPv4 Checksum of the entire IPv4Header only without
// subsequent encapsulated protocols. Note: the checksum
// will be computed with the header values except the
// dummy checksum zeroed.
// See: IPv4WriteValidChecksum()
byte chksum[2];
// Source Ip address.
byte srcip[4];
// Destination Ip address.
byte dstip[4];
};
// IPv4Type enum.
typedef enum
{
// IPv4 Protocol type ICMP
IPv4Type_ICMP = 1,
// IPv4 Protocol type UDP
IPv4Type_UDP = 17
} IPv4Type;
// Retrieve IPv4Header from the Eth2.
IPv4Header *IPv4GetHeader(Eth2Header *hdr);
// Fills out the IPv4 packet with the given `ipv4Len' total len,
// `type' ip protocol type and follow settings:
// - Internet header length : 5 ( that is 5x4 = 20 bytes )
// - Internet version : 4 ( ip v.4 )
// - Services : 0
// - Flags : 0
// - Fragmented Offset : 0
// - Ttl : 128 ( = IPV4_TTL )
// - Source ip : ethernet card ip
void IPv4Fill(const EthNet *net, IPv4Header *ipv4, uint16_t ipv4Len, IPv4Type type);
// Compute the IPv4Header checksum and store the value into the
// chksum field. See also: IPv4Header::chksum
void IPv4WriteValidChecksum(IPv4Header *ipv4);
// Prints IPv4 Header fields.
void IPv4Print(IPv4Header *ipv4);
//----------------------------------------------------------
// ICMP
//----------------------------------------------------------
// Internet Control Message Protocol Header
// http://www.networksorcery.com/enp/protocol/icmp.htm
typedef struct ICMPHeader
{
// ICMP type. See: ICMPType.
byte type;
// ICMP code.
byte code;
// ICMP checksum. Its the checksum of the ICMP packet with
// dummy checksum of 0 excluded Eth2 and IPv4 headers.
byte chksum[2];
};
// ICMP Echo Header
// http://www.networksorcery.com/enp/protocol/icmp/msg8.htm
struct ICMPEchoHeader
{
// ICMP Header
ICMPHeader icmp;
// Identifier
byte identifier[2];
// Sequence number
byte seqnr[2];
};
// ICMP Type enum.
typedef enum
{
// ICMP type Echo Reply.
ICMPType_EchoReply = 0,
// ICMP type Echo Request.
ICMPType_EchoRequest = 8
} ICMPType;
// Retrieve the ICMP Header from the given `ipv4' header.
ICMPHeader *ICMPGetHeader(IPv4Header *ipv4);
// Compute the ICMP checksum and store the value into the
// checksum field. See also: ICMPHeader::chksum
void ICMPWriteValidChecksum(IPv4Header *ipv4, ICMPHeader *icmp);
// Prints ICMP Header fields.
void ICMPPrint(ICMPHeader *icmp);
//----------------------------------------------------------
// UDP
//----------------------------------------------------------
// Standard UDP server port. DHCP server/client system filters
// udp packets based on these port values.
const byte UDP_PORT_BOOTP_SERVER = 67;
// Standard UDP client port. DHCP server/client system filters
// udp packets based on these port values.
const byte UDP_PORT_BOOTP_CLIENT = 68;
// UDP Header.
// http://www.networksorcery.com/enp/protocol/udp.htm
typedef struct UDPHeader
{
// Source port.
byte sourcePort[2];
// Destination port.
byte destPort[2];
// Data length.
byte length[2];
// Checksum computed over a pseudo header with dummy chksum=0
// {srcip, dstip, protocol, udpLen} and UDP header + data.
// http://www.tcpipguide.com/free/t_UDPMessageFormat-2.htm
byte chksum[2];
};
// Retrieve the UDP header.
UDPHeader *UDPGetHeader(IPv4Header *ipv4);
// Retrieve the UDP pointer to the data.
byte *UDPGetData(UDPHeader *udp);
// Write a valid UDP checksum.
void UDPWriteValidChecksum(IPv4Header *ipv4, UDPHeader *udp);
// Fills out the UDP packet with the given `srcPort', `dstPort'
// and udp packet+data `len'
void UDPFill(UDPHeader *udp, uint16_t srcPort, uint16_t dstPort, uint16_t len);
// Prints UDP Header fields.
void UDPPrint(UDPHeader *udp);
//----------------------------------------------------------
// DHCP
//----------------------------------------------------------
// Bytes count of the DHCP magic cookie.
const byte DHCPMagicCookieSIZE = 4;
// DHCP Header
// http://www.networksorcery.com/enp/protocol/dhcp.htm - size:240
typedef struct DHCPHeader
{
// DHCP opcode. See: DHCPOpCode.
byte opCode;
// DHCP hardware type. See: DHCPHwType.
byte hwType;
// Hardware addres length.
byte hwLength;
// Hop count.
byte hopCount;
// Transaction Id.
byte transactionId[4];
// Seconds from last renewal.
byte nrSeconds[2];
// Flags
byte flags[2];
// Client Ip address.
byte clientIpAddress[4];
// Ip assigned by the server.
byte yourIp[4];
// Ip of the server during a request. (0 for discover).
byte serverIp[4];
// Gateway (0 for discover).
byte gatewayIp[4];
// Client MAC
byte clientHwAddress[16];
// Server hostname ( 0 )
byte serverHostname[64];
// Bootfilename ( 0 )
byte bootFilename[128];
// 0x63825363 ( magic cookie bootp )
byte magic[4];
// DATA
//------
// Data of the DHCP protocol consists of one or more
// option ( see DHCPOption ) ending with the
// DHCPOption::DHCPOptionEnd ( 0xff ).
//
// Typical data composition:
//
// DISCOVER
// 0x35 0x01 0x01 (discover)
// 0xff (end)
//
// OFFER
// 0x35 0x01 0x02 (offer)
// 0xff (end)
//
// REQUEST
// 0x35 0x01 0x03 (request)
// 0x32 0x04 a b c d (requested ip address a.b.c.d)
// 0x36 0x04 a b c d (server identifier a.b.c.d)
// 0xff (end)
//
// ACK
// 0x35 0x01 0x05 (ack)
// 0x33 leaseH leaseL (lease time)
// 0x0c len x x x ... (hostname)
// 0x0f len x x x ... (domainname)
// 0x03 0x04 a b c d (gateway)
// 0x1c 0x04 a b c d (broadcast)
// 0x06 0x04 a b c d (dns)
// 0x01 0x04 a b c d (subnet)
// 0xff ( end )
};
// DHCP opcode enum.
typedef enum
{
// DHCP request.
DHCPOpCode_BootRequest = 1,
// DHCP reply.
DHCPOpCode_BootReply = 2
} DHCPOpCode;
// DHCP hardware type.
typedef enum
{
// Ethernet.
DHCPHwType_Ethernet = 1
} DHCPHwType;
// DHCP Option enum.
typedef enum
{
// Subnet.
DHCPOptionSubnetMask = 1,
// Gateway.
DHCPOptionGateway = 3,
// Dns.
DHCPOptionDns = 6,
// Domain name.
DHCPOptionDomainName = 15,
// Broadcast.
DHCPOptionBroadcast = 28,
// Requested ip address.
DHCPOptionRequestedIpAddress = 50,
// Lease time.
DHCPOptionLeaseTime = 51,
// Msg type.
DHCPOptionMsgType = 53,
// Server identifier.
DHCPOptionServerIdentifier = 54,
// Hostname.
DHCPOptionHostname = 12,
// End.
DHCPOptionEnd = 0xff
} DHCPOption;
// DHCP message type.
typedef enum
{
// Discover.
DHCPMsgTypeDiscover = 1,
// Offer.
DHCPMsgTypeOffer = 2,
// Request.
DHCPMsgTypeRequest = 3,
// Ack.
DHCPMsgTypeAck = 5
} DHCPMsgType;
// DHCP Magic Cookie ( 0x63 0x82 0x53 0x63 )
extern byte DHCPMagicCookie[];
// Retrieve the DHCP Header from the given `udp'.
DHCPHeader *DHCPGetHeader(UDPHeader *udp);
// Sets the DHCP magic cookie in the given DHCP header.
void DHCPSetMagicCookie(DHCPHeader *dhcp);
// Retrieve the options pointer from the given `dhcp' header.
byte *DHCPGetOptions(DHCPHeader *dhcp);
// Checks if the given `dhcp' packet contains the given
// `optionInfo' between their available options.
bool DHCPMatchesOption(IPv4Header *ipv4, UDPHeader *udp, DHCPHeader *dhcp, BufferInfo optionInfo);
// Locate the buf pointer of the given `dhcp' packet where the
// given `option' starts. Returns NULL if no such option found.
byte *DHCPLocateOption(IPv4Header *ipv4, UDPHeader *udp, DHCPHeader *dhcp, byte option);
//----------------------------------------------------------
// DNS
//----------------------------------------------------------
// UDP destination port to send request to a DNS server.
const byte UDP_PORT_DNS_SERVER = 53;
// DNS class Internet.
const byte DNS_CLASS_IN = 1;
// DNS qr-code Query.
const byte DNS_QR_CODE_QUERY = 0;
// DNS qr-code Response.
const byte DNS_QR_CODE_RESPONSE = 1;
// DNS query type A. ( Address )
const byte DNS_QUERY_TYPE_A = 1;
// DNS query type MX. (Mail Exchanger)
const byte DNS_QUERY_TYPE_MX = 15;
// DNS Header.
// http://www.networksorcery.com/enp/protocol/dns.htm and
// http://www.networksorcery.com/enp/rfc/rfc1035.txt
typedef struct DNSHeader
{
// Transaction Id.
byte id[2];
// request recursive
byte rd : 1;
byte tc : 1;
byte aa : 1;
byte opCode : 4;
// qr-code ( DNS_QR_CODE_{ QUERY, RESPONSE } )
byte qr : 1;
byte rCode : 4;
byte cd : 1;
byte ad : 1;
byte z : 1;
byte ra : 1;
// Number of questions. We'll issue 1 question TypeA ClassIN
byte totalQuestions[2];
// Number of answers. We'll search for first answer
// TypeA ClassIN
byte totalAnswer[2];
// Number of authorities.
byte totalAuthority[2];
// Number of additions.
byte totalAdditions[2];
};
// Retrieve the DNS Header from the given `udp'.
DNSHeader *DNSGetHeader(UDPHeader *udp);
//----------------------------------------------------------
// SRUDP
// ref. doc https://searchathing.com/?p=59
//----------------------------------------------------------
// SRUDP Header.
typedef struct SRUDPHeader
{
// Connect request ( octet lower bit ).
byte connect : 1;
// Notify an Ack ( received message `id' ).
byte ack : 1;
// Message contains Data.
byte data : 1;
// Request Disconnect.
byte disconnect : 1;
// (octet padding)
byte pad : 4;
// Sequence identifier. Each ends starts its own transmitted
// packet with sequence from 0 and increment at each
// new transmitted packet after it received correspondent
// acknowledge. At the same time each ends manage a separate
// rx Id that starts from 0 to state right sequence of
// incoming packets from the other ends.
byte id[2];
// This packet data len.
byte dataLen[2];
// Data len left that expects over next packets.
byte dataLenLeft[2];
};
// Retrieve the SRUDP header from the given `udp'.
SRUDPHeader *SRUDPGetHeader(UDPHeader *udp);
// Set the opcode ( Connect, Ack, Data, Cont, Disconnect, Pad )
// to zero.
void SRUDPResetOpCode(SRUDPHeader *srudp);
// Retrieve the SRUDP data pointer from the given `srudp' packet.
byte *SRUDPGetData(SRUDPHeader *srudp);
// Retrieve the SRUDP data length from the given `srudp' packet.
uint16_t SRUDPPacketDataLength(IPv4Header *ipv4, SRUDPHeader *srudp);
// Prints SRUDP fields from the given packet.
void SRUDPPrint(BufferInfo& pkt);
}
}
}
#endif