src/internet-stack/loopback-net-device.cc
changeset 4472 e20a31541404
child 4578 88434ff8f0a5
equal deleted inserted replaced
4471:ef1730710767 4472:e20a31541404
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2008 INRIA
       
     4  *
       
     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;
       
     8  *
       
     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.
       
    13  *
       
    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
       
    17  *
       
    18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    19  */
       
    20 #include "loopback-net-device.h"
       
    21 #include "ns3/log.h"
       
    22 #include "ns3/channel.h"
       
    23 #include "ns3/node.h"
       
    24 #include "ns3/packet.h"
       
    25 
       
    26 NS_LOG_COMPONENT_DEFINE ("LoopbackNetDevice");
       
    27 
       
    28 namespace ns3 {
       
    29 
       
    30 TypeId 
       
    31 LoopbackNetDevice::GetTypeId (void)
       
    32 {
       
    33   static TypeId tid = TypeId ("ns3::LoopbackNetDevice")
       
    34     .SetParent<NetDevice> ()
       
    35     .AddConstructor<LoopbackNetDevice> ()
       
    36     ;
       
    37   return tid;
       
    38 }
       
    39 
       
    40 LoopbackNetDevice::LoopbackNetDevice ()
       
    41   : m_node (0),
       
    42     m_mtu (0xffff),
       
    43     m_ifIndex (0),
       
    44     m_address (Mac48Address ("00:00:00:00:00:00"))
       
    45 {
       
    46   NS_LOG_FUNCTION_NOARGS ();
       
    47 }
       
    48 
       
    49 void 
       
    50 LoopbackNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol, 
       
    51                             Mac48Address to, Mac48Address from)
       
    52 {
       
    53   NS_LOG_FUNCTION (packet << " " << protocol << " " << to << " " << from);
       
    54   NetDevice::PacketType packetType;
       
    55   if (to == m_address)
       
    56     {
       
    57       packetType = NetDevice::PACKET_HOST;
       
    58     }
       
    59   else if (to.IsBroadcast ())
       
    60     {
       
    61       packetType = NetDevice::PACKET_HOST;
       
    62     }
       
    63   else if (to.IsGroup ())
       
    64     {
       
    65       packetType = NetDevice::PACKET_MULTICAST;
       
    66     }
       
    67   else 
       
    68     {
       
    69       packetType = NetDevice::PACKET_OTHERHOST;
       
    70     }
       
    71   m_rxCallback (this, packet, protocol, from);
       
    72   if (!m_promiscCallback.IsNull ())
       
    73     {
       
    74       m_promiscCallback (this, packet, protocol, from, to, packetType);
       
    75     }
       
    76 }
       
    77 
       
    78 void 
       
    79 LoopbackNetDevice::SetIfIndex(const uint32_t index)
       
    80 {
       
    81   m_ifIndex = index;
       
    82 }
       
    83 
       
    84 uint32_t 
       
    85 LoopbackNetDevice::GetIfIndex(void) const
       
    86 {
       
    87   return m_ifIndex;
       
    88 }
       
    89 
       
    90 Ptr<Channel> 
       
    91 LoopbackNetDevice::GetChannel (void) const
       
    92 {
       
    93   return 0;
       
    94 }
       
    95 
       
    96 Address 
       
    97 LoopbackNetDevice::GetAddress (void) const
       
    98 {
       
    99   return m_address;
       
   100 }
       
   101 
       
   102 bool 
       
   103 LoopbackNetDevice::SetMtu (const uint16_t mtu)
       
   104 {
       
   105   m_mtu = mtu;
       
   106   return true;
       
   107 }
       
   108 
       
   109 uint16_t 
       
   110 LoopbackNetDevice::GetMtu (void) const
       
   111 {
       
   112   return m_mtu;
       
   113 }
       
   114 
       
   115 bool 
       
   116 LoopbackNetDevice::IsLinkUp (void) const
       
   117 {
       
   118   return true;
       
   119 }
       
   120 
       
   121 void 
       
   122 LoopbackNetDevice::SetLinkChangeCallback (Callback<void> callback)
       
   123 {}
       
   124 
       
   125 bool 
       
   126 LoopbackNetDevice::IsBroadcast (void) const
       
   127 {
       
   128   return true;
       
   129 }
       
   130 
       
   131 Address
       
   132 LoopbackNetDevice::GetBroadcast (void) const
       
   133 {
       
   134   // This is typically set to all zeros rather than all ones in real systems
       
   135   return Mac48Address ("00:00:00:00:00:00");
       
   136 }
       
   137 
       
   138 bool 
       
   139 LoopbackNetDevice::IsMulticast (void) const
       
   140 {
       
   141   // Multicast loopback will need to be supported for outgoing 
       
   142   // datagrams but this will probably be handled in multicast sockets
       
   143   return false;
       
   144 }
       
   145 
       
   146 Address 
       
   147 LoopbackNetDevice::GetMulticast (Ipv4Address multicastGroup) const
       
   148 {
       
   149   return Mac48Address::GetMulticast (multicastGroup);
       
   150 }
       
   151 
       
   152 Address LoopbackNetDevice::GetMulticast (Ipv6Address addr) const
       
   153 {
       
   154 	return Mac48Address::GetMulticast (addr);
       
   155 }
       
   156 
       
   157 bool 
       
   158 LoopbackNetDevice::IsPointToPoint (void) const
       
   159 {
       
   160   return false;
       
   161 }
       
   162 
       
   163 bool 
       
   164 LoopbackNetDevice::IsBridge (void) const
       
   165 {
       
   166   return false;
       
   167 }
       
   168 
       
   169 bool 
       
   170 LoopbackNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
       
   171 {
       
   172   NS_LOG_FUNCTION (packet << " " << dest << " " << protocolNumber);
       
   173   Mac48Address to = Mac48Address::ConvertFrom (dest);
       
   174   NS_ASSERT_MSG (to == GetBroadcast () || to == m_address, "Invalid destination address");
       
   175   Receive (packet, protocolNumber, to, m_address);
       
   176   return true;
       
   177 }
       
   178 
       
   179 bool 
       
   180 LoopbackNetDevice::SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
       
   181 {
       
   182   NS_LOG_FUNCTION (packet << " " << source << " " << dest << " " << protocolNumber);
       
   183   Mac48Address to = Mac48Address::ConvertFrom (dest);
       
   184   Mac48Address from = Mac48Address::ConvertFrom (source);
       
   185   NS_ASSERT_MSG (to.IsBroadcast () || to == m_address, "Invalid destination address");
       
   186   Receive (packet, protocolNumber, to, from);
       
   187   return true;
       
   188 }
       
   189 
       
   190 Ptr<Node> 
       
   191 LoopbackNetDevice::GetNode (void) const
       
   192 {
       
   193   return m_node;
       
   194 }
       
   195 
       
   196 void 
       
   197 LoopbackNetDevice::SetNode (Ptr<Node> node)
       
   198 {
       
   199   m_node = node;
       
   200 }
       
   201 
       
   202 bool 
       
   203 LoopbackNetDevice::NeedsArp (void) const
       
   204 {
       
   205   return false;
       
   206 }
       
   207 
       
   208 void 
       
   209 LoopbackNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
       
   210 {
       
   211   m_rxCallback = cb;
       
   212 }
       
   213 
       
   214 void
       
   215 LoopbackNetDevice::DoDispose (void)
       
   216 {
       
   217   m_node = 0;
       
   218   NetDevice::DoDispose ();
       
   219 }
       
   220 
       
   221 
       
   222 void
       
   223 LoopbackNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb)
       
   224 {
       
   225   m_promiscCallback = cb;
       
   226 }
       
   227 
       
   228 bool
       
   229 LoopbackNetDevice::SupportsSendFrom (void) const
       
   230 {
       
   231   return true;
       
   232 }
       
   233 
       
   234 } // namespace ns3