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.

226 lines
5.3 KiB

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