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.

212 lines
4.7 KiB

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