Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
ip4/icmp/icmp4.cpp@59:e0e556c8bd46, 2017-12-14 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Dec 14 20:55:40 2017 +0000
- Revision:
- 59:e0e556c8bd46
- Parent:
- 52:fbc5a46b5e16
Added buffer length to help avoid over runs
Who changed what in which revision?
| User | Revision | Line number | New 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 ECHO_REPLY 0 |
| andrewboyson | 43:bc028d5a6424 | 10 | #define UNREACHABLE 3 |
| andrewboyson | 43:bc028d5a6424 | 11 | #define REDIRECT 5 |
| andrewboyson | 43:bc028d5a6424 | 12 | #define ECHO_REQUEST 8 |
| andrewboyson | 43:bc028d5a6424 | 13 | |
| andrewboyson | 43:bc028d5a6424 | 14 | __packed struct header |
| andrewboyson | 43:bc028d5a6424 | 15 | { |
| andrewboyson | 43:bc028d5a6424 | 16 | uint8_t type; |
| andrewboyson | 43:bc028d5a6424 | 17 | uint8_t code; |
| andrewboyson | 43:bc028d5a6424 | 18 | uint16_t checksum; |
| andrewboyson | 43:bc028d5a6424 | 19 | }; |
| andrewboyson | 43:bc028d5a6424 | 20 | static uint8_t type; |
| andrewboyson | 43:bc028d5a6424 | 21 | static uint8_t code; |
| andrewboyson | 43:bc028d5a6424 | 22 | static uint16_t checksum; |
| andrewboyson | 43:bc028d5a6424 | 23 | static uint16_t calculated; |
| andrewboyson | 43:bc028d5a6424 | 24 | |
| andrewboyson | 47:73af5c0b0dc2 | 25 | static void logType(uint16_t type) |
| andrewboyson | 43:bc028d5a6424 | 26 | { |
| andrewboyson | 43:bc028d5a6424 | 27 | switch (type) |
| andrewboyson | 43:bc028d5a6424 | 28 | { |
| andrewboyson | 47:73af5c0b0dc2 | 29 | case ECHO_REPLY: Log ("Echo Reply" ); break; |
| andrewboyson | 47:73af5c0b0dc2 | 30 | case ECHO_REQUEST: Log ("Echo Request" ); break; |
| andrewboyson | 47:73af5c0b0dc2 | 31 | default: LogF("Unknown type %d", type); break; |
| andrewboyson | 43:bc028d5a6424 | 32 | } |
| andrewboyson | 43:bc028d5a6424 | 33 | } |
| andrewboyson | 43:bc028d5a6424 | 34 | static void logHeader() |
| andrewboyson | 47:73af5c0b0dc2 | 35 | { |
| andrewboyson | 43:bc028d5a6424 | 36 | if (NetTraceVerbose) |
| andrewboyson | 43:bc028d5a6424 | 37 | { |
| andrewboyson | 43:bc028d5a6424 | 38 | Log ("ICMP4 header\r\n"); |
| andrewboyson | 47:73af5c0b0dc2 | 39 | LogF(" Type "); logType(type); Log("\r\n"); |
| andrewboyson | 43:bc028d5a6424 | 40 | LogF(" Code %u\r\n", code); |
| andrewboyson | 43:bc028d5a6424 | 41 | LogF(" Checksum (hex) %04hX\r\n", checksum); |
| andrewboyson | 43:bc028d5a6424 | 42 | LogF(" Calculated %04hX\r\n", calculated); |
| andrewboyson | 43:bc028d5a6424 | 43 | } |
| andrewboyson | 43:bc028d5a6424 | 44 | else |
| andrewboyson | 43:bc028d5a6424 | 45 | { |
| andrewboyson | 43:bc028d5a6424 | 46 | Log("ICMP4 header "); |
| andrewboyson | 47:73af5c0b0dc2 | 47 | logType(type); |
| andrewboyson | 43:bc028d5a6424 | 48 | Log("\r\n"); |
| andrewboyson | 43:bc028d5a6424 | 49 | } |
| andrewboyson | 43:bc028d5a6424 | 50 | } |
| andrewboyson | 43:bc028d5a6424 | 51 | static void readHeader(void* pPacket, int size) |
| andrewboyson | 43:bc028d5a6424 | 52 | { |
| andrewboyson | 43:bc028d5a6424 | 53 | struct header* pHeader = (header*)pPacket; |
| andrewboyson | 43:bc028d5a6424 | 54 | type = pHeader->type; |
| andrewboyson | 43:bc028d5a6424 | 55 | code = pHeader->code; |
| andrewboyson | 43:bc028d5a6424 | 56 | checksum = NetToHost16(pHeader->checksum); |
| andrewboyson | 43:bc028d5a6424 | 57 | calculated = NetCheckSum(size, pPacket); |
| andrewboyson | 43:bc028d5a6424 | 58 | } |
| andrewboyson | 43:bc028d5a6424 | 59 | static void writeHeader(void* pPacket, int size) |
| andrewboyson | 43:bc028d5a6424 | 60 | { |
| andrewboyson | 43:bc028d5a6424 | 61 | struct header* pHeader = (header*)pPacket; |
| andrewboyson | 43:bc028d5a6424 | 62 | pHeader->type = type; |
| andrewboyson | 43:bc028d5a6424 | 63 | pHeader->code = code; |
| andrewboyson | 43:bc028d5a6424 | 64 | pHeader->checksum = 0; |
| andrewboyson | 43:bc028d5a6424 | 65 | pHeader->checksum = NetCheckSum(size, pPacket); |
| andrewboyson | 43:bc028d5a6424 | 66 | calculated = 0; |
| andrewboyson | 43:bc028d5a6424 | 67 | } |
| andrewboyson | 43:bc028d5a6424 | 68 | static void (*pTraceBack)(void); |
| andrewboyson | 43:bc028d5a6424 | 69 | static void trace() |
| andrewboyson | 43:bc028d5a6424 | 70 | { |
| andrewboyson | 43:bc028d5a6424 | 71 | pTraceBack(); |
| andrewboyson | 43:bc028d5a6424 | 72 | logHeader(); |
| andrewboyson | 43:bc028d5a6424 | 73 | } |
| andrewboyson | 59:e0e556c8bd46 | 74 | int Icmp4HandleReceivedPacket(void (*traceback)(void), void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, uint32_t* pSrcIp, uint32_t* pDstIp) |
| andrewboyson | 43:bc028d5a6424 | 75 | { |
| andrewboyson | 43:bc028d5a6424 | 76 | pTraceBack = traceback; |
| andrewboyson | 43:bc028d5a6424 | 77 | |
| andrewboyson | 59:e0e556c8bd46 | 78 | readHeader(pPacketRx, sizeRx); |
| andrewboyson | 59:e0e556c8bd46 | 79 | |
| andrewboyson | 59:e0e556c8bd46 | 80 | int dataLengthRx = sizeRx - sizeof(header); |
| andrewboyson | 59:e0e556c8bd46 | 81 | int dataLengthTx = *pSizeTx - sizeof(header); |
| andrewboyson | 59:e0e556c8bd46 | 82 | char* pPayloadRx = (char*)pPacketRx + sizeof(header); |
| andrewboyson | 59:e0e556c8bd46 | 83 | char* pPayloadTx = (char*)pPacketTx + sizeof(header); |
| andrewboyson | 43:bc028d5a6424 | 84 | |
| andrewboyson | 43:bc028d5a6424 | 85 | int action = DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 86 | switch (type) |
| andrewboyson | 43:bc028d5a6424 | 87 | { |
| andrewboyson | 43:bc028d5a6424 | 88 | case ECHO_REQUEST: |
| andrewboyson | 59:e0e556c8bd46 | 89 | action = Echo4HandleRequest(trace, &type, &code, pPayloadRx, dataLengthRx, pPayloadTx, &dataLengthTx); |
| andrewboyson | 43:bc028d5a6424 | 90 | break; |
| andrewboyson | 43:bc028d5a6424 | 91 | case UNREACHABLE: |
| andrewboyson | 43:bc028d5a6424 | 92 | return DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 93 | case REDIRECT: |
| andrewboyson | 43:bc028d5a6424 | 94 | return DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 95 | default: |
| andrewboyson | 43:bc028d5a6424 | 96 | LogTimeF("ICMP4 packet type %d unknown\r\n", type); |
| andrewboyson | 43:bc028d5a6424 | 97 | return DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 98 | } |
| andrewboyson | 43:bc028d5a6424 | 99 | if (!action) return DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 100 | |
| andrewboyson | 43:bc028d5a6424 | 101 | *pDstIp = *pSrcIp; |
| andrewboyson | 43:bc028d5a6424 | 102 | *pSrcIp = DhcpLocalIp; |
| andrewboyson | 43:bc028d5a6424 | 103 | |
| andrewboyson | 59:e0e556c8bd46 | 104 | *pSizeTx = sizeof(header) + dataLengthTx; |
| andrewboyson | 43:bc028d5a6424 | 105 | |
| andrewboyson | 59:e0e556c8bd46 | 106 | writeHeader(pPacketTx, *pSizeTx); |
| andrewboyson | 43:bc028d5a6424 | 107 | |
| andrewboyson | 43:bc028d5a6424 | 108 | if (ActionGetTracePart(action)) logHeader(); |
| andrewboyson | 43:bc028d5a6424 | 109 | |
| andrewboyson | 43:bc028d5a6424 | 110 | return action; |
| andrewboyson | 43:bc028d5a6424 | 111 | } |