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