Inheritance: System.IO.MemoryStream
Ejemplo n.º 1
0
 private void ListenActivity()
 {
     EndPoint remote = new IPEndPoint(IPAddress.Any, 0);
     for (;;)
     {
         byte[] message = new byte[BUF];
         while (this.socket.Available == 0) { }
         int size = this.socket.ReceiveFrom(message, ref remote);
         if (size == 0) continue;
         Debug.Print("Received " + size + " bytes from " + ((IPEndPoint)remote).Address + " (" + ((IPEndPoint)remote).Port + ")");
         string messageString = "";
         for (int i = 0; i < size; ++i)
         {
             messageString += (int)message[i] + " ";
         }
         Debug.Print(messageString);
         OtpInputStream inStream = new OtpInputStream(message);
         OtpErlangObject msg = inStream.read_any();
         Debug.Print(msg.ToString());
         OtpErlangTuple t = new OtpErlangTuple(new OtpErlangObject[] { new OtpErlangAtom("ok"), msg });
         OtpOutputStream outStream = new OtpOutputStream(t, false);
         byte[] answer = outStream.GetBuffer();
         this.socket.SendTo(answer, remote);
         Debug.Print(t.ToString());
     }
 }
 public OtpErlangExternalFun(OtpInputStream buf)
 {
     OtpErlangExternalFun f = buf.read_external_fun();
     module = f.module;
     function = f.function;
     arity = f.arity;
 }
Ejemplo n.º 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;
        }
Ejemplo n.º 4
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;
        }
Ejemplo n.º 5
0
        private async Task Port_R4(IOtpTransport socket, OtpInputStream ibuf)
        {
            try
            {
                int    len   = (int)(ibuf.Length - 1);
                byte[] alive = ibuf.ReadN(len);

                string name = OtpErlangString.FromEncoding(alive);
                if (traceLevel >= traceThreshold)
                {
                    Logger.Debug($"<- PORT (r4) {name}");
                }

                if (!portmap.TryGetValue(name, out AbstractNode node))
                {
                    node = null;
                }

                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);
                    if (traceLevel >= traceThreshold)
                    {
                        Logger.Debug("-> 0 (success)");
                    }
                }
                else
                {
                    obuf.Write1(port4resp);
                    obuf.Write1(1);
                    if (traceLevel >= traceThreshold)
                    {
                        Logger.Debug("-> 1 (failure)");
                    }
                }

                await obuf.WriteToAsync(socket.OutputStream);
            }
            catch (IOException e)
            {
                if (traceLevel >= traceThreshold)
                {
                    Logger.Debug("<- (no response)");
                }
                throw new IOException("Request not responding", e);
            }
        }
Ejemplo n.º 6
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;
        }
Ejemplo n.º 7
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;
            }
Ejemplo n.º 8
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;
 }
Ejemplo n.º 9
0
            public override void run()
            {
                byte[]         lbuf = new byte[2];
                OtpInputStream ibuf;
                int            len;

                try
                {
                    while (!done)
                    {
                        readSock(sock, lbuf);
                        ibuf = new OtpInputStream(lbuf, 0);
                        len  = ibuf.read2BE();
                        byte[] tmpbuf = new byte[len];
                        readSock(sock, tmpbuf);
                        ibuf = new OtpInputStream(tmpbuf, 0);

                        int request = ibuf.read1();
                        switch (request)
                        {
                        case publish4req:
                            r4_publish(sock, ibuf);
                            break;

                        case port4req:
                            r4_port(sock, ibuf);
                            done = true;
                            break;

                        case names4req:
                            r4_names(sock, ibuf);
                            done = true;
                            break;

                        case stopReq:
                            break;

                        default:
                            log.InfoFormat("[OtpEpmd] Unknown request (request={0}, length={1}) from {2}",
                                           request, len, sock.Client.RemoteEndPoint);
                            break;
                        }
                    }
                }
                catch (IOException)
                {
                }
                finally
                {
                    quit();
                }
            }
Ejemplo n.º 10
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;
        }
Ejemplo n.º 11
0
        public OtpErlangFun(OtpInputStream buf)
        {
            OtpErlangFun f = buf.ReadFun();

            Pid      = f.Pid;
            Module   = f.Module;
            Arity    = f.Arity;
            Md5      = f.Md5;
            Index    = f.Index;
            OldIndex = f.OldIndex;
            Uniq     = f.Uniq;
            FreeVars = f.FreeVars;
        }
Ejemplo n.º 12
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);
            }
        }
Ejemplo n.º 13
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;
            }
Ejemplo n.º 14
0
        public OtpErlangMap(OtpInputStream buf)
        {
            int arity = buf.ReadMapHead();

            if (arity == 0)
            {
                return;
            }
            for (int i = 0; i < arity; i++)
            {
                IOtpErlangObject key   = buf.ReadAny();
                IOtpErlangObject value = buf.ReadAny();
                Add(key, value);
            }
        }
Ejemplo n.º 15
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;
            }
        }
Ejemplo n.º 16
0
        private async Task <string> Publish_R4(IOtpTransport socket, OtpInputStream ibuf)
        {
            try
            {
                int    port     = ibuf.Read2BE();
                int    type     = ibuf.Read1();
                int    proto    = ibuf.Read1();
                int    distHigh = ibuf.Read2BE();
                int    distLow  = ibuf.Read2BE();
                string name     = ibuf.ReadStringData();
                ibuf.ReadStringData(); // extra
                AbstractNode node = new AbstractNode()
                {
                    Node     = name,
                    Port     = port,
                    Type     = type,
                    DistHigh = distHigh,
                    DistLow  = distLow,
                    Proto    = proto
                };

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

                OtpOutputStream obuf = new OtpOutputStream();
                obuf.Write1(ALIVE2_RESP);
                obuf.Write1(0);
                obuf.Write2BE(NextCreation());
                await obuf.WriteToAsync(socket.OutputStream);

                portmap.TryAdd(name, node);
                return(name);
            }
            catch (IOException e)
            {
                if (traceLevel >= traceThreshold)
                {
                    Logger.Debug("<- (no response)");
                }
                throw new IOException("Request not responding", e);
            }
        }
Ejemplo n.º 17
0
        protected void RecvComplement(int send_name_tag)
        {
            if (send_name_tag == 'n' && (Peer.CapFlags & AbstractNode.dFlagHandshake23) != 0)
            {
                try
                {
                    OtpInputStream ibuf = Read2BytePackage();
                    if (ibuf.Read1() != 'c')
                    {
                        throw new IOException("Not a complement tag");
                    }

                    long flagsHigh = ibuf.Read4BE();
                    Peer.CapFlags |= flagsHigh << 32;
                    Peer.Creation  = ibuf.Read4BE();
                }
                catch (OtpDecodeException e)
                {
                    throw new IOException("Handshake failed - not enough data", e);
                }
            }
        }
Ejemplo n.º 18
0
        /**
         * Read a compressed term from the stream
         */
        public IOtpErlangObject ReadCompressed()
        {
            int tag = Read1SkipVersion();

            if (tag != OtpExternal.compressedTag)
            {
                throw new OtpDecodeException("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 OtpDecodeException("Decompression gave " + dsize + " bytes, not " + size);
                }
            }
            catch (OtpDecodeException)
            {
                throw;
            }
            catch (InvalidDataException e)
            {
                throw new OtpDecodeException("Failed to decode compressed", e);
            }

            OtpInputStream ois = new OtpInputStream(buf)
            {
                Flags = Flags
            };

            return(ois.ReadAny());
        }
Ejemplo n.º 19
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());
        }
Ejemplo n.º 20
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)
 {
 }
Ejemplo n.º 21
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;
 }
 /**
  * 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, or the value is negative.
  */
 public OtpErlangUShort(OtpInputStream buf)
     : base(buf)
 {
 }
Ejemplo n.º 23
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();
 }
Ejemplo n.º 24
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;
            }
Ejemplo n.º 25
0
 /**
  * Read binary data in the Erlang external format, and produce a
  * corresponding Erlang data type object. This method is normally used when
  * Erlang terms are received in messages, however it can also be used for
  * reading terms from disk.
  *
  * @param buf
  *                an input stream containing one or more encoded Erlang
  *                terms.
  *
  * @return an object representing one of the Erlang data types.
  *
  * @exception OtpErlangDecodeException
  *                    if the stream does not contain a valid representation
  *                    of an Erlang term.
  */
 public static OtpErlangObject decode(OtpInputStream buf)
 {
     return buf.read_any();
 }
Ejemplo n.º 26
0
        protected OtpInputStream Read2BytePackage()
        {
            OtpInputStream ibuf = new OtpInputStream(ReadSock(2));

            return(new OtpInputStream(ReadSock(ibuf.Read2BE())));
        }
Ejemplo n.º 27
0
 /**
  * Create a list from a stream containing an list encoded in Erlang external
  * format.
  *
  * @param buf
  *            the stream containing the encoded list.
  *
  * @exception OtpErlangDecodeException
  *                if the buffer does not contain a valid external
  *                representation of an Erlang list.
  */
 public OtpErlangList(OtpInputStream buf)
 {
     int arity = buf.read_list_head();
     if (arity > 0)
     {
         elems = new OtpErlangObject[arity];
         for (int i = 0; i < arity; i++)
         {
             elems[i] = buf.read_any();
         }
         /* discard the terminating nil (empty list) or read tail */
         if (buf.peek1() == OtpExternal.nilTag)
         {
             buf.read_nil();
         }
         else
         {
             lastTail = buf.read_any();
         }
     }
     else
     {
         elems = NO_ELEMENTS;
     }
 }
Ejemplo n.º 28
0
 // send has receiver pid but no sender information
 internal OtpMsg(OtpErlangPid to, OtpErlangObject payload)
 {
     tag = sendTag;
     from = null;
     this.to = to;
     toName = null;
     paybuf = null;
     this.payload = payload;
 }
Ejemplo n.º 29
0
 // send has receiver pid but no sender information
 internal OtpMsg(OtpErlangPid to, OtpInputStream paybuf)
 {
     tag = sendTag;
     from = null;
     this.to = to;
     toName = null;
     this.paybuf = paybuf;
     payload = null;
 }
Ejemplo n.º 30
0
 // special case when reason is an atom (i.e. most of the time)
 internal OtpMsg(int tag, OtpErlangPid from, OtpErlangPid to, String reason)
 {
     this.tag = tag;
     this.from = from;
     this.to = to;
     paybuf = null;
     payload = new OtpErlangAtom(reason);
 }
Ejemplo n.º 31
0
 // exit (etc) has from, to, reason
 internal OtpMsg(int tag, OtpErlangPid from, OtpErlangPid to, OtpErlangObject reason)
 {
     this.tag = tag;
     this.from = from;
     this.to = to;
     paybuf = null;
     payload = reason;
 }
Ejemplo n.º 32
0
 // send_reg has sender pid and receiver name
 internal OtpMsg(OtpErlangPid from, String toName, OtpErlangObject payload)
 {
     tag = regSendTag;
     this.from = from;
     this.toName = toName;
     to = null;
     paybuf = null;
     this.payload = payload;
 }
Ejemplo n.º 33
0
 // send_reg has sender pid and receiver name
 internal OtpMsg(OtpErlangPid from, String toName, OtpInputStream paybuf)
 {
     tag = regSendTag;
     this.from = from;
     this.toName = toName;
     to = null;
     this.paybuf = paybuf;
     payload = null;
 }
        /**
         * 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);
            }
        }
Ejemplo n.º 35
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();
 }
Ejemplo n.º 36
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();
        }
Ejemplo n.º 37
0
        protected int RecvChallenge()
        {
            int challenge;

            try
            {
                OtpInputStream ibuf = Read2BytePackage();
                int            namelen;
                switch (ibuf.Read1())
                {
                case 'n':
                    if (Peer.DistChoose != 5)
                    {
                        throw new IOException("Old challenge wrong version");
                    }
                    Peer.DistLow  = Peer.DistHigh = ibuf.Read2BE();
                    Peer.CapFlags = ibuf.Read4BE();
                    if ((Peer.CapFlags & AbstractNode.dFlagHandshake23) != 0)
                    {
                        throw new IOException("Old challenge unexpected DFLAG_HANDHAKE_23");
                    }
                    challenge = ibuf.Read4BE();
                    namelen   = (int)ibuf.Length - (1 + 2 + 4 + 4);
                    break;

                case 'N':
                    Peer.DistLow  = Peer.DistHigh = Peer.DistChoose = 6;
                    Peer.CapFlags = ibuf.Read8BE();
                    if ((Peer.CapFlags & AbstractNode.dFlagHandshake23) == 0)
                    {
                        throw new IOException("New challenge missing DFLAG_HANDHAKE_23");
                    }
                    challenge     = ibuf.Read4BE();
                    Peer.Creation = ibuf.Read4BE();
                    namelen       = ibuf.Read2BE();
                    break;

                default:
                    throw new IOException("Unexpected peer type");
                }

                string hisname = ibuf.ReadStringData(namelen);
                if (!hisname.Equals(Peer.Node))
                {
                    throw new IOException("Handshake failed - peer has wrong name: " + hisname);
                }

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

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

            if (TraceLevel >= TraceHandshake)
            {
                Logger.Debug($"<- HANDSHAKE recvChallenge from={Peer.Node} challenge={challenge} local={Local}");
            }

            return(challenge);
        }
Ejemplo n.º 38
0
 /**
  * Create an Erlang float from a stream containing a float 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 float.
  *
  * @exception OtpErlangRangeException
  *                    if the value cannot be represented as a Java float.
  */
 public OtpErlangFloat(OtpInputStream buf)
     : base(buf)
 {
 }
Ejemplo n.º 39
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.
  */
 public OtpErlangBoolean(OtpInputStream buf) : base(buf)
 {
 }
Ejemplo n.º 40
0
 /**
  * Create an atom from a stream containing an atom encoded in Erlang
  * external format.
  */
 public OtpErlangAtom(OtpInputStream buf) => Value = buf.ReadAtom();
Ejemplo n.º 41
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)
 {
 }
Ejemplo n.º 42
0
        public override void Run()
        {
            if (!Connected)
            {
                Deliver(new IOException("Not connected"));
                return;
            }

            byte[] lbuf = new byte[4];
            int    len;

            try
            {
                while (!Stopping)
                {
                    // 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!

                    // read 4 bytes - get length of incoming packet
                    ReadSock(lbuf);
                    OtpInputStream ibuf = new OtpInputStream(lbuf)
                    {
                        Flags = Local.Flags
                    };
                    len = ibuf.Read4BE();

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

                        continue;
                    }

                    // got a real message (maybe) - read len bytes
                    byte[] tmpbuf = new byte[len];
                    ReadSock(tmpbuf);
                    ibuf = new OtpInputStream(tmpbuf)
                    {
                        Flags = Local.Flags
                    };

                    if (ibuf.Read1() != passThrough)
                    {
                        continue;
                    }

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

                    // decode the header
                    tmp = ibuf.ReadAny();
                    if (!(tmp is OtpErlangTuple))
                    {
                        continue;
                    }

                    head = (OtpErlangTuple)tmp;
                    if (!(head.ElementAt(0) is OtpErlangLong))
                    {
                        continue;
                    }

                    // 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))
                            {
                                continue;
                            }

                            cookie = (OtpErlangAtom)head.ElementAt(1);
                            if (sendCookie)
                            {
                                if (cookie.Value != Local.Cookie)
                                {
                                    CookieError(Local, cookie);
                                }
                            }
                            else
                            {
                                if (cookie.Value != "")
                                {
                                    CookieError(Local, cookie);
                                }
                            }
                            cookieOk = true;
                        }

                        if (TraceLevel >= TraceSend)
                        {
                            Logger.Debug($"<- {HeaderType(head)} {head}");

                            /* show received payload too */
                            ibuf.Mark();
                            Logger.Debug($"   {ibuf.ReadAny()}");
                            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))
                            {
                                continue;
                            }

                            cookie = (OtpErlangAtom)head.ElementAt(2);
                            if (sendCookie)
                            {
                                if (cookie.Value != Local.Cookie)
                                {
                                    CookieError(Local, cookie);
                                }
                            }
                            else
                            {
                                if (cookie.Value != "")
                                {
                                    CookieError(Local, cookie);
                                }
                            }

                            cookieOk = true;
                        }

                        if (TraceLevel >= TraceSend)
                        {
                            Logger.Debug($"<- {HeaderType(head)} {head}");

                            /* show received payload too */
                            ibuf.Mark();
                            Logger.Debug($"   {ibuf.ReadAny()}");
                            ibuf.Reset();
                        }

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

                        Deliver(new OtpMsg(from, toName.Value, ibuf));
                        break;

                    case exitTag:      // { EXIT, FromPid, ToPid, Reason }
                    case exit2Tag:     // { EXIT2, FromPid, ToPid, Reason }
                        if (head.ElementAt(3) == null)
                        {
                            continue;
                        }

                        if (TraceLevel >= TraceCTRL)
                        {
                            Logger.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)
                        {
                            continue;
                        }

                        if (TraceLevel >= TraceCTRL)
                        {
                            Logger.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 >= TraceCTRL)
                        {
                            Logger.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}
                        // (just show trace)
                        if (TraceLevel >= TraceCTRL)
                        {
                            Logger.Debug("<- " + HeaderType(head) + " " + head);
                        }
                        break;

                    default:
                        // garbage?
                        break;
                    }
                }

                // this section reachable only with break
                // we have received garbage from peer
                Deliver(new OtpExit("Remote is sending garbage"));
            }
            catch (OtpAuthException e)
            {
                Deliver(e);
            }
            catch (OtpDecodeException)
            {
                Deliver(new OtpExit("Remote is sending garbage"));
            }
            catch (IOException)
            {
                Deliver(new OtpExit("Remote has closed connection"));
            }
            finally
            {
                Close();
            }
        }
Ejemplo n.º 43
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)
 {
 }
Ejemplo n.º 44
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;
            }
Ejemplo n.º 45
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 char.
  */
 public OtpErlangChar(OtpInputStream buf)
     : base(buf)
 {
 }
Ejemplo n.º 46
0
 /**
  * Create a bitstr from a stream containing a bitstr encoded in Erlang
  * external format.
  */
 public OtpErlangBitstr(OtpInputStream buf)
 {
     Bin     = buf.ReadBitstr(out int padBits);
     PadBits = padBits;
     CheckBitstr(Bin, PadBits);
 }
Ejemplo n.º 47
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;
            }
        }
Ejemplo n.º 48
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();

            val = OtpInputStream.byte_array_to_long(b, false);
        }
Ejemplo n.º 49
0
 /**
  * Create a binary from a stream containing a binary encoded in Erlang
  * external format.
  */
 public OtpErlangBinary(OtpInputStream buf) : base() => Bin = buf.ReadBinary();
Ejemplo n.º 50
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);
        }
Ejemplo n.º 51
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);
        }
Ejemplo n.º 52
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);
        }
Ejemplo n.º 53
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;
            }
Ejemplo n.º 54
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
        }
Ejemplo n.º 55
0
        protected int RecvName()
        {
            int    nameTag;
            string hisname;

            try
            {
                OtpInputStream ibuf = Read2BytePackage();
                nameTag = ibuf.Read1();
                switch (nameTag)
                {
                case 'n':
                    Peer.DistLow = Peer.DistHigh = ibuf.Read2BE();
                    if (Peer.DistLow != 5)
                    {
                        throw new IOException("Invalid handshake version");
                    }
                    Peer.CapFlags = ibuf.Read4BE();
                    hisname       = ibuf.ReadStringData((int)ibuf.Length - 7);
                    break;

                case 'N':
                    Peer.DistLow  = Peer.DistHigh = 6;
                    Peer.CapFlags = ibuf.Read8BE();
                    if ((Peer.CapFlags & AbstractNode.dFlagHandshake23) == 0)
                    {
                        throw new IOException("Missing DFLAG_HANDSHAKE_23");
                    }
                    Peer.Creation = ibuf.Read4BE();
                    hisname       = ibuf.ReadStringData();
                    break;

                default:
                    throw new IOException("Unknown remote node type");
                }

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

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

            Peer.Node = hisname;
            if (Peer.Alive == null || Peer.Host == null)
            {
                throw new IOException("Handshake failed - peer name invalid: " + hisname);
            }

            if (TraceLevel >= TraceHandshake)
            {
                Logger.Debug($"<- HANDSHAKE ntype={Peer.Type} dist={Peer.DistHigh} remote={Peer}");
            }

            return(nameTag);
        }
Ejemplo n.º 56
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);
        }
Ejemplo n.º 57
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();
 }
Ejemplo n.º 58
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");
            }
        }
Ejemplo n.º 59
0
 /**
  * Create an Erlang float from a stream containing a double 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 float.
  */
 public OtpErlangDouble(OtpInputStream buf)
 {
     d = buf.read_double();
 }
Ejemplo n.º 60
0
            public override void run()
            {
                byte[] lbuf = new byte[2];
                OtpInputStream ibuf;
                int len;

                try
                {
                    while (!done)
                    {
                        readSock(sock, lbuf);
                        ibuf = new OtpInputStream(lbuf, 0);
                        len = ibuf.read2BE();
                        byte[] tmpbuf = new byte[len];
                        readSock(sock, tmpbuf);
                        ibuf = new OtpInputStream(tmpbuf, 0);

                        int request = ibuf.read1();
                        switch (request)
                        {
                            case publish4req:
                                r4_publish(sock, ibuf);
                                break;

                            case port4req:
                                r4_port(sock, ibuf);
                                done = true;
                                break;

                            case names4req:
                                r4_names(sock, ibuf);
                                done = true;
                                break;

                            case stopReq:
                                break;

                            default:
                                log.InfoFormat("[OtpEpmd] Unknown request (request={0}, length={1}) from {2}",
                                           request, len, sock.Client.RemoteEndPoint);
                                break;
                        }
                    }
                }
                catch (IOException)
                {
                }
                finally
                {
                    quit();
                }
            }