Style tweaks and some *IDG->IGD in UPnP code

This commit is contained in:
Jakob Borg 2014-12-08 17:07:55 +01:00
parent febeed3277
commit 25e363c5fb
3 changed files with 15 additions and 15 deletions

View File

@ -755,7 +755,7 @@ func setupUPnP() {
if len(igds) > 0 { if len(igds) > 0 {
// Configure the first discovered IGD only. This is a work-around until we have a better mechanism // 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 // for handling multiple IGDs, which will require changes to the global discovery service
igd = igds[0] igd = &igds[0]
externalPort = setupExternalPort(igd, port) externalPort = setupExternalPort(igd, port)
if externalPort == 0 { if externalPort == 0 {
@ -803,7 +803,7 @@ func renewUPnP(port int) {
if len(igds) > 0 { if len(igds) > 0 {
// Configure the first discovered IGD only. This is a work-around until we have a better mechanism // 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 // for handling multiple IGDs, which will require changes to the global discovery service
igd = igds[0] igd = &igds[0]
} else { } else {
if debugNet { if debugNet {
l.Debugln("Failed to discover IGD during UPnP port mapping renewal.") l.Debugln("Failed to discover IGD during UPnP port mapping renewal.")

View File

@ -101,8 +101,8 @@ type upnpRoot struct {
// Discover discovers UPnP InternetGatewayDevices. // Discover discovers UPnP InternetGatewayDevices.
// The order in which the devices appear in the result list is not deterministic. // The order in which the devices appear in the result list is not deterministic.
func Discover() []*IGD { func Discover() []IGD {
result := make([]*IGD, 0) var result []IGD
l.Infoln("Starting UPnP discovery...") l.Infoln("Starting UPnP discovery...")
timeout := 3 timeout := 3
@ -137,7 +137,7 @@ func Discover() []*IGD {
// Search for UPnP InternetGatewayDevices for <timeout> seconds, ignoring responses from any devices listed in knownDevices. // Search for UPnP InternetGatewayDevices for <timeout> seconds, ignoring responses from any devices listed in knownDevices.
// The order in which the devices appear in the result list is not deterministic // 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} ssdp := &net.UDPAddr{IP: []byte{239, 255, 255, 250}, Port: 1900}
tpl := `M-SEARCH * HTTP/1.1 tpl := `M-SEARCH * HTTP/1.1
@ -155,8 +155,8 @@ Mx: %d
l.Debugln("Starting discovery of device type " + deviceType + "...") l.Debugln("Starting discovery of device type " + deviceType + "...")
} }
results := make([]*IGD, 0) var results []IGD
resultChannel := make(chan *IGD, 8) resultChannel := make(chan IGD, 8)
socket, err := net.ListenUDP("udp4", &net.UDPAddr{}) socket, err := net.ListenUDP("udp4", &net.UDPAddr{})
if err != nil { if err != nil {
@ -231,7 +231,7 @@ Mx: %d
return results 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 defer resultWaitGroup.Done() // Signal when we've finished processing
if debug { if debug {
@ -321,7 +321,7 @@ func handleSearchResponse(deviceType string, knownDevices []*IGD, resp []byte, l
return return
} }
igd := &IGD{ igd := IGD{
uuid: deviceUUID, uuid: deviceUUID,
friendlyName: upnpRoot.Device.FriendlyName, friendlyName: upnpRoot.Device.FriendlyName,
url: deviceDescriptionURL, url: deviceDescriptionURL,
@ -352,7 +352,7 @@ func localIP(url *url.URL) (string, error) {
} }
func getChildDevices(d upnpDevice, deviceType string) []upnpDevice { func getChildDevices(d upnpDevice, deviceType string) []upnpDevice {
result := make([]upnpDevice, 0) var result []upnpDevice
for _, dev := range d.Devices { for _, dev := range d.Devices {
if dev.DeviceType == deviceType { if dev.DeviceType == deviceType {
result = append(result, dev) result = append(result, dev)
@ -362,7 +362,7 @@ func getChildDevices(d upnpDevice, deviceType string) []upnpDevice {
} }
func getChildServices(d upnpDevice, serviceType string) []upnpService { func getChildServices(d upnpDevice, serviceType string) []upnpService {
result := make([]upnpService, 0) var result []upnpService
for _, svc := range d.Services { for _, svc := range d.Services {
if svc.ServiceType == serviceType { if svc.ServiceType == serviceType {
result = append(result, svc) result = append(result, svc)
@ -372,7 +372,7 @@ func getChildServices(d upnpDevice, serviceType string) []upnpService {
} }
func getServiceDescriptions(rootURL string, device upnpDevice) ([]IGDService, error) { 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" { if device.DeviceType == "urn:schemas-upnp-org:device:InternetGatewayDevice:1" {
descriptions := getIGDServices(rootURL, device, 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 { func getIGDServices(rootURL string, device upnpDevice, wanDeviceURN string, wanConnectionURN string, serviceURNs []string) []IGDService {
result := make([]IGDService, 0) var result []IGDService
devices := getChildDevices(device, wanDeviceURN) devices := getChildDevices(device, wanDeviceURN)

View File

@ -21,7 +21,7 @@ import (
) )
func TestExternalIPParsing(t *testing.T) { func TestExternalIPParsing(t *testing.T) {
soap_response := soapResponse :=
[]byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> []byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body> <s:Body>
<u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"> <u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
@ -31,7 +31,7 @@ func TestExternalIPParsing(t *testing.T) {
</s:Envelope>`) </s:Envelope>`)
envelope := &soapGetExternalIPAddressResponseEnvelope{} envelope := &soapGetExternalIPAddressResponseEnvelope{}
err := xml.Unmarshal(soap_response, envelope) err := xml.Unmarshal(soapResponse, envelope)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }