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
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2007-2008 Louis Pasteur University
     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: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
    19  */
    20 
    21 #include "inet6-socket-address.h"
    22 #include "ns3/assert.h"
    23 
    24 namespace ns3 {
    25 
    26 Inet6SocketAddress::Inet6SocketAddress (Ipv6Address ipv6, uint16_t port)
    27 : m_ipv6(ipv6),
    28   m_port(port)
    29 {
    30 }
    31 
    32 Inet6SocketAddress::Inet6SocketAddress (Ipv6Address ipv6)
    33 : m_ipv6(ipv6),
    34   m_port(0)
    35 {
    36 }
    37 
    38   Inet6SocketAddress::Inet6SocketAddress (const char* ipv6, uint16_t port)
    39 : m_ipv6(Ipv6Address(ipv6)),
    40   m_port(port)
    41 {
    42 }
    43 
    44   Inet6SocketAddress::Inet6SocketAddress (const char* ipv6)
    45 : m_ipv6(Ipv6Address(ipv6)),
    46   m_port(0)
    47 {
    48 }
    49 
    50   Inet6SocketAddress::Inet6SocketAddress (uint16_t port)
    51 : m_ipv6(Ipv6Address::GetAny()),
    52   m_port(port)
    53 {
    54 }
    55 
    56 uint16_t Inet6SocketAddress::GetPort (void) const
    57 {
    58   return m_port;
    59 }
    60 
    61 void Inet6SocketAddress::SetPort (uint16_t port)
    62 {
    63   m_port=port;
    64 }
    65 
    66 Ipv6Address Inet6SocketAddress::GetIpv6 (void) const
    67 {
    68   return m_ipv6;
    69 }
    70 
    71 void Inet6SocketAddress::SetIpv6 (Ipv6Address ipv6)
    72 {
    73   m_ipv6=ipv6;
    74 }
    75 
    76 bool Inet6SocketAddress::IsMatchingType (const Address &addr)
    77 {
    78   return addr.CheckCompatible(GetType(), 18); /* 16 (address) + 2  (port) */
    79 }
    80 
    81 Inet6SocketAddress::operator Address (void) const
    82 {
    83   return ConvertTo();
    84 }
    85 
    86 Address Inet6SocketAddress::ConvertTo (void) const
    87 {
    88   uint8_t buf[18];
    89   m_ipv6.Serialize(buf);
    90   buf[16]=m_port & 0xff;
    91   buf[17]=(m_port >> 8) &0xff;
    92   return Address(GetType(), buf, 18);
    93 }
    94 
    95 Inet6SocketAddress Inet6SocketAddress::ConvertFrom (const Address &addr)
    96 {
    97   NS_ASSERT(addr.CheckCompatible(GetType(), 18));
    98   uint8_t buf[18];
    99   addr.CopyTo(buf);
   100   Ipv6Address ipv6=Ipv6Address::Deserialize(buf);
   101   uint16_t port= buf[16] | (buf[17] << 8);
   102   return Inet6SocketAddress(ipv6, port);
   103 }
   104 
   105 uint8_t Inet6SocketAddress::GetType (void)
   106 {
   107   static uint8_t type=Address::Register();
   108   return type;
   109 }
   110 
   111 } /* namespace ns3 */
   112