Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Dec 14 20:55:40 2017 +0000
Revision:
59:e0e556c8bd46
Parent:
53:77f8a49adf89
Added buffer length to help avoid over runs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 45:3dd57903ec99 1 #include "mbed.h"
andrewboyson 45:3dd57903ec99 2 #include "log.h"
andrewboyson 45:3dd57903ec99 3 #include "net.h"
andrewboyson 45:3dd57903ec99 4 #include "action.h"
andrewboyson 49:1a6336f2b3f9 5 #include "ip6addr.h"
andrewboyson 45:3dd57903ec99 6 #include "mac.h"
andrewboyson 50:492f2d2954e4 7 #include "nr6.h"
andrewboyson 48:952dddb74b8b 8 #include "ar6.h"
andrewboyson 45:3dd57903ec99 9 #include "ip6.h"
andrewboyson 45:3dd57903ec99 10 #include "slaac.h"
andrewboyson 47:73af5c0b0dc2 11 #include "ndp.h"
andrewboyson 45:3dd57903ec99 12
andrewboyson 45:3dd57903ec99 13 bool NsTraceRecvSol = false;
andrewboyson 45:3dd57903ec99 14 bool NsTraceRecvAdv = false;
andrewboyson 45:3dd57903ec99 15 bool NsTraceSendSol = false;
andrewboyson 45:3dd57903ec99 16
andrewboyson 45:3dd57903ec99 17 char NsAddressToResolve[16];
andrewboyson 45:3dd57903ec99 18 bool NsResolveRequestFlag = false;
andrewboyson 45:3dd57903ec99 19
andrewboyson 47:73af5c0b0dc2 20 __packed struct header
andrewboyson 45:3dd57903ec99 21 {
andrewboyson 47:73af5c0b0dc2 22 uint32_t reserved;
andrewboyson 47:73af5c0b0dc2 23 char target[16];
andrewboyson 47:73af5c0b0dc2 24 };
andrewboyson 45:3dd57903ec99 25
andrewboyson 59:e0e556c8bd46 26 static void logHeader(struct header* pHeader, int size)
andrewboyson 45:3dd57903ec99 27 {
andrewboyson 45:3dd57903ec99 28 char* pData = (char*)pHeader + sizeof(struct header);
andrewboyson 47:73af5c0b0dc2 29 int dataLength = size - sizeof(struct header);
andrewboyson 45:3dd57903ec99 30
andrewboyson 47:73af5c0b0dc2 31 if (NetTraceVerbose)
andrewboyson 45:3dd57903ec99 32 {
andrewboyson 47:73af5c0b0dc2 33 Log("NS header\r\n");
andrewboyson 47:73af5c0b0dc2 34 LogF(" Size %d\r\n", size);
andrewboyson 47:73af5c0b0dc2 35 LogF(" Target "); Ip6AddressLog(pHeader->target); Log("\r\n");
andrewboyson 47:73af5c0b0dc2 36 NdpLogOptionsVerbose(pData, dataLength);
andrewboyson 45:3dd57903ec99 37 }
andrewboyson 47:73af5c0b0dc2 38 else
andrewboyson 47:73af5c0b0dc2 39 {
andrewboyson 47:73af5c0b0dc2 40 Log("NS header ");
andrewboyson 47:73af5c0b0dc2 41 Ip6AddressLog(pHeader->target);
andrewboyson 47:73af5c0b0dc2 42 NdpLogOptionsQuiet(pData, dataLength);
andrewboyson 47:73af5c0b0dc2 43 Log("\r\n");
andrewboyson 47:73af5c0b0dc2 44 }
andrewboyson 45:3dd57903ec99 45 }
andrewboyson 59:e0e556c8bd46 46 static struct header * pTraceHeader;
andrewboyson 59:e0e556c8bd46 47 static int traceSize;
andrewboyson 48:952dddb74b8b 48 static void (*pTraceBack)(void);
andrewboyson 48:952dddb74b8b 49 static void trace()
andrewboyson 48:952dddb74b8b 50 {
andrewboyson 48:952dddb74b8b 51 pTraceBack();
andrewboyson 59:e0e556c8bd46 52 logHeader(pTraceHeader, traceSize);
andrewboyson 48:952dddb74b8b 53 }
andrewboyson 59:e0e556c8bd46 54 int NsHandleReceivedSolicitation(void (*traceback)(void), uint8_t* pType, uint8_t* pCode, void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx)
andrewboyson 45:3dd57903ec99 55 {
andrewboyson 48:952dddb74b8b 56 pTraceBack = traceback;
andrewboyson 59:e0e556c8bd46 57 struct header* pHeaderRx = (header*)pPacketRx;
andrewboyson 59:e0e556c8bd46 58 struct header* pHeaderTx = (header*)pPacketTx;
andrewboyson 59:e0e556c8bd46 59
andrewboyson 59:e0e556c8bd46 60 pTraceHeader = pHeaderRx;
andrewboyson 59:e0e556c8bd46 61 traceSize = sizeRx;
andrewboyson 45:3dd57903ec99 62
andrewboyson 45:3dd57903ec99 63 //Check it is us
andrewboyson 59:e0e556c8bd46 64 if (!SlaacScope(pHeaderRx->target)) return DO_NOTHING;
andrewboyson 45:3dd57903ec99 65
andrewboyson 45:3dd57903ec99 66 if (NsTraceRecvSol)
andrewboyson 45:3dd57903ec99 67 {
andrewboyson 45:3dd57903ec99 68 if (NetTraceNewLine) Log("\r\n");
andrewboyson 45:3dd57903ec99 69 LogTimeF("NDP received neighbour solicit\r\n");
andrewboyson 45:3dd57903ec99 70 if (NetTraceStack) traceback();
andrewboyson 59:e0e556c8bd46 71 logHeader(pHeaderRx, sizeRx);
andrewboyson 45:3dd57903ec99 72 }
andrewboyson 45:3dd57903ec99 73
andrewboyson 45:3dd57903ec99 74 //Send advertisement
andrewboyson 45:3dd57903ec99 75 *pType = 136;
andrewboyson 45:3dd57903ec99 76 *pCode = 0;
andrewboyson 59:e0e556c8bd46 77 pHeaderTx->reserved = 0x00000060; //R=0 (not a router); S=1 (solicited); O=1 (override)
andrewboyson 59:e0e556c8bd46 78 Ip6AddressCopy(pHeaderTx->target, pHeaderRx->target); //Target toes not change
andrewboyson 45:3dd57903ec99 79
andrewboyson 47:73af5c0b0dc2 80 //Add target MAC
andrewboyson 59:e0e556c8bd46 81 char* pDataTx = (char*)pHeaderTx + sizeof(struct header);
andrewboyson 59:e0e556c8bd46 82 char* p = pDataTx;
andrewboyson 47:73af5c0b0dc2 83 p += NdpAddOptionTargetMac(p, MacLocal);
andrewboyson 45:3dd57903ec99 84
andrewboyson 59:e0e556c8bd46 85 *pSizeTx = sizeof(struct header) + p - pDataTx;
andrewboyson 45:3dd57903ec99 86
andrewboyson 59:e0e556c8bd46 87 if (NsTraceRecvSol) logHeader(pHeaderTx, *pSizeTx);
andrewboyson 45:3dd57903ec99 88
andrewboyson 45:3dd57903ec99 89 return ActionMakeFromDestAndTrace(UNICAST, NsTraceRecvSol && NetTraceStack);
andrewboyson 45:3dd57903ec99 90
andrewboyson 45:3dd57903ec99 91 }
andrewboyson 45:3dd57903ec99 92 int NsHandleReceivedAdvertisement(void (*traceback)(void), void* pPacket, int* pSize)
andrewboyson 45:3dd57903ec99 93 {
andrewboyson 48:952dddb74b8b 94 pTraceBack = traceback;
andrewboyson 59:e0e556c8bd46 95 struct header* pHeader = (header*)pPacket;
andrewboyson 45:3dd57903ec99 96 char* pData = (char*)pHeader + sizeof(struct header);
andrewboyson 59:e0e556c8bd46 97 int dataLength = *pSize - sizeof(struct header);
andrewboyson 45:3dd57903ec99 98
andrewboyson 45:3dd57903ec99 99 if (NsTraceRecvAdv)
andrewboyson 45:3dd57903ec99 100 {
andrewboyson 45:3dd57903ec99 101 if (NetTraceNewLine) Log("\r\n");
andrewboyson 45:3dd57903ec99 102 LogTimeF("NDP received neighbour advertise\r\n");
andrewboyson 45:3dd57903ec99 103 if (NetTraceStack) traceback();
andrewboyson 59:e0e556c8bd46 104 logHeader(pHeader, *pSize);
andrewboyson 45:3dd57903ec99 105 }
andrewboyson 45:3dd57903ec99 106
andrewboyson 47:73af5c0b0dc2 107 char tgtMac[6];
andrewboyson 47:73af5c0b0dc2 108 NdpDecodeOptions(pData, dataLength, NULL, tgtMac);
andrewboyson 45:3dd57903ec99 109
andrewboyson 48:952dddb74b8b 110 Ar6AddIpRecord(trace, tgtMac, pHeader->target);
andrewboyson 50:492f2d2954e4 111 Nr6MakeRequestForNameFromIp(pHeader->target);
andrewboyson 45:3dd57903ec99 112
andrewboyson 45:3dd57903ec99 113 return DO_NOTHING;
andrewboyson 45:3dd57903ec99 114 }
andrewboyson 45:3dd57903ec99 115
andrewboyson 45:3dd57903ec99 116 int NsGetWaitingSolicitation(void* pPacket, int* pSize, uint8_t* pType, uint8_t* pCode)
andrewboyson 45:3dd57903ec99 117 {
andrewboyson 45:3dd57903ec99 118 if (!NsResolveRequestFlag) return DO_NOTHING;
andrewboyson 46:40d33e9037e4 119 NsResolveRequestFlag = false;
andrewboyson 45:3dd57903ec99 120
andrewboyson 45:3dd57903ec99 121 *pType = 135; //Neighbour solicitation
andrewboyson 45:3dd57903ec99 122 *pCode = 0;
andrewboyson 47:73af5c0b0dc2 123
andrewboyson 59:e0e556c8bd46 124 struct header* pHeader = (header*)pPacket;
andrewboyson 47:73af5c0b0dc2 125 pHeader->reserved = 0;
andrewboyson 49:1a6336f2b3f9 126 Ip6AddressCopy(pHeader->target, NsAddressToResolve);
andrewboyson 45:3dd57903ec99 127
andrewboyson 45:3dd57903ec99 128 char* pData = (char*)pHeader + sizeof(struct header);
andrewboyson 45:3dd57903ec99 129 char* p = pData;
andrewboyson 47:73af5c0b0dc2 130 p += NdpAddOptionSourceMac(p, MacLocal);
andrewboyson 45:3dd57903ec99 131
andrewboyson 59:e0e556c8bd46 132 *pSize = sizeof(struct header) + p - pData;
andrewboyson 45:3dd57903ec99 133
andrewboyson 45:3dd57903ec99 134 if (NsTraceSendSol)
andrewboyson 45:3dd57903ec99 135 {
andrewboyson 45:3dd57903ec99 136 if (NetTraceNewLine) Log("\r\n");
andrewboyson 45:3dd57903ec99 137 LogTimeF("NDP sent neighbour solicit\r\n");
andrewboyson 59:e0e556c8bd46 138 logHeader(pHeader, *pSize);
andrewboyson 45:3dd57903ec99 139 }
andrewboyson 45:3dd57903ec99 140
andrewboyson 45:3dd57903ec99 141 return ActionMakeFromDestAndTrace(MULTICAST_NODE, NsTraceSendSol && NetTraceStack);
andrewboyson 45:3dd57903ec99 142
andrewboyson 45:3dd57903ec99 143 }