A Discrete-Event Network Simulator
API
address-utils.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #include "address-utils.h"
20 
21 #include "inet-socket-address.h"
22 
23 #include "ns3/log.h"
24 
25 namespace ns3
26 {
27 
28 NS_LOG_COMPONENT_DEFINE("AddressUtils");
29 
30 void
32 {
33  NS_LOG_FUNCTION(&i << &ad);
34  i.WriteHtonU32(ad.Get());
35 }
36 
37 void
39 {
40  NS_LOG_FUNCTION(&i << &ad);
41  uint8_t buf[16];
42  ad.GetBytes(buf);
43  i.Write(buf, 16);
44 }
45 
46 void
48 {
49  NS_LOG_FUNCTION(&i << &ad);
50  uint8_t mac[Address::MAX_SIZE];
51  ad.CopyTo(mac);
52  i.Write(mac, ad.GetLength());
53 }
54 
55 void
57 {
58  NS_LOG_FUNCTION(&i << &ad);
59  uint8_t mac[8];
60  ad.CopyTo(mac);
61  i.Write(mac, 8);
62 }
63 
64 void
66 {
67  NS_LOG_FUNCTION(&i << &ad);
68  uint8_t mac[6];
69  ad.CopyTo(mac);
70  i.Write(mac, 6);
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION(&i << &ad);
77  uint8_t mac[2];
78  ad.CopyTo(mac);
79  i.Write(mac + 1, 1);
80  i.Write(mac, 1);
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION(&i << &ad);
87  ad.Set(i.ReadNtohU32());
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION(&i << &ad);
94  uint8_t ipv6[16];
95  i.Read(ipv6, 16);
96  ad.Set(ipv6);
97 }
98 
99 void
100 ReadFrom(Buffer::Iterator& i, Address& ad, uint32_t len)
101 {
102  NS_LOG_FUNCTION(&i << &ad << len);
103  uint8_t mac[Address::MAX_SIZE];
104  i.Read(mac, len);
105  ad.CopyFrom(mac, len);
106 }
107 
108 void
110 {
111  NS_LOG_FUNCTION(&i << &ad);
112  uint8_t mac[8];
113  i.Read(mac, 8);
114  ad.CopyFrom(mac);
115 }
116 
117 void
119 {
120  NS_LOG_FUNCTION(&i << &ad);
121  uint8_t mac[6];
122  i.Read(mac, 6);
123  ad.CopyFrom(mac);
124 }
125 
126 void
128 {
129  NS_LOG_FUNCTION(&i << &ad);
130  uint8_t mac[2];
131  i.Read(mac + 1, 1);
132  i.Read(mac, 1);
133  ad.CopyFrom(mac);
134 }
135 
136 namespace addressUtils
137 {
138 
139 bool
141 {
142  NS_LOG_FUNCTION(&ad);
144  {
146  Ipv4Address ipv4 = inetAddr.GetIpv4();
147  return ipv4.IsMulticast();
148  }
149  // IPv6 case can go here, in future
150  return false;
151 }
152 
153 } // namespace addressUtils
154 
155 } // namespace ns3
a polymophic address class
Definition: address.h:101
uint32_t CopyFrom(const uint8_t *buffer, uint8_t len)
Definition: address.cc:106
static constexpr uint32_t MAX_SIZE
The maximum size of a byte buffer which can be stored in an Address instance.
Definition: address.h:107
uint8_t GetLength() const
Get the length of the underlying address.
Definition: address.cc:78
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition: address.cc:86
iterator in a Buffer instance
Definition: buffer.h:100
void Write(const uint8_t *buffer, uint32_t size)
Definition: buffer.cc:948
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1125
uint32_t ReadNtohU32()
Definition: buffer.h:978
void WriteHtonU32(uint32_t data)
Definition: buffer.h:933
an Inet address class
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
void Set(uint32_t address)
input address is in host order.
uint32_t Get() const
Get the host-order 32-bit IP address.
Describes an IPv6 address.
Definition: ipv6-address.h:49
void GetBytes(uint8_t buf[16]) const
Get the bytes corresponding to the address.
void Set(const char *address)
Sets an Ipv6Address by parsing the input C-string.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
void CopyTo(uint8_t buffer[2]) const
void CopyFrom(const uint8_t buffer[2])
an EUI-48 address
Definition: mac48-address.h:46
void CopyFrom(const uint8_t buffer[6])
void CopyTo(uint8_t buffer[6]) const
an EUI-64 address
Definition: mac64-address.h:46
void CopyFrom(const uint8_t buffer[8])
void CopyTo(uint8_t buffer[8]) const
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
bool IsMulticast(const Address &ad)
Address family-independent test for a multicast address.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
mac
Definition: third.py:92