00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2005,2006,2007 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 00021 #include "ipv4-interface.h" 00022 #include "ns3/ipv4-address.h" 00023 #include "ns3/net-device.h" 00024 #include "ns3/log.h" 00025 #include "ns3/packet.h" 00026 00027 NS_LOG_COMPONENT_DEFINE ("Ipv4Interface"); 00028 00029 namespace ns3 { 00030 00031 TypeId 00032 Ipv4Interface::GetTypeId (void) 00033 { 00034 static TypeId tid = TypeId ("ns3::Ipv4Interface") 00035 .SetParent<Object> () 00036 ; 00037 return tid; 00038 } 00039 00040 /** 00041 * By default, Ipv4 interface are created in the "down" state 00042 * with ip address 192.168.0.1 and a matching mask. Before 00043 * becoming useable, the user must invoke SetUp on them 00044 * once the final Ipv4 address and mask has been set. 00045 */ 00046 Ipv4Interface::Ipv4Interface () 00047 : m_ifup(false), 00048 m_metric(1) 00049 { 00050 NS_LOG_FUNCTION (this); 00051 } 00052 00053 Ipv4Interface::~Ipv4Interface () 00054 { 00055 NS_LOG_FUNCTION_NOARGS (); 00056 } 00057 00058 void 00059 Ipv4Interface::DoDispose (void) 00060 { 00061 NS_LOG_FUNCTION_NOARGS (); 00062 Object::DoDispose (); 00063 } 00064 00065 void 00066 Ipv4Interface::SetAddress (Ipv4Address a) 00067 { 00068 NS_LOG_FUNCTION (this << a); 00069 m_address = a; 00070 } 00071 00072 void 00073 Ipv4Interface::SetNetworkMask (Ipv4Mask mask) 00074 { 00075 NS_LOG_FUNCTION (this << mask); 00076 m_netmask = mask; 00077 } 00078 00079 Ipv4Address 00080 Ipv4Interface::GetBroadcast (void) const 00081 { 00082 NS_LOG_FUNCTION_NOARGS (); 00083 uint32_t mask = m_netmask.Get (); 00084 uint32_t address = m_address.Get (); 00085 Ipv4Address broadcast = Ipv4Address (address | (~mask)); 00086 return broadcast; 00087 } 00088 00089 Ipv4Mask 00090 Ipv4Interface::GetNetworkMask (void) const 00091 { 00092 NS_LOG_FUNCTION_NOARGS (); 00093 return m_netmask; 00094 } 00095 00096 void 00097 Ipv4Interface::SetMetric (uint16_t metric) 00098 { 00099 NS_LOG_FUNCTION (metric); 00100 m_metric = metric; 00101 } 00102 00103 uint16_t 00104 Ipv4Interface::GetMetric (void) const 00105 { 00106 NS_LOG_FUNCTION_NOARGS (); 00107 return m_metric; 00108 } 00109 00110 Ipv4Address 00111 Ipv4Interface::GetAddress (void) const 00112 { 00113 NS_LOG_FUNCTION_NOARGS (); 00114 return m_address; 00115 } 00116 00117 uint16_t 00118 Ipv4Interface::GetMtu (void) const 00119 { 00120 NS_LOG_FUNCTION_NOARGS (); 00121 if (GetDevice () == 0) 00122 { 00123 uint32_t mtu = (1<<16) - 1; 00124 return mtu; 00125 } 00126 return GetDevice ()->GetMtu (); 00127 } 00128 00129 /** 00130 * These are IP interface states and may be distinct from 00131 * NetDevice states, such as found in real implementations 00132 * (where the device may be down but IP interface state is still up). 00133 */ 00134 bool 00135 Ipv4Interface::IsUp (void) const 00136 { 00137 NS_LOG_FUNCTION_NOARGS (); 00138 return m_ifup; 00139 } 00140 00141 bool 00142 Ipv4Interface::IsDown (void) const 00143 { 00144 NS_LOG_FUNCTION_NOARGS (); 00145 return !m_ifup; 00146 } 00147 00148 void 00149 Ipv4Interface::SetUp (void) 00150 { 00151 NS_LOG_FUNCTION_NOARGS (); 00152 m_ifup = true; 00153 } 00154 00155 void 00156 Ipv4Interface::SetDown (void) 00157 { 00158 NS_LOG_FUNCTION_NOARGS (); 00159 m_ifup = false; 00160 } 00161 00162 // public wrapper on private virtual function 00163 void 00164 Ipv4Interface::Send(Ptr<Packet> p, Ipv4Address dest) 00165 { 00166 NS_LOG_FUNCTION_NOARGS (); 00167 if (IsUp()) { 00168 NS_LOG_LOGIC ("SendTo"); 00169 SendTo(p, dest); 00170 } 00171 } 00172 00173 }; // namespace ns3 00174