1 // -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*-
3 // Copyright (c) 2006 Georgia Tech Research Corporation
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation;
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // Author: George F. Riley<riley@ece.gatech.edu>
21 #ifndef IPV4_L3_PROTOCOL_H
22 #define IPV4_L3_PROTOCOL_H
26 #include "ns3/ipv4-address.h"
28 #include "ns3/net-device.h"
30 #include "ns3/traced-callback.h"
31 #include "ns3/ipv4-header.h"
32 #include "ns3/ipv4-routing-protocol.h"
33 #include "ipv4-netfilter.h"
42 class Ipv4RoutingTableEntry;
46 class Ipv4RawSocketImpl;
49 class Icmpv4L4Protocol;
53 * \brief Implement the Ipv4 layer.
55 * This is the actual implementation of IP. It contains APIs to send and
56 * receive packets at the IP layer, as well as APIs for IP routing.
58 class Ipv4L3Protocol : public Ipv4
61 static TypeId GetTypeId (void);
62 static const uint16_t PROT_NUMBER;
65 virtual ~Ipv4L3Protocol ();
67 void SetNode (Ptr<Node> node);
69 // functions defined in base class Ipv4
71 void SetRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol);
72 Ptr<Ipv4RoutingProtocol> GetRoutingProtocol (void) const;
74 Ptr<Socket> CreateRawSocket (void);
75 void DeleteRawSocket (Ptr<Socket> socket);
78 * \param protocol a template for the protocol to add to this L4 Demux.
79 * \returns the L4Protocol effectively added.
81 * Invoke Copy on the input template to get a copy of the input
82 * protocol which can be used on the Node on which this L4 Demux
83 * is running. The new L4Protocol is registered internally as
84 * a working L4 Protocol and returned from this method.
85 * The caller does not get ownership of the returned pointer.
87 void Insert(Ptr<Ipv4L4Protocol> protocol);
89 * \param protocolNumber number of protocol to lookup
91 * \returns a matching L4 Protocol
93 * This method is typically called by lower layers
94 * to forward packets up the stack to the right protocol.
95 * It is also called from NodeImpl::GetUdp for example.
97 Ptr<Ipv4L4Protocol> GetProtocol(int protocolNumber) const;
99 * \param protocol protocol to remove from this demux.
101 * The input value to this method should be the value
102 * returned from the Ipv4L4Protocol::Insert method.
104 void Remove (Ptr<Ipv4L4Protocol> protocol);
107 * \param ttl default ttl to use
109 * When we need to send an ipv4 packet, we use this default
112 void SetDefaultTtl (uint8_t ttl);
115 * Lower layer calls this method after calling L3Demux::Lookup
116 * The ARP subclass needs to know from which NetDevice this
117 * packet is coming to:
118 * - implement a per-NetDevice ARP cache
119 * - send back arp replies on the right device
121 void Receive( Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
122 const Address &to, NetDevice::PacketType packetType);
125 * \param packet packet to send
126 * \param source source address of packet
127 * \param destination address of packet
128 * \param protocol number of packet
129 * \param route route entry
131 * Higher-level layers call this method to send a packet
132 * down the stack to the MAC and PHY layers.
134 void Send (Ptr<Packet> packet, Ipv4Address source,
135 Ipv4Address destination, uint8_t protocol, Ptr<Ipv4Route> route);
137 uint32_t AddInterface (Ptr<NetDevice> device);
138 Ptr<Ipv4Interface> GetInterface (uint32_t i) const;
139 uint32_t GetNInterfaces (void) const;
141 int32_t GetInterfaceForAddress (Ipv4Address addr) const;
142 int32_t GetInterfaceForPrefix (Ipv4Address addr, Ipv4Mask mask) const;
143 int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const;
145 bool AddAddress (uint32_t i, Ipv4InterfaceAddress address);
146 Ipv4InterfaceAddress GetAddress (uint32_t interfaceIndex, uint32_t addressIndex) const;
147 uint32_t GetNAddresses (uint32_t interface) const;
148 bool RemoveAddress (uint32_t interfaceIndex, uint32_t addressIndex);
150 void SetMetric (uint32_t i, uint16_t metric);
151 uint16_t GetMetric (uint32_t i) const;
152 uint16_t GetMtu (uint32_t i) const;
153 bool IsUp (uint32_t i) const;
154 void SetUp (uint32_t i);
155 void SetDown (uint32_t i);
156 bool IsForwarding (uint32_t i) const;
157 void SetForwarding (uint32_t i, bool val);
159 Ptr<NetDevice> GetNetDevice (uint32_t i);
160 Ipv4Netfilter* GetNetfilter();
164 virtual void DoDispose (void);
166 * This function will notify other components connected to the node that a new stack member is now connected
167 * This will be used to notify Layer 3 protocol of layer 4 protocol stack to connect them together.
169 virtual void NotifyNewAggregate ();
171 friend class Ipv4L3ProtocolTest;
172 Ipv4L3Protocol(const Ipv4L3Protocol &);
173 Ipv4L3Protocol &operator = (const Ipv4L3Protocol &);
175 virtual void SetIpForward (bool forward);
176 virtual bool GetIpForward (void) const;
178 Ipv4Header BuildHeader (
180 Ipv4Address destination,
182 uint16_t payloadSize,
187 SendRealOut (Ptr<Ipv4Route> route,
189 Ipv4Header const &ipHeader);
192 IpForward (Ptr<Ipv4Route> rtentry,
194 const Ipv4Header &header);
197 IpMulticastForward (Ptr<Ipv4MulticastRoute> mrtentry,
199 const Ipv4Header &header);
201 void LocalDeliver (Ptr<const Packet> p, Ipv4Header const&ip, uint32_t iif);
202 void RouteInputError (Ptr<const Packet> p, const Ipv4Header & ipHeader, Socket::SocketErrno sockErrno);
204 uint32_t AddIpv4Interface (Ptr<Ipv4Interface> interface);
205 void SetupLoopback (void);
206 Ptr<Icmpv4L4Protocol> GetIcmp (void) const;
207 bool IsUnicast (Ipv4Address ad, Ipv4Mask interfaceMask) const;
209 typedef std::list<Ptr<Ipv4Interface> > Ipv4InterfaceList;
210 typedef std::list<Ptr<Ipv4RawSocketImpl> > SocketList;
211 typedef std::list<Ptr<Ipv4L4Protocol> > L4List_t;
214 L4List_t m_protocols;
215 Ipv4InterfaceList m_interfaces;
216 uint32_t m_nInterfaces;
217 uint8_t m_defaultTtl;
218 uint16_t m_identification;
220 TracedCallback<Ptr<const Packet>, uint32_t> m_txTrace;
221 TracedCallback<Ptr<const Packet>, uint32_t> m_rxTrace;
222 TracedCallback<Ptr<const Packet> > m_dropTrace;
224 Ptr<Ipv4RoutingProtocol> m_routingProtocol;
226 SocketList m_sockets;
227 Ipv4Netfilter netfilter;
232 #endif /* IPV4_L3_PROTOCOL_H */