A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Wed Dec 05 18:30:37 2018 +0000
Revision:
94:e2973a2c488e
Parent:
93:580fc113d9e9
Child:
97:d91f7db00235
Fixed bug - incorrect MSS being sent from a polled sync: expected 1440 but had -60. Traced to buffer datalength in EthPollForPacketToSend being set to zero instead of being calculated from the buffer length - headersize.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdint.h>
andrewboyson 61:aad055f1b0d1 2 #include <stdbool.h>
andrewboyson 61:aad055f1b0d1 3
andrewboyson 59:e0e556c8bd46 4 #include "link.h"
andrewboyson 7:b794780e33b4 5 #include "net.h"
andrewboyson 52:fbc5a46b5e16 6 #include "tcb.h"
andrewboyson 7:b794780e33b4 7 #include "dhcp.h"
andrewboyson 48:952dddb74b8b 8 #include "ar4.h"
andrewboyson 48:952dddb74b8b 9 #include "ar6.h"
andrewboyson 50:492f2d2954e4 10 #include "nr4.h"
andrewboyson 50:492f2d2954e4 11 #include "nr6.h"
andrewboyson 7:b794780e33b4 12 #include "dns.h"
andrewboyson 7:b794780e33b4 13 #include "slaac.h"
andrewboyson 47:73af5c0b0dc2 14 #include "ndp.h"
andrewboyson 22:914b970356f0 15 #include "ntp.h"
andrewboyson 57:e0fb648acf48 16 #include "tftp.h"
andrewboyson 83:08c983006a6e 17 #include "led.h"
andrewboyson 37:793b39683406 18
andrewboyson 43:bc028d5a6424 19 bool NetTraceStack = false;
andrewboyson 43:bc028d5a6424 20 bool NetTraceNewLine = false;
andrewboyson 43:bc028d5a6424 21 bool NetTraceVerbose = false;
andrewboyson 59:e0e556c8bd46 22 const char* NetName4;
andrewboyson 59:e0e556c8bd46 23 const char* NetName6;
andrewboyson 57:e0fb648acf48 24
andrewboyson 57:e0fb648acf48 25 static bool hostMatched = false;
andrewboyson 57:e0fb648acf48 26 bool NetTraceHostGetMatched()
andrewboyson 57:e0fb648acf48 27 {
andrewboyson 57:e0fb648acf48 28 return hostMatched;
andrewboyson 57:e0fb648acf48 29 }
andrewboyson 57:e0fb648acf48 30 void NetTraceHostResetMatched()
andrewboyson 57:e0fb648acf48 31 {
andrewboyson 57:e0fb648acf48 32 hostMatched = false;
andrewboyson 57:e0fb648acf48 33 }
andrewboyson 59:e0e556c8bd46 34 char NetTraceHost[2];
andrewboyson 57:e0fb648acf48 35 void NetTraceHostCheckIp6(char* ip)
andrewboyson 57:e0fb648acf48 36 {
andrewboyson 57:e0fb648acf48 37 if (!ip[0]) return;
andrewboyson 57:e0fb648acf48 38 if (NetTraceHost[0] != ip[14]) return;
andrewboyson 57:e0fb648acf48 39 if (NetTraceHost[1] != ip[15]) return;
andrewboyson 57:e0fb648acf48 40 hostMatched = true;
andrewboyson 57:e0fb648acf48 41 }
andrewboyson 57:e0fb648acf48 42 void NetTraceHostCheckMac(char* mac)
andrewboyson 57:e0fb648acf48 43 {
andrewboyson 57:e0fb648acf48 44 if (NetTraceHost[0] != mac[4]) return;
andrewboyson 57:e0fb648acf48 45 if (NetTraceHost[1] != mac[5]) return;
andrewboyson 57:e0fb648acf48 46 hostMatched = true;
andrewboyson 57:e0fb648acf48 47 }
andrewboyson 0:faa09bd4e6bf 48 int16_t NetToHost16(int16_t n)
andrewboyson 0:faa09bd4e6bf 49 {
andrewboyson 0:faa09bd4e6bf 50 int16_t h;
andrewboyson 0:faa09bd4e6bf 51
andrewboyson 0:faa09bd4e6bf 52 char* pn = (char*)&n;
andrewboyson 0:faa09bd4e6bf 53 char* ph = (char*)&h + 1;
andrewboyson 0:faa09bd4e6bf 54
andrewboyson 0:faa09bd4e6bf 55 *ph = *pn; ph--; pn++; // 1<-0
andrewboyson 0:faa09bd4e6bf 56 *ph = *pn; // 0<-1
andrewboyson 0:faa09bd4e6bf 57
andrewboyson 0:faa09bd4e6bf 58 return h;
andrewboyson 0:faa09bd4e6bf 59 }
andrewboyson 0:faa09bd4e6bf 60
andrewboyson 0:faa09bd4e6bf 61 int32_t NetToHost32(int32_t n)
andrewboyson 0:faa09bd4e6bf 62 {
andrewboyson 0:faa09bd4e6bf 63 int32_t h;
andrewboyson 0:faa09bd4e6bf 64
andrewboyson 0:faa09bd4e6bf 65 char* pn = (char*)&n;
andrewboyson 0:faa09bd4e6bf 66 char* ph = (char*)&h + 3;
andrewboyson 0:faa09bd4e6bf 67
andrewboyson 0:faa09bd4e6bf 68 *ph = *pn; ph--; pn++; // 3<-0
andrewboyson 0:faa09bd4e6bf 69 *ph = *pn; ph--; pn++; // 2<-1
andrewboyson 0:faa09bd4e6bf 70 *ph = *pn; ph--; pn++; // 1<-2
andrewboyson 0:faa09bd4e6bf 71 *ph = *pn; // 0<-3
andrewboyson 0:faa09bd4e6bf 72
andrewboyson 0:faa09bd4e6bf 73 return h;
andrewboyson 0:faa09bd4e6bf 74 }
andrewboyson 0:faa09bd4e6bf 75 int64_t NetToHost64(int64_t n)
andrewboyson 0:faa09bd4e6bf 76 {
andrewboyson 0:faa09bd4e6bf 77 int64_t h;
andrewboyson 0:faa09bd4e6bf 78
andrewboyson 0:faa09bd4e6bf 79 char* pn = (char*)&n;
andrewboyson 0:faa09bd4e6bf 80 char* ph = (char*)&h + 7;
andrewboyson 0:faa09bd4e6bf 81
andrewboyson 0:faa09bd4e6bf 82 *ph = *pn; ph--; pn++; // 7<-0
andrewboyson 0:faa09bd4e6bf 83 *ph = *pn; ph--; pn++; // 6<-1
andrewboyson 0:faa09bd4e6bf 84 *ph = *pn; ph--; pn++; // 5<-2
andrewboyson 0:faa09bd4e6bf 85 *ph = *pn; ph--; pn++; // 4<-3
andrewboyson 0:faa09bd4e6bf 86 *ph = *pn; ph--; pn++; // 3<-4
andrewboyson 0:faa09bd4e6bf 87 *ph = *pn; ph--; pn++; // 2<-5
andrewboyson 0:faa09bd4e6bf 88 *ph = *pn; ph--; pn++; // 1<-6
andrewboyson 0:faa09bd4e6bf 89 *ph = *pn; // 0<-7
andrewboyson 0:faa09bd4e6bf 90
andrewboyson 0:faa09bd4e6bf 91 return h;
andrewboyson 0:faa09bd4e6bf 92 }
andrewboyson 0:faa09bd4e6bf 93 uint16_t onesComplement(uint16_t start, int count, void* pData)
andrewboyson 0:faa09bd4e6bf 94 {
andrewboyson 0:faa09bd4e6bf 95 uint32_t sum = start; //Initialise the 32 bit accumulator with the last sum
andrewboyson 0:faa09bd4e6bf 96 uint16_t* p = (uint16_t*)pData; //Set up a 16 bit pointer for the data
andrewboyson 0:faa09bd4e6bf 97
andrewboyson 0:faa09bd4e6bf 98 while(count > 1)
andrewboyson 0:faa09bd4e6bf 99 {
andrewboyson 0:faa09bd4e6bf 100 sum += *p++; // Add each pair of bytes into 32 bit accumulator
andrewboyson 0:faa09bd4e6bf 101 count -= 2;
andrewboyson 0:faa09bd4e6bf 102 }
andrewboyson 0:faa09bd4e6bf 103 if(count) sum += * (uint8_t*) p; // Add left-over byte, if any
andrewboyson 0:faa09bd4e6bf 104 while (sum>>16) sum = (sum & 0xffff) + (sum >> 16); // Add any carries from the sum back into the sum to make it ones complement
andrewboyson 0:faa09bd4e6bf 105 return sum;
andrewboyson 0:faa09bd4e6bf 106 }
andrewboyson 0:faa09bd4e6bf 107 uint16_t NetCheckSumTwo(int count1, void* pData1, int count2, void* pData2)
andrewboyson 0:faa09bd4e6bf 108 {
andrewboyson 0:faa09bd4e6bf 109 uint16_t sum = onesComplement(0, count1, pData1);
andrewboyson 0:faa09bd4e6bf 110 return ~onesComplement(sum, count2, pData2);
andrewboyson 0:faa09bd4e6bf 111 }
andrewboyson 0:faa09bd4e6bf 112 uint16_t NetCheckSum(int count, void* pData)
andrewboyson 0:faa09bd4e6bf 113 {
andrewboyson 0:faa09bd4e6bf 114 return ~onesComplement(0, count, pData);
andrewboyson 0:faa09bd4e6bf 115 }
andrewboyson 2:849103b5a16d 116
andrewboyson 61:aad055f1b0d1 117 void NetInit(const char* name4, const char* name6)
andrewboyson 59:e0e556c8bd46 118 {
andrewboyson 44:83ce5ace337b 119 NetName4 = name4;
andrewboyson 44:83ce5ace337b 120 NetName6 = name6;
andrewboyson 59:e0e556c8bd46 121 LinkInit();
andrewboyson 52:fbc5a46b5e16 122 TcbInit();
andrewboyson 48:952dddb74b8b 123 Ar4Init();
andrewboyson 48:952dddb74b8b 124 Ar6Init();
andrewboyson 50:492f2d2954e4 125 Nr4Init();
andrewboyson 50:492f2d2954e4 126 Nr6Init();
andrewboyson 30:e34173b7585c 127 SlaacInit();
andrewboyson 2:849103b5a16d 128 }
andrewboyson 61:aad055f1b0d1 129 void NetMain()
andrewboyson 2:849103b5a16d 130 {
andrewboyson 59:e0e556c8bd46 131 LinkMain();
andrewboyson 57:e0fb648acf48 132 Ar4Main();
andrewboyson 57:e0fb648acf48 133 Ar6Main();
andrewboyson 57:e0fb648acf48 134 Nr4Main();
andrewboyson 57:e0fb648acf48 135 Nr6Main();
andrewboyson 57:e0fb648acf48 136 DnsMain();
andrewboyson 57:e0fb648acf48 137 NdpMain();
andrewboyson 84:e453577f2e92 138 TftpMain();
andrewboyson 2:849103b5a16d 139 }