mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-06-07 01:57:36 -04:00
Expose WriteMessageToConn and start work on sessions
This commit is contained in:
@ -69,7 +69,7 @@ func (s *NTCP2Session) PerformIncomingHandshake(conn net.Conn) error {
|
||||
}
|
||||
|
||||
// Write SessionCreated to connection
|
||||
if err := s.writeMessageToConn(
|
||||
if err := s.WriteMessageToConn(
|
||||
conn,
|
||||
obfuscatedKey,
|
||||
encryptedPayload,
|
||||
|
@ -48,7 +48,7 @@ func (s *NTCP2Session) PerformOutboundHandshake(conn net.Conn) error {
|
||||
}
|
||||
|
||||
// Write complete SessionRequest message to connection
|
||||
if err := s.writeMessageToConn(conn, obfuscatedKey, encryptedPayload, requestProcessor.GetPadding(msg)); err != nil {
|
||||
if err := s.WriteMessageToConn(conn, obfuscatedKey, encryptedPayload, requestProcessor.GetPadding(msg)); err != nil {
|
||||
return oops.Errorf("failed to write session request: %w", err)
|
||||
}
|
||||
|
||||
@ -97,7 +97,7 @@ func (s *NTCP2Session) PerformOutboundHandshake(conn net.Conn) error {
|
||||
}
|
||||
|
||||
// Write SessionConfirmed to connection
|
||||
if err := s.writeMessageToConn(
|
||||
if err := s.WriteMessageToConn(
|
||||
conn,
|
||||
staticKey[:],
|
||||
encryptedConfirmedPayload,
|
||||
@ -109,30 +109,3 @@ func (s *NTCP2Session) PerformOutboundHandshake(conn net.Conn) error {
|
||||
// Handshake complete, mark session as established
|
||||
return s.HandshakeState.CompleteHandshake()
|
||||
}
|
||||
|
||||
// Helper to write message parts to connection
|
||||
func (s *NTCP2Session) writeMessageToConn(conn net.Conn, obfuscatedKey, encryptedPayload, padding []byte) error {
|
||||
// Calculate total size
|
||||
totalSize := len(obfuscatedKey) + len(encryptedPayload)
|
||||
if padding != nil {
|
||||
totalSize += len(padding)
|
||||
}
|
||||
|
||||
// Create buffer and copy data
|
||||
message := make([]byte, totalSize)
|
||||
offset := 0
|
||||
|
||||
copy(message[offset:], obfuscatedKey)
|
||||
offset += len(obfuscatedKey)
|
||||
|
||||
copy(message[offset:], encryptedPayload)
|
||||
offset += len(encryptedPayload)
|
||||
|
||||
if padding != nil {
|
||||
copy(message[offset:], padding)
|
||||
}
|
||||
|
||||
// Write to connection
|
||||
_, err := conn.Write(message)
|
||||
return err
|
||||
}
|
||||
|
@ -6,7 +6,10 @@ package ntcp
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/common/data"
|
||||
"github.com/go-i2p/go-i2p/lib/common/router_info"
|
||||
"github.com/go-i2p/go-i2p/lib/transport"
|
||||
"github.com/go-i2p/go-i2p/lib/transport/noise"
|
||||
@ -28,6 +31,17 @@ type NTCP2Transport struct {
|
||||
*noise.NoiseTransport
|
||||
*sntp.RouterTimestamper
|
||||
transportStyle string
|
||||
|
||||
activeSessions map[data.Hash]*NTCP2Session
|
||||
activeSessionsLock sync.RWMutex
|
||||
|
||||
// Configuration
|
||||
dialTimeout time.Duration
|
||||
readTimeout time.Duration
|
||||
writeTimeout time.Duration
|
||||
maxQueueSize int
|
||||
minDesiredSessions int
|
||||
maxSessions int
|
||||
}
|
||||
|
||||
func (t *NTCP2Transport) Name() string {
|
||||
|
@ -97,3 +97,30 @@ func Intn(n int) int {
|
||||
}
|
||||
return int(cryptoRand.Int64())
|
||||
}
|
||||
|
||||
// Helper to write message parts to connection
|
||||
func (s *NTCP2Session) WriteMessageToConn(conn net.Conn, obfuscatedKey, encryptedPayload, padding []byte) error {
|
||||
// Calculate total size
|
||||
totalSize := len(obfuscatedKey) + len(encryptedPayload)
|
||||
if padding != nil {
|
||||
totalSize += len(padding)
|
||||
}
|
||||
|
||||
// Create buffer and copy data
|
||||
message := make([]byte, totalSize)
|
||||
offset := 0
|
||||
|
||||
copy(message[offset:], obfuscatedKey)
|
||||
offset += len(obfuscatedKey)
|
||||
|
||||
copy(message[offset:], encryptedPayload)
|
||||
offset += len(encryptedPayload)
|
||||
|
||||
if padding != nil {
|
||||
copy(message[offset:], padding)
|
||||
}
|
||||
|
||||
// Write to connection
|
||||
_, err := conn.Write(message)
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user