{"record":{"id":"ccc3895f5ce1a13c","repo":"XX-net/XX-Net","slug":"get-local-ip-fail-r","errorCode":null,"errorMessage":"get local ip fail:%r","messagePattern":"get local ip fail:%r","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"code/default/smart_router/local/dns_query.py","lineNumber":82,"sourceCode":"    elif sys.platform == \"darwin\":\n        try:\n            proc = subprocess.Popen([\"ifconfig | egrep inet | awk '{{print $2}}' | awk -F'/' '{{print $1}}'\"],\n                                    stdout=subprocess.PIPE, shell=True)\n            x = proc.communicate()[0]\n            ips = x.strip().split(b\"\\n\")\n        except Exception as e:\n            xlog.warn(\"get ip address e:%r\", e)\n            ips = [b'127.0.0.1']\n    elif sys.platform == \"ios\":\n        ips = [b'127.0.0.1']\n    elif sys.platform == \"win32\":\n        try:\n            ips = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]]\n            ips = utils.to_bytes(ips)\n            if b\"127.0.0.1\" not in ips:\n                ips.append(b\"127.0.0.1\")\n        except Exception as e:\n            xlog.warn(\"get local ip fail:%r\", e)\n            ips = [b\"127.0.0.1\"]\n    else:\n        ips = []\n        try:\n            for ix in socket.if_nameindex():\n                name = ix[1]\n                ip = get_ip_address(name)\n                ips.append(ip)\n        except Exception as e:\n            xlog.warn(\"get ip address e:%r\", e)\n            ips = [b'127.0.0.1']\n\n    xlog.debug(\"local ips: %s\", ips)\n    return ips\n\n\ndef query_dns_from_xxnet(domain, dns_type=None):\n    if not g.x_tunnel:","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/XX-net/XX-Net/blob/cfa5bc17b67676e467f37ec50766127e0ab5f0aa/code/default/smart_router/local/dns_query.py#L64-L100","documentation":"On Windows, get_local_ips() tries socket.gethostbyname_ex(socket.gethostname()) to enumerate local IPv4 addresses; on failure (DNS/hostname resolution error, Unicode hostname, no IPv4) it logs this warning and falls back to [b'127.0.0.1'].","triggerScenarios":"Calling smart router start() on win32 where gethostbyname_ex raises socket.gaierror or herror — e.g. hostname not resolvable via hosts file/DNS, machine renamed, or hostname containing non-ASCII characters.","commonSituations":"Fresh Windows installs where the hostname isn't in the hosts file; corporate DNS refusing local hostname resolution; hostnames with non-ASCII characters breaking gethostbyname_ex; machines with no active IPv4 interface.","solutions":["Verify the hostname resolves: 'ping %COMPUTERNAME%' should return a local IP; if not, add an entry to C:\\Windows\\System32\\drivers\\etc\\hosts mapping the hostname to 127.0.0.1.","Rename the machine to an ASCII hostname if it contains non-ASCII characters.","Use a more robust enumeration (e.g. iterating if_nameindex/get_ip_address or calling GetAdaptersAddresses) instead of gethostbyname_ex.","Accept the 127.0.0.1 fallback if only loopback detection matters."],"exampleFix":"// before\nips = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]]\n\n// after\ntry:\n    ips = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]]\nexcept socket.gaierror:\n    ips = []\nfor ix in socket.if_nameindex():\n    ip = get_ip_address(ix[1])\n    if ip and ip not in ips:\n        ips.append(ip)","handlingStrategy":"fallback","validationCode":"import socket\ntry:\n    socket.gethostbyname_ex(socket.gethostname())\nexcept socket.gaierror:\n    print('hostname unresolvable; local IP detection will use 127.0.0.1 only')","typeGuard":null,"tryCatchPattern":"try:\n    ips = get_local_ips()\nexcept Exception:\n    ips = [b'127.0.0.1']","preventionTips":["Add the machine hostname to the hosts file on Windows to make gethostbyname_ex succeed.","Keep hostnames ASCII-only.","Treat g.local_ips as possibly loopback-only and don't gate critical logic on real IPs being present."],"tags":["windows","hostname-resolution","socket","smart-router","ip-address"],"backgroundTag":"hostname-resolution-failed","analyzedSha":"cfa5bc17b67676e467f37ec50766127e0ab5f0aa","analyzedAt":"2026-08-27T19:28:28.225Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}