{"record":{"id":"2348d081d19346cc","repo":"txthinking/brook","slug":"this-is-ipv4","errorCode":null,"errorMessage":"This is ipv4","messagePattern":"This is ipv4","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"resolve.go","lineNumber":12,"sourceCode":"package brook\n\nimport (\n\t\"errors\"\n\t\"net\"\n\n\t\"github.com/miekg/dns\"\n)\n\nfunc Resolve6(host string) (string, error) {\n\tif net.ParseIP(host).To4() != nil {\n\t\treturn \"\", errors.New(\"This is ipv4\")\n\t}\n\tif net.ParseIP(host).To16() != nil {\n\t\treturn host, nil\n\t}\n\tm := &dns.Msg{}\n\tm.SetQuestion(host+\".\", dns.TypeAAAA)\n\tr, err := dns.Exchange(m, \"[2001:4860:4860::8888]:53\")\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tfor _, v := range r.Answer {\n\t\tif t, ok := v.(*dns.AAAA); ok {\n\t\t\treturn t.AAAA.String(), nil\n\t\t}\n\t}\n\treturn \"\", errors.New(\"Can not find IP\")\n}\n","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/txthinking/brook/blob/5cd13ef3b1fb574e88ebf2c1b5d95f2ebe1342c8/resolve.go#L1-L30","documentation":"Resolve6 resolves a hostname/IP to an IPv6 address. Before doing anything it parses the input; if the input is already an IP with a non-nil To4() (an IPv4 or IPv4-mapped address), it refuses with \"This is ipv4\" because the AAAA lookup path only handles IPv6. Pure IPv6 literals are returned directly; hostnames go through a DNS AAAA query.","triggerScenarios":"Calling Resolve6 with a dotted-quad IPv4 string like \"8.8.8.8\" or an IPv4-mapped form like \"::ffff:8.8.8.8\" — net.ParseIP succeeds and To4() is non-nil, so the function returns the error instead of resolving.","commonSituations":"Config files or URL hosts containing IPv4 addresses passed to an IPv6-only resolution path; code that assumed Resolve6 would fall back to IPv4; environment variables whose host field switches between IPv4 and IPv6 depending on deployment.","solutions":["Check the input with net.ParseIP before calling: if ip.To4() != nil use Resolve4 (or the IPv4 path) instead of Resolve6.","If the goal is dual-stack resolution, call the generic resolver and let DNS pick A or AAAA records based on connectivity.","Fix the configuration or environment so the host field for this code path is a hostname or IPv6 address only."],"exampleFix":"// before\naddr, err := Resolve6(\"8.8.8.8\") // error: This is ipv4\n\n// after\nip := net.ParseIP(host)\nif ip != nil && ip.To4() != nil {\n    addr, err = Resolve4(host)\n} else {\n    addr, err = Resolve6(host)\n}","handlingStrategy":"type-guard","validationCode":"func isIPv4Literal(host string) bool {\n    ip := net.ParseIP(host)\n    return ip != nil && ip.To4() != nil\n}\n// call Resolve4 when isIPv4Literal(host), else Resolve6","typeGuard":"func isIPv6(host string) bool {\n    ip := net.ParseIP(host)\n    return ip != nil && ip.To4() == nil\n}","tryCatchPattern":"addr, err := Resolve6(host)\nif err != nil && strings.Contains(err.Error(), \"ipv4\") {\n    addr, err = Resolve4(host)\n}","preventionTips":["Parse and classify the host (To4/To16) before choosing Resolve4 vs Resolve6.","Normalize IPv4-mapped literals (::ffff:x.x.x.x) to the IPv4 path.","Prefer passing hostnames, not IP literals, to resolution helpers."],"tags":["network","dns","ipv6","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"5cd13ef3b1fb574e88ebf2c1b5d95f2ebe1342c8","analyzedAt":"2026-09-06T04:35:00.432Z","contentChangedAt":"2026-09-06T04:35:00.432Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}