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:
Sat Dec 16 14:39:50 2017 +0000
Revision:
60:1d8c7a1e7483
Obtained MAC directly from __semihost call

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 60:1d8c7a1e7483 1 #include "mbed.h"
andrewboyson 60:1d8c7a1e7483 2 #include "log.h"
andrewboyson 60:1d8c7a1e7483 3 #include "net.h"
andrewboyson 60:1d8c7a1e7483 4 #include "action.h"
andrewboyson 60:1d8c7a1e7483 5 #include "eth.h"
andrewboyson 60:1d8c7a1e7483 6 #include "http.h"
andrewboyson 60:1d8c7a1e7483 7
andrewboyson 60:1d8c7a1e7483 8 char MacLocal[6];
andrewboyson 60:1d8c7a1e7483 9 void MacClear(char* mac)
andrewboyson 60:1d8c7a1e7483 10 {
andrewboyson 60:1d8c7a1e7483 11 memset(mac, 0, 6);
andrewboyson 60:1d8c7a1e7483 12 }
andrewboyson 60:1d8c7a1e7483 13 void MacCopy(char* macTo, char* macFrom)
andrewboyson 60:1d8c7a1e7483 14 {
andrewboyson 60:1d8c7a1e7483 15 memcpy(macTo, macFrom, 6);
andrewboyson 60:1d8c7a1e7483 16 }
andrewboyson 60:1d8c7a1e7483 17 bool MacIsSame(char* macA, char* macB)
andrewboyson 60:1d8c7a1e7483 18 {
andrewboyson 60:1d8c7a1e7483 19 return memcmp(macA, macB, 6) == 0;
andrewboyson 60:1d8c7a1e7483 20 }
andrewboyson 60:1d8c7a1e7483 21 bool MacIsEmpty(char* mac)
andrewboyson 60:1d8c7a1e7483 22 {
andrewboyson 60:1d8c7a1e7483 23 if (*mac) return false;
andrewboyson 60:1d8c7a1e7483 24 mac++; if (*mac) return false;
andrewboyson 60:1d8c7a1e7483 25 mac++; if (*mac) return false;
andrewboyson 60:1d8c7a1e7483 26 mac++; if (*mac) return false;
andrewboyson 60:1d8c7a1e7483 27 mac++; if (*mac) return false;
andrewboyson 60:1d8c7a1e7483 28 mac++; if (*mac) return false;
andrewboyson 60:1d8c7a1e7483 29 return true;
andrewboyson 60:1d8c7a1e7483 30 }
andrewboyson 60:1d8c7a1e7483 31
andrewboyson 60:1d8c7a1e7483 32 int MacToString(char* mac, int size, char* text)
andrewboyson 60:1d8c7a1e7483 33 {
andrewboyson 60:1d8c7a1e7483 34 return snprintf(text, size, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
andrewboyson 60:1d8c7a1e7483 35 }
andrewboyson 60:1d8c7a1e7483 36 int MacLog(char* mac)
andrewboyson 60:1d8c7a1e7483 37 {
andrewboyson 60:1d8c7a1e7483 38 return LogF("%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
andrewboyson 60:1d8c7a1e7483 39 }
andrewboyson 60:1d8c7a1e7483 40 int MacHttp(char* mac)
andrewboyson 60:1d8c7a1e7483 41 {
andrewboyson 60:1d8c7a1e7483 42 return HttpAddF("%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
andrewboyson 60:1d8c7a1e7483 43 }
andrewboyson 60:1d8c7a1e7483 44
andrewboyson 60:1d8c7a1e7483 45 int MacAccept(char* p)
andrewboyson 60:1d8c7a1e7483 46 {
andrewboyson 60:1d8c7a1e7483 47 switch (*p)
andrewboyson 60:1d8c7a1e7483 48 {
andrewboyson 60:1d8c7a1e7483 49 case 0xff: //Broadcast
andrewboyson 60:1d8c7a1e7483 50 {
andrewboyson 60:1d8c7a1e7483 51 p++; if (*p != 0xff) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 52 p++; if (*p != 0xff) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 53 p++; if (*p != 0xff) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 54 p++; if (*p != 0xff) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 55 p++; if (*p != 0xff) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 56 return BROADCAST;
andrewboyson 60:1d8c7a1e7483 57 }
andrewboyson 60:1d8c7a1e7483 58 case 0x01: //Multicast IP4
andrewboyson 60:1d8c7a1e7483 59 {
andrewboyson 60:1d8c7a1e7483 60 p++; if (*p != 0x00) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 61 p++; if (*p != 0x5e) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 62 p++; if (*p != 0x00) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 63 p++; if (*p != 0x00) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 64 p++;
andrewboyson 60:1d8c7a1e7483 65 switch (*p) //01 00 5E 00 00 xx
andrewboyson 60:1d8c7a1e7483 66 {
andrewboyson 60:1d8c7a1e7483 67 case 0x01: return MULTICAST_NODE;
andrewboyson 60:1d8c7a1e7483 68 case 0x02: return MULTICAST_ROUTER;
andrewboyson 60:1d8c7a1e7483 69 case 0xfb: return MULTICAST_MDNS;
andrewboyson 60:1d8c7a1e7483 70 case 0xfc: return MULTICAST_LLMNR;
andrewboyson 60:1d8c7a1e7483 71 default: return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 72 }
andrewboyson 60:1d8c7a1e7483 73 }
andrewboyson 60:1d8c7a1e7483 74 case 0x33: //Multicast IP6
andrewboyson 60:1d8c7a1e7483 75 {
andrewboyson 60:1d8c7a1e7483 76 p++; if (*p != 0x33) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 77 p++;
andrewboyson 60:1d8c7a1e7483 78 switch (*p) //33 33
andrewboyson 60:1d8c7a1e7483 79 {
andrewboyson 60:1d8c7a1e7483 80 case 0x00: //33 33 00 Special address
andrewboyson 60:1d8c7a1e7483 81 {
andrewboyson 60:1d8c7a1e7483 82 p++;
andrewboyson 60:1d8c7a1e7483 83 switch (*p)
andrewboyson 60:1d8c7a1e7483 84 {
andrewboyson 60:1d8c7a1e7483 85 case 0x00: //33 33 00 00
andrewboyson 60:1d8c7a1e7483 86 {
andrewboyson 60:1d8c7a1e7483 87 p++; if (*p != 0x00) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 88 p++;
andrewboyson 60:1d8c7a1e7483 89 switch (*p) //33 33 00 00 00 xx
andrewboyson 60:1d8c7a1e7483 90 {
andrewboyson 60:1d8c7a1e7483 91 case 0x01: return MULTICAST_NODE;
andrewboyson 60:1d8c7a1e7483 92 case 0x02: return MULTICAST_ROUTER;
andrewboyson 60:1d8c7a1e7483 93 case 0xfb: return MULTICAST_MDNS;
andrewboyson 60:1d8c7a1e7483 94 default: return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 95 }
andrewboyson 60:1d8c7a1e7483 96 }
andrewboyson 60:1d8c7a1e7483 97 case 0x01: //33 33 00 01
andrewboyson 60:1d8c7a1e7483 98 {
andrewboyson 60:1d8c7a1e7483 99 p++; if (*p != 0x00) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 100 p++;
andrewboyson 60:1d8c7a1e7483 101 switch (*p) //33 33 00 01 00 xx
andrewboyson 60:1d8c7a1e7483 102 {
andrewboyson 60:1d8c7a1e7483 103 case 0x03: return MULTICAST_LLMNR;
andrewboyson 60:1d8c7a1e7483 104 default: return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 105 }
andrewboyson 60:1d8c7a1e7483 106 }
andrewboyson 60:1d8c7a1e7483 107 default: return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 108 }
andrewboyson 60:1d8c7a1e7483 109 }
andrewboyson 60:1d8c7a1e7483 110 case 0xff: //33 33 FF LL LL LL Solicited address
andrewboyson 60:1d8c7a1e7483 111 {
andrewboyson 60:1d8c7a1e7483 112 char* q = MacLocal + 3;
andrewboyson 60:1d8c7a1e7483 113 p++; if (*p != *q++) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 114 p++; if (*p != *q++) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 115 p++; if (*p != *q++) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 116 return SOLICITED_NODE;
andrewboyson 60:1d8c7a1e7483 117 }
andrewboyson 60:1d8c7a1e7483 118 default: return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 119 }
andrewboyson 60:1d8c7a1e7483 120 }
andrewboyson 60:1d8c7a1e7483 121 default: //Unicast to me
andrewboyson 60:1d8c7a1e7483 122 {
andrewboyson 60:1d8c7a1e7483 123 char* q = MacLocal;
andrewboyson 60:1d8c7a1e7483 124 if (*p != *q++) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 125 p++; if (*p != *q++) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 126 p++; if (*p != *q++) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 127 p++; if (*p != *q++) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 128 p++; if (*p != *q++) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 129 p++; if (*p != *q) return DO_NOTHING;
andrewboyson 60:1d8c7a1e7483 130 return UNICAST;
andrewboyson 60:1d8c7a1e7483 131 }
andrewboyson 60:1d8c7a1e7483 132 }
andrewboyson 60:1d8c7a1e7483 133 }
andrewboyson 60:1d8c7a1e7483 134
andrewboyson 60:1d8c7a1e7483 135 void MacMakeFromDest(int dest, int pro, char* p)
andrewboyson 60:1d8c7a1e7483 136 {
andrewboyson 60:1d8c7a1e7483 137 if (dest == BROADCAST) { *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p = 0xff; return; }
andrewboyson 60:1d8c7a1e7483 138
andrewboyson 60:1d8c7a1e7483 139 if (pro == IPV4)
andrewboyson 60:1d8c7a1e7483 140 {
andrewboyson 60:1d8c7a1e7483 141 switch (dest)
andrewboyson 60:1d8c7a1e7483 142 {
andrewboyson 60:1d8c7a1e7483 143 case MULTICAST_NODE: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0x01; break;
andrewboyson 60:1d8c7a1e7483 144 case MULTICAST_ROUTER: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0x02; break;
andrewboyson 60:1d8c7a1e7483 145 case MULTICAST_MDNS: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0xfb; break;
andrewboyson 60:1d8c7a1e7483 146 case MULTICAST_LLMNR: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0xfc; break;
andrewboyson 60:1d8c7a1e7483 147 }
andrewboyson 60:1d8c7a1e7483 148 }
andrewboyson 60:1d8c7a1e7483 149 else
andrewboyson 60:1d8c7a1e7483 150 {
andrewboyson 60:1d8c7a1e7483 151 switch (dest)
andrewboyson 60:1d8c7a1e7483 152 {
andrewboyson 60:1d8c7a1e7483 153 case MULTICAST_NODE: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0x01; break;
andrewboyson 60:1d8c7a1e7483 154 case MULTICAST_ROUTER: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0x02; break;
andrewboyson 60:1d8c7a1e7483 155 case MULTICAST_MDNS: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0xfb; break;
andrewboyson 60:1d8c7a1e7483 156 case MULTICAST_LLMNR: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x01; *p++ = 0x00; *p = 0x03; break;
andrewboyson 60:1d8c7a1e7483 157 }
andrewboyson 60:1d8c7a1e7483 158 }
andrewboyson 60:1d8c7a1e7483 159 }