Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Mon Jul 03 14:29:07 2017 +0000
Revision:
22:914b970356f0
Parent:
14:e75a59c1123d
Child:
30:e34173b7585c
Corrected NTP client function

Who changed what in which revision?

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