internal async Task <OtpInputStream> ReadRequestAsync(IOtpTransport sock)
        {
            OtpInputStream ibuf = await sock.ReadAsync(2);

            ibuf = await sock.ReadAsync(ibuf.Read2BE());

            return(ibuf);
        }
        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);
            }
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
0
        protected OtpInputStream Read2BytePackage()
        {
            OtpInputStream ibuf = new OtpInputStream(ReadSock(2));

            return(new OtpInputStream(ReadSock(ibuf.Read2BE())));
        }