Beispiel #1
0
func NewDefaultListener(protocol string, lAddr string, requireUPNPHairpin bool) Listener {
	// Local listen IP & port
	lAddrIP, lAddrPort := splitHostPort(lAddr)

	// Create listener
	listener, err := net.Listen(protocol, lAddr)
	if err != nil {
		panic(err)
	}
	// Actual listener local IP & port
	listenerIP, listenerPort := splitHostPort(listener.Addr().String())
	log.Debug("Local listener", "ip", listenerIP, "port", listenerPort)

	// Determine internal address...
	var intAddr *NetAddress = NewNetAddressString(lAddr)

	// Determine external address...
	var extAddr *NetAddress
	// If the lAddrIP is INADDR_ANY, try UPnP
	if lAddrIP == "" || lAddrIP == "0.0.0.0" {
		if requireUPNPHairpin {
			upnpCapabilities, err := upnp.Probe()
			if err != nil {
				log.Warn("Failed to probe UPNP", "error", err)
				goto SKIP_UPNP
			}
			if !upnpCapabilities.Hairpin {
				goto SKIP_UPNP
			}
		}
		extAddr = getUPNPExternalAddress(lAddrPort, listenerPort)
	}
SKIP_UPNP:

	// Otherwise just use the local address...
	if extAddr == nil {
		extAddr = getNaiveExternalAddress(listenerPort)
	}
	if extAddr == nil {
		panic("Could not determine external address!")
	}

	dl := &DefaultListener{
		listener:    listener,
		intAddr:     intAddr,
		extAddr:     extAddr,
		connections: make(chan net.Conn, numBufferedConnections),
	}

	go dl.listenRoutine()

	return dl
}
Beispiel #2
0
func NewDefaultListener(protocol string, lAddr string, requireUPNPHairpin bool) Listener {
	// Local listen IP & port
	lAddrIP, lAddrPort := splitHostPort(lAddr)

	// Create listener
	var listener net.Listener
	var err error
	for i := 0; i < tryListenSeconds; i++ {
		listener, err = net.Listen(protocol, lAddr)
		if err == nil {
			break
		} else if i < tryListenSeconds-1 {
			time.Sleep(time.Second * 1)
		}
	}
	if err != nil {
		PanicCrisis(err)
	}
	// Actual listener local IP & port
	listenerIP, listenerPort := splitHostPort(listener.Addr().String())
	log.Info("Local listener", "ip", listenerIP, "port", listenerPort)

	// Determine internal address...
	var intAddr *NetAddress = NewNetAddressString(lAddr)

	// Determine external address...
	var extAddr *NetAddress
	// If the lAddrIP is INADDR_ANY, try UPnP
	if lAddrIP == "" || lAddrIP == "0.0.0.0" {
		if requireUPNPHairpin {
			upnpCapabilities, err := upnp.Probe()
			if err != nil {
				log.Warn("Failed to probe UPNP", "error", err)
				goto SKIP_UPNP
			}
			if !upnpCapabilities.Hairpin {
				goto SKIP_UPNP
			}
		}
		extAddr = getUPNPExternalAddress(lAddrPort, listenerPort)
	}
SKIP_UPNP:

	// Otherwise just use the local address...
	if extAddr == nil {
		extAddr = getNaiveExternalAddress(listenerPort)
	}
	if extAddr == nil {
		PanicCrisis("Could not determine external address!")
	}

	dl := &DefaultListener{
		listener:    listener,
		intAddr:     intAddr,
		extAddr:     extAddr,
		connections: make(chan net.Conn, numBufferedConnections),
	}
	dl.BaseService = *NewBaseService(log, "DefaultListener", dl)
	dl.Start() // Started upon construction
	return dl
}