@@ -39,7 +39,7 @@ import (
3939 "fmt"
4040 "net"
4141 "net/http"
42- "os "
42+ "net/url "
4343 "strconv"
4444 "strings"
4545 "time"
@@ -126,8 +126,9 @@ func Discover() (nat NAT, err error) {
126126 if err != nil {
127127 return
128128 }
129+ var serviceIP string = getServiceIP (serviceURL )
129130 var ourIP string
130- ourIP , err = getOurIP ()
131+ ourIP , err = getOurIP (serviceIP )
131132 if err != nil {
132133 return
133134 }
@@ -212,13 +213,22 @@ func getChildService(d *device, serviceType string) *service {
212213 return nil
213214}
214215
215- // getOurIP returns a best guess at what the local IP is.
216- func getOurIP () (ip string , err error ) {
217- hostname , err := os .Hostname ()
218- if err != nil {
219- return
216+ func getServiceIP (serviceURL string ) (routerIP string ) {
217+ url , _ := url .Parse (serviceURL )
218+ return url .Hostname ()
219+ }
220+
221+ // getOurIP returns the local IP that is on the same subnet as the serviceIP.
222+ func getOurIP (serviceIP string ) (ip string , err error ) {
223+ _ , serviceNet , _ := net .ParseCIDR (serviceIP + "/24" )
224+ addrs , err := net .InterfaceAddrs ()
225+ for _ , addr := range addrs {
226+ ip , _ , _ := net .ParseCIDR (addr .String ())
227+ if serviceNet .Contains (ip ) {
228+ return ip .String (), nil
229+ }
220230 }
221- return net . LookupCNAME ( hostname )
231+ return
222232}
223233
224234// getServiceURL parses the xml description at the given root url to find the
0 commit comments