Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Wed Nov 08 20:46:36 2017 +0000
Revision:
52:fbc5a46b5e16
Parent:
47:73af5c0b0dc2
Child:
59:e0e556c8bd46
Fixed bug in NDP options decoder which was crashing the system

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 43:bc028d5a6424 1 #include "mbed.h"
andrewboyson 43:bc028d5a6424 2 #include "log.h"
andrewboyson 43:bc028d5a6424 3 #include "net.h"
andrewboyson 43:bc028d5a6424 4 #include "action.h"
andrewboyson 43:bc028d5a6424 5 #include "ip4.h"
andrewboyson 43:bc028d5a6424 6 #include "dhcp.h"
andrewboyson 43:bc028d5a6424 7 #include "echo4.h"
andrewboyson 43:bc028d5a6424 8
andrewboyson 43:bc028d5a6424 9 #define HEADER_SIZE 4
andrewboyson 43:bc028d5a6424 10
andrewboyson 43:bc028d5a6424 11 #define ECHO_REPLY 0
andrewboyson 43:bc028d5a6424 12 #define UNREACHABLE 3
andrewboyson 43:bc028d5a6424 13 #define REDIRECT 5
andrewboyson 43:bc028d5a6424 14 #define ECHO_REQUEST 8
andrewboyson 43:bc028d5a6424 15
andrewboyson 43:bc028d5a6424 16 __packed struct header
andrewboyson 43:bc028d5a6424 17 {
andrewboyson 43:bc028d5a6424 18 uint8_t type;
andrewboyson 43:bc028d5a6424 19 uint8_t code;
andrewboyson 43:bc028d5a6424 20 uint16_t checksum;
andrewboyson 43:bc028d5a6424 21 };
andrewboyson 43:bc028d5a6424 22 static uint8_t type;
andrewboyson 43:bc028d5a6424 23 static uint8_t code;
andrewboyson 43:bc028d5a6424 24 static uint16_t checksum;
andrewboyson 43:bc028d5a6424 25 static uint16_t calculated;
andrewboyson 43:bc028d5a6424 26 static int dataLength;
andrewboyson 52:fbc5a46b5e16 27 //static void* pData;
andrewboyson 43:bc028d5a6424 28
andrewboyson 47:73af5c0b0dc2 29 static void logType(uint16_t type)
andrewboyson 43:bc028d5a6424 30 {
andrewboyson 43:bc028d5a6424 31 switch (type)
andrewboyson 43:bc028d5a6424 32 {
andrewboyson 47:73af5c0b0dc2 33 case ECHO_REPLY: Log ("Echo Reply" ); break;
andrewboyson 47:73af5c0b0dc2 34 case ECHO_REQUEST: Log ("Echo Request" ); break;
andrewboyson 47:73af5c0b0dc2 35 default: LogF("Unknown type %d", type); break;
andrewboyson 43:bc028d5a6424 36 }
andrewboyson 43:bc028d5a6424 37 }
andrewboyson 43:bc028d5a6424 38 static void logHeader()
andrewboyson 47:73af5c0b0dc2 39 {
andrewboyson 43:bc028d5a6424 40 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 41 {
andrewboyson 43:bc028d5a6424 42 Log ("ICMP4 header\r\n");
andrewboyson 47:73af5c0b0dc2 43 LogF(" Type "); logType(type); Log("\r\n");
andrewboyson 43:bc028d5a6424 44 LogF(" Code %u\r\n", code);
andrewboyson 43:bc028d5a6424 45 LogF(" Checksum (hex) %04hX\r\n", checksum);
andrewboyson 43:bc028d5a6424 46 LogF(" Calculated %04hX\r\n", calculated);
andrewboyson 43:bc028d5a6424 47 LogF(" Data length %d\r\n", dataLength);
andrewboyson 43:bc028d5a6424 48 }
andrewboyson 43:bc028d5a6424 49 else
andrewboyson 43:bc028d5a6424 50 {
andrewboyson 43:bc028d5a6424 51 Log("ICMP4 header ");
andrewboyson 47:73af5c0b0dc2 52 logType(type);
andrewboyson 43:bc028d5a6424 53 Log("\r\n");
andrewboyson 43:bc028d5a6424 54 }
andrewboyson 43:bc028d5a6424 55 }
andrewboyson 43:bc028d5a6424 56 static void readHeader(void* pPacket, int size)
andrewboyson 43:bc028d5a6424 57 {
andrewboyson 43:bc028d5a6424 58 struct header* pHeader = (header*)pPacket;
andrewboyson 43:bc028d5a6424 59 type = pHeader->type;
andrewboyson 43:bc028d5a6424 60 code = pHeader->code;
andrewboyson 43:bc028d5a6424 61 checksum = NetToHost16(pHeader->checksum);
andrewboyson 43:bc028d5a6424 62 calculated = NetCheckSum(size, pPacket);
andrewboyson 52:fbc5a46b5e16 63 // pData = (char*)pPacket + HEADER_SIZE;
andrewboyson 43:bc028d5a6424 64 dataLength = size - HEADER_SIZE;
andrewboyson 43:bc028d5a6424 65 }
andrewboyson 43:bc028d5a6424 66 static void writeHeader(void* pPacket, int size)
andrewboyson 43:bc028d5a6424 67 {
andrewboyson 43:bc028d5a6424 68 struct header* pHeader = (header*)pPacket;
andrewboyson 43:bc028d5a6424 69 pHeader->type = type;
andrewboyson 43:bc028d5a6424 70 pHeader->code = code;
andrewboyson 43:bc028d5a6424 71 pHeader->checksum = 0;
andrewboyson 43:bc028d5a6424 72 pHeader->checksum = NetCheckSum(size, pPacket);
andrewboyson 43:bc028d5a6424 73 calculated = 0;
andrewboyson 43:bc028d5a6424 74 }
andrewboyson 43:bc028d5a6424 75 static void (*pTraceBack)(void);
andrewboyson 43:bc028d5a6424 76 static void trace()
andrewboyson 43:bc028d5a6424 77 {
andrewboyson 43:bc028d5a6424 78 pTraceBack();
andrewboyson 43:bc028d5a6424 79 logHeader();
andrewboyson 43:bc028d5a6424 80 }
andrewboyson 43:bc028d5a6424 81 int Icmp4HandleReceivedPacket(void (*traceback)(void), uint32_t* pSrcIp, uint32_t* pDstIp, int* pSize, void * pPacket)
andrewboyson 43:bc028d5a6424 82 {
andrewboyson 43:bc028d5a6424 83 pTraceBack = traceback;
andrewboyson 43:bc028d5a6424 84
andrewboyson 43:bc028d5a6424 85 readHeader(pPacket, *pSize);
andrewboyson 43:bc028d5a6424 86
andrewboyson 43:bc028d5a6424 87 int action = DO_NOTHING;
andrewboyson 43:bc028d5a6424 88 switch (type)
andrewboyson 43:bc028d5a6424 89 {
andrewboyson 43:bc028d5a6424 90 case ECHO_REQUEST:
andrewboyson 43:bc028d5a6424 91 action = Echo4HandleRequest(trace, &type, &code);
andrewboyson 43:bc028d5a6424 92 break;
andrewboyson 43:bc028d5a6424 93 case UNREACHABLE:
andrewboyson 43:bc028d5a6424 94 return DO_NOTHING;
andrewboyson 43:bc028d5a6424 95 case REDIRECT:
andrewboyson 43:bc028d5a6424 96 return DO_NOTHING;
andrewboyson 43:bc028d5a6424 97 default:
andrewboyson 43:bc028d5a6424 98 LogTimeF("ICMP4 packet type %d unknown\r\n", type);
andrewboyson 43:bc028d5a6424 99 return DO_NOTHING;
andrewboyson 43:bc028d5a6424 100 }
andrewboyson 43:bc028d5a6424 101 if (!action) return DO_NOTHING;
andrewboyson 43:bc028d5a6424 102
andrewboyson 43:bc028d5a6424 103 *pDstIp = *pSrcIp;
andrewboyson 43:bc028d5a6424 104 *pSrcIp = DhcpLocalIp;
andrewboyson 43:bc028d5a6424 105
andrewboyson 43:bc028d5a6424 106 *pSize = HEADER_SIZE + dataLength;
andrewboyson 43:bc028d5a6424 107
andrewboyson 43:bc028d5a6424 108 writeHeader(pPacket, *pSize);
andrewboyson 43:bc028d5a6424 109
andrewboyson 43:bc028d5a6424 110 if (ActionGetTracePart(action)) logHeader();
andrewboyson 43:bc028d5a6424 111
andrewboyson 43:bc028d5a6424 112 return action;
andrewboyson 43:bc028d5a6424 113 }