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
Diff: arp/arp.c
- Revision:
- 136:8a65abb0dc63
- Parent:
- 61:aad055f1b0d1
- Child:
- 172:9bc3c7b2cca1
--- a/arp/arp.c Sat Mar 23 12:25:48 2019 +0000
+++ b/arp/arp.c Sat Apr 06 11:20:20 2019 +0000
@@ -1,15 +1,17 @@
+
#include <stdint.h>
#include <stdbool.h>
-#include "log.h"
+#include "log.h"
#include "action.h"
-#include "net.h"
-#include "eth.h"
-#include "mac.h"
-#include "dhcp.h"
-#include "ar4.h"
-#include "nr4.h"
+#include "net.h"
+#include "eth.h"
+#include "mac.h"
+#include "dhcp.h"
+#include "ar4.h"
+#include "nr4.h"
#include "ip4addr.h"
+#include "arphdr.h"
#define REQUEST 1
#define REPLY 2
@@ -19,48 +21,35 @@
uint32_t ArpAddressToResolve;
bool ArpResolveRequestFlag = false;
-__packed struct header
-{
- int16_t hardwareType; //16.bit: (ar$hrd) Hardware address space (e.g., Ethernet, Packet Radio Net). Always 1.
- int16_t protocolType; //16.bit: (ar$pro) Protocol address space. For Ethernet hardware, this is from the set of type fields ether_typ$<protocol>. As held in eth.h, eg IPv4 = 0x8000.
- int8_t hardwareLength; // 8.bit: (ar$hln) byte length of each hardware address. Always 6 bytes.
- int8_t protocolLength; // 8.bit: (ar$pln) byte length of each protocol address. Always 4 bytes.
- int16_t opCode; //16.bit: (ar$op) opcode (ares_op$REQUEST = 1 | ares_op$REPLY = 2), high byte transmitted first.
- char senderHardwareAddress[6]; //nbytes: (ar$sha) Hardware address of sender of this packet, n from the ar$hln field.
- uint32_t senderProtocolAddress; //mbytes: (ar$spa) Protocol address of sender of this packet, m from the ar$pln field.
- char targetHardwareAddress[6]; //nbytes: (ar$tha) Hardware address of target of this packet (if known).
- uint32_t targetProtocolAddress; //mbytes: (ar$tpa) Protocol address of target.
-};
-
-static void logHeader(struct header* pHeader)
+static void logHeader(char* pPacket)
{
if (NetTraceVerbose)
{
LogTime("ARP header\r\n");
- if (NetToHost16(pHeader->hardwareType) == ETHERNET) Log (" hardwareType = ETHERNET\r\n");
- else LogF(" hardwareType = %d\r\n", NetToHost16(pHeader->hardwareType));
- Log (" protocolType = "); EthProtocolLog(pHeader->protocolType); Log("\r\n");
- LogF(" hardwareLength = %d\r\n", pHeader->hardwareLength);
- LogF(" protocolLength = %d\r\n", pHeader->protocolLength);
- if (NetToHost16(pHeader->opCode) == REQUEST) Log (" opCode = REQUEST\r\n");
- else if (NetToHost16(pHeader->opCode) == REPLY ) Log (" opCode = REPLY\r\n");
- else LogF(" opCode = %d\r\n", NetToHost16(pHeader->opCode));
- Log(" senderHardwareAddress = "); MacLog(pHeader->senderHardwareAddress); Log("\r\n");
- Log(" senderProtocolAddress = "); Ip4AddressLog(pHeader->senderProtocolAddress); Log("\r\n");
- Log(" targetHardwareAddress = "); MacLog(pHeader->targetHardwareAddress); Log("\r\n");
- Log(" targetProtocolAddress = "); Ip4AddressLog(pHeader->targetProtocolAddress); Log("\r\n");
+ if (ArpHdrGetHardwareType(pPacket) == ETHERNET) Log (" hardwareType = ETHERNET\r\n");
+ else LogF(" hardwareType = %d\r\n", ArpHdrGetHardwareType(pPacket));
+ Log (" protocolType = "); EthProtocolLog(ArpHdrGetProtocolType (pPacket)); Log("\r\n");
+ LogF(" hardwareLength = %d\r\n", ArpHdrGetHardwareLength(pPacket));
+ LogF(" protocolLength = %d\r\n", ArpHdrGetProtocolLength(pPacket));
+ if (ArpHdrGetOpCode(pPacket) == REQUEST) Log (" opCode = REQUEST\r\n");
+ else if (ArpHdrGetOpCode(pPacket) == REPLY ) Log (" opCode = REPLY\r\n");
+ else LogF(" opCode = %d\r\n", ArpHdrGetOpCode(pPacket));
+ Log(" senderHardwareAddress = "); MacLog(ArpHdrPtrSenderHardwareAddr(pPacket)); Log("\r\n");
+ Log(" senderProtocolAddress = "); Ip4AddressLog(ArpHdrGetSenderProtocolAddr(pPacket)); Log("\r\n");
+ Log(" targetHardwareAddress = "); MacLog(ArpHdrPtrTargetHardwareAddr(pPacket)); Log("\r\n");
+ Log(" targetProtocolAddress = "); Ip4AddressLog(ArpHdrGetTargetProtocolAddr(pPacket)); Log("\r\n");
}
else
{
Log("ARP header ");
- MacLog(pHeader->senderHardwareAddress); Log("==");
- Ip4AddressLog(pHeader->senderProtocolAddress); Log(" >>> ");
- MacLog(pHeader->targetHardwareAddress); Log("==");
- Ip4AddressLog(pHeader->targetProtocolAddress); Log("\r\n");
+ MacLog(ArpHdrPtrSenderHardwareAddr(pPacket)); Log("==");
+ Ip4AddressLog(ArpHdrGetSenderProtocolAddr(pPacket)); Log(" >>> ");
+ MacLog(ArpHdrPtrTargetHardwareAddr(pPacket)); Log("==");
+ Ip4AddressLog(ArpHdrGetTargetProtocolAddr(pPacket)); Log("\r\n");
}
}
-static struct header* pHeaderTrace;
+static char* pHeaderTrace;
static void (*pTraceBack)(void);
static void trace()
{
@@ -68,19 +57,17 @@
logHeader(pHeaderTrace);
}
-int ArpHandleReceivedPacket(void (*traceback)(void), void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx)
+int ArpHandleReceivedPacket(void (*traceback)(void), char* pPacketRx, int sizeRx, char* pPacketTx, int* pSizeTx)
{
pTraceBack = traceback;
- struct header* pHeaderRx = (struct header*)pPacketRx;
- struct header* pHeaderTx = (struct header*)pPacketTx;
- pHeaderTrace = pHeaderRx;
+ pHeaderTrace = pPacketRx;
- int16_t hardwareType = NetToHost16(pHeaderRx->hardwareType);
- int16_t protocolType = NetToHost16(pHeaderRx->protocolType);
- int8_t hardwareLength = pHeaderRx->hardwareLength;
- int8_t protocolLength = pHeaderRx->protocolLength;
- int16_t opCode = NetToHost16(pHeaderRx->opCode);
- uint32_t targetProtocolAddress = pHeaderRx->targetProtocolAddress;
+ int16_t hardwareType = ArpHdrGetHardwareType (pPacketRx);
+ int16_t protocolType = ArpHdrGetProtocolType (pPacketRx);
+ int8_t hardwareLength = ArpHdrGetHardwareLength (pPacketRx);
+ int8_t protocolLength = ArpHdrGetProtocolLength (pPacketRx);
+ int16_t opCode = ArpHdrGetOpCode (pPacketRx);
+ uint32_t targetProtocolAddress = ArpHdrGetTargetProtocolAddr(pPacketRx);
if (hardwareType != ETHERNET ) return DO_NOTHING; //This is not ethernet
if (protocolType != IPV4 ) return DO_NOTHING; //This is not IPv4
@@ -96,19 +83,19 @@
if (NetTraceNewLine) Log("\r\n");
LogTime("ARP received request\r\n");
if (NetTraceStack) traceback();
- logHeader(pHeaderRx);
+ logHeader(pPacketRx);
}
- pHeaderTx->hardwareType = NetToHost16(ETHERNET);
- pHeaderTx->protocolType = NetToHost16(IPV4);
- pHeaderTx->hardwareLength = 6;
- pHeaderTx->protocolLength = 4;
- pHeaderTx->opCode = NetToHost16(REPLY);
- MacCopy(pHeaderTx->targetHardwareAddress, pHeaderRx->senderHardwareAddress);
- pHeaderTx->targetProtocolAddress = pHeaderRx->senderProtocolAddress;
- MacCopy(pHeaderTx->senderHardwareAddress, MacLocal);
- pHeaderTx->senderProtocolAddress = DhcpLocalIp;
- *pSizeTx = sizeof(struct header);
- if (ArpTrace) logHeader(pHeaderTx);
+ ArpHdrSetHardwareType (pPacketTx, ETHERNET );
+ ArpHdrSetProtocolType (pPacketTx, IPV4 );
+ ArpHdrSetHardwareLength (pPacketTx, 6 );
+ ArpHdrSetProtocolLength (pPacketTx, 4 );
+ ArpHdrSetOpCode (pPacketTx, REPLY );
+ MacCopy(ArpHdrPtrTargetHardwareAddr(pPacketTx), ArpHdrPtrSenderHardwareAddr(pPacketRx));
+ ArpHdrSetTargetProtocolAddr(pPacketTx, ArpHdrGetSenderProtocolAddr(pPacketRx));
+ MacCopy(ArpHdrPtrSenderHardwareAddr(pPacketTx), MacLocal );
+ ArpHdrSetSenderProtocolAddr(pPacketTx, DhcpLocalIp );
+ *pSizeTx = ARP_HEADER_LENGTH;
+ if (ArpTrace) logHeader(pPacketTx);
return ActionMakeFromDestAndTrace(UNICAST, ArpTrace && NetTraceStack);
case REPLY:
@@ -117,41 +104,37 @@
if (NetTraceNewLine) Log("\r\n");
LogTime("ARP received reply\r\n");
if (NetTraceStack) traceback();
- logHeader(pHeaderRx);
+ logHeader(pPacketRx);
}
- Ar4AddIpRecord(trace, pHeaderRx->senderHardwareAddress, pHeaderRx->senderProtocolAddress);
- Nr4MakeRequestForNameFromIp(pHeaderRx->senderProtocolAddress);
+ Ar4AddIpRecord(trace, ArpHdrPtrSenderHardwareAddr(pPacketRx), ArpHdrGetSenderProtocolAddr(pPacketRx));
+ Nr4MakeRequestForNameFromIp(ArpHdrGetSenderProtocolAddr(pPacketRx));
return DO_NOTHING;
default:
return DO_NOTHING;
}
}
-int ArpPollForPacketToSend(void* pPacketTx, int* pSizeTx)
+int ArpPollForPacketToSend(char* pPacketTx, int* pSizeTx)
{
if (!ArpResolveRequestFlag) return DO_NOTHING;
ArpResolveRequestFlag = false;
- struct header* pHeaderTx = (struct header*)pPacketTx;
-
- pHeaderTx->hardwareType = NetToHost16(ETHERNET);
- pHeaderTx->protocolType = NetToHost16(IPV4);
- pHeaderTx->hardwareLength = 6;
- pHeaderTx->protocolLength = 4;
- pHeaderTx->opCode = NetToHost16(REQUEST);
-
- MacClear(pHeaderTx->targetHardwareAddress);
- pHeaderTx->targetProtocolAddress = ArpAddressToResolve;
- MacCopy (pHeaderTx->senderHardwareAddress, MacLocal);
- pHeaderTx->senderProtocolAddress = DhcpLocalIp;
-
- *pSizeTx = sizeof(struct header);
+ ArpHdrSetHardwareType (pPacketTx, ETHERNET );
+ ArpHdrSetProtocolType (pPacketTx, IPV4 );
+ ArpHdrSetHardwareLength (pPacketTx, 6 );
+ ArpHdrSetProtocolLength (pPacketTx, 4 );
+ ArpHdrSetOpCode (pPacketTx, REQUEST );
+ MacClear(ArpHdrPtrTargetHardwareAddr(pPacketTx) );
+ ArpHdrSetTargetProtocolAddr(pPacketTx, ArpAddressToResolve);
+ MacCopy (ArpHdrPtrSenderHardwareAddr(pPacketTx), MacLocal );
+ ArpHdrSetSenderProtocolAddr(pPacketTx, DhcpLocalIp );
+ *pSizeTx = ARP_HEADER_LENGTH;
if (ArpTrace)
{
if (NetTraceNewLine) Log("\r\n");
LogTime("ARP send request\r\n");
- logHeader(pHeaderTx);
+ logHeader(pPacketTx);
}
return ActionMakeFromDestAndTrace(BROADCAST, ArpTrace && NetTraceStack);
}