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
ip4/icmp/icmp4.c@61:aad055f1b0d1, 2018-01-11 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Jan 11 17:38:21 2018 +0000
- Revision:
- 61:aad055f1b0d1
- Parent:
- ip4/icmp/icmp4.cpp@59:e0e556c8bd46
- Child:
- 136:8a65abb0dc63
Removed dependence on Mbed OS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdint.h> |
andrewboyson | 61:aad055f1b0d1 | 2 | |
andrewboyson | 43:bc028d5a6424 | 3 | #include "log.h" |
andrewboyson | 43:bc028d5a6424 | 4 | #include "net.h" |
andrewboyson | 43:bc028d5a6424 | 5 | #include "action.h" |
andrewboyson | 43:bc028d5a6424 | 6 | #include "ip4.h" |
andrewboyson | 43:bc028d5a6424 | 7 | #include "dhcp.h" |
andrewboyson | 43:bc028d5a6424 | 8 | #include "echo4.h" |
andrewboyson | 43:bc028d5a6424 | 9 | |
andrewboyson | 43:bc028d5a6424 | 10 | #define ECHO_REPLY 0 |
andrewboyson | 43:bc028d5a6424 | 11 | #define UNREACHABLE 3 |
andrewboyson | 43:bc028d5a6424 | 12 | #define REDIRECT 5 |
andrewboyson | 43:bc028d5a6424 | 13 | #define ECHO_REQUEST 8 |
andrewboyson | 43:bc028d5a6424 | 14 | |
andrewboyson | 43:bc028d5a6424 | 15 | __packed struct header |
andrewboyson | 43:bc028d5a6424 | 16 | { |
andrewboyson | 43:bc028d5a6424 | 17 | uint8_t type; |
andrewboyson | 43:bc028d5a6424 | 18 | uint8_t code; |
andrewboyson | 43:bc028d5a6424 | 19 | uint16_t checksum; |
andrewboyson | 43:bc028d5a6424 | 20 | }; |
andrewboyson | 43:bc028d5a6424 | 21 | static uint8_t type; |
andrewboyson | 43:bc028d5a6424 | 22 | static uint8_t code; |
andrewboyson | 43:bc028d5a6424 | 23 | static uint16_t checksum; |
andrewboyson | 43:bc028d5a6424 | 24 | static uint16_t calculated; |
andrewboyson | 43:bc028d5a6424 | 25 | |
andrewboyson | 47:73af5c0b0dc2 | 26 | static void logType(uint16_t type) |
andrewboyson | 43:bc028d5a6424 | 27 | { |
andrewboyson | 43:bc028d5a6424 | 28 | switch (type) |
andrewboyson | 43:bc028d5a6424 | 29 | { |
andrewboyson | 47:73af5c0b0dc2 | 30 | case ECHO_REPLY: Log ("Echo Reply" ); break; |
andrewboyson | 47:73af5c0b0dc2 | 31 | case ECHO_REQUEST: Log ("Echo Request" ); break; |
andrewboyson | 47:73af5c0b0dc2 | 32 | default: LogF("Unknown type %d", type); break; |
andrewboyson | 43:bc028d5a6424 | 33 | } |
andrewboyson | 43:bc028d5a6424 | 34 | } |
andrewboyson | 43:bc028d5a6424 | 35 | static void logHeader() |
andrewboyson | 47:73af5c0b0dc2 | 36 | { |
andrewboyson | 43:bc028d5a6424 | 37 | if (NetTraceVerbose) |
andrewboyson | 43:bc028d5a6424 | 38 | { |
andrewboyson | 43:bc028d5a6424 | 39 | Log ("ICMP4 header\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 40 | LogF(" Type "); logType(type); Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 41 | LogF(" Code %u\r\n", code); |
andrewboyson | 43:bc028d5a6424 | 42 | LogF(" Checksum (hex) %04hX\r\n", checksum); |
andrewboyson | 43:bc028d5a6424 | 43 | LogF(" Calculated %04hX\r\n", calculated); |
andrewboyson | 43:bc028d5a6424 | 44 | } |
andrewboyson | 43:bc028d5a6424 | 45 | else |
andrewboyson | 43:bc028d5a6424 | 46 | { |
andrewboyson | 43:bc028d5a6424 | 47 | Log("ICMP4 header "); |
andrewboyson | 47:73af5c0b0dc2 | 48 | logType(type); |
andrewboyson | 43:bc028d5a6424 | 49 | Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 50 | } |
andrewboyson | 43:bc028d5a6424 | 51 | } |
andrewboyson | 43:bc028d5a6424 | 52 | static void readHeader(void* pPacket, int size) |
andrewboyson | 43:bc028d5a6424 | 53 | { |
andrewboyson | 61:aad055f1b0d1 | 54 | struct header* pHeader = (struct header*)pPacket; |
andrewboyson | 43:bc028d5a6424 | 55 | type = pHeader->type; |
andrewboyson | 43:bc028d5a6424 | 56 | code = pHeader->code; |
andrewboyson | 43:bc028d5a6424 | 57 | checksum = NetToHost16(pHeader->checksum); |
andrewboyson | 43:bc028d5a6424 | 58 | calculated = NetCheckSum(size, pPacket); |
andrewboyson | 43:bc028d5a6424 | 59 | } |
andrewboyson | 43:bc028d5a6424 | 60 | static void writeHeader(void* pPacket, int size) |
andrewboyson | 43:bc028d5a6424 | 61 | { |
andrewboyson | 61:aad055f1b0d1 | 62 | struct header* pHeader = (struct header*)pPacket; |
andrewboyson | 43:bc028d5a6424 | 63 | pHeader->type = type; |
andrewboyson | 43:bc028d5a6424 | 64 | pHeader->code = code; |
andrewboyson | 43:bc028d5a6424 | 65 | pHeader->checksum = 0; |
andrewboyson | 43:bc028d5a6424 | 66 | pHeader->checksum = NetCheckSum(size, pPacket); |
andrewboyson | 43:bc028d5a6424 | 67 | calculated = 0; |
andrewboyson | 43:bc028d5a6424 | 68 | } |
andrewboyson | 43:bc028d5a6424 | 69 | static void (*pTraceBack)(void); |
andrewboyson | 43:bc028d5a6424 | 70 | static void trace() |
andrewboyson | 43:bc028d5a6424 | 71 | { |
andrewboyson | 43:bc028d5a6424 | 72 | pTraceBack(); |
andrewboyson | 43:bc028d5a6424 | 73 | logHeader(); |
andrewboyson | 43:bc028d5a6424 | 74 | } |
andrewboyson | 59:e0e556c8bd46 | 75 | int Icmp4HandleReceivedPacket(void (*traceback)(void), void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, uint32_t* pSrcIp, uint32_t* pDstIp) |
andrewboyson | 43:bc028d5a6424 | 76 | { |
andrewboyson | 43:bc028d5a6424 | 77 | pTraceBack = traceback; |
andrewboyson | 43:bc028d5a6424 | 78 | |
andrewboyson | 59:e0e556c8bd46 | 79 | readHeader(pPacketRx, sizeRx); |
andrewboyson | 59:e0e556c8bd46 | 80 | |
andrewboyson | 61:aad055f1b0d1 | 81 | int dataLengthRx = sizeRx - sizeof(struct header); |
andrewboyson | 61:aad055f1b0d1 | 82 | int dataLengthTx = *pSizeTx - sizeof(struct header); |
andrewboyson | 61:aad055f1b0d1 | 83 | char* pPayloadRx = (char*)pPacketRx + sizeof(struct header); |
andrewboyson | 61:aad055f1b0d1 | 84 | char* pPayloadTx = (char*)pPacketTx + sizeof(struct header); |
andrewboyson | 43:bc028d5a6424 | 85 | |
andrewboyson | 43:bc028d5a6424 | 86 | int action = DO_NOTHING; |
andrewboyson | 43:bc028d5a6424 | 87 | switch (type) |
andrewboyson | 43:bc028d5a6424 | 88 | { |
andrewboyson | 43:bc028d5a6424 | 89 | case ECHO_REQUEST: |
andrewboyson | 59:e0e556c8bd46 | 90 | action = Echo4HandleRequest(trace, &type, &code, pPayloadRx, dataLengthRx, pPayloadTx, &dataLengthTx); |
andrewboyson | 43:bc028d5a6424 | 91 | break; |
andrewboyson | 43:bc028d5a6424 | 92 | case UNREACHABLE: |
andrewboyson | 43:bc028d5a6424 | 93 | return DO_NOTHING; |
andrewboyson | 43:bc028d5a6424 | 94 | case REDIRECT: |
andrewboyson | 43:bc028d5a6424 | 95 | return DO_NOTHING; |
andrewboyson | 43:bc028d5a6424 | 96 | default: |
andrewboyson | 43:bc028d5a6424 | 97 | LogTimeF("ICMP4 packet type %d unknown\r\n", type); |
andrewboyson | 43:bc028d5a6424 | 98 | return DO_NOTHING; |
andrewboyson | 43:bc028d5a6424 | 99 | } |
andrewboyson | 43:bc028d5a6424 | 100 | if (!action) return DO_NOTHING; |
andrewboyson | 43:bc028d5a6424 | 101 | |
andrewboyson | 43:bc028d5a6424 | 102 | *pDstIp = *pSrcIp; |
andrewboyson | 43:bc028d5a6424 | 103 | *pSrcIp = DhcpLocalIp; |
andrewboyson | 43:bc028d5a6424 | 104 | |
andrewboyson | 61:aad055f1b0d1 | 105 | *pSizeTx = sizeof(struct header) + dataLengthTx; |
andrewboyson | 43:bc028d5a6424 | 106 | |
andrewboyson | 59:e0e556c8bd46 | 107 | writeHeader(pPacketTx, *pSizeTx); |
andrewboyson | 43:bc028d5a6424 | 108 | |
andrewboyson | 43:bc028d5a6424 | 109 | if (ActionGetTracePart(action)) logHeader(); |
andrewboyson | 43:bc028d5a6424 | 110 | |
andrewboyson | 43:bc028d5a6424 | 111 | return action; |
andrewboyson | 43:bc028d5a6424 | 112 | } |