Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Sun Apr 18 19:04:48 2021 +0000
Parent:
194:f35c6e218de1
Child:
196:0876a7609a60
Commit message:
Added a user module to allow user defined UDPs port to be used - in this case for a WIZ module.

Changed in this revision

action.h Show annotated file Show diff for this revision Revisions of this file
eth/eth.c Show annotated file Show diff for this revision Revisions of this file
eth/eth.h Show annotated file Show diff for this revision Revisions of this file
eth/mac.c Show annotated file Show diff for this revision Revisions of this file
eth/mac.h Show annotated file Show diff for this revision Revisions of this file
ip4/ip4.c Show annotated file Show diff for this revision Revisions of this file
ip4/ip4.h Show annotated file Show diff for this revision Revisions of this file
ip4/ip4addr.c Show annotated file Show diff for this revision Revisions of this file
resolve/ar4.c Show annotated file Show diff for this revision Revisions of this file
resolve/ar4.h Show annotated file Show diff for this revision Revisions of this file
udp/udp.c Show annotated file Show diff for this revision Revisions of this file
udp/udp.h Show annotated file Show diff for this revision Revisions of this file
user.c Show annotated file Show diff for this revision Revisions of this file
user.h Show annotated file Show diff for this revision Revisions of this file
--- a/action.h	Thu Mar 04 12:08:14 2021 +0000
+++ b/action.h	Sun Apr 18 19:04:48 2021 +0000
@@ -1,3 +1,5 @@
+#include <stdbool.h>
+
 extern int  ActionMakeFromDestAndTrace(int dest, bool trace);
 extern int  ActionGetDestPart         (int action);
 extern bool ActionGetTracePart        (int action);
@@ -8,13 +10,14 @@
 #define   UNICAST_DHCP   3
 #define   UNICAST_NTP    4
 #define   UNICAST_TFTP   5
-#define MULTICAST_NODE   6
-#define MULTICAST_ROUTER 7
-#define MULTICAST_MDNS   8
-#define MULTICAST_LLMNR  9
-#define MULTICAST_NTP   10
-#define SOLICITED_NODE  11
-#define BROADCAST       12
+#define   UNICAST_USER   6
+#define MULTICAST_NODE   7
+#define MULTICAST_ROUTER 8
+#define MULTICAST_MDNS   9
+#define MULTICAST_LLMNR 10
+#define MULTICAST_NTP   11
+#define SOLICITED_NODE  12
+#define BROADCAST       13
 
 /*
 If DO_NOTHING then no other flags may be set.
--- a/eth/eth.c	Thu Mar 04 12:08:14 2021 +0000
+++ b/eth/eth.c	Sun Apr 18 19:04:48 2021 +0000
@@ -13,6 +13,7 @@
 #define MTU 1500
 
 uint16_t EthProtocol; //Set when receiving or sending packets so that higher levels can read the protocol in use
+char*    EthMacRemote; //Set when receiving packets so that higher levels can read the protocol in use
 
 //header variables
 static char*    hdrDstPtr(char* pPacket) { return pPacket +  0; }
@@ -83,12 +84,12 @@
     NetTraceHostCheckMac(hdrSrcPtr(pPacketRx));
 
     int   action = DO_NOTHING;
-    char* macRemote = hdrSrcPtr(pPacketRx);
+    EthMacRemote = hdrSrcPtr(pPacketRx);
     switch (EthProtocol)
     {
-        case ETH_ARP:  action = ArpHandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx);            break;
-        case ETH_IPV4: action = Ip4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, macRemote); break;
-        case ETH_IPV6: action = Ip6HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, macRemote); break;
+        case ETH_ARP:  action = ArpHandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx);               break;
+        case ETH_IPV4: action = Ip4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, EthMacRemote); break;
+        case ETH_IPV6: action = Ip6HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, EthMacRemote); break;
         case 0x6970: break; //Drop Sonos group membership packet
         case 0x7374: break; //Drop Sky Q packet
         case 0x7475: break; //Drop Sky Q packet
@@ -105,9 +106,9 @@
         return DO_NOTHING;
     }
         
-    MacMakeFromDest(ActionGetDestPart(action), EthProtocol, macRemote);
+    MacMakeFromDest(ActionGetDestPart(action), EthProtocol, EthMacRemote);
     MacCopy(hdrSrcPtr(pPacketTx), MacLocal);
-    MacCopy(hdrDstPtr(pPacketTx), macRemote);
+    MacCopy(hdrDstPtr(pPacketTx), EthMacRemote);
     hdrTypSet(pPacketTx, EthProtocol);
     
     *pSizeTx = HEADER_LENGTH + dataLengthTx;
--- a/eth/eth.h	Thu Mar 04 12:08:14 2021 +0000
+++ b/eth/eth.h	Sun Apr 18 19:04:48 2021 +0000
@@ -1,6 +1,7 @@
 #include <stdint.h>
 
 extern uint16_t EthProtocol;
+extern char*    EthMacRemote;
 
 extern void EthProtocolLog(uint16_t prototype);
 extern int  EthHandlePacket       (char* pPacketRx, int sizeRx, char* pPacketTx, int* pSizeTx);
--- a/eth/mac.c	Thu Mar 04 12:08:14 2021 +0000
+++ b/eth/mac.c	Sun Apr 18 19:04:48 2021 +0000
@@ -32,6 +32,50 @@
     return true;
 }
 
+void MacParse(const char *text, char *mac)
+{
+    MacClear(mac);
+    int field = 0;
+    int byte = 0;
+    while(true)
+    {
+        switch (*text)
+        {
+            case ':':
+                mac[field] = byte;
+                field++;
+                if (field > 6) return;
+                byte = 0;
+                break;
+            case '0': byte <<= 4; byte |= 0; break;
+            case '1': byte <<= 4; byte |= 1; break;
+            case '2': byte <<= 4; byte |= 2; break;
+            case '3': byte <<= 4; byte |= 3; break;
+            case '4': byte <<= 4; byte |= 4; break;
+            case '5': byte <<= 4; byte |= 5; break;
+            case '6': byte <<= 4; byte |= 6; break;
+            case '7': byte <<= 4; byte |= 7; break;
+            case '8': byte <<= 4; byte |= 8; break;
+            case '9': byte <<= 4; byte |= 9; break;
+            case 'a':
+            case 'A': byte <<= 4; byte |= 10; break;
+            case 'b':
+            case 'B': byte <<= 4; byte |= 11; break;
+            case 'c':
+            case 'C': byte <<= 4; byte |= 12; break;
+            case 'd':
+            case 'D': byte <<= 4; byte |= 13; break;
+            case 'e':
+            case 'E': byte <<= 4; byte |= 14; break;
+            case 'f':
+            case 'F': byte <<= 4; byte |= 15; break;
+            case 0:
+                mac[field] = byte;
+                return;
+        }
+        text++;
+    }
+}
 int MacToString(const char* mac, int size, char* text)
 {
     return snprintf(text, size, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
--- a/eth/mac.h	Thu Mar 04 12:08:14 2021 +0000
+++ b/eth/mac.h	Sun Apr 18 19:04:48 2021 +0000
@@ -4,6 +4,7 @@
 extern void MacCopy(char* macTo, const char* macFrom);
 extern bool MacIsEmpty(const char* mac);
 extern bool MacIsSame(const char* macA, const char* macB);
+extern void MacParse(const char *pText, char *mac);
 extern  int MacToString(const char* mac, int size, char* text);
 extern  int MacLog(const char* mac);
 extern  int MacHttp(const char* mac);
--- a/ip4/ip4.c	Thu Mar 04 12:08:14 2021 +0000
+++ b/ip4/ip4.c	Sun Apr 18 19:04:48 2021 +0000
@@ -19,7 +19,8 @@
 #include "restart.h"
 #include "checksum.h"
 
-bool Ip4Trace = true;
+bool     Ip4Trace = true;
+uint32_t Ip4Remote = 0;
 
 #define OFF_LINK_TTL 64
 
@@ -96,6 +97,8 @@
     uint32_t srcIp         = Ip4HdrGetSrc      (pPacketRx);
     uint32_t dstIp         = Ip4HdrGetDst      (pPacketRx);
     
+    Ip4Remote = srcIp;
+    
     char* pDataRx = pPacketRx + headerLengthRx;
     char* pDataTx = pPacketTx + IP4_HEADER_LENGTH;
 
@@ -185,6 +188,7 @@
         case UNICAST_DHCP:
         case UNICAST_NTP:
         case UNICAST_TFTP:
+        case UNICAST_USER:
             if (DhcpIpNeedsToBeRouted(dstIp))
             {
                 Ar4IpToMac(DhcpRouterIp, pDstMac); //send via router
--- a/ip4/ip4.h	Thu Mar 04 12:08:14 2021 +0000
+++ b/ip4/ip4.h	Sun Apr 18 19:04:48 2021 +0000
@@ -1,4 +1,5 @@
 
-extern bool Ip4Trace;
-extern int  Ip4HandleReceivedPacket(void (*traceback)(void), void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, char* pRemoteMac);
-extern int  Ip4PollForPacketToSend (                                                      void* pPacketTx, int* pSizeTx, char* pRemoteMac);
+extern bool     Ip4Trace;
+extern uint32_t Ip4Remote;
+extern int      Ip4HandleReceivedPacket(void (*traceback)(void), void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, char* pRemoteMac);
+extern int      Ip4PollForPacketToSend (                                                      void* pPacketTx, int* pSizeTx, char* pRemoteMac);
--- a/ip4/ip4addr.c	Thu Mar 04 12:08:14 2021 +0000
+++ b/ip4/ip4addr.c	Sun Apr 18 19:04:48 2021 +0000
@@ -8,6 +8,7 @@
 #include "dhcp.h"
 #include "ntpclient.h"
 #include "tftp.h"
+#include "user.h"
 
 int Ip4AddrToString(const uint32_t ip, const int size, char* text)
 {
@@ -78,6 +79,7 @@
         case UNICAST_DHCP:     *pDstIp = DhcpServerIp;                break;
         case UNICAST_NTP:      *pDstIp = NtpClientQueryServerIp4;     break;
         case UNICAST_TFTP:     *pDstIp = TftpServerIp4;               break;
+        case UNICAST_USER:     *pDstIp = UserIp4;                     break;
         case MULTICAST_NODE:   *pDstIp = IP4_MULTICAST_ALL_HOSTS;     break;
         case MULTICAST_ROUTER: *pDstIp = IP4_MULTICAST_ALL_ROUTERS;   break;
         case MULTICAST_MDNS:   *pDstIp = IP4_MULTICAST_DNS_ADDRESS;   break;
@@ -85,7 +87,7 @@
         case MULTICAST_NTP:    *pDstIp = IP4_MULTICAST_NTP_ADDRESS;   break;
         case BROADCAST:        *pDstIp = IP4_BROADCAST_ADDRESS;       break;
         default:
-            LogTimeF("Ip4AddressFromDest unknown destination %d\r\n", dest);
+            LogTimeF("Ip4AddrFromDest unknown destination %d\r\n", dest);
             break;
     }
 }
--- a/resolve/ar4.c	Thu Mar 04 12:08:14 2021 +0000
+++ b/resolve/ar4.c	Sun Apr 18 19:04:48 2021 +0000
@@ -144,6 +144,14 @@
     }
     MacClear(mac);
 }
+uint32_t Ar4GetIpFromMac(char* pMac)
+{
+    for (int i = 0; i < RECORDS_COUNT; i++)
+    {
+        if (records[i].state == STATE_VALID && MacIsSame(records[i].mac, pMac)) return records[i].ip;
+    }
+    return 0;
+}
 bool Ar4HaveMacForIp(uint32_t ip)
 {
     for (int i = 0; i < RECORDS_COUNT; i++)
--- a/resolve/ar4.h	Thu Mar 04 12:08:14 2021 +0000
+++ b/resolve/ar4.h	Sun Apr 18 19:04:48 2021 +0000
@@ -8,6 +8,7 @@
 extern int  Ar4AddIpRecord(void (*traceback)(void), char* pMac, uint32_t ip);
 
 extern void     Ar4IpToMac(uint32_t ip, char* pMac);
+extern uint32_t Ar4GetIpFromMac(char* pMac);
 extern uint32_t Ar4IndexToIp(int index);
 extern bool     Ar4HaveMacForIp(uint32_t ip);
 extern bool     Ar4CheckHaveMacAndFetchIfNot(uint32_t ip);
--- a/udp/udp.c	Thu Mar 04 12:08:14 2021 +0000
+++ b/udp/udp.c	Sun Apr 18 19:04:48 2021 +0000
@@ -16,6 +16,7 @@
 #include "ns.h"
 #include "ntpclient.h"
 #include "restart.h"
+#include "user.h"
 
 bool UdpTrace = true;
 
@@ -70,6 +71,8 @@
 
 static int handlePort(void (*traceback)(void), int dataLengthRx, char* pDataRx, int* pPataLengthTx, char* pDataTx)
 {
+    if (UserHandleReceivedUdpPacket && (dstPort == UserUdpPort1 || dstPort == UserUdpPort2)) return UserHandleReceivedUdpPacket(dstPort, traceback, dataLengthRx, pDataRx, pPataLengthTx, pDataTx);
+    
     switch (dstPort)
     {
         //Handle these
@@ -187,6 +190,15 @@
             dstPort = TFTP_SERVER_PORT;
         }
     }
+    if (!action)
+    {
+        action = UserPollForUdpPacketToSend(type, pDataLength, pData);
+        if (action)
+        {
+            srcPort = UserUdpDstPort;
+            dstPort = UserUdpSrcPort;
+        }
+    }
     
     return action;
 }
--- a/udp/udp.h	Thu Mar 04 12:08:14 2021 +0000
+++ b/udp/udp.h	Sun Apr 18 19:04:48 2021 +0000
@@ -1,3 +1,6 @@
+#include <stdbool.h>
+#include <stdint.h>
+
 extern bool UdpTrace;
 
 extern int  UdpHandleReceivedPacket(void (*traceback)(void), int sizeRx, char* pPacketRx, int* pSizeTx, char* pPacketTx);
@@ -6,4 +9,4 @@
 extern void UdpLogHeader(uint16_t calculatedChecksum);
 
 extern void UdpMakeHeader(int size, char* pPacket);
-extern void UdpHdrSetChecksum(void*pPacket, uint16_t checksum);
\ No newline at end of file
+extern void UdpHdrSetChecksum(void*pPacket, uint16_t checksum);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/user.c	Sun Apr 18 19:04:48 2021 +0000
@@ -0,0 +1,13 @@
+#include <stdint.h>
+
+uint16_t UserUdpPort1 = 0;
+uint16_t UserUdpPort2 = 0;
+int (*UserHandleReceivedUdpPacket)(uint16_t port, void (*traceback)(void), int dataLengthRx, char* pDataRx, int* pPataLengthTx, char* pDataTx) = 0;
+
+
+uint16_t UserUdpDstPort = 0;
+uint16_t UserUdpSrcPort = 0;
+uint32_t UserIp4;
+char     UserMac[6];
+
+int (*UserPollForUdpPacketToSend)(int type, int* pDataLength, char* pData);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/user.h	Sun Apr 18 19:04:48 2021 +0000
@@ -0,0 +1,13 @@
+#include <stdint.h>
+
+extern uint16_t UserUdpPort1;
+extern uint16_t UserUdpPort2;
+extern int (*UserHandleReceivedUdpPacket)(uint16_t port, void (*traceback)(void), int dataLengthRx, char* pDataRx, int* pPataLengthTx, char* pDataTx);
+
+
+extern uint16_t UserUdpDstPort;
+extern uint16_t UserUdpSrcPort;
+extern uint32_t UserIp4;
+extern char     UserMac[6];
+
+extern int (*UserPollForUdpPacketToSend)(int type, int* pDataLength, char* pData);
\ No newline at end of file