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 Aug 10 17:45:37 2017 +0000
Revision:
30:e34173b7585c
Parent:
22:914b970356f0
Child:
35:93c39d260a83
Added dns address request - only name requests was there before.

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