From 25e363c5fb0343d7409632e8b2249bfe1d091b58 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 8 Dec 2014 17:07:55 +0100 Subject: [PATCH] Style tweaks and some *IDG->IGD in UPnP code --- cmd/syncthing/main.go | 4 ++-- internal/upnp/upnp.go | 22 +++++++++++----------- internal/upnp/upnp_test.go | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 738d5d41..dd648ed2 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -755,7 +755,7 @@ func setupUPnP() { if len(igds) > 0 { // Configure the first discovered IGD only. This is a work-around until we have a better mechanism // for handling multiple IGDs, which will require changes to the global discovery service - igd = igds[0] + igd = &igds[0] externalPort = setupExternalPort(igd, port) if externalPort == 0 { @@ -803,7 +803,7 @@ func renewUPnP(port int) { if len(igds) > 0 { // Configure the first discovered IGD only. This is a work-around until we have a better mechanism // for handling multiple IGDs, which will require changes to the global discovery service - igd = igds[0] + igd = &igds[0] } else { if debugNet { l.Debugln("Failed to discover IGD during UPnP port mapping renewal.") diff --git a/internal/upnp/upnp.go b/internal/upnp/upnp.go index c1d9786f..8bb0367b 100644 --- a/internal/upnp/upnp.go +++ b/internal/upnp/upnp.go @@ -101,8 +101,8 @@ type upnpRoot struct { // Discover discovers UPnP InternetGatewayDevices. // The order in which the devices appear in the result list is not deterministic. -func Discover() []*IGD { - result := make([]*IGD, 0) +func Discover() []IGD { + var result []IGD l.Infoln("Starting UPnP discovery...") timeout := 3 @@ -137,7 +137,7 @@ func Discover() []*IGD { // Search for UPnP InternetGatewayDevices for seconds, ignoring responses from any devices listed in knownDevices. // The order in which the devices appear in the result list is not deterministic -func discover(deviceType string, timeout int, knownDevices []*IGD) []*IGD { +func discover(deviceType string, timeout int, knownDevices []IGD) []IGD { ssdp := &net.UDPAddr{IP: []byte{239, 255, 255, 250}, Port: 1900} tpl := `M-SEARCH * HTTP/1.1 @@ -155,8 +155,8 @@ Mx: %d l.Debugln("Starting discovery of device type " + deviceType + "...") } - results := make([]*IGD, 0) - resultChannel := make(chan *IGD, 8) + var results []IGD + resultChannel := make(chan IGD, 8) socket, err := net.ListenUDP("udp4", &net.UDPAddr{}) if err != nil { @@ -231,7 +231,7 @@ Mx: %d return results } -func handleSearchResponse(deviceType string, knownDevices []*IGD, resp []byte, length int, resultChannel chan<- *IGD, resultWaitGroup *sync.WaitGroup) { +func handleSearchResponse(deviceType string, knownDevices []IGD, resp []byte, length int, resultChannel chan<- IGD, resultWaitGroup *sync.WaitGroup) { defer resultWaitGroup.Done() // Signal when we've finished processing if debug { @@ -321,7 +321,7 @@ func handleSearchResponse(deviceType string, knownDevices []*IGD, resp []byte, l return } - igd := &IGD{ + igd := IGD{ uuid: deviceUUID, friendlyName: upnpRoot.Device.FriendlyName, url: deviceDescriptionURL, @@ -352,7 +352,7 @@ func localIP(url *url.URL) (string, error) { } func getChildDevices(d upnpDevice, deviceType string) []upnpDevice { - result := make([]upnpDevice, 0) + var result []upnpDevice for _, dev := range d.Devices { if dev.DeviceType == deviceType { result = append(result, dev) @@ -362,7 +362,7 @@ func getChildDevices(d upnpDevice, deviceType string) []upnpDevice { } func getChildServices(d upnpDevice, serviceType string) []upnpService { - result := make([]upnpService, 0) + var result []upnpService for _, svc := range d.Services { if svc.ServiceType == serviceType { result = append(result, svc) @@ -372,7 +372,7 @@ func getChildServices(d upnpDevice, serviceType string) []upnpService { } func getServiceDescriptions(rootURL string, device upnpDevice) ([]IGDService, error) { - result := make([]IGDService, 0) + var result []IGDService if device.DeviceType == "urn:schemas-upnp-org:device:InternetGatewayDevice:1" { descriptions := getIGDServices(rootURL, device, @@ -400,7 +400,7 @@ func getServiceDescriptions(rootURL string, device upnpDevice) ([]IGDService, er } func getIGDServices(rootURL string, device upnpDevice, wanDeviceURN string, wanConnectionURN string, serviceURNs []string) []IGDService { - result := make([]IGDService, 0) + var result []IGDService devices := getChildDevices(device, wanDeviceURN) diff --git a/internal/upnp/upnp_test.go b/internal/upnp/upnp_test.go index 2023b708..e7fc6b96 100644 --- a/internal/upnp/upnp_test.go +++ b/internal/upnp/upnp_test.go @@ -21,7 +21,7 @@ import ( ) func TestExternalIPParsing(t *testing.T) { - soap_response := + soapResponse := []byte(` @@ -31,7 +31,7 @@ func TestExternalIPParsing(t *testing.T) { `) envelope := &soapGetExternalIPAddressResponseEnvelope{} - err := xml.Unmarshal(soap_response, envelope) + err := xml.Unmarshal(soapResponse, envelope) if err != nil { t.Error(err) }