69 #include "ns3/point-to-point-module.h"
70 #include "ns3/internet-module.h"
71 #include "ns3/onion-routing.h"
72 #include "ns3/applications-module.h"
75 #define ONION_NO_CONTENT 0
76 #define ONION_ENDCONTENT 1
77 #define ONION_LAYERCONTENT 2
78 #define ONION_LAYERCONTENT_ENDCONTENT 3
85 NS_LOG_COMPONENT_DEFINE (
"OnionRoutingDummyEncryptionExample");
89 uint8_t * IpToBuff (Ipv4Address in)
91 uint8_t * out =
new uint8_t[4];
92 in.Serialize (&out[0]);
97 Ipv4Address ConstructIpv4 (uint8_t * buf)
109 return Ipv4Address (ip);
115 uint8_t * StringToUchar (std::string in)
117 uint8_t * out =
new uint8_t[in.length ()];
118 memcpy (&out[0], &in[0], in.length ());
123 std::string UcharToString (uint8_t* seq,
int len)
125 std::string strForm (&seq[0], &seq[0] + len);
134 class MyApp :
public Application
138 MyApp (uint8_t onionMode,uint16_t layerContentLen);
141 static TypeId GetTypeId (
void);
144 uint8_t * GetEncryptionKey ();
146 Ipv4Address GetAddress ();
149 void SetRoute (uint16_t routeLen, uint8_t ** ipRoute, uint8_t ** keys, uint8_t ** layerContent, uint8_t layerContentLen);
152 virtual void StartApplication (
void);
153 virtual void StopApplication (
void);
156 void RecvOnion (Ptr<Socket> socket);
158 Ptr<Socket> m_socket;
161 Ipv4Address m_address;
165 uint8_t ** m_ipRoute;
167 uint8_t ** m_layerContent;
168 uint16_t m_layerContentLen;
176 m_onionManager (32,Ipv4L3Protocol::PROT_NUMBER),
182 MyApp::MyApp (uint8_t onionMode,uint16_t layerContentLen)
186 m_onionManager (32,Ipv4L3Protocol::PROT_NUMBER),
187 m_onionMode (onionMode),
189 m_layerContentLen (layerContentLen)
201 uint8_t * MyApp::GetEncryptionKey ()
203 return m_onionManager.GetEncryptionKey ();
206 Ipv4Address MyApp::GetAddress ()
217 m_onionManager.GenerateNewKey ();
221 Ptr<Node> PtrNode = this->GetNode ();
222 Ptr<Ipv4> ipv4 = PtrNode->GetObject<Ipv4> ();
223 Ipv4InterfaceAddress iaddr = ipv4->GetAddress (1, 0);
224 m_address = iaddr.GetLocal ();
230 void MyApp::SetRoute (uint16_t routeLen, uint8_t ** ipRoute, uint8_t ** keys, uint8_t ** layerContent, uint8_t layerContentLen)
232 m_routeLen = routeLen;
235 m_layerContent = layerContent;
236 m_layerContentLen = layerContentLen;
242 TypeId MyApp::GetTypeId (
void)
244 static TypeId tid = TypeId (
"MyApp")
245 .SetParent<Application> ()
246 .SetGroupName (
"OR-dummy-example")
247 .AddConstructor<MyApp> ()
259 std::string s (
"Some content to send anonymously.");
260 uint8_t * content = StringToUchar (s);
261 uint16_t contentLen = s.length ();
267 if (m_onionMode == ONION_NO_CONTENT)
269 cipherLen = m_onionManager.OnionLength (m_routeLen,0,0);
270 cipher =
new uint8_t[cipherLen];
272 m_onionManager.BuildOnion (cipher, m_ipRoute, m_keys, m_routeLen);
274 else if (m_onionMode == ONION_ENDCONTENT)
276 cipherLen = m_onionManager.OnionLength (m_routeLen,0,contentLen);
277 cipher =
new uint8_t[cipherLen];
279 m_onionManager.BuildOnion (cipher, m_ipRoute, m_keys, m_routeLen,content,contentLen);
281 else if (m_onionMode == ONION_LAYERCONTENT)
283 cipherLen = m_onionManager.OnionLength (m_routeLen,m_layerContentLen,0);
284 cipher =
new uint8_t[cipherLen];
286 m_onionManager.BuildOnion (cipher, m_ipRoute, m_keys, m_layerContent, m_layerContentLen, m_routeLen);
290 cipherLen = m_onionManager.OnionLength (m_routeLen,m_layerContentLen,contentLen);
291 cipher =
new uint8_t[cipherLen];
293 m_onionManager.BuildOnion (cipher, m_ipRoute, m_keys, m_layerContent, m_layerContentLen, m_routeLen,content,contentLen);
298 uint8_t
const * buff = cipher;
299 Ptr<Packet> p = Create<Packet> (buff,cipherLen);
300 m_socket->SendTo (p, 0, InetSocketAddress (ConstructIpv4 (m_ipRoute[0]),m_port));
301 NS_LOG_INFO (
"Onion construction--Onion sent to: " << ConstructIpv4 (m_ipRoute[0]) <<
" of size: " << p->GetSize () <<
" bytes" );
307 MyApp::RecvOnion (Ptr<Socket> socket)
310 Ptr<Packet> p = socket->RecvFrom (from);
313 uint32_t cipherLen = p->GetSize ();
317 NS_LOG_INFO (
"Onion reveal--Empty onion sent from: " << InetSocketAddress::ConvertFrom (from).GetIpv4 () <<
" received at: " << m_address );
321 uint8_t cipher[cipherLen];
322 p->CopyData (cipher, cipherLen);
325 orLayer * onionLayer = m_onionManager.PeelOnion (cipher,cipherLen,m_onionManager.GetEncryptionKey (),m_onionManager.GetEncryptionKey ());
328 if (ConstructIpv4 (onionLayer->
nextHopIP).Get () == 0)
330 NS_LOG_INFO (
"Onion reveal--Onion sent from: " << InetSocketAddress::ConvertFrom (from).GetIpv4 () <<
" received at: " << m_address <<
" of size: " << p->GetSize () <<
" bytes, containing the end content:" << UcharToString (onionLayer->
innerLayer,onionLayer->
innerLayerLen));
334 if (m_onionMode == ONION_LAYERCONTENT || m_onionMode == ONION_LAYERCONTENT_ENDCONTENT)
336 uint8_t
const * buff = &onionLayer->
innerLayer[m_layerContentLen];
337 Ptr<Packet> np = Create<Packet> (buff,onionLayer->
innerLayerLen - m_layerContentLen);
338 m_socket->SendTo (np, 0, InetSocketAddress (ConstructIpv4 (onionLayer->
nextHopIP),m_port));
339 NS_LOG_INFO (
"Onion routing--Onion sent from: " << InetSocketAddress::ConvertFrom (from).GetIpv4 () <<
" received at: " << m_address <<
" of size: " << p->GetSize () <<
" bytes, containing the layer content: " << UcharToString (onionLayer->
innerLayer,m_layerContentLen) <<
", sent to: " << ConstructIpv4 (onionLayer->
nextHopIP));
344 uint8_t
const * buff = onionLayer->
innerLayer;
345 Ptr<Packet> np = Create<Packet> (buff,onionLayer->
innerLayerLen);
346 m_socket->SendTo (np, 0, InetSocketAddress (ConstructIpv4 (onionLayer->
nextHopIP),m_port));
347 NS_LOG_INFO (
"Onion routing--Onion sent from: " << InetSocketAddress::ConvertFrom (from).GetIpv4 () <<
" received at: " << m_address <<
" of size: " << p->GetSize () <<
" bytes, sent to: " << ConstructIpv4 (onionLayer->
nextHopIP));
356 MyApp::StartApplication (
void)
360 m_socket = Socket::CreateSocket (this->GetNode (), TypeId::LookupByName (
"ns3::UdpSocketFactory"));
361 m_socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), m_port));
362 m_socket->SetRecvCallback (MakeCallback (&MyApp::RecvOnion,
this));
368 Simulator::Schedule (Seconds (2), &MyApp::SendOnion,
this);
374 MyApp::StopApplication (
void)
389 main (
int argc,
char *argv[])
392 uint8_t onionMode = ONION_ENDCONTENT;
394 CommandLine cmd (__FILE__);
395 cmd.AddValue (
"verbose",
"Tell application to log if true", verbose);
396 cmd.AddValue (
"onionMode",
"Select the mode of operation", onionMode);
398 cmd.Parse (argc,argv);
402 LogComponentEnable (
"OnionRoutingDummyEncryptionExample", LOG_LEVEL_INFO);
403 LogComponentEnable (
"onionrouting", LOG_LEVEL_INFO);
409 NS_FATAL_ERROR (
"Wrong mode of operation selected, select one in range 0 to 3");
419 NodeContainer n0n1 = NodeContainer(nc.Get(0),nc.Get(1));
420 NodeContainer n1n2 = NodeContainer(nc.Get(1),nc.Get(2));
421 NodeContainer n2n3 = NodeContainer(nc.Get(2),nc.Get(3));
422 NodeContainer n2n4 = NodeContainer(nc.Get(2),nc.Get(4));
423 NodeContainer n3n4 = NodeContainer(nc.Get(3),nc.Get(4));
424 NodeContainer n4n5 = NodeContainer(nc.Get(4),nc.Get(5));
428 InternetStackHelper stack;
432 NS_LOG_INFO (
"Create channels.");
433 PointToPointHelper p2p;
434 p2p.SetDeviceAttribute (
"DataRate",StringValue (
"5Mbps"));
435 p2p.SetChannelAttribute(
"Delay",StringValue(
"2ms"));
436 NetDeviceContainer d0d1 = p2p.Install (n0n1);
438 NetDeviceContainer d1d2 = p2p.Install (n1n2);
440 NetDeviceContainer d4d5 = p2p.Install (n4n5);
442 p2p.SetDeviceAttribute (
"DataRate",StringValue (
"1Mbps"));
443 p2p.SetChannelAttribute(
"Delay",StringValue(
"3ms"));
444 NetDeviceContainer d2d3 = p2p.Install (n2n3);
445 NetDeviceContainer d3d4 = p2p.Install (n3n4);
446 NetDeviceContainer d2d4 = p2p.Install (n2n4);
451 Ipv4AddressHelper address;
453 address.SetBase (
"10.1.1.0",
"255.255.255.0");
454 Ipv4InterfaceContainer i0i1 = address.Assign (d0d1);
456 address.SetBase (
"10.1.2.0",
"255.255.255.0");
457 Ipv4InterfaceContainer i1i2 = address.Assign (d1d2);
459 address.SetBase (
"10.1.3.0",
"255.255.255.0");
460 Ipv4InterfaceContainer i2i3 = address.Assign (d2d3);
462 address.SetBase (
"10.1.4.0",
"255.255.255.0");
463 Ipv4InterfaceContainer i3i4 = address.Assign (d3d4);
465 address.SetBase (
"10.1.5.0",
"255.255.255.0");
466 Ipv4InterfaceContainer i2i4 = address.Assign (d2d4);
468 address.SetBase (
"10.1.6.0",
"255.255.255.0");
469 Ipv4InterfaceContainer i4i5 = address.Assign (d4d5);
472 Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
476 uint16_t routeLen = 5;
477 uint8_t * ipRoute[routeLen];
478 uint8_t * keys[routeLen];
479 uint16_t layerContentLen = 27;
480 uint8_t * layerContent[routeLen];
484 ApplicationContainer applications (CreateObject<MyApp> (onionMode,layerContentLen));
485 applications.Add (CreateObject<MyApp> (onionMode,layerContentLen));
486 applications.Add (CreateObject<MyApp> (onionMode,layerContentLen));
487 applications.Add (CreateObject<MyApp> (onionMode,layerContentLen));
488 applications.Add (CreateObject<MyApp> (onionMode,layerContentLen));
489 applications.Add (CreateObject<MyApp> (onionMode,layerContentLen));
491 nc.Get (0)->AddApplication (applications.Get (0));
492 nc.Get (1)->AddApplication (applications.Get (1));
493 nc.Get (2)->AddApplication (applications.Get (2));
494 nc.Get (3)->AddApplication (applications.Get (3));
495 nc.Get (4)->AddApplication (applications.Get (4));
496 nc.Get (5)->AddApplication (applications.Get (5));
500 for (uint32_t i = 0; i < applications.GetN (); ++i)
503 applications.Get (i)->GetObject<MyApp> ()->Setup ();
508 ipRoute[0] = IpToBuff (applications.Get (2)->GetObject<MyApp> ()->GetAddress ());
509 ipRoute[1] = IpToBuff (applications.Get (3)->GetObject<MyApp> ()->GetAddress ());
510 ipRoute[2] = IpToBuff (applications.Get (1)->GetObject<MyApp> ()->GetAddress ());
511 ipRoute[3] = IpToBuff (applications.Get (4)->GetObject<MyApp> ()->GetAddress ());
512 ipRoute[4] = IpToBuff (applications.Get (5)->GetObject<MyApp> ()->GetAddress ());
515 keys[0] = applications.Get (2)->GetObject<MyApp> ()->GetEncryptionKey ();
516 keys[1] = applications.Get (3)->GetObject<MyApp> ()->GetEncryptionKey ();
517 keys[2] = applications.Get (1)->GetObject<MyApp> ()->GetEncryptionKey ();
518 keys[3] = applications.Get (4)->GetObject<MyApp> ()->GetEncryptionKey ();
519 keys[4] = applications.Get (5)->GetObject<MyApp> ()->GetEncryptionKey ()
521 layerContent[0] = StringToUchar (
"OnionLayer 4 secret content");
522 layerContent[1] = StringToUchar (
"OnionLayer 3 secret content");
523 layerContent[2] = StringToUchar (
"OnionLayer 2 secret content");
524 layerContent[3] = StringToUchar (
"OnionLayer 1 secret content");
525 layerContent[4] = StringToUchar (
"OnionLayer 0 secret content");
528 applications.Get (0)->GetObject<MyApp> ()->SetRoute (routeLen,ipRoute,keys,layerContent,layerContentLen);
531 applications.Start (Seconds (1));
532 applications.Stop (Seconds (20));
535 Simulator::Stop (Seconds (20));
537 Simulator::Destroy ();