From dad73be727d38e8304c1160b9034ad9941a865e8 Mon Sep 17 00:00:00 2001 From: mhhcbon Date: Mon, 21 Mar 2022 14:54:05 +0100 Subject: [PATCH] #48: Add local address information --- goupnp.go | 5 +++++ httpu/httpu.go | 5 +++++ service_client.go | 13 ++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/goupnp.go b/goupnp.go index 36081aa..ff91511 100644 --- a/goupnp.go +++ b/goupnp.go @@ -22,6 +22,7 @@ import ( "net/url" "time" + "github.com/huin/goupnp/httpu" "github.com/huin/goupnp/ssdp" ) @@ -63,6 +64,9 @@ type MaybeRootDevice struct { // the discovery of a device, regardless of if there was an error probing it. Location *url.URL + // The address from which the device was discovered (if known - otherwise nil). + LocalAddr string + // Any error encountered probing a discovered device. Err error } @@ -99,6 +103,7 @@ func DiscoverDevices(searchTarget string) ([]MaybeRootDevice, error) { } else { maybe.Root = root } + maybe.LocalAddr = response.Header.Get(httpu.LocalAddress) } return results, nil diff --git a/httpu/httpu.go b/httpu/httpu.go index 3367c86..a7a302d 100644 --- a/httpu/httpu.go +++ b/httpu/httpu.go @@ -143,9 +143,14 @@ func (httpu *HTTPUClient) Do( continue } + // Set the related local address used to discover the device. + response.Header.Add(LocalAddress, httpu.conn.LocalAddr().(*net.UDPAddr).IP.String()) + responses = append(responses, response) } // Timeout reached - return discovered responses. return responses, nil } + +const LocalAddress = "goupnp-local-address" diff --git a/service_client.go b/service_client.go index 9111c93..a52ff2a 100644 --- a/service_client.go +++ b/service_client.go @@ -17,6 +17,7 @@ type ServiceClient struct { RootDevice *RootDevice Location *url.URL Service *Service + localAddr string } // NewServiceClients discovers services, and returns clients for them. err will @@ -36,7 +37,7 @@ func NewServiceClients(searchTarget string) (clients []ServiceClient, errors []e continue } - deviceClients, err := NewServiceClientsFromRootDevice(maybeRootDevice.Root, maybeRootDevice.Location, searchTarget) + deviceClients, err := newServiceClientsFromRootDevice(maybeRootDevice.Root, maybeRootDevice.Location, searchTarget, maybeRootDevice.LocalAddr) if err != nil { errors = append(errors, err) continue @@ -61,6 +62,10 @@ func NewServiceClientsByURL(loc *url.URL, searchTarget string) ([]ServiceClient, // a given root device. The loc parameter is simply assigned to the // Location attribute of the returned ServiceClient(s). func NewServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searchTarget string) ([]ServiceClient, error) { + return newServiceClientsFromRootDevice(rootDevice, loc, searchTarget, "") +} + +func newServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searchTarget string, lAddr string) ([]ServiceClient, error) { device := &rootDevice.Device srvs := device.FindService(searchTarget) if len(srvs) == 0 { @@ -75,6 +80,7 @@ func NewServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searc RootDevice: rootDevice, Location: loc, Service: srv, + localAddr: lAddr, }) } return clients, nil @@ -86,3 +92,8 @@ func NewServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searc func (client *ServiceClient) GetServiceClient() *ServiceClient { return client } + +// LocalAddr returns the address from which the device was discovered (if known - otherwise empty). +func (client *ServiceClient) LocalAddr() string { + return client.localAddr +}