A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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"
23
24#include "ns3/log.h"
25
26namespace ns3
27{
28
29NS_LOG_COMPONENT_DEFINE("AddressUtils");
30
31void
33{
34 NS_LOG_FUNCTION(&i << &ad);
35 i.WriteHtonU32(ad.Get());
36}
37
38void
40{
41 NS_LOG_FUNCTION(&i << &ad);
42 uint8_t buf[16];
43 ad.GetBytes(buf);
44 i.Write(buf, 16);
45}
46
47void
49{
50 NS_LOG_FUNCTION(&i << &ad);
51 uint8_t mac[Address::MAX_SIZE];
52 ad.CopyTo(mac);
53 i.Write(mac, ad.GetLength());
54}
55
56void
58{
59 NS_LOG_FUNCTION(&i << &ad);
60 uint8_t mac[8];
61 ad.CopyTo(mac);
62 i.Write(mac, 8);
63}
64
65void
67{
68 NS_LOG_FUNCTION(&i << &ad);
69 uint8_t mac[6];
70 ad.CopyTo(mac);
71 i.Write(mac, 6);
72}
73
74void
76{
77 NS_LOG_FUNCTION(&i << &ad);
78 uint8_t mac[2];
79 ad.CopyTo(mac);
80 i.Write(mac + 1, 1);
81 i.Write(mac, 1);
82}
83
84void
86{
87 NS_LOG_FUNCTION(&i << &ad);
88 ad.Set(i.ReadNtohU32());
89}
90
91void
93{
94 NS_LOG_FUNCTION(&i << &ad);
95 uint8_t ipv6[16];
96 i.Read(ipv6, 16);
97 ad.Set(ipv6);
98}
99
100void
102{
103 NS_LOG_FUNCTION(&i << &ad << len);
104 uint8_t mac[Address::MAX_SIZE];
105 i.Read(mac, len);
106 ad.CopyFrom(mac, len);
107}
108
109void
111{
112 NS_LOG_FUNCTION(&i << &ad);
113 uint8_t mac[8];
114 i.Read(mac, 8);
115 ad.CopyFrom(mac);
116}
117
118void
120{
121 NS_LOG_FUNCTION(&i << &ad);
122 uint8_t mac[6];
123 i.Read(mac, 6);
124 ad.CopyFrom(mac);
125}
126
127void
129{
130 NS_LOG_FUNCTION(&i << &ad);
131 uint8_t mac[2];
132 i.Read(mac + 1, 1);
133 i.Read(mac, 1);
134 ad.CopyFrom(mac);
135}
136
137namespace addressUtils
138{
139
140bool
142{
143 NS_LOG_FUNCTION(&ad);
145 {
147 Ipv4Address ipv4 = inetAddr.GetIpv4();
148 return ipv4.IsMulticast();
149 }
150 else if (Ipv4Address::IsMatchingType(ad))
151 {
153 return ipv4.IsMulticast();
154 }
156 {
158 Ipv6Address ipv6 = inetAddr.GetIpv6();
159 return ipv6.IsMulticast();
160 }
161 else if (Ipv6Address::IsMatchingType(ad))
162 {
164 return ipv6.IsMulticast();
165 }
166
167 return false;
168}
169
170} // namespace addressUtils
171
172} // 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 Inet6 address class.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
Ipv6Address GetIpv6() const
Get the IPv6 address.
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
static Ipv4Address ConvertFrom(const Address &address)
static bool IsMatchingType(const Address &address)
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
bool IsMulticast() const
If the IPv6 address is multicast (ff00::/8).
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.
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.
static bool IsMatchingType(const Address &address)
If the Address matches the type.
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.