Files
unison/src/internet-stack/ipv4-interface.cc

175 lines
3.4 KiB
C++
Raw Normal View History

2007-02-09 17:54:49 +01:00
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2005,2006,2007 INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
#include "ipv4-interface.h"
2007-05-04 15:04:07 +02:00
#include "ns3/ipv4-address.h"
#include "ns3/net-device.h"
2007-09-13 17:47:42 -07:00
#include "ns3/log.h"
#include "ns3/packet.h"
2007-08-12 22:41:24 -07:00
2007-09-13 17:47:42 -07:00
NS_LOG_COMPONENT_DEFINE ("Ipv4Interface");
2007-02-09 17:54:49 +01:00
namespace ns3 {
2008-04-09 17:28:19 -07:00
TypeId
Ipv4Interface::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::Ipv4Interface")
.SetParent<Object> ()
;
return tid;
}
2007-02-09 17:54:49 +01:00
/**
* By default, Ipv4 interface are created in the "down" state
* with ip address 192.168.0.1 and a matching mask. Before
* becoming useable, the user must invoke SetUp on them
* once the final Ipv4 address and mask has been set.
*/
2008-03-11 13:30:12 -07:00
Ipv4Interface::Ipv4Interface ()
: m_ifup(false),
m_metric(1)
2007-08-12 22:41:24 -07:00
{
NS_LOG_FUNCTION (this);
2007-08-12 22:41:24 -07:00
}
2007-02-09 17:54:49 +01:00
Ipv4Interface::~Ipv4Interface ()
2007-08-12 22:41:24 -07:00
{
NS_LOG_FUNCTION_NOARGS ();
2007-08-12 22:41:24 -07:00
}
2007-02-09 17:54:49 +01:00
void
Ipv4Interface::DoDispose (void)
2007-02-09 17:54:49 +01:00
{
NS_LOG_FUNCTION_NOARGS ();
Object::DoDispose ();
2007-02-09 17:54:49 +01:00
}
void
Ipv4Interface::SetAddress (Ipv4Address a)
{
NS_LOG_FUNCTION (this << a);
2007-02-09 17:54:49 +01:00
m_address = a;
}
2007-08-12 22:41:24 -07:00
2007-02-09 17:54:49 +01:00
void
Ipv4Interface::SetNetworkMask (Ipv4Mask mask)
{
NS_LOG_FUNCTION (this << mask);
2007-02-09 17:54:49 +01:00
m_netmask = mask;
}
Ipv4Address
Ipv4Interface::GetBroadcast (void) const
{
NS_LOG_FUNCTION_NOARGS ();
2008-05-29 09:37:07 -07:00
uint32_t mask = m_netmask.Get ();
uint32_t address = m_address.Get ();
2007-02-09 17:54:49 +01:00
Ipv4Address broadcast = Ipv4Address (address | (~mask));
return broadcast;
}
2007-08-12 22:41:24 -07:00
2007-02-09 17:54:49 +01:00
Ipv4Mask
Ipv4Interface::GetNetworkMask (void) const
{
NS_LOG_FUNCTION_NOARGS ();
2007-02-09 17:54:49 +01:00
return m_netmask;
}
2007-08-12 22:41:24 -07:00
void
Ipv4Interface::SetMetric (uint16_t metric)
{
NS_LOG_FUNCTION (metric);
m_metric = metric;
}
uint16_t
Ipv4Interface::GetMetric (void) const
{
NS_LOG_FUNCTION_NOARGS ();
return m_metric;
}
2007-02-09 17:54:49 +01:00
Ipv4Address
Ipv4Interface::GetAddress (void) const
{
NS_LOG_FUNCTION_NOARGS ();
2007-02-09 17:54:49 +01:00
return m_address;
}
uint16_t
Ipv4Interface::GetMtu (void) const
{
NS_LOG_FUNCTION_NOARGS ();
2008-03-11 13:30:12 -07:00
if (GetDevice () == 0)
2007-02-09 17:54:49 +01:00
{
uint32_t mtu = (1<<16) - 1;
return mtu;
}
2008-03-11 13:30:12 -07:00
return GetDevice ()->GetMtu ();
2007-02-09 17:54:49 +01:00
}
2007-08-12 22:41:24 -07:00
/**
* These are IP interface states and may be distinct from
* NetDevice states, such as found in real implementations
* (where the device may be down but IP interface state is still up).
*/
2007-02-09 17:54:49 +01:00
bool
Ipv4Interface::IsUp (void) const
{
NS_LOG_FUNCTION_NOARGS ();
2007-02-09 17:54:49 +01:00
return m_ifup;
}
bool
Ipv4Interface::IsDown (void) const
{
NS_LOG_FUNCTION_NOARGS ();
2007-02-09 17:54:49 +01:00
return !m_ifup;
}
void
Ipv4Interface::SetUp (void)
{
NS_LOG_FUNCTION_NOARGS ();
2007-02-09 17:54:49 +01:00
m_ifup = true;
}
void
Ipv4Interface::SetDown (void)
{
NS_LOG_FUNCTION_NOARGS ();
2007-02-09 17:54:49 +01:00
m_ifup = false;
}
// public wrapper on private virtual function
void
Ipv4Interface::Send(Ptr<Packet> p, Ipv4Address dest)
2007-02-09 17:54:49 +01:00
{
NS_LOG_FUNCTION_NOARGS ();
2007-02-09 17:54:49 +01:00
if (IsUp()) {
2007-09-13 17:47:42 -07:00
NS_LOG_LOGIC ("SendTo");
2007-02-09 17:54:49 +01:00
SendTo(p, dest);
}
}
}; // namespace ns3