상속: IComparable
예제 #1
0
        //adding some kind of factory methods
        public static TransportAddress CreateInstance(string s)
        {
            Cache            ta_cache = Interlocked.Exchange <Cache>(ref _ta_cache, null);
            TransportAddress result   = null;

            if (ta_cache != null)
            {
                try {
                    result = (TransportAddress)ta_cache[s];
                    if (result == null)
                    {
                        result = NoCacheCreateInstance(s);
                        string r_ts = result.ToString();
                        if (r_ts.Equals(s))
                        {
                            //Keep the internal reference which is being saved already
                            s = r_ts;
                        }
                        ta_cache[s] = result;
                    }
                }
                finally {
                    Interlocked.Exchange <Cache>(ref _ta_cache, ta_cache);
                }
            }
            else
            {
                result = NoCacheCreateInstance(s);
            }
            return(result);
        }
예제 #2
0
        /**
         * return the list of TAs that should be tried
         */
        override public IList TargetTAs(IEnumerable hist)
        {
            /*
             * The trick here is, for a cone nat, we should only report
             * the most recently used ip/port pair.
             * For safety, we return the most recent two
             */
            ArrayList tas = new ArrayList();

            foreach (NatDataPoint p in hist)
            {
                TransportAddress last_reported = p.PeerViewOfLocalTA;
                if (last_reported != null)
                {
                    if (tas.Count == 0 || !last_reported.Equals(tas[0]))
                    {
                        tas.Add(last_reported);
                    }
                    if (tas.Count == 2)
                    {
                        return(tas);
                    }
                }
            }
            return(tas);
        }
예제 #3
0
 /// <summary>Retrieve a given EL Dictionary for the TA Type.  This could leak,
 /// though that would take the creation of many different EL types and in normal
 /// usage there will only be 1 or 2 types.</summary>
 static protected Dictionary<int, SimulationEdgeListener> GetEdgeListenerList(TransportAddress.TAType type)
 {
   if(!_el_map.ContainsKey(type)) {
     _el_map[type] = new Dictionary<int, SimulationEdgeListener>();
   }
   return _el_map[type];
 }
예제 #4
0
        public static TransportAddress CreateInstance(TransportAddress.TAType t,
                                                      IPAddress host, int port)
        {
            Cache ta_cache = Interlocked.Exchange <Cache>(ref _ta_cache, null);

            if (ta_cache != null)
            {
                TransportAddress ta = null;
                try {
                    CacheKey key = new CacheKey(host, port, t);
                    ta = (TransportAddress)ta_cache[key];
                    if (ta == null)
                    {
                        ta            = new IPTransportAddress(t, host, port);
                        ta_cache[key] = ta;
                    }
                }
                finally {
                    Interlocked.Exchange <Cache>(ref _ta_cache, ta_cache);
                }
                return(ta);
            }
            else
            {
                return(new IPTransportAddress(t, host, port));
            }
        }
예제 #5
0
        override public bool IsMyType(IEnumerable h)
        {
            int  port        = 0;
            bool port_is_set = false;
            bool retv        = true;

            foreach (NatDataPoint p in h)
            {
                int this_port = ((IPTransportAddress)p.LocalTA).Port;
                if (port_is_set == false)
                {
                    port        = this_port;
                    port_is_set = true;
                }
                else
                {
                    retv = retv && (this_port == port);
                }
                TransportAddress pv = p.PeerViewOfLocalTA;
                if (pv != null)
                {
                    //Check that everything is okay:
                    retv = retv && (((IPTransportAddress)pv).Port == port);
                }
                if (retv == false)
                {
                    break;
                }
            }
            return(retv);
        }
예제 #6
0
        /*
         * This is easy, just return the most recent non-null PeerViewTA.
         */
        override public IList TargetTAs(IEnumerable hist)
        {
            ArrayList        l     = new ArrayList();
            TransportAddress local = null;

            foreach (NatDataPoint p in hist)
            {
                if (local == null)
                {
                    //Get the most recent local
                    local = p.LocalTA;
                }
                TransportAddress pv = p.PeerViewOfLocalTA;
                if (pv != null)
                {
                    l.Add(pv);
                    break;
                }
            }
            if (l.Count == 0 && (local != null))
            {
                //We never found one, just use a local one
                l.Add(local);
            }
            return(l);
        }
예제 #7
0
        //adding some kind of factory methods
        public static TransportAddress CreateInstance(string s)
        {
            var ta_cache            = Interlocked.Exchange <WeakValueTable <string, TransportAddress> >(ref _ta_cache, null);
            TransportAddress result = null;

            if (ta_cache != null)
            {
                try {
                    result = ta_cache.GetValue(s);
                    if (result == null)
                    {
                        result = NoCacheCreateInstance(s);
                        string r_ts = result.ToString();
                        if (r_ts.Equals(s))
                        {
                            //Keep the internal reference which is being saved already
                            s = r_ts;
                        }
                        ta_cache.Replace(s, result);
                    }
                }
                finally {
                    Interlocked.Exchange <WeakValueTable <string, TransportAddress> >(ref _ta_cache, ta_cache);
                }
            }
            else
            {
                result = NoCacheCreateInstance(s);
            }
            return(result);
        }
예제 #8
0
        public override TAAuthorizer.Decision Authorize(TransportAddress a)
        {
            IPAddress ipa = null;

            try {
                ipa = (IPAddress)(((IPTransportAddress)a).GetIPAddress());
            } catch (Exception x) {
                BU.ProtocolLog.WriteIf(BU.ProtocolLog.Exceptions, String.Format(
                                           "{0}", x));
            }
            if (ipa == null)
            {
                return(_result_on_mismatch);
            }

            byte[] add_bytes = ipa.GetAddressBytes();
            int    bits      = _bit_c;
            int    block     = 0;
            bool   match     = true;

            while (bits > 0 && match)
            {
                match = FirstBitsMatch(add_bytes[block], _nw_bytes[block], bits);
                bits -= 8;
                block++;
            }
            if (match)
            {
                return(_result_on_match);
            }
            else
            {
                return(_result_on_mismatch);
            }
        }
예제 #9
0
        public void Test()
        {
            TAAuthorizer     a1 = new ConstantAuthorizer(TAAuthorizer.Decision.Allow);
            TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.udp://127.0.0.1:45");

            Assert.IsTrue(a1.IsNotDenied(ta), "constant allow");
            TAAuthorizer a2 = new ConstantAuthorizer(TAAuthorizer.Decision.Deny);

            Assert.IsFalse(a2.IsNotDenied(ta), "constant deny");

            IPAddress    network = IPAddress.Parse("10.128.0.0");
            TAAuthorizer a3      = new NetmaskTAAuthorizer(network, 9,
                                                           TAAuthorizer.Decision.Deny,
                                                           TAAuthorizer.Decision.None);
            TransportAddress ta2 = TransportAddressFactory.CreateInstance("brunet.udp://10.255.255.255:80");

            Assert.AreEqual(a3.Authorize(ta2), TAAuthorizer.Decision.Deny, "Netmask Deny");
            TransportAddress ta3 = TransportAddressFactory.CreateInstance("brunet.udp://10.1.255.255:80");

            Assert.AreEqual(a3.Authorize(ta3), TAAuthorizer.Decision.None, "Netmask None");
            //Here is the series:
            //If Netmask doesn't say no, constant says yes:
            TAAuthorizer[] my_auths = new TAAuthorizer[] { a3, a1 };
            TAAuthorizer   a4       = new SeriesTAAuthorizer(my_auths);

            Assert.AreEqual(a4.Authorize(ta2), TAAuthorizer.Decision.Deny, "Series Deny");
            Assert.AreEqual(a4.Authorize(ta3), TAAuthorizer.Decision.Allow, "Series Allow");
        }
예제 #10
0
        /* //////////////////////////////
         *
         * Here are all the normal methods of TcpEdgeListener
         *
         * //////////////////////////////
         */


        public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb)
        {
            try {
                if (!IsStarted)
                {
                    throw new EdgeException("TcpEdgeListener is not started");
                }
                else if (ta.TransportAddressType != TransportAddress.TAType.Tcp)
                {
                    throw new EdgeException(ta.TransportAddressType.ToString()
                                            + " is not my type: Tcp");
                }
                else if (_ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny)
                {
                    //Too bad.  Can't make this edge:
                    throw new EdgeException(ta.ToString() + " is not authorized");
                }
                else
                {
                    //Everything looks good:
                    ArrayList tmp_ips = new ArrayList();
                    tmp_ips.Add(((IPTransportAddress)ta).GetIPAddress());
                    CreationState cs = new CreationState(ecb,
                                                         new Queue(tmp_ips),
                                                         ((IPTransportAddress)ta).Port,
                                                         this);
                    ActionQueue.Enqueue(cs);
                }
            } catch (Exception e) {
                ecb(false, null, e);
            }
        }
예제 #11
0
        /** return the base TransportAddress and the path associated with it
         */
        public static TransportAddress SplitPath(TransportAddress ta, out string path)
        {
            string tas = ta.ToString();
            // Need to be careful of the case ta:////ta:9/
            int  init_idx = tas.IndexOf("://") + 3;
            int  idx      = init_idx;
            int  pos      = 0;
            bool next     = false;

            for (; idx < tas.Length; idx++)
            {
                if (tas[idx] == '/')
                {
                    if (!next)
                    {
                        pos = idx;
                    }
                }
                else
                {
                    next = false;
                }
            }

            if (pos > 0)
            {
                path = tas.Substring(pos);
                return(TransportAddressFactory.CreateInstance(tas.Substring(0, pos)));
            }
            else
            {
                path = "/";
                return(ta);
            }
        }
예제 #12
0
        /**
         * Implements the EdgeListener function to
         * create edges of this type.
         */
        public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb)
        {
            Edge      e  = null;
            Exception ex = null;

            if (!IsStarted)
            {
                ex = new EdgeException("UdpEdgeListener is not started");
            }
            else if (ta.TransportAddressType != this.TAType)
            {
                ex = new EdgeException(ta.TransportAddressType.ToString()
                                       + " is not my type: " + this.TAType.ToString());
            }
            else if (_ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny)
            {
                ex = new EdgeException(ta.ToString() + " is not authorized");
            }
            else
            {
                IPAddress  first_ip = ((IPTransportAddress)ta).GetIPAddress();
                IPEndPoint end      = new IPEndPoint(first_ip, ((IPTransportAddress)ta).Port);
                /* We have to keep our mapping of end point to edges up to date */
                lock ( _id_ht ) {
                    //Get a random ID for this edge:
                    int id;
                    do
                    {
                        id = _rand.Next();
                        //Make sure we don't have negative ids
                        if (id < 0)
                        {
                            id = ~id;
                        }
                    } while(_id_ht.Contains(id) || id == 0);
                    e          = new UdpEdge(this, false, end, _local_ep, id, 0);
                    _id_ht[id] = e;
                }
                NatDataPoint dp = new NewEdgePoint(DateTime.UtcNow, e);
                Interlocked.Exchange <NatHistory>(ref _nat_hist, _nat_hist + dp);
                Interlocked.Exchange <IEnumerable>(ref _nat_tas, new NatTAs(_tas, _nat_hist));

                try {
                    /* Tell me when you close so I can clean up the table */
                    e.CloseEvent += this.CloseHandler;
                } catch (Exception x) {
                    e  = null;
                    ex = x;
                }
            }

            if (e != null)
            {
                ecb(true, e, null);
            }
            else
            {
                ecb(false, null, ex);
            }
        }
예제 #13
0
        /**
         * Gets a list of all the IPAddress objects which may
         * represent NATs we are behind
         */
        public IEnumerable PeerViewIPs()
        {
            Filter f = delegate(NatDataPoint p) {
                TransportAddress ta = p.PeerViewOfLocalTA;
                IPAddress        a  = null;
                if (ta != null)
                {
                    try {
                        a = ((IPTransportAddress)ta).GetIPAddress();
                    } catch (Exception x) {
                        BU.ProtocolLog.WriteIf(BU.ProtocolLog.Exceptions, String.Format(
                                                   "{0}", x));
                    }
                }
                return(a);
            };
            IEnumerable e = new FilteredNDP(this, f);

            /*
             * Go through all the addresses, but only keep
             * one copy of each, with the most recent address last.
             */
            ArrayList list = new ArrayList();

            foreach (IPAddress a in e)
            {
                if (list.Contains(a))
                {
                    list.Remove(a);
                }
                list.Add(a);
            }
            return(list);
        }
예제 #14
0
파일: IDTAAuth.cs 프로젝트: pstjuste/brunet
 public override TAAuthorizer.Decision Authorize(TransportAddress a) {
   if (_denied_id == ((SimulationTransportAddress)a).ID) {
     return TAAuthorizer.Decision.Deny;
   } else {
     //else this decision should not matter
     return TAAuthorizer.Decision.None;
   }
 }
예제 #15
0
파일: ConeNat.cs 프로젝트: pstjuste/brunet
 public void UpdateTAs(TransportAddress remote_ta, TransportAddress local_ta)
 {
   if(_known_tas.Length == 2) {
     return;
   }
   _known_tas = new TransportAddress[2] { _internal_ta[0], local_ta };
   _allow_inbound = true;
 }
예제 #16
0
 public override TAAuthorizer.Decision Authorize(TransportAddress a)
 {
     if (_match_table.ContainsKey(a.TransportAddressType))
     {
         return(_on_match);
     }
     return(_on_mismatch);
 }
예제 #17
0
 public void TestTATypeToString()
 {
     foreach (TransportAddress.TAType t in
              Enum.GetValues(typeof(TransportAddress.TAType)))
     {
         string s = t.ToString().ToLower();
         Assert.AreEqual(s, TransportAddress.TATypeToString(t), "TATypeToString");
     }
 }
예제 #18
0
        public override void UpdateRemoteTAs(IList <TransportAddress> list, Edge e, TransportAddress ta)
        {
            PathEdge pe = e as PathEdge;

            if (pe != null)
            {
                _el.UpdateRemoteTAs(list, pe.Edge, ta);
            }
        }
예제 #19
0
 public EdgeCreationWrapper(TransportAddress ta, EdgeListener.EdgeCreationCallback ecb,
                            Edge edge, WrapperEdgeListener parent)
 {
     ExternalECB = ecb;
     TA          = ta;
     Parent      = parent;
     _edge       = edge;
     _called     = 0;
 }
예제 #20
0
        ///<summary>This is passed tothe underlying EdgeListener.  By default we do
        ///nothing to this.</summary>
        public override void UpdateRemoteTAs(IList <TransportAddress> list, Edge e, TransportAddress ta)
        {
            WrapperEdge edge = e as WrapperEdge;

            if (edge != null)
            {
                _el.UpdateRemoteTAs(list, edge.WrappedEdge, ta);
            }
        }
예제 #21
0
        ///<summary>This is passed tothe underlying EdgeListener.  By default we do
        ///nothing to this.</summary>
        public override void UpdateLocalTAs(Edge e, TransportAddress ta)
        {
            WrapperEdge edge = e as WrapperEdge;

            if (edge != null)
            {
                _el.UpdateLocalTAs(edge.WrappedEdge, ta);
            }
        }
예제 #22
0
 public LocalMappingChangePoint(DateTime dt, Edge e,
                                TransportAddress new_ta)
 {
     _date    = dt;
     _edge_no = e.Number;
     _local   = e.LocalTA;
     _remote  = e.RemoteTA;
     _p_local = new_ta;
 }
예제 #23
0
파일: XmppEdge.cs 프로젝트: pstjuste/brunet
 /// <summary>Create a XmppEdge.</summary>
 public XmppEdge(IEdgeSendHandler send_handler, XmppTransportAddress local_ta,
     XmppTransportAddress remote_ta, bool inbound) :
   base(send_handler, inbound)
 {
   _ip = new IdentifierPair();
   _local_ta = local_ta;
   _remote_ta = remote_ta;
   To = remote_ta.JID;
 }
예제 #24
0
파일: ConeNat.cs 프로젝트: pstjuste/brunet
 public ConeNat(TransportAddress ta, int timeout)
 {
   _external_ta = new TransportAddress[1] { ta };
   _internal_ta = new TransportAddress[1] { ((SimulationTransportAddress) ta).Invert() };
   _known_tas = _internal_ta;
   // TBC uses a staged GC, so values are still in after one timeout
   _allowed = new TimeBasedCache<TransportAddress, bool>(timeout / 2);
   _allowed.EvictionHandler += HandleEviction;
   _allow_inbound = false;
 }
예제 #25
0
 /// <summary>Constructor for an outgoing edge, since we don't know the remote
 /// id yet, it must be outgoing!</summary>
 public SubringEdge(TransportAddress local_ta, TransportAddress remote_ta,
     bool inbound, ISender sender, PType ptype) :
     base(null, inbound)
 {
   _ip = new IdentifierPair();
   _local_ta = local_ta;
   _remote_ta = remote_ta;
   _ptype = ptype;
   _overlay_sender = sender;
 }
예제 #26
0
        //Remote the TA from the set
        protected void RemoveRemoteTA(Hashtable ht, int port, TransportAddress ta)
        {
            ArrayList l = (ArrayList)ht[port];

            l.Remove(ta);
            if (l.Count == 0)
            {
                //Get this out of here.
                ht.Remove(port);
            }
        }
예제 #27
0
 /**
  * When a new Connection is added, we may need to update the list
  * of TAs to make sure it is not too long, and that the it is sorted
  * from most likely to least likely to be successful
  * @param e the new Edge
  * @param ta the TransportAddress our TA according to our peer
  */
 public override void UpdateLocalTAs(Edge e, TransportAddress ta)
 {
     if (e.TAType == this.TAType)
     {
         UdpEdge ue = (UdpEdge)e;
         ue.PeerViewOfLocalTA = ta;
         NatDataPoint dp = new LocalMappingChangePoint(DateTime.UtcNow, e, ta);
         Interlocked.Exchange <NatHistory>(ref _nat_hist, _nat_hist + dp);
         Interlocked.Exchange <IEnumerable>(ref _nat_tas, new NatTAs(_tas, _nat_hist));
     }
 }
예제 #28
0
 public SimulationEdge(IEdgeSendHandler s, int local_id, int remote_id,
     bool is_in, int delay, TransportAddress.TAType type) : base(s, is_in)
 {
   Delay = delay;
   LocalID = local_id;
   RemoteID = remote_id;
   _ta_type = type;
   _local_ta = GetTransportAddress(local_id);
   _remote_ta = GetTransportAddress(remote_id);
   SimEL = s as SimulationEdgeListener;
 }
예제 #29
0
 public override TAAuthorizer.Decision Authorize(TransportAddress a)
 {
     if (_denied_id == ((SimulationTransportAddress)a).ID)
     {
         return(TAAuthorizer.Decision.Deny);
     }
     else
     {
         //else this decision should not matter
         return(TAAuthorizer.Decision.None);
     }
 }
예제 #30
0
 public override TAAuthorizer.Decision Authorize(TransportAddress a)
 {
     if (_denied_port == ((IPTransportAddress)a).Port)
     {
         return(TAAuthorizer.Decision.Deny);
     }
     else
     {
         //else this decision should not matter
         return(TAAuthorizer.Decision.None);
     }
 }
예제 #31
0
 /**
  * This creates Edges of a given type
  */
 public void CreateEdgeTo(TransportAddress destination,
                          EdgeListener.EdgeCreationCallback ecb)
 {
   TransportAddress.TAType t = destination.TransportAddressType;
   if( _el_map.Contains( t ) ) {
     EdgeListener el = (EdgeListener)_el_map[ t ];
     el.CreateEdgeTo( destination, ecb );
   }
   else {
     ecb(false, null, new EdgeException("No EdgeListener for TA type: " +
                                        t.ToString() ) );
   }
 }
예제 #32
0
 /**
  * Go through the list of Authorizers returning the first decision
  */
 public override TAAuthorizer.Decision Authorize(TransportAddress a)
 {
     TAAuthorizer.Decision result = TAAuthorizer.Decision.None;
     foreach (TAAuthorizer ipa in _authorizers)
     {
         result = ipa.Authorize(a);
         if (result != TAAuthorizer.Decision.None)
         {
             break;
         }
     }
     return(result);
 }
예제 #33
0
        public override void UpdateLocalTAs(Edge e, TransportAddress ta)
        {
            PathEdge pe = e as PathEdge;

            if (pe != null)
            {
                _el.UpdateLocalTAs(pe.Edge, ta);
            }
            else
            {
                _el.UpdateLocalTAs(e, ta);
            }
        }
예제 #34
0
 public override TAAuthorizer.Decision Authorize(TransportAddress a)
 {
     if (_deny_list.Contains(a))
     {
         return(TAAuthorizer.Decision.Deny);
     }
     // randomly deny the TA
     if (_rand.NextDouble() > _deny_prob)
     {
         _deny_list.Add(a);
         return(TAAuthorizer.Decision.Deny);
     }
     return(TAAuthorizer.Decision.Allow);
 }
예제 #35
0
        /** Join a path to the end of a TransportAddress
         */
        public static TransportAddress JoinPath(TransportAddress ta, string path)
        {
            Uri    orig_u = ta.Uri;
            string s      = orig_u.ToString();

            if (s[s.Length - 1] == '/')
            {
                s = s.Substring(0, s.Length - 1);
            }
            if (path[0] == '/')
            {
                path = path.Substring(1);
            }
            return(TransportAddressFactory.CreateInstance(String.Format("{0}/{1}", s, path)));
        }
예제 #36
0
 /**
  * This creates Edges of a given type
  */
 public void CreateEdgeTo(TransportAddress destination,
                          EdgeListener.EdgeCreationCallback ecb)
 {
     TransportAddress.TAType t = destination.TransportAddressType;
     if (_el_map.Contains(t))
     {
         EdgeListener el = (EdgeListener)_el_map[t];
         el.CreateEdgeTo(destination, ecb);
     }
     else
     {
         ecb(false, null, new EdgeException("No EdgeListener for TA type: " +
                                            t.ToString()));
     }
 }
예제 #37
0
        //Remote the TA from the set
        protected void RemoveRemoteTA(Hashtable ht, int port, TransportAddress ta)
        {
            ArrayList l = ht[port] as ArrayList;

            if (l == null)
            {
                return;
            }
            l.Remove(ta);
            if (l.Count == 0)
            {
                //Get this out of here.
                ht.Remove(port);
            }
        }
예제 #38
0
        /**
         * An IEnumerator of all the LocalTAs (our view of them, not peer view)
         */
        public IEnumerable LocalTAs()
        {
            Hashtable ht = new Hashtable();
            Filter    f  = delegate(NatDataPoint p) {
                TransportAddress ta = p.LocalTA;
                if (ta != null && (false == ht.Contains(ta)))
                {
                    ht[ta] = true;
                    return(ta);
                }
                return(null);
            };

            return(new FilteredNDP(this, f));
        }
예제 #39
0
    /// <summary>Create a SubringEdgeListener.</summary>
    /// <param name="shared_node">The overlay used for the transport.</param>
    /// <param name="private_node">The overlay needing edges.</param>
    public SubringEdgeListener(Node shared_node, Node private_node)
    {
      _shared_node = shared_node;
      _private_node = private_node;
      _it = new IdentifierTable();

      _local_ta = new SubringTransportAddress(shared_node.Address as AHAddress,
          shared_node.Realm);

      _ptype = new PType("ns:" + shared_node.Realm);
      shared_node.DemuxHandler.GetTypeSource(_ptype).Subscribe(this, null);

      _running = 0;
      _started = 0;
    }
예제 #40
0
    public override TAAuthorizer.Decision Authorize(TransportAddress a)
    {
      int id = ((SimulationTransportAddress) a).ID;
      if(id == 0) {
        return TAAuthorizer.Decision.Allow;
      }

      if(!_allowed.Contains(id)) {
        if(Rand.NextDouble() > Prob) {
          _allowed[id] = TAAuthorizer.Decision.Allow;
        } else {
          _allowed[id] = TAAuthorizer.Decision.Deny;
        }
      }

      return (TAAuthorizer.Decision) _allowed[id];
    }
예제 #41
0
    public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb)
    {
      SubringTransportAddress sta = ta as SubringTransportAddress;

      if(sta == null) {
        ecb(false, null, new Exception("TA Type is not Subring!"));
      } else if(!sta.Namespace.Equals(_shared_node.Realm)) {
        ecb(false, null, new Exception("Namespace mismatch"));
      } else if(sta.Target.Equals(_private_node.Address)) {
        ecb(false, null, new Exception("You are me!"));
      } else {
        SubringEdge se = new SubringEdge(_local_ta, sta, false,
            new AHExactSender(_shared_node, sta.Target), _ptype);
        se.CloseEvent += CloseHandler;
        _it.Add(se);
        ecb(true, se, null);
      }
    }
예제 #42
0
    public void Test()
    {
      SimulationTransportAddress.Enable();
      SimulationTransportAddressOther.Enable();
      var ta = TransportAddressFactory.CreateInstance("b.s://234580") as SimulationTransportAddress;
      var tai = ta.Invert();
      TransportAddress[] tas = new TransportAddress[2] { tai, ta };
      var ta_oth = TransportAddressFactory.CreateInstance("b.s://234581");
      var ta_oth0 = TransportAddressFactory.CreateInstance("b.s://234582");
      var nat = new RestrictedConeNat(ta, 30000);
      Assert.IsFalse(nat.Incoming(ta_oth), "No outbound yet...");
      Assert.IsTrue(nat.Outgoing(ta_oth), "outbound...");
      Assert.IsFalse(nat.AllowingIncomingConnections, "Have not received external ta.");
      Assert.AreEqual(nat.InternalTransportAddresses, nat.KnownTransportAddresses, "ITA and KTA match");

      nat.UpdateTAs(ta_oth, ta);
      Assert.IsTrue(nat.Incoming(ta_oth), "Allowed incoming");
      Assert.IsFalse(nat.Incoming(ta_oth0), "Port mapped systems must send out a packet first...");
      Assert.IsTrue(nat.AllowingIncomingConnections, "Have received external ta.");
      Assert.AreEqual(tas, nat.KnownTransportAddresses, "Two TAs!");
      Assert.IsTrue(nat.Outgoing(ta_oth0), "outbound...");

      Brunet.Util.SimpleTimer.RunSteps(7500);
      Assert.IsTrue(nat.Incoming(ta_oth0), "Allowed incoming 0");
      Brunet.Util.SimpleTimer.RunSteps(7500);
      Assert.IsTrue(nat.Incoming(ta_oth0), "Allowed incoming 0");
      Brunet.Util.SimpleTimer.RunSteps(7500);
      Assert.IsTrue(nat.Incoming(ta_oth0), "Allowed incoming 0");
      Brunet.Util.SimpleTimer.RunSteps(7500);
      Assert.IsTrue(nat.Incoming(ta_oth0), "Allowed incoming 0");
      Assert.IsTrue(nat.AllowingIncomingConnections, "Have received external ta.");
      Assert.AreEqual(tas, nat.KnownTransportAddresses, "Two TAs!");

      Brunet.Util.SimpleTimer.RunSteps(60000);
      Assert.IsFalse(nat.AllowingIncomingConnections, "AllowIC:  Timed out...");
      Assert.IsFalse(nat.Incoming(ta_oth), "Incoming:  Timed out....");
    }
예제 #43
0
 /**
  * This gets the name of the local machine, then does a DNS lookup on that
  * name, and finally does the same as TransportAddress.Create for that
  * list of IPAddress objects.
  *
  * If the DNS hostname is not correctly configured, it will return the
  * loopback address.
  */
 public static IEnumerable CreateForLocalHost(TransportAddress.TAType tat, int port) {
   try {
     string StrLocalHost = Dns.GetHostName();
     IPHostEntry IPEntry = Dns.GetHostEntry(StrLocalHost);
     return Create(tat, port, IPEntry.AddressList);
   }
   catch(Exception) {
     //Oh, well, that didn't work.
     ArrayList tas = new ArrayList(1);
     //Just put the loopback address, it might help us talk to some other
     //local node.
     tas.Add( CreateInstance(tat, new IPEndPoint(IPAddress.Loopback, port) ) );
     return tas;
   }
 }    
예제 #44
0
 public override void UpdateRemoteTAs(IList<TransportAddress> list, Edge e, TransportAddress ta) {
   PathEdge pe = e as PathEdge;
   if(pe != null) {
     _el.UpdateRemoteTAs(list, pe.Edge, ta);
   }
 }
예제 #45
0
 public override void UpdateLocalTAs(Edge e, TransportAddress ta) {
   PathEdge pe = e as PathEdge;
   if(pe != null) {
     _el.UpdateLocalTAs(pe.Edge, ta);
   } else {
     _el.UpdateLocalTAs(e, ta);
   }
 }
예제 #46
0
 /** creates a new outgoing Edge using the pathing protocol
  */
 public override void CreateEdgeTo(TransportAddress ta,
                                   EdgeListener.EdgeCreationCallback ecb) {
   if( !IsStarted ) {
     throw new EdgeException("PathEdgeListener is not started");
   }
   string rempath;
   TransportAddress base_ta = PathELManager.SplitPath(ta, out rempath);
   bool root = false;
   if( _path == PathELManager.Root && rempath == PathELManager.Root ) {
     root = true;
   }
   CreateState cs = new CreateState(this, rempath, _path, ecb, root);
   _el.CreateEdgeTo(base_ta, cs.HandleEC);
 }
예제 #47
0
    /** return the base TransportAddress and the path associated with it
     */
    public static TransportAddress SplitPath(TransportAddress ta, out string path) {
      string tas = ta.ToString();
      // Need to be careful of the case ta:////ta:9/
      int init_idx = tas.IndexOf("://") + 3;
      int idx = init_idx;
      int pos = 0;
      bool next = false;
      for(; idx < tas.Length; idx++) {
        if(tas[idx] == '/') {
          if(!next) {
            pos = idx;
          }
        } else {
          next = false;
        }
      }

      if(pos > 0) {
        path = tas.Substring(pos);
        return TransportAddressFactory.CreateInstance(tas.Substring(0, pos));
      } else {
        path = "/";
        return ta;
      }
    }
예제 #48
0
 /** Join a path to the end of a TransportAddress
  */
 public static TransportAddress JoinPath(TransportAddress ta, string path) {
   Uri orig_u = ta.Uri;
   string s = orig_u.ToString();
   if( s[s.Length - 1] == '/' ) {
     s = s.Substring(0, s.Length - 1);
   }
   if (path[0] == '/') {
     path = path.Substring(1);
   }
   return TransportAddressFactory.CreateInstance(String.Format("{0}/{1}", s, path));
 }
예제 #49
0
    /**
     * Implements the EdgeListener function to 
     * create edges of this type.
     */
    public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb)
    {
      Edge e = null;
      Exception ex = null;
      if( !IsStarted ) {
        ex = new EdgeException("UdpEdgeListener is not started");
      } else if( ta.TransportAddressType != this.TAType ) {
        ex = new EdgeException(ta.TransportAddressType.ToString()
            + " is not my type: " + this.TAType.ToString() );
      } else if( _ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny ) {
        ex = new EdgeException( ta.ToString() + " is not authorized");
      } else {
        IPAddress first_ip = ((IPTransportAddress) ta).GetIPAddress();
        IPEndPoint end = new IPEndPoint(first_ip, ((IPTransportAddress) ta).Port);
        /* We have to keep our mapping of end point to edges up to date */
        lock( _id_ht ) {
          //Get a random ID for this edge:
          int id;
          do {
            id = _rand.Next();
	    //Make sure we don't have negative ids
            if( id < 0 ) { id = ~id; }
          } while( _id_ht.Contains(id) || id == 0 );
          e = new UdpEdge(this, false, end, _local_ep, id, 0);
          _id_ht[id] = e;
        }
        NatDataPoint dp = new NewEdgePoint(DateTime.UtcNow, e);
        Interlocked.Exchange<NatHistory>(ref _nat_hist, _nat_hist + dp);
        Interlocked.Exchange<IEnumerable>(ref _nat_tas, new NatTAs( _tas, _nat_hist ));

        try {
          /* Tell me when you close so I can clean up the table */
          e.CloseEvent += this.CloseHandler;
        } catch (Exception x) {
          e = null;
          ex = x;
        }
      }

      if(e != null) {
        ecb(true, e, null);
      } else {
        ecb(false, null, ex);
      }
    }
예제 #50
0
 /**
  * When a new Connection is added, we may need to update the list
  * of TAs to make sure it is not too long, and that the it is sorted
  * from most likely to least likely to be successful
  * @param e the new Edge
  * @param ta the TransportAddress our TA according to our peer
  */
 public override void UpdateLocalTAs(Edge e, TransportAddress ta) {
   if( e.TAType == this.TAType ) {
     UdpEdge ue = (UdpEdge)e;
     ue.PeerViewOfLocalTA = ta;
     NatDataPoint dp = new LocalMappingChangePoint(DateTime.UtcNow, e, ta);
     Interlocked.Exchange<NatHistory>(ref _nat_hist, _nat_hist + dp);
     Interlocked.Exchange<IEnumerable>(ref _nat_tas, new NatTAs( _tas, _nat_hist ));
   }
 }
예제 #51
0
 public IPTransportEnum(TransportAddress.TAType tat, int port, IEnumerable ips) {
   _tat = tat;
   _port = port;
   _ips = ips;
 }
예제 #52
0
 public static TransportAddress CreateInstance(TransportAddress.TAType t,
        IPEndPoint ep){
   return CreateInstance(t, ep.Address, ep.Port);
 }
예제 #53
0
    /*
     * Implements the EdgeListener function to 
     * create edges of this type.
     */
    public override void CreateEdgeTo(TransportAddress ta,
                                      EdgeCreationCallback ecb)
    {
      if( !IsStarted )
      {
        // it should return null and not throw an exception
        // for graceful disconnect and preventing others to
        // connect to us after we've disconnected.
        ecb(false, null, new EdgeException("Not started"));
        return;
      }

      if( ta.TransportAddressType != this.TAType ) {
        //Can't make an edge of this type
        ecb(false, null, new EdgeException("Can't make edge of this type"));
        return;
      }
      
      if( _ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny ) {
        //Not authorized.  Can't make this edge:
        ecb(false, null, new EdgeException( ta.ToString() + " is not authorized") );
        return;
      }
      
      int remote_id = ((SimulationTransportAddress) ta).ID;
      //Get the edgelistener: 

      //Outbound edge:
      int delay = 0;
      if(_use_delay) {
        if(LatencyMap != null) {
          // id != 0, so we reduce all by 1
          delay = LatencyMap[_listener_id][remote_id] / 1000;
        } else {
          delay = 100;
        }
      }

      SimulationEdge se_l = new SimulationEdge(this, _listener_id, remote_id, false, delay);
      AddEdge(se_l);

      SimulationEdgeListener remote = (SimulationEdgeListener) _listener_map[remote_id];
      if( remote != null ) {
        //
        // Make sure that the remote listener does not deny 
        // our TAs.
        //

        foreach (TransportAddress ta_local in LocalTAs) {
          if (remote.TAAuth.Authorize(ta_local) == TAAuthorizer.Decision.Deny ) {
          //Not authorized.  Can't make this edge:
            ecb(false, null, new EdgeException( ta_local.ToString() + " is not authorized by remote node.") );
            return;
          }
        }

        SimulationEdge se_r = new SimulationEdge(remote, remote_id, _listener_id, true, delay);
        remote.AddEdge(se_r);

        se_l.Partner = se_r;
        se_r.Partner = se_l;
        remote.SendEdgeEvent(se_r);
      } else {
          //There is no other edge, for now, we use "udp-like"
          //behavior of just making an edge that goes nowhere.
      }
      ecb(true, se_l, null);
    }
예제 #54
0
 /**
  * Creates an IEnumerable of TransportAddresses for a fixed type and port,
  * over a list of IPAddress objects.
  * Each time this the result is enumerated, ips.GetEnumerator is called,
  * so, if it changes, that is okay, (this is like a map() over a list, and
  * the original list can change).
  */
 public static IEnumerable Create(TransportAddress.TAType tat, int port, IEnumerable ips)
 {
   return new IPTransportEnum(tat, port, ips);
 }
예제 #55
0
 public bool Outgoing(TransportAddress remote_ta)
 {
   return true;
 }
예제 #56
0
 public PublicNat(TransportAddress local_ta)
 {
   _local_tas = new TransportAddress[1] { local_ta };
 }
예제 #57
0
 public bool Incoming(TransportAddress remote_ta)
 {
   return true;
 }
예제 #58
0
 public IPTransportAddress(TransportAddress.TAType t, string host, int port) :
   this(String.Format("brunet.{0}://{1}:{2}", TATypeToString(t), host, port))
 {
   _type = t;
   _ips = null;
 }
예제 #59
0
 public void UpdateTAs(TransportAddress remote_ta, TransportAddress local_ta)
 {
 }
예제 #60
0
 public IPTransportAddress(TransportAddress.TAType t, IPAddress addr, int port):
   this(String.Format("brunet.{0}://{1}:{2}", TATypeToString(t), addr, port))
 {
   _type = t;
   _ips = new ArrayList(1);
   _ips.Add( addr );
 }