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@30:e34173b7585c, 2017-08-10 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Aug 10 17:45:37 2017 +0000
- Revision:
- 30:e34173b7585c
- Parent:
- 29:39277bf2003d
- Child:
- 35:93c39d260a83
Added dns address request - only name requests was there before.
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 | 30:e34173b7585c | 7 | #include "dnscache.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 | 10:f0854784e960 | 15 | char* NetName; |
andrewboyson | 10:f0854784e960 | 16 | |
andrewboyson | 0:faa09bd4e6bf | 17 | int16_t NetToHost16(int16_t n) |
andrewboyson | 0:faa09bd4e6bf | 18 | { |
andrewboyson | 0:faa09bd4e6bf | 19 | int16_t h; |
andrewboyson | 0:faa09bd4e6bf | 20 | |
andrewboyson | 0:faa09bd4e6bf | 21 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 22 | char* ph = (char*)&h + 1; |
andrewboyson | 0:faa09bd4e6bf | 23 | |
andrewboyson | 0:faa09bd4e6bf | 24 | *ph = *pn; ph--; pn++; // 1<-0 |
andrewboyson | 0:faa09bd4e6bf | 25 | *ph = *pn; // 0<-1 |
andrewboyson | 0:faa09bd4e6bf | 26 | |
andrewboyson | 0:faa09bd4e6bf | 27 | return h; |
andrewboyson | 0:faa09bd4e6bf | 28 | } |
andrewboyson | 0:faa09bd4e6bf | 29 | |
andrewboyson | 0:faa09bd4e6bf | 30 | int32_t NetToHost32(int32_t n) |
andrewboyson | 0:faa09bd4e6bf | 31 | { |
andrewboyson | 0:faa09bd4e6bf | 32 | int32_t h; |
andrewboyson | 0:faa09bd4e6bf | 33 | |
andrewboyson | 0:faa09bd4e6bf | 34 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 35 | char* ph = (char*)&h + 3; |
andrewboyson | 0:faa09bd4e6bf | 36 | |
andrewboyson | 0:faa09bd4e6bf | 37 | *ph = *pn; ph--; pn++; // 3<-0 |
andrewboyson | 0:faa09bd4e6bf | 38 | *ph = *pn; ph--; pn++; // 2<-1 |
andrewboyson | 0:faa09bd4e6bf | 39 | *ph = *pn; ph--; pn++; // 1<-2 |
andrewboyson | 0:faa09bd4e6bf | 40 | *ph = *pn; // 0<-3 |
andrewboyson | 0:faa09bd4e6bf | 41 | |
andrewboyson | 0:faa09bd4e6bf | 42 | return h; |
andrewboyson | 0:faa09bd4e6bf | 43 | } |
andrewboyson | 0:faa09bd4e6bf | 44 | int64_t NetToHost64(int64_t n) |
andrewboyson | 0:faa09bd4e6bf | 45 | { |
andrewboyson | 0:faa09bd4e6bf | 46 | int64_t h; |
andrewboyson | 0:faa09bd4e6bf | 47 | |
andrewboyson | 0:faa09bd4e6bf | 48 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 49 | char* ph = (char*)&h + 7; |
andrewboyson | 0:faa09bd4e6bf | 50 | |
andrewboyson | 0:faa09bd4e6bf | 51 | *ph = *pn; ph--; pn++; // 7<-0 |
andrewboyson | 0:faa09bd4e6bf | 52 | *ph = *pn; ph--; pn++; // 6<-1 |
andrewboyson | 0:faa09bd4e6bf | 53 | *ph = *pn; ph--; pn++; // 5<-2 |
andrewboyson | 0:faa09bd4e6bf | 54 | *ph = *pn; ph--; pn++; // 4<-3 |
andrewboyson | 0:faa09bd4e6bf | 55 | *ph = *pn; ph--; pn++; // 3<-4 |
andrewboyson | 0:faa09bd4e6bf | 56 | *ph = *pn; ph--; pn++; // 2<-5 |
andrewboyson | 0:faa09bd4e6bf | 57 | *ph = *pn; ph--; pn++; // 1<-6 |
andrewboyson | 0:faa09bd4e6bf | 58 | *ph = *pn; // 0<-7 |
andrewboyson | 0:faa09bd4e6bf | 59 | |
andrewboyson | 0:faa09bd4e6bf | 60 | return h; |
andrewboyson | 0:faa09bd4e6bf | 61 | } |
andrewboyson | 0:faa09bd4e6bf | 62 | uint16_t onesComplement(uint16_t start, int count, void* pData) |
andrewboyson | 0:faa09bd4e6bf | 63 | { |
andrewboyson | 0:faa09bd4e6bf | 64 | uint32_t sum = start; //Initialise the 32 bit accumulator with the last sum |
andrewboyson | 0:faa09bd4e6bf | 65 | uint16_t* p = (uint16_t*)pData; //Set up a 16 bit pointer for the data |
andrewboyson | 0:faa09bd4e6bf | 66 | |
andrewboyson | 0:faa09bd4e6bf | 67 | while(count > 1) |
andrewboyson | 0:faa09bd4e6bf | 68 | { |
andrewboyson | 0:faa09bd4e6bf | 69 | sum += *p++; // Add each pair of bytes into 32 bit accumulator |
andrewboyson | 0:faa09bd4e6bf | 70 | count -= 2; |
andrewboyson | 0:faa09bd4e6bf | 71 | } |
andrewboyson | 0:faa09bd4e6bf | 72 | if(count) sum += * (uint8_t*) p; // Add left-over byte, if any |
andrewboyson | 0:faa09bd4e6bf | 73 | 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 | 74 | return sum; |
andrewboyson | 0:faa09bd4e6bf | 75 | } |
andrewboyson | 0:faa09bd4e6bf | 76 | uint16_t NetCheckSumTwo(int count1, void* pData1, int count2, void* pData2) |
andrewboyson | 0:faa09bd4e6bf | 77 | { |
andrewboyson | 0:faa09bd4e6bf | 78 | uint16_t sum = onesComplement(0, count1, pData1); |
andrewboyson | 0:faa09bd4e6bf | 79 | return ~onesComplement(sum, count2, pData2); |
andrewboyson | 0:faa09bd4e6bf | 80 | } |
andrewboyson | 0:faa09bd4e6bf | 81 | uint16_t NetCheckSum(int count, void* pData) |
andrewboyson | 0:faa09bd4e6bf | 82 | { |
andrewboyson | 0:faa09bd4e6bf | 83 | return ~onesComplement(0, count, pData); |
andrewboyson | 0:faa09bd4e6bf | 84 | } |
andrewboyson | 2:849103b5a16d | 85 | |
andrewboyson | 10:f0854784e960 | 86 | int NetInit(char* name) |
andrewboyson | 2:849103b5a16d | 87 | { |
andrewboyson | 10:f0854784e960 | 88 | NetName = name; |
andrewboyson | 30:e34173b7585c | 89 | PhyInit(); |
andrewboyson | 30:e34173b7585c | 90 | TcpInit(); |
andrewboyson | 30:e34173b7585c | 91 | ArInit(); |
andrewboyson | 30:e34173b7585c | 92 | DnsCacheInit(); |
andrewboyson | 30:e34173b7585c | 93 | SlaacInit(); |
andrewboyson | 2:849103b5a16d | 94 | return 0; |
andrewboyson | 2:849103b5a16d | 95 | } |
andrewboyson | 2:849103b5a16d | 96 | int NetMain() |
andrewboyson | 2:849103b5a16d | 97 | { |
andrewboyson | 30:e34173b7585c | 98 | PhyMain(); |
andrewboyson | 30:e34173b7585c | 99 | ArMain(); |
andrewboyson | 30:e34173b7585c | 100 | DnsCacheMain(); |
andrewboyson | 23:b641979389b2 | 101 | if (ClockTicked) |
andrewboyson | 2:849103b5a16d | 102 | { |
andrewboyson | 30:e34173b7585c | 103 | DhcpTick(); |
andrewboyson | 30:e34173b7585c | 104 | ArTick(); |
andrewboyson | 30:e34173b7585c | 105 | DnsCacheTick(); |
andrewboyson | 30:e34173b7585c | 106 | DnsTick(); |
andrewboyson | 30:e34173b7585c | 107 | RaTick(); |
andrewboyson | 30:e34173b7585c | 108 | NtpTick(); |
andrewboyson | 2:849103b5a16d | 109 | } |
andrewboyson | 2:849103b5a16d | 110 | return 0; |
andrewboyson | 2:849103b5a16d | 111 | } |