OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright 2007 University of Washington |
| 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 * Modified by: NIST |
| 19 */ |
| 20 |
| 21 #include "ns3/log.h" |
| 22 #include "ns3/ipv4-address.h" |
| 23 #include "ns3/ipv6-address.h" |
| 24 #include "ns3/address-utils.h" |
| 25 #include "ns3/nstime.h" |
| 26 #include "ns3/inet-socket-address.h" |
| 27 #include "ns3/inet6-socket-address.h" |
| 28 #include "ns3/socket.h" |
| 29 #include "ns3/udp-socket.h" |
| 30 #include "ns3/simulator.h" |
| 31 #include "ns3/socket-factory.h" |
| 32 #include "ns3/packet.h" |
| 33 #include "ns3/uinteger.h" |
| 34 |
| 35 #include "nist-udp-echo-server.h" |
| 36 |
| 37 namespace ns3 { |
| 38 |
| 39 NS_LOG_COMPONENT_DEFINE ("NistUdpEchoServerApplication"); |
| 40 |
| 41 NS_OBJECT_ENSURE_REGISTERED (NistUdpEchoServer); |
| 42 |
| 43 TypeId |
| 44 NistUdpEchoServer::GetTypeId (void) |
| 45 { |
| 46 static TypeId tid = TypeId ("ns3::NistUdpEchoServer") |
| 47 .SetParent<Application> () |
| 48 .AddConstructor<NistUdpEchoServer> () |
| 49 .AddAttribute ("Port", "Port on which we listen for incoming packets.", |
| 50 UintegerValue (9), |
| 51 MakeUintegerAccessor (&NistUdpEchoServer::m_port), |
| 52 MakeUintegerChecker<uint16_t> ()) |
| 53 .AddTraceSource ("Rx", "A packet has been received", |
| 54 MakeTraceSourceAccessor (&NistUdpEchoServer::m_rxTrace), |
| 55 "ns3::Packet::PacketAddressTracedCallback") |
| 56 .AddTraceSource ("RxWithAddresses", "A packet has been received", |
| 57 MakeTraceSourceAccessor (&NistUdpEchoServer::m_rxTraceWithA
ddresses), |
| 58 "ns3::NistUdpEchoServer::PacketAddressTracedCallback") |
| 59 ; |
| 60 return tid; |
| 61 } |
| 62 |
| 63 NistUdpEchoServer::NistUdpEchoServer () |
| 64 { |
| 65 NS_LOG_FUNCTION (this); |
| 66 } |
| 67 |
| 68 NistUdpEchoServer::~NistUdpEchoServer() |
| 69 { |
| 70 NS_LOG_FUNCTION (this); |
| 71 m_socket = 0; |
| 72 m_socket6 = 0; |
| 73 } |
| 74 |
| 75 void |
| 76 NistUdpEchoServer::DoDispose (void) |
| 77 { |
| 78 NS_LOG_FUNCTION (this); |
| 79 Application::DoDispose (); |
| 80 } |
| 81 |
| 82 void· |
| 83 NistUdpEchoServer::StartApplication (void) |
| 84 { |
| 85 NS_LOG_FUNCTION (this); |
| 86 |
| 87 if (m_socket == 0) |
| 88 { |
| 89 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
| 90 m_socket = Socket::CreateSocket (GetNode (), tid); |
| 91 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), m_por
t); |
| 92 m_socket->Bind (local); |
| 93 if (addressUtils::IsMulticast (m_local)) |
| 94 { |
| 95 Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket); |
| 96 if (udpSocket) |
| 97 { |
| 98 // equivalent to setsockopt (MCAST_JOIN_GROUP) |
| 99 udpSocket->MulticastJoinGroup (0, m_local); |
| 100 } |
| 101 else |
| 102 { |
| 103 NS_FATAL_ERROR ("Error: Failed to join multicast group"); |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 if (m_socket6 == 0) |
| 109 { |
| 110 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
| 111 m_socket6 = Socket::CreateSocket (GetNode (), tid); |
| 112 Inet6SocketAddress local6 = Inet6SocketAddress (Ipv6Address::GetAny (), m_
port); |
| 113 m_socket6->Bind (local6); |
| 114 if (addressUtils::IsMulticast (local6)) |
| 115 { |
| 116 Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket6); |
| 117 if (udpSocket) |
| 118 { |
| 119 // equivalent to setsockopt (MCAST_JOIN_GROUP) |
| 120 udpSocket->MulticastJoinGroup (0, local6); |
| 121 } |
| 122 else |
| 123 { |
| 124 NS_FATAL_ERROR ("Error: Failed to join multicast group"); |
| 125 } |
| 126 } |
| 127 } |
| 128 |
| 129 m_socket->SetRecvCallback (MakeCallback (&NistUdpEchoServer::HandleRead, this)
); |
| 130 m_socket6->SetRecvCallback (MakeCallback (&NistUdpEchoServer::HandleRead, this
)); |
| 131 } |
| 132 |
| 133 void· |
| 134 NistUdpEchoServer::StopApplication () |
| 135 { |
| 136 NS_LOG_FUNCTION (this); |
| 137 |
| 138 if (m_socket != 0)· |
| 139 { |
| 140 m_socket->Close (); |
| 141 m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); |
| 142 } |
| 143 if (m_socket6 != 0)· |
| 144 { |
| 145 m_socket6->Close (); |
| 146 m_socket6->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); |
| 147 } |
| 148 } |
| 149 |
| 150 void· |
| 151 NistUdpEchoServer::HandleRead (Ptr<Socket> socket) |
| 152 { |
| 153 NS_LOG_FUNCTION (this << socket); |
| 154 |
| 155 Ptr<Packet> packet; |
| 156 Address from,localAddress; |
| 157 while ((packet = socket->RecvFrom (from))) |
| 158 { |
| 159 socket->GetSockName (localAddress); |
| 160 m_rxTrace (packet, from); |
| 161 m_rxTraceWithAddresses (packet, from, localAddress); |
| 162 |
| 163 if (InetSocketAddress::IsMatchingType (from)) |
| 164 { |
| 165 NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s serve
r received " << packet->GetSize () << " bytes from " << |
| 166 InetSocketAddress::ConvertFrom (from).GetIpv4 () << " por
t " << |
| 167 InetSocketAddress::ConvertFrom (from).GetPort ()); |
| 168 } |
| 169 else if (Inet6SocketAddress::IsMatchingType (from)) |
| 170 { |
| 171 NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s serve
r received " << packet->GetSize () << " bytes from " << |
| 172 Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " po
rt " << |
| 173 Inet6SocketAddress::ConvertFrom (from).GetPort ()); |
| 174 } |
| 175 |
| 176 packet->RemoveAllPacketTags (); |
| 177 packet->RemoveAllByteTags (); |
| 178 |
| 179 NS_LOG_LOGIC ("Echoing packet"); |
| 180 socket->SendTo (packet, 0, from); |
| 181 |
| 182 if (InetSocketAddress::IsMatchingType (from)) |
| 183 { |
| 184 NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s serve
r sent " << packet->GetSize () << " bytes to " << |
| 185 InetSocketAddress::ConvertFrom (from).GetIpv4 () << " por
t " << |
| 186 InetSocketAddress::ConvertFrom (from).GetPort ()); |
| 187 } |
| 188 else if (Inet6SocketAddress::IsMatchingType (from)) |
| 189 { |
| 190 NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s serve
r sent " << packet->GetSize () << " bytes to " << |
| 191 Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " po
rt " << |
| 192 Inet6SocketAddress::ConvertFrom (from).GetPort ()); |
| 193 } |
| 194 ·········· |
| 195 } |
| 196 } |
| 197 |
| 198 } // Namespace ns3 |
OLD | NEW |