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@42:222a4f45f916, 2017-10-15 (annotated)
- Committer:
- andrewboyson
- Date:
- Sun Oct 15 17:54:09 2017 +0000
- Revision:
- 42:222a4f45f916
- Parent:
- 37:793b39683406
- Child:
- 43:bc028d5a6424
Made the use of IPv4 or IPv6 for client requests a configurable option
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 | 9:91dae5300a4d | 6 | #include "ar.h" |
andrewboyson | 35:93c39d260a83 | 7 | #include "nr.h" |
andrewboyson | 7:b794780e33b4 | 8 | #include "dns.h" |
andrewboyson | 7:b794780e33b4 | 9 | #include "slaac.h" |
andrewboyson | 29:39277bf2003d | 10 | #include "ra.h" |
andrewboyson | 22:914b970356f0 | 11 | #include "ntp.h" |
andrewboyson | 22:914b970356f0 | 12 | #include "io.h" |
andrewboyson | 22:914b970356f0 | 13 | #include "clock.h" |
andrewboyson | 2:849103b5a16d | 14 | |
andrewboyson | 37:793b39683406 | 15 | |
andrewboyson | 37:793b39683406 | 16 | bool NetTraceBack = false; |
andrewboyson | 37:793b39683406 | 17 | bool NetTraceForward = false; |
andrewboyson | 37:793b39683406 | 18 | |
andrewboyson | 42:222a4f45f916 | 19 | bool NetPreferIp4Polled = false; |
andrewboyson | 42:222a4f45f916 | 20 | |
andrewboyson | 10:f0854784e960 | 21 | char* NetName; |
andrewboyson | 10:f0854784e960 | 22 | |
andrewboyson | 0:faa09bd4e6bf | 23 | int16_t NetToHost16(int16_t n) |
andrewboyson | 0:faa09bd4e6bf | 24 | { |
andrewboyson | 0:faa09bd4e6bf | 25 | int16_t h; |
andrewboyson | 0:faa09bd4e6bf | 26 | |
andrewboyson | 0:faa09bd4e6bf | 27 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 28 | char* ph = (char*)&h + 1; |
andrewboyson | 0:faa09bd4e6bf | 29 | |
andrewboyson | 0:faa09bd4e6bf | 30 | *ph = *pn; ph--; pn++; // 1<-0 |
andrewboyson | 0:faa09bd4e6bf | 31 | *ph = *pn; // 0<-1 |
andrewboyson | 0:faa09bd4e6bf | 32 | |
andrewboyson | 0:faa09bd4e6bf | 33 | return h; |
andrewboyson | 0:faa09bd4e6bf | 34 | } |
andrewboyson | 0:faa09bd4e6bf | 35 | |
andrewboyson | 0:faa09bd4e6bf | 36 | int32_t NetToHost32(int32_t n) |
andrewboyson | 0:faa09bd4e6bf | 37 | { |
andrewboyson | 0:faa09bd4e6bf | 38 | int32_t h; |
andrewboyson | 0:faa09bd4e6bf | 39 | |
andrewboyson | 0:faa09bd4e6bf | 40 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 41 | char* ph = (char*)&h + 3; |
andrewboyson | 0:faa09bd4e6bf | 42 | |
andrewboyson | 0:faa09bd4e6bf | 43 | *ph = *pn; ph--; pn++; // 3<-0 |
andrewboyson | 0:faa09bd4e6bf | 44 | *ph = *pn; ph--; pn++; // 2<-1 |
andrewboyson | 0:faa09bd4e6bf | 45 | *ph = *pn; ph--; pn++; // 1<-2 |
andrewboyson | 0:faa09bd4e6bf | 46 | *ph = *pn; // 0<-3 |
andrewboyson | 0:faa09bd4e6bf | 47 | |
andrewboyson | 0:faa09bd4e6bf | 48 | return h; |
andrewboyson | 0:faa09bd4e6bf | 49 | } |
andrewboyson | 0:faa09bd4e6bf | 50 | int64_t NetToHost64(int64_t n) |
andrewboyson | 0:faa09bd4e6bf | 51 | { |
andrewboyson | 0:faa09bd4e6bf | 52 | int64_t h; |
andrewboyson | 0:faa09bd4e6bf | 53 | |
andrewboyson | 0:faa09bd4e6bf | 54 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 55 | char* ph = (char*)&h + 7; |
andrewboyson | 0:faa09bd4e6bf | 56 | |
andrewboyson | 0:faa09bd4e6bf | 57 | *ph = *pn; ph--; pn++; // 7<-0 |
andrewboyson | 0:faa09bd4e6bf | 58 | *ph = *pn; ph--; pn++; // 6<-1 |
andrewboyson | 0:faa09bd4e6bf | 59 | *ph = *pn; ph--; pn++; // 5<-2 |
andrewboyson | 0:faa09bd4e6bf | 60 | *ph = *pn; ph--; pn++; // 4<-3 |
andrewboyson | 0:faa09bd4e6bf | 61 | *ph = *pn; ph--; pn++; // 3<-4 |
andrewboyson | 0:faa09bd4e6bf | 62 | *ph = *pn; ph--; pn++; // 2<-5 |
andrewboyson | 0:faa09bd4e6bf | 63 | *ph = *pn; ph--; pn++; // 1<-6 |
andrewboyson | 0:faa09bd4e6bf | 64 | *ph = *pn; // 0<-7 |
andrewboyson | 0:faa09bd4e6bf | 65 | |
andrewboyson | 0:faa09bd4e6bf | 66 | return h; |
andrewboyson | 0:faa09bd4e6bf | 67 | } |
andrewboyson | 0:faa09bd4e6bf | 68 | uint16_t onesComplement(uint16_t start, int count, void* pData) |
andrewboyson | 0:faa09bd4e6bf | 69 | { |
andrewboyson | 0:faa09bd4e6bf | 70 | uint32_t sum = start; //Initialise the 32 bit accumulator with the last sum |
andrewboyson | 0:faa09bd4e6bf | 71 | uint16_t* p = (uint16_t*)pData; //Set up a 16 bit pointer for the data |
andrewboyson | 0:faa09bd4e6bf | 72 | |
andrewboyson | 0:faa09bd4e6bf | 73 | while(count > 1) |
andrewboyson | 0:faa09bd4e6bf | 74 | { |
andrewboyson | 0:faa09bd4e6bf | 75 | sum += *p++; // Add each pair of bytes into 32 bit accumulator |
andrewboyson | 0:faa09bd4e6bf | 76 | count -= 2; |
andrewboyson | 0:faa09bd4e6bf | 77 | } |
andrewboyson | 0:faa09bd4e6bf | 78 | if(count) sum += * (uint8_t*) p; // Add left-over byte, if any |
andrewboyson | 0:faa09bd4e6bf | 79 | 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 | 80 | return sum; |
andrewboyson | 0:faa09bd4e6bf | 81 | } |
andrewboyson | 0:faa09bd4e6bf | 82 | uint16_t NetCheckSumTwo(int count1, void* pData1, int count2, void* pData2) |
andrewboyson | 0:faa09bd4e6bf | 83 | { |
andrewboyson | 0:faa09bd4e6bf | 84 | uint16_t sum = onesComplement(0, count1, pData1); |
andrewboyson | 0:faa09bd4e6bf | 85 | return ~onesComplement(sum, count2, pData2); |
andrewboyson | 0:faa09bd4e6bf | 86 | } |
andrewboyson | 0:faa09bd4e6bf | 87 | uint16_t NetCheckSum(int count, void* pData) |
andrewboyson | 0:faa09bd4e6bf | 88 | { |
andrewboyson | 0:faa09bd4e6bf | 89 | return ~onesComplement(0, count, pData); |
andrewboyson | 0:faa09bd4e6bf | 90 | } |
andrewboyson | 2:849103b5a16d | 91 | |
andrewboyson | 10:f0854784e960 | 92 | int NetInit(char* name) |
andrewboyson | 2:849103b5a16d | 93 | { |
andrewboyson | 10:f0854784e960 | 94 | NetName = name; |
andrewboyson | 30:e34173b7585c | 95 | PhyInit(); |
andrewboyson | 30:e34173b7585c | 96 | TcpInit(); |
andrewboyson | 30:e34173b7585c | 97 | ArInit(); |
andrewboyson | 35:93c39d260a83 | 98 | NrInit(); |
andrewboyson | 30:e34173b7585c | 99 | SlaacInit(); |
andrewboyson | 2:849103b5a16d | 100 | return 0; |
andrewboyson | 2:849103b5a16d | 101 | } |
andrewboyson | 2:849103b5a16d | 102 | int NetMain() |
andrewboyson | 2:849103b5a16d | 103 | { |
andrewboyson | 35:93c39d260a83 | 104 | PhyMain(); |
andrewboyson | 35:93c39d260a83 | 105 | ArMain(); |
andrewboyson | 35:93c39d260a83 | 106 | NrMain(); |
andrewboyson | 23:b641979389b2 | 107 | if (ClockTicked) |
andrewboyson | 2:849103b5a16d | 108 | { |
andrewboyson | 35:93c39d260a83 | 109 | DhcpTick(); |
andrewboyson | 35:93c39d260a83 | 110 | ArTick(); |
andrewboyson | 35:93c39d260a83 | 111 | NrTick(); |
andrewboyson | 35:93c39d260a83 | 112 | DnsTick(); |
andrewboyson | 35:93c39d260a83 | 113 | RaTick(); |
andrewboyson | 2:849103b5a16d | 114 | } |
andrewboyson | 2:849103b5a16d | 115 | return 0; |
andrewboyson | 2:849103b5a16d | 116 | } |