1package yamux
2
3import (
4	"fmt"
5	"net"
6)
7
8// hasAddr is used to get the address from the underlying connection
9type hasAddr interface {
10	LocalAddr() net.Addr
11	RemoteAddr() net.Addr
12}
13
14// yamuxAddr is used when we cannot get the underlying address
15type yamuxAddr struct {
16	Addr string
17}
18
19func (*yamuxAddr) Network() string {
20	return "yamux"
21}
22
23func (y *yamuxAddr) String() string {
24	return fmt.Sprintf("yamux:%s", y.Addr)
25}
26
27// Addr is used to get the address of the listener.
28func (s *Session) Addr() net.Addr {
29	return s.LocalAddr()
30}
31
32// LocalAddr is used to get the local address of the
33// underlying connection.
34func (s *Session) LocalAddr() net.Addr {
35	addr, ok := s.conn.(hasAddr)
36	if !ok {
37		return &yamuxAddr{"local"}
38	}
39	return addr.LocalAddr()
40}
41
42// RemoteAddr is used to get the address of remote end
43// of the underlying connection
44func (s *Session) RemoteAddr() net.Addr {
45	addr, ok := s.conn.(hasAddr)
46	if !ok {
47		return &yamuxAddr{"remote"}
48	}
49	return addr.RemoteAddr()
50}
51
52// LocalAddr returns the local address
53func (s *Stream) LocalAddr() net.Addr {
54	return s.session.LocalAddr()
55}
56
57// RemoteAddr returns the remote address
58func (s *Stream) RemoteAddr() net.Addr {
59	return s.session.RemoteAddr()
60}
61