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@14:e75a59c1123d, 2017-05-05 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri May 05 17:44:16 2017 +0000
- Revision:
- 14:e75a59c1123d
- Parent:
- 13:9cd54f7db57a
- Child:
- 35:93c39d260a83
Made IP addresses and ports available to debug messages
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 13:9cd54f7db57a | 1 | #include "mbed.h" |
andrewboyson | 14:e75a59c1123d | 2 | #include "net.h" |
andrewboyson | 14:e75a59c1123d | 3 | #include "eth.h" |
andrewboyson | 13:9cd54f7db57a | 4 | |
andrewboyson | 13:9cd54f7db57a | 5 | char MacLocal[6]; |
andrewboyson | 13:9cd54f7db57a | 6 | |
andrewboyson | 14:e75a59c1123d | 7 | int MacToString(char* mac, int size, char* text) |
andrewboyson | 13:9cd54f7db57a | 8 | { |
andrewboyson | 14:e75a59c1123d | 9 | 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 | 10 | } |
andrewboyson | 14:e75a59c1123d | 11 | |
andrewboyson | 14:e75a59c1123d | 12 | int MacAccept(char* p) |
andrewboyson | 13:9cd54f7db57a | 13 | { |
andrewboyson | 14:e75a59c1123d | 14 | switch (*p) |
andrewboyson | 14:e75a59c1123d | 15 | { |
andrewboyson | 14:e75a59c1123d | 16 | case 0xff: //Broadcast |
andrewboyson | 14:e75a59c1123d | 17 | { |
andrewboyson | 14:e75a59c1123d | 18 | p++; if (*p != 0xff) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 19 | p++; if (*p != 0xff) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 20 | p++; if (*p != 0xff) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 21 | p++; if (*p != 0xff) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 22 | p++; if (*p != 0xff) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 23 | return BROADCAST; |
andrewboyson | 14:e75a59c1123d | 24 | } |
andrewboyson | 14:e75a59c1123d | 25 | case 0x01: //Multicast IP4 |
andrewboyson | 14:e75a59c1123d | 26 | { |
andrewboyson | 14:e75a59c1123d | 27 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 28 | p++; if (*p != 0x5e) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 29 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 30 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 31 | p++; |
andrewboyson | 14:e75a59c1123d | 32 | switch (*p) //01 00 5E 00 00 xx |
andrewboyson | 14:e75a59c1123d | 33 | { |
andrewboyson | 14:e75a59c1123d | 34 | case 0x01: return MULTICAST_NODE; |
andrewboyson | 14:e75a59c1123d | 35 | case 0x02: return MULTICAST_ROUTER; |
andrewboyson | 14:e75a59c1123d | 36 | case 0xfb: return MULTICAST_MDNS; |
andrewboyson | 14:e75a59c1123d | 37 | case 0xfc: return MULTICAST_LLMNR; |
andrewboyson | 14:e75a59c1123d | 38 | default: return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 39 | } |
andrewboyson | 14:e75a59c1123d | 40 | } |
andrewboyson | 14:e75a59c1123d | 41 | case 0x33: //Multicast IP6 |
andrewboyson | 14:e75a59c1123d | 42 | { |
andrewboyson | 14:e75a59c1123d | 43 | p++; if (*p != 0x33) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 44 | p++; |
andrewboyson | 14:e75a59c1123d | 45 | switch (*p) //33 33 |
andrewboyson | 14:e75a59c1123d | 46 | { |
andrewboyson | 14:e75a59c1123d | 47 | case 0x00: //33 33 00 Special address |
andrewboyson | 14:e75a59c1123d | 48 | { |
andrewboyson | 14:e75a59c1123d | 49 | p++; |
andrewboyson | 14:e75a59c1123d | 50 | switch (*p) |
andrewboyson | 14:e75a59c1123d | 51 | { |
andrewboyson | 14:e75a59c1123d | 52 | case 0x00: //33 33 00 00 |
andrewboyson | 14:e75a59c1123d | 53 | { |
andrewboyson | 14:e75a59c1123d | 54 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 55 | p++; |
andrewboyson | 14:e75a59c1123d | 56 | switch (*p) //33 33 00 00 00 xx |
andrewboyson | 14:e75a59c1123d | 57 | { |
andrewboyson | 14:e75a59c1123d | 58 | case 0x01: return MULTICAST_NODE; |
andrewboyson | 14:e75a59c1123d | 59 | case 0x02: return MULTICAST_ROUTER; |
andrewboyson | 14:e75a59c1123d | 60 | case 0xfb: return MULTICAST_MDNS; |
andrewboyson | 14:e75a59c1123d | 61 | default: return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 62 | } |
andrewboyson | 14:e75a59c1123d | 63 | } |
andrewboyson | 14:e75a59c1123d | 64 | case 0x01: //33 33 00 01 |
andrewboyson | 14:e75a59c1123d | 65 | { |
andrewboyson | 14:e75a59c1123d | 66 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 67 | p++; |
andrewboyson | 14:e75a59c1123d | 68 | switch (*p) //33 33 00 01 00 xx |
andrewboyson | 14:e75a59c1123d | 69 | { |
andrewboyson | 14:e75a59c1123d | 70 | case 0x03: return MULTICAST_LLMNR; |
andrewboyson | 14:e75a59c1123d | 71 | default: return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 72 | } |
andrewboyson | 14:e75a59c1123d | 73 | } |
andrewboyson | 14:e75a59c1123d | 74 | default: return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 75 | } |
andrewboyson | 14:e75a59c1123d | 76 | } |
andrewboyson | 14:e75a59c1123d | 77 | case 0xff: //33 33 FF LL LL LL Solicited address |
andrewboyson | 14:e75a59c1123d | 78 | { |
andrewboyson | 14:e75a59c1123d | 79 | char* q = MacLocal + 3; |
andrewboyson | 14:e75a59c1123d | 80 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 81 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 82 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 83 | return SOLICITED_NODE; |
andrewboyson | 14:e75a59c1123d | 84 | } |
andrewboyson | 14:e75a59c1123d | 85 | default: return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 86 | } |
andrewboyson | 14:e75a59c1123d | 87 | } |
andrewboyson | 14:e75a59c1123d | 88 | default: //Unicast to me |
andrewboyson | 14:e75a59c1123d | 89 | { |
andrewboyson | 14:e75a59c1123d | 90 | char* q = MacLocal; |
andrewboyson | 14:e75a59c1123d | 91 | if (*p != *q++) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 92 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 93 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 94 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 95 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 96 | p++; if (*p != *q) return DO_NOTHING; |
andrewboyson | 14:e75a59c1123d | 97 | return UNICAST; |
andrewboyson | 14:e75a59c1123d | 98 | } |
andrewboyson | 14:e75a59c1123d | 99 | } |
andrewboyson | 13:9cd54f7db57a | 100 | } |
andrewboyson | 14:e75a59c1123d | 101 | |
andrewboyson | 14:e75a59c1123d | 102 | void MacMake(int action, int pro, char* p) |
andrewboyson | 13:9cd54f7db57a | 103 | { |
andrewboyson | 14:e75a59c1123d | 104 | if (action == BROADCAST) { *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p = 0xff; return; } |
andrewboyson | 14:e75a59c1123d | 105 | if (pro == IPV4) |
andrewboyson | 14:e75a59c1123d | 106 | { |
andrewboyson | 14:e75a59c1123d | 107 | switch (action) |
andrewboyson | 14:e75a59c1123d | 108 | { |
andrewboyson | 14:e75a59c1123d | 109 | case MULTICAST_NODE: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0x01; break; |
andrewboyson | 14:e75a59c1123d | 110 | case MULTICAST_ROUTER: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0x02; break; |
andrewboyson | 14:e75a59c1123d | 111 | case MULTICAST_MDNS: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0xfb; break; |
andrewboyson | 14:e75a59c1123d | 112 | case MULTICAST_LLMNR: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0xfc; break; |
andrewboyson | 14:e75a59c1123d | 113 | } |
andrewboyson | 14:e75a59c1123d | 114 | } |
andrewboyson | 14:e75a59c1123d | 115 | else |
andrewboyson | 14:e75a59c1123d | 116 | { |
andrewboyson | 14:e75a59c1123d | 117 | switch (action) |
andrewboyson | 14:e75a59c1123d | 118 | { |
andrewboyson | 14:e75a59c1123d | 119 | case MULTICAST_NODE: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0x01; break; |
andrewboyson | 14:e75a59c1123d | 120 | case MULTICAST_ROUTER: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0x02; break; |
andrewboyson | 14:e75a59c1123d | 121 | case MULTICAST_MDNS: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0xfb; break; |
andrewboyson | 14:e75a59c1123d | 122 | case MULTICAST_LLMNR: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x01; *p++ = 0x00; *p = 0x03; break; |
andrewboyson | 14:e75a59c1123d | 123 | } |
andrewboyson | 14:e75a59c1123d | 124 | } |
andrewboyson | 13:9cd54f7db57a | 125 | } |