View | Details | Raw Unified | Return to bug 69
Collapse All | Expand All

(-)a/src/node/ipv4-address.cc (+12 lines)
 Lines 99-104   Ipv4Mask::SetHostOrder (uint32_t value) Link Here 
99
{
99
{
100
  m_mask = value;
100
  m_mask = value;
101
}
101
}
102
uint32_t 
103
Ipv4Mask::GetInverse (void) const
104
{
105
  return ~m_mask;
106
}
102
107
103
void 
108
void 
104
Ipv4Mask::Print (std::ostream &os) const
109
Ipv4Mask::Print (std::ostream &os) const
 Lines 161-166   Ipv4Address::CombineMask (Ipv4Mask const Link Here 
161
{
166
{
162
  return Ipv4Address (GetHostOrder () & mask.GetHostOrder ());
167
  return Ipv4Address (GetHostOrder () & mask.GetHostOrder ());
163
}
168
}
169
170
Ipv4Address 
171
Ipv4Address::GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const
172
{
173
  return Ipv4Address (GetHostOrder () | mask.GetInverse ());
174
}
175
164
176
165
bool
177
bool
166
Ipv4Address::IsBroadcast (void) const
178
Ipv4Address::IsBroadcast (void) const
(-)a/src/node/ipv4-address.h (+13 lines)
 Lines 119-124   public: Link Here 
119
   * \param mask a network mask 
119
   * \param mask a network mask 
120
   */
120
   */
121
  Ipv4Address CombineMask (Ipv4Mask const &mask) const;
121
  Ipv4Address CombineMask (Ipv4Mask const &mask) const;
122
  /**
123
   * \brief Generate subnet-directed broadcast address corresponding to mask
124
   *
125
   * The subnet-directed broadcast address has the host bits set to all
126
   * ones.
127
   *
128
   * \param mask a network mask 
129
   */
130
  Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
122
131
123
  static bool IsMatchingType (const Address &address);
132
  static bool IsMatchingType (const Address &address);
124
  operator Address ();
133
  operator Address ();
 Lines 151-156   public: Link Here 
151
   */
160
   */
152
  uint32_t GetHostOrder (void) const;
161
  uint32_t GetHostOrder (void) const;
153
  void SetHostOrder (uint32_t value);
162
  void SetHostOrder (uint32_t value);
163
  /**
164
   * \brief Return the inverse mask in host order. 
165
   */
166
  uint32_t GetInverse (void) const;
154
167
155
  void Print (std::ostream &os) const;
168
  void Print (std::ostream &os) const;
156
169
(-)a/src/internet-node/arp-ipv4-interface.cc (-2 / +3 lines)
 Lines 62-69   ArpIpv4Interface::SendTo (Packet p, Ipv4 Link Here 
62
      Ptr<ArpL3Protocol> arp = m_node->QueryInterface<ArpL3Protocol> (ArpL3Protocol::iid);
62
      Ptr<ArpL3Protocol> arp = m_node->QueryInterface<ArpL3Protocol> (ArpL3Protocol::iid);
63
      Address hardwareDestination;
63
      Address hardwareDestination;
64
      bool found;
64
      bool found;
65
65
      
66
      if (dest.IsBroadcast ())
66
      if (dest.IsBroadcast () || 
67
          dest.IsSubnetDirectedBroadcast (GetNetworkMask ()) )
67
        {
68
        {
68
          hardwareDestination = GetDevice ()->GetBroadcast ();
69
          hardwareDestination = GetDevice ()->GetBroadcast ();
69
          found = true;
70
          found = true;
(-)a/src/internet-node/udp-socket.cc (-2 / +18 lines)
 Lines 214-219   int Link Here 
214
int
214
int
215
UdpSocket::DoSendTo (const Packet &p, Ipv4Address dest, uint16_t port)
215
UdpSocket::DoSendTo (const Packet &p, Ipv4Address dest, uint16_t port)
216
{
216
{
217
  Ipv4Route routeToDest;
217
  if (m_endPoint == 0)
218
  if (m_endPoint == 0)
218
    {
219
    {
219
      if (Bind () == -1)
220
      if (Bind () == -1)
 Lines 228-235   UdpSocket::DoSendTo (const Packet &p, Ip Link Here 
228
      m_errno = ERROR_SHUTDOWN;
229
      m_errno = ERROR_SHUTDOWN;
229
      return -1;
230
      return -1;
230
    }
231
    }
231
  Ipv4Route routeToDest;
232
  //
232
  if (GetIpv4RouteToDestination (m_node, routeToDest, dest) )
233
  // If dest is sent to the limited broadcast address (all ones),
234
  // convert it to send a copy of the packet out of every interface
235
  //
236
  if (dest.IsBroadcast ())
237
    {
238
      Ptr<Ipv4> ipv4 = m_node->QueryInterface<Ipv4> (Ipv4::iid);
239
      for (uint32_t i = 0; i < ipv4->GetNInterfaces (); i++ )
240
        {
241
          Ipv4Address addri = ipv4->GetAddress (i);
242
          Ipv4Mask maski = ipv4->GetNetworkMask (i);
243
          m_udp->Send (p, addri, addri.GetSubnetDirectedBroadcast (maski),
244
                       m_endPoint->GetLocalPort (), port);
245
          NotifyDataSent (p.GetSize ());
246
        }
247
    }
248
  else if (GetIpv4RouteToDestination (m_node, routeToDest, dest) )
233
    {
249
    {
234
      uint32_t localIfIndex = routeToDest.GetInterface ();
250
      uint32_t localIfIndex = routeToDest.GetInterface ();
235
      Ptr<Ipv4> ipv4 = m_node->QueryInterface<Ipv4> (Ipv4::iid);
251
      Ptr<Ipv4> ipv4 = m_node->QueryInterface<Ipv4> (Ipv4::iid);
(-)a/src/node/ipv4-address.cc (+5 lines)
 Lines 173-178   Ipv4Address::GetSubnetDirectedBroadcast Link Here 
173
  return Ipv4Address (GetHostOrder () | mask.GetInverse ());
173
  return Ipv4Address (GetHostOrder () | mask.GetInverse ());
174
}
174
}
175
175
176
bool
177
Ipv4Address::IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const
178
{
179
  return ( (GetHostOrder () | mask.GetInverse ()) == GetHostOrder () );
180
}
176
181
177
bool
182
bool
178
Ipv4Address::IsBroadcast (void) const
183
Ipv4Address::IsBroadcast (void) const
(-)a/src/node/ipv4-address.h (+1 lines)
 Lines 128-133   public: Link Here 
128
   * \param mask a network mask 
128
   * \param mask a network mask 
129
   */
129
   */
130
  Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
130
  Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
131
  bool IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
131
132
132
  static bool IsMatchingType (const Address &address);
133
  static bool IsMatchingType (const Address &address);
133
  operator Address ();
134
  operator Address ();
(-)b59dd2a565da (+159 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 */
16
17
//
18
// Example of the sending of a datagram to a broadcast address
19
//
20
// Network topology
21
//     ==============
22
//       |          |
23
//       n0    n1   n2   
24
//       |     |       
25
//     ==========
26
//
27
//   n0 originates UDP broadcast to 255.255.255.255, which is replicated 
28
//   and received on both n1 and n2
29
30
#include <iostream>
31
#include <fstream>
32
#include <string>
33
#include <cassert>
34
35
#include "ns3/command-line.h"
36
#include "ns3/default-value.h"
37
#include "ns3/ptr.h"
38
#include "ns3/random-variable.h"
39
#include "ns3/debug.h"
40
41
#include "ns3/simulator.h"
42
#include "ns3/nstime.h"
43
#include "ns3/data-rate.h"
44
45
#include "ns3/ascii-trace.h"
46
#include "ns3/pcap-trace.h"
47
#include "ns3/internet-node.h"
48
#include "ns3/csma-channel.h"
49
#include "ns3/csma-net-device.h"
50
#include "ns3/csma-topology.h"
51
#include "ns3/csma-ipv4-topology.h"
52
#include "ns3/eui48-address.h"
53
#include "ns3/ipv4-address.h"
54
#include "ns3/inet-socket-address.h"
55
#include "ns3/ipv4.h"
56
#include "ns3/socket.h"
57
#include "ns3/ipv4-route.h"
58
#include "ns3/onoff-application.h"
59
60
61
using namespace ns3;
62
63
64
int main (int argc, char *argv[])
65
{
66
67
  // Users may find it convenient to turn on explicit debugging
68
  // for selected modules; the below lines suggest how to do this
69
#if 0 
70
  DebugComponentEnable("CsmaNetDevice");
71
  DebugComponentEnable("Ipv4L3Protocol");
72
  DebugComponentEnable("NetDevice");
73
  DebugComponentEnable("Channel");
74
  DebugComponentEnable("CsmaChannel");
75
  DebugComponentEnable("PacketSocket");
76
#endif
77
78
  // Set up some default values for the simulation.  Use the Bind()
79
  // technique to tell the system what subclass of Queue to use,
80
  // and what the queue limit is
81
82
  // The below Bind command tells the queue factory which class to
83
  // instantiate, when the queue factory is invoked in the topology code
84
  DefaultValue::Bind ("Queue", "DropTailQueue");
85
86
  // Allow the user to override any of the defaults and the above
87
  // Bind()s at run-time, via command-line arguments
88
  CommandLine::Parse (argc, argv);
89
90
  // Here, we will explicitly create four nodes.  In more sophisticated
91
  // topologies, we could configure a node factory.
92
  Ptr<Node> n0 = Create<InternetNode> ();
93
  Ptr<Node> n1 = Create<InternetNode> (); 
94
  Ptr<Node> n2 = Create<InternetNode> (); 
95
96
  // We create the channels first without any IP addressing information
97
  Ptr<CsmaChannel> channel0 = 
98
    CsmaTopology::CreateCsmaChannel(
99
      DataRate(5000000), MilliSeconds(2));
100
101
  // We create the channels first without any IP addressing information
102
  Ptr<CsmaChannel> channel1 = 
103
    CsmaTopology::CreateCsmaChannel(
104
      DataRate(5000000), MilliSeconds(2));
105
106
  uint32_t n0ifIndex0 = CsmaIpv4Topology::AddIpv4CsmaNode (n0, channel0, 
107
                                         Eui48Address("10:54:23:54:0:50"));
108
  uint32_t n0ifIndex1 = CsmaIpv4Topology::AddIpv4CsmaNode (n0, channel1, 
109
                                         Eui48Address("10:54:23:54:0:51"));
110
  uint32_t n1ifIndex = CsmaIpv4Topology::AddIpv4CsmaNode (n1, channel0,
111
                                         Eui48Address("10:54:23:54:23:51"));
112
  uint32_t n2ifIndex = CsmaIpv4Topology::AddIpv4CsmaNode (n2, channel1,
113
                                         Eui48Address("10:54:23:54:23:52"));
114
115
  // Later, we add IP addresses.  
116
  CsmaIpv4Topology::AddIpv4Address (
117
      n0, n0ifIndex0, Ipv4Address("10.1.0.1"), Ipv4Mask("255.255.0.0"));
118
119
  CsmaIpv4Topology::AddIpv4Address (
120
      n1, n1ifIndex, Ipv4Address("10.1.0.2"), Ipv4Mask("255.255.0.0"));
121
122
  CsmaIpv4Topology::AddIpv4Address (
123
      n0, n0ifIndex1, Ipv4Address("192.168.1.1"), Ipv4Mask("255.255.255.0"));
124
  
125
  CsmaIpv4Topology::AddIpv4Address (
126
      n2, n2ifIndex, Ipv4Address("192.168.1.2"), Ipv4Mask("255.255.255.0"));
127
128
  // Create the OnOff application to send UDP datagrams of size
129
  // 210 bytes at a rate of 448 Kb/s
130
  // from n0 to n1
131
  Ptr<OnOffApplication> ooff = Create<OnOffApplication> (
132
    n0, 
133
    InetSocketAddress ("255.255.255.255", 80), 
134
    "Udp",
135
    ConstantVariable(1), 
136
    ConstantVariable(0));
137
  // Start the application
138
  ooff->Start(Seconds(1.0));
139
  ooff->Stop (Seconds(10.0));
140
141
 
142
  // Configure tracing of all enqueue, dequeue, and NetDevice receive events
143
  // Trace output will be sent to the csma-broadcast.tr file
144
  AsciiTrace asciitrace ("csma-broadcast.tr");
145
  asciitrace.TraceAllNetDeviceRx ();
146
  asciitrace.TraceAllQueues ();
147
148
  // Also configure some tcpdump traces; each interface will be traced
149
  // The output files will be named 
150
  // simple-point-to-point.pcap-<nodeId>-<interfaceId>
151
  // and can be read by the "tcpdump -r" command (use "-tt" option to
152
  // display timestamps correctly)
153
  PcapTrace pcaptrace ("csma-broadcast.pcap");
154
  pcaptrace.TraceAllIp ();
155
156
  Simulator::Run ();
157
    
158
  Simulator::Destroy ();
159
}
(-)a/examples/wscript (+4 lines)
 Lines 14-19   def build(bld): Link Here 
14
        ['csma', 'internet-node'])
14
        ['csma', 'internet-node'])
15
    obj.source = 'csma-one-subnet.cc'
15
    obj.source = 'csma-one-subnet.cc'
16
16
17
    obj = bld.create_ns3_program('csma-broadcast',
18
        ['csma', 'internet-node'])
19
    obj.source = 'csma-broadcast.cc'
20
17
    obj = bld.create_ns3_program('csma-packet-socket',
21
    obj = bld.create_ns3_program('csma-packet-socket',
18
        ['csma', 'internet-node'])
22
        ['csma', 'internet-node'])
19
    obj.source = 'csma-packet-socket.cc'
23
    obj.source = 'csma-packet-socket.cc'
(-)a/src/applications/onoff-application.cc (-2 / +3 lines)
 Lines 141-147   void OnOffApplication::StartApplication( Link Here 
141
      Ptr<SocketFactory> socketFactory = GetNode ()->QueryInterface<SocketFactory> (iid);
141
      Ptr<SocketFactory> socketFactory = GetNode ()->QueryInterface<SocketFactory> (iid);
142
      m_socket = socketFactory->CreateSocket ();
142
      m_socket = socketFactory->CreateSocket ();
143
      m_socket->Bind ();
143
      m_socket->Bind ();
144
      m_socket->Connect (m_peer);
144
      //m_socket->Connect (m_peer);
145
    }
145
    }
146
  // Insure no pending event
146
  // Insure no pending event
147
  StopApplication();
147
  StopApplication();
 Lines 206-212   void OnOffApplication::SendPacket() Link Here 
206
void OnOffApplication::SendPacket()
206
void OnOffApplication::SendPacket()
207
{
207
{
208
  NS_ASSERT (m_sendEvent.IsExpired ());
208
  NS_ASSERT (m_sendEvent.IsExpired ());
209
  m_socket->Send(Packet (m_pktSize));
209
  // XXX m_socket->Send(Packet (m_pktSize));
210
  m_socket->SendTo(m_peer, Packet (m_pktSize));
210
  m_totBytes += m_pktSize;
211
  m_totBytes += m_pktSize;
211
  m_lastStartTime = Simulator::Now();
212
  m_lastStartTime = Simulator::Now();
212
  m_residualBits = 0;
213
  m_residualBits = 0;
(-)a/src/applications/onoff-application.cc (-3 / +2 lines)
 Lines 141-147   void OnOffApplication::StartApplication( Link Here 
141
      Ptr<SocketFactory> socketFactory = GetNode ()->QueryInterface<SocketFactory> (iid);
141
      Ptr<SocketFactory> socketFactory = GetNode ()->QueryInterface<SocketFactory> (iid);
142
      m_socket = socketFactory->CreateSocket ();
142
      m_socket = socketFactory->CreateSocket ();
143
      m_socket->Bind ();
143
      m_socket->Bind ();
144
      //m_socket->Connect (m_peer);
144
      m_socket->Connect (m_peer);
145
    }
145
    }
146
  // Insure no pending event
146
  // Insure no pending event
147
  StopApplication();
147
  StopApplication();
 Lines 206-213   void OnOffApplication::SendPacket() Link Here 
206
void OnOffApplication::SendPacket()
206
void OnOffApplication::SendPacket()
207
{
207
{
208
  NS_ASSERT (m_sendEvent.IsExpired ());
208
  NS_ASSERT (m_sendEvent.IsExpired ());
209
  // XXX m_socket->Send(Packet (m_pktSize));
209
  m_socket->Send(Packet (m_pktSize));
210
  m_socket->SendTo(m_peer, Packet (m_pktSize));
211
  m_totBytes += m_pktSize;
210
  m_totBytes += m_pktSize;
212
  m_lastStartTime = Simulator::Now();
211
  m_lastStartTime = Simulator::Now();
213
  m_residualBits = 0;
212
  m_residualBits = 0;
(-)a/src/internet-node/udp-socket.cc (-12 / +14 lines)
 Lines 153-162   UdpSocket::Connect(const Address & addre Link Here 
153
  InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
153
  InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
154
  m_defaultAddress = transport.GetIpv4 ();
154
  m_defaultAddress = transport.GetIpv4 ();
155
  m_defaultPort = transport.GetPort ();
155
  m_defaultPort = transport.GetPort ();
156
  if (m_defaultAddress.IsBroadcast () )
157
    {
158
      NS_ASSERT_MSG(false, "UdpSocket::Connect, can't connect to broadcast");
159
    }
160
  NotifyConnectionSucceeded ();
156
  NotifyConnectionSucceeded ();
161
  m_connected = true;
157
  m_connected = true;
162
  if (GetIpv4RouteToDestination (m_node, routeToDest, m_defaultAddress) )
158
  if (GetIpv4RouteToDestination (m_node, routeToDest, m_defaultAddress) )
 Lines 196-215   UdpSocket::DoSend (const Packet &p) Link Here 
196
      m_errno = ERROR_SHUTDOWN;
192
      m_errno = ERROR_SHUTDOWN;
197
      return -1;
193
      return -1;
198
    } 
194
    } 
199
  m_udp->Send (p, m_endPoint->GetLocalAddress (), m_defaultAddress,
195
  
200
                  m_endPoint->GetLocalPort (), m_defaultPort);
196
  return DoSendTo (p, m_defaultAddress, m_defaultPort);
201
  NotifyDataSent (p.GetSize ());
202
  return 0;
203
}
197
}
204
198
205
199
206
int
200
int
207
UdpSocket::DoSendTo (const Packet &p, const Address &address)
201
UdpSocket::DoSendTo (const Packet &p, const Address &address)
208
{
202
{
209
  InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
203
  if (!m_connected)
210
  Ipv4Address ipv4 = transport.GetIpv4 ();
204
    {
211
  uint16_t port = transport.GetPort ();
205
      InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
212
  return DoSendTo (p, ipv4, port);
206
      Ipv4Address ipv4 = transport.GetIpv4 ();
207
      uint16_t port = transport.GetPort ();
208
      return DoSendTo (p, ipv4, port);
209
    }
210
  else
211
    {
212
      // connected UDP socket must use default addresses
213
      return DoSendTo (p, m_defaultAddress, m_defaultPort);
214
    }
213
}
215
}
214
int
216
int
215
UdpSocket::DoSendTo (const Packet &p, Ipv4Address dest, uint16_t port)
217
UdpSocket::DoSendTo (const Packet &p, Ipv4Address dest, uint16_t port)

Return to bug 69