Example #1
0
        protected void FriendPing(string address)
        {
            Address addr = AddressParser.Parse(address);
            Channel q    = new Channel();

            q.CloseAfterEnqueue();
            q.CloseEvent += delegate(object obj, EventArgs eargs) {
                try {
                    RpcResult res      = (RpcResult)q.Dequeue();
                    string    result   = (string)res.Result;
                    string[]  parts    = result.Split(DELIM);
                    string    dht_key  = parts[0];
                    string    response = parts[1];
                    if (response == "online")
                    {
                        SocialUser friend = _friends[dht_key];
                        friend.Time = DateTime.Now.ToString();
                    }
                    ProtocolLog.Write(SocialLog.SVPNLog, "PING FRIEND REPLY: " +
                                      result);
                } catch (Exception e) {
                    ProtocolLog.Write(SocialLog.SVPNLog, e.Message);
                    ProtocolLog.Write(SocialLog.SVPNLog, "PING FRIEND FAILURE: " +
                                      address);
                }
            };
            ISender sender = new AHExactSender(_node, addr);

            _rpc.Invoke(sender, q, "SocialVPN.FriendPing", _local_user.DhtKey);
        }
 /// <summary>This methods send some ICopyable data to the remote address.
 /// </summary>
 /// <param name="remote_addr">Remote Nodes are referenced by their P2P
 /// Address, typically of type AHAddress.</param>
 /// <param name="data">This is an ICopyable object which contains the data
 /// to send.</param>
 public void SendMessage(Address remote_addr, ICopyable data) {
   // This instantiates a multi-use method to sending to the remote node,
   // though we will only use it once.  It is VERY similar to UDP.
   AHExactSender sender = new AHExactSender(_app_node.Node, remote_addr);
   // This is the process of actually sending the data.
   sender.Send(new CopyList(HW, data));
 }
        // Provides an Exact AH Secure Sender using the default SPI  given an address
        public SecurityAssociation GetSecureSender(Address target)
        {
            PeerSecAssociation sa = null;
            bool new_sa           = false;

            lock (_sync) {
                if (_address_to_sa.ContainsKey(target))
                {
                    sa = _address_to_sa[target] as PeerSecAssociation;
                }
                else
                {
                    AHSender sender = new AHExactSender(_node, target);
                    sa = base.CreateSecurityAssociation(sender, SecurityPolicy.DefaultSPI);
                    _address_to_sa[target] = sa;
                    _sa_to_address[sa]     = target;
                    new_sa = true;
                }
            }

            if (new_sa)
            {
                StartSA(sa);
            }

            return(sa);
        }
Example #4
0
        protected void CreateSecurityAssociation()
        {
            var sender = new AHExactSender(Node0.Node, Node1.Node.Address);
            var sa     = Node0.Sso.CreateSecurityAssociation(sender);

            SecurityAssociation.StateChangeHandler callback =
                delegate(SecurityAssociation in_sa, SecurityAssociation.States state)
            {
                if (state == SecurityAssociation.States.Active)
                {
                    SecurityAssociationEstablished();
                }
                else if (state == SecurityAssociation.States.Closed)
                {
                    Finished();
                }
                else
                {
                    Console.WriteLine(state);
                }
            };

            if (sa.State != SecurityAssociation.States.Waiting)
            {
                callback(sa, sa.State);
            }
            else
            {
                sa.StateChangeEvent += callback;
            }
        }
Example #5
0
        virtual protected void ConnectTo(Address target, string ConnectionType,
                                         string token)
        {
            ISender sender = new AHExactSender(_node, target);

            ConnectTo(sender, target, ConnectionType, _node.Address.ToString());
        }
Example #6
0
        protected void MissedMapping(string ip, Address addr)
        {
            ProtocolLog.WriteIf(IpopLog.ResolverLog, String.Format(
                                    "Notifying remote node of missing address: {0} : {1}", ip, addr));
            ISender sender = new AHExactSender(AppNode.Node, addr);

            AppNode.Node.Rpc.Invoke(sender, null, "Ipop.NoSuchMapping", ip);
        }
Example #7
0
        override public void Start()
        {
            base.Start();
            var     sender = new AHExactSender(Node0, Node1.Address);
            Channel q      = new Channel(1);

            q.CloseEvent += Callback;
            Node0.Rpc.Invoke(sender, q, "sys:link.Ping", 0);
        }
        /// <summary>This methods send some ICopyable data to the remote address.
        /// </summary>
        /// <param name="remote_addr">Remote Nodes are referenced by their P2P
        /// Address, typically of type AHAddress.</param>
        /// <param name="data">This is an ICopyable object which contains the data
        /// to send.</param>
        public void SendMessage(Address remote_addr, ICopyable data)
        {
            // This instantiates a multi-use method to sending to the remote node,
            // though we will only use it once.  It is VERY similar to UDP.
            AHExactSender sender = new AHExactSender(_app_node.Node, remote_addr);

            // This is the process of actually sending the data.
            sender.Send(new CopyList(HW, data));
        }
Example #9
0
        /// <summary>First we try to find a third party we can connect with for
        /// overlap, if that is successful, we attempt to connect to him, if that
        /// is successful, we create a new tunnel edge.</summary>
        protected void AttemptToCreateOverlap(RelayEdgeCallbackAction teca)
        {
            WaitCallback create_connection = delegate(object o) {
                Address target = o as Address;
                if (o == null)
                {
                    FailedEdgeCreate(teca);
                    return;
                }

                ConnectionList cons  = _connections;
                int            index = cons.IndexOf(target);
                if (index < 0)
                {
                    FailedEdgeCreate(teca);
                    return;
                }

                List <Connection> overlap = new List <Connection>(1);
                overlap.Add(cons[index]);
                CreateEdge(teca, overlap);
            };

            Channel chan = new Channel(1);

            chan.CloseEvent += delegate(object o, EventArgs ea) {
                Address target = null;
                try {
                    IDictionary msg = (chan.Dequeue() as RpcResult).Result as IDictionary;
                    target = _ito.EvaluatePotentialOverlap(msg);
                } catch {
                }

                if (target == null)
                {
                    FailedEdgeCreate(teca);
                }
                else
                {
                    _oco.ConnectTo(target, create_connection);
                }
            };

            ISender s = new AHExactSender(_node, teca.RelayTA.Target);

            try { _node.Rpc.Invoke(s, chan, "tunnel.RequestSync"); }
            catch { chan.Close(); }
        }
        protected void SendRpcMessage(Address addr, string method,
                                      string query, bool secure)
        {
            DateTime sent = DateTime.Now;

#if !SVPN_NUNIT
            string  meth_call = RPCID + "." + method;
            Channel q         = new Channel();
            q.CloseAfterEnqueue();
            q.CloseEvent += delegate(object obj, EventArgs eargs) {
                try {
                    RpcResult res    = (RpcResult)q.Dequeue();
                    string    result = (string)res.Result;

                    if (method == "Ping")
                    {
                        DateTime recv = DateTime.Now;
                        _times = _times.InsertIntoNew(result, recv);
                        TimeSpan rtt = recv - sent;
                        _ssm.UpdateLatency(result, rtt.TotalMilliseconds);
                    }

                    ProtocolLog.WriteIf(SocialLog.SVPNLog,
                                        String.Format("RPC {0} {1} {2}", addr.ToString(), method,
                                                      query));
                } catch (Exception e) {
                    ProtocolLog.WriteIf(SocialLog.SVPNLog, e.ToString());
                }
            };

            ISender sender;
            if (!secure)
            {
                sender = new AHExactSender(_node.Node, addr);
            }
            else
            {
                sender = _node.Bso.GetSecureSender(addr);
            }
            _rpc.Invoke(sender, q, meth_call, _node.Address, query);
#endif
        }
Example #11
0
        /// <summary>Sends the IP Packet to the specified target address.</summary>
        /// <param name="target"> the Brunet Address of the target</param>
        /// <param name="packet"> the data to send to the recepient</param>
        protected virtual void SendIP(Address target, MemBlock packet)
        {
            ISender s = null;

            if (_secure_senders)
            {
                try {
                    s = AppNode.SymphonySecurityOverlord.GetSecureSender(target);
                }
                catch (Exception e) {
                    Console.WriteLine(e);
                    return;
                }
            }
            else
            {
                s = new AHExactSender(AppNode.Node, target);
            }
            s.Send(new CopyList(PType.Protocol.IP, packet));
        }
    // Provides an Exact AH Secure Sender using the default SPI  given an address
    public SecurityAssociation GetSecureSender(Address target)
    {
      SecurityAssociation sa = null;
      bool new_sa = false;
      lock(_sync) {
        if(_address_to_sa.ContainsKey(target)) {
          sa = _address_to_sa[target];
        } else {
          AHSender sender = new AHExactSender(_node, target);
          sa = base.CreateSecurityAssociation(sender, SecurityPolicy.DefaultSPI);
          _address_to_sa[target] = sa;
          _sa_to_address[sa] = target;
          new_sa = true;
        }
      }

      if(new_sa) {
        StartSA(sa);
      }

      return sa;
    }
    protected void SendRpcMessage(Address addr, string method, 
      string query, bool secure) {

      System.DateTime sent = System.DateTime.Now;

#if !SVPN_NUNIT
      string meth_call = RPCID + "." + method;
      Channel q = new Channel();
      q.CloseAfterEnqueue();
      q.CloseEvent += delegate(object obj, EventArgs eargs) {
        try {
          RpcResult res = (RpcResult) q.Dequeue();
          string result = (string) res.Result;

          if(method == "Ping") {
              System.DateTime recv = System.DateTime.Now;
            _times = _times.InsertIntoNew(result, recv);
            TimeSpan rtt = recv - sent;
            _ssm.UpdateLatency(result, rtt.TotalMilliseconds);
          }

          ProtocolLog.WriteIf(SocialLog.SVPNLog,
            String.Format("RPC {0} {1} {2}", addr.ToString(), method, 
            query));

        } catch(Exception e) {
          ProtocolLog.WriteIf(SocialLog.SVPNLog, e.ToString());
        }
      };

      ISender sender;
      if(!secure) {
        sender = new AHExactSender(_node.Node, addr);
      }
      else {
        sender = _node.Bso.GetSecureSender(addr);
      }
      _rpc.Invoke(sender, q, meth_call, _node.Address, query);
#endif
    }
Example #14
0
    /// <summary>This method handles IPPackets that come from Brunet, i.e.,
    /// abroad.  </summary>
    /// <param name="packet"> The packet from Brunet.</param>
    /// <param name="ret">An ISender to send data to the Brunet node that sent
    /// the packet.</param>
    public virtual void HandleIPIn(MemBlock packet, ISender ret) {
      Address addr = _conn_handler.GetAddress(ret);

      if(_translator != null) {
        try {
          packet = _translator.Translate(packet, addr);
        }
        catch (Exception e) {
          if(ProtocolLog.Exceptions.Enabled) {
            ProtocolLog.Write(ProtocolLog.Exceptions, e.ToString());
          }
          return;
        }
      }

      IPPacket ipp = new IPPacket(packet);

      if(!_address_resolver.Check(ipp.SourceIP, addr)) {
        Address other = _address_resolver.Resolve(ipp.SourceIP);
        if(other == null) {
          ProtocolLog.WriteIf(IpopLog.ResolverLog, String.Format(
              "Notifying remote node of missing address: {0} : {1}",
              addr, ipp.SSourceIP));
          ISender sender = new AHExactSender(AppNode.Node, addr);
          AppNode.Node.Rpc.Invoke(sender, null, "Ipop.NoSuchMapping", ipp.SSourceIP);
          return;
        } else if(other != addr) {
          ProtocolLog.WriteIf(IpopLog.ResolverLog, String.Format(
              "IP:P2P Mismatch IP: {0}, Claimed P2P: {1}, Local P2P: {2}",
              ipp.SourceIP, addr, other));
          return;
        }
      }

      if(IpopLog.PacketLog.Enabled) {
        ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                          "Incoming packet:: IP src: {0}, IP dst: {1}, p2p " +
                              "from: {2}, size: {3}", ipp.SSourceIP, ipp.SDestinationIP,
                              ret, packet.Length));
      }

      WriteIP(packet);
    }
Example #15
0
    public static void Main(string []args) {
      if (args.Length < 1) {
	Console.WriteLine("please specify the number edge protocol."); 
        Environment.Exit(0);
      }
      if (args.Length < 2) {
        Console.WriteLine("please specify the number of p2p nodes."); 
        Environment.Exit(0);
      }
      if (args.Length < 3) {
        Console.WriteLine("please specify the number of missing edges."); 
        Environment.Exit(0);
      }
      string proto = "function";
      try {
	proto = args[0].Trim();
      } catch(Exception) {}

      bool tunnel = false;
      int base_port = 54000;
      int network_size = Int32.Parse(args[1]);
      int missing_count = Int32.Parse(args[2]);
      try {
	tunnel = args[3].Trim().Equals("tunnel");
      } catch (Exception) {}

      Console.WriteLine("use tunnel edges: {0}", tunnel);

      Random rand = new Random();

      ArrayList missing_edges = new ArrayList();
      for (int i = 0; i < missing_count; i++) {
	int idx = -1;
	int left, right;
	do {
	  idx = rand.Next(0, network_size);
	  left = (idx + 1)%network_size;
	  if (idx == 0) {
	    right = network_size - 1;
	  } else {
	    right = idx - 1;
	  }
	} while (missing_edges.Contains(idx));// ||
	//missing_edges.Contains(left) ||
	//missing_edges.Contains(right));
	
	Console.WriteLine("Will drop a left edge on idx {0}: ", idx);
	missing_edges.Add(idx);
      }
      
      //
      // Sort missing edges.
      //
      missing_edges.Sort();
      SortedList dist = new SortedList();
      //
      // Compute the average distance between missing edges. 
      //
      if (missing_count > 1) {
	for (int i = 0; i < missing_count; i++) {
	  int idx = (int) missing_edges[i];
	  int idx_next;
	  int d;
	  if (i == missing_count - 1) {
	    idx_next = (int) missing_edges[0];
	    d = (network_size - 1) - idx + idx_next;
	  } else {
	    idx_next = (int) missing_edges[i+1];
	    d = idx_next - idx - 1;
	  }
	  if (!dist.Contains(d)) {
	    dist[d] = 0;
	  } else {
	    int c = (int) dist[d];
	    dist[d] = c + 1;
	  }
	}
      }
      double sum = 0.0;
      int num = 0;
      Console.WriteLine("distribution of missing edges separation");
      foreach(DictionaryEntry de in dist) {
	int k = (int) de.Key;
	int c = (int) de.Value;
	Console.WriteLine("{0} {1}", k, c);
	sum = sum + k*c;
	num = num + c;
      }

      Console.WriteLine("average separation: {0}", (double) sum/num);
      string brunet_namespace = "testing";
      Console.WriteLine("Initializing...");

      ArrayList RemoteTA = new ArrayList();
      for(int i = 0; i < network_size; i++) {
	if (proto.Equals("udp")) {
	  RemoteTA.Add(TransportAddressFactory.CreateInstance("brunet.udp://localhost:" + (base_port + i)));
	} else if (proto.Equals("function")) { 
	  RemoteTA.Add(TransportAddressFactory.CreateInstance("brunet.function://localhost:" + (base_port + i)));
	}
      }

      for(int i = 0; i < network_size; i++) {
        AHAddress address = new AHAddress(new RNGCryptoServiceProvider());
        Node node = new StructuredNode(address, brunet_namespace);
        _sorted_node_list.Add((Address) address, node);
	_node_list.Add(node);
	RouteTestHandler test_handler = new RouteTestHandler();
	node.GetTypeSource(new PType(routing_test)).Subscribe(test_handler, address.ToMemBlock());
	RpcManager rpc_man = node.Rpc;
	rpc_man.AddHandler("rpc_routing_test", new  RpcRoutingTestHandler(node));
      }

      for (int i = 0; i < network_size; i++) {
	Node node = (Node) _sorted_node_list.GetByIndex(i);
	Console.WriteLine("Configuring node: {0} ", node.Address);
	TAAuthorizer ta_auth = null;
	if (missing_edges.Contains(i)) {
	  int remote_port;
	  if (i == network_size - 1) {
	    remote_port = base_port;
	  } else {
	    remote_port = base_port + i + 1;
	  }

	  PortTAAuthorizer port_auth = new PortTAAuthorizer(remote_port);
	  Console.WriteLine("Adding a port TA authorizer at: {0} for remote port: {1}", base_port + i, remote_port);
	  ArrayList arr_tas = new ArrayList();
	  arr_tas.Add(port_auth);
	  arr_tas.Add(new ConstantAuthorizer(TAAuthorizer.Decision.Allow));
	  ta_auth = new SeriesTAAuthorizer(arr_tas);
	}
	
	if (proto.Equals("udp")) { 
	  node.AddEdgeListener(new UdpEdgeListener(base_port + i, null, ta_auth));
	} else if(proto.Equals("function")) {
	  node.AddEdgeListener(new FunctionEdgeListener(base_port + i, -1.00, ta_auth));
	}
	
	if (tunnel) {
	  Console.WriteLine("Adding a tunnel edge listener");
	  node.AddEdgeListener(new Tunnel.TunnelEdgeListener(node));
	}
	_node_to_port[node] = base_port + i;
        node.RemoteTAs = RemoteTA;	
      }

      //start nodes one by one.
      for (int i  = 0; i < network_size; i++) {
	Node node = (Node) _node_list[i];
	Console.WriteLine("Starting node: {0}, {1}", i, node.Address);
        node.Connect();
	Console.WriteLine("Going to sleep for 2 seconds.");
        System.Threading.Thread.Sleep(2000);
      }

      //wait for 300000 more seconds
      Console.WriteLine("Going to sleep for 300000 seconds.");
      System.Threading.Thread.Sleep(300000);
      bool complete = CheckStatus();

      int count = 0;
      //
      // Send a large number of packets as exact packets to random destinations
      // and make sure exact routing is perfect.
      //
      
      for (int i = 0; i < network_size; i++) {
	for (int j = 0; j < network_size; j++) {
	  
	  int src_idx = i;
	  int dest_idx = j;
	  Node src_node = (Node) _sorted_node_list.GetByIndex(src_idx);
	  Node dest_node = (Node) _sorted_node_list.GetByIndex(dest_idx);
	  //Console.WriteLine("{0} -> {1}", src_idx, dest_idx);
	  Address dest_address = (Address) dest_node.Address;
	  ISender s = new AHExactSender(src_node, dest_address);
	  MemBlock p = dest_address.ToMemBlock();
	  s.Send(new CopyList(new PType(routing_test), p));
	  _sent++;
	  //System.Threading.Thread.Sleep(10);
	  s.Send(new CopyList(new PType(routing_test), p));
	  _sent++;
	  //System.Threading.Thread.Sleep(10);
	}
      }
      //wait for 10 more seconds
      Console.WriteLine("Going to sleep for 10 seconds.");
      System.Threading.Thread.Sleep(10000);      
      Console.WriteLine("Final statistics");
      lock(_class_lock) {
	Console.WriteLine("Sent: {0}, Received: {1}, Wrongly routed: {2}", 
			  _sent, _received, _wrongly_routed);
      }

      int missing_rpcs = 0;
      int correct_rpcs = 0;
      int incorrect_rpcs = 0;
      Hashtable queue_to_address = new Hashtable();
      for (int i = 0; i < network_size; i++) {
	for (int j = 0; j < network_size; j++) {
	  
	  int src_idx = i;
	  int dest_idx = j;
	  Node src_node = (Node) _sorted_node_list.GetByIndex(src_idx);
	  Node dest_node = (Node) _sorted_node_list.GetByIndex(dest_idx);
	  //Console.WriteLine("{0} -> {1}", src_idx, dest_idx);
	  Address dest_address = (Address) dest_node.Address;
	  ISender s = new AHExactSender(src_node, dest_address);
	  RpcManager rpc_man = src_node.Rpc;
	  Channel q = new Channel();
	  lock (_class_lock) {
	    queue_to_address[q] = dest_address;
	  }
	  q.CloseAfterEnqueue();
	  q.CloseEvent += delegate(object o, EventArgs cargs) {
	    lock(_class_lock) {
	      Channel qu = (Channel) o;
	      if (qu.Count == 0) {
		missing_rpcs++;
	      }
	      queue_to_address.Remove(qu);
	    }
	  };
	  q.EnqueueEvent += delegate(object o, EventArgs cargs) {
	    lock(_class_lock) {
	      Channel qu = (Channel) o;
	      RpcResult rpc_reply = (RpcResult) qu.Peek();
	      byte []result = (byte[]) rpc_reply.Result;
	      Address target = new AHAddress(result);
	      if (target.Equals(queue_to_address[qu])) {
		correct_rpcs++;
	      } else {
		incorrect_rpcs++;
	      }
	    }
	  };
	  rpc_man.Invoke(s, q, "rpc_routing_test.GetIdentification", new object[]{});
	}
      }
      
      //wait for 10 more seconds
      while (true) {
	int c = -1;
	lock(_class_lock) {
	  c = incorrect_rpcs + missing_rpcs + correct_rpcs;
	}
	if (c < network_size*network_size) {
	  Console.WriteLine("Going to sleep for 10 seconds.");
	  System.Threading.Thread.Sleep(10000);
	} else {
	  break;
	}
      }
      
      Console.WriteLine("Final statistics");
      Console.WriteLine("correct rpcs: {0}, incorrect rpcs: {1}, missing rpcs: {2}", 
			correct_rpcs, incorrect_rpcs, missing_rpcs);
      
      System.Environment.Exit(1);
    }
Example #16
0
    /// <summary>First we try to find a third party we can connect with for
    /// overlap, if that is successful, we attempt to connect to him, if that
    /// is successful, we create a new tunnel edge.</summary>
    protected void AttemptToCreateOverlap(TunnelEdgeCallbackAction teca)
    {
      WaitCallback create_connection = delegate(object o) {
        Address target = o as Address;
        if(o == null) {
          FailedEdgeCreate(teca);
          return;
        }

        ConnectionList cons = _connections;
        int index = cons.IndexOf(target);
        if(index < 0) {
          FailedEdgeCreate(teca);
          return;
        }

        List<Connection> overlap = new List<Connection>(1);
        overlap.Add(cons[index]);
        CreateEdge(teca, overlap);
      };

      Channel chan = new Channel(1);
      chan.CloseEvent += delegate(object o, EventArgs ea) {
        Address target = null;
        try {
          IDictionary msg = (chan.Dequeue() as RpcResult).Result as IDictionary;
          target = _ito.EvaluatePotentialOverlap(msg);
        } catch {
        }

        if(target == null) {
          FailedEdgeCreate(teca);
        } else {
          _oco.ConnectTo(target, create_connection);
        }
      };

      ISender s = new AHExactSender(_node, teca.TunnelTA.Target);
      _node.Rpc.Invoke(s, chan, "tunnel.RequestSync");
    }
Example #17
0
        /// <summary>This method handles IPPackets that come from Brunet, i.e.,
        /// abroad.  </summary>
        /// <param name="packet"> The packet from Brunet.</param>
        /// <param name="ret">An ISender to send data to the Brunet node that sent
        /// the packet.</param>
        public virtual void HandleIPIn(MemBlock packet, ISender ret)
        {
            if (_secure_senders && !(ret is SecurityAssociation))
            {
                return;
            }

            Address addr = null;

            if (ret is SecurityAssociation)
            {
                ret = ((SecurityAssociation)ret).Sender;
            }

            if (ret is AHSender)
            {
                addr = ((AHSender)ret).Destination;
            }
            else
            {
                ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                                      "Incoming packet was not from an AHSender: {0}.", ret));
                return;
            }

            if (_translator != null)
            {
                try {
                    packet = _translator.Translate(packet, addr);
                }
                catch (Exception e) {
                    if (ProtocolLog.Exceptions.Enabled)
                    {
                        ProtocolLog.Write(ProtocolLog.Exceptions, e.ToString());
                    }
                    return;
                }
            }

            IPPacket ipp = new IPPacket(packet);

            try {
                if (!_address_resolver.Check(ipp.SourceIP, addr))
                {
                    return;
                }
            } catch (AddressResolutionException ex) {
                if (ex.Issue == AddressResolutionException.Issues.DoesNotExist)
                {
                    ProtocolLog.WriteIf(IpopLog.ResolverLog, "Notifying remote node of " +
                                        " missing address: " + addr + ":" + ipp.SSourceIP);
                    ISender sender = new AHExactSender(AppNode.Node, addr);
                    AppNode.Node.Rpc.Invoke(sender, null, "Ipop.NoSuchMapping", ipp.SSourceIP);
                    return;
                }
                else
                {
                    throw;
                }
            }

            if (IpopLog.PacketLog.Enabled)
            {
                ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                                      "Incoming packet:: IP src: {0}, IP dst: {1}, p2p " +
                                      "from: {2}, size: {3}", ipp.SSourceIP, ipp.SDestinationIP,
                                      ret, packet.Length));
            }

            WriteIP(packet);
        }
Example #18
0
        public static void Main(string [] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("please specify the number edge protocol.");
                Environment.Exit(0);
            }
            if (args.Length < 2)
            {
                Console.WriteLine("please specify the number of p2p nodes.");
                Environment.Exit(0);
            }
            if (args.Length < 3)
            {
                Console.WriteLine("please specify the number of missing edges.");
                Environment.Exit(0);
            }
            string proto = "function";

            try {
                proto = args[0].Trim();
            } catch (Exception) {}

            bool tunnel        = false;
            int  base_port     = 54000;
            int  network_size  = Int32.Parse(args[1]);
            int  missing_count = Int32.Parse(args[2]);

            try {
                tunnel = args[3].Trim().Equals("tunnel");
            } catch (Exception) {}

            Console.WriteLine("use tunnel edges: {0}", tunnel);

            Random rand = new Random();

            ArrayList missing_edges = new ArrayList();

            for (int i = 0; i < missing_count; i++)
            {
                int idx = -1;
                int left, right;
                do
                {
                    idx  = rand.Next(0, network_size);
                    left = (idx + 1) % network_size;
                    if (idx == 0)
                    {
                        right = network_size - 1;
                    }
                    else
                    {
                        right = idx - 1;
                    }
                } while (missing_edges.Contains(idx));// ||
                //missing_edges.Contains(left) ||
                //missing_edges.Contains(right));

                Console.WriteLine("Will drop a left edge on idx {0}: ", idx);
                missing_edges.Add(idx);
            }

            //
            // Sort missing edges.
            //
            missing_edges.Sort();
            SortedList dist = new SortedList();

            //
            // Compute the average distance between missing edges.
            //
            if (missing_count > 1)
            {
                for (int i = 0; i < missing_count; i++)
                {
                    int idx = (int)missing_edges[i];
                    int idx_next;
                    int d;
                    if (i == missing_count - 1)
                    {
                        idx_next = (int)missing_edges[0];
                        d        = (network_size - 1) - idx + idx_next;
                    }
                    else
                    {
                        idx_next = (int)missing_edges[i + 1];
                        d        = idx_next - idx - 1;
                    }
                    if (!dist.Contains(d))
                    {
                        dist[d] = 0;
                    }
                    else
                    {
                        int c = (int)dist[d];
                        dist[d] = c + 1;
                    }
                }
            }
            double sum = 0.0;
            int    num = 0;

            Console.WriteLine("distribution of missing edges separation");
            foreach (DictionaryEntry de in dist)
            {
                int k = (int)de.Key;
                int c = (int)de.Value;
                Console.WriteLine("{0} {1}", k, c);
                sum = sum + k * c;
                num = num + c;
            }

            Console.WriteLine("average separation: {0}", (double)sum / num);
            string brunet_namespace = "testing";

            Console.WriteLine("Initializing...");

            var RemoteTA = new List <TransportAddress>();

            for (int i = 0; i < network_size; i++)
            {
                if (proto.Equals("udp"))
                {
                    RemoteTA.Add(TransportAddressFactory.CreateInstance("brunet.udp://localhost:" + (base_port + i)));
                }
                else if (proto.Equals("function"))
                {
                    RemoteTA.Add(TransportAddressFactory.CreateInstance("brunet.function://localhost:" + (base_port + i)));
                }
            }

            for (int i = 0; i < network_size; i++)
            {
                AHAddress address = new AHAddress(new RNGCryptoServiceProvider());
                Node      node    = new StructuredNode(address, brunet_namespace);
                _sorted_node_list.Add((Address)address, node);
                _node_list.Add(node);
                RouteTestHandler test_handler = new RouteTestHandler();
                node.GetTypeSource(new PType(routing_test)).Subscribe(test_handler, address.ToMemBlock());
                RpcManager rpc_man = node.Rpc;
                rpc_man.AddHandler("rpc_routing_test", new  RpcRoutingTestHandler(node));
            }

            for (int i = 0; i < network_size; i++)
            {
                Node node = (Node)_sorted_node_list.GetByIndex(i);
                Console.WriteLine("Configuring node: {0} ", node.Address);
                TAAuthorizer ta_auth = null;
                if (missing_edges.Contains(i))
                {
                    int remote_port;
                    if (i == network_size - 1)
                    {
                        remote_port = base_port;
                    }
                    else
                    {
                        remote_port = base_port + i + 1;
                    }

                    PortTAAuthorizer port_auth = new PortTAAuthorizer(remote_port);
                    Console.WriteLine("Adding a port TA authorizer at: {0} for remote port: {1}", base_port + i, remote_port);
                    ArrayList arr_tas = new ArrayList();
                    arr_tas.Add(port_auth);
                    arr_tas.Add(new ConstantAuthorizer(TAAuthorizer.Decision.Allow));
                    ta_auth = new SeriesTAAuthorizer(arr_tas);
                }

                if (proto.Equals("udp"))
                {
                    node.AddEdgeListener(new UdpEdgeListener(base_port + i, null, ta_auth));
                }
                else if (proto.Equals("function"))
                {
                    node.AddEdgeListener(new FunctionEdgeListener(base_port + i, -1.00, ta_auth));
                }

                if (tunnel)
                {
                    Console.WriteLine("Adding a tunnel edge listener");
                    node.AddEdgeListener(new Relay.RelayEdgeListener(node));
                }
                _node_to_port[node] = base_port + i;
                node.RemoteTAs      = RemoteTA;
            }

            //start nodes one by one.
            for (int i = 0; i < network_size; i++)
            {
                Node node = (Node)_node_list[i];
                Console.WriteLine("Starting node: {0}, {1}", i, node.Address);
                node.Connect();
                Console.WriteLine("Going to sleep for 2 seconds.");
                System.Threading.Thread.Sleep(2000);
            }

            //wait for 300000 more seconds
            Console.WriteLine("Going to sleep for 300000 seconds.");
            System.Threading.Thread.Sleep(300000);
            bool complete = CheckStatus();

            int count = 0;

            //
            // Send a large number of packets as exact packets to random destinations
            // and make sure exact routing is perfect.
            //

            for (int i = 0; i < network_size; i++)
            {
                for (int j = 0; j < network_size; j++)
                {
                    int  src_idx   = i;
                    int  dest_idx  = j;
                    Node src_node  = (Node)_sorted_node_list.GetByIndex(src_idx);
                    Node dest_node = (Node)_sorted_node_list.GetByIndex(dest_idx);
                    //Console.WriteLine("{0} -> {1}", src_idx, dest_idx);
                    Address  dest_address = (Address)dest_node.Address;
                    ISender  s            = new AHExactSender(src_node, dest_address);
                    MemBlock p            = dest_address.ToMemBlock();
                    s.Send(new CopyList(new PType(routing_test), p));
                    _sent++;
                    //System.Threading.Thread.Sleep(10);
                    s.Send(new CopyList(new PType(routing_test), p));
                    _sent++;
                    //System.Threading.Thread.Sleep(10);
                }
            }
            //wait for 10 more seconds
            Console.WriteLine("Going to sleep for 10 seconds.");
            System.Threading.Thread.Sleep(10000);
            Console.WriteLine("Final statistics");
            lock (_class_lock) {
                Console.WriteLine("Sent: {0}, Received: {1}, Wrongly routed: {2}",
                                  _sent, _received, _wrongly_routed);
            }

            int       missing_rpcs     = 0;
            int       correct_rpcs     = 0;
            int       incorrect_rpcs   = 0;
            Hashtable queue_to_address = new Hashtable();

            for (int i = 0; i < network_size; i++)
            {
                for (int j = 0; j < network_size; j++)
                {
                    int  src_idx   = i;
                    int  dest_idx  = j;
                    Node src_node  = (Node)_sorted_node_list.GetByIndex(src_idx);
                    Node dest_node = (Node)_sorted_node_list.GetByIndex(dest_idx);
                    //Console.WriteLine("{0} -> {1}", src_idx, dest_idx);
                    Address    dest_address = (Address)dest_node.Address;
                    ISender    s            = new AHExactSender(src_node, dest_address);
                    RpcManager rpc_man      = src_node.Rpc;
                    Channel    q            = new Channel();
                    lock (_class_lock) {
                        queue_to_address[q] = dest_address;
                    }
                    q.CloseAfterEnqueue();
                    q.CloseEvent += delegate(object o, EventArgs cargs) {
                        lock (_class_lock) {
                            Channel qu = (Channel)o;
                            if (qu.Count == 0)
                            {
                                missing_rpcs++;
                            }
                            queue_to_address.Remove(qu);
                        }
                    };
                    q.EnqueueEvent += delegate(object o, EventArgs cargs) {
                        lock (_class_lock) {
                            Channel   qu        = (Channel)o;
                            RpcResult rpc_reply = (RpcResult)qu.Peek();
                            byte []   result    = (byte[])rpc_reply.Result;
                            Address   target    = new AHAddress(result);
                            if (target.Equals(queue_to_address[qu]))
                            {
                                correct_rpcs++;
                            }
                            else
                            {
                                incorrect_rpcs++;
                            }
                        }
                    };
                    rpc_man.Invoke(s, q, "rpc_routing_test.GetIdentification", new object[] {});
                }
            }

            //wait for 10 more seconds
            while (true)
            {
                int c = -1;
                lock (_class_lock) {
                    c = incorrect_rpcs + missing_rpcs + correct_rpcs;
                }
                if (c < network_size * network_size)
                {
                    Console.WriteLine("Going to sleep for 10 seconds.");
                    System.Threading.Thread.Sleep(10000);
                }
                else
                {
                    break;
                }
            }

            Console.WriteLine("Final statistics");
            Console.WriteLine("correct rpcs: {0}, incorrect rpcs: {1}, missing rpcs: {2}",
                              correct_rpcs, incorrect_rpcs, missing_rpcs);

            System.Environment.Exit(1);
        }
Example #19
0
 virtual protected void ConnectTo(Address target, string ConnectionType,
     string token)
 {
   ISender sender = new AHExactSender(_node, target);
   ConnectTo(sender, target, ConnectionType, _node.Address.ToString());
 }
Example #20
0
 protected void MissedMapping(string ip, Address addr)
 {
   ProtocolLog.WriteIf(IpopLog.ResolverLog, String.Format(
       "Notifying remote node of missing address: {0} : {1}", ip, addr));
   ISender sender = new AHExactSender(AppNode.Node, addr);
   AppNode.Node.Rpc.Invoke(sender, null, "Ipop.NoSuchMapping", ip);
 }
Example #21
0
    /// <summary>This method handles IPPackets that come from Brunet, i.e.,
    /// abroad.  </summary>
    /// <param name="packet"> The packet from Brunet.</param>
    /// <param name="ret">An ISender to send data to the Brunet node that sent
    /// the packet.</param>
    public virtual void HandleIPIn(MemBlock packet, ISender ret) {
      if(_secure_senders && !(ret is SecurityAssociation)) {
        return;
      }

      Address addr = null;
      if(ret is SecurityAssociation) {
        ret = ((SecurityAssociation) ret).Sender;
      }

      if(ret is AHSender) {
        addr = ((AHSender) ret).Destination;
      } else {
        ProtocolLog.Write(IpopLog.PacketLog, String.Format(
          "Incoming packet was not from an AHSender: {0}.", ret));
        return;
      }

      if(_translator != null) {
        try {
          packet = _translator.Translate(packet, addr);
        }
        catch (Exception e) {
          if(ProtocolLog.Exceptions.Enabled) {
            ProtocolLog.Write(ProtocolLog.Exceptions, e.ToString());
          }
          return;
        }
      }

      IPPacket ipp = new IPPacket(packet);

      try {
        if(!_address_resolver.Check(ipp.SourceIP, addr)) {
          return;
        }
      } catch (AddressResolutionException ex) {
        if(ex.Issue == AddressResolutionException.Issues.DoesNotExist) {
          ProtocolLog.WriteIf(IpopLog.ResolverLog, "Notifying remote node of " +
              " missing address: " + addr + ":" + ipp.SSourceIP);
          ISender sender = new AHExactSender(AppNode.Node, addr);
          AppNode.Node.Rpc.Invoke(sender, null, "Ipop.NoSuchMapping", ipp.SSourceIP);
          return;
        } else {
          throw;
        }
      }

      if(IpopLog.PacketLog.Enabled) {
        ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                          "Incoming packet:: IP src: {0}, IP dst: {1}, p2p " +
                              "from: {2}, size: {3}", ipp.SSourceIP, ipp.SDestinationIP,
                              ret, packet.Length));
      }

      WriteIP(packet);
    }
Example #22
0
 /// <summary>Sends the IP Packet to the specified target address.</summary>
 /// <param name="target"> the Brunet Address of the target</param>
 /// <param name="packet"> the data to send to the recepient</param>
 protected virtual void SendIP(Address target, MemBlock packet) {
   ISender s = null;
   if(_secure_senders) {
     try {
       s = AppNode.SymphonySecurityOverlord.GetSecureSender(target);
     }
     catch(Exception e) {
       Console.WriteLine(e);
       return;
     }
   }
   else {
     s = new AHExactSender(AppNode.Node, target);
   }
   s.Send(new CopyList(PType.Protocol.IP, packet));
 }