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.

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