00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 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 #include "packet-socket-address.h" 00021 #include "net-device.h" 00022 00023 namespace ns3 { 00024 00025 PacketSocketAddress::PacketSocketAddress () 00026 {} 00027 void 00028 PacketSocketAddress::SetProtocol (uint16_t protocol) 00029 { 00030 m_protocol = protocol; 00031 } 00032 void 00033 PacketSocketAddress::SetAllDevices (void) 00034 { 00035 m_isSingleDevice = false; 00036 m_device = 0; 00037 } 00038 void 00039 PacketSocketAddress::SetSingleDevice (uint32_t index) 00040 { 00041 m_isSingleDevice = true; 00042 m_device = index; 00043 } 00044 void 00045 PacketSocketAddress::SetPhysicalAddress (const Address address) 00046 { 00047 m_address = address; 00048 } 00049 00050 uint16_t 00051 PacketSocketAddress::GetProtocol (void) const 00052 { 00053 return m_protocol; 00054 } 00055 bool 00056 PacketSocketAddress::IsSingleDevice (void) const 00057 { 00058 return m_isSingleDevice; 00059 } 00060 uint32_t 00061 PacketSocketAddress::GetSingleDevice (void) const 00062 { 00063 return m_device; 00064 } 00065 Address 00066 PacketSocketAddress::GetPhysicalAddress (void) const 00067 { 00068 return m_address; 00069 } 00070 00071 PacketSocketAddress::operator Address () const 00072 { 00073 return ConvertTo (); 00074 } 00075 00076 Address 00077 PacketSocketAddress::ConvertTo (void) const 00078 { 00079 Address address; 00080 uint8_t buffer[Address::MAX_SIZE]; 00081 buffer[0] = m_protocol & 0xff; 00082 buffer[1] = (m_protocol >> 8) & 0xff; 00083 buffer[2] = (m_device >> 24) & 0xff; 00084 buffer[3] = (m_device >> 16) & 0xff; 00085 buffer[4] = (m_device >> 8) & 0xff; 00086 buffer[5] = (m_device >> 0) & 0xff; 00087 buffer[6] = m_isSingleDevice?1:0; 00088 uint32_t copied = m_address.CopyAllTo (buffer + 7, Address::MAX_SIZE - 7); 00089 return Address (GetType (), buffer, 7 + copied); 00090 } 00091 PacketSocketAddress 00092 PacketSocketAddress::ConvertFrom (const Address &address) 00093 { 00094 NS_ASSERT (IsMatchingType (address)); 00095 uint8_t buffer[Address::MAX_SIZE]; 00096 address.CopyTo (buffer); 00097 uint16_t protocol = buffer[0] | (buffer[1] << 8); 00098 uint32_t device = 0; 00099 device |= buffer[2]; 00100 device <<= 8; 00101 device |= buffer[3]; 00102 device <<= 8; 00103 device |= buffer[4]; 00104 device <<= 8; 00105 device |= buffer[5]; 00106 bool isSingleDevice = (buffer[6] == 1)?true:false; 00107 Address physical; 00108 physical.CopyAllFrom (buffer + 7, Address::MAX_SIZE - 7); 00109 PacketSocketAddress ad; 00110 ad.SetProtocol (protocol); 00111 if (isSingleDevice) 00112 { 00113 ad.SetSingleDevice (device); 00114 } 00115 else 00116 { 00117 ad.SetAllDevices (); 00118 } 00119 ad.SetPhysicalAddress (physical); 00120 return ad; 00121 } 00122 bool 00123 PacketSocketAddress::IsMatchingType (const Address &address) 00124 { 00125 return address.IsMatchingType (GetType ()); 00126 } 00127 uint8_t 00128 PacketSocketAddress::GetType (void) 00129 { 00130 static uint8_t type = Address::Register (); 00131 return type; 00132 } 00133 00134 } // namespace ns3