Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Sun Oct 15 17:54:09 2017 +0000
Revision:
42:222a4f45f916
Parent:
37:793b39683406
Child:
43:bc028d5a6424
Made the use of IPv4 or IPv6 for client requests a configurable option

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 37:793b39683406 1 #include "mbed.h"
andrewboyson 37:793b39683406 2 #include "log.h"
andrewboyson 37:793b39683406 3 #include "action.h"
andrewboyson 37:793b39683406 4 #include "net.h"
andrewboyson 37:793b39683406 5 #include "eth.h"
andrewboyson 37:793b39683406 6 #include "mac.h"
andrewboyson 37:793b39683406 7 #include "dhcp.h"
andrewboyson 37:793b39683406 8 #include "ar.h"
andrewboyson 37:793b39683406 9 #include "nr.h"
andrewboyson 37:793b39683406 10 #include "io.h"
andrewboyson 10:f0854784e960 11
andrewboyson 10:f0854784e960 12 #define REQUEST 1
andrewboyson 10:f0854784e960 13 #define REPLY 2
andrewboyson 10:f0854784e960 14
andrewboyson 22:914b970356f0 15 uint32_t ArpAddressToResolve;
andrewboyson 22:914b970356f0 16 bool ArpResolveRequestFlag = false;
andrewboyson 22:914b970356f0 17
andrewboyson 10:f0854784e960 18 __packed struct header
andrewboyson 10:f0854784e960 19 {
andrewboyson 10:f0854784e960 20 int16_t hardwareType; //16.bit: (ar$hrd) Hardware address space (e.g., Ethernet, Packet Radio Net). Always 1.
andrewboyson 10:f0854784e960 21 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.
andrewboyson 10:f0854784e960 22 int8_t hardwareLength; // 8.bit: (ar$hln) byte length of each hardware address. Always 6 bytes.
andrewboyson 10:f0854784e960 23 int8_t protocolLength; // 8.bit: (ar$pln) byte length of each protocol address. Always 4 bytes.
andrewboyson 10:f0854784e960 24 int16_t opCode; //16.bit: (ar$op) opcode (ares_op$REQUEST = 1 | ares_op$REPLY = 2), high byte transmitted first.
andrewboyson 10:f0854784e960 25 char senderHardwareAddress[6]; //nbytes: (ar$sha) Hardware address of sender of this packet, n from the ar$hln field.
andrewboyson 10:f0854784e960 26 uint32_t senderProtocolAddress; //mbytes: (ar$spa) Protocol address of sender of this packet, m from the ar$pln field.
andrewboyson 10:f0854784e960 27 char targetHardwareAddress[6]; //nbytes: (ar$tha) Hardware address of target of this packet (if known).
andrewboyson 10:f0854784e960 28 uint32_t targetProtocolAddress; //mbytes: (ar$tpa) Protocol address of target.
andrewboyson 10:f0854784e960 29 };
andrewboyson 37:793b39683406 30 int ArpHandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void * pPacket, int* pSize, char* pDstMac)
andrewboyson 10:f0854784e960 31 {
andrewboyson 10:f0854784e960 32 struct header* pHeader = (header*)pPacket;
andrewboyson 10:f0854784e960 33 int16_t hardwareType = NetToHost16(pHeader->hardwareType);
andrewboyson 10:f0854784e960 34 int16_t protocolType = NetToHost16(pHeader->protocolType);
andrewboyson 10:f0854784e960 35 int8_t hardwareLength = pHeader->hardwareLength;
andrewboyson 10:f0854784e960 36 int8_t protocolLength = pHeader->protocolLength;
andrewboyson 10:f0854784e960 37 int16_t opCode = NetToHost16(pHeader->opCode);
andrewboyson 10:f0854784e960 38 uint32_t targetProtocolAddress = pHeader->targetProtocolAddress;
andrewboyson 10:f0854784e960 39
andrewboyson 10:f0854784e960 40 if (hardwareType != ETHERNET ) return DO_NOTHING; //This is not ethernet
andrewboyson 10:f0854784e960 41 if (protocolType != IPV4 ) return DO_NOTHING; //This is not IPv4
andrewboyson 10:f0854784e960 42 if (hardwareLength != 6 ) return DO_NOTHING; //This is not a MAC hardware address
andrewboyson 10:f0854784e960 43 if (protocolLength != 4 ) return DO_NOTHING; //This is not an IPv4 IP address
andrewboyson 10:f0854784e960 44 if (targetProtocolAddress != DhcpLocalIp ) return DO_NOTHING; //This packet was not addressed to us
andrewboyson 10:f0854784e960 45
andrewboyson 22:914b970356f0 46 switch (opCode)
andrewboyson 22:914b970356f0 47 {
andrewboyson 22:914b970356f0 48 case REQUEST:
andrewboyson 36:900e24b27bfb 49 MacCopy(pHeader->targetHardwareAddress, pHeader->senderHardwareAddress);
andrewboyson 22:914b970356f0 50 pHeader->targetProtocolAddress = pHeader->senderProtocolAddress;
andrewboyson 36:900e24b27bfb 51 MacCopy(pHeader->senderHardwareAddress, MacLocal);
andrewboyson 22:914b970356f0 52 pHeader->senderProtocolAddress = DhcpLocalIp;
andrewboyson 22:914b970356f0 53 pHeader->opCode = NetToHost16(REPLY);
andrewboyson 36:900e24b27bfb 54 MacCopy(pDstMac, pSrcMac);
andrewboyson 22:914b970356f0 55 return UNICAST;
andrewboyson 22:914b970356f0 56 case REPLY:
andrewboyson 35:93c39d260a83 57 ArAddIp4Record(pHeader->senderHardwareAddress, pHeader->senderProtocolAddress);
andrewboyson 35:93c39d260a83 58 NrMakeRequestForNameFromIp4(pHeader->senderProtocolAddress);
andrewboyson 22:914b970356f0 59 return DO_NOTHING;
andrewboyson 22:914b970356f0 60 default:
andrewboyson 22:914b970356f0 61 return DO_NOTHING;
andrewboyson 22:914b970356f0 62 }
andrewboyson 22:914b970356f0 63 }
andrewboyson 22:914b970356f0 64 int ArpPollForPacketToSend(void* pPacket, int* pSize)
andrewboyson 22:914b970356f0 65 {
andrewboyson 22:914b970356f0 66 if (!ArpResolveRequestFlag) return DO_NOTHING;
andrewboyson 22:914b970356f0 67 ArpResolveRequestFlag = false;
andrewboyson 22:914b970356f0 68
andrewboyson 22:914b970356f0 69 struct header* pHeader = (header*)pPacket;
andrewboyson 22:914b970356f0 70
andrewboyson 22:914b970356f0 71 pHeader->hardwareType = NetToHost16(ETHERNET);
andrewboyson 22:914b970356f0 72 pHeader->protocolType = NetToHost16(IPV4);
andrewboyson 22:914b970356f0 73 pHeader->hardwareLength = 6;
andrewboyson 22:914b970356f0 74 pHeader->protocolLength = 4;
andrewboyson 22:914b970356f0 75 pHeader->opCode = NetToHost16(REQUEST);
andrewboyson 22:914b970356f0 76
andrewboyson 36:900e24b27bfb 77 MacClear(pHeader->targetHardwareAddress);
andrewboyson 36:900e24b27bfb 78 pHeader->targetProtocolAddress = ArpAddressToResolve;
andrewboyson 36:900e24b27bfb 79 MacCopy(pHeader->senderHardwareAddress, MacLocal);
andrewboyson 10:f0854784e960 80 pHeader->senderProtocolAddress = DhcpLocalIp;
andrewboyson 22:914b970356f0 81
andrewboyson 22:914b970356f0 82 *pSize = sizeof(header);
andrewboyson 10:f0854784e960 83
andrewboyson 22:914b970356f0 84 return BROADCAST;
andrewboyson 35:93c39d260a83 85 }