예제 #1
0
        public I2CPSession(SessionStatusMessage msg, I2PDestination mydest)
        {
            SessionId = msg.SessionId;

            //MyLeaseInfo = new I2PLeaseInfo( I2PSigningKey.SigningKeyTypes.DSA_SHA1 );
            MyDestination = mydest;
        }
예제 #2
0
 internal override I2CPState MessageReceived(I2CPMessage msg)
 {
     if (msg.MessageType == I2CPMessage.ProtocolMessageType.CreateSession)
     {
         var reply = new SessionStatusMessage(1, SessionStatusMessage.SessionStates.Created);
         Session.Send(reply);
         return(new WaitForEstablishedDestinationState(Session));
     }
     return(this);
 }
예제 #3
0
 void Connection_ReceivedSessionStatusMessage(SessionStatusMessage msg)
 {
 }
예제 #4
0
        internal override I2CPState MessageReceived(I2CPMessage msg)
        {
            try
            {
                switch (msg)
                {
                case CreateSessionMessage csm:
                    Logging.LogDebug($"{this}: Received message {csm}.");

                    var signok = I2PSignature.DoVerify(
                        csm.Config.Destination.SigningPublicKey,
                        csm.Config.Signature,
                        csm.Config.SignedBuf);

                    if (!signok)
                    {
                        Logging.LogWarning($"{this} CreateSessionMessage: Signature check failed.");
                        Session.Send(new SessionStatusMessage(0, SessionStates.Invalid));
                        return(this);
                    }

                    var newdest = Router.CreateDestination(
                        csm.Config.Destination,
                        null,
                        !csm.Config.DontPublishLeaseSet,
                        out var alreadyrunning);

                    if (alreadyrunning || newdest is null)
                    {
                        Logging.LogWarning($"{this}: Destination already running. {csm}");
                        Session.Send(new SessionStatusMessage(0, SessionStates.Refused));
                        return(this);
                    }

                    var newsession = Session.GenerateNewSessionId();
                    newsession.MyDestination = newdest;

                    newsession.Config = csm.Config;
                    UpdateConfiguration(newsession, csm.Config);

                    Session.AttachDestination(newsession.MyDestination);

                    Logging.LogDebug($"{this}: Creating session {newsession.SessionId}.");

                    var reply = new SessionStatusMessage(newsession.SessionId, SessionStates.Created);
                    Session.Send(reply);

                    Session.SendPendingLeaseUpdates(true);
                    break;

                case ReconfigureSessionMessage rcm:
                    var rcms = Session.SessionIds[rcm.SessionId];
                    rcms.Config = rcm.Config;
                    UpdateConfiguration(rcms, rcms.Config);
                    break;

                case CreateLeaseSetMessage clsm:
                    Logging.LogDebug($"{this}: {clsm} {clsm.PrivateKey}");

                    var s = Session.SessionIds[clsm.SessionId];
                    s.PrivateKey = clsm.PrivateKey;
                    s.MyDestination.TemporaryPrivateKey = clsm.PrivateKey;

                    s.LeaseInfo = clsm.Info;
                    s.MyDestination.SignedLeases = clsm.Leases;

                    Session.SendPendingLeaseUpdates(true);
                    break;

                case DestLookupMessage dlum:
                    Logging.LogDebug($"{this}: {dlum} {dlum.Ident.Id32Short}");

                    Session
                    .SessionIds
                    .First()
                    .Value
                    .MyDestination
                    .LookupDestination(dlum.Ident, HandleDestinationLookupResult, null);
                    break;

                case HostLookupMessage hlum:
                    Logging.LogDebug($"{this}: {hlum} {hlum.SessionId} {hlum.RequestId} {hlum.Hash?.Id32Short}");

                    I2PIdentHash lookuphash;

                    if (hlum.RequestType == HostLookupMessage.HostLookupTypes.HostName)
                    {
                        lookuphash = ParseHostName(hlum);

                        if (lookuphash is null)
                        {
                            Session.Send(new HostReplyMessage(
                                             hlum.SessionId,
                                             hlum.RequestId,
                                             HostLookupResults.Failure));
                            break;
                        }
                    }
                    else
                    {
                        lookuphash = hlum.Hash;
                    }

                    if (hlum.SessionId == 0xFFFF)
                    {
                        Router.LookupDestination(
                            lookuphash,
                            HandleHostLookupResult,
                            new HostLookupInfo {
                            RequestId = hlum.RequestId, SessionId = hlum.SessionId
                        });
                    }
                    else
                    {
                        var s2 = Session.SessionIds[hlum.SessionId];

                        s2.MyDestination.LookupDestination(
                            lookuphash,
                            HandleHostLookupResult,
                            new HostLookupInfo {
                            RequestId = hlum.RequestId, SessionId = hlum.SessionId
                        });
                    }
                    break;

                case SendMessageMessage smm:
                    Logging.LogDebugData($"{this}: {smm} {smm.Destination.IdentHash.Id32Short} {smm.Payload}");

                    SendMessageToDestination(
                        smm.Destination,
                        smm.SessionId,
                        smm.Payload,
                        smm.Nonce);
                    break;

                case SendMessageExpiresMessage smem:
                    Logging.LogDebugData($"{this}: {smem} {smem.Destination.IdentHash.Id32Short} {(PayloadFormat)smem.Payload[9]} {smem.Payload}");

                    SendMessageToDestination(
                        smem.Destination,
                        smem.SessionId,
                        smem.Payload,
                        smem.Nonce);
                    break;

                case DestroySessionMessage dsm:
                    Logging.LogDebug($"{this}: {dsm}");
                    Session.Send(new SessionStatusMessage(dsm.SessionId, SessionStates.Destroyed));
                    return(null);

                default:
                    Logging.LogWarning($"{this}: Unhandled message {msg}");
                    break;
                }
                return(this);
            }
            catch (Exception ex)
            {
                Logging.LogWarning($"{this} MessageReceived: {msg.MessageType} {ex}");
                throw;
            }
        }