Пример #1
0
        // Ask epmd to close his end of the connection.
        // Caller should close his epmd socket as well.
        // This method is pretty forgiving...

        /*
         * Unregister from Epmd. Other nodes wishing to connect will no
         * longer be able to.
         *
         * <p> This method does not report any failures.
         **/
        public static void  unPublishPort(OtpLocalNode node)
        {
            TcpClient s = null;

            try
            {
                s = new TcpClient(System.Net.Dns.GetHostName(), epmdPort);
                OtpOutputStream obuf = new OtpOutputStream();
                obuf.write2BE(node.getAlive().Length + 1);
                obuf.write1(stopReq);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int    i;
                string tmpStr;
                tmpStr   = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i        = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte)tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);
                obuf.writeTo((System.IO.Stream)s.GetStream());
                // don't even wait for a response (is there one?)
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("-> UNPUBLISH " + node + " port=" + node.port());
                    OtpTrace.TraceEvent("<- OK (assumed)");
                }
            }
            catch (System.Exception)
            {
                /*ignore all failures */
            }
            finally
            {
                try
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                }
                catch (System.IO.IOException)
                {
                    /*ignore close failure */
                }
                s = null;
            }
        }
Пример #2
0
 /*
 * Send a pre-encoded message to a process on a remote node.
 *
 * @param dest the Erlang PID of the remote process.
 * @param msg the encoded message to send.
 *
 * @exception C#.io.IOException if the connection is not active
 * or a communication error occurs.
 **/
 protected internal virtual void  sendBuf(Erlang.Pid from, Erlang.Pid dest, OtpOutputStream payload)
 {
     if (!connected)
     {
         throw new System.IO.IOException("Not connected");
     }
     OtpOutputStream header = new OtpOutputStream(headerLen);
     
     // preamble: 4 byte length + "passthrough" tag + version
     header.write4BE(0); // reserve space for length
     header.write1(passThrough);
     header.write1(version);
     
     // header info
     header.write_tuple_head(3);
     header.write_long((long)OtpMsg.Tag.sendTag);
     if (sendCookie)
         header.write_atom(auth_cookie);
     else
         header.write_atom("");
     header.write_any(dest);
     
     // version for payload
     header.write1(version);
     
     // fix up length in preamble
     header.poke4BE(0, header.count() + payload.count() - 4);
     
     do_send(header, payload);
 }
Пример #3
0
		private static System.Net.Sockets.TcpClient r3_publish(OtpLocalNode node)
		{
			System.Net.Sockets.TcpClient s = null;
			
			try
			{
				OtpOutputStream obuf = new OtpOutputStream();
				s = new System.Net.Sockets.TcpClient(System.Net.Dns.GetHostName(), epmdPort);
				
				obuf.write2BE(node.getAlive().Length + 3);
				
				obuf.write1(publish3req);
				obuf.write2BE(node.port());
				//UPGRADE_NOTE: This code will be optimized in the future;
				byte[] tmpBytes;
				int i;
				string tmpStr;
				tmpStr = node.getAlive();
				tmpBytes = new byte[tmpStr.Length];
				i = 0;
				while (i < tmpStr.Length)
				{
					tmpBytes[i] = (byte) tmpStr[i];
					i++;
				}
				obuf.writeN(tmpBytes);
				
				// send request
				obuf.writeTo((System.IO.Stream) s.GetStream());
				if (traceLevel >= traceThreshold)
					OtpTrace.TraceEvent("-> PUBLISH (r3) " + node + " port=" + node.port());
				
				byte[] tmpbuf = new byte[100];

				int n = s.GetStream().Read(tmpbuf, 0, 100);
				
				if (n < 0)
				{
					if (s != null)
						s.Close();
					if (traceLevel >= traceThreshold)
						OtpTrace.TraceEvent("<- (no response)");
					return null;
				}
				
				OtpInputStream ibuf = new OtpInputStream(tmpbuf);
				
				if (ibuf.read1() == publish3ok)
				{
					node._creation = ibuf.read2BE();
					if (traceLevel >= traceThreshold)
						OtpTrace.TraceEvent("<- OK");
					return s; // success - don't close socket
				}
			}
			catch (System.IO.IOException)
			{
				// epmd closed the connection = fail
				if (s != null)
					s.Close();
				if (traceLevel >= traceThreshold)
					OtpTrace.TraceEvent("<- (no response)");
				throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
			}
			catch (Erlang.Exception)
			{
				if (s != null)
					s.Close();
				if (traceLevel >= traceThreshold)
					OtpTrace.TraceEvent("<- (invalid response)");
				throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
			}
			
			if (s != null)
				s.Close();
			return null; // failure
		}
Пример #4
0
 /*
  * Send a pre-encoded message to a named process on a remote node.
  *
  * @param dest the name of the remote process.
  * @param payload the encoded message to send.
  *
  * @exception C#.io.IOException if the connection is not active or
  * a communication error occurs.
  **/
 public virtual void  sendBuf(System.String dest, OtpOutputStream payload)
 {
     base.sendBuf(this._self.pid(), dest, payload);
 }
Пример #5
0
        private static System.Net.Sockets.TcpClient r3_publish(OtpLocalNode node)
        {
            System.Net.Sockets.TcpClient s = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new System.Net.Sockets.TcpClient(System.Net.Dns.GetHostName(), epmdPort);

                obuf.write2BE(node.getAlive().Length + 3);

                obuf.write1(publish3req);
                obuf.write2BE(node.port());
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int    i;
                string tmpStr;
                tmpStr   = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i        = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte)tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);

                // send request
                obuf.writeTo((System.IO.Stream)s.GetStream());
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("-> PUBLISH (r3) " + node + " port=" + node.port());
                }

                byte[] tmpbuf = new byte[100];

                int n = s.GetStream().Read(tmpbuf, 0, 100);

                if (n < 0)
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                    if (traceLevel >= traceThreshold)
                    {
                        OtpTrace.TraceEvent("<- (no response)");
                    }
                    return(null);
                }

                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                if (ibuf.read1() == publish3ok)
                {
                    node._creation = ibuf.read2BE();
                    if (traceLevel >= traceThreshold)
                    {
                        OtpTrace.TraceEvent("<- OK");
                    }
                    return(s);                    // success - don't close socket
                }
            }
            catch (System.IO.IOException)
            {
                // epmd closed the connection = fail
                if (s != null)
                {
                    s.Close();
                }
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (no response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
            }
            catch (Erlang.Exception)
            {
                if (s != null)
                {
                    s.Close();
                }
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (invalid response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
            }

            if (s != null)
            {
                s.Close();
            }
            return(null);            // failure
        }
Пример #6
0
        private static int r3_lookupPort(AbstractNode node)
        {
            int port = 0;

            System.Net.Sockets.TcpClient s = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new System.Net.Sockets.TcpClient(node.host(), epmdPort);

                // build and send epmd request
                // length[2], tag[1], alivename[n] (length = n+1)
                obuf.write2BE(node.getAlive().Length + 1);
                obuf.write1(port3req);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int    i;
                string tmpStr;
                tmpStr   = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i        = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte)tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);

                // send request
                obuf.writeTo((System.IO.Stream)s.GetStream());

                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("-> LOOKUP (r3) " + node);
                }

                // receive and decode reply
                byte[] tmpbuf = new byte[100];

                s.GetStream().Read(tmpbuf, 0, 100);
                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                port = ibuf.read2BE();
            }
            catch (System.IO.IOException)
            {
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (no response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            catch (Erlang.Exception)
            {
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (invalid response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            finally
            {
                try
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                }
                catch (System.IO.IOException)
                {
                    /*ignore close errors */
                }
                s = null;
            }

            if (traceLevel >= traceThreshold)
            {
                if (port == 0)
                {
                    OtpTrace.TraceEvent("<- NOT FOUND");
                }
                else
                {
                    OtpTrace.TraceEvent("<- PORT " + port);
                }
            }
            return(port);
        }
Пример #7
0
 // used by the other message types
 protected internal virtual void  do_send(OtpOutputStream header)
 {
     lock(this)
     {
         try
         {
             if (traceLevel >= OtpTrace.Type.ctrlThreshold)
             {
                 try
                 {
                     Erlang.Object h = (header.getOtpInputStream(5)).read_any();
                     OtpTrace.TraceEvent("-> " + headerType(h) + " " + h);
                 }
                 catch (Erlang.Exception e)
                 {
                     OtpTrace.TraceEvent("   " + "can't decode output buffer: " + e);
                 }
             }
             header.writeTo((System.IO.Stream) socket.GetStream());
         }
         catch (System.Net.Sockets.SocketException e)
         {
             close();
             throw e;
         }
         catch (System.IO.IOException e)
         {
             close();
             throw e;
         }
     }
 }
Пример #8
0
 private void  sendExit(int tag, Erlang.Pid from, Erlang.Pid dest, System.String reason)
 {
     if (!connected)
     {
         throw new System.IO.IOException("Not connected");
     }
     OtpOutputStream header = new OtpOutputStream(headerLen);
     
     // preamble: 4 byte length + "passthrough" tag
     header.write4BE(0); // reserve space for length
     header.write1(passThrough);
     header.write1(version);
     
     // header
     header.write_tuple_head(4);
     header.write_long(tag);
     header.write_any(from);
     header.write_any(dest);
     header.write_string(reason);
     
     // fix up length in preamble
     header.poke4BE(0, header.count() - 4);
     
     do_send(header);
 }
Пример #9
0
        /*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.
        */
        private static System.Net.Sockets.TcpClient r4_publish(OtpLocalNode node)
        {
            System.Net.Sockets.TcpClient s = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new TcpClient(System.Net.Dns.GetHostName(), epmdPort);

                obuf.write2BE(node.getAlive().Length + 13);

                obuf.write1(publish4req);
                obuf.write2BE(node.port());

                obuf.write1(node.type());

                obuf.write1(node.proto());
                obuf.write2BE(node.distHigh());
                obuf.write2BE(node.distLow());

                obuf.write2BE(node.getAlive().Length);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int i;
                string tmpStr;
                tmpStr = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte) tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);
                obuf.write2BE(0); // No extra

                // send request
                obuf.writeTo((System.IO.Stream) s.GetStream());

                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("-> PUBLISH (r4) " + node + " port=" + node.port());

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

                if (n < 0)
                {
                    // this was an r3 node => not a failure (yet)
                    if (s != null)
                        s.Close();
                    throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
                }

                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                int response = ibuf.read1();
                if (response == publish4resp)
                {
                    int result = ibuf.read1();
                    if (result == 0)
                    {
                        node._creation = ibuf.read2BE();
                        if (traceLevel >= traceThreshold)
                            System.Console.Out.WriteLine("<- OK");
                        return s; // success
                    }
                }
            }
            catch (System.IO.IOException)
            {
                // epmd closed the connection = fail
                if (s != null)
                    s.Close();
                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("<- (no response)");
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
            }
            catch (Erlang.DecodeException)
            {
                if (s != null)
                    s.Close();
                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("<- (invalid response)");
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
            }

            if (s != null)
                s.Close();
            return null;
        }
Пример #10
0
        private static int r4_lookupPort(AbstractNode node)
        {
            int port = 0;
            System.Net.Sockets.TcpClient s = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new System.Net.Sockets.TcpClient(node.host(), epmdPort);

                // build and send epmd request
                // length[2], tag[1], alivename[n] (length = n+1)
                obuf.write2BE(node.getAlive().Length + 1);
                obuf.write1(port4req);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int i;
                string tmpStr;
                tmpStr = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte) tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);

                // send request
                obuf.writeTo((System.IO.Stream) s.GetStream());

                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("-> LOOKUP (r4) " + node);

                // 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]
                byte[] tmpbuf = new byte[100];

                int n = s.GetStream().Read(tmpbuf, 0, 100);

                if (n < 0)
                {
                    // this was an r3 node => not a failure (yet)
                    if (s != null)
                        s.Close();
                    throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
                }

                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                int response = ibuf.read1();
                if (response == port4resp)
                {
                    int result = ibuf.read1();
                    if (result == 0)
                    {
                        port = ibuf.read2BE();

                        node.ntype = ibuf.read1();
                        node._proto = ibuf.read1();
                        node._distHigh = ibuf.read2BE();
                        node._distLow = ibuf.read2BE();
                        // ignore rest of fields
                    }
                }
            }
            catch (System.IO.IOException)
            {
                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("<- (no response)");
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            catch (Erlang.DecodeException)
            {
                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("<- (invalid response)");
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            finally
            {
                try
                {
                    if (s != null)
                        s.Close();
                }
                catch (System.IO.IOException)
                {
                    /*ignore close errors */
                }
                s = null;
            }

            if (traceLevel >= traceThreshold)
            {
                if (port == 0)
                    System.Console.Out.WriteLine("<- NOT FOUND");
                else
                    System.Console.Out.WriteLine("<- PORT " + port);
            }
            return port;
        }
Пример #11
0
        private static int r3_lookupPort(AbstractNode node)
        {
            int port = 0;
            System.Net.Sockets.TcpClient s = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new System.Net.Sockets.TcpClient(node.host(), epmdPort);

                // build and send epmd request
                // length[2], tag[1], alivename[n] (length = n+1)
                obuf.write2BE(node.getAlive().Length + 1);
                obuf.write1(port3req);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int i;
                string tmpStr;
                tmpStr = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte) tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);

                // send request
                obuf.writeTo((System.IO.Stream) s.GetStream());

                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("-> LOOKUP (r3) " + node);

                // receive and decode reply
                byte[] tmpbuf = new byte[100];

                s.GetStream().Read(tmpbuf, 0, 100);
                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                port = ibuf.read2BE();
            }
            catch (System.IO.IOException)
            {
                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("<- (no response)");
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            catch (Erlang.DecodeException)
            {
                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("<- (invalid response)");
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            finally
            {
                try
                {
                    if (s != null)
                        s.Close();
                }
                catch (System.IO.IOException)
                {
                    /*ignore close errors */
                }
                s = null;
            }

            if (traceLevel >= traceThreshold)
            {
                if (port == 0)
                    System.Console.Out.WriteLine("<- NOT FOUND");
                else
                    System.Console.Out.WriteLine("<- PORT " + port);
            }
            return port;
        }
Пример #12
0
        // Ask epmd to close his end of the connection.
        // Caller should close his epmd socket as well.
        // This method is pretty forgiving...
        /*
        * Unregister from Epmd. Other nodes wishing to connect will no
        * longer be able to.
        *
        * <p> This method does not report any failures.
        **/
        public static void unPublishPort(OtpLocalNode node)
        {
            TcpClient s = null;

            try
            {
                s = new TcpClient(System.Net.Dns.GetHostName(), epmdPort);
                OtpOutputStream obuf = new OtpOutputStream();
                obuf.write2BE(node.getAlive().Length + 1);
                obuf.write1(stopReq);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int i;
                string tmpStr;
                tmpStr = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte) tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);
                obuf.writeTo((System.IO.Stream) s.GetStream());
                // don't even wait for a response (is there one?)
                if (traceLevel >= traceThreshold)
                {
                    System.Console.Out.WriteLine("-> UNPUBLISH " + node + " port=" + node.port());
                    System.Console.Out.WriteLine("<- OK (assumed)");
                }
            }
            catch (System.Exception)
            {
                /*ignore all failures */
            }
            finally
            {
                try
                {
                    if (s != null)
                        s.Close();
                }
                catch (System.IO.IOException)
                {
                    /*ignore close failure */
                }
                s = null;
            }
        }
Пример #13
0
 // used by the other message types
 protected internal virtual void do_send(OtpOutputStream header)
 {
     lock(this)
     {
         try
         {
             if (traceLevel >= ctrlThreshold)
             {
                 try
                 {
                     Erlang.Object h = (header.getOtpInputStream(5)).read_any();
                     System.Console.Out.WriteLine("-> " + headerType(h) + " " + h);
                 }
                 catch (Erlang.DecodeException e)
                 {
                     System.Console.Out.WriteLine("   " + "can't decode output buffer: " + e);
                 }
             }
             header.writeTo((System.IO.Stream) socket.GetStream());
         }
         catch (System.IO.IOException e)
         {
             close();
             throw e;
         }
     }
 }
Пример #14
0
        protected internal virtual void sendChallengeReply(int challenge, byte[] digest)
        {
            OtpOutputStream obuf = new OtpOutputStream();
            obuf.write2BE(21);
            obuf.write1(ChallengeReply);
            obuf.write4BE(challenge);
            obuf.write(digest);
            obuf.writeTo((System.IO.Stream) socket.GetStream());

            if (traceLevel >= handshakeThreshold)
            {
                System.Console.Out.WriteLine("-> " + "HANDSHAKE sendChallengeReply" + " challenge=" + challenge + " digest=" + hex(digest) + " local=" + self);
            }
        }
Пример #15
0
 /*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
 */
 private void  cookieError(OtpLocalNode local, Erlang.Atom cookie)
 {
     try
     {
         OtpOutputStream header = new OtpOutputStream(headerLen);
         
         // preamble: 4 byte length + "passthrough" tag + version
         header.write4BE(0); // reserve space for length
         header.write1(passThrough);
         header.write1(version);
         
         header.write_tuple_head(4);
         header.write_long((long)OtpMsg.Tag.regSendTag);
         header.write_any(local.createPid()); // disposable pid
         header.write_atom(cookie.atomValue()); // important: his cookie, not mine...
         header.write_atom("auth");
         
         // version for payload
         header.write1(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", [foo@aule]}}
         Erlang.Object[] msg = new Erlang.Object[2];
         Erlang.Object[] msgbody = new Erlang.Object[3];
         
         msgbody[0] = new Erlang.Atom("print");
         msgbody[1] = new Erlang.String("~n** Bad cookie sent to " + local + " **~n");
         // Erlang will crash and burn if there is no third argument here...
         msgbody[2] = new Erlang.List(); // empty list
         
         msg[0] = new Erlang.Atom("$gen_cast");
         msg[1] = new Erlang.Tuple(msgbody);
         
         OtpOutputStream payload = new OtpOutputStream(new Erlang.Tuple(msg));
         
         // fix up length in preamble
         header.poke4BE(0, header.count() + payload.count() - 4);
         
         try
         {
             do_send(header, payload);
         }
         catch (System.IO.IOException)
         {
         } // ignore
     }
     finally
     {
         close();
         throw new OtpAuthException("Remote cookie not authorized: " + cookie.atomValue());
     }
 }
Пример #16
0
 /*
 * Remove a link between the local node and the specified process on
 * the remote node. This method deactivates links created with
 * {@link #sendLink link()}.
 *
 * @param dest the Erlang PID of the remote process.
 *
 * @exception C#.io.IOException if the connection is not active or
 * a communication error occurs.
 **/
 protected internal virtual void  sendUnlink(Erlang.Pid from, Erlang.Pid dest)
 {
     if (!connected)
     {
         throw new System.IO.IOException("Not connected");
     }
     OtpOutputStream header = new OtpOutputStream(headerLen);
     
     // preamble: 4 byte length + "passthrough" tag
     header.write4BE(0); // reserve space for length
     header.write1(passThrough);
     header.write1(version);
     
     // header
     header.write_tuple_head(3);
     header.write_long((long)OtpMsg.Tag.unlinkTag);
     header.write_any(from);
     header.write_any(dest);
     
     // fix up length in preamble
     header.poke4BE(0, header.count() - 4);
     
     do_send(header);
 }
Пример #17
0
		/*
		* Send a pre-encoded message to a named process on a remote node.
		*
		* @param dest the name of the remote process.
		* @param payload the encoded message to send.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		**/
		public virtual void  sendBuf(System.String dest, OtpOutputStream payload)
		{
			base.sendBuf(this._self.pid(), dest, payload);
		}
Пример #18
0
 // used by  send and send_reg (message types with payload)
 protected internal virtual void  do_send(OtpOutputStream header, OtpOutputStream payload)
 {
     lock(this)
     {
         try
         {
             if (traceLevel >= OtpTrace.Type.sendThreshold)
             {
                 // Need to decode header and output buffer to show trace message!
                 // First make OtpInputStream, then decode.
                 try
                 {
                     Erlang.Object h = (header.getOtpInputStream(5)).read_any();
                     OtpTrace.TraceEvent("-> " + headerType(h) + " " + h.ToString());
                     
                     Erlang.Object o = (payload.getOtpInputStream(0)).read_any();
                     OtpTrace.TraceEvent("   " + o.ToString());
                     o = null;
                 }
                 catch (Erlang.Exception e)
                 {
                     OtpTrace.TraceEvent("   " + "can't decode output buffer:" + e);
                 }
             }
             
             header.writeTo((System.IO.Stream) socket.GetStream());
             payload.writeTo((System.IO.Stream) socket.GetStream());
         }
         catch (System.Net.Sockets.SocketException e)
         {
             close();
             throw e;
         }
         catch (System.IO.IOException e)
         {
             close();
             throw e;
         }
     }
 }
Пример #19
0
		/*
		* Send a pre-encoded message to a process on a remote node.
		*
		* @param dest the Erlang PID of the remote process.
		* @param msg the encoded message to send.
		*
		* @exception C#.io.IOException if the connection is not active
		* or a communication error occurs.
		**/
		public virtual void  sendBuf(Erlang.Pid dest, OtpOutputStream payload)
		{
			base.sendBuf(this._self.pid(), dest, payload);
		}
Пример #20
0
        // used by  send and send_reg (message types with payload)
        protected internal virtual void  do_send(OtpOutputStream header, OtpOutputStream payload)
        {
            lock(this)
            {
                try
                {
                    if (traceLevel >= OtpTrace.Type.sendThreshold)
                    {
                        // Need to decode header and output buffer to show trace message!
                        // First make OtpInputStream, then decode.
                        try
                        {
                            if (traceLevel >= OtpTrace.Type.wireThreshold)
                            {
                                Erlang.Object h = (header.getOtpInputStream(5)).read_any();
                                Erlang.Binary hb = header.ToBinary();
                                Erlang.Binary ob = payload.ToBinary();
                                System.Text.StringBuilder s = new System.Text.StringBuilder();
                                s.AppendFormat("-> {0} {1} (header_sz={2}, msg_sz={3})\n" +
                                               "   Header: {4}\n" +
                                               "   Msg:    {5}", headerType(h), h.ToString(), hb.size(), ob.size(),
                                                                 hb.ToBinaryString(), ob.ToBinaryString());
                                OtpTrace.TraceEvent(s.ToString());
                                h  = null;
                                hb = null;
                                ob = null;
                            }
                            else
                            {
                                Erlang.Object h = (header.getOtpInputStream(5)).read_any();
                                OtpTrace.TraceEvent("-> " + headerType(h) + " " + h.ToString());
                                Erlang.Object o = (payload.getOtpInputStream(0)).read_any();
                                OtpTrace.TraceEvent("   " + o.ToString());
                                h = null;
                                o = null;
                            }
                        }
                        catch (Erlang.Exception e)
                        {
                            OtpTrace.TraceEvent("   " + "can't decode output buffer:" + e);
                        }
                    }
                    
                    header.writeTo((System.IO.Stream) socket.GetStream());
                    payload.writeTo((System.IO.Stream) socket.GetStream());

                    long written = header.count() + payload.count();
                    sentBytes += written;
                    sentMsgs++;
                    if (onReadWrite != null)
                        onReadWrite(this, Operation.Write, written, sentBytes, sentMsgs);
                }
                catch (System.Exception e)
                {
                    close();
                    throw e;
                }
            }
        }
Пример #21
0
 protected internal virtual void  sendChallenge(int dist, int flags, int challenge)
 {
     
     OtpOutputStream obuf = new OtpOutputStream();
     System.String str = self.node();
     obuf.write2BE(str.Length + 11); // 11 bytes + nodename
     obuf.write1(AbstractNode.NTYPE_R6);
     obuf.write2BE(dist);
     obuf.write4BE(flags);
     obuf.write4BE(challenge);
     //UPGRADE_NOTE: This code will be optimized in the future;
     byte[] tmpBytes;
     int i;
     string tmpStr;
     tmpStr = str;
     tmpBytes = new byte[tmpStr.Length];
     i = 0;
     while (i < tmpStr.Length)
     {
         tmpBytes[i] = (byte) tmpStr[i];
         i++;
     }
     obuf.write(tmpBytes);
     
     obuf.writeTo((System.IO.Stream) socket.GetStream());
     
     if (traceLevel >= OtpTrace.Type.handshakeThreshold)
     {
         OtpTrace.TraceEvent("-> " + "HANDSHAKE sendChallenge" + " flags=" + flags + " dist=" + dist + " challenge=" + challenge + " local=" + self);
     }
 }
Пример #22
0
        private static int r4_lookupPort(AbstractNode node)
        {
            int port = 0;

            System.Net.Sockets.TcpClient s = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new System.Net.Sockets.TcpClient(node.host(), epmdPort);

                // build and send epmd request
                // length[2], tag[1], alivename[n] (length = n+1)
                obuf.write2BE(node.getAlive().Length + 1);
                obuf.write1(port4req);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int    i;
                string tmpStr;
                tmpStr   = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i        = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte)tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);

                // send request
                obuf.writeTo((System.IO.Stream)s.GetStream());

                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("-> LOOKUP (r4) " + node);
                }

                // 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]
                byte[] tmpbuf = new byte[100];

                int n = s.GetStream().Read(tmpbuf, 0, 100);

                if (n < 0)
                {
                    // this was an r3 node => not a failure (yet)
                    if (s != null)
                    {
                        s.Close();
                    }
                    throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
                }

                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                int response = ibuf.read1();
                if (response == port4resp)
                {
                    int result = ibuf.read1();
                    if (result == 0)
                    {
                        port = ibuf.read2BE();

                        node.ntype     = ibuf.read1();
                        node._proto    = ibuf.read1();
                        node._distHigh = ibuf.read2BE();
                        node._distLow  = ibuf.read2BE();
                        // ignore rest of fields
                    }
                }
            }
            catch (System.IO.IOException)
            {
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (no response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            catch (Erlang.Exception)
            {
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (invalid response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            catch (System.Net.Sockets.SocketException)
            {
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (no epmd response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            finally
            {
                try
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                }
                catch (System.IO.IOException)
                {
                    /*ignore close errors */
                }
                s = null;
            }

            if (traceLevel >= traceThreshold)
            {
                if (port == 0)
                {
                    OtpTrace.TraceEvent("<- NOT FOUND");
                }
                else
                {
                    OtpTrace.TraceEvent("<- PORT " + port);
                }
            }
            return(port);
        }
Пример #23
0
 protected internal virtual void  sendChallengeAck(byte[] digest)
 {
     
     OtpOutputStream obuf = new OtpOutputStream();
     obuf.write2BE(17);
     obuf.write1(ChallengeAck);
     obuf.write(digest);
     
     obuf.writeTo((System.IO.Stream) socket.GetStream());
     
     if (traceLevel >= OtpTrace.Type.handshakeThreshold)
     {
         OtpTrace.TraceEvent("-> " + "HANDSHAKE sendChallengeAck" + " digest=" + hex(digest) + " local=" + self);
     }
 }
Пример #24
0
        /*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.
         */
        private static System.Net.Sockets.TcpClient r4_publish(OtpLocalNode node)
        {
            System.Net.Sockets.TcpClient s     = null;
            System.Exception             error = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new TcpClient(System.Net.Dns.GetHostName(), epmdPort);

                obuf.write2BE(node.getAlive().Length + 13);

                obuf.write1(publish4req);
                obuf.write2BE(node.port());

                obuf.write1(node.type());

                obuf.write1(node.proto());
                obuf.write2BE(node.distHigh());
                obuf.write2BE(node.distLow());

                obuf.write2BE(node.getAlive().Length);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int    i;
                string tmpStr;
                tmpStr   = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i        = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte)tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);
                obuf.write2BE(0);                 // No extra

                // send request
                obuf.writeTo((System.IO.Stream)s.GetStream());

                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("-> PUBLISH (r4) " + node + " port=" + node.port());
                }

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

                if (n < 0)
                {
                    // this was an r3 node => not a failure (yet)
                    if (s != null)
                    {
                        s.Close();
                    }
                    throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
                }

                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                int response = ibuf.read1();
                if (response == publish4resp)
                {
                    int result = ibuf.read1();
                    if (result == 0)
                    {
                        node._creation = ibuf.read2BE();
                        if (traceLevel >= traceThreshold)
                        {
                            OtpTrace.TraceEvent("<- OK");
                        }
                        return(s);                        // success
                    }
                }
            }
            catch (System.IO.IOException e)
            {
                error = e;
            }
            catch (Erlang.Exception e)
            {
                error = e;
            }
            catch (System.Net.Sockets.SocketException e)
            {
                error = e;
            }

            if (error == null)
            {
                return(s);
            }
            else
            {
                // epmd closed the connection = fail
                if (s != null)
                {
                    s.Close();
                }
                if (traceLevel >= traceThreshold)
                {
                    System.Console.Out.WriteLine("<- (no response)");
                }

                string err = "Nameserver not responding on " + node.host() + " when publishing " + node.getAlive();
                node.epmdFailedConnAttempt(node.node(), err);

                if (traceLevel >= traceThreshold)
                {
                    System.Console.Out.WriteLine("Failed to connect to empd daemon!");
                }

                if (!OtpLocalNode.ignoreLocalEpmdConnectErrors)
                {
                    throw new System.Exception(err);
                }

                node._creation = 0;
                return(null);
            }
        }
Пример #25
0
 protected internal virtual void  sendStatus(System.String status)
 {
     
     OtpOutputStream obuf = new OtpOutputStream();
     obuf.write2BE(status.Length + 1);
     obuf.write1(ChallengeStatus);
     //UPGRADE_NOTE: This code will be optimized in the future;
     byte[] tmpBytes;
     int i;
     string tmpStr;
     tmpStr = status;
     tmpBytes = new byte[tmpStr.Length];
     i = 0;
     while (i < tmpStr.Length)
     {
         tmpBytes[i] = (byte) tmpStr[i];
         i++;
     }
     obuf.write(tmpBytes);
     
     obuf.writeTo((System.IO.Stream) socket.GetStream());
     
     if (traceLevel >= OtpTrace.Type.handshakeThreshold)
     {
         OtpTrace.TraceEvent("-> " + "HANDSHAKE sendStatus" + " status=" + status + " local=" + self);
     }
 }
Пример #26
0
 /*
  * Send a pre-encoded message to a process on a remote node.
  *
  * @param dest the Erlang PID of the remote process.
  * @param msg the encoded message to send.
  *
  * @exception C#.io.IOException if the connection is not active
  * or a communication error occurs.
  **/
 public virtual void  sendBuf(Erlang.Pid dest, OtpOutputStream payload)
 {
     base.sendBuf(this._self.pid(), dest, payload);
 }
Пример #27
0
 public void TestEncodeDecode()
 {
     {
         OtpOutputStream os  = new OtpOutputStream(new Erlang.Atom("abc"));
         OtpInputStream  ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         Assert.IsTrue("abc" == ins.read_atom());
     }
     {
         OtpOutputStream os  = new OtpOutputStream(new Erlang.String("string"));
         OtpInputStream  ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         Assert.IsTrue("string" == ins.read_string());
     }
     {
         Erlang.Pid      pid = new Erlang.Pid("abc", 1, 2, 3);
         OtpOutputStream os  = new OtpOutputStream(pid);
         OtpInputStream  ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         Assert.IsTrue(pid.Equals(ins.read_pid()));
     }
     {
         Erlang.Port     p   = new Erlang.Port("abc", 1, 2);
         OtpOutputStream os  = new OtpOutputStream(p);
         OtpInputStream  ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         Assert.IsTrue(p.Equals(ins.read_port()));
     }
     {
         Erlang.Ref p = new Erlang.Ref("abc", new int[3] {
             1, 2, 3
         }, 2);
         OtpOutputStream os  = new OtpOutputStream(p);
         OtpInputStream  ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         Assert.IsTrue(p.Equals(ins.read_ref()));
     }
     {
         OtpOutputStream os = new OtpOutputStream();
         os.write_long(1);
         OtpInputStream ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         long           n   = ins.read_long();
         Assert.IsTrue(1 == n);
     }
     {
         OtpOutputStream os = new OtpOutputStream();
         os.write_long(0xFFFFFF);
         OtpInputStream ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         long           n   = ins.read_long();
         Assert.IsTrue(0xFFFFFF == n);
     }
     {
         OtpOutputStream os = new OtpOutputStream();
         os.write_long(0xFFFFFFFF);
         OtpInputStream ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         long           n   = ins.read_long();
         Assert.IsTrue(0xFFFFFFFF == n);
     }
     {
         OtpOutputStream os = new OtpOutputStream();
         os.write_ulong((ulong)0xFFFFFFFFFF);
         OtpInputStream ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         ulong          n   = ins.read_ulong();
         Assert.IsTrue((ulong)0xFFFFFFFFFF == n);
     }
     {
         OtpOutputStream os = new OtpOutputStream();
         os.write_ulong((ulong)0xFFFFFFFFFFFF);
         OtpInputStream ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         ulong          n   = ins.read_ulong();
         Assert.IsTrue((ulong)0xFFFFFFFFFFFF == n);
     }
     {
         OtpOutputStream os = new OtpOutputStream();
         os.write_ulong((ulong)0xFFFFFFFFFFFFFF);
         OtpInputStream ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         ulong          n   = ins.read_ulong();
         Assert.IsTrue((ulong)0xFFFFFFFFFFFFFF == n);
     }
     {
         OtpOutputStream os = new OtpOutputStream();
         os.write_ulong((ulong)0xFFFFFFFFFFFFFFFF);
         OtpInputStream ins = new OtpInputStream(os.getBuffer(), 0, os.size());
         ulong          n   = ins.read_ulong();
         Assert.IsTrue((ulong)0xFFFFFFFFFFFFFFFF == n);
     }
 }