Пример #1
0
        public void TreatTLV(PeerAddress a, TLV tlv)
        {
            try
            {
                TLV_utils.DataMessage dm;
                switch (tlv.type)
                {
                case TLV.Type.Data:
                    dm = tlv_utils.getDataMessage(tlv);
                    if (dm != null && peers.IsSymetricNeighbor(a))
                    {
                        MessageIdentifier mid = new MessageIdentifier();
                        mid.nonce  = dm.nonce;
                        mid.sender = dm.sender;

                        // Adding message to the flooding list...
                        if (!recent_messages.ContainsKey(mid))
                        {
                            MessageFloodInfo mii = InitNewFloodInfo(dm.msg);
                            recent_messages[mid] = mii;
                            new_message_action(mid.sender, dm.msg);
                        }
                        // Ack & Remove from flooding list
                        com.SendMessage(a, messages.PackTLV(tlv_utils.ack(mid.sender, mid.nonce)));
                        try { recent_messages[mid].neighbors.Remove(a); } catch { }
                    }
                    break;

                case TLV.Type.Ack:
                    dm = tlv_utils.getAckMessage(tlv);
                    if (dm != null && peers.IsSymetricNeighbor(a))
                    {
                        MessageIdentifier mid = new MessageIdentifier();
                        mid.nonce  = dm.nonce;
                        mid.sender = dm.sender;
                        try { recent_messages[mid].neighbors.Remove(a); } catch { }
                    }
                    break;
                }
            }
            catch { }
        }
Пример #2
0
 public DataMessage getAckMessage(TLV tlv)
 {
     try
     {
         if (tlv.type != TLV.Type.Ack)
         {
             return(null);
         }
         if (tlv.body.Length != 12)
         {
             return(null);
         }
         DataMessage dm = new DataMessage();
         dm.sender = Utils.ToUInt64(tlv.body, 0);
         dm.nonce  = Utils.ToUInt32(tlv.body, 8);
         dm.msg    = null;
         return(dm);
     }
     catch { return(null); }
 }
Пример #3
0
        // Read the TLV at the given position, and update the pointer offset. Return null if it is a Pad or an incorrect/unknown TLV.
        public TLV Read(byte[] buffer, ref int offset)
        {
            if (buffer == null)
            {
                return(null);
            }
            try
            {
                byte type = buffer[offset];
                offset++;
                if (type == (byte)TLV.Type.Pad1)
                {
                    return(null);
                }
                byte length = buffer[offset];
                offset++;
                byte[] body = new byte[length];
                Array.Copy(buffer, offset, body, 0, length);
                offset += length;

                if (!Enum.IsDefined(typeof(TLV.Type), (int)type))
                {
                    return(null);
                }
                TLV t = new TLV();
                t.type = (TLV.Type)type;
                if (t.type == TLV.Type.PadN)
                {
                    return(null);
                }
                t.body = body;
                return(t);
            }
            catch
            {
                Utils.Debug("[ERROR] Invalid TLV !");
                // We put the pointer to the end of the buffer to avoid reading incorrect TLVs again.
                offset = buffer.Length;
            }
            return(null);
        }
Пример #4
0
 public byte?getGoAwayCode(TLV tlv)
 {
     try
     {
         if (tlv.type != TLV.Type.GoAway)
         {
             return(null);
         }
         try
         {
             string msg = Encoding.UTF8.GetString(tlv.body, 1, tlv.body.Length - 1);
             if (!String.IsNullOrEmpty(msg))
             {
                 Utils.Debug("[GO_AWAY] " + msg);
             }
         }
         catch { Utils.Debug("[ERROR] GoAway invalid message."); }
         return(tlv.body[0]);
     }
     catch { return(null); }
 }
Пример #5
0
 public byte[] PackTLV(TLV t)
 {
     return(PackBody(tlv_reader.Write(t)));
 }