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
Diff: net.c
- Revision:
- 61:aad055f1b0d1
- Parent:
- 59:e0e556c8bd46
- Child:
- 83:08c983006a6e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/net.c Thu Jan 11 17:38:21 2018 +0000 @@ -0,0 +1,141 @@ +#include <stdint.h> +#include <stdbool.h> + +#include "link.h" +#include "net.h" +#include "tcb.h" +#include "dhcp.h" +#include "ar4.h" +#include "ar6.h" +#include "nr4.h" +#include "nr6.h" +#include "dns.h" +#include "slaac.h" +#include "ndp.h" +#include "ntp.h" +#include "tftp.h" + +bool NetTraceStack = false; +bool NetTraceNewLine = false; +bool NetTraceVerbose = false; +bool NetPreferIp4Polled = false; +const char* NetName4; +const char* NetName6; + +static bool hostMatched = false; +bool NetTraceHostGetMatched() +{ + return hostMatched; +} +void NetTraceHostResetMatched() +{ + hostMatched = false; +} +char NetTraceHost[2]; +void NetTraceHostCheckIp6(char* ip) +{ + if (!ip[0]) return; + if (NetTraceHost[0] != ip[14]) return; + if (NetTraceHost[1] != ip[15]) return; + hostMatched = true; +} +void NetTraceHostCheckMac(char* mac) +{ + if (NetTraceHost[0] != mac[4]) return; + if (NetTraceHost[1] != mac[5]) return; + hostMatched = true; +} +int16_t NetToHost16(int16_t n) +{ + int16_t h; + + char* pn = (char*)&n; + char* ph = (char*)&h + 1; + + *ph = *pn; ph--; pn++; // 1<-0 + *ph = *pn; // 0<-1 + + return h; +} + +int32_t NetToHost32(int32_t n) +{ + int32_t h; + + char* pn = (char*)&n; + char* ph = (char*)&h + 3; + + *ph = *pn; ph--; pn++; // 3<-0 + *ph = *pn; ph--; pn++; // 2<-1 + *ph = *pn; ph--; pn++; // 1<-2 + *ph = *pn; // 0<-3 + + return h; +} +int64_t NetToHost64(int64_t n) +{ + int64_t h; + + char* pn = (char*)&n; + char* ph = (char*)&h + 7; + + *ph = *pn; ph--; pn++; // 7<-0 + *ph = *pn; ph--; pn++; // 6<-1 + *ph = *pn; ph--; pn++; // 5<-2 + *ph = *pn; ph--; pn++; // 4<-3 + *ph = *pn; ph--; pn++; // 3<-4 + *ph = *pn; ph--; pn++; // 2<-5 + *ph = *pn; ph--; pn++; // 1<-6 + *ph = *pn; // 0<-7 + + return h; +} +uint16_t onesComplement(uint16_t start, int count, void* pData) +{ + uint32_t sum = start; //Initialise the 32 bit accumulator with the last sum + uint16_t* p = (uint16_t*)pData; //Set up a 16 bit pointer for the data + + while(count > 1) + { + sum += *p++; // Add each pair of bytes into 32 bit accumulator + count -= 2; + } + if(count) sum += * (uint8_t*) p; // Add left-over byte, if any + while (sum>>16) sum = (sum & 0xffff) + (sum >> 16); // Add any carries from the sum back into the sum to make it ones complement + return sum; +} +uint16_t NetCheckSumTwo(int count1, void* pData1, int count2, void* pData2) +{ + uint16_t sum = onesComplement(0, count1, pData1); + return ~onesComplement(sum, count2, pData2); +} +uint16_t NetCheckSum(int count, void* pData) +{ + return ~onesComplement(0, count, pData); +} + +void NetInit(const char* name4, const char* name6) +{ + NetName4 = name4; + NetName6 = name6; + LinkInit(); + TcbInit(); + Ar4Init(); + Ar6Init(); + Nr4Init(); + Nr6Init(); + SlaacInit(); +} +void NetMain() +{ + LinkMain(); + TcbMain(); + Ar4Main(); + Ar6Main(); + Nr4Main(); + Nr6Main(); + DhcpMain(); + DnsMain(); + NdpMain(); + TftpMain(); +} \ No newline at end of file