Пример #1
0
        private void sendName(int dist, ErlAbstractNode.NodeCompatibility flags)
        {
            string str = m_Home.NodeName.Value;
              var obuf = new ErlOutputStream(writeVersion: false, writePktSize: false, capacity: 7 + str.Length);
              obuf.Write2BE(str.Length + 7); // 7 bytes + nodename
              obuf.Write1((int)ErlAbstractNode.NodeType.Ntype_R6);
              obuf.Write2BE((short)dist);
              obuf.Write4BE((int)flags);
              obuf.Write(Encoding.ASCII.GetBytes(str));

              obuf.WriteTo(m_Transport.GetStream());

              m_Home.OnTrace(ErlTraceLevel.Handshake, Direction.Outbound, () =>
              "sendName(flags({0:X2})={1}, dist={2}, local={3}".Args(
              (int)flags, flags, dist, m_Home.AliveName));
        }
Пример #2
0
        private void sendStatus(string status)
        {
            var obuf = new ErlOutputStream(writeVersion: false, writePktSize: false, capacity: 19);
              obuf.Write2BE(status.Length + 1);
              obuf.Write1(CHALLENGE_STATUS);
              obuf.Write(Encoding.ASCII.GetBytes(status));

              obuf.WriteTo(m_Transport.GetStream());

              m_Home.OnTrace(ErlTraceLevel.Handshake, Direction.Outbound, () =>
              "sendStatus(status={0}, local={1}".Args(status, m_Home.NodeName.Value));
        }
Пример #3
0
        private void sendChallengeAck(byte[] digest)
        {
            var obuf = new ErlOutputStream(writeVersion: false, writePktSize: false, capacity: 19);
              obuf.Write2BE(17);
              obuf.Write1(CHALLENGE_ACK);
              obuf.Write(digest);

              obuf.WriteTo(m_Transport.GetStream());

              m_Home.OnTrace(ErlTraceLevel.Handshake, Direction.Outbound, () =>
              "sendChallengeAck(digest={1}, local={1}".Args(
              hex(digest), m_Home.NodeName.Value));
        }
Пример #4
0
        private void sendChallengeReply(int challenge, byte[] digest)
        {
            var obuf = new ErlOutputStream(writeVersion: false, writePktSize: false, capacity: 23);
              obuf.Write2BE(21);
              obuf.Write1(CHALLENGE_REPLY);
              obuf.Write4BE(challenge);
              obuf.Write(digest);
              obuf.WriteTo(m_Transport.GetStream());

              m_Home.OnTrace(ErlTraceLevel.Handshake, Direction.Outbound, () =>
              "sendChallengeReply(challenge={0}, digest={1}, local={2}".Args(
                  challenge, hex(digest), m_Home.NodeName.Value));
        }
Пример #5
0
        // used by Send and SendReg (message types with payload)
        protected void DoSend(ErlOutputStream header, ErlOutputStream payload = null)
        {
            bool tw = ErlApp.TraceEnabled(TraceLevel, ErlTraceLevel.Wire);
              bool ts = ErlApp.TraceEnabled(TraceLevel, ErlTraceLevel.Send);

              if (tw || ts)
              {
            var h = header.InputStream(5).Read(true);

            m_Home.OnTrace(ErlTraceLevel.Wire, Direction.Outbound, () =>
            {
              var hb = header.ToBinary();
              return "{0} {1} (header_sz={2}{3})\n   Header: {4}{5}"
              .Args(HeaderType(h), h.ToString(), hb.Length,
                  payload == null ? string.Empty : ", msg_sz={0}".Args(payload.Length),
                  hb.ToBinaryString(),
                  payload == null ? string.Empty : "\n   Msg:    {0}".Args(payload.ToBinaryString()));
            });

            m_Home.OnTrace(ErlTraceLevel.Send, Direction.Outbound, () =>
            {
              var o = payload == null ? "" : payload.InputStream(0).Read(true).ToString();
              return "{0} {1} {2}".Args(HeaderType(h), h.ToString(), o);
            });
              }

              var written = (int)header.Length;

              lock (m_Transport)
              {
            try
            {
              header.WriteTo(m_Transport.GetStream());
              if (payload != null)
              {
            written += (int)payload.Length;
            payload.WriteTo(m_Transport.GetStream());
              }

              m_SentBytes += written;
              m_SentMsgs++;
            }
            catch (Exception e)
            {
              Close();
              throw e;
            }
              }

              onReadWrite(Direction.Outbound, written, m_SentBytes, m_SentMsgs);
        }
Пример #6
0
        private void sendBuf(ErlTuple ctrl, ErlOutputStream payload = null)
        {
            if (!m_Connected)
            throw new ErlException(StringConsts.ERL_CONN_NOT_CONNECTED_ERROR);

              var header = new ErlOutputStream(writeVersion: false, capacity: HEADER_LEN);

              // preamble: 4 byte length + "PASS_THROUGH" tag + version
              header.Write4BE(0); // reserve space for length
              header.Write1(PASS_THROUGH);
              header.Write1((byte)ErlExternalTag.Version);

              // header info
              header.WriteTuple(ctrl);

              // version for payload
              //header.Write1((byte)ErlExternalTag.Version); // Note that it's already written in the payload

              if (payload == null)
              {
            header.Poke4BE(0, (int)(header.Length - 4));
            DoSend(header);
              }
              else
              {
            header.Poke4BE(0, (int)(header.Length + payload.Length - 4));
            DoSend(header, payload);
              }
        }
Пример #7
0
        public void ErlTermSerializeTest()
        {
            {
            var b = new byte[] { 131, 100, 0, 3, 97, 98, 99 };
            var t = new ErlAtom("abc");
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 109, 0, 0, 0, 3, 1, 2, 3 };
            var t = new ErlBinary(new byte[] { 1, 2, 3 });
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b1 = new byte[] { 131, 100, 0, 4, 116, 114, 117, 101 };
            var t1 = new ErlBoolean(true);
            var os1 = new ErlOutputStream(t1);
            Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
            var es1 = new ErlInputStream(b1);
            Assert.AreEqual(t1, es1.Read());

            var b2 = new byte[] { 131, 100, 0, 5, 102, 97, 108, 115, 101 };
            var t2 = new ErlBoolean(false);
            var os2 = new ErlOutputStream(t2);
            Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
            var es2 = new ErlInputStream(b2);
            Assert.AreEqual(t2, es2.Read());
              }
              {
            var b = new byte[] { 131, 97, 127 };
            var t = new ErlByte(127);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 70, 64, 36, 62, 249, 219, 34, 208, 229 };
            var t = new ErlDouble(10.123);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 108, 0, 0, 0, 2, 107, 0, 1, 1, 107, 0, 1, 2, 106 };
            var t = new ErlList(new ErlList(1), new ErlList(2));
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131,108,0,0,0,2,108,0,0,0,2,97,1,107,0,1,2,106,107,0,1,3,106 };
            var t = new ErlList(new ErlList(1, new ErlList(2)), new ErlList(3));
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131,108,0,0,0,3,97,1,70,64,36,61,112,163,215,10,61,108,0,0,0,2,
                             100,0,4,116,114,117,101,107,0,1,97,106,106 };
            var t = new ErlList(1, 10.12, new ErlList(true, "a"));
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] {
              131,108,0,0,0,3,97,23,97,4,104,1,108,0,0,0,1,104,2,109,0,0,0,5,101,118,101,
              110,116,104,1,108,0,0,0,2,104,2,109,0,0,0,10,102,108,101,101,116,95,104,97,
              115,104,109,0,0,0,36,97,54,97,50,50,100,49,52,45,56,52,56,51,45,52,49,102,99,
              45,97,52,54,98,45,50,56,51,98,57,55,55,55,99,50,97,50,104,2,109,0,0,0,4,116,
              121,112,101,109,0,0,0,13,102,108,101,101,116,95,99,104,97,110,103,101,100,
              106,106,106 };
            var t = ErlObject.Parse("[23,4,{[{<<\"event\">>,"+
                                "{[{<<\"fleet_hash\">>,<<\"a6a22d14-8483-41fc-a46b-283b9777c2a2\">>},"+
                                "{<<\"type\">>,<<\"fleet_changed\">>}]}}]}]");
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b1 = new byte[] { 131, 98, 255, 255, 255, 251 };
            var t1 = new ErlLong(-5);
            var os1 = new ErlOutputStream(t1);
            Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
            var es1 = new ErlInputStream(b1);
            Assert.AreEqual(t1, es1.Read());

            var b2 = new byte[] { 131, 97, 5 };
            var t2 = new ErlLong(5);
            var os2 = new ErlOutputStream(t2);
            Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
            var es2 = new ErlInputStream(b2);
            Assert.AreEqual(t2, es2.Read());

            var b3 = new byte[] { 131, 98, 0, 16, 0, 0 };
            var t3 = new ErlLong(1024 * 1024);
            var os3 = new ErlOutputStream(t3);
            Assert.AreEqual(b3, os3.GetBuffer().TakeWhile((_, i) => i < b3.Length).ToArray());
            var es3 = new ErlInputStream(b3);
            Assert.AreEqual(t3, es3.Read());

            var b4 = new byte[] { 131, 110, 6, 0, 0, 0, 0, 0, 0, 4 };
            var t4 = new ErlLong(1024L * 1024 * 1024 * 1024 * 4);
            var os4 = new ErlOutputStream(t4);
            Assert.AreEqual(b4, os4.GetBuffer().TakeWhile((_, i) => i < b4.Length).ToArray());
            var es4 = new ErlInputStream(b4);
            Assert.AreEqual(t4, es4.Read());

            var b5 = new byte[] { 131, 110, 8, 1, 0, 0, 0, 0, 0, 0, 0, 128 };
            var t5 = new ErlLong(1L << 63);
            var os5 = new ErlOutputStream(t5);
            Assert.AreEqual(b5, os5.GetBuffer().TakeWhile((_, i) => i < b5.Length).ToArray());
            var es5 = new ErlInputStream(b5);
            Assert.AreEqual(t5, es5.Read());

            var b6 = new byte[] { 131, 110, 8, 1, 0, 0, 0, 0, 0, 0, 0, 128 };
            var t6 = new ErlLong(-1L << 63);
            var os6 = new ErlOutputStream(t6);
            Assert.AreEqual(b6, os6.GetBuffer().TakeWhile((_, i) => i < b6.Length).ToArray());
            var es6 = new ErlInputStream(b6);
            Assert.AreEqual(t6, es6.Read());

            var b7 = new byte[] { 131, 110, 8, 0, 255, 255, 255, 255, 255, 255, 255, 255 };
            var es7 = new ErlInputStream(b7);
            var t7 = new ErlLong(-1);
            Assert.AreEqual(t7, es7.Read());
            var bi7 = new byte[] {131, 98, 255, 255, 255, 255};
            var os7 = new ErlOutputStream(t7);
            Assert.AreEqual(bi7, os7.GetBuffer().TakeWhile((_, i) => i < bi7.Length).ToArray());
              }
              {
            var b = new byte[] { 131, 103, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 0, 0, 0, 38, 0, 0, 0, 0, 1 };
            var t = new ErlPid("[email protected]", 38, 0, 1);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 102, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 0, 0, 0, 38, 1 };
            var t = new ErlPort("[email protected]", 38, 1);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 114, 0, 3, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 1, 0, 0, 0, 181, 0, 0, 0, 0, 0, 0, 0, 0 };
            var t = new ErlRef("[email protected]", 181, 0, 0, 1);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 107, 0, 3, 115, 116, 114 };
            var t = new ErlString("str");
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 104, 3, 97, 1, 100, 0, 1, 97, 104, 2, 97, 10, 70, 63, 241, 247, 206, 217, 22, 135, 43 };
            var t = new ErlTuple(1, new ErlAtom("a"), new ErlTuple(10, 1.123));
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
        }
Пример #8
0
    /// <summary>
    /// Send an auth error to peer because he sent a bad cookie
    /// The auth error uses his cookie (not revealing ours).
    /// This is just like send_reg otherwise
    /// </summary>
    private void cookieError(ErlLocalNode local, ErlAtom cookie)
    {
      var header = new ErlOutputStream(writeVersion: false, capacity: HEADER_LEN);

      // preamble: 4 byte length + "PASS_THROUGH" tag + version
      header.Write4BE(0); // reserve space for length
      header.Write1(PASS_THROUGH);
      header.Write1((byte)ErlExternalTag.Version);

      header.WriteTupleHead(4);
      header.WriteLong((long)ErlMsg.Tag.RegSend);
      header.WritePid(local.CreatePid()); // disposable pid
      header.WriteAtom(cookie); // important: his cookie, not mine...
      header.WriteAtom("auth");

      // version for payload written later by the payload stream
      //header.Write1((byte)ErlExternalTag.Version);

      // the payload

      // the no_auth message (copied from Erlang) Don't change this (Erlang will crash)
      // {$gen_cast, {print, "~n** Unauthorized cookie ~w **~n", [[email protected]]}}

      var msg = ErlObject.Parse(
          "{'$gen_cast', {print, \"\n** Unauthorized cookie ~w **\n\", [" + local.NodeName.Value + "]}}");

      var payload = new ErlOutputStream(msg, writeVersion: true);

      // fix up length in preamble
      header.Poke4BE(0, (int)(header.Length + payload.Length - 4));

      try
      {
        DoSend(header, payload);
      }
      catch (Exception e)
      {
        Close();
        throw new ErlException(StringConsts.ERL_CONN_UNAUTH_COOKIE_ERROR.Args(cookie.Value), e);
      }
    }
Пример #9
0
        /// <summary>
        /// Determine what port a node listens for incoming connections on
        /// </summary>
        /// <param name="home">Local node</param>
        /// <param name="node">Remote lode for which to look up the port number from remote EPMD</param>
        /// <param name="closeSocket">If true, close the connection to remote EPMD at return</param>
        /// <returns>the listen port for the specified node, or 0 if the node
        /// was not registered with Epmd</returns>
        public static int LookupPort(ErlLocalNode home, ErlRemoteNode node,
            bool closeSocket = false)
        {
            IErlTransport s = null;

              try
              {
            var obuf = new ErlOutputStream(writeVersion: false, capacity: 4 + 3 + node.AliveName.Length + 1);
            s = node.Epmd;

            if (s == null)
            {
              //create transport
              s = ErlTransportFactory.Create(node.TransportClassName, node.NodeName.Value);

              s.Trace += (o, t, d, msg) => home.OnTrace(t, d, msg);

              //append SSH params to transport
              node.AppendSSHParamsToTransport(s);

              //connect (I am not sure)
              s.Connect(node.Host, EPMD_PORT, node.ConnectTimeout);
            }

            // build and send epmd request
            // length[2], tag[1], alivename[n] (length = n+1)
            obuf.Write2BE(node.AliveName.Length + 1);
            obuf.Write1((byte)Indicator.Port4req);
            //UPGRADE_NOTE: This code will be optimized in the future;
            byte[] buf = Encoding.ASCII.GetBytes(node.AliveName);
            obuf.Write(buf);

            // send request
            obuf.WriteTo(s.GetStream());

            home.OnTrace(ErlTraceLevel.Epmd, Direction.Outbound,
                StringConsts.ERL_EPMD_LOOKUP_R4.Args(node.NodeName.Value));

            // receive and decode reply
            // resptag[1], result[1], port[2], ntype[1], proto[1],
            // disthigh[2], distlow[2], nlen[2], alivename[n],
            // elen[2], edata[m]
            buf = new byte[100];

            int n = s.GetStream().Read(buf, 0, buf.Length);

            if (n < 0)
              throw new ErlException(StringConsts.ERL_EPMD_NOT_RESPONDING.Args(node.Host, node.AliveName));

            var ibuf = new ErlInputStream(buf, 0, n, checkVersion: false);

            if (Indicator.Port4resp == (Indicator)ibuf.Read1())
            {
              if (0 == ibuf.Read1())
              {
            node.Port = ibuf.Read2BE();

            node.Ntype = (ErlAbstractNode.NodeType)ibuf.Read1();
            node.Proto = ibuf.Read1();
            node.DistHigh = ibuf.Read2BE();
            node.DistLow = ibuf.Read2BE();
            // ignore rest of fields
              }
            }

            if (!closeSocket)
            {
              node.Epmd = s;
              s = null;
            }
              }
              catch (Exception e)
              {
            home.OnTrace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_INVALID_RESPONSE_ERROR.Args(node.Host, node.AliveName, e.ToString()));
            throw new ErlException(StringConsts.ERL_EPMD_NOT_RESPONDING.Args(node.Host, node.AliveName, e.ToString()));
              }
              finally
              {
            if (s != null)
              try { s.Close(); } catch { }
            s = null;
              }

              home.OnTrace(ErlTraceLevel.Epmd, Direction.Inbound, () =>
              node.Port == 0 ? StringConsts.ERL_EPMD_NOT_FOUND : StringConsts.ERL_EPMD_PORT.Args(node.Port));

              return node.Port;
        }
Пример #10
0
        /// <summary>
        /// Unregister from Epmd.
        /// Other nodes wishing to connect will no longer be able to
        /// </summary>
        public static void UnPublishPort(ErlLocalNode node)
        {
            IErlTransport s = null;

              try
              {
            s = node.Epmd ?? new ErlTcpTransport(ErlLocalNode.LocalHost, EPMD_PORT) { NodeName = node.NodeName.Value };
            ErlOutputStream obuf = new ErlOutputStream(
            writeVersion: false, capacity: node.AliveName.Length + 8);
            obuf.Write2BE(node.AliveName.Length + 1);
            obuf.Write1((byte)Indicator.StopReq);
            var buf = Encoding.ASCII.GetBytes(node.AliveName);
            obuf.Write(buf);
            obuf.WriteTo(s.GetStream());

            node.OnTrace(ErlTraceLevel.Epmd, Direction.Outbound, () =>
                StringConsts.ERL_EPMD_UNPUBLISH.Args(node.NodeName.Value, node.Port));
              }
              catch
              {
            s = null;
              }
              finally
              {
            if (s != null)
              try { s.Close(); } catch { }
            node.Epmd = null;
              }
        }
Пример #11
0
        /// <summary>
        /// Publish node's port at local EPMD, so that other nodes can connect to it.
        ///
        /// On failure to connect to EPMD the function may throw if the value of
        /// ErlApp.IgnoreLocalEpmdConnectErrors variable is true.
        ///
        /// On failed connection attempt the function calls
        /// node.OnEpmdFailedConnectAttempt delegate
        /// </summary>
        /// <remarks>
        /// This function will get an exception if it tries to talk to an r3
        /// epmd, or if something else happens that it cannot forsee. In both
        /// cases we return an exception (and the caller should try again, using
        /// the r3 protocol).
        ///
        /// If we manage to successfully communicate with an r4 epmd, we return
        /// either the socket, or null, depending on the result
        /// </remarks>
        public static bool PublishPort(ErlLocalNode node)
        {
            Exception error = null;
              IErlTransport s = null;

              try
              {
            ErlOutputStream obuf = new ErlOutputStream(writeVersion: false, capacity: node.AliveName.Length + 20);
            s = node.Epmd ?? new ErlTcpTransport(ErlLocalNode.LocalHost, EPMD_PORT) { NodeName = node.NodeName.Value };

            obuf.Write2BE(node.AliveName.Length + 13);

            obuf.Write1((byte)Indicator.Publish4req);
            obuf.Write2BE(node.Port);

            obuf.Write1((byte)node.Ntype);

            obuf.Write1((byte)node.Proto);
            obuf.Write2BE(node.DistHigh);
            obuf.Write2BE(node.DistLow);

            obuf.Write2BE(node.AliveName.Length);
            var buf = Encoding.ASCII.GetBytes(node.AliveName);
            //UPGRADE_NOTE: This code will be optimized in the future;
            obuf.Write(buf);
            obuf.Write2BE(0); // No extra

            // send request
            obuf.WriteTo(s.GetStream());

            node.OnTrace(ErlTraceLevel.Epmd, Direction.Outbound, () =>
                StringConsts.ERL_EPMD_PUBLISH.Args(node.AliveName, node.Port, "r4"));

            // get reply
            buf = new byte[100];
            int n = s.GetStream().Read(buf, 0, buf.Length);

            if (n < 0)
              // this was an r3 node => not a failure (yet)
              throw new ErlException(StringConsts.ERL_EPMD_NOT_RESPONDING.Args(node.Host, node.AliveName));

            ErlInputStream ibuf = new ErlInputStream(buf, 0, n, checkVersion: false);

            if (Indicator.Publish4resp == (Indicator)ibuf.Read1())
            {
              int result = ibuf.Read1();
              if (result == 0)
              {
            node.Creation = (byte)ibuf.Read2BE();
            node.OnTrace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_OK);

            node.Epmd = s;
            return true; // success
              }
            }
            error = new ErlException("Cannot register node '{0}' (not unique?)".Args(node.AliveName));
              }
              catch (Exception e)
              {
            error = e;
              }

              // epmd closed the connection = fail
              if (s != null)
            try { s.Close(); } catch { }

              node.Epmd = null;

              node.OnTrace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_NO_RESPONSE);

              node.OnEpmdFailedConnectAttempt(node.NodeName, error.Message);

              node.OnTrace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_FAILED_TO_CONNECT_ERROR);

              if (!ErlApp.IgnoreLocalEpmdConnectErrors)
            throw new ErlException(StringConsts.ERL_EPMD_FAILED_TO_CONNECT_ERROR + ": " + error.Message);

              node.Creation = 0;
              return false;
        }
Пример #12
0
    public void ErlTermSerializeTest()
    {
      {
        var b = new byte[] { 131, 100, 0, 3, 97, 98, 99 };
        var t = new ErlAtom("abc");
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 109, 0, 0, 0, 3, 1, 2, 3 };
        var t = new ErlBinary(new byte[] { 1, 2, 3 });
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b1 = new byte[] { 131, 100, 0, 4, 116, 114, 117, 101 };
        var t1 = new ErlBoolean(true);
        var os1 = new ErlOutputStream(t1);
        Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
        var es1 = new ErlInputStream(b1);
        Assert.AreEqual(t1, es1.Read());

        var b2 = new byte[] { 131, 100, 0, 5, 102, 97, 108, 115, 101 };
        var t2 = new ErlBoolean(false);
        var os2 = new ErlOutputStream(t2);
        Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
        var es2 = new ErlInputStream(b2);
        Assert.AreEqual(t2, es2.Read());
      }
      {
        var b = new byte[] { 131, 97, 127 };
        var t = new ErlByte(127);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 70, 64, 36, 62, 249, 219, 34, 208, 229 };
        var t = new ErlDouble(10.123);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131,108,0,0,0,3,97,1,70,64,36,61,112,163,215,10,61,108,0,0,0,2,
                             100,0,4,116,114,117,101,107,0,1,97,106,106 };
        var t = new ErlList(1, 10.12, new ErlList(true, "a"));
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b1 = new byte[] { 131, 98, 255, 255, 255, 251 };
        var t1 = new ErlLong(-5);
        var os1 = new ErlOutputStream(t1);
        Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
        var es1 = new ErlInputStream(b1);
        Assert.AreEqual(t1, es1.Read());

        var b2 = new byte[] { 131, 97, 5 };
        var t2 = new ErlLong(5);
        var os2 = new ErlOutputStream(t2);
        Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
        var es2 = new ErlInputStream(b2);
        Assert.AreEqual(t2, es2.Read());

        var b3 = new byte[] { 131, 98, 0, 16, 0, 0 };
        var t3 = new ErlLong(1024 * 1024);
        var os3 = new ErlOutputStream(t3);
        Assert.AreEqual(b3, os3.GetBuffer().TakeWhile((_, i) => i < b3.Length).ToArray());
        var es3 = new ErlInputStream(b3);
        Assert.AreEqual(t3, es3.Read());

        var b4 = new byte[] { 131, 110, 6, 0, 0, 0, 0, 0, 0, 4 };
        var t4 = new ErlLong(1024L * 1024 * 1024 * 1024 * 4);
        var os4 = new ErlOutputStream(t4);
        Assert.AreEqual(b4, os4.GetBuffer().TakeWhile((_, i) => i < b4.Length).ToArray());
        var es4 = new ErlInputStream(b4);
        Assert.AreEqual(t4, es4.Read());

        var b5 = new byte[] { 131, 110, 8, 0, 0, 0, 0, 0, 0, 0, 0, 128 };
        var t5 = new ErlLong(1L << 63);
        var os5 = new ErlOutputStream(t5);
        Assert.AreEqual(b5, os5.GetBuffer().TakeWhile((_, i) => i < b5.Length).ToArray());
        var es5 = new ErlInputStream(b5);
        Assert.AreEqual(t5, es5.Read());

        var b6 = new byte[] { 131, 110, 8, 0, 0, 0, 0, 0, 0, 0, 0, 128 };
        var t6 = new ErlLong(-1L << 63);
        var os6 = new ErlOutputStream(t6);
        Assert.AreEqual(b6, os6.GetBuffer().TakeWhile((_, i) => i < b6.Length).ToArray());
        var es6 = new ErlInputStream(b6);
        Assert.AreEqual(t6, es6.Read());

        var b7 = new byte[] { 131, 110, 8, 0, 255, 255, 255, 255, 255, 255, 255, 255 };
        var es7 = new ErlInputStream(b7);
        var t7 = new ErlLong(-1);
        Assert.AreEqual(t7, es7.Read());
        var bi7 = new byte[] {131, 98, 255, 255, 255, 255};
        var os7 = new ErlOutputStream(t7);
        Assert.AreEqual(bi7, os7.GetBuffer().TakeWhile((_, i) => i < bi7.Length).ToArray());
      }
      {
        var b = new byte[] { 131, 103, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 0, 0, 0, 38, 0, 0, 0, 0, 1 };
        var t = new ErlPid("[email protected]", 38, 0, 1);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 102, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 0, 0, 0, 38, 1 };
        var t = new ErlPort("[email protected]", 38, 1);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 114, 0, 3, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 1, 0, 0, 0, 181, 0, 0, 0, 0, 0, 0, 0, 0 };
        var t = new ErlRef("[email protected]", 181, 0, 0, 1);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 107, 0, 3, 115, 116, 114 };
        var t = new ErlString("str");
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 104, 3, 97, 1, 100, 0, 1, 97, 104, 2, 97, 10, 70, 63, 241, 247, 206, 217, 22, 135, 43 };
        var t = new ErlTuple(1, new ErlAtom("a"), new ErlTuple(10, 1.123));
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
    }
Пример #13
0
    private void sendChallenge(int dist, ErlAbstractNode.NodeCompatibility flags, int challenge)
    {
      ErlAtom str = m_Home.NodeName;
      var obuf = new ErlOutputStream(writeVersion: false, writePktSize: false, capacity: 11 + str.Length);
      obuf.Write2BE((short)str.Length + 11); // 11 bytes + nodename
      obuf.Write1((byte)ErlAbstractNode.NodeType.Ntype_R6);
      obuf.Write2BE((short)dist);
      obuf.Write4BE((int)flags);
      obuf.Write4BE(challenge);
      obuf.Write(Encoding.ASCII.GetBytes(str.Value));

      obuf.WriteTo(m_TcpClient.GetStream());

      m_Home.Trace(ErlTraceLevel.Handshake, Direction.Outbound, () =>
          "sendChallenge(flags({0:X2})={1}, dist={2}, challenge={3}, local={4}".Args(
              (int)flags, flags, dist, challenge, m_Home.AliveName));
    }