| 1 |
#include "nsProtoSimAgent.h" |
| 2 |
#include "ip.h" // for ns-2 hdr_ip def |
| 3 |
#include "flags.h" // for ns-2 hdr_flags def |
| 4 |
|
| 5 |
// (TBD) should "NsProtoSimAgent" specify a different packet type? |
| 6 |
NsProtoSimAgent::NsProtoSimAgent() |
| 7 |
: Agent(PT_UDP), scheduler(NULL) |
| 8 |
{ |
| 9 |
scheduler = &Scheduler::instance(); |
| 10 |
} |
| 11 |
|
| 12 |
NsProtoSimAgent::~NsProtoSimAgent() |
| 13 |
{ |
| 14 |
|
| 15 |
} |
| 16 |
|
| 17 |
int NsProtoSimAgent::command(int argc, const char*const* argv) |
| 18 |
{ |
| 19 |
if (argc >= 2) |
| 20 |
{ |
| 21 |
if (!strcmp("startup", argv[1])) |
| 22 |
{ |
| 23 |
if (OnStartup(argc-1, argv+1)) |
| 24 |
{ |
| 25 |
return TCL_OK; |
| 26 |
} |
| 27 |
else |
| 28 |
{ |
| 29 |
fprintf(stderr, "NsProtoSimAgent::command() bad command\n"); |
| 30 |
return TCL_ERROR; |
| 31 |
} |
| 32 |
} |
| 33 |
else if (!strcmp("shutdown", argv[1])) |
| 34 |
{ |
| 35 |
OnShutdown(); |
| 36 |
return TCL_OK; |
| 37 |
} |
| 38 |
else if (ProcessCommands(argc, argv)) |
| 39 |
{ |
| 40 |
return TCL_OK; |
| 41 |
} |
| 42 |
} |
| 43 |
return Agent::command(argc, argv); |
| 44 |
} // end NsProtoSimAgent::command() |
| 45 |
|
| 46 |
bool NsProtoSimAgent::InvokeSystemCommand(const char* cmd) |
| 47 |
{ |
| 48 |
PLOG(PL_DETAIL, "NsProtoSimAgent: invoking command \"%s\"\n", cmd); |
| 49 |
|
| 50 |
Tcl& tcl = Tcl::instance(); |
| 51 |
Agent* agent = dynamic_cast<Agent*>(this); |
| 52 |
tcl.evalf("%s set node_", agent->name()); |
| 53 |
const char* nodeName = tcl.result(); |
| 54 |
|
| 55 |
tcl.evalf("%s %s", nodeName, cmd); |
| 56 |
|
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
ProtoSimAgent::SocketProxy* NsProtoSimAgent::OpenSocket(ProtoSocket& theSocket) |
| 61 |
{ |
| 62 |
PLOG(PL_MAX, "NsProtoSimAgent::OpenSocket Entering \n"); |
| 63 |
|
| 64 |
// Create an appropriate ns-2 transport agent |
| 65 |
Tcl& tcl = Tcl::instance(); |
| 66 |
if (ProtoSocket::UDP == theSocket.GetProtocol()) |
| 67 |
tcl.evalf("eval new Agent/ProtoUdpSocket"); |
| 68 |
else |
| 69 |
tcl.evalf("eval new Agent/ProtoSocket/TCP"); |
| 70 |
|
| 71 |
const char* tclResult = tcl.result(); |
| 72 |
|
| 73 |
NSSocketProxy* socketAgent=NULL; |
| 74 |
|
| 75 |
if (tclResult!=NULL) |
| 76 |
socketAgent = dynamic_cast<NSSocketProxy*>(tcl.lookup(tclResult)); |
| 77 |
|
| 78 |
if (socketAgent) |
| 79 |
{ |
| 80 |
socketAgent->AttachSocket(theSocket); |
| 81 |
socket_proxy_list.Prepend(*socketAgent); |
| 82 |
return socketAgent; |
| 83 |
} |
| 84 |
else |
| 85 |
{ |
| 86 |
fprintf(stderr, "NsProtoSimAgent::OpenSocket() error creating socket agent\n"); |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
PLOG(PL_MAX, "NsProtoSimAgent::OpenSocket Exiting \n"); |
| 90 |
} // end NSProtoSimAgent::OpenSocket() |
| 91 |
|
| 92 |
void NsProtoSimAgent::CloseSocket(ProtoSocket& theSocket) |
| 93 |
{ |
| 94 |
PLOG(PL_MAX, "NsProtoSimAgent::CloseSocket Entering \n"); |
| 95 |
|
| 96 |
ASSERT(theSocket.IsOpen()); |
| 97 |
|
| 98 |
PLOG(PL_DETAIL, "NsProtoSimAgent : Closing Socket \n"); |
| 99 |
|
| 100 |
NSSocketProxy* socketAgent = static_cast<NSSocketProxy*>(theSocket.GetHandle()); |
| 101 |
|
| 102 |
// Let the socket decide how it is to be closed using an over-ridden socket-specific implementation |
| 103 |
// see UDP or TCP implementation |
| 104 |
|
| 105 |
socketAgent->Close(); |
| 106 |
|
| 107 |
socket_proxy_list.Remove(*socketAgent); |
| 108 |
delete socketAgent; |
| 109 |
PLOG(PL_MAX, "NsProtoSimAgent::CloseSocket Exiting \n"); |
| 110 |
|
| 111 |
} // end NSProtoSimAgent::OpenSocket() |
| 112 |
|
| 113 |
NsProtoSimAgent::NSSocketProxy::NSSocketProxy() |
| 114 |
: ecn_capable(false), ecn_status(false), |
| 115 |
mcast_ttl(255), mcast_loopback(false), |
| 116 |
recv_data(NULL), recv_data_len(0), recv_data_offset(0) |
| 117 |
{ |
| 118 |
} |
| 119 |
|
| 120 |
NsProtoSimAgent::NSSocketProxy::~NSSocketProxy() |
| 121 |
{ |
| 122 |
} |
| 123 |
|
| 124 |
bool NsProtoSimAgent::NSSocketProxy::Bind(UINT16& thePort) |
| 125 |
{ |
| 126 |
PLOG(PL_MAX, "NsProtoSimAgent::NSSocketProxy::Bind Entering \n"); |
| 127 |
|
| 128 |
ASSERT(proto_socket && proto_socket->IsOpen()); |
| 129 |
|
| 130 |
Agent* simAgent = dynamic_cast<Agent*>(proto_socket->GetNotifier()); |
| 131 |
Tcl& tcl = Tcl::instance(); |
| 132 |
tcl.evalf("%s set node_", simAgent->name()); |
| 133 |
const char* nodeName = tcl.result(); |
| 134 |
|
| 135 |
if (nodeName==NULL) |
| 136 |
return false; |
| 137 |
|
| 138 |
Agent* socketProxyAgent = dynamic_cast<Agent*>(this); |
| 139 |
|
| 140 |
if (thePort > 0) |
| 141 |
tcl.evalf("%s attach %s %hu", nodeName, socketProxyAgent->name(), thePort); |
| 142 |
else |
| 143 |
tcl.evalf("%s attach %s", nodeName, socketProxyAgent->name()); |
| 144 |
|
| 145 |
thePort = (UINT16)socketProxyAgent->port(); |
| 146 |
|
| 147 |
PLOG(PL_MAX, "NsProtoSimAgent::NSSocketProxy::Bind Exiting \n"); |
| 148 |
|
| 149 |
return true; |
| 150 |
} // end NsProtoSimAgent::NSSocketProxy::Bind() |
| 151 |
|
| 152 |
|
| 153 |
// New default close method for NSSocketProxy |
| 154 |
bool NsProtoSimAgent::NSSocketProxy::Close() { |
| 155 |
PLOG(PL_MAX, "NsProtoSimAgent::NSSocketProxy::Close Entering \n"); |
| 156 |
|
| 157 |
Tcl& tcl = Tcl::instance(); |
| 158 |
tcl.evalf("Simulator instance"); |
| 159 |
|
| 160 |
char simName[32]; |
| 161 |
strncpy(simName, tcl.result(), 32); |
| 162 |
|
| 163 |
Agent* simAgent = dynamic_cast<Agent*>(proto_socket->GetNotifier()); |
| 164 |
ASSERT(NULL != simAgent); |
| 165 |
tcl.evalf("%s set node_", simAgent->name()); |
| 166 |
const char* nodeName = tcl.result(); |
| 167 |
|
| 168 |
if (nodeName==NULL) |
| 169 |
return false; |
| 170 |
|
| 171 |
Agent* socketProxyAgent = dynamic_cast<Agent*>(this); |
| 172 |
|
| 173 |
PLOG(PL_MAX, "NsProtoSimAgent : Running: %s detach-agent %s %s\n", simName, nodeName, socketProxyAgent->name()); |
| 174 |
tcl.evalf("%s detach-agent %s %s", simName, nodeName, socketProxyAgent->name()); |
| 175 |
|
| 176 |
PLOG(PL_MAX, "NsProtoSimAgent::NSSocketProxy::Close Exiting \n"); |
| 177 |
return true; |
| 178 |
} // end NsProtoSimAgent::NSSocketProxy::Close() |
| 179 |
|
| 180 |
|
| 181 |
bool NsProtoSimAgent::NSSocketProxy::SetNodeColor(const char* color) |
| 182 |
{ |
| 183 |
PLOG(PL_DETAIL, "NsProtoSimAgent::NSSocketProxy::SetNodeColor Entering \n"); |
| 184 |
Agent* simAgent = dynamic_cast<Agent*>(proto_socket->GetNotifier()); |
| 185 |
Tcl& tcl = Tcl::instance(); |
| 186 |
tcl.evalf("%s set node_", simAgent->name()); |
| 187 |
const char* nodeName = tcl.result(); |
| 188 |
|
| 189 |
tcl.evalf("%s color %s", nodeName, color); |
| 190 |
return true; |
| 191 |
} // end NsProtoSimAgent:: SetNodeColor() |
| 192 |
|
| 193 |
bool NsProtoSimAgent::NSSocketProxy::RecvFrom(char* buffer, |
| 194 |
unsigned int& numBytes, |
| 195 |
ProtoAddress& srcAddr) |
| 196 |
{ |
| 197 |
PLOG(PL_MAX, "NsProtoSimAgent::NSSocketProxy::RecvFrom Entering \n"); |
| 198 |
if (recv_data) |
| 199 |
{ |
| 200 |
PLOG(PL_DETAIL, "NSSocketProxy::RecvFrom: bytes requested = %i\n", numBytes); |
| 201 |
PLOG(PL_DETAIL, "NSSocketProxy::RecvFrom: data in read buffer = %i, current readpoint = %i\n", recv_data_len, recv_data_offset); |
| 202 |
PLOG(PL_DETAIL, "NSSocketProxy::RecvFrom: Sender Address: %s, port %i\n", src_addr.GetHostString(), src_addr.GetPort()); |
| 203 |
|
| 204 |
unsigned long remainingBuffer = (unsigned long)recv_data_len-recv_data_offset; |
| 205 |
unsigned long readpoint= (unsigned long )recv_data+recv_data_offset; |
| 206 |
srcAddr = src_addr; |
| 207 |
|
| 208 |
if (numBytes >= remainingBuffer) |
| 209 |
{ |
| 210 |
numBytes = remainingBuffer; |
| 211 |
memcpy(buffer, (char *)readpoint, numBytes); |
| 212 |
recv_data = NULL; |
| 213 |
recv_data_len = 0; |
| 214 |
recv_data_offset=0; |
| 215 |
return true; |
| 216 |
} |
| 217 |
else // requested read is smaller than buffer, so read only part of the input data |
| 218 |
{ |
| 219 |
memcpy(buffer, (char *)readpoint, numBytes); |
| 220 |
recv_data_offset+=numBytes; |
| 221 |
return true; |
| 222 |
} |
| 223 |
} |
| 224 |
else // EOF |
| 225 |
{ |
| 226 |
numBytes = -1; // for compatibility with Java |
| 227 |
srcAddr = src_addr; |
| 228 |
recv_data = NULL; |
| 229 |
recv_data_len = 0; |
| 230 |
recv_data_offset=0; |
| 231 |
return false; |
| 232 |
} |
| 233 |
PLOG(PL_MAX, "NsProtoSimAgent::NSSocketProxy::RecvFrom Exiting \n"); |
| 234 |
} // end NsProtoSimAgent::NSSocketProxy::RecvFrom() |
| 235 |
|
| 236 |
bool NsProtoSimAgent::NSSocketProxy::JoinGroup(const ProtoAddress& groupAddr) |
| 237 |
{ |
| 238 |
if (groupAddr.IsBroadcast()) return true; // bcast membership implicit |
| 239 |
Agent* socketAgent = dynamic_cast<Agent*>(this); |
| 240 |
ASSERT(socketAgent); |
| 241 |
|
| 242 |
ProtoSocket* s = GetSocket(); |
| 243 |
if (!s->IsBound()) s->Bind(0); // can only join on bound sockets (TBD) return false instead??? |
| 244 |
|
| 245 |
Tcl& tcl = Tcl::instance(); |
| 246 |
tcl.evalf("%s set node_", socketAgent->name()); |
| 247 |
const char* nodeName = tcl.result(); |
| 248 |
tcl.evalf("%s join-group %s %d", nodeName, |
| 249 |
socketAgent->name(), groupAddr.SimGetAddress()); |
| 250 |
return true; |
| 251 |
} // end NsProtoSimAgent::NSSocketProxy::JoinGroup() |
| 252 |
|
| 253 |
bool NsProtoSimAgent::NSSocketProxy::LeaveGroup(const ProtoAddress& groupAddr) |
| 254 |
{ |
| 255 |
if (groupAddr.IsBroadcast()) return true; // bcast membership implicit |
| 256 |
Tcl& tcl = Tcl::instance(); |
| 257 |
Agent* socketAgent = dynamic_cast<Agent*>(this); |
| 258 |
ASSERT(socketAgent); |
| 259 |
tcl.evalf("%s set node_", socketAgent->name()); |
| 260 |
const char* nodeName = tcl.result(); |
| 261 |
tcl.evalf("%s leave-group %s %d", nodeName, |
| 262 |
socketAgent->name(), groupAddr.SimGetAddress()); |
| 263 |
return true; |
| 264 |
} // end NsProtoSimAgent::NSSocketProxy::LeaveGroup(() |
| 265 |
|
| 266 |
///////////////////////////////////////////////////////////////////////// |
| 267 |
// NsProtoSimAgent::UdpSocketAgent implementation |
| 268 |
// |
| 269 |
static class ProtoSocketUdpAgentClass : public TclClass { |
| 270 |
public: |
| 271 |
ProtoSocketUdpAgentClass() : TclClass("Agent/ProtoUdpSocket") {} |
| 272 |
TclObject* create(int, const char*const*) { |
| 273 |
return (new NsProtoSimAgent::UdpSocketAgent()); |
| 274 |
} |
| 275 |
} class_proto_socket_udp_agent; |
| 276 |
|
| 277 |
NsProtoSimAgent::UdpSocketAgent::UdpSocketAgent() |
| 278 |
: Agent(PT_UDP) |
| 279 |
{ |
| 280 |
} |
| 281 |
|
| 282 |
bool NsProtoSimAgent::UdpSocketAgent::SendTo(const char* buffer, |
| 283 |
unsigned int& buflen, |
| 284 |
const ProtoAddress& dstAddr) |
| 285 |
{ |
| 286 |
PLOG(PL_MAX, "NsProtoSimAgent::UdpSocketAgent::SendTo Entering \n"); |
| 287 |
PLOG(PL_DETAIL, "NsProtoSimAgent::UdpSocketAgent::SendTo called: %s length %u\n", buffer, buflen); |
| 288 |
|
| 289 |
// Allocate _and_ init packet |
| 290 |
Packet* p = allocpkt(buflen); |
| 291 |
if (p) |
| 292 |
{ |
| 293 |
// Set packet destination addr/port |
| 294 |
hdr_ip* iph = hdr_ip::access(p); |
| 295 |
if (ecn_capable) |
| 296 |
hdr_flags::access(p)->ect() = 1; // ecn enabled |
| 297 |
iph->daddr() = dstAddr.SimGetAddress(); |
| 298 |
iph->dport() = dstAddr.GetPort(); |
| 299 |
if (dstAddr.IsMulticast()) |
| 300 |
iph->ttl() = mcast_ttl; |
| 301 |
else |
| 302 |
iph->ttl() = 255; |
| 303 |
|
| 304 |
PLOG(PL_DETAIL, "NsProtoSimAgent::UdpSocketAgent::sending to %u port %u\n", iph->daddr(), iph->dport()); |
| 305 |
|
| 306 |
hdr_cmn* ch = hdr_cmn::access(p); |
| 307 |
// We could "fake" other packet types here if we liked |
| 308 |
ch->ptype() = PT_UDP; |
| 309 |
// Copy data to be sent into packet |
| 310 |
memcpy(p->accessdata(), buffer, buflen); |
| 311 |
// Set packet length field to UDP payload size for now |
| 312 |
// (TBD) Should we add UDP/IP header overhead? |
| 313 |
ch->size() = buflen; |
| 314 |
send(p, 0); |
| 315 |
return true; |
| 316 |
} |
| 317 |
else |
| 318 |
{ |
| 319 |
PLOG(PL_ERROR, "NsProtoSimAgent::UdpSocketAgent::SendTo() new Packet error"); |
| 320 |
return false; |
| 321 |
} |
| 322 |
PLOG(PL_MAX, "NsProtoSimAgent::UdpSocketAgent::SendTo Exiting \n"); |
| 323 |
} // end NsProtoSimAgent::UdpSocketAgent::SendTo() |
| 324 |
|
| 325 |
|
| 326 |
void NsProtoSimAgent::UdpSocketAgent::recv(Packet* pkt, Handler* /*h*/) |
| 327 |
{ |
| 328 |
PLOG(PL_MAX, "NsProtoSimAgent::UdpSocketAgent::recv() Entering\n"); |
| 329 |
|
| 330 |
if (pkt->userdata() && (PACKET_DATA == pkt->userdata()->type())) |
| 331 |
{ |
| 332 |
PacketData* pktData = (PacketData*)pkt->userdata(); |
| 333 |
char* recvData = (char*)pktData->data(); |
| 334 |
unsigned int recvLen = pktData->size(); |
| 335 |
ns_addr_t srcAddr = HDR_IP(pkt)->src(); |
| 336 |
ns_addr_t dstAddr = HDR_IP(pkt)->dst(); |
| 337 |
|
| 338 |
PLOG(PL_DETAIL, "Source address %u, my address %u, destination %u ...\n", srcAddr.addr_, addr(), dstAddr.addr_); |
| 339 |
/* Adamson - commented this out as we _do_ want to allow "locally" arriving data if |
| 340 |
mcast_loopback is enabled! |
| 341 |
if (addr() == srcAddr.addr_) { |
| 342 |
PLOG(PL_WARN, "Data arrived locally on node %u, ignoring ...\n", addr()); |
| 343 |
PLOG(PL_WARN, "Details: my port %u, senders port %u\n", port(), srcAddr.port_); |
| 344 |
return; |
| 345 |
} |
| 346 |
*/ |
| 347 |
|
| 348 |
if (0 != hdr_flags::access(pkt)->ect()) |
| 349 |
{ |
| 350 |
//TRACE("received ecn capable packet status = %d (%d)...\n", |
| 351 |
// hdr_flags::access(pkt)->ecnecho(), hdr_flags::access(pkt)->ce()); |
| 352 |
ecn_status = (0 != hdr_flags::access(pkt)->ce()); |
| 353 |
} |
| 354 |
else |
| 355 |
ecn_status = false; |
| 356 |
|
| 357 |
if (dstAddr.port_!=port()) { |
| 358 |
PLOG(PL_DEBUG, "My socket port is %u but destination port is %u, dropping packet ...\n", port(), dstAddr.port_); |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
bool isUnicast = (0 == (dstAddr.addr_ & 0x80000000)); |
| 363 |
if ((addr() != srcAddr.addr_) || |
| 364 |
(port() != srcAddr.port_) || |
| 365 |
isUnicast || mcast_loopback) |
| 366 |
{ |
| 367 |
recv_data = recvData; |
| 368 |
recv_data_len = recvLen; |
| 369 |
recv_data_offset=0; // reset the buffer |
| 370 |
PLOG(PL_DETAIL, "NsProtoSimAgent::UdpSocketAgent::recv recv_data_len = %i\n", recv_data_len); |
| 371 |
|
| 372 |
src_addr.SimSetAddress(srcAddr.addr_); |
| 373 |
src_addr.SetPort(srcAddr.port_); |
| 374 |
PLOG(PL_DETAIL, "NsProtoSimAgent::UdpSocketAgent::recv event being sent to socket ... \n"); |
| 375 |
if (proto_socket) |
| 376 |
proto_socket->OnNotify(ProtoSocket::NOTIFY_INPUT); |
| 377 |
} |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
PLOG(PL_WARN, "NsProtoSimAgent::UdpSocketAgent::recv() warning: " |
| 382 |
"received packet with no payload\n"); |
| 383 |
} |
| 384 |
Packet::free(pkt); |
| 385 |
|
| 386 |
PLOG(PL_MAX, "NsProtoSimAgent::UdpSocketAgent::recv() Exiting \n"); |
| 387 |
} // end NsProtoSimAgent::UdpSocketAgent::recv() |
| 388 |
|
| 389 |
|