Ejemplo n.º 1
0
 public override string ToString()
 {
     try {
         return(Data.GetString(System.Text.Encoding.ASCII));
     }
     catch {
         return("AnnounceState: could not get string as ASCII");
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// This method replaces IP addresses based on some identifier
 /// </summary>
 /// <param name="payload">Payload to be translated</param>
 /// <param name="old_ss_ip">Old source IP address</param>
 /// <param name="old_sd_ip">Old destination IP</param>
 /// <param name="new_ss_ip">New source IP address</param>
 /// <param name="new_sd_ip">New destination IP address</param>
 /// <param name="packet_id">A packet identifier</param>
 /// <returns>A MemBlock of the translated payload</returns>
 public static MemBlock TextTranslate(MemBlock payload, string old_ss_ip,
                                      string old_sd_ip, string new_ss_ip,
                                      string new_sd_ip, string packet_id) {
   string sdata = payload.GetString(System.Text.Encoding.UTF8);
   if(sdata.Contains(packet_id)) {
     sdata = sdata.Replace(old_ss_ip, new_ss_ip);
     sdata = sdata.Replace(old_sd_ip, new_sd_ip);
     payload = MemBlock.Reference(System.Text.Encoding.UTF8.GetBytes(sdata));
   }
   return payload;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// This method replaces IP addresses based on some identifier
        /// </summary>
        /// <param name="payload">Payload to be translated</param>
        /// <param name="old_ss_ip">Old source IP address</param>
        /// <param name="old_sd_ip">Old destination IP</param>
        /// <param name="new_ss_ip">New source IP address</param>
        /// <param name="new_sd_ip">New destination IP address</param>
        /// <param name="packet_id">A packet identifier</param>
        /// <returns>A MemBlock of the translated payload</returns>
        public static MemBlock TextTranslate(MemBlock payload, string old_ss_ip,
                                             string old_sd_ip, string new_ss_ip,
                                             string new_sd_ip, string packet_id)
        {
            string sdata = payload.GetString(System.Text.Encoding.UTF8);

            if (sdata.Contains(packet_id))
            {
                sdata   = sdata.Replace(old_ss_ip, new_ss_ip);
                sdata   = sdata.Replace(old_sd_ip, new_sd_ip);
                payload = MemBlock.Reference(System.Text.Encoding.UTF8.GetBytes(sdata));
            }
            return(payload);
        }
Ejemplo n.º 4
0
        /// <summary>All messages for the SecurityOverlord come through this loop.
        /// It demuxes between Security, SecureData, and SecureControl packets, while
        /// the remaining packets are left to the default handler.</summary>
        override public void HandleData(MemBlock data, ISender return_path, object state)
        {
            MemBlock payload = null;
            PType    t       = null;

            try {
                t = PType.Parse(data, out payload);

                if (t.Equals(Security))
                {
                    HandleData(payload, return_path, null);
                }
                else if (t.Equals(SecureData))
                {
                    HandleData(payload, return_path);
                }
                else if (t.Equals(SecureControl))
                {
                    HandleControl(payload, return_path);
                }
                else if (t.Equals(PType.Protocol.ReqRep))
                {
                    _rrman.HandleData(payload, return_path, null);
                }
                else
                {
                    Edge edge = return_path as Edge;
                    if (edge != null && !(edge is Brunet.Security.Transport.SecureEdge))
                    {
                        throw new Exception("Insecure edge attempting to communicate with the node!");
                    }
                    Subscriber sub = _sub;
                    if (sub == null)
                    {
                        throw new Exception("No default handler... this won't do!");
                    }
                    _sub.Handle(data, return_path);
                }
            } catch (Exception e) {
                string ps = string.Empty;
                try {
                    ps = payload.GetString(System.Text.Encoding.ASCII);
                } catch { }
                ProtocolLog.WriteIf(ProtocolLog.SecurityExceptions, String.Format(
                                        "Security Packet Handling Exception, Type: {0}, From: {1}\n\tData: {2}\n\tException:{3}\n\tStack Trace:{4}",
                                        t, return_path, ps, e, new System.Diagnostics.StackTrace(true)));
            }
        }
Ejemplo n.º 5
0
        /**
         * When a packet is to be delivered to this node,
         * this method is called.  This method is public so that
         * we can chain protocols through the node.  For instance,
         * after a packet is handled, it may be a wrapped packet
         * which actually contains another packet inside.  Thus,
         * the unwrapped packet could be "Announced" by the handler
         *
         * One needs to be careful to prevent an infinite loop of
         * a Handler announcing the packet it is supposed to handle.
         */
        protected virtual void Announce(MemBlock b, ISender from)
        {
            //When Subscribe or unsubscribe are called,
            //they make copies of the ArrayList, thus we
            //only need to hold the sync while we are
            //getting the list of handlers.

            /*
             * Note that getting from Hashtable is threadsafe, multiple
             * threads writing is a problem
             */
            MemBlock    payload  = null;
            int         handlers = 0;
            MultiSource ns       = null;
            PType       t        = null;

            try {
                t        = PType.Parse(b, out payload);
                ns       = (MultiSource)DemuxHandler.GetTypeSource(t);
                handlers = ns.Announce(payload, from);

                /**
                 * @todo if no one handled the packet, we might want to send some
                 * ICMP-like message.
                 */
                if (handlers == 0)
                {
                    string p_s = payload.GetString(System.Text.Encoding.ASCII);
                    ProtocolLog.WriteIf(ProtocolLog.Exceptions, String.Format(
                                            "No Handler for packet type: {0} from: {2}\n{1} :: {3}", t, p_s, from, b.ToBase16String()));
                }
            }
            catch (Exception x) {
                ProtocolLog.WriteIf(ProtocolLog.Exceptions, String.Format(
                                        "Packet Handling Exception"));
                string nodeSource = "null";
                if (ns != null)
                {
                    nodeSource = ns.ToString();
                }
                ProtocolLog.WriteIf(ProtocolLog.Exceptions, String.Format(
                                        "Handler: {0}\tEdge: {1}", nodeSource, from));
                ProtocolLog.WriteIf(ProtocolLog.Exceptions, String.Format(
                                        "Exception: {0}", x));
            }
        }
Ejemplo n.º 6
0
        public void HandleData(MemBlock b, ISender from, object state)
        {
            MemBlock payload = null;
            PType    t       = null;

            try {
                t = PType.Parse(b, out payload);
                if (t.Equals(PType.Protocol.ReqRep))
                {
                    _rrm.HandleData(payload, from, state);
                }
                else if (t.Equals(PType.Protocol.Rpc))
                {
                    Rpc.HandleData(payload, from, state);
                }
            }
            catch (Exception x) {
                Console.Error.WriteLine("Packet Handling Exception: {3}\n\tType: {0}\n\t\n\tFrom: {1}\n\tData: {2}",
                                        t, from, payload.GetString(System.Text.Encoding.ASCII), x);
            }
        }
Ejemplo n.º 7
0
        virtual public void HandleData(MemBlock data, ISender return_path, object state)
        {
            Address addr = GetAddress(return_path);

            if (addr == null)
            {
                ProtocolLog.WriteIf(ProtocolLog.ConnectionHandlerLog,
                                    String.Format("Unable to obtain an address for: {0}", return_path));
                return;
            }
            _ondemand.Set(addr);

            try {
                _sub.Handle(data, return_path);
            } catch {
                string d_s = data.GetString(System.Text.Encoding.ASCII);
                ProtocolLog.WriteIf(ProtocolLog.Exceptions, String.Format(
                                        "Error handling packet from {0}, containing {1}",
                                        return_path, d_s));
            }
        }
Ejemplo n.º 8
0
 /// <summary>This is the only method declared by IDataHandler.  All packets that
 /// are of PType("HelloWorld") will arrive here.</summary>
 /// <param name="payload">The data portion of the packet.</summary>
 /// <param name="return_path">The return path to the sending node.</summary>
 /// <param name="state">Optional state that is specified during the
 /// subscribe state.</summary>
 public void HandleData(MemBlock payload, ISender return_path, object state) {
   Console.WriteLine(return_path + ": " + payload.GetString(System.Text.Encoding.UTF8));
 }
Ejemplo n.º 9
0
 /// <summary>This is the only method declared by IDataHandler.  All packets that
 /// are of PType("HelloWorld") will arrive here.</summary>
 /// <param name="payload">The data portion of the packet.</summary>
 /// <param name="return_path">The return path to the sending node.</summary>
 /// <param name="state">Optional state that is specified during the
 /// subscribe state.</summary>
 public void HandleData(MemBlock payload, ISender return_path, object state)
 {
     Console.WriteLine(return_path + ": " + payload.GetString(System.Text.Encoding.UTF8));
 }