This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
ip_arp_udp_tcp.c
1783 lines (1682 loc) · 65.6 KB
/
ip_arp_udp_tcp.c
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************
* vim:sw=8:ts=8:si:et
* To use the above modeline in vim you must have "set modeline" in your .vimrc
*
* Author: Guido Socher
* Copyright: GPL V2
* See http://www.gnu.org/licenses/gpl.html
*
* IP, Arp, UDP and TCP functions.
*
* The TCP implementation uses some size optimisations which are valid
* only if all data can be sent in one single packet. This is however
* not a big limitation for a microcontroller as you will anyhow use
* small web-pages. The web server must send the entire web page in one
* packet. The client "web browser" as implemented here can also receive
* large pages.
*
* Chip type : ATMEGA88/168/328 with ENC28J60
*********************************************/
#include "ip_config.h"
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <string.h>
#include <stdlib.h>
#include "net.h"
#include "enc28j60.h"
#undef ETHERSHIELD_DEBUG
#ifdef ETHERSHIELD_DEBUG
static void serial_write(unsigned char c) {
while (!(UCSR0A & (1 << UDRE0))) {}
UDR0 = c;
}
void ethershieldDebug(char *message) {
uint8_t i;
for (i = 0; message[i] != '\0'; i++) {
serial_write(message[i]);
}
}
#endif
// Web server port, used when implementing webserver
static uint8_t wwwport_l=80; // server port
static uint8_t wwwport_h=0; // Note: never use same as TCPCLIENT_SRC_PORT_H
#if defined (WWW_client) || defined (TCP_client)
// just lower byte, the upper byte is TCPCLIENT_SRC_PORT_H:
static uint8_t tcpclient_src_port_l=1;
static uint8_t tcp_fd=0; // a file descriptor, will be encoded into the port
static uint8_t tcp_client_state=0;
// TCP client Destination port
static uint8_t tcp_client_port_h=0;
static uint8_t tcp_client_port_l=0;
// This function will be called if we ever get a result back from the
// TCP connection to the sever:
// close_connection= your_client_tcp_result_callback(uint8_t fd, uint8_t statuscode,uint16_t data_start_pos_in_buf, uint16_t len_of_data){...your code}
// statuscode=0 means the buffer has valid data
static uint8_t (*client_tcp_result_callback)(uint8_t,uint8_t,uint16_t,uint16_t);
// len_of_data_filled_in=your_client_tcp_datafill_callback(uint8_t fd){...your code}
static uint16_t (*client_tcp_datafill_callback)(uint8_t);
#endif
#define TCPCLIENT_SRC_PORT_H 11
#if defined (WWW_client)
// WWW_client uses TCP_client
#if ! defined(TCP_client)
#define TCP_client 1
#endif
static uint8_t www_fd=0;
static uint8_t browsertype=0; // 0 = get, 1 = post
static void (*client_browser_callback)(uint8_t,uint16_t,uint16_t);
#ifdef FLASH_VARS
static prog_char *client_additionalheaderline;
static prog_char *client_method; // for POST or PUT, no trailing space needed
static prog_char *client_urlbuf;
static prog_char *client_hoststr;
#else
static char *client_additionalheaderline;
static char *client_method; // for POST or PUT, no trailing space needed
static char *client_urlbuf;
static char *client_hoststr;
#endif
static char *client_postval;
static char *client_urlbuf_var;
static uint8_t *bufptr=0; // ugly workaround for backward compatibility
#endif
static void (*icmp_callback)(uint8_t *ip);
// 0=wait, 1=first req no anser, 2=have gwmac, 4=refeshing but have gw mac, 8=accept an arp reply
#define WGW_INITIAL_ARP 1
#define WGW_HAVE_GW_MAC 2
#define WGW_REFRESHING 4
#define WGW_ACCEPT_ARP_REPLY 8
static int16_t delaycnt=1;
static uint8_t gwip[4];
static uint8_t gwmacaddr[6];
static uint8_t tcpsrvip[4];
static volatile uint8_t waitgwmac=WGW_INITIAL_ARP;
uint8_t macaddr[6];
static uint8_t ipaddr[4];
static uint16_t info_data_len=0;
static uint8_t seqnum=0xa; // my initial tcp sequence number
#define CLIENTMSS 550
#define TCP_DATA_START ((uint16_t)TCP_SRC_PORT_H_P+(buf[TCP_HEADER_LEN_P]>>4)*4)
const char arpreqhdr[] PROGMEM ={0,1,8,0,6,4,0,1};
#if defined (NTP_client) || defined (WOL_client) || defined (UDP_client) || defined (TCP_client) || defined (PING_client)
const char iphdr[] PROGMEM ={0x45,0,0,0x82,0,0,0x40,0,0x20}; // 0x82 is the total len on ip, 0x20 is ttl (time to live)
#endif
#ifdef NTP_client
const char ntpreqhdr[] PROGMEM ={0xe3,0,4,0xfa,0,1,0,1,0,0};
#endif
// The Ip checksum is calculated over the ip header only starting
// with the header length field and a total length of 20 bytes
// unitl ip.dst
// You must set the IP checksum field to zero before you start
// the calculation.
// len for ip is 20.
//
// For UDP/TCP we do not make up the required pseudo header. Instead we
// use the ip.src and ip.dst fields of the real packet:
// The udp checksum calculation starts with the ip.src field
// Ip.src=4bytes,Ip.dst=4 bytes,Udp header=8bytes + data length=16+len
// In other words the len here is 8 + length over which you actually
// want to calculate the checksum.
// You must set the checksum field to zero before you start
// the calculation.
// The same algorithm is also used for udp and tcp checksums.
// len for udp is: 8 + 8 + data length
// len for tcp is: 4+4 + 20 + option len + data length
//
// For more information on how this algorithm works see:
// http://www.netfor2.com/checksum.html
// http://www.msc.uky.edu/ken/cs471/notes/chap3.htm
// The RFC has also a C code example: http://www.faqs.org/rfcs/rfc1071.html
uint16_t checksum(uint8_t *buf, uint16_t len,uint8_t type){
// type 0=ip , icmp
// 1=udp
// 2=tcp
uint32_t sum = 0;
//if(type==0){
// // do not add anything, standard IP checksum as described above
// // Usable for ICMP and IP header
//}
if(type==1){
sum+=IP_PROTO_UDP_V; // protocol udp
// the length here is the length of udp (data+header len)
// =length given to this function - (IP.scr+IP.dst length)
sum+=len-8; // = real udp len
}
if(type==2){
sum+=IP_PROTO_TCP_V;
// the length here is the length of tcp (data+header len)
// =length given to this function - (IP.scr+IP.dst length)
sum+=len-8; // = real tcp len
}
// build the sum of 16bit words
while(len >1){
sum += 0xFFFF & (((uint32_t)*buf<<8)|*(buf+1));
buf+=2;
len-=2;
}
// if there is a byte left then add it (padded with zero)
if (len){
sum += ((uint32_t)(0xFF & *buf))<<8;
}
// now calculate the sum over the bytes in the sum
// until the result is only 16bit long
while (sum>>16){
sum = (sum & 0xFFFF)+(sum >> 16);
}
// build 1's complement:
return( (uint16_t) sum ^ 0xFFFF);
}
// This initializes the web server
// you must call this function once before you use any of the other functions:
void init_ip_arp_udp_tcp(uint8_t *mymac,uint8_t *myip,uint16_t port){
uint8_t i=0;
wwwport_h=(port>>8)&0xff;
wwwport_l=(port&0xff);
while(i<4){
ipaddr[i]=myip[i];
i++;
}
i=0;
while(i<6){
macaddr[i]=mymac[i];
i++;
}
}
uint8_t check_ip_message_is_from(uint8_t *buf,uint8_t *ip)
{
uint8_t i=0;
while(i<4){
if(buf[IP_SRC_P+i]!=ip[i]){
return(0);
}
i++;
}
return(1);
}
uint8_t eth_type_is_arp_and_my_ip(uint8_t *buf,uint16_t len){
uint8_t i=0;
//
if (len<41){
return(0);
}
if(buf[ETH_TYPE_H_P] != ETHTYPE_ARP_H_V ||
buf[ETH_TYPE_L_P] != ETHTYPE_ARP_L_V){
return(0);
}
while(i<4){
if(buf[ETH_ARP_DST_IP_P+i] != ipaddr[i]){
return(0);
}
i++;
}
return(1);
}
uint8_t eth_type_is_ip_and_my_ip(uint8_t *buf,uint16_t len){
uint8_t i=0;
//eth+ip+udp header is 42
if (len<42){
return(0);
}
if(buf[ETH_TYPE_H_P]!=ETHTYPE_IP_H_V ||
buf[ETH_TYPE_L_P]!=ETHTYPE_IP_L_V){
return(0);
}
if (buf[IP_HEADER_LEN_VER_P]!=0x45){
// must be IP V4 and 20 byte header
return(0);
}
while(i<4){
if(buf[IP_DST_P+i]!=ipaddr[i]){
return(0);
}
i++;
}
return(1);
}
// make a return eth header from a received eth packet
void make_eth(uint8_t *buf)
{
uint8_t i=0;
//
//copy the destination mac from the source and fill my mac into src
while(i<6){
buf[ETH_DST_MAC +i]=buf[ETH_SRC_MAC +i];
buf[ETH_SRC_MAC +i]=macaddr[i];
i++;
}
}
// make a new eth header for IP packet
void make_eth_ip_new(uint8_t *buf, uint8_t* dst_mac)
{
uint8_t i=0;
//
//copy the destination mac from the source and fill my mac into src
while(i<6){
buf[ETH_DST_MAC +i]=dst_mac[i];
buf[ETH_SRC_MAC +i]=macaddr[i];
i++;
}
buf[ ETH_TYPE_H_P ] = ETHTYPE_IP_H_V;
buf[ ETH_TYPE_L_P ] = ETHTYPE_IP_L_V;
}
void fill_ip_hdr_checksum(uint8_t *buf)
{
uint16_t ck;
// clear the 2 byte checksum
buf[IP_CHECKSUM_P]=0;
buf[IP_CHECKSUM_P+1]=0;
buf[IP_FLAGS_P]=0x40; // don't fragment
buf[IP_FLAGS_P+1]=0; // fragement offset
buf[IP_TTL_P]=64; // ttl
// calculate the checksum:
ck=checksum(&buf[IP_P], IP_HEADER_LEN,0);
buf[IP_CHECKSUM_P]=ck>>8;
buf[IP_CHECKSUM_P+1]=ck & 0xff;
}
// is it an arp reply (no len check here, you must first call eth_type_is_arp_and_my_ip)
uint8_t eth_type_is_arp_reply(uint8_t *buf){
return (buf[ETH_ARP_OPCODE_L_P]==ETH_ARP_OPCODE_REPLY_L_V);
}
// is it an arp request (no len check here, you must first call eth_type_is_arp_and_my_ip)
uint8_t eth_type_is_arp_req(uint8_t *buf){
return (buf[ETH_ARP_OPCODE_L_P]==ETH_ARP_OPCODE_REQ_L_V);
}
// make a return ip header from a received ip packet
static uint16_t ip_identifier = 1;
// make a new ip header for tcp packet
// make a return ip header from a received ip packet
void make_ip_tcp_new(uint8_t *buf, uint16_t len,uint8_t *dst_ip)
{
uint8_t i=0;
// set ipv4 and header length
buf[ IP_P ] = IP_V4_V | IP_HEADER_LENGTH_V;
// set TOS to default 0x00
buf[ IP_TOS_P ] = 0x00;
// set total length
buf[ IP_TOTLEN_H_P ] = (len >>8)& 0xff;
buf[ IP_TOTLEN_L_P ] = len & 0xff;
// set packet identification
buf[ IP_ID_H_P ] = (ip_identifier >>8) & 0xff;
buf[ IP_ID_L_P ] = ip_identifier & 0xff;
ip_identifier++;
// set fragment flags
buf[ IP_FLAGS_H_P ] = 0x00;
buf[ IP_FLAGS_L_P ] = 0x00;
// set Time To Live
buf[ IP_TTL_P ] = 128;
// set ip packettype to tcp/udp/icmp...
buf[ IP_PROTO_P ] = IP_PROTO_TCP_V;
// set source and destination ip address
while(i<4){
buf[IP_DST_P+i]=dst_ip[i];
buf[IP_SRC_P+i]=ipaddr[i];
i++;
}
fill_ip_hdr_checksum(buf);
}
void make_ip(uint8_t *buf)
{
uint8_t i=0;
while(i<4){
buf[IP_DST_P+i]=buf[IP_SRC_P+i];
buf[IP_SRC_P+i]=ipaddr[i];
i++;
}
fill_ip_hdr_checksum(buf);
}
// swap seq and ack number and count ack number up
void step_seq(uint8_t *buf,uint16_t rel_ack_num,uint8_t cp_seq)
{
uint8_t i;
uint8_t tseq;
i=4;
// sequence numbers:
// add the rel ack num to SEQACK
while(i>0){
rel_ack_num=buf[TCP_SEQ_H_P+i-1]+rel_ack_num;
tseq=buf[TCP_SEQACK_H_P+i-1];
buf[TCP_SEQACK_H_P+i-1]=0xff&rel_ack_num;
if (cp_seq){
// copy the acknum sent to us into the sequence number
buf[TCP_SEQ_H_P+i-1]=tseq;
}else{
buf[TCP_SEQ_H_P+i-1]= 0; // some preset vallue
}
rel_ack_num=rel_ack_num>>8;
i--;
}
}
// make a return tcp header from a received tcp packet
// rel_ack_num is how much we must step the seq number received from the
// other side. We do not send more than 765 bytes of text (=data) in the tcp packet.
// No mss is included here.
//
// After calling this function you can fill in the first data byte at TCP_OPTIONS_P+4
// If cp_seq=0 then an initial sequence number is used (should be use in synack)
// otherwise it is copied from the packet we received
void make_tcphead(uint8_t *buf,uint16_t rel_ack_num,uint8_t cp_seq)
{
uint8_t i;
// copy ports:
i=buf[TCP_DST_PORT_H_P];
buf[TCP_DST_PORT_H_P]=buf[TCP_SRC_PORT_H_P];
buf[TCP_SRC_PORT_H_P]=i;
//
i=buf[TCP_DST_PORT_L_P];
buf[TCP_DST_PORT_L_P]=buf[TCP_SRC_PORT_L_P];
buf[TCP_SRC_PORT_L_P]=i;
step_seq(buf,rel_ack_num,cp_seq);
// zero the checksum
buf[TCP_CHECKSUM_H_P]=0;
buf[TCP_CHECKSUM_L_P]=0;
// no options:
// 20 bytes:
// The tcp header length is only a 4 bit field (the upper 4 bits).
// It is calculated in units of 4 bytes.
// E.g 20 bytes: 20/4=6 => 0x50=header len field
buf[TCP_HEADER_LEN_P]=0x50;
}
void make_arp_answer_from_request(uint8_t *buf)
{
uint8_t i=0;
//
make_eth(buf);
buf[ETH_ARP_OPCODE_H_P]=ETH_ARP_OPCODE_REPLY_H_V;
buf[ETH_ARP_OPCODE_L_P]=ETH_ARP_OPCODE_REPLY_L_V;
// fill the mac addresses:
while(i<6){
buf[ETH_ARP_DST_MAC_P+i]=buf[ETH_ARP_SRC_MAC_P+i];
buf[ETH_ARP_SRC_MAC_P+i]=macaddr[i];
i++;
}
i=0;
while(i<4){
buf[ETH_ARP_DST_IP_P+i]=buf[ETH_ARP_SRC_IP_P+i];
buf[ETH_ARP_SRC_IP_P+i]=ipaddr[i];
i++;
}
// eth+arp is 42 bytes:
enc28j60PacketSend(42,buf);
}
void make_echo_reply_from_request(uint8_t *buf,uint16_t len)
{
make_eth(buf);
make_ip(buf);
buf[ICMP_TYPE_P]=ICMP_TYPE_ECHOREPLY_V;
// we changed only the icmp.type field from request(=8) to reply(=0).
// we can therefore easily correct the checksum:
if (buf[ICMP_CHECKSUM_P] > (0xff-0x08)){
buf[ICMP_CHECKSUM_P+1]++;
}
buf[ICMP_CHECKSUM_P]+=0x08;
//
enc28j60PacketSend(len,buf);
}
// you can send a max of 220 bytes of data
void make_udp_reply_from_request(uint8_t *buf,char *data,uint16_t datalen,uint16_t port)
{
uint8_t i=0;
uint16_t ck;
make_eth(buf);
if (datalen>220){
datalen=220;
}
// total length field in the IP header must be set:
buf[IP_TOTLEN_H_P]=(IP_HEADER_LEN+UDP_HEADER_LEN+datalen) >>8;
buf[IP_TOTLEN_L_P]=(IP_HEADER_LEN+UDP_HEADER_LEN+datalen) & 0xff;
make_ip(buf);
// send to port:
//buf[UDP_DST_PORT_H_P]=port>>8;
//buf[UDP_DST_PORT_L_P]=port & 0xff;
// sent to port of sender and use "port" as own source:
buf[UDP_DST_PORT_H_P]=buf[UDP_SRC_PORT_H_P];
buf[UDP_DST_PORT_L_P]= buf[UDP_SRC_PORT_L_P];
buf[UDP_SRC_PORT_H_P]=port>>8;
buf[UDP_SRC_PORT_L_P]=port & 0xff;
// calculte the udp length:
buf[UDP_LEN_H_P]=(UDP_HEADER_LEN+datalen) >> 8;
buf[UDP_LEN_L_P]=(UDP_HEADER_LEN+datalen) & 0xff;
// zero the checksum
buf[UDP_CHECKSUM_H_P]=0;
buf[UDP_CHECKSUM_L_P]=0;
// copy the data:
while(i<datalen){
buf[UDP_DATA_P+i]=data[i];
i++;
}
ck=checksum(&buf[IP_SRC_P], 16 + datalen,1);
buf[UDP_CHECKSUM_H_P]=ck>>8;
buf[UDP_CHECKSUM_L_P]=ck & 0xff;
enc28j60PacketSend(UDP_HEADER_LEN+IP_HEADER_LEN+ETH_HEADER_LEN+datalen,buf);
}
// this is for the server not the client:
void make_tcp_synack_from_syn(uint8_t *buf)
{
uint16_t ck;
make_eth(buf);
// total length field in the IP header must be set:
// 20 bytes IP + 24 bytes (20tcp+4tcp options)
buf[IP_TOTLEN_H_P]=0;
buf[IP_TOTLEN_L_P]=IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+4;
make_ip(buf);
buf[TCP_FLAGS_P]=TCP_FLAGS_SYNACK_V;
make_tcphead(buf,1,0);
// put an inital seq number
buf[TCP_SEQ_H_P+0]= 0;
buf[TCP_SEQ_H_P+1]= 0;
// we step only the second byte, this allows us to send packts
// with 255 bytes, 512 or 765 (step by 3) without generating
// overlapping numbers.
buf[TCP_SEQ_H_P+2]= seqnum;
buf[TCP_SEQ_H_P+3]= 0;
// step the inititial seq num by something we will not use
// during this tcp session:
seqnum+=3;
// add an mss options field with MSS to 1280:
// 1280 in hex is 0x500
buf[TCP_OPTIONS_P]=2;
buf[TCP_OPTIONS_P+1]=4;
buf[TCP_OPTIONS_P+2]=0x05;
buf[TCP_OPTIONS_P+3]=0x0;
// The tcp header length is only a 4 bit field (the upper 4 bits).
// It is calculated in units of 4 bytes.
// E.g 24 bytes: 24/4=6 => 0x60=header len field
buf[TCP_HEADER_LEN_P]=0x60;
// here we must just be sure that the web browser contacting us
// will send only one get packet
buf[TCP_WIN_SIZE]=0x5; // 1400=0x578
buf[TCP_WIN_SIZE+1]=0x78;
// calculate the checksum, len=8 (start from ip.src) + TCP_HEADER_LEN_PLAIN + 4 (one option: mss)
ck=checksum(&buf[IP_SRC_P], 8+TCP_HEADER_LEN_PLAIN+4,2);
buf[TCP_CHECKSUM_H_P]=ck>>8;
buf[TCP_CHECKSUM_L_P]=ck& 0xff;
// add 4 for option mss:
enc28j60PacketSend(IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+4+ETH_HEADER_LEN,buf);
}
// do some basic length calculations and store the result in static variables
uint16_t get_tcp_data_len(uint8_t *buf)
{
int16_t i;
i=(((int16_t)buf[IP_TOTLEN_H_P])<<8)|(buf[IP_TOTLEN_L_P]&0xff);
i-=IP_HEADER_LEN;
i-=(buf[TCP_HEADER_LEN_P]>>4)*4; // generate len in bytes;
if (i<=0){
i=0;
}
return((uint16_t)i);
}
// get a pointer to the start of tcp data in buf
// Returns 0 if there is no data
// You must call init_len_info once before calling this function
// Not used?
/*
uint16_t get_tcp_data_pointer(void)
{
if (info_data_len){
return((uint16_t)TCP_SRC_PORT_H_P+info_hdr_len);
}else{
return(0);
}
}
*/
// do some basic length calculations and store the result in static varibales
// Not used?
/*
void init_len_info(uint8_t *buf)
{
info_data_len=(((int16_t)buf[IP_TOTLEN_H_P])<<8)|(buf[IP_TOTLEN_L_P]&0xff);
info_data_len-=IP_HEADER_LEN;
info_hdr_len=(buf[TCP_HEADER_LEN_P]>>4)*4; // generate len in bytes;
info_data_len-=info_hdr_len;
if (info_data_len<=0){
info_data_len=0;
}
}
*/
// fill in tcp data at position pos. pos=0 means start of
// tcp data. Returns the position at which the string after
// this string could be filled.
uint16_t fill_tcp_data_p(uint8_t *buf,uint16_t pos, const prog_char *progmem_s)
{
char c;
// fill in tcp data at position pos
//
// with no options the data starts after the checksum + 2 more bytes (urgent ptr)
while ((c = pgm_read_byte(progmem_s++))) {
buf[TCP_CHECKSUM_L_P+3+pos]=c;
pos++;
}
return(pos);
}
// fill a binary string of len data into the tcp packet
uint16_t fill_tcp_data_len(uint8_t *buf,uint16_t pos, const uint8_t *s, uint16_t len)
{
// fill in tcp data at position pos
//
// with no options the data starts after the checksum + 2 more bytes (urgent ptr)
while (len) {
buf[TCP_CHECKSUM_L_P+3+pos]=*s;
pos++;
s++;
len--;
}
return(pos);
}
// fill in tcp data at position pos. pos=0 means start of
// tcp data. Returns the position at which the string after
// this string could be filled.
uint16_t fill_tcp_data(uint8_t *buf,uint16_t pos, const char *s)
{
return(fill_tcp_data_len(buf,pos,(uint8_t*)s,strlen(s)));
}
// Make just an ack packet with no tcp data inside
// This will modify the eth/ip/tcp header
void make_tcp_ack_from_any(uint8_t *buf,int16_t datlentoack,uint8_t addflags)
{
uint16_t j;
make_eth(buf);
// fill the header:
buf[TCP_FLAGS_P]=TCP_FLAGS_ACK_V|addflags;
if (addflags==TCP_FLAGS_RST_V){
make_tcphead(buf,datlentoack,1);
} else {
if (datlentoack==0){
// if there is no data then we must still acknoledge one packet
datlentoack = 1;
}
make_tcphead(buf,datlentoack,1); // no options
}
// total length field in the IP header must be set:
// 20 bytes IP + 20 bytes tcp (when no options)
j=IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN;
buf[IP_TOTLEN_H_P]=j>>8;
buf[IP_TOTLEN_L_P]=j& 0xff;
make_ip(buf);
// use a low window size otherwise we have to have
// timers and can not just react on every packet.
buf[TCP_WIN_SIZE]=0x4; // 1024=0x400
buf[TCP_WIN_SIZE+1]=0x0;
// calculate the checksum, len=8 (start from ip.src) + TCP_HEADER_LEN_PLAIN + data len
j=checksum(&buf[IP_SRC_P], 8+TCP_HEADER_LEN_PLAIN,2);
buf[TCP_CHECKSUM_H_P]=j>>8;
buf[TCP_CHECKSUM_L_P]=j& 0xff;
enc28j60PacketSend(IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+ETH_HEADER_LEN,buf);
}
// dlen is the amount of tcp data (http data) we send in this packet
// You can use this function only immediately after make_tcp_ack_from_any
// This is because this function will NOT modify the eth/ip/tcp header except for
// length and checksum
// You must set TCP_FLAGS before calling this
void make_tcp_ack_with_data_noflags(uint8_t *buf,uint16_t dlen)
{
uint16_t j;
// total length field in the IP header must be set:
// 20 bytes IP + 20 bytes tcp (when no options) + len of data
j=IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+dlen;
buf[IP_TOTLEN_H_P]=j>>8;
buf[IP_TOTLEN_L_P]=j& 0xff;
fill_ip_hdr_checksum(buf);
// zero the checksum
buf[TCP_CHECKSUM_H_P]=0;
buf[TCP_CHECKSUM_L_P]=0;
// calculate the checksum, len=8 (start from ip.src) + TCP_HEADER_LEN_PLAIN + data len
j=checksum(&buf[IP_SRC_P], 8+TCP_HEADER_LEN_PLAIN+dlen,2);
buf[TCP_CHECKSUM_H_P]=j>>8;
buf[TCP_CHECKSUM_L_P]=j& 0xff;
enc28j60PacketSend(IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+dlen+ETH_HEADER_LEN,buf);
}
// you must have called init_len_info at some time before calling this function
// dlen is the amount of tcp data (http data) we send in this packet
// You can use this function only immediately after make_tcp_ack_from_any
// This is because this function will NOT modify the eth/ip/tcp header except for
// length and checksum
// Used?
void make_tcp_ack_with_data(uint8_t *buf,uint16_t dlen)
{
uint16_t j;
// fill the header:
// This code requires that we send only one data packet
// because we keep no state information. We must therefore set
// the fin here:
buf[TCP_FLAGS_P]=TCP_FLAGS_ACK_V|TCP_FLAGS_PUSH_V|TCP_FLAGS_FIN_V;
// total length field in the IP header must be set:
// 20 bytes IP + 20 bytes tcp (when no options) + len of data
j=IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+dlen;
buf[IP_TOTLEN_H_P]=j>>8;
buf[IP_TOTLEN_L_P]=j& 0xff;
fill_ip_hdr_checksum(buf);
// zero the checksum
buf[TCP_CHECKSUM_H_P]=0;
buf[TCP_CHECKSUM_L_P]=0;
// calculate the checksum, len=8 (start from ip.src) + TCP_HEADER_LEN_PLAIN + data len
j=checksum(&buf[IP_SRC_P], 8+TCP_HEADER_LEN_PLAIN+dlen,2);
buf[TCP_CHECKSUM_H_P]=j>>8;
buf[TCP_CHECKSUM_L_P]=j& 0xff;
enc28j60PacketSend(IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+dlen+ETH_HEADER_LEN,buf);
}
// you must have initialized info_data_len at some time before calling this function
//
// This info_data_len initialisation is done automatically if you call
// packetloop_icmp_tcp(buf,enc28j60PacketReceive(BUFFER_SIZE, buf));
// and test the return value for non zero.
//
// dlen is the amount of tcp data (http data) we send in this packet
// You can use this function only immediately after make_tcp_ack_from_any
// This is because this function will NOT modify the eth/ip/tcp header except for
// length and checksum
void www_server_reply(uint8_t *buf,uint16_t dlen)
{
make_tcp_ack_from_any(buf,info_data_len,0); // send ack for http get
// fill the header:
// This code requires that we send only one data packet
// because we keep no state information. We must therefore set
// the fin here:
buf[TCP_FLAGS_P]=TCP_FLAGS_ACK_V|TCP_FLAGS_PUSH_V|TCP_FLAGS_FIN_V;
make_tcp_ack_with_data_noflags(buf,dlen); // send data
}
#if defined (NTP_client) || defined (WOL_client) || defined (UDP_client) || defined (TCP_client) || defined (PING_client)
// fill buffer with a prog-mem string
void fill_buf_p(uint8_t *buf,uint16_t len, const prog_char *progmem_s)
{
while (len){
*buf= pgm_read_byte(progmem_s);
buf++;
progmem_s++;
len--;
}
}
#endif
#ifdef PING_client
// icmp echo, matchpat is a pattern that has to be sent back by the
// host answering the ping.
// The ping is sent to destip and mac gwmacaddr
void client_icmp_request(uint8_t *buf,uint8_t *destip)
{
uint8_t i=0;
uint16_t ck;
//
while(i<6){
buf[ETH_DST_MAC +i]=gwmacaddr[i]; // gw mac in local lan or host mac
buf[ETH_SRC_MAC +i]=macaddr[i];
i++;
}
buf[ETH_TYPE_H_P] = ETHTYPE_IP_H_V;
buf[ETH_TYPE_L_P] = ETHTYPE_IP_L_V;
fill_buf_p(&buf[IP_P],9,iphdr);
buf[IP_TOTLEN_L_P]=0x82; // TUX Code has 0x54, here has 0x82
buf[IP_PROTO_P]=IP_PROTO_UDP_V;
i=0;
while(i<4){
buf[IP_DST_P+i]=destip[i];
buf[IP_SRC_P+i]=ipaddr[i];
i++;
}
fill_ip_hdr_checksum(buf);
buf[ICMP_TYPE_P]=ICMP_TYPE_ECHOREQUEST_V;
buf[ICMP_TYPE_P+1]=0; // code
// zero the checksum
buf[ICMP_CHECKSUM_H_P]=0;
buf[ICMP_CHECKSUM_L_P]=0;
// a possibly unique id of this host:
buf[ICMP_IDENT_H_P]=5; // some number
buf[ICMP_IDENT_L_P]=ipaddr[3]; // last byte of my IP
//
buf[ICMP_IDENT_L_P+1]=0; // seq number, high byte
buf[ICMP_IDENT_L_P+2]=1; // seq number, low byte, we send only 1 ping at a time
// copy the data:
i=0;
while(i<56){
buf[ICMP_DATA_P+i]=PINGPATTERN;
i++;
}
//
ck=checksum(&buf[ICMP_TYPE_P], 56+8,0);
buf[ICMP_CHECKSUM_H_P]=ck>>8;
buf[ICMP_CHECKSUM_L_P]=ck& 0xff;
enc28j60PacketSend(98,buf);
}
#endif // PING_client
#ifdef NTP_client
// ntp udp packet
// See http://tools.ietf.org/html/rfc958 for details
//
void client_ntp_request(uint8_t *buf,uint8_t *ntpip,uint8_t srcport)
{
uint8_t i=0;
uint16_t ck;
//
while(i<6){
buf[ETH_DST_MAC +i]=gwmacaddr[i]; // gw mac in local lan or host mac
buf[ETH_SRC_MAC +i]=macaddr[i];
i++;
}
buf[ETH_TYPE_H_P] = ETHTYPE_IP_H_V;
buf[ETH_TYPE_L_P] = ETHTYPE_IP_L_V;
fill_buf_p(&buf[IP_P],9,iphdr);
buf[IP_TOTLEN_L_P]=0x4c;
buf[IP_PROTO_P]=IP_PROTO_UDP_V;
i=0;
while(i<4){
buf[IP_DST_P+i]=ntpip[i];
buf[IP_SRC_P+i]=ipaddr[i];
i++;
}
fill_ip_hdr_checksum(buf);
buf[UDP_DST_PORT_H_P]=0;
buf[UDP_DST_PORT_L_P]=0x7b; // ntp=123
buf[UDP_SRC_PORT_H_P]=10;
buf[UDP_SRC_PORT_L_P]=srcport; // lower 8 bit of src port
buf[UDP_LEN_H_P]=0;
buf[UDP_LEN_L_P]=56; // fixed len
// zero the checksum
buf[UDP_CHECKSUM_H_P]=0;
buf[UDP_CHECKSUM_L_P]=0;
// copy the data:
i=0;
// most fields are zero, here we zero everything and fill later
while(i<48){
buf[UDP_DATA_P+i]=0;
i++;
}
fill_buf_p(&buf[UDP_DATA_P],10,ntpreqhdr);
//
ck=checksum(&buf[IP_SRC_P], 16 + 48,1);
buf[UDP_CHECKSUM_H_P]=ck>>8;
buf[UDP_CHECKSUM_L_P]=ck& 0xff;
enc28j60PacketSend(90,buf);
}
// process the answer from the ntp server:
// if dstport==0 then accept any port otherwise only answers going to dstport
// return 1 on sucessful processing of answer
uint8_t client_ntp_process_answer(uint8_t *buf,uint32_t *time,uint8_t dstport_l){
if (dstport_l){
if (buf[UDP_DST_PORT_L_P]!=dstport_l){
return(0);
}
}
if (buf[UDP_LEN_H_P]!=0 || buf[UDP_LEN_L_P]!=56 || buf[UDP_SRC_PORT_L_P]!=0x7b){
// not ntp
return(0);
}
// copy time from the transmit time stamp field:
*time=((uint32_t)buf[0x52]<<24)|((uint32_t)buf[0x53]<<16)|((uint32_t)buf[0x54]<<8)|((uint32_t)buf[0x55]);
return(1);
}
#endif
#ifdef UDP_client
// -------------------- send a spontanious UDP packet to a server
// There are two ways of using this:
// 1) you call send_udp_prepare, you fill the data yourself into buf starting at buf[UDP_DATA_P],
// you send the packet by calling send_udp_transmit
//
// 2) You just allocate a large enough buffer for you data and you call send_udp and nothing else
// needs to be done.
//
// send_udp sends via gwip, you must call client_set_gwip at startup
void send_udp_prepare(uint8_t *buf,uint16_t sport, uint8_t *dip, uint16_t dport)
{
uint8_t i=0;
while(i<6){
buf[ETH_DST_MAC +i]=gwmacaddr[i]; // gw mac in local lan or host mac
buf[ETH_SRC_MAC +i]=macaddr[i];
i++;
}
buf[ETH_TYPE_H_P] = ETHTYPE_IP_H_V;
buf[ETH_TYPE_L_P] = ETHTYPE_IP_L_V;
fill_buf_p(&buf[IP_P],9,iphdr);
// total length field in the IP header must be set:
buf[IP_TOTLEN_H_P]=0;
// done in transmit: buf[IP_TOTLEN_L_P]=IP_HEADER_LEN+UDP_HEADER_LEN+datalen;
buf[IP_PROTO_P]=IP_PROTO_UDP_V;
i=0;
while(i<4){
buf[IP_DST_P+i]=dip[i];
buf[IP_SRC_P+i]=ipaddr[i];
i++;
}
// done in transmit: fill_ip_hdr_checksum(buf);
buf[UDP_DST_PORT_H_P]=(dport>>8);
buf[UDP_DST_PORT_L_P]=0xff&dport;
buf[UDP_SRC_PORT_H_P]=(sport>>8);
buf[UDP_SRC_PORT_L_P]=sport&0xff;
buf[UDP_LEN_H_P]=0;
// done in transmit: buf[UDP_LEN_L_P]=UDP_HEADER_LEN+datalen;
// zero the checksum
buf[UDP_CHECKSUM_H_P]=0;
buf[UDP_CHECKSUM_L_P]=0;
// copy the data:
// now starting with the first byte at buf[UDP_DATA_P]
}
void send_udp_transmit(uint8_t *buf,uint16_t datalen)
{
uint16_t ck;
buf[IP_TOTLEN_H_P]=(IP_HEADER_LEN+UDP_HEADER_LEN+datalen) >> 8;
buf[IP_TOTLEN_L_P]=(IP_HEADER_LEN+UDP_HEADER_LEN+datalen) & 0xff;
fill_ip_hdr_checksum(buf);
//buf[UDP_LEN_L_P]=UDP_HEADER_LEN+datalen;
buf[UDP_LEN_H_P]=(UDP_HEADER_LEN+datalen) >>8;
buf[UDP_LEN_L_P]=(UDP_HEADER_LEN+datalen) & 0xff;
//
ck=checksum(&buf[IP_SRC_P], 16 + datalen,1);
buf[UDP_CHECKSUM_H_P]=ck>>8;
buf[UDP_CHECKSUM_L_P]=ck& 0xff;
enc28j60PacketSend(UDP_HEADER_LEN+IP_HEADER_LEN+ETH_HEADER_LEN+datalen,buf);
}
void send_udp(uint8_t *buf,char *data,uint16_t datalen,uint16_t sport, uint8_t *dip, uint16_t dport)
{
send_udp_prepare(buf,sport, dip, dport);
uint8_t i=0;
// limit the length: Why??? ADL
if (datalen>220){
datalen=220;
}
// copy the data:
i=0;
while(i<datalen){
buf[UDP_DATA_P+i]=data[i];
i++;
}
//
send_udp_transmit(buf,datalen);
}
#endif // UDP_client
#ifdef WOL_client
// -------------------- special code to make a WOL packet
// A WOL (Wake on Lan) packet is a UDP packet to the broadcast
// address and UDP port 9. The data part contains 6x FF followed by
// 16 times the mac address of the host to wake-up
//
void send_wol(uint8_t *buf,uint8_t *wolmac)
{
uint8_t i=0;
uint8_t m=0;
uint8_t pos=0;
uint16_t ck;
//
while(i<6){
buf[ETH_DST_MAC +i]=0xff;
buf[ETH_SRC_MAC +i]=macaddr[i];
i++;
}
buf[ETH_TYPE_H_P] = ETHTYPE_IP_H_V;
buf[ETH_TYPE_L_P] = ETHTYPE_IP_L_V;
fill_buf_p(&buf[IP_P],9,iphdr);
buf[IP_TOTLEN_L_P]=0x82;
buf[IP_PROTO_P]=IP_PROTO_UDP_V;
i=0;
while(i<4){
buf[IP_SRC_P+i]=ipaddr[i];
buf[IP_DST_P+i]=0xff;
i++;
}
fill_ip_hdr_checksum(buf);
buf[UDP_DST_PORT_H_P]=0;
buf[UDP_DST_PORT_L_P]=0x9; // wol=normally 9
buf[UDP_SRC_PORT_H_P]=10;
buf[UDP_SRC_PORT_L_P]=0x42; // source port does not matter
buf[UDP_LEN_H_P]=0;
buf[UDP_LEN_L_P]=110; // fixed len
// zero the checksum
buf[UDP_CHECKSUM_H_P]=0;
buf[UDP_CHECKSUM_L_P]=0;
// copy the data (102 bytes):
i=0;
while(i<6){
buf[UDP_DATA_P+i]=0xff;
i++;
}
m=0;
pos=UDP_DATA_P+i;
while (m<16){
i=0;
while(i<6){
buf[pos]=wolmac[i];