{"record":{"id":"4de0acbdee02aca9","repo":"iflytek/astron-agent","slug":"bad-ip","errorCode":null,"errorMessage":"Bad IP !! ","messagePattern":"Bad IP !! ","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"core/plugin/link/utils/sid/sid_generator2.py","lineNumber":111,"sourceCode":"    sid2 = 2\n\n    def __init__(\n        self,\n        sub: Optional[str],\n        location: Optional[str],\n        local_ip: str,\n        local_port: Optional[str],\n    ) -> None:\n        self.index = 0\n        ip = socket.inet_aton(local_ip)\n        if ip:\n            ip_sec3 = ip[2]\n            ip_sec4 = ip[3]\n            ip3 = ip_sec3 & 0xFF\n            ip4 = ip_sec4 & 0xFF\n            self.short_local_ip = f\"{ip3:02x}{ip4:02x}\"\n        else:\n            raise ValueError(\"Bad IP !! \" + local_ip)\n        if local_port is None or len(local_port) < 4:\n            raise ValueError(\"Bad Port!! \")\n        self.port = local_port\n        self.location = location\n        self.sub = sub\n        print(\"sid init success\")\n\n    def gen(self) -> str:\n        \"\"\"Generate a unique service ID.\n\n        Returns:\n            str: Unique service ID in format:\n            {sub}{pid}{index}@{location}{timestamp}{ip}{port}{version}\n        \"\"\"\n        if self.sub is None or len(self.sub) == 0:\n            self.sub = \"src\"\n        pid = os.getpid() & 0xFF\n        self.index = (self.index + 1) & 0xFFFF","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/plugin/link/utils/sid/sid_generator2.py#L93-L129","documentation":"The SID generator's __init__ derives a short hex representation from the last two octets of the local IP. If the IP string does not split into the expected 4 dotted segments (ip[0..3]), a ValueError 'Bad IP !! <ip>' is raised. A related guard raises 'Bad Port!!' if the port is missing or shorter than 4 chars.","triggerScenarios":"Constructing the SID generator with a local_ip that is not a valid dotted-quad IPv4 (e.g. 'localhost', '127.0.0.1 extra', an IPv6 address like '::1', empty string from a failed lookup).","commonSituations":"get_host_ip returning a hostname or IPv6 address on odd network setups; env var holding a bad IP; passing 'localhost' instead of a numeric IP; misconfigured multi-NIC hosts returning an unexpected format.","solutions":["Pass a valid dotted-quad IPv4 address (e.g. '192.168.1.10'), not a hostname or IPv6","Resolve the hostname to an IPv4 via socket.gethostbyname before constructing the generator","Verify the IP source (env var/config) contains the expected format; log the offending value","Also ensure local_port is provided and at least 4 characters to avoid the adjacent 'Bad Port!!' error"],"exampleFix":"// before\nsid = SidGenerator(local_ip=socket.gethostname(), local_port=port)\n// after\nip = socket.gethostbyname(socket.gethostname())\nassert len(ip.split(\".\")) == 4, ip\nsid = SidGenerator(local_ip=ip, local_port=port)","handlingStrategy":"validation","validationCode":"def is_ipv4(ip: str) -> bool:\n    parts = ip.split(\".\")\n    return len(parts) == 4 and all(p.isdigit() and 0 <= int(p) <= 255 for p in parts)","typeGuard":"def is_valid_sid_ip(ip: object) -> bool:\n    return isinstance(ip, str) and is_ipv4(ip)","tryCatchPattern":"try:\n    gen = SidGenerator(local_ip=ip, local_port=port)\nexcept ValueError as e:\n    logger.error(\"invalid IP/port for SID: %s\", e)\n    raise","preventionTips":["Always pass a numeric dotted-quad IPv4 (never hostname or IPv6) to the SID generator","Resolve hostnames with socket.gethostbyname before construction","Validate port is present and at least 4 characters as the constructor expects"],"tags":["ip","sid","validation","valueerror"],"backgroundTag":"invalid-argument-value","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}