simple reliable networking over ethernet. Provides IPv4 ARP, ICMP echo reply, and UDP unicast. Does NOT provide TCP, that's not simple :-).

Committer:
altasoul
Date:
Wed Apr 01 20:58:30 2015 +0000
Revision:
7:45bae1fecc36
Parent:
5:7ed0abcc02d1
Quiet the inet unhandled protocol printf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
altasoul 2:397316d97354 1 /*
altasoul 2:397316d97354 2 The MIT License (MIT)
altasoul 2:397316d97354 3
altasoul 2:397316d97354 4 Copyright (c) 2015 Tom Soulanille
altasoul 2:397316d97354 5
altasoul 2:397316d97354 6 Permission is hereby granted, free of charge, to any person obtaining a copy
altasoul 2:397316d97354 7 of this software and associated documentation files (the "Software"), to deal
altasoul 2:397316d97354 8 in the Software without restriction, including without limitation the rights
altasoul 2:397316d97354 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
altasoul 2:397316d97354 10 copies of the Software, and to permit persons to whom the Software is
altasoul 2:397316d97354 11 furnished to do so, subject to the following conditions:
altasoul 2:397316d97354 12
altasoul 2:397316d97354 13 The above copyright notice and this permission notice shall be included in
altasoul 2:397316d97354 14 all copies or substantial portions of the Software.
altasoul 2:397316d97354 15
altasoul 2:397316d97354 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
altasoul 2:397316d97354 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
altasoul 2:397316d97354 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
altasoul 2:397316d97354 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
altasoul 2:397316d97354 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
altasoul 2:397316d97354 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
altasoul 2:397316d97354 22 THE SOFTWARE.
altasoul 2:397316d97354 23
altasoul 2:397316d97354 24 @file snet.cpp
altasoul 2:397316d97354 25 @purpose simple networking
altasoul 2:397316d97354 26 @version 0.1
altasoul 2:397316d97354 27 @date 31 March 2015
altasoul 2:397316d97354 28 @author Tom Soulanille
altasoul 2:397316d97354 29 */
altasoul 2:397316d97354 30
altasoul 0:6df0a6ed91d4 31 #include "snet.h"
altasoul 0:6df0a6ed91d4 32
altasoul 2:397316d97354 33 #if 0
altasoul 2:397316d97354 34 // if not otherwise available, use this to print memory in hex
altasoul 2:397316d97354 35 void print_hex(const uint8_t *p, int len) {
altasoul 2:397316d97354 36 while (len > 0) {
altasoul 2:397316d97354 37 for (int i=0; i<16 && len-->0; i++) {
altasoul 2:397316d97354 38 printf(" 0x%02x", *p++);
altasoul 2:397316d97354 39 }
altasoul 2:397316d97354 40 printf("\r\n");
altasoul 2:397316d97354 41 }
altasoul 2:397316d97354 42 }
altasoul 2:397316d97354 43 #endif
altasoul 0:6df0a6ed91d4 44
altasoul 0:6df0a6ed91d4 45 const uint8_t Snet::broadcast_mac[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
altasoul 0:6df0a6ed91d4 46 const uint8_t Snet::ipEtherType[] = { 0x08, 0x00 };
altasoul 0:6df0a6ed91d4 47 const uint8_t Snet::arp_req_payload_prefix[] = { 0x0, 0x1, 0x8, 0x0, 0x6, 0x4, 0x0, 0x1 };
altasoul 0:6df0a6ed91d4 48
altasoul 0:6df0a6ed91d4 49 Snet::Snet(): eth()
altasoul 0:6df0a6ed91d4 50 {
altasoul 0:6df0a6ed91d4 51 eth.address((char *) my_mac);
altasoul 0:6df0a6ed91d4 52 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\r\n",
altasoul 0:6df0a6ed91d4 53 my_mac[0], my_mac[1], my_mac[2], my_mac[3], my_mac[4], my_mac[5]);
altasoul 0:6df0a6ed91d4 54 printf("no IP\r\n");
altasoul 0:6df0a6ed91d4 55 }
altasoul 0:6df0a6ed91d4 56
altasoul 0:6df0a6ed91d4 57 Snet::Snet(const uint8_t *ip): eth()
altasoul 0:6df0a6ed91d4 58 {
altasoul 0:6df0a6ed91d4 59 eth.address((char *) my_mac);
altasoul 0:6df0a6ed91d4 60 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\r\n",
altasoul 0:6df0a6ed91d4 61 my_mac[0], my_mac[1], my_mac[2], my_mac[3], my_mac[4], my_mac[5]);
altasoul 0:6df0a6ed91d4 62 memcpy(my_ip, ip, 4);
altasoul 0:6df0a6ed91d4 63 printf("IP %d.%d.%d.%d\r\n",
altasoul 0:6df0a6ed91d4 64 my_ip[0], my_ip[1], my_ip[2], my_ip[3]);
altasoul 0:6df0a6ed91d4 65 }
altasoul 0:6df0a6ed91d4 66
altasoul 0:6df0a6ed91d4 67 void Snet::handle_arp_request(uint8_t *buf, int len) {
altasoul 0:6df0a6ed91d4 68 #if 0
altasoul 5:7ed0abcc02d1 69 printf("Arp from %02x:%02x:%02x:%02x:%02x:%02x\r\n",
altasoul 0:6df0a6ed91d4 70 buf[ENET_SMAC_O+0], buf[ENET_SMAC_O+1],
altasoul 0:6df0a6ed91d4 71 buf[ENET_SMAC_O+2], buf[ENET_SMAC_O+3],
altasoul 5:7ed0abcc02d1 72 buf[ENET_SMAC_O+4], buf[ENET_SMAC_O+5]);
altasoul 0:6df0a6ed91d4 73 #endif
altasoul 0:6df0a6ed91d4 74 eth.read((char *) &buf[ENET_PAYLOAD_O], len-ENET_PAYLOAD_O);
altasoul 0:6df0a6ed91d4 75 /*print_hex((uint8_t *) &buf[ENET_PAYLOAD_O], len-ENET_PAYLOAD_O);*/
altasoul 0:6df0a6ed91d4 76 if (!(memcmp(&buf[ENET_PAYLOAD_O], arp_req_payload_prefix, sizeof(arp_req_payload_prefix)) // ARP request
altasoul 0:6df0a6ed91d4 77 || memcmp(&buf[ENET_ARP_TPA_O], my_ip, sizeof(my_ip)))) { // for me
altasoul 0:6df0a6ed91d4 78 //ARP request for me. Build reply.
altasoul 0:6df0a6ed91d4 79 // Ethernet packet addresses:
altasoul 0:6df0a6ed91d4 80 memcpy(&buf[ENET_DMAC_O], &buf[ENET_SMAC_O], 6); // Back to sender
altasoul 0:6df0a6ed91d4 81 memcpy(&buf[ENET_SMAC_O], my_mac, 6); // from me
altasoul 0:6df0a6ed91d4 82 // ARP protocol packet:
altasoul 0:6df0a6ed91d4 83 buf[ENET_ARP_OPER_O+1] = 0x02; // we are replying
altasoul 0:6df0a6ed91d4 84 memcpy(&buf[ENET_ARP_THA_O], &buf[ENET_ARP_SHA_O], 6); // to hardware address of request sender
altasoul 0:6df0a6ed91d4 85 memcpy(&buf[ENET_ARP_TPA_O], &buf[ENET_ARP_SPA_O], 4); // to IP address of request sender
altasoul 0:6df0a6ed91d4 86 memcpy(&buf[ENET_ARP_SHA_O], my_mac, 6); // from my hardware address
altasoul 0:6df0a6ed91d4 87 memcpy(&buf[ENET_ARP_SPA_O], my_ip, 4); // and my IP address
altasoul 5:7ed0abcc02d1 88 #if 0
altasoul 5:7ed0abcc02d1 89 printf("ARP replying\r\n");
altasoul 5:7ed0abcc02d1 90 print_hex((uint8_t *) buf, len);
altasoul 5:7ed0abcc02d1 91 #endif
altasoul 0:6df0a6ed91d4 92 eth.write((char *) buf, len);
altasoul 0:6df0a6ed91d4 93 eth.send();
altasoul 0:6df0a6ed91d4 94 };
altasoul 0:6df0a6ed91d4 95 };
altasoul 0:6df0a6ed91d4 96
altasoul 0:6df0a6ed91d4 97 void Snet::got_broadcast(uint8_t *buf, int len) {
altasoul 0:6df0a6ed91d4 98 #if 0
altasoul 0:6df0a6ed91d4 99 printf("Broadcast from: %02x:%02x:%02x:%02x:%02x:%02x type %02x%02x count %d\r\n",
altasoul 0:6df0a6ed91d4 100 buf[ENET_SMAC_O+0], buf[ENET_SMAC_O+1],
altasoul 0:6df0a6ed91d4 101 buf[ENET_SMAC_O+2], buf[ENET_SMAC_O+3],
altasoul 0:6df0a6ed91d4 102 buf[ENET_SMAC_O+4], buf[ENET_SMAC_O+5],
altasoul 0:6df0a6ed91d4 103 buf[ENET_ETHERTYPE_O+0], buf[ENET_ETHERTYPE_O+1],
altasoul 0:6df0a6ed91d4 104 enet_rx_int_cnt);
altasoul 0:6df0a6ed91d4 105 #endif
altasoul 0:6df0a6ed91d4 106 };
altasoul 0:6df0a6ed91d4 107
altasoul 0:6df0a6ed91d4 108 void Snet::got_unicast(uint8_t *buf, int len) {
altasoul 2:397316d97354 109 if (memcmp(&buf[ENET_ETHERTYPE_O], ipEtherType, 2)) return;
altasoul 2:397316d97354 110 // IP packets only from this point
altasoul 0:6df0a6ed91d4 111 // printf("Unicast from: %02x:%02x:%02x:%02x:%02x:%02x %d\r\n",
altasoul 0:6df0a6ed91d4 112 // buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], enet_rx_int_cnt);
altasoul 0:6df0a6ed91d4 113 memcpy(correspondent_mac, &buf[ENET_SMAC_O], sizeof(correspondent_mac));
altasoul 0:6df0a6ed91d4 114 eth.read((char *) &buf[ENET_PAYLOAD_O], len-ENET_PAYLOAD_O); // Read in the whole rest of the packet
altasoul 0:6df0a6ed91d4 115 interpret_inet_packet(buf, len);
altasoul 0:6df0a6ed91d4 116 }
altasoul 0:6df0a6ed91d4 117
altasoul 1:9c211ac06a12 118 int Snet::rx_and_process_available_packets() {
altasoul 0:6df0a6ed91d4 119 #if 0
altasoul 1:9c211ac06a12 120 // if the MBED Ethernet worked correctly:
altasoul 0:6df0a6ed91d4 121 uint8_t buf[0x600];
altasoul 0:6df0a6ed91d4 122 #else
altasoul 0:6df0a6ed91d4 123 uint8_t buf[750+6+6+2]; // Experimentally-derived maximum packet size is 750 at the IP layer
altasoul 0:6df0a6ed91d4 124 #endif
altasoul 0:6df0a6ed91d4 125 int rv = 0;
altasoul 0:6df0a6ed91d4 126 int len;
altasoul 0:6df0a6ed91d4 127
altasoul 0:6df0a6ed91d4 128 while ((len = eth.receive()) > 0) {
altasoul 0:6df0a6ed91d4 129 eth.read((char *) buf, ENET_PAYLOAD_O); // destination MAC, source MAC, EtherType, lengcode
altasoul 0:6df0a6ed91d4 130 enet_rx_cnt++;
altasoul 5:7ed0abcc02d1 131 // ARP request?
altasoul 5:7ed0abcc02d1 132 if (buf[ENET_ETHERTYPE_O+0] == 0x08 && buf[ENET_ETHERTYPE_O+1] == 0x06)
altasoul 5:7ed0abcc02d1 133 handle_arp_request(buf, len);
altasoul 5:7ed0abcc02d1 134 else if (memcmp(&buf[ENET_DMAC_O], my_mac, 6) == 0) {
altasoul 0:6df0a6ed91d4 135 got_unicast(buf, len);
altasoul 0:6df0a6ed91d4 136 } else if (memcmp(&buf[ENET_DMAC_O], broadcast_mac, 6) == 0) {
altasoul 0:6df0a6ed91d4 137 got_broadcast(buf, len);
altasoul 0:6df0a6ed91d4 138 }
altasoul 3:59f3c806f127 139 rv++;
altasoul 0:6df0a6ed91d4 140 }
altasoul 0:6df0a6ed91d4 141 return rv;
altasoul 0:6df0a6ed91d4 142 }
altasoul 0:6df0a6ed91d4 143