src/node/inet6-socket-address.cc
author vincent@clarinet.u-strasbg.fr
Fri Nov 07 11:36:15 2008 -0800 (2008-11-07)
changeset 3852 9cf7ad0cac85
child 4731 510db8599bfb
permissions -rw-r--r--
Initial IPv6 capability
vincent@3852
     1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
vincent@3852
     2
/*
vincent@3852
     3
 * Copyright (c) 2007-2008 Louis Pasteur University
vincent@3852
     4
 *
vincent@3852
     5
 * This program is free software; you can redistribute it and/or modify
vincent@3852
     6
 * it under the terms of the GNU General Public License version 2 as
vincent@3852
     7
 * published by the Free Software Foundation;
vincent@3852
     8
 *
vincent@3852
     9
 * This program is distributed in the hope that it will be useful,
vincent@3852
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
vincent@3852
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
vincent@3852
    12
 * GNU General Public License for more details.
vincent@3852
    13
 *
vincent@3852
    14
 * You should have received a copy of the GNU General Public License
vincent@3852
    15
 * along with this program; if not, write to the Free Software
vincent@3852
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
vincent@3852
    17
 *
vincent@3852
    18
 * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
vincent@3852
    19
 */
vincent@3852
    20
vincent@3852
    21
#include "inet6-socket-address.h"
vincent@3852
    22
#include "ns3/assert.h"
vincent@3852
    23
vincent@3852
    24
namespace ns3 {
vincent@3852
    25
vincent@3852
    26
Inet6SocketAddress::Inet6SocketAddress (Ipv6Address ipv6, uint16_t port)
vincent@3852
    27
: m_ipv6(ipv6),
vincent@3852
    28
  m_port(port)
vincent@3852
    29
{
vincent@3852
    30
}
vincent@3852
    31
vincent@3852
    32
Inet6SocketAddress::Inet6SocketAddress (Ipv6Address ipv6)
vincent@3852
    33
: m_ipv6(ipv6),
vincent@3852
    34
  m_port(0)
vincent@3852
    35
{
vincent@3852
    36
}
vincent@3852
    37
vincent@3852
    38
  Inet6SocketAddress::Inet6SocketAddress (const char* ipv6, uint16_t port)
vincent@3852
    39
: m_ipv6(Ipv6Address(ipv6)),
vincent@3852
    40
  m_port(port)
vincent@3852
    41
{
vincent@3852
    42
}
vincent@3852
    43
vincent@3852
    44
  Inet6SocketAddress::Inet6SocketAddress (const char* ipv6)
vincent@3852
    45
: m_ipv6(Ipv6Address(ipv6)),
vincent@3852
    46
  m_port(0)
vincent@3852
    47
{
vincent@3852
    48
}
vincent@3852
    49
vincent@3852
    50
  Inet6SocketAddress::Inet6SocketAddress (uint16_t port)
vincent@3852
    51
: m_ipv6(Ipv6Address::GetAny()),
vincent@3852
    52
  m_port(port)
vincent@3852
    53
{
vincent@3852
    54
}
vincent@3852
    55
vincent@3852
    56
uint16_t Inet6SocketAddress::GetPort (void) const
vincent@3852
    57
{
vincent@3852
    58
  return m_port;
vincent@3852
    59
}
vincent@3852
    60
vincent@3852
    61
void Inet6SocketAddress::SetPort (uint16_t port)
vincent@3852
    62
{
vincent@3852
    63
  m_port=port;
vincent@3852
    64
}
vincent@3852
    65
vincent@3852
    66
Ipv6Address Inet6SocketAddress::GetIpv6 (void) const
vincent@3852
    67
{
vincent@3852
    68
  return m_ipv6;
vincent@3852
    69
}
vincent@3852
    70
vincent@3852
    71
void Inet6SocketAddress::SetIpv6 (Ipv6Address ipv6)
vincent@3852
    72
{
vincent@3852
    73
  m_ipv6=ipv6;
vincent@3852
    74
}
vincent@3852
    75
vincent@3852
    76
bool Inet6SocketAddress::IsMatchingType (const Address &addr)
vincent@3852
    77
{
vincent@3852
    78
  return addr.CheckCompatible(GetType(), 18); /* 16 (address) + 2  (port) */
vincent@3852
    79
}
vincent@3852
    80
vincent@3852
    81
Inet6SocketAddress::operator Address (void) const
vincent@3852
    82
{
vincent@3852
    83
  return ConvertTo();
vincent@3852
    84
}
vincent@3852
    85
vincent@3852
    86
Address Inet6SocketAddress::ConvertTo (void) const
vincent@3852
    87
{
vincent@3852
    88
  uint8_t buf[18];
vincent@3852
    89
  m_ipv6.Serialize(buf);
vincent@3852
    90
  buf[16]=m_port & 0xff;
vincent@3852
    91
  buf[17]=(m_port >> 8) &0xff;
vincent@3852
    92
  return Address(GetType(), buf, 18);
vincent@3852
    93
}
vincent@3852
    94
vincent@3852
    95
Inet6SocketAddress Inet6SocketAddress::ConvertFrom (const Address &addr)
vincent@3852
    96
{
vincent@3852
    97
  NS_ASSERT(addr.CheckCompatible(GetType(), 18));
vincent@3852
    98
  uint8_t buf[18];
vincent@3852
    99
  addr.CopyTo(buf);
vincent@3852
   100
  Ipv6Address ipv6=Ipv6Address::Deserialize(buf);
vincent@3852
   101
  uint16_t port= buf[16] | (buf[17] << 8);
vincent@3852
   102
  return Inet6SocketAddress(ipv6, port);
vincent@3852
   103
}
vincent@3852
   104
vincent@3852
   105
uint8_t Inet6SocketAddress::GetType (void)
vincent@3852
   106
{
vincent@3852
   107
  static uint8_t type=Address::Register();
vincent@3852
   108
  return type;
vincent@3852
   109
}
vincent@3852
   110
vincent@3852
   111
} /* namespace ns3 */
vincent@3852
   112