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: ip4/ip4.cpp
- Revision:
- 47:73af5c0b0dc2
- Parent:
- 44:83ce5ace337b
- Child:
- 48:952dddb74b8b
--- a/ip4/ip4.cpp Tue Oct 24 07:01:35 2017 +0000 +++ b/ip4/ip4.cpp Thu Oct 26 14:50:24 2017 +0000 @@ -12,6 +12,7 @@ #include "ip4.h" #include "ntp.h" #include "mac.h" +#include "http-reply.h" #define SHOW_FILTERED true @@ -29,6 +30,22 @@ int a3 = (ip & 0x000000FF); return snprintf(text, size, "%d.%d.%d.%d", a3, a2, a1, a0); } +int Ip4AddressLog(uint32_t ip) +{ + int a0 = (ip & 0xFF000000) >> 24; + int a1 = (ip & 0x00FF0000) >> 16; + int a2 = (ip & 0x0000FF00) >> 8; + int a3 = (ip & 0x000000FF); + return LogF("%d.%d.%d.%d", a3, a2, a1, a0); +} +int Ip4AddressHttp(uint32_t ip) +{ + int a0 = (ip & 0xFF000000) >> 24; + int a1 = (ip & 0x00FF0000) >> 16; + int a2 = (ip & 0x0000FF00) >> 8; + int a3 = (ip & 0x000000FF); + return HttpReplyAddF("%d.%d.%d.%d", a3, a2, a1, a0); +} uint32_t Ip4Parse(char* text) { @@ -37,7 +54,7 @@ return (ints[0] << 24) + (ints[1] << 16) + (ints[2] << 8) + ints[3]; } -void Ip4DestIpFromDest(int dest, uint32_t* pDstIp) +void Ip4DstIpFromDest(int dest, uint32_t* pDstIp) { switch (dest) { @@ -134,7 +151,6 @@ static void logHeader() { - char text[30]; if (NetTraceVerbose) { Log ("IP4 header\r\n"); @@ -149,26 +165,20 @@ else LogF(" No more fragments\r\n"); LogF(" Offset %d\r\n", offset); LogF(" Time to live %d\r\n", ttl); - IpProtocolToString(protocol, sizeof(text), text); - LogF(" Protocol %s\r\n", text); + LogF(" Protocol "); IpProtocolLog(protocol); Log("\r\n"); LogF(" Checksum (hex) %04hX\r\n", checksum); LogF(" Calculated (hex) %04hX\r\n", calcsum); - Ip4AddressToString(srcIp, sizeof(text), text); - LogF(" Source IP %s\r\n", text); - Ip4AddressToString(dstIp, sizeof(text), text); - LogF(" Destination IP %s\r\n", text); + LogF(" Source IP "); Ip4AddressLog(srcIp); Log("\r\n"); + LogF(" Destination IP "); Ip4AddressLog(dstIp); Log("\r\n"); } else { Log ("IP4 header "); - IpProtocolToString(protocol, sizeof(text), text); - Log(text); + IpProtocolLog(protocol); Log(" "); - Ip4AddressToString(srcIp, sizeof(text), text); - Log(text); + Ip4AddressLog(srcIp); Log(" >>> "); - Ip4AddressToString(dstIp, sizeof(text), text); - Log(text); + Ip4AddressLog(dstIp); Log("\r\n"); } } @@ -194,11 +204,10 @@ { if (SHOW_FILTERED) { - char text[20]; - Ip4AddressToString(dstIp, sizeof(text), text); - LogTimeF("IP4 filtered out ip %s ", text); - Ip4AddressToString(srcIp, sizeof(text), text); - LogF("from %s \r\n", text); + LogTimeF("IP4 filtered out ip "); Ip4AddressLog(dstIp); + Log(" from "); + Ip4AddressLog(srcIp); + Log("\r\n"); } return DO_NOTHING; }