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