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@94:e2973a2c488e, 2018-12-05 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Dec 05 18:30:37 2018 +0000
- Revision:
- 94:e2973a2c488e
- Parent:
- 61:aad055f1b0d1
- Child:
- 105:ef2dd21d808c
Fixed bug - incorrect MSS being sent from a polled sync: expected 1440 but had -60. Traced to buffer datalength in EthPollForPacketToSend being set to zero instead of being calculated from the buffer length - headersize.
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 | 60:1d8c7a1e7483 | 63 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 64 | p++; if (*p != 0x5e) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 65 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 66 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 67 | p++; |
andrewboyson | 60:1d8c7a1e7483 | 68 | switch (*p) //01 00 5E 00 00 xx |
andrewboyson | 60:1d8c7a1e7483 | 69 | { |
andrewboyson | 60:1d8c7a1e7483 | 70 | case 0x01: return MULTICAST_NODE; |
andrewboyson | 60:1d8c7a1e7483 | 71 | case 0x02: return MULTICAST_ROUTER; |
andrewboyson | 60:1d8c7a1e7483 | 72 | case 0xfb: return MULTICAST_MDNS; |
andrewboyson | 60:1d8c7a1e7483 | 73 | case 0xfc: return MULTICAST_LLMNR; |
andrewboyson | 60:1d8c7a1e7483 | 74 | default: return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 75 | } |
andrewboyson | 60:1d8c7a1e7483 | 76 | } |
andrewboyson | 60:1d8c7a1e7483 | 77 | case 0x33: //Multicast IP6 |
andrewboyson | 60:1d8c7a1e7483 | 78 | { |
andrewboyson | 60:1d8c7a1e7483 | 79 | p++; if (*p != 0x33) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 80 | p++; |
andrewboyson | 60:1d8c7a1e7483 | 81 | switch (*p) //33 33 |
andrewboyson | 60:1d8c7a1e7483 | 82 | { |
andrewboyson | 60:1d8c7a1e7483 | 83 | case 0x00: //33 33 00 Special address |
andrewboyson | 60:1d8c7a1e7483 | 84 | { |
andrewboyson | 60:1d8c7a1e7483 | 85 | p++; |
andrewboyson | 60:1d8c7a1e7483 | 86 | switch (*p) |
andrewboyson | 60:1d8c7a1e7483 | 87 | { |
andrewboyson | 60:1d8c7a1e7483 | 88 | case 0x00: //33 33 00 00 |
andrewboyson | 60:1d8c7a1e7483 | 89 | { |
andrewboyson | 60:1d8c7a1e7483 | 90 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 91 | p++; |
andrewboyson | 60:1d8c7a1e7483 | 92 | switch (*p) //33 33 00 00 00 xx |
andrewboyson | 60:1d8c7a1e7483 | 93 | { |
andrewboyson | 60:1d8c7a1e7483 | 94 | case 0x01: return MULTICAST_NODE; |
andrewboyson | 60:1d8c7a1e7483 | 95 | case 0x02: return MULTICAST_ROUTER; |
andrewboyson | 60:1d8c7a1e7483 | 96 | case 0xfb: return MULTICAST_MDNS; |
andrewboyson | 60:1d8c7a1e7483 | 97 | default: return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 98 | } |
andrewboyson | 60:1d8c7a1e7483 | 99 | } |
andrewboyson | 60:1d8c7a1e7483 | 100 | case 0x01: //33 33 00 01 |
andrewboyson | 60:1d8c7a1e7483 | 101 | { |
andrewboyson | 60:1d8c7a1e7483 | 102 | p++; if (*p != 0x00) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 103 | p++; |
andrewboyson | 60:1d8c7a1e7483 | 104 | switch (*p) //33 33 00 01 00 xx |
andrewboyson | 60:1d8c7a1e7483 | 105 | { |
andrewboyson | 60:1d8c7a1e7483 | 106 | case 0x03: return MULTICAST_LLMNR; |
andrewboyson | 60:1d8c7a1e7483 | 107 | default: return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 108 | } |
andrewboyson | 60:1d8c7a1e7483 | 109 | } |
andrewboyson | 60:1d8c7a1e7483 | 110 | default: return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 111 | } |
andrewboyson | 60:1d8c7a1e7483 | 112 | } |
andrewboyson | 60:1d8c7a1e7483 | 113 | case 0xff: //33 33 FF LL LL LL Solicited address |
andrewboyson | 60:1d8c7a1e7483 | 114 | { |
andrewboyson | 60:1d8c7a1e7483 | 115 | char* q = MacLocal + 3; |
andrewboyson | 60:1d8c7a1e7483 | 116 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 117 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 118 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 119 | return SOLICITED_NODE; |
andrewboyson | 60:1d8c7a1e7483 | 120 | } |
andrewboyson | 60:1d8c7a1e7483 | 121 | default: return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 122 | } |
andrewboyson | 60:1d8c7a1e7483 | 123 | } |
andrewboyson | 60:1d8c7a1e7483 | 124 | default: //Unicast to me |
andrewboyson | 60:1d8c7a1e7483 | 125 | { |
andrewboyson | 60:1d8c7a1e7483 | 126 | char* q = MacLocal; |
andrewboyson | 60:1d8c7a1e7483 | 127 | 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 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 131 | p++; if (*p != *q++) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 132 | p++; if (*p != *q) return DO_NOTHING; |
andrewboyson | 60:1d8c7a1e7483 | 133 | return UNICAST; |
andrewboyson | 60:1d8c7a1e7483 | 134 | } |
andrewboyson | 60:1d8c7a1e7483 | 135 | } |
andrewboyson | 60:1d8c7a1e7483 | 136 | } |
andrewboyson | 60:1d8c7a1e7483 | 137 | |
andrewboyson | 60:1d8c7a1e7483 | 138 | void MacMakeFromDest(int dest, int pro, char* p) |
andrewboyson | 60:1d8c7a1e7483 | 139 | { |
andrewboyson | 60:1d8c7a1e7483 | 140 | if (dest == BROADCAST) { *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p++ = 0xff; *p = 0xff; return; } |
andrewboyson | 60:1d8c7a1e7483 | 141 | |
andrewboyson | 60:1d8c7a1e7483 | 142 | if (pro == IPV4) |
andrewboyson | 60:1d8c7a1e7483 | 143 | { |
andrewboyson | 60:1d8c7a1e7483 | 144 | switch (dest) |
andrewboyson | 60:1d8c7a1e7483 | 145 | { |
andrewboyson | 60:1d8c7a1e7483 | 146 | case MULTICAST_NODE: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0x01; break; |
andrewboyson | 60:1d8c7a1e7483 | 147 | case MULTICAST_ROUTER: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0x02; break; |
andrewboyson | 60:1d8c7a1e7483 | 148 | case MULTICAST_MDNS: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0xfb; break; |
andrewboyson | 60:1d8c7a1e7483 | 149 | case MULTICAST_LLMNR: *p++ = 0x01; *p++ = 0x00; *p++ = 0x5e; *p++ = 0x00; *p++ = 0x00; *p = 0xfc; break; |
andrewboyson | 60:1d8c7a1e7483 | 150 | } |
andrewboyson | 60:1d8c7a1e7483 | 151 | } |
andrewboyson | 60:1d8c7a1e7483 | 152 | else |
andrewboyson | 60:1d8c7a1e7483 | 153 | { |
andrewboyson | 60:1d8c7a1e7483 | 154 | switch (dest) |
andrewboyson | 60:1d8c7a1e7483 | 155 | { |
andrewboyson | 60:1d8c7a1e7483 | 156 | case MULTICAST_NODE: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0x01; break; |
andrewboyson | 60:1d8c7a1e7483 | 157 | case MULTICAST_ROUTER: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0x02; break; |
andrewboyson | 60:1d8c7a1e7483 | 158 | case MULTICAST_MDNS: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x00; *p++ = 0x00; *p = 0xfb; break; |
andrewboyson | 60:1d8c7a1e7483 | 159 | case MULTICAST_LLMNR: *p++ = 0x33; *p++ = 0x33; *p++ = 0x00; *p++ = 0x01; *p++ = 0x00; *p = 0x03; break; |
andrewboyson | 60:1d8c7a1e7483 | 160 | } |
andrewboyson | 60:1d8c7a1e7483 | 161 | } |
andrewboyson | 60:1d8c7a1e7483 | 162 | } |