Inheritance: System.IO.MemoryStream
コード例 #1
0
 public OtpErlangExternalFun(OtpInputStream buf)
 {
     OtpErlangExternalFun f = buf.read_external_fun();
     module = f.module;
     function = f.function;
     arity = f.arity;
 }
コード例 #2
0
        /**
         * Create an Erlang ref from a stream containing a ref encoded in Erlang
         * external format.
         *
         * @param buf
         *                the stream containing the encoded ref.
         *
         * @exception OtpErlangDecodeException
         *                    if the buffer does not contain a valid external
         *                    representation of an Erlang ref.
         */
        public OtpErlangRef(OtpInputStream buf)
        {
            OtpErlangRef r = buf.read_ref();

            node = r.Node;
            creation = r.Creation;
            ids = r.Ids;
        }
コード例 #3
0
        /**
         * Create an Erlang port from a stream containing a port encoded in Erlang
         * external format.
         *
         * @param buf
         *                the stream containing the encoded port.
         *
         * @exception OtpErlangDecodeException
         *                    if the buffer does not contain a valid external
         *                    representation of an Erlang port.
         */
        public OtpErlangPort(OtpInputStream buf)
        {
            OtpErlangPort p = buf.read_port();

            node = p.Node;
            id = p.Id;
            creation = p.Creation;
        }
コード例 #4
0
        /**
         * Create an Erlang PID from a stream containing a PID encoded in Erlang
         * external format.
         *
         * @param buf
         *                the stream containing the encoded PID.
         *
         * @exception OtpErlangDecodeException
         *                    if the buffer does not contain a valid external
         *                    representation of an Erlang PID.
         */
        public OtpErlangPid(OtpInputStream buf)
        {
            OtpErlangPid p = buf.read_pid();

            node = p.Node;
            id = p.Id;
            serial = p.Serial;
            creation = p.Creation;
        }
コード例 #5
0
 public OtpErlangFun(OtpInputStream buf)
 {
     OtpErlangFun f = buf.read_fun();
     pid = f.pid;
     module = f.module;
     arity = f.arity;
     md5 = f.md5;
     index = f.index;
     old_index = f.old_index;
     uniq = f.uniq;
     freeVars = f.freeVars;
 }
コード例 #6
0
        public override void run()
        {
            if (!connected)
            {
                deliver(new IOException("Not connected"));
                return;
            }

            byte[] lbuf = new byte[4];
            OtpInputStream ibuf;
            OtpErlangObject traceobj;
            int len;
            byte[] tock = { 0, 0, 0, 0 };

            try
            {
            receive_loop:
                while (!done)
                {
                    // don't return until we get a real message
                    // or a failure of some kind (e.g. EXIT)
                    // read length and read buffer must be atomic!
                    do
                    {
                        // read 4 bytes - get length of incoming packet
                        // socket.getInputStream().read(lbuf);
                        readSock(socket, lbuf);
                        ibuf = new OtpInputStream(lbuf, flags);
                        len = ibuf.read4BE();

                        // received tick? send tock!
                        if (len == 0)
                        {
                            lock (this)
                            {
                                socket.GetOutputStream().Write(tock, 0, tock.Length);
                                socket.GetOutputStream().Flush();
                            }
                        }

                    } while (len == 0); // tick_loop

                    // got a real message (maybe) - read len bytes
                    byte[] tmpbuf = new byte[len];
                    // i = socket.getInputStream().read(tmpbuf);
                    readSock(socket, tmpbuf);
                    ibuf = new OtpInputStream(tmpbuf, flags);

                    if (ibuf.read1() != passThrough)
                    {
                        goto receive_loop;
                    }

                    // got a real message (really)
                    OtpErlangObject reason = null;
                    OtpErlangAtom cookie = null;
                    OtpErlangObject tmp = null;
                    OtpErlangTuple head = null;
                    OtpErlangAtom toName;
                    OtpErlangPid to;
                    OtpErlangPid from;
                    int tag;

                    // decode the header
                    tmp = ibuf.read_any();
                    if (!(tmp is OtpErlangTuple))
                    {
                        goto receive_loop;
                    }

                    head = (OtpErlangTuple)tmp;
                    if (!(head.elementAt(0) is OtpErlangLong))
                    {
                        goto receive_loop;
                    }

                    // lets see what kind of message this is
                    tag = (int)((OtpErlangLong)head.elementAt(0)).longValue();

                    switch (tag)
                    {
                        case sendTag: // { SEND, Cookie, ToPid }
                        case sendTTTag: // { SEND, Cookie, ToPid, TraceToken }
                            if (!cookieOk)
                            {
                                // we only check this once, he can send us bad cookies
                                // later if he likes
                                if (!(head.elementAt(1) is OtpErlangAtom))
                                {
                                    goto receive_loop;
                                }
                                cookie = (OtpErlangAtom)head.elementAt(1);
                                if (sendCookie)
                                {
                                    if (!cookie.atomValue().Equals(self.Cookie))
                                    {
                                        cookieError(self, cookie);
                                    }
                                }
                                else
                                {
                                    if (!cookie.atomValue().Equals(""))
                                    {
                                        cookieError(self, cookie);
                                    }
                                }
                                cookieOk = true;
                            }

                            if (traceLevel >= sendThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);

                                /* show received payload too */
                                ibuf.Mark(0);
                                traceobj = ibuf.read_any();

                                if (traceobj != null)
                                {
                                    log.Debug("   " + traceobj);
                                }
                                else
                                {
                                    log.Debug("   (null)");
                                }
                                ibuf.Reset();
                            }

                            to = (OtpErlangPid)head.elementAt(2);

                            deliver(new OtpMsg(to, ibuf));
                            break;

                        case regSendTag: // { REG_SEND, FromPid, Cookie, ToName }
                        case regSendTTTag: // { REG_SEND, FromPid, Cookie, ToName,
                            // TraceToken }
                            if (!cookieOk)
                            {
                                // we only check this once, he can send us bad cookies
                                // later if he likes
                                if (!(head.elementAt(2) is OtpErlangAtom))
                                {
                                    goto receive_loop;
                                }
                                cookie = (OtpErlangAtom)head.elementAt(2);
                                if (sendCookie)
                                {
                                    if (!cookie.atomValue().Equals(self.Cookie))
                                    {
                                        cookieError(self, cookie);
                                    }
                                }
                                else
                                {
                                    if (!cookie.atomValue().Equals(""))
                                    {
                                        cookieError(self, cookie);
                                    }
                                }
                                cookieOk = true;
                            }

                            if (traceLevel >= sendThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);

                                /* show received payload too */
                                ibuf.Mark(0);
                                traceobj = ibuf.read_any();

                                if (traceobj != null)
                                {
                                    log.Debug("   " + traceobj);
                                }
                                else
                                {
                                    log.Debug("   (null)");
                                }
                                ibuf.Reset();
                            }

                            from = (OtpErlangPid)head.elementAt(1);
                            toName = (OtpErlangAtom)head.elementAt(3);

                            deliver(new OtpMsg(from, toName.atomValue(), ibuf));
                            break;

                        case exitTag: // { EXIT, FromPid, ToPid, Reason }
                        case exit2Tag: // { EXIT2, FromPid, ToPid, Reason }
                            if (head.elementAt(3) == null)
                            {
                                goto receive_loop;
                            }
                            if (traceLevel >= ctrlThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);
                            }

                            from = (OtpErlangPid)head.elementAt(1);
                            to = (OtpErlangPid)head.elementAt(2);
                            reason = head.elementAt(3);

                            deliver(new OtpMsg(tag, from, to, reason));
                            break;

                        case exitTTTag: // { EXIT, FromPid, ToPid, TraceToken, Reason }
                        case exit2TTTag: // { EXIT2, FromPid, ToPid, TraceToken,
                            // Reason
                            // }
                            // as above, but bifferent element number
                            if (head.elementAt(4) == null)
                            {
                                goto receive_loop;
                            }
                            if (traceLevel >= ctrlThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);
                            }

                            from = (OtpErlangPid)head.elementAt(1);
                            to = (OtpErlangPid)head.elementAt(2);
                            reason = head.elementAt(4);

                            deliver(new OtpMsg(tag, from, to, reason));
                            break;

                        case linkTag: // { LINK, FromPid, ToPid}
                        case unlinkTag: // { UNLINK, FromPid, ToPid}
                            if (traceLevel >= ctrlThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);
                            }

                            from = (OtpErlangPid)head.elementAt(1);
                            to = (OtpErlangPid)head.elementAt(2);

                            deliver(new OtpMsg(tag, from, to));
                            break;

                        // absolutely no idea what to do with these, so we ignore
                        // them...
                        case groupLeaderTag: // { GROUPLEADER, FromPid, ToPid}
                        case nodeLinkTag: // { NODELINK }
                            // (just show trace)
                            if (traceLevel >= ctrlThreshold)
                            {
                                log.Debug("<- " + headerType(head) + " " + head);
                            }
                            break;

                        default:
                            // garbage?
                            goto receive_loop;
                    }
                } // end receive_loop

                // this section reachable only with break
                // we have received garbage from peer
                deliver(new OtpErlangExit("Remote is sending garbage"));

            } // try

            catch (OtpAuthException e)
            {
                deliver(e);
            }
            catch (OtpErlangDecodeException)
            {
                deliver(new OtpErlangExit("Remote is sending garbage"));
            }
            catch (IOException)
            {
                deliver(new OtpErlangExit("Remote has closed connection"));
            }
            finally
            {
                close();
            }
        }
コード例 #7
0
        protected void recvStatus()
        {
            try
            {
                byte[] buf = read2BytePackage();
                OtpInputStream ibuf = new OtpInputStream(buf, 0);
                int tag = ibuf.read1();
                if (tag != ChallengeStatus)
                {
                    throw new IOException("Handshake protocol error");
                }
                byte[] tmpbuf = new byte[buf.Length - 1];
                ibuf.readN(tmpbuf);
                String status = OtpErlangString.newString(tmpbuf);

                if (status.CompareTo("ok") != 0)
                {
                    throw new IOException("Peer replied with status '" + status + "' instead of 'ok'");
                }
            }
            catch (OtpErlangDecodeException)
            {
                throw new IOException("Handshake failed - not enough data");
            }
            if (traceLevel >= handshakeThreshold)
            {
                log.Debug("<- " + "HANDSHAKE recvStatus (ok)" + " local=" + self);
            }
        }
コード例 #8
0
        protected void recvName(OtpPeer peer)
        {
            String hisname = "";

            try
            {
                byte[] tmpbuf = read2BytePackage();
                OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0);
                byte[] tmpname;
                int len = tmpbuf.Length;
                peer.Type = ibuf.read1();
                if (peer.Type != AbstractNode.NTYPE_R6)
                {
                    throw new IOException("Unknown remote node type");
                }
                peer.DistLow = peer.DistHigh = ibuf.read2BE();
                if (peer.DistLow < 5)
                {
                    throw new IOException("Unknown remote node type");
                }
                peer.Flags = ibuf.read4BE();
                tmpname = new byte[len - 7];
                ibuf.readN(tmpname);
                hisname = OtpErlangString.newString(tmpname);
                // Set the old nodetype parameter to indicate hidden/normal status
                // When the old handshake is removed, the ntype should also be.
                if ((peer.Flags & AbstractNode.dFlagPublished) != 0)
                {
                    peer.Type = AbstractNode.NTYPE_R4_ERLANG;
                }
                else
                {
                    peer.Type = AbstractNode.NTYPE_R4_HIDDEN;
                }

                if ((peer.Flags & AbstractNode.dFlagExtendedReferences) == 0)
                {
                    throw new IOException("Handshake failed - peer cannot handle extended references");
                }

                if ((peer.Flags & AbstractNode.dFlagExtendedPidsPorts) == 0)
                {
                    throw new IOException("Handshake failed - peer cannot handle extended pids and ports");
                }
            }
            catch (OtpErlangDecodeException)
            {
                throw new IOException("Handshake failed - not enough data");
            }

            int i = hisname.IndexOf('@', 0);
            peer.Node = hisname;
            peer.Alive = hisname.Substring(0, i);
            peer.Host = hisname.Substring(i + 1, hisname.Length - (i + 1));

            if (traceLevel >= handshakeThreshold)
            {
                log.Debug("<- " + "HANDSHAKE" + " ntype=" + peer.Type
                             + " dist=" + peer.DistHigh + " remote=" + peer);
            }
        }
コード例 #9
0
 /**
  * Create an Erlang integer from a stream containing an integer encoded in
  * Erlang external format.
  *
  * @param buf
  *                the stream containing the encoded value.
  *
  * @exception OtpErlangDecodeException
  *                    if the buffer does not contain a valid external
  *                    representation of an Erlang integer.
  *
  * @exception OtpErlangRangeException
  *                    if the value is too large to be represented as an int,
  *                    or the value is negative.
  */
 public OtpErlangUInt(OtpInputStream buf)
     : base(buf)
 {
 }
コード例 #10
0
 /**
  * Create an Erlang integer from a stream containing an integer encoded in
  * Erlang external format.
  *
  * @param buf
  *                the stream containing the encoded value.
  *
  * @exception OtpErlangDecodeException
  *                    if the buffer does not contain a valid external
  *                    representation of an Erlang integer.
  *
  * @exception OtpErlangRangeException
  *                    if the value is too large to be represented as a
  *                    short.
  */
 public OtpErlangShort(OtpInputStream buf)
     : base(buf)
 {
 }
コード例 #11
0
 /**
  * Create an Erlang string from a stream containing a string encoded in
  * Erlang external format.
  *
  * @param buf
  *            the stream containing the encoded string.
  *
  * @exception OtpErlangDecodeException
  *                if the buffer does not contain a valid external
  *                representation of an Erlang string.
  */
 public OtpErlangString(OtpInputStream buf)
 {
     str = buf.read_string();
 }
コード例 #12
0
        protected void recvChallengeAck(int our_challenge)
        {
            byte[] her_digest = new byte[16];
            try
            {
                byte[] buf = read2BytePackage();
                OtpInputStream ibuf = new OtpInputStream(buf, 0);
                int tag = ibuf.read1();
                if (tag != ChallengeAck)
                {
                    throw new IOException("Handshake protocol error");
                }
                ibuf.readN(her_digest);
                byte[] our_digest = genDigest(our_challenge, self.Cookie);
                if (!digests_equals(her_digest, our_digest))
                {
                    throw new OtpAuthException("Peer authentication error.");
                }
            }
            catch (OtpErlangDecodeException)
            {
                throw new IOException("Handshake failed - not enough data");
            }
            catch (Exception)
            {
                throw new OtpAuthException("Peer authentication error.");
            }

            if (traceLevel >= handshakeThreshold)
            {
                log.Debug("<- " + "HANDSHAKE recvChallengeAck" + " from="
                             + peer.Node + " digest=" + hex(her_digest) + " local=" + self);
            }
        }
コード例 #13
0
 /**
  * Create a binary from a stream containing a binary encoded in Erlang
  * external format.
  *
  * @param buf
  *                the stream containing the encoded binary.
  *
  * @exception OtpErlangDecodeException
  *                    if the buffer does not contain a valid external
  *                    representation of an Erlang binary.
  */
 public OtpErlangBinary(OtpInputStream buf)
     : base(new byte[0])
 {
     bin = buf.read_binary();
     pad_bits = 0;
 }
コード例 #14
0
 /**
  * Create an Erlang integer from a stream containing an integer encoded in
  * Erlang external format.
  *
  * @param buf
  *                the stream containing the encoded value.
  *
  * @exception OtpErlangDecodeException
  *                    if the buffer does not contain a valid external
  *                    representation of an Erlang integer.
  *
  * @exception OtpErlangRangeException
  *                    if the value is too large to be represented as a byte.
  */
 public OtpErlangByte(OtpInputStream buf)
     : base(buf)
 {
 }
コード例 #15
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 TcpClient r4_publish(OtpLocalNode node)
        {
            TcpClient s = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new TcpClient(node.Host, EpmdPort.get());

                obuf.write2BE(node.Alive.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.Alive.Length);
                obuf.writeN(Encoding.GetEncoding("iso-8859-1").GetBytes(node.Alive));
                obuf.write2BE(0); // No extra

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

                if (traceLevel >= traceThreshold)
                {
                    log.Debug("-> PUBLISH (r4) " + node + " port=" + node.Port);
                }

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

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

                OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0);

                int response = ibuf.read1();
                if (response == publish4resp)
                {
                    int result = ibuf.read1();
                    if (result == 0)
                    {
                        node.Creation = ibuf.read2BE();
                        if (traceLevel >= traceThreshold)
                        {
                            log.Debug("<- OK");
                        }
                        return s; // success
                    }
                }

            }
            catch (SocketException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (no response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host);
            }
            catch (IOException)
            {
                // epmd closed the connection = fail
                if (s != null)
                {
                    s.Close();
                }
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (no response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host
                              + " when publishing " + node.Alive);
            }
            catch (OtpErlangDecodeException)
            {
                if (s != null)
                {
                    s.Close();
                }
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (invalid response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host
                              + " when publishing " + node.Alive);
            }

            if (s != null)
            {
                s.Close();
            }
            return null;
        }
コード例 #16
0
        private static int r4_lookupPort(AbstractNode node)
        {
            int port = 0;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                using (TcpClient s = new TcpClient(node.Host, EpmdPort.get()))
                {
                    // build and send epmd request
                    // length[2], tag[1], alivename[n] (length = n+1)
                    obuf.write2BE(node.Alive.Length + 1);
                    obuf.write1(port4req);
                    obuf.writeN(Encoding.GetEncoding("iso-8859-1").GetBytes(node.Alive));

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

                    if (traceLevel >= traceThreshold)
                    {
                        log.Debug("-> 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, tmpbuf.Length);

                    if (n < 0)
                    {
                        // this was an r3 node => not a failure (yet)
                        throw new IOException("Nameserver not responding on "
                                      + node.Host + " when looking up " + node.Alive);
                    }

                    OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0);

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

                            node.Type = ibuf.read1();
                            node.Proto = ibuf.read1();
                            node.DistHigh = ibuf.read2BE();
                            node.DistLow = ibuf.read2BE();
                            // ignore rest of fields
                        }
                    }
                }
            }
            catch (SocketException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (no response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host);
            }
            catch (IOException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (no response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host
                              + " when looking up " + node.Alive);
            }
            catch (OtpErlangDecodeException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (invalid response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host
                              + " when looking up " + node.Alive);
            }

            if (traceLevel >= traceThreshold)
            {
                if (port == 0)
                {
                    log.Debug("<- NOT FOUND");
                }
                else
                {
                    log.Debug("<- PORT " + port);
                }
            }
            return port;
        }
コード例 #17
0
        private static TcpClient r3_publish(OtpLocalNode node)
        {
            TcpClient s;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new TcpClient(node.Host, EpmdPort.get());

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

                obuf.write1(publish3req);
                obuf.write2BE(node.Port);
                obuf.writeN(Encoding.GetEncoding("iso-8859-1").GetBytes(node.Alive));

                // send request
                obuf.WriteTo(s.GetStream());
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("-> PUBLISH (r3) " + node + " port=" + node.Port);
                }

                byte[] tmpbuf = new byte[100];

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

                if (n < 0)
                {
                    if (traceLevel >= traceThreshold)
                    {
                        log.Debug("<- (no response)");
                    }
                    return null;
                }

                OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0);

                if (ibuf.read1() == publish3ok)
                {
                    node.Creation = ibuf.read2BE();
                    if (traceLevel >= traceThreshold)
                    {
                        log.Debug("<- OK");
                    }
                    return s; // success - don't close socket
                }
            }
            catch (SocketException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (no response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host);
            }
            catch (IOException)
            {
                // epmd closed the connection = fail
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (no response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host
                              + " when publishing " + node.Alive);
            }
            catch (OtpErlangDecodeException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (invalid response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host
                              + " when publishing " + node.Alive);
            }
            return null; // failure
        }
コード例 #18
0
        private static int r3_lookupPort(AbstractNode node)
        {
            int port = 0;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                using (TcpClient s = new TcpClient(node.Host, EpmdPort.get()))
                {
                    // build and send epmd request
                    // length[2], tag[1], alivename[n] (length = n+1)
                    obuf.write2BE(node.Alive.Length + 1);
                    obuf.write1(port3req);
                    obuf.writeN(Encoding.GetEncoding("iso-8859-1").GetBytes(node.Alive));

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

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

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

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

                    port = ibuf.read2BE();
                }
            }
            catch (SocketException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (no response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host);
            }
            catch (IOException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (no response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host
                              + " when looking up " + node.Alive);
            }
            catch (OtpErlangDecodeException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (invalid response)");
                }
                throw new IOException("Nameserver not responding on " + node.Host
                              + " when looking up " + node.Alive);
            }

            if (traceLevel >= traceThreshold)
            {
                if (port == 0)
                {
                    log.Debug("<- NOT FOUND");
                }
                else
                {
                    log.Debug("<- PORT " + port);
                }
            }
            return port;
        }
コード例 #19
0
        public static String[] lookupNames(IPAddress address)
        {
            try
            {
                OtpOutputStream obuf = new OtpOutputStream();

                using (TcpClient s = new TcpClient(address.ToString(), EpmdPort.get()))
                {
                    obuf.write2BE(1);
                    obuf.write1(names4req);
                    // send request
                    obuf.WriteTo(s.GetStream());

                    if (traceLevel >= traceThreshold)
                    {
                        log.Debug("-> NAMES (r4) ");
                    }

                    // get reply
                    byte[] buffer = new byte[256];
                    MemoryStream ms = new MemoryStream(256);
                    while (true)
                    {
                        int bytesRead = s.GetStream().Read(buffer, 0, buffer.Length);
                        if (bytesRead == -1)
                        {
                            break;
                        }
                        ms.Write(buffer, 0, bytesRead);
                    }
                    byte[] tmpbuf = ms.GetBuffer();
                    OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0);
                    ibuf.read4BE(); // read port int
                    // int port = ibuf.read4BE();
                    // check if port = epmdPort

                    int n = tmpbuf.Length;
                    byte[] buf = new byte[n - 4];
                    Array.Copy(tmpbuf, 4, buf, 0, n - 4);
                    String all = OtpErlangString.newString(buf);
                    return all.Split(new char[] { '\n' });
                }
            }
            catch (SocketException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (no response)");
                }
                throw new IOException("Nameserver not responding on " + address);
            }
            catch (IOException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (no response)");
                }
                throw new IOException("Nameserver not responding when requesting names");
            }
            catch (OtpErlangDecodeException)
            {
                if (traceLevel >= traceThreshold)
                {
                    log.Debug("<- (invalid response)");
                }
                throw new IOException("Nameserver not responding when requesting names");
            }
        }
コード例 #20
0
        protected byte[] read2BytePackage()
        {
            byte[] lbuf = new byte[2];
            byte[] tmpbuf;

            readSock(socket, lbuf);
            OtpInputStream ibuf = new OtpInputStream(lbuf, 0);
            int len = ibuf.read2BE();
            tmpbuf = new byte[len];
            readSock(socket, tmpbuf);
            return tmpbuf;
        }
コード例 #21
0
        protected int recvChallenge()
        {
            int challenge;

            try
            {
                byte[] buf = read2BytePackage();
                OtpInputStream ibuf = new OtpInputStream(buf, 0);
                peer.Type = ibuf.read1();
                if (peer.Type != AbstractNode.NTYPE_R6)
                {
                    throw new IOException("Unexpected peer type");
                }
                peer.DistLow = peer.DistHigh = ibuf.read2BE();
                peer.Flags = ibuf.read4BE();
                challenge = ibuf.read4BE();
                byte[] tmpname = new byte[buf.Length - 11];
                ibuf.readN(tmpname);
                String hisname = OtpErlangString.newString(tmpname);
                if (!hisname.Equals(peer.Node))
                {
                    throw new IOException("Handshake failed - peer has wrong name: " + hisname);
                }

                if ((peer.Flags & AbstractNode.dFlagExtendedReferences) == 0)
                {
                    throw new IOException("Handshake failed - peer cannot handle extended references");
                }

                if ((peer.Flags & AbstractNode.dFlagExtendedPidsPorts) == 0)
                {
                    throw new IOException("Handshake failed - peer cannot handle extended pids and ports");
                }

            }
            catch (OtpErlangDecodeException)
            {
                throw new IOException("Handshake failed - not enough data");
            }

            if (traceLevel >= handshakeThreshold)
            {
                log.Debug("<- " + "HANDSHAKE recvChallenge" + " from="
                             + peer.Node + " challenge=" + challenge + " local=" + self);
            }

            return challenge;
        }
コード例 #22
0
            private void r4_port(TcpClient s, OtpInputStream ibuf)
            {
                try
                {
                    int len = (int)(ibuf.Length - 1);
                    byte[] alive = new byte[len];
                    ibuf.readN(alive);
                    String name = OtpErlangString.newString(alive);
                    OtpPublishedNode node = null;

                    if (traceLevel >= traceThreshold)
                    {
                        log.Debug("<- PORT (r4) " + name);
                    }

                    lock (portmap)
                    {
                        if (portmap.ContainsKey(name))
                        {
                            node = portmap[name];
                        }
                    }

                    OtpOutputStream obuf = new OtpOutputStream();
                    if (node != null)
                    {
                        obuf.write1(port4resp);
                        obuf.write1(0);
                        obuf.write2BE(node.Port);
                        obuf.write1(node.Type);
                        obuf.write1(node.Proto);
                        obuf.write2BE(node.DistHigh);
                        obuf.write2BE(node.DistLow);
                        obuf.write2BE(len);
                        obuf.writeN(alive);
                        obuf.write2BE(0);
                    }
                    else
                    {
                        obuf.write1(port4resp);
                        obuf.write1(1);
                    }
                    obuf.WriteTo(s.GetStream());
                }
                catch (IOException)
                {
                    if (traceLevel >= traceThreshold)
                    {
                        log.Debug("<- (no response)");
                    }
                    throw new IOException("Request not responding");
                }
                return;
            }
コード例 #23
0
        /**
         * Read a compressed term from the stream
         *
         * @return the resulting uncompressed term.
         *
         * @exception OtpErlangDecodeException
         *                if the next term in the stream is not a compressed term.
         */
        public OtpErlangObject read_compressed()
        {
            int tag = read1skip_version();

            if (tag != OtpExternal.compressedTag)
            {
                throw new OtpErlangDecodeException("Wrong tag encountered, expected "
                                   + OtpExternal.compressedTag + ", got " + tag);
            }

            int size = read4BE();
            byte[] buf = new byte[size];
            DeflateStream dos = new DeflateStream(this, CompressionMode.Decompress, true);
            try
            {
                int dsize = dos.Read(buf, 0, size);
                if (dsize != size)
                {
                    throw new OtpErlangDecodeException("Decompression gave " + dsize + " bytes, not " + size);
                }
            }
            catch (OtpErlangDecodeException)
            {
                throw;
            }
            catch (InvalidDataException e)
            {
                throw new OtpErlangDecodeException(e.Message);
            }

            OtpInputStream ois = new OtpInputStream(buf, flags);
            return ois.read_any();
        }
コード例 #24
0
            private void r4_names(TcpClient s, OtpInputStream ibuf)
            {
                try
                {
                    if (traceLevel >= traceThreshold)
                    {
                        log.Debug("<- NAMES(r4) ");
                    }

                    OtpOutputStream obuf = new OtpOutputStream();
                    obuf.write4BE(EpmdPort.get());
                    lock (portmap)
                    {
                        foreach (KeyValuePair<string, OtpPublishedNode> pair in portmap)
                        {
                            OtpPublishedNode node = pair.Value;
                            string info = String.Format("name {0} at port {1}\n", node.Alive, node.Port);
                            byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(info);
                            obuf.writeN(bytes);
                        }
                    }
                    obuf.WriteTo(s.GetStream());
                }
                catch (IOException)
                {
                    if (traceLevel >= traceThreshold)
                    {
                        log.Debug("<- (no response)");
                    }
                    throw new IOException("Request not responding");
                }
                return;
            }
コード例 #25
0
        /**
         * Create a bitstr from a stream containing a bitstr encoded in Erlang
         * external format.
         *
         * @param buf
         *                the stream containing the encoded bitstr.
         *
         * @exception OtpErlangDecodeException
         *                    if the buffer does not contain a valid external
         *                    representation of an Erlang bitstr.
         */
        public OtpErlangBitstr(OtpInputStream buf)
        {
            bin = buf.read_bitstr(out pad_bits);

            check_bitstr(bin, pad_bits);
        }
コード例 #26
0
        /**
         * Create an Erlang integer from a stream containing an integer encoded in
         * Erlang external format.
         *
         * @param buf
         *                the stream containing the encoded value.
         *
         * @exception OtpErlangDecodeException
         *                    if the buffer does not contain a valid external
         *                    representation of an Erlang integer.
         */
        public OtpErlangLong(OtpInputStream buf)
        {
            byte[] b = buf.read_integer_byte_array();

            try
            {
                val = OtpInputStream.byte_array_to_long(b, false);
            }
            catch (OtpErlangDecodeException)
            {
                bigVal = new BigInteger(b);
            }
        }
コード例 #27
0
        /**
         * Create a tuple from a stream containing an tuple encoded in Erlang
         * external format.
         *
         * @param buf
         *                the stream containing the encoded tuple.
         *
         * @exception OtpErlangDecodeException
         *                    if the buffer does not contain a valid external
         *                    representation of an Erlang tuple.
         */
        public OtpErlangTuple(OtpInputStream buf)
        {
            int arity = buf.read_tuple_head();

            if (arity > 0)
            {
                elems = new OtpErlangObject[arity];

                for (int i = 0; i < arity; i++)
                {
                    elems[i] = buf.read_any();
                }
            }
            else
            {
                elems = NO_ELEMENTS;
            }
        }
コード例 #28
0
            private void r4_publish(TcpClient s, OtpInputStream ibuf)
            {
                try
                {
                    int port = ibuf.read2BE();
                    int type = ibuf.read1();
                    int proto = ibuf.read1();
                    int distHigh = ibuf.read2BE();
                    int distLow = ibuf.read2BE();
                    int len = ibuf.read2BE();
                    byte[] alive = new byte[len];
                    ibuf.readN(alive);
                    int elen = ibuf.read2BE();
                    byte[] extra = new byte[elen];
                    ibuf.readN(extra);
                    String name = OtpErlangString.newString(alive);
                    OtpPublishedNode node = new OtpPublishedNode(name);
                    node.Type = type;
                    node.DistHigh = distHigh;
                    node.DistLow = distLow;
                    node.Proto = proto;
                    node.Port = port;

                    if (traceLevel >= traceThreshold)
                    {
                        log.Debug("<- PUBLISH (r4) " + name + " port=" + node.Port);
                    }

                    OtpOutputStream obuf = new OtpOutputStream();
                    obuf.write1(publish4resp);
                    obuf.write1(0);
                    obuf.write2BE(epmd.Creation);
                    obuf.WriteTo(s.GetStream());

                    lock (portmap)
                    {
                        portmap.Add(name, node);
                    }
                    publishedPort.Add(name);
                }
                catch (IOException)
                {
                    if (traceLevel >= traceThreshold)
                    {
                        log.Debug("<- (no response)");
                    }
                    throw new IOException("Request not responding");
                }
                return;
            }
コード例 #29
0
 /**
  * Create an atom from a stream containing an atom encoded in Erlang
  * external format.
  *
  * @param buf
  *                the stream containing the encoded atom.
  *
  * @exception OtpErlangDecodeException
  *                    if the buffer does not contain a valid external
  *                    representation of an Erlang atom.
  */
 public OtpErlangAtom(OtpInputStream buf)
 {
     atom = buf.read_atom();
 }
コード例 #30
0
 /**
  * Create a boolean from a stream containing an atom encoded in Erlang
  * external format. The value of the boolean will be true if the atom
  * represented by the stream is "true" without regard to case. For other
  * atom values, the boolean will have the value false.
  *
  * @exception OtpErlangDecodeException
  *                    if the buffer does not contain a valid external
  *                    representation of an Erlang atom.
  */
 public OtpErlangBoolean(OtpInputStream buf)
     : base(buf)
 {
 }