Files CCore/inc/net/NanoIPDevice.h CCore/src/net/NanoIPDevice.cpp
NanoIPDevice is a set of classes implementing a "reduced IP" protocol support. This implementation does not support IP options, IP fragmentation and there is no routing facility. This feature is intended to be used in devices, connected by a local Ethernet network, to support distributed system implementation. Another use is an interaction between a device and a host computer to support boot, configuring or data exchange.
NanoIPDevice implements IP, ICMP ping, ARP and UDP protocols to some extent. PTP protocol can be attached atop UDP packet devices.
class NanoIPDevice : public ObjBase
{
....
public:
// constructors
NanoIPDevice(StrLen eth_dev_name,IPAddress address,IPAddress net_mask);
virtual ~NanoIPDevice();
// properties
EthDevice * getEthDevice() const;
IPAddress getAddress() const;
IPAddress getNetMask() const;
bool sameNet(IPAddress ip) const;
PacketFormat getTxFormat() const;
PacketFormat getUDPTxFormat() const;
PacketFormat getRxFormat() const;
PacketFormat getUDPRxFormat() const;
// stat
using StatInfo = IPStatInfo ;
void getStat(StatInfo &ret);
// send
template <class P>
bool send_prepare(P packet,PacketFunction function);
template <class P>
void send_prepare_or_complete(P packet,PacketFunction function);
void send(Packet<uint8,IPTxExt> packet);
void send_udp(Packet<uint8,UDPTxExt> packet);
void send_icmp_echo(IPAddress dst,const ICMPEcho &echo,PtrLen<const uint8> data);
// attach/detach
void attach(ICMPEchoEndpoint *endpoint);
void detach(ICMPEchoEndpoint *endpoint);
void attach(IPTickEndpoint *endpoint);
void detach(IPTickEndpoint *endpoint);
void attach(UDPEndpoint *endpoint);
void detach(UDPEndpoint *endpoint);
};
The class NanoIPDevice is the main class of the set. It is an ObjBased class. It is working been attached to some EthDevice device. The NanoIPDevice constructor first argument eth_dev_name is the name of the device to be attached to. Other arguments are: address is the local IP address and net_mask is the local network mask. Network mask is used to filter out incoming packets. An incoming IP packet is passed only if it is destined to the local address and comes from the local network. Otherwise it is dropped.
NanoIPDevice has various properties.
getEthDevice() returns the EthDevice the object is attached to.
getAddress() returns the local IP address.
getNetMask() returns the local network mask.
sameNet() can be used to test if the given IP address belongs to the local network.
getTxFormat() is the packet format for IP Tx operations.
getUDPTxFormat() is the packet format for UDP Tx operations.
getRxFormat() is the packet format of incoming IP packets.
getUDPRxFormat() is the packet format of incoming UDP packets.
NanoIPDevice counts statistics and generates network events.
getStat() returns statistic counters of the type StatInfo. This info block counts network events.
The following methods can be used to send a packet to the network.
send_prepare() tries to put the given packet into the prepare list. The given packet function is used to "prepare" the packet before sending out. If the method is successful it consumes the packet and returns true. Otherwise it leaves the packet alone and returns false. The called must deal with the packet in the last case.
send_prepare_or_complete() is similar to the send_prepare(), but in case of failure this method just completes the packet.
send() sends an IP packet. The packet has an IPTxExt extension and must have an IP data payload fitting getTxFormat() format.
send_udp() sends a UDP packet. The packet has an UDPTxExt extension and must have a UDP data payload fitting getUDPTxFormat() format.
send_icmp_echo() sends an ICMP echo to the given IP address. Other arguments specify an ICMP header and ICMP data payload.
There are three types of endpoints: ICMPEchoEndpoint, IPTickEndpoint and UDPEndpoint. These endpoints can be attached to the NanoIPDevice object to process the incoming traffic. Multiple endpoints can be attached the same times (with natural limitations).
IPTxExt is used to send an IP packet. This structure specifies the destination IP address and the IP protocol type of the packet.
struct IPTxExt
{
IPAddress dst;
IProto proto;
// constructors
IPTxExt();
IPTxExt(IPAddress dst,IProto proto);
// print object
template <class P>
void print(P &out) const;
};
UDPTxExt is used to send a UDP packet. This structure specifies the destination IP address, the source and the destination port numbers and the checksum flag. If the checksum flag is set, then the UDP packet is sent with checksum, otherwise without one.
struct UDPTxExt
{
IPAddress dst;
UDPort dst_port;
UDPort src_port;
bool do_checksum;
// constructors
UDPTxExt();
UDPTxExt(IPAddress dst,UDPort dst_port,UDPort src_port,bool do_checksum);
// print object
template <class P>
void print(P &out) const;
};
ICMPEchoEndpoint can be used to ping a particular IP address.
using ICMPEchoNumber = uint16 ; using ICMPReplyFunction = Function<void (ICMPEchoNumber num)> ; class ICMPEchoEndpoint : NoCopy { enum { DataLen = 32 }; .... public: ICMPEchoEndpoint(StrLen ip_dev_name,IPAddress dst,ICMPReplyFunction reply_function); ~ICMPEchoEndpoint(); ICMPEchoNumber send(); };
ICMPEchoEndpoint constructor has the following arguments:
ip_dev_name is the name of some NanoIPDevice,
dst is the destination IP address,
reply_function is a replay function. This function is called in the "unknown task context" to process an ICMP ping reply. This function has the following arguments:
num is an ICMP ping reply number.
send() prepares ans sends an ICMP ping request to the destination IP address. The request number is returned. It is a uint16 number, incremented with each request (the first value is 1).
IPTickEndpoint is a "tick" endpoint. It receives the network tick. This event happens 10 times per second and is intended to be used for the protocol timeout processing.
using IPTickFunction = Function<void (void)> ; class IPTickEndpoint : NoCopy { .... public: IPTickEndpoint(StrLen ip_dev_name,IPTickFunction tick_function); ~IPTickEndpoint(); };
IPTickEndpoint constructor has the following arguments:
ip_dev_name is the name of some NanoIPDevice,
tick_function is a tick processing function without arguments. It is called in the "unknown task context".
UDPEndpoint can be attached to the NanoIPDevice to process incoming UDP traffic on the particular port. Multiple objects can be attached, but only one on a particular port.
using UDPFunction = Function<void (Packet<uint8> packet,PtrLen<const uint8> data,UDPoint udpoint)> ; class UDPEndpoint : NoCopy { .... public: UDPEndpoint(StrLen ip_dev_name,UDPort udport,bool do_checksum,UDPFunction rx_function); ~UDPEndpoint(); ulen getMaxInboundLen() const; PacketFormat getUDPTxFormat() const; void send(Packet<uint8> packet,UDPoint dst); };
UDPEndpoint constructor has the following arguments:
ip_dev_name is the name of some NanoIPDevice,
udport is the local UDP port,
do_checksum is the UDP checksum flag. If it is true, the UDP checksum processing is enabled on this particular port.
rx_function is the inbound processing function. This function is called to process incoming UDP packets in the "unknown task context". The function has the following arguments:
packet is a packet with an attached data buffer.
data is a UDP payload data range from the attached data buffer.
udpoint designates the source of the packet.
getMaxInboundLen() is the maximum inbound packet data length.
getUDPTxFormat() is the Tx packet format. Use this format to prepare a packet to be sent to the UDP communication peer.
send() sends the given packet to the given UDP endpoint. The packet carries UDP data payload and must be prepared according the getUDPTxFormat() packet format.
NanoUDPEndpointDevice class implements the PacketEndpointDevice interface over a UDP port.
class NanoUDPEndpointDevice : public ObjBase , public PacketEndpointDevice , public PortManager
{
....
public:
NanoUDPEndpointDevice(StrLen ip_dev_name,UDPort udport,bool do_checksum,UDPoint dst);
virtual ~NanoUDPEndpointDevice();
// methods
UDPort getPort() const;
UDPoint getDst() const;
// PacketEndpointDevice
virtual PacketFormat getOutboundFormat() const;
virtual void outbound(Packet<uint8> packet);
virtual ulen getMaxInboundLen() const;
virtual void attach(InboundProc *proc);
virtual void detach();
// PortManager
virtual XPoint getDevicePort() const;
virtual XPoint getPort(XPoint point) const;
virtual XPoint changePort(XPoint point,XPoint port) const;
};
NanoUDPEndpointDevice constructor has the following arguments:
ip_dev_name is the name of some NanoIPDevice,
udport is the local UDP port,
do_checksum is the UDP checksum flag. If it is true, the UDP checksum processing is enabled on this particular port.
dst is the communication peer UDP address.
getPort() returns the assigned UDP port number.
getDst() returns the assigned UDP peer address.
Other methods implement PacketEndpointDevice interface and PortManager interface.
NanoUDPMultipointDevice class implements the PacketMultipointDevice interface over a UDP port.
class NanoUDPMultipointDevice : public ObjBase , public PacketMultipointDevice , public PortManager
{
....
public:
NanoUDPMultipointDevice(StrLen ip_dev_name,UDPort udport,bool do_checksum);
virtual ~NanoUDPMultipointDevice();
// methods
UDPort getPort() const;
// PacketMultipointDevice
virtual StrLen toText(XPoint point,PtrLen<char> buf) const;
virtual PacketFormat getOutboundFormat() const;
virtual void outbound(XPoint point,Packet<uint8> packet);
virtual ulen getMaxInboundLen() const;
virtual void attach(InboundProc *proc);
virtual void detach();
// PortManager
virtual XPoint getDevicePort() const;
virtual XPoint getPort(XPoint point) const;
virtual XPoint changePort(XPoint point,XPoint port) const;
};
NanoUDPMultipointDevice constructor has the following arguments:
ip_dev_name is the name of some NanoIPDevice,
udport is the local UDP port,
do_checksum is the UDP checksum flag. If it is true, the UDP checksum processing is enabled on this particular port.
getPort() returns the assigned UDP port number.
Other methods implement PacketMultipointDevice interface and PortManager interface.