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
net.cpp@48:952dddb74b8b, 2017-10-31 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Oct 31 21:25:09 2017 +0000
- Revision:
- 48:952dddb74b8b
- Parent:
- 47:73af5c0b0dc2
- Child:
- 50:492f2d2954e4
Split address resolution into AR4 and AR6. Corrected issue clearing a AR6 record.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 7:b794780e33b4 | 1 | #include "mbed.h" |
andrewboyson | 7:b794780e33b4 | 2 | #include "phy.h" |
andrewboyson | 7:b794780e33b4 | 3 | #include "net.h" |
andrewboyson | 7:b794780e33b4 | 4 | #include "tcp.h" |
andrewboyson | 7:b794780e33b4 | 5 | #include "dhcp.h" |
andrewboyson | 48:952dddb74b8b | 6 | #include "ar4.h" |
andrewboyson | 48:952dddb74b8b | 7 | #include "ar6.h" |
andrewboyson | 35:93c39d260a83 | 8 | #include "nr.h" |
andrewboyson | 7:b794780e33b4 | 9 | #include "dns.h" |
andrewboyson | 7:b794780e33b4 | 10 | #include "slaac.h" |
andrewboyson | 47:73af5c0b0dc2 | 11 | #include "ndp.h" |
andrewboyson | 22:914b970356f0 | 12 | #include "ntp.h" |
andrewboyson | 22:914b970356f0 | 13 | #include "io.h" |
andrewboyson | 2:849103b5a16d | 14 | |
andrewboyson | 37:793b39683406 | 15 | |
andrewboyson | 43:bc028d5a6424 | 16 | bool NetTraceStack = false; |
andrewboyson | 43:bc028d5a6424 | 17 | bool NetTraceNewLine = false; |
andrewboyson | 43:bc028d5a6424 | 18 | bool NetTraceVerbose = false; |
andrewboyson | 42:222a4f45f916 | 19 | bool NetPreferIp4Polled = false; |
andrewboyson | 42:222a4f45f916 | 20 | |
andrewboyson | 44:83ce5ace337b | 21 | char* NetName4; |
andrewboyson | 44:83ce5ace337b | 22 | char* NetName6; |
andrewboyson | 10:f0854784e960 | 23 | |
andrewboyson | 0:faa09bd4e6bf | 24 | int16_t NetToHost16(int16_t n) |
andrewboyson | 0:faa09bd4e6bf | 25 | { |
andrewboyson | 0:faa09bd4e6bf | 26 | int16_t h; |
andrewboyson | 0:faa09bd4e6bf | 27 | |
andrewboyson | 0:faa09bd4e6bf | 28 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 29 | char* ph = (char*)&h + 1; |
andrewboyson | 0:faa09bd4e6bf | 30 | |
andrewboyson | 0:faa09bd4e6bf | 31 | *ph = *pn; ph--; pn++; // 1<-0 |
andrewboyson | 0:faa09bd4e6bf | 32 | *ph = *pn; // 0<-1 |
andrewboyson | 0:faa09bd4e6bf | 33 | |
andrewboyson | 0:faa09bd4e6bf | 34 | return h; |
andrewboyson | 0:faa09bd4e6bf | 35 | } |
andrewboyson | 0:faa09bd4e6bf | 36 | |
andrewboyson | 0:faa09bd4e6bf | 37 | int32_t NetToHost32(int32_t n) |
andrewboyson | 0:faa09bd4e6bf | 38 | { |
andrewboyson | 0:faa09bd4e6bf | 39 | int32_t h; |
andrewboyson | 0:faa09bd4e6bf | 40 | |
andrewboyson | 0:faa09bd4e6bf | 41 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 42 | char* ph = (char*)&h + 3; |
andrewboyson | 0:faa09bd4e6bf | 43 | |
andrewboyson | 0:faa09bd4e6bf | 44 | *ph = *pn; ph--; pn++; // 3<-0 |
andrewboyson | 0:faa09bd4e6bf | 45 | *ph = *pn; ph--; pn++; // 2<-1 |
andrewboyson | 0:faa09bd4e6bf | 46 | *ph = *pn; ph--; pn++; // 1<-2 |
andrewboyson | 0:faa09bd4e6bf | 47 | *ph = *pn; // 0<-3 |
andrewboyson | 0:faa09bd4e6bf | 48 | |
andrewboyson | 0:faa09bd4e6bf | 49 | return h; |
andrewboyson | 0:faa09bd4e6bf | 50 | } |
andrewboyson | 0:faa09bd4e6bf | 51 | int64_t NetToHost64(int64_t n) |
andrewboyson | 0:faa09bd4e6bf | 52 | { |
andrewboyson | 0:faa09bd4e6bf | 53 | int64_t h; |
andrewboyson | 0:faa09bd4e6bf | 54 | |
andrewboyson | 0:faa09bd4e6bf | 55 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 56 | char* ph = (char*)&h + 7; |
andrewboyson | 0:faa09bd4e6bf | 57 | |
andrewboyson | 0:faa09bd4e6bf | 58 | *ph = *pn; ph--; pn++; // 7<-0 |
andrewboyson | 0:faa09bd4e6bf | 59 | *ph = *pn; ph--; pn++; // 6<-1 |
andrewboyson | 0:faa09bd4e6bf | 60 | *ph = *pn; ph--; pn++; // 5<-2 |
andrewboyson | 0:faa09bd4e6bf | 61 | *ph = *pn; ph--; pn++; // 4<-3 |
andrewboyson | 0:faa09bd4e6bf | 62 | *ph = *pn; ph--; pn++; // 3<-4 |
andrewboyson | 0:faa09bd4e6bf | 63 | *ph = *pn; ph--; pn++; // 2<-5 |
andrewboyson | 0:faa09bd4e6bf | 64 | *ph = *pn; ph--; pn++; // 1<-6 |
andrewboyson | 0:faa09bd4e6bf | 65 | *ph = *pn; // 0<-7 |
andrewboyson | 0:faa09bd4e6bf | 66 | |
andrewboyson | 0:faa09bd4e6bf | 67 | return h; |
andrewboyson | 0:faa09bd4e6bf | 68 | } |
andrewboyson | 0:faa09bd4e6bf | 69 | uint16_t onesComplement(uint16_t start, int count, void* pData) |
andrewboyson | 0:faa09bd4e6bf | 70 | { |
andrewboyson | 0:faa09bd4e6bf | 71 | uint32_t sum = start; //Initialise the 32 bit accumulator with the last sum |
andrewboyson | 0:faa09bd4e6bf | 72 | uint16_t* p = (uint16_t*)pData; //Set up a 16 bit pointer for the data |
andrewboyson | 0:faa09bd4e6bf | 73 | |
andrewboyson | 0:faa09bd4e6bf | 74 | while(count > 1) |
andrewboyson | 0:faa09bd4e6bf | 75 | { |
andrewboyson | 0:faa09bd4e6bf | 76 | sum += *p++; // Add each pair of bytes into 32 bit accumulator |
andrewboyson | 0:faa09bd4e6bf | 77 | count -= 2; |
andrewboyson | 0:faa09bd4e6bf | 78 | } |
andrewboyson | 0:faa09bd4e6bf | 79 | if(count) sum += * (uint8_t*) p; // Add left-over byte, if any |
andrewboyson | 0:faa09bd4e6bf | 80 | 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 | 81 | return sum; |
andrewboyson | 0:faa09bd4e6bf | 82 | } |
andrewboyson | 0:faa09bd4e6bf | 83 | uint16_t NetCheckSumTwo(int count1, void* pData1, int count2, void* pData2) |
andrewboyson | 0:faa09bd4e6bf | 84 | { |
andrewboyson | 0:faa09bd4e6bf | 85 | uint16_t sum = onesComplement(0, count1, pData1); |
andrewboyson | 0:faa09bd4e6bf | 86 | return ~onesComplement(sum, count2, pData2); |
andrewboyson | 0:faa09bd4e6bf | 87 | } |
andrewboyson | 0:faa09bd4e6bf | 88 | uint16_t NetCheckSum(int count, void* pData) |
andrewboyson | 0:faa09bd4e6bf | 89 | { |
andrewboyson | 0:faa09bd4e6bf | 90 | return ~onesComplement(0, count, pData); |
andrewboyson | 0:faa09bd4e6bf | 91 | } |
andrewboyson | 2:849103b5a16d | 92 | |
andrewboyson | 44:83ce5ace337b | 93 | int NetInit(char* name4, char* name6) |
andrewboyson | 2:849103b5a16d | 94 | { |
andrewboyson | 44:83ce5ace337b | 95 | NetName4 = name4; |
andrewboyson | 44:83ce5ace337b | 96 | NetName6 = name6; |
andrewboyson | 30:e34173b7585c | 97 | PhyInit(); |
andrewboyson | 30:e34173b7585c | 98 | TcpInit(); |
andrewboyson | 48:952dddb74b8b | 99 | Ar4Init(); |
andrewboyson | 48:952dddb74b8b | 100 | Ar6Init(); |
andrewboyson | 35:93c39d260a83 | 101 | NrInit(); |
andrewboyson | 30:e34173b7585c | 102 | SlaacInit(); |
andrewboyson | 2:849103b5a16d | 103 | return 0; |
andrewboyson | 2:849103b5a16d | 104 | } |
andrewboyson | 2:849103b5a16d | 105 | int NetMain() |
andrewboyson | 2:849103b5a16d | 106 | { |
andrewboyson | 35:93c39d260a83 | 107 | PhyMain(); |
andrewboyson | 48:952dddb74b8b | 108 | Ar4Main(); |
andrewboyson | 48:952dddb74b8b | 109 | Ar6Main(); |
andrewboyson | 35:93c39d260a83 | 110 | NrMain(); |
andrewboyson | 43:bc028d5a6424 | 111 | DhcpMain(); |
andrewboyson | 43:bc028d5a6424 | 112 | DnsMain(); |
andrewboyson | 47:73af5c0b0dc2 | 113 | NdpMain(); |
andrewboyson | 2:849103b5a16d | 114 | return 0; |
andrewboyson | 2:849103b5a16d | 115 | } |