Example #1
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());
            }
            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);
        }
Example #2
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);
        }
Example #3
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;
        }
Example #4
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;
        }
Example #5
0
 /*
 * Determine what port a node listens for incoming connections on.
 *
 * @return the listen port for the specified node, or 0 if the node
 * was not registered with Epmd.
 *
 * @exception C#.io.IOException if there was no response from the
 * name server.
 **/
 public static int lookupPort(AbstractNode node)
 {
     try
     {
         return r4_lookupPort(node);
     }
     catch (System.IO.IOException)
     {
         return r3_lookupPort(node);
     }
 }