Beispiel #1
0
        protected void RecvChallengeAck(int our_challenge)
        {
            byte[] peer_digest;
            try
            {
                OtpInputStream ibuf = Read2BytePackage();
                int            tag  = ibuf.Read1();
                if (tag != ChallengeAck)
                {
                    throw new IOException("Handshake protocol error");
                }
                peer_digest = ibuf.ReadN(16);
                byte[] our_digest = GenDigest(our_challenge, Local.Cookie);
                if (!peer_digest.SequenceEqual(our_digest))
                {
                    throw new OtpAuthException("Peer authentication error.");
                }
            }
            catch (OtpDecodeException e)
            {
                throw new IOException("Handshake failed - not enough data", e);
            }
            catch (Exception e)
            {
                Logger.Error("Peer authentication error", e);
                throw new OtpAuthException("Peer authentication error.", e);
            }

            if (TraceLevel >= TraceHandshake)
            {
                Logger.Debug($"<- HANDSHAKE recvChallengeAck from={Peer.Node} digest={Hex(peer_digest)} local={Local}");
            }
        }
        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);
            }
        }