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
eth/mac.c@105:ef2dd21d808c, 2019-01-11 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Jan 11 14:41:50 2019 +0000
- Revision:
- 105:ef2dd21d808c
- Parent:
- 61:aad055f1b0d1
- Child:
- 143:8cec8f08dc54
Added multicast NTP to action list
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdbool.h> |
andrewboyson | 61:aad055f1b0d1 | 2 | #include <string.h> |
andrewboyson | 61:aad055f1b0d1 | 3 | #include <stdio.h> |
andrewboyson | 61:aad055f1b0d1 | 4 | |
andrewboyson | 60:1d8c7a1e7483 | 5 | #include "log.h" |
andrewboyson | 60:1d8c7a1e7483 | 6 | #include "net.h" |
andrewboyson | 60:1d8c7a1e7483 | 7 | #include "action.h" |
andrewboyson | 60:1d8c7a1e7483 | 8 | #include "eth.h" |
andrewboyson | 60:1d8c7a1e7483 | 9 | #include "http.h" |
andrewboyson | 60:1d8c7a1e7483 | 10 | |
andrewboyson | 60:1d8c7a1e7483 | 11 | char MacLocal[6]; |
andrewboyson | 60:1d8c7a1e7483 | 12 | void MacClear(char* mac) |
andrewboyson | 60:1d8c7a1e7483 | 13 | { |
andrewboyson | 60:1d8c7a1e7483 | 14 | memset(mac, 0, 6); |
andrewboyson | 60:1d8c7a1e7483 | 15 | } |
andrewboyson | 60:1d8c7a1e7483 | 16 | void MacCopy(char* macTo, char* macFrom) |
andrewboyson | 60:1d8c7a1e7483 | 17 | { |
andrewboyson | 60:1d8c7a1e7483 | 18 | memcpy(macTo, macFrom, 6); |
andrewboyson | 60:1d8c7a1e7483 | 19 | } |
andrewboyson | 60:1d8c7a1e7483 | 20 | bool MacIsSame(char* macA, char* macB) |
andrewboyson | 60:1d8c7a1e7483 | 21 | { |
andrewboyson | 60:1d8c7a1e7483 | 22 | return memcmp(macA, macB, 6) == 0; |
andrewboyson | 60:1d8c7a1e7483 | 23 | } |
andrewboyson | 60:1d8c7a1e7483 | 24 | bool MacIsEmpty(char* mac) |
andrewboyson | 60:1d8c7a1e7483 | 25 | { |
andrewboyson | 60:1d8c7a1e7483 | 26 | 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 | mac++; if (*mac) return false; |
andrewboyson | 60:1d8c7a1e7483 | 30 | mac++; if (*mac) return false; |
andrewboyson | 60:1d8c7a1e7483 | 31 | mac++; if (*mac) return false; |
andrewboyson | 60:1d8c7a1e7483 | 32 | return true; |
andrewboyson | 60:1d8c7a1e7483 | 33 | } |
andrewboyson | 60:1d8c7a1e7483 | 34 | |
andrewboyson | 60:1d8c7a1e7483 | 35 | int MacToString(char* mac, int size, char* text) |
andrewboyson | 60:1d8c7a1e7483 | 36 | { |
andrewboyson | 60:1d8c7a1e7483 | 37 | 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 | 38 | } |
andrewboyson | 60:1d8c7a1e7483 | 39 | int MacLog(char* mac) |
andrewboyson | 60:1d8c7a1e7483 | 40 | { |
andrewboyson | 60:1d8c7a1e7483 | 41 | return LogF("%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
andrewboyson | 60:1d8c7a1e7483 | 42 | } |
andrewboyson | 60:1d8c7a1e7483 | 43 | int MacHttp(char* mac) |
andrewboyson | 60:1d8c7a1e7483 | 44 | { |
andrewboyson | 60:1d8c7a1e7483 | 45 | return HttpAddF("%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
andrewboyson | 60:1d8c7a1e7483 | 46 | } |
andrewboyson | 60:1d8c7a1e7483 | 47 | |
andrewboyson | 60:1d8c7a1e7483 | 48 | int MacAccept(char* p) |
andrewboyson | 60:1d8c7a1e7483 | 49 | { |
andrewboyson | 60:1d8c7a1e7483 | 50 | switch (*p) |
andrewboyson | 60:1d8c7a1e7483 | 51 | { |
andrewboyson | 60:1d8c7a1e7483 | 52 | case 0xff: //Broadcast |
andrewboyson | 60:1d8c7a1e7483 | 53 | { |
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 | p++; if (*p != 0xff) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 57 | p++; if (*p != 0xff) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 58 | p++; if (*p != 0xff) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 59 | return BROADCAST; |
andrewboyson | 60:1d8c7a1e7483 | 60 | } |
andrewboyson | 60:1d8c7a1e7483 | 61 | case 0x01: //Multicast IP4 |
andrewboyson | 60:1d8c7a1e7483 | 62 | { |
andrewboyson | 105:ef2dd21d808c | 63 | p++; if (*p != 0x00) return DO_NOTHING; //01 00 |
andrewboyson | 105:ef2dd21d808c | 64 | p++; if (*p != 0x5e) return DO_NOTHING; //01 00 5E |
andrewboyson | 105:ef2dd21d808c | 65 | p++; if (*p != 0x00) return DO_NOTHING; //01 00 5E 00 |
andrewboyson | 60:1d8c7a1e7483 | 66 | p++; |
andrewboyson | 105:ef2dd21d808c | 67 | switch (*p) |
andrewboyson | 60:1d8c7a1e7483 | 68 | { |
andrewboyson | 105:ef2dd21d808c | 69 | case 0x00: //01 00 5E 00 00 |
andrewboyson | 105:ef2dd21d808c | 70 | p++; |
andrewboyson | 105:ef2dd21d808c | 71 | switch (*p) //01 00 5E 00 00 xx |
andrewboyson | 105:ef2dd21d808c | 72 | { |
andrewboyson | 105:ef2dd21d808c | 73 | case 0x01: return MULTICAST_NODE; |
andrewboyson | 105:ef2dd21d808c | 74 | case 0x02: return MULTICAST_ROUTER; |
andrewboyson | 105:ef2dd21d808c | 75 | case 0xfb: return MULTICAST_MDNS; |
andrewboyson | 105:ef2dd21d808c | 76 | case 0xfc: return MULTICAST_LLMNR; |
andrewboyson | 105:ef2dd21d808c | 77 | default: return DO_NOTHING; |
andrewboyson | 105:ef2dd21d808c | 78 | } |
andrewboyson | 105:ef2dd21d808c | 79 | case 0x01: //01 00 5E 00 01 |
andrewboyson | 105:ef2dd21d808c | 80 | p++; |
andrewboyson | 105:ef2dd21d808c | 81 | switch (*p) //01 00 5E 00 01 xx |
andrewboyson | 105:ef2dd21d808c | 82 | { |
andrewboyson | 105:ef2dd21d808c | 83 | case 0x01: return MULTICAST_NTP; |
andrewboyson | 105:ef2dd21d808c | 84 | default: return DO_NOTHING; |
andrewboyson | 105:ef2dd21d808c | 85 | } |
andrewboyson | 105:ef2dd21d808c | 86 | default: return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 87 | } |
andrewboyson | 60:1d8c7a1e7483 | 88 | } |
andrewboyson | 60:1d8c7a1e7483 | 89 | case 0x33: //Multicast IP6 |
andrewboyson | 60:1d8c7a1e7483 | 90 | { |
andrewboyson | 60:1d8c7a1e7483 | 91 | p++; if (*p != 0x33) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 92 | p++; |
andrewboyson | 60:1d8c7a1e7483 | 93 | switch (*p) //33 33 |
andrewboyson | 60:1d8c7a1e7483 | 94 | { |
andrewboyson | 60:1d8c7a1e7483 | 95 | case 0x00: //33 33 00 Special address |
andrewboyson | 60:1d8c7a1e7483 | 96 | { |
andrewboyson | 60:1d8c7a1e7483 | 97 | p++; |
andrewboyson | 60:1d8c7a1e7483 | 98 | switch (*p) |
andrewboyson | 60:1d8c7a1e7483 | 99 | { |
andrewboyson | 60:1d8c7a1e7483 | 100 | case 0x00: //33 33 00 00 |
andrewboyson | 60:1d8c7a1e7483 | 101 | { |
andrewboyson | 105:ef2dd21d808c | 102 | p++; |
andrewboyson | 105:ef2dd21d808c | 103 | switch (*p) |
andrewboyson | 60:1d8c7a1e7483 | 104 | { |
andrewboyson | 105:ef2dd21d808c | 105 | case 0x00: |
andrewboyson | 105:ef2dd21d808c | 106 | p++; |
andrewboyson | 105:ef2dd21d808c | 107 | switch (*p) //33 33 00 00 00 xx |
andrewboyson | 105:ef2dd21d808c | 108 | { |
andrewboyson | 105:ef2dd21d808c | 109 | case 0x01: return MULTICAST_NODE; |
andrewboyson | 105:ef2dd21d808c | 110 | case 0x02: return MULTICAST_ROUTER; |
andrewboyson | 105:ef2dd21d808c | 111 | case 0xfb: return MULTICAST_MDNS; |
andrewboyson | 105:ef2dd21d808c | 112 | default: return DO_NOTHING; |
andrewboyson | 105:ef2dd21d808c | 113 | } |
andrewboyson | 105:ef2dd21d808c | 114 | case 0x01: |
andrewboyson | 105:ef2dd21d808c | 115 | p++; |
andrewboyson | 105:ef2dd21d808c | 116 | switch (*p) //33 33 00 00 01 xx |
andrewboyson | 105:ef2dd21d808c | 117 | { |
andrewboyson | 105:ef2dd21d808c | 118 | case 0x01: return MULTICAST_NTP; |
andrewboyson | 105:ef2dd21d808c | 119 | default: return DO_NOTHING; |
andrewboyson | 105:ef2dd21d808c | 120 | } |
andrewboyson | 105:ef2dd21d808c | 121 | default: return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 122 | } |
andrewboyson | 60:1d8c7a1e7483 | 123 | } |
andrewboyson | 60:1d8c7a1e7483 | 124 | case 0x01: //33 33 00 01 |
andrewboyson | 60:1d8c7a1e7483 | 125 | { |
andrewboyson | 60:1d8c7a1e7483 | 126 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 127 | p++; |
andrewboyson | 60:1d8c7a1e7483 | 128 | switch (*p) //33 33 00 01 00 xx |
andrewboyson | 60:1d8c7a1e7483 | 129 | { |
andrewboyson | 60:1d8c7a1e7483 | 130 | case 0x03: return MULTICAST_LLMNR; |
andrewboyson | 60:1d8c7a1e7483 | 131 | default: return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 132 | } |
andrewboyson | 60:1d8c7a1e7483 | 133 | } |
andrewboyson | 60:1d8c7a1e7483 | 134 | default: return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 135 | } |
andrewboyson | 60:1d8c7a1e7483 | 136 | } |
andrewboyson | 60:1d8c7a1e7483 | 137 | case 0xff: //33 33 FF LL LL LL Solicited address |
andrewboyson | 60:1d8c7a1e7483 | 138 | { |
andrewboyson | 60:1d8c7a1e7483 | 139 | char* q = MacLocal + 3; |
andrewboyson | 60:1d8c7a1e7483 | 140 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 141 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 142 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 143 | return SOLICITED_NODE; |
andrewboyson | 60:1d8c7a1e7483 | 144 | } |
andrewboyson | 60:1d8c7a1e7483 | 145 | default: return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 146 | } |
andrewboyson | 60:1d8c7a1e7483 | 147 | } |
andrewboyson | 60:1d8c7a1e7483 | 148 | default: //Unicast to me |
andrewboyson | 60:1d8c7a1e7483 | 149 | { |
andrewboyson | 60:1d8c7a1e7483 | 150 | char* q = MacLocal; |
andrewboyson | 60:1d8c7a1e7483 | 151 | if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 152 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 153 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 154 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 155 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 156 | p++; if (*p != *q) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 157 | return UNICAST; |
andrewboyson | 60:1d8c7a1e7483 | 158 | } |
andrewboyson | 60:1d8c7a1e7483 | 159 | } |
andrewboyson | 60:1d8c7a1e7483 | 160 | } |
andrewboyson | 60:1d8c7a1e7483 | 161 | |
andrewboyson | 60:1d8c7a1e7483 | 162 | void MacMakeFromDest(int dest, int pro, char* p) |
andrewboyson | 60:1d8c7a1e7483 | 163 | { |
andrewboyson | 60:1d8c7a1e7483 | 164 | if (dest == BROADCAST) { *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p = 0xff; return; } |
andrewboyson | 60:1d8c7a1e7483 | 165 | |
andrewboyson | 60:1d8c7a1e7483 | 166 | if (pro == IPV4) |
andrewboyson | 60:1d8c7a1e7483 | 167 | { |
andrewboyson | 60:1d8c7a1e7483 | 168 | switch (dest) |
andrewboyson | 60:1d8c7a1e7483 | 169 | { |
andrewboyson | 60:1d8c7a1e7483 | 170 | case MULTICAST_NODE: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0x01; break; |
andrewboyson | 60:1d8c7a1e7483 | 171 | case MULTICAST_ROUTER: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0x02; break; |
andrewboyson | 60:1d8c7a1e7483 | 172 | case MULTICAST_MDNS: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0xfb; break; |
andrewboyson | 60:1d8c7a1e7483 | 173 | case MULTICAST_LLMNR: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0xfc; break; |
andrewboyson | 105:ef2dd21d808c | 174 | case MULTICAST_NTP: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x01; *p = 0x01; break; |
andrewboyson | 60:1d8c7a1e7483 | 175 | } |
andrewboyson | 60:1d8c7a1e7483 | 176 | } |
andrewboyson | 60:1d8c7a1e7483 | 177 | else |
andrewboyson | 60:1d8c7a1e7483 | 178 | { |
andrewboyson | 60:1d8c7a1e7483 | 179 | switch (dest) |
andrewboyson | 60:1d8c7a1e7483 | 180 | { |
andrewboyson | 60:1d8c7a1e7483 | 181 | case MULTICAST_NODE: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0x01; break; |
andrewboyson | 60:1d8c7a1e7483 | 182 | case MULTICAST_ROUTER: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0x02; break; |
andrewboyson | 60:1d8c7a1e7483 | 183 | case MULTICAST_MDNS: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0xfb; break; |
andrewboyson | 60:1d8c7a1e7483 | 184 | case MULTICAST_LLMNR: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x01; *p++ = 0x00; *p = 0x03; break; |
andrewboyson | 105:ef2dd21d808c | 185 | case MULTICAST_NTP: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x01; *p = 0x01; break; |
andrewboyson | 60:1d8c7a1e7483 | 186 | } |
andrewboyson | 60:1d8c7a1e7483 | 187 | } |
andrewboyson | 60:1d8c7a1e7483 | 188 | } |