Browse Source

add a LocalAddr to the Session

Marten Seemann 1 year ago
parent
commit
34b688b3b9
8 changed files with 28 additions and 0 deletions
  1. 1 0
      benchmark_test.go
  2. 5 0
      conn.go
  3. 9 0
      conn_test.go
  4. 3 0
      h2quic/server_test.go
  5. 2 0
      interface.go
  6. 3 0
      server_test.go
  7. 4 0
      session.go
  8. 1 0
      session_test.go

+ 1 - 0
benchmark_test.go

@@ -63,6 +63,7 @@ func (c *linkedConnection) Write(p []byte) error {
 
 func (c *linkedConnection) Read(p []byte) (int, net.Addr, error) { panic("not implemented") }
 func (*linkedConnection) SetCurrentRemoteAddr(addr net.Addr)     {}
+func (*linkedConnection) LocalAddr() net.Addr                    { panic("not implemented") }
 func (*linkedConnection) RemoteAddr() net.Addr                   { return &net.UDPAddr{} }
 func (c *linkedConnection) Close() error                         { return nil }
 

+ 5 - 0
conn.go

@@ -9,6 +9,7 @@ type connection interface {
 	Write([]byte) error
 	Read([]byte) (int, net.Addr, error)
 	Close() error
+	LocalAddr() net.Addr
 	RemoteAddr() net.Addr
 	SetCurrentRemoteAddr(net.Addr)
 }
@@ -37,6 +38,10 @@ func (c *conn) SetCurrentRemoteAddr(addr net.Addr) {
 	c.mutex.Unlock()
 }
 
+func (c *conn) LocalAddr() net.Addr {
+	return c.pconn.LocalAddr()
+}
+
 func (c *conn) RemoteAddr() net.Addr {
 	c.mutex.RLock()
 	addr := c.currentAddr

+ 9 - 0
conn_test.go

@@ -82,6 +82,15 @@ var _ = Describe("Connection", func() {
 		Expect(c.RemoteAddr().String()).To(Equal("192.168.100.200:1337"))
 	})
 
+	It("gets the local address", func() {
+		addr := &net.UDPAddr{
+			IP:   net.IPv4(192, 168, 0, 1),
+			Port: 1234,
+		}
+		packetConn.addr = addr
+		Expect(c.LocalAddr()).To(Equal(addr))
+	})
+
 	It("changes the remote address", func() {
 		addr := &net.UDPAddr{
 			IP:   net.IPv4(127, 0, 0, 1),

+ 3 - 0
h2quic/server_test.go

@@ -56,6 +56,9 @@ func (s *mockSession) Close(e error) error {
 	s.closedWithError = e
 	return nil
 }
+func (s *mockSession) LocalAddr() net.Addr {
+	panic("not implemented")
+}
 func (s *mockSession) RemoteAddr() net.Addr {
 	return &net.UDPAddr{IP: []byte{127, 0, 0, 1}, Port: 42}
 }

+ 2 - 0
interface.go

@@ -30,6 +30,8 @@ type Session interface {
 	// OpenStreamSync opens a new QUIC stream, blocking until the peer's concurrent stream limit allows a new stream to be opened.
 	// It always picks the smallest possible stream ID.
 	OpenStreamSync() (Stream, error)
+	// LocalAddr returns the local address.
+	LocalAddr() net.Addr
 	// RemoteAddr returns the address of the peer.
 	RemoteAddr() net.Addr
 	// Close closes the connection. The error will be sent to the remote peer in a CONNECTION_CLOSE frame. An error value of nil is allowed and will cause a normal PeerGoingAway to be sent.

+ 3 - 0
server_test.go

@@ -42,6 +42,9 @@ func (s *mockSession) OpenStream() (Stream, error) {
 func (s *mockSession) OpenStreamSync() (Stream, error) {
 	panic("not implemented")
 }
+func (s *mockSession) LocalAddr() net.Addr {
+	panic("not implemented")
+}
 func (s *mockSession) RemoteAddr() net.Addr {
 	panic("not implemented")
 }

+ 4 - 0
session.go

@@ -814,6 +814,10 @@ func (s *session) ackAlarmChanged(t time.Time) {
 	s.maybeResetTimer()
 }
 
+func (s *session) LocalAddr() net.Addr {
+	return s.conn.LocalAddr()
+}
+
 // RemoteAddr returns the net.Addr of the client
 func (s *session) RemoteAddr() net.Addr {
 	return s.conn.RemoteAddr()

+ 1 - 0
session_test.go

@@ -39,6 +39,7 @@ func (m *mockConnection) Read([]byte) (int, net.Addr, error) { panic("not implem
 func (m *mockConnection) SetCurrentRemoteAddr(addr net.Addr) {
 	m.remoteAddr = addr
 }
+func (*mockConnection) LocalAddr() net.Addr  { panic("not implemented") }
 func (*mockConnection) RemoteAddr() net.Addr { return &net.UDPAddr{} }
 func (*mockConnection) Close() error         { panic("not implemented") }