You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
4.7 KiB

11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. "sync/atomic"
  7. . "github.com/tendermint/tendermint/common"
  8. "github.com/tendermint/tendermint/p2p/upnp"
  9. )
  10. type Listener interface {
  11. Connections() <-chan net.Conn
  12. InternalAddress() *NetAddress
  13. ExternalAddress() *NetAddress
  14. String() string
  15. Stop()
  16. }
  17. // Implements Listener
  18. type DefaultListener struct {
  19. listener net.Listener
  20. intAddr *NetAddress
  21. extAddr *NetAddress
  22. connections chan net.Conn
  23. stopped uint32
  24. }
  25. const (
  26. numBufferedConnections = 10
  27. defaultExternalPort = 8770
  28. )
  29. func splitHostPort(addr string) (host string, port int) {
  30. host, portStr, err := net.SplitHostPort(addr)
  31. if err != nil {
  32. panic(err)
  33. }
  34. port, err = strconv.Atoi(portStr)
  35. if err != nil {
  36. panic(err)
  37. }
  38. return host, port
  39. }
  40. func NewDefaultListener(protocol string, lAddr string, requireUPNPHairpin bool) Listener {
  41. // Local listen IP & port
  42. lAddrIP, lAddrPort := splitHostPort(lAddr)
  43. // Create listener
  44. listener, err := net.Listen(protocol, lAddr)
  45. if err != nil {
  46. panic(err)
  47. }
  48. // Actual listener local IP & port
  49. listenerIP, listenerPort := splitHostPort(listener.Addr().String())
  50. log.Debug("Local listener", "ip", listenerIP, "port", listenerPort)
  51. // Determine internal address...
  52. var intAddr *NetAddress = NewNetAddressString(lAddr)
  53. // Determine external address...
  54. var extAddr *NetAddress
  55. // If the lAddrIP is INADDR_ANY, try UPnP
  56. if lAddrIP == "" || lAddrIP == "0.0.0.0" {
  57. if requireUPNPHairpin {
  58. upnpCapabilities, err := upnp.Probe()
  59. if err != nil {
  60. log.Warn("Failed to probe UPNP", "error", err)
  61. goto SKIP_UPNP
  62. }
  63. if !upnpCapabilities.Hairpin {
  64. goto SKIP_UPNP
  65. }
  66. }
  67. extAddr = getUPNPExternalAddress(lAddrPort, listenerPort)
  68. }
  69. SKIP_UPNP:
  70. // Otherwise just use the local address...
  71. if extAddr == nil {
  72. extAddr = getNaiveExternalAddress(listenerPort)
  73. }
  74. if extAddr == nil {
  75. panic("Could not determine external address!")
  76. }
  77. dl := &DefaultListener{
  78. listener: listener,
  79. intAddr: intAddr,
  80. extAddr: extAddr,
  81. connections: make(chan net.Conn, numBufferedConnections),
  82. }
  83. go dl.listenRoutine()
  84. return dl
  85. }
  86. // Accept connections and pass on the channel
  87. // Reading from the channel blocks on the peerHandshake for each connection
  88. // Connection is ignored if we have too many connections to that ip range
  89. func (l *DefaultListener) listenRoutine() {
  90. for {
  91. conn, err := l.listener.Accept()
  92. if atomic.LoadUint32(&l.stopped) == 1 {
  93. break // Go to cleanup
  94. }
  95. // listener wasn't stopped,
  96. // yet we encountered an error.
  97. if err != nil {
  98. panic(err)
  99. }
  100. l.connections <- conn
  101. }
  102. // Cleanup
  103. close(l.connections)
  104. for _ = range l.connections {
  105. // Drain
  106. }
  107. }
  108. // A channel of inbound connections.
  109. // It gets closed when the listener closes.
  110. func (l *DefaultListener) Connections() <-chan net.Conn {
  111. return l.connections
  112. }
  113. func (l *DefaultListener) InternalAddress() *NetAddress {
  114. return l.intAddr
  115. }
  116. func (l *DefaultListener) ExternalAddress() *NetAddress {
  117. return l.extAddr
  118. }
  119. // NOTE: The returned listener is already Accept()'ing.
  120. // So it's not suitable to pass into http.Serve().
  121. func (l *DefaultListener) NetListener() net.Listener {
  122. return l.listener
  123. }
  124. func (l *DefaultListener) Stop() {
  125. if atomic.CompareAndSwapUint32(&l.stopped, 0, 1) {
  126. l.listener.Close()
  127. }
  128. }
  129. func (l *DefaultListener) String() string {
  130. return fmt.Sprintf("Listener(@%v)", l.extAddr)
  131. }
  132. /* external address helpers */
  133. // UPNP external address discovery & port mapping
  134. func getUPNPExternalAddress(externalPort, internalPort int) *NetAddress {
  135. log.Debug("Getting UPNP external address")
  136. nat, err := upnp.Discover()
  137. if err != nil {
  138. log.Debug("Could not perform UPNP discover", "error", err)
  139. return nil
  140. }
  141. ext, err := nat.GetExternalAddress()
  142. if err != nil {
  143. log.Debug("Could not get UPNP external address", "error", err)
  144. return nil
  145. }
  146. // UPnP can't seem to get the external port, so let's just be explicit.
  147. if externalPort == 0 {
  148. externalPort = defaultExternalPort
  149. }
  150. externalPort, err = nat.AddPortMapping("tcp", externalPort, internalPort, "tendermint", 0)
  151. if err != nil {
  152. log.Debug("Could not add UPNP port mapping", "error", err)
  153. return nil
  154. }
  155. log.Debug("Got UPNP external address", "address", ext)
  156. return NewNetAddressIPPort(ext, uint16(externalPort))
  157. }
  158. // TODO: use syscalls: http://pastebin.com/9exZG4rh
  159. func getNaiveExternalAddress(port int) *NetAddress {
  160. addrs, err := net.InterfaceAddrs()
  161. if err != nil {
  162. panic(Fmt("Could not fetch interface addresses: %v", err))
  163. }
  164. for _, a := range addrs {
  165. ipnet, ok := a.(*net.IPNet)
  166. if !ok {
  167. continue
  168. }
  169. v4 := ipnet.IP.To4()
  170. if v4 == nil || v4[0] == 127 {
  171. continue
  172. } // loopback
  173. return NewNetAddressIPPort(ipnet.IP, uint16(port))
  174. }
  175. return nil
  176. }