A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
address-utils.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "address-utils.h"
21 #include "inet-socket-address.h"
22 #include "ns3/log.h"
23 
24 NS_LOG_COMPONENT_DEFINE ("AddressUtils");
25 
26 namespace ns3 {
27 
28 void WriteTo (Buffer::Iterator &i, Ipv4Address ad)
29 {
30  NS_LOG_FUNCTION (&i << &ad);
31  i.WriteHtonU32 (ad.Get ());
32 }
33 void WriteTo (Buffer::Iterator &i, Ipv6Address ad)
34 {
35  NS_LOG_FUNCTION (&i << &ad);
36  uint8_t buf[16];
37  ad.GetBytes (buf);
38  i.Write (buf, 16);
39 }
40 void WriteTo (Buffer::Iterator &i, const Address &ad)
41 {
42  NS_LOG_FUNCTION (&i << &ad);
43  uint8_t mac[Address::MAX_SIZE];
44  ad.CopyTo (mac);
45  i.Write (mac, ad.GetLength ());
46 }
47 void WriteTo (Buffer::Iterator &i, Mac48Address ad)
48 {
49  NS_LOG_FUNCTION (&i << &ad);
50  uint8_t mac[6];
51  ad.CopyTo (mac);
52  i.Write (mac, 6);
53 }
54 
55 void ReadFrom (Buffer::Iterator &i, Ipv4Address &ad)
56 {
57  NS_LOG_FUNCTION (&i << &ad);
58  ad.Set (i.ReadNtohU32 ());
59 }
60 void ReadFrom (Buffer::Iterator &i, Ipv6Address &ad)
61 {
62  NS_LOG_FUNCTION (&i << &ad);
63  uint8_t ipv6[16];
64  i.Read (ipv6, 16);
65  ad.Set (ipv6);
66 }
67 void ReadFrom (Buffer::Iterator &i, Address &ad, uint32_t len)
68 {
69  NS_LOG_FUNCTION (&i << &ad << len);
70  uint8_t mac[Address::MAX_SIZE];
71  i.Read (mac, len);
72  ad.CopyFrom (mac, len);
73 }
74 void ReadFrom (Buffer::Iterator &i, Mac48Address &ad)
75 {
76  NS_LOG_FUNCTION (&i << &ad);
77  uint8_t mac[6];
78  i.Read (mac, 6);
79  ad.CopyFrom (mac);
80 }
81 
82 namespace addressUtils {
83 
84 bool IsMulticast (const Address &ad)
85 {
86  NS_LOG_FUNCTION (&ad);
88  {
89  InetSocketAddress inetAddr = InetSocketAddress::ConvertFrom (ad);
90  Ipv4Address ipv4 = inetAddr.GetIpv4 ();
91  return ipv4.IsMulticast ();
92  }
93  // IPv6 case can go here, in future
94  return false;
95 }
96 
97 } // namespace addressUtils
98 
99 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
static InetSocketAddress ConvertFrom(const Address &address)
static bool IsMatchingType(const Address &address)