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.

217 lines
4.8 KiB

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