Exemplo n.º 1
0
        protected void RecvStatus()
        {
            try
            {
                OtpInputStream ibuf = Read2BytePackage();
                int            tag  = ibuf.Read1();
                if (tag != ChallengeStatus)
                {
                    throw new IOException("Handshake protocol error");
                }
                string status = ibuf.ReadStringData((int)ibuf.Length - 1);
                if (status.CompareTo("ok") != 0)
                {
                    throw new IOException("Peer replied with status '" + status + "' instead of 'ok'");
                }
            }
            catch (OtpDecodeException e)
            {
                throw new IOException("Handshake failed - not enough data", e);
            }

            if (TraceLevel >= TraceHandshake)
            {
                Logger.Debug($"<- HANDSHAKE recvStatus (ok) local={Local}");
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }