friendev EtherDune TCP/IP library
/home/jander/temp/etherdune/NetworkService.cpp
Go to the documentation of this file.
1 // EtherDune Network Service base class
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Base class for any network service running in EtherDune
4 //
5 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
6 //
7 // This source is subject to the GPLv2 license.
8 // Please see the License.txt file for more information.
9 // All other rights reserved.
10 //
11 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
12 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 // PARTICULAR PURPOSE.
15 
16 
17 
18 #include "NetworkService.h"
19 #include "ARP.h"
20 #include "DNS.h"
21 #include "Checksum.h"
22 
23 #define AC_LOGLEVEL 2
24 #include <ACLog.h>
25 ACROSS_MODULE("NetworkService");
26 
27 
28 List NetworkService::activeServices;
29 List SharedBuffer::bufferList;
30 
32 
34 IPAddress NetworkService::localIP = { 0, 0, 0, 0 };
35 IPAddress NetworkService::gatewayIP = { 0, 0, 0, 0 };
36 IPAddress NetworkService::netmask = { 0, 0, 0, 0 };
37 IPAddress NetworkService::dnsIP = { 0, 0, 0, 0 };
38 
39 
40 
41 
42 
45 
59 void NetworkService::onDNSResolve(uint8_t status, uint16_t identification, const IPAddress& ip) {}
60 
61 
67 bool NetworkService::begin(uint8_t cspin)
68 {
70  return 0!= ENC28J60::begin(cspin);
71 }
72 
73 
75 {
76  activeServices.add(this);
77 }
79 {
80  activeServices.remove(this);
81 }
82 void NetworkService::processIncomingPacket()
83 {
84  ACDEBUG("Incoming packet etherType=%x", packet.eth.etherType.getValue());
85 
86 #if ENABLE_IP_RX_CHECKSUM || ENABLE_UDPTCP_RX_CHECKSUM
87 
89  {
90  uint16_t sum = ~Checksum::calc(sizeof(IPHeader), (uint8_t*)&packet.ip);
91  if (0 != sum)
92  {
93  ACWARN("IP Header checksum error");
94  return ; // drop packet, IP Header checksum error
95  }
96  }
97 #endif
98 
99  for (NetworkService* service = (NetworkService*)activeServices.first; service != NULL; service = (NetworkService*)service->nextItem)
100  {
101  if (service->onPacketReceived())
102  return;
103 
104  }
105  ACTRACE("nobody wants this packet");
106  return;
107 
108 }
109 
115 {
117 
118  if ((int32_t)(millis() - tickTimer) >= 0)
119  {
120 
121  for (NetworkService* service = (NetworkService*)activeServices.first; service != NULL; service = (NetworkService*)service->nextItem)
122  service->tick();
123 
124  tickTimer = millis() + NETWORK_TIMER_RESOLUTION;
125  }
126 }
127 
133 bool NetworkService::sendIPPacket(uint16_t length)
134 {
135 
136  if (packet.ip.destinationIP.b[3] == 255) //(cheap hack to detect if it is an IP-layer broadcast)
137  {
138  //LAN broadcast then.
139  memset(&packet.eth.dstMAC, 0xFF, sizeof(MACAddress));
140  }
141  else
142  {
144 
145  MACAddress* dstMac = ARP.whoHas(dstIP);
146 
147  if (dstMac == NULL)
148  return false;
149 
150  packet.eth.dstMAC = *dstMac;
151  }
152 
155 
156  ENC28J60::writeBuf(TXSTART_INIT_DATA, sizeof(EthernetHeader) + length, packet.raw);
158 
159  return true;
160 }
161 
162 
169 {
170  if (localIP.b[0] == 0 || dst.b[0] == 0)
171  return false;
172 
173  for (int i = 0; i < 4; i++)
174  if ((localIP.b[i] & netmask.b[i]) != (dst.b[i] & netmask.b[i]))
175  return false;
176 
177  return true;
178 }
179 
180 void NetworkService::notifyOnDNSResolve(uint8_t status, uint16_t id, const IPAddress& ip)
181 {
182  for (NetworkService* service = (NetworkService*)activeServices.first; service != NULL; service = (NetworkService*)service->nextItem)
183  {
184  service->onDNSResolve(status, id, ip);
185  }
186 }
187 
188 
189 
195 {
196  packet.ip.version = 4;
197  packet.ip.IHL = 0x05; //20 bytes
198  packet.ip.raw[1] = 0x00; //DSCP/ECN=0;
200  packet.ip.flags = 0;
202  packet.ip.destinationIP = remoteIP;
204  packet.ip.TTL = 255;
206  packet.ip.checksum.rawValue = ~Checksum::calc(sizeof(IPHeader), (uint8_t*)&packet.ip);
207 }
208 
216 {
217  static DNSClient dns;
218  return dns;
219 }
220 
Stores a MAC address in memory.
Definition: inet.h:252
Union of all the different protocol headers and layers to help EtherDune interpret or build packet...
Definition: inet.h:496
uint8_t version
Protocol version.
Definition: inet.h:303
ListItem * nextItem
Definition: List.h:39
uint8_t TTL
Time To Live.
Definition: inet.h:317
nint16_t etherType
Protocol type, e.g.
Definition: inet.h:461
static const uint16_t ETHTYPE_IP
Ethernet header protocol type for IP.
Definition: inet.h:578
IPAddress sourceIP
Source address.
Definition: inet.h:320
MACAddress * whoHas(IPAddress &ip)
Attempts to return the MAC address associated with the provided IP address.
Definition: ARP.cpp:95
static uint8_t begin(uint8_t cspin)
Definition: ENC28J60.cpp:310
uint8_t b[4]
byte-wise access to IP address
Definition: inet.h:183
uint8_t raw[566]
Definition: inet.h:537
static DNSClient & DNS()
Obtains access to the DNS service singleton instance.
static EthBuffer packet
in-memory packet buffer currently being processed.
uint16_t rawValue
provides low level access to the memory containing the network-order integer
Definition: inet.h:61
virtual void onDNSResolve(uint8_t status, uint16_t identification, const IPAddress &ip)
Called on each network service every time a DNS response is received.
uint16_t flags
Flags.
Definition: inet.h:315
static bool begin(uint8_t cspin)
Initializes EtherDune and the underlying hardware
Represents the header of an Ethernet frame.
Definition: inet.h:457
MACAddress srcMAC
Source MAC address.
Definition: inet.h:460
ListItem * first
Definition: List.h:47
void add(ListItem *item)
Definition: List.cpp:27
uint8_t raw[20]
byte-wise access to all fields
Definition: inet.h:323
represents an IP address in memory
Definition: inet.h:181
static IPAddress netmask
Subnet mask.
void remove(ListItem *item)
Definition: List.cpp:33
Basic linked list class.
Definition: List.h:43
static IPAddress gatewayIP
IP address of the gateway in this network.
ACROSS_MODULE("NetworkService")
MACAddress dstMAC
Destination MAC address.
Definition: inet.h:459
nint16_t checksum
Header Checksum.
Definition: inet.h:319
uint16_t fragmentOffset
Fragment Offset.
Definition: inet.h:314
virtual void tick()
This is a timer function that is called every NETWORK_TIMER_RESOLUTION milliseconds on every service...
static void packetSend(uint16_t len)
Definition: ENC28J60.cpp:256
IP header.
Definition: inet.h:296
static uint32_t tickTimer
uint8_t IHL
Internet Header Length.
Definition: inet.h:302
Contains functions to perform IP checksum operations.
Definition: Checksum.h:26
static IPAddress dnsIP
IP address of the DNS server to use.
static MACAddress localMAC
Ethernet MAC address.
Implements the ARP protocol as an EtherDune service.
Definition: ARP.h:31
static bool sendIPPacket(uint16_t length)
Puts the current in-memory packet in the network
static ARPService ARP
ARP singleton instance.
EtherDune DNS Service.
Definition: DNS.h:52
static void prepareIPPacket(const IPAddress &remoteIP)
Sets up common IP header values for all outgoing IP packets and calculates the IP header checksum ...
IPAddress destinationIP
Destination address.
Definition: inet.h:321
static void writeBuf(uint16_t dst, uint16_t len, const byte *data)
Definition: ENC28J60.cpp:123
static void loop()
Gives processing time to EtherDune so that it can check for incoming packets or send queued packets...
static bool sameLAN(IPAddress &dst)
Determines whether the given IP is in the same subnet as localIP
nint16_t identification
Identification.
Definition: inet.h:311
static IPAddress localIP
IP address of this application.
IPHeader ip
Definition: inet.h:506
nint16_t totalLength
This 16-bit field defines the entire packet (fragment) size, including header and data...
Definition: inet.h:310
static const uint16_t NETWORK_TIMER_RESOLUTION
Resolution of the NetworkService timer.
Definition: config.h:30
Abstract base class for any network service running in EtherDune.
static void loadSample()
Definition: ENC28J60.cpp:367
EthernetHeader eth
Definition: inet.h:500
void zero()
sets the variable to 0.
Definition: inet.h:97