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

Committer:
andrewboyson
Date:
Thu May 20 14:32:52 2021 +0000
Revision:
200:5acbc41bf469
Parent:
187:122fc1996c86
Increased number of arp entries from 20 to 30 to accommodate the number of WIZ devices plus a few incoming port 80 calls from the internet.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 136:8a65abb0dc63 1
andrewboyson 61:aad055f1b0d1 2 #include <stdint.h>
andrewboyson 61:aad055f1b0d1 3 #include <stdbool.h>
andrewboyson 61:aad055f1b0d1 4
andrewboyson 136:8a65abb0dc63 5 #include "log.h"
andrewboyson 37:793b39683406 6 #include "action.h"
andrewboyson 136:8a65abb0dc63 7 #include "net.h"
andrewboyson 136:8a65abb0dc63 8 #include "eth.h"
andrewboyson 136:8a65abb0dc63 9 #include "mac.h"
andrewboyson 136:8a65abb0dc63 10 #include "dhcp.h"
andrewboyson 136:8a65abb0dc63 11 #include "ar4.h"
andrewboyson 172:9bc3c7b2cca1 12 #include "nr.h"
andrewboyson 49:1a6336f2b3f9 13 #include "ip4addr.h"
andrewboyson 136:8a65abb0dc63 14 #include "arphdr.h"
andrewboyson 10:f0854784e960 15
andrewboyson 10:f0854784e960 16 #define REQUEST 1
andrewboyson 10:f0854784e960 17 #define REPLY 2
andrewboyson 10:f0854784e960 18
andrewboyson 43:bc028d5a6424 19 bool ArpTrace = false;
andrewboyson 43:bc028d5a6424 20
andrewboyson 22:914b970356f0 21 uint32_t ArpAddressToResolve;
andrewboyson 22:914b970356f0 22 bool ArpResolveRequestFlag = false;
andrewboyson 22:914b970356f0 23
andrewboyson 136:8a65abb0dc63 24 static void logHeader(char* pPacket)
andrewboyson 48:952dddb74b8b 25 {
andrewboyson 43:bc028d5a6424 26 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 27 {
andrewboyson 43:bc028d5a6424 28 LogTime("ARP header\r\n");
andrewboyson 136:8a65abb0dc63 29 if (ArpHdrGetHardwareType(pPacket) == ETHERNET) Log (" hardwareType = ETHERNET\r\n");
andrewboyson 136:8a65abb0dc63 30 else LogF(" hardwareType = %d\r\n", ArpHdrGetHardwareType(pPacket));
andrewboyson 136:8a65abb0dc63 31 Log (" protocolType = "); EthProtocolLog(ArpHdrGetProtocolType (pPacket)); Log("\r\n");
andrewboyson 136:8a65abb0dc63 32 LogF(" hardwareLength = %d\r\n", ArpHdrGetHardwareLength(pPacket));
andrewboyson 136:8a65abb0dc63 33 LogF(" protocolLength = %d\r\n", ArpHdrGetProtocolLength(pPacket));
andrewboyson 136:8a65abb0dc63 34 if (ArpHdrGetOpCode(pPacket) == REQUEST) Log (" opCode = REQUEST\r\n");
andrewboyson 136:8a65abb0dc63 35 else if (ArpHdrGetOpCode(pPacket) == REPLY ) Log (" opCode = REPLY\r\n");
andrewboyson 136:8a65abb0dc63 36 else LogF(" opCode = %d\r\n", ArpHdrGetOpCode(pPacket));
andrewboyson 187:122fc1996c86 37 Log(" senderHardwareAddress = "); MacLog(ArpHdrPtrSenderHardwareAddr(pPacket)); Log("\r\n");
andrewboyson 187:122fc1996c86 38 Log(" senderProtocolAddress = "); Ip4AddrLog(ArpHdrGetSenderProtocolAddr(pPacket)); Log("\r\n");
andrewboyson 187:122fc1996c86 39 Log(" targetHardwareAddress = "); MacLog(ArpHdrPtrTargetHardwareAddr(pPacket)); Log("\r\n");
andrewboyson 187:122fc1996c86 40 Log(" targetProtocolAddress = "); Ip4AddrLog(ArpHdrGetTargetProtocolAddr(pPacket)); Log("\r\n");
andrewboyson 43:bc028d5a6424 41 }
andrewboyson 43:bc028d5a6424 42 else
andrewboyson 43:bc028d5a6424 43 {
andrewboyson 43:bc028d5a6424 44 Log("ARP header ");
andrewboyson 187:122fc1996c86 45 MacLog(ArpHdrPtrSenderHardwareAddr(pPacket)); Log("==");
andrewboyson 187:122fc1996c86 46 Ip4AddrLog(ArpHdrGetSenderProtocolAddr(pPacket)); Log(" >>> ");
andrewboyson 187:122fc1996c86 47 MacLog(ArpHdrPtrTargetHardwareAddr(pPacket)); Log("==");
andrewboyson 187:122fc1996c86 48 Ip4AddrLog(ArpHdrGetTargetProtocolAddr(pPacket)); Log("\r\n");
andrewboyson 43:bc028d5a6424 49
andrewboyson 43:bc028d5a6424 50 }
andrewboyson 43:bc028d5a6424 51 }
andrewboyson 136:8a65abb0dc63 52 static char* pHeaderTrace;
andrewboyson 48:952dddb74b8b 53 static void (*pTraceBack)(void);
andrewboyson 48:952dddb74b8b 54 static void trace()
andrewboyson 48:952dddb74b8b 55 {
andrewboyson 48:952dddb74b8b 56 pTraceBack();
andrewboyson 59:e0e556c8bd46 57 logHeader(pHeaderTrace);
andrewboyson 48:952dddb74b8b 58 }
andrewboyson 43:bc028d5a6424 59
andrewboyson 136:8a65abb0dc63 60 int ArpHandleReceivedPacket(void (*traceback)(void), char* pPacketRx, int sizeRx, char* pPacketTx, int* pSizeTx)
andrewboyson 10:f0854784e960 61 {
andrewboyson 48:952dddb74b8b 62 pTraceBack = traceback;
andrewboyson 136:8a65abb0dc63 63 pHeaderTrace = pPacketRx;
andrewboyson 59:e0e556c8bd46 64
andrewboyson 136:8a65abb0dc63 65 int16_t hardwareType = ArpHdrGetHardwareType (pPacketRx);
andrewboyson 136:8a65abb0dc63 66 int16_t protocolType = ArpHdrGetProtocolType (pPacketRx);
andrewboyson 136:8a65abb0dc63 67 int8_t hardwareLength = ArpHdrGetHardwareLength (pPacketRx);
andrewboyson 136:8a65abb0dc63 68 int8_t protocolLength = ArpHdrGetProtocolLength (pPacketRx);
andrewboyson 136:8a65abb0dc63 69 int16_t opCode = ArpHdrGetOpCode (pPacketRx);
andrewboyson 136:8a65abb0dc63 70 uint32_t targetProtocolAddress = ArpHdrGetTargetProtocolAddr(pPacketRx);
andrewboyson 48:952dddb74b8b 71
andrewboyson 10:f0854784e960 72 if (hardwareType != ETHERNET ) return DO_NOTHING; //This is not ethernet
andrewboyson 172:9bc3c7b2cca1 73 if (protocolType != ETH_IPV4 ) return DO_NOTHING; //This is not IPv4
andrewboyson 10:f0854784e960 74 if (hardwareLength != 6 ) return DO_NOTHING; //This is not a MAC hardware address
andrewboyson 10:f0854784e960 75 if (protocolLength != 4 ) return DO_NOTHING; //This is not an IPv4 IP address
andrewboyson 10:f0854784e960 76 if (targetProtocolAddress != DhcpLocalIp ) return DO_NOTHING; //This packet was not addressed to us
andrewboyson 10:f0854784e960 77
andrewboyson 22:914b970356f0 78 switch (opCode)
andrewboyson 22:914b970356f0 79 {
andrewboyson 22:914b970356f0 80 case REQUEST:
andrewboyson 43:bc028d5a6424 81 if (ArpTrace)
andrewboyson 43:bc028d5a6424 82 {
andrewboyson 43:bc028d5a6424 83 if (NetTraceNewLine) Log("\r\n");
andrewboyson 43:bc028d5a6424 84 LogTime("ARP received request\r\n");
andrewboyson 44:83ce5ace337b 85 if (NetTraceStack) traceback();
andrewboyson 136:8a65abb0dc63 86 logHeader(pPacketRx);
andrewboyson 43:bc028d5a6424 87 }
andrewboyson 136:8a65abb0dc63 88 ArpHdrSetHardwareType (pPacketTx, ETHERNET );
andrewboyson 172:9bc3c7b2cca1 89 ArpHdrSetProtocolType (pPacketTx, ETH_IPV4 );
andrewboyson 136:8a65abb0dc63 90 ArpHdrSetHardwareLength (pPacketTx, 6 );
andrewboyson 136:8a65abb0dc63 91 ArpHdrSetProtocolLength (pPacketTx, 4 );
andrewboyson 136:8a65abb0dc63 92 ArpHdrSetOpCode (pPacketTx, REPLY );
andrewboyson 136:8a65abb0dc63 93 MacCopy(ArpHdrPtrTargetHardwareAddr(pPacketTx), ArpHdrPtrSenderHardwareAddr(pPacketRx));
andrewboyson 136:8a65abb0dc63 94 ArpHdrSetTargetProtocolAddr(pPacketTx, ArpHdrGetSenderProtocolAddr(pPacketRx));
andrewboyson 136:8a65abb0dc63 95 MacCopy(ArpHdrPtrSenderHardwareAddr(pPacketTx), MacLocal );
andrewboyson 136:8a65abb0dc63 96 ArpHdrSetSenderProtocolAddr(pPacketTx, DhcpLocalIp );
andrewboyson 136:8a65abb0dc63 97 *pSizeTx = ARP_HEADER_LENGTH;
andrewboyson 136:8a65abb0dc63 98 if (ArpTrace) logHeader(pPacketTx);
andrewboyson 43:bc028d5a6424 99 return ActionMakeFromDestAndTrace(UNICAST, ArpTrace && NetTraceStack);
andrewboyson 43:bc028d5a6424 100
andrewboyson 22:914b970356f0 101 case REPLY:
andrewboyson 43:bc028d5a6424 102 if (ArpTrace)
andrewboyson 43:bc028d5a6424 103 {
andrewboyson 43:bc028d5a6424 104 if (NetTraceNewLine) Log("\r\n");
andrewboyson 43:bc028d5a6424 105 LogTime("ARP received reply\r\n");
andrewboyson 44:83ce5ace337b 106 if (NetTraceStack) traceback();
andrewboyson 136:8a65abb0dc63 107 logHeader(pPacketRx);
andrewboyson 43:bc028d5a6424 108 }
andrewboyson 136:8a65abb0dc63 109 Ar4AddIpRecord(trace, ArpHdrPtrSenderHardwareAddr(pPacketRx), ArpHdrGetSenderProtocolAddr(pPacketRx));
andrewboyson 172:9bc3c7b2cca1 110 NrMakeRequestForNameFromAddress4(ArpHdrGetSenderProtocolAddr(pPacketRx));
andrewboyson 22:914b970356f0 111 return DO_NOTHING;
andrewboyson 43:bc028d5a6424 112
andrewboyson 22:914b970356f0 113 default:
andrewboyson 22:914b970356f0 114 return DO_NOTHING;
andrewboyson 22:914b970356f0 115 }
andrewboyson 22:914b970356f0 116 }
andrewboyson 136:8a65abb0dc63 117 int ArpPollForPacketToSend(char* pPacketTx, int* pSizeTx)
andrewboyson 22:914b970356f0 118 {
andrewboyson 22:914b970356f0 119 if (!ArpResolveRequestFlag) return DO_NOTHING;
andrewboyson 22:914b970356f0 120 ArpResolveRequestFlag = false;
andrewboyson 48:952dddb74b8b 121
andrewboyson 136:8a65abb0dc63 122 ArpHdrSetHardwareType (pPacketTx, ETHERNET );
andrewboyson 172:9bc3c7b2cca1 123 ArpHdrSetProtocolType (pPacketTx, ETH_IPV4 );
andrewboyson 136:8a65abb0dc63 124 ArpHdrSetHardwareLength (pPacketTx, 6 );
andrewboyson 136:8a65abb0dc63 125 ArpHdrSetProtocolLength (pPacketTx, 4 );
andrewboyson 136:8a65abb0dc63 126 ArpHdrSetOpCode (pPacketTx, REQUEST );
andrewboyson 136:8a65abb0dc63 127 MacClear(ArpHdrPtrTargetHardwareAddr(pPacketTx) );
andrewboyson 136:8a65abb0dc63 128 ArpHdrSetTargetProtocolAddr(pPacketTx, ArpAddressToResolve);
andrewboyson 136:8a65abb0dc63 129 MacCopy (ArpHdrPtrSenderHardwareAddr(pPacketTx), MacLocal );
andrewboyson 136:8a65abb0dc63 130 ArpHdrSetSenderProtocolAddr(pPacketTx, DhcpLocalIp );
andrewboyson 136:8a65abb0dc63 131 *pSizeTx = ARP_HEADER_LENGTH;
andrewboyson 10:f0854784e960 132
andrewboyson 43:bc028d5a6424 133 if (ArpTrace)
andrewboyson 43:bc028d5a6424 134 {
andrewboyson 43:bc028d5a6424 135 if (NetTraceNewLine) Log("\r\n");
andrewboyson 43:bc028d5a6424 136 LogTime("ARP send request\r\n");
andrewboyson 136:8a65abb0dc63 137 logHeader(pPacketTx);
andrewboyson 43:bc028d5a6424 138 }
andrewboyson 43:bc028d5a6424 139 return ActionMakeFromDestAndTrace(BROADCAST, ArpTrace && NetTraceStack);
andrewboyson 35:93c39d260a83 140 }