// NewHttpClient is the constructor for the bucket API implementation of HttpClient. func NewHttpClient(client *http.Client, url string) (*HttpClient, error) { // get hostname hostname, err := os.Hostname() if err != nil { return nil, err } // get canonical hostname canonical, err := net.LookupCNAME(hostname) if err != nil { return nil, err } // get ipv4 var ip string addrs, err := net.LookupIP(canonical) if err != nil { return nil, err } for _, addr := range addrs { if ipv4 := addr.To4(); ipv4 != nil { ip = ipv4.String() } } // create instance return &HttpClient{instance: client, url: url, ipv4: ip, hostname: canonical}, nil }
//export ReturnString func ReturnString(val string) string { cname, err := net.LookupCNAME(val) if err != nil { return "Could not find CNAME" } return cname }
func (me *WorkerDaemon) report(coordinator string, port int) { client, err := rpc.DialHTTP("tcp", coordinator) if err != nil { log.Println("dialing coordinator:", err) return } hostname, err := os.Hostname() if err != nil { log.Println("hostname", err) return } cname, err := net.LookupCNAME(hostname) if err != nil { log.Println("cname", err) return } cname = strings.TrimRight(cname, ".") req := Registration{ Address: fmt.Sprintf("%v:%d", cname, port), Name: fmt.Sprintf("%s:%d", hostname, port), Version: Version(), } rep := 0 err = client.Call("Coordinator.Register", &req, &rep) if err != nil { log.Println("coordinator rpc error:", err) } }
func getOurIP() (ip string, err error) { hostname, err := os.Hostname() if err != nil { return } return net.LookupCNAME(hostname) }
func (me *Worker) Report() { if me.options.Coordinator == "" { return } client, err := rpc.DialHTTP("tcp", me.options.Coordinator) if err != nil { log.Println("dialing coordinator:", err) return } cname, err := net.LookupCNAME(Hostname) if err != nil { log.Println("cname", err) return } cname = strings.TrimRight(cname, ".") req := Registration{ Address: fmt.Sprintf("%v:%d", cname, me.options.Port), Name: fmt.Sprintf("%s:%d", Hostname, me.options.Port), Version: Version(), } rep := 0 err = client.Call("Coordinator.Register", &req, &rep) if err != nil { log.Println("coordinator rpc error:", err) } }
//export ReturnString func ReturnString(val string) *C.char { cname, err := net.LookupCNAME(val) if err != nil { C.CString("Could not find CNAME") } return C.CString(cname) }
// GetCNAME resolves an IP or hostname into a normalized valid CNAME func GetCNAME(hostname string) (string, error) { res, err := net.LookupCNAME(hostname) if err != nil { return hostname, err } res = strings.TrimRight(res, ".") return res, nil }
func resourceDnsCnameRecordRead(d *schema.ResourceData, meta interface{}) error { cname, err := net.LookupCNAME(d.Id()) if err != nil { return err } d.Set("cname", cname) return nil }
// FullyQualifiedHostname returns the full hostname with domain func FullyQualifiedHostname() (string, error) { hostname, err := os.Hostname() if err != nil { return "", err } cname, err := net.LookupCNAME(hostname) if err != nil { return "", err } return strings.TrimRight(cname, "."), nil }
func dataSourceDnsCnameRecordRead(d *schema.ResourceData, meta interface{}) error { host := d.Get("host").(string) cname, err := net.LookupCNAME(host) if err != nil { return err } d.Set("cname", cname) d.SetId(host) return nil }
func cname(host string) (data []byte, err error) { cname, err := net.LookupCNAME(host) if err != nil { err = os.ErrNotExist return } buf := bytes.NewBuffer(make([]byte, 0, len(cname)+1)) buf.WriteString(cname) buf.WriteByte('\n') data = buf.Bytes() return }
func lcname(domain string) { cname, err := net.LookupCNAME(domain) if err != nil { //fmt.Printf("err:%s\n", err) return } fmt.Printf("-------------------\n") fmt.Printf(" Information CNAME \n") fmt.Printf("-------------------\n") fmt.Printf("\t%s\n", cname) }
// FullHostname is a best effort attempt to resolve the canonical FQDN of // the host. On failure it will fall back to a reasonable looking default // such as 'localhost.' or 'hostname.invalid.' func FullHostname() string { hostname, err := os.Hostname() if err != nil || hostname == "localhost" || hostname == "(none)" { return "localhost." } fullname, err := net.LookupCNAME(hostname) if err != nil { fullname = hostname if !strings.Contains(fullname, ".") { fullname += ".invalid." } } return fullname }
func main() { if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "Usage:%s hostname\n", os.Args[0]) os.Exit(1) } name := os.Args[1] cname, err := net.LookupCNAME(name) if err != nil { fmt.Println("LookupCNAME error:", err.Error()) os.Exit(1) } fmt.Println("The host caonical name is:", cname) os.Exit(0) }
// printInfo prints a brief informative header about the connection func printInfo(urlTarget string, resp *http.Response) { fmt.Println("********* This is gobble version ", version, " ***************") urlInfo, err := url.Parse(urlTarget) if err != nil { return } cname, _ := net.LookupCNAME(urlInfo.Host) ips, _ := net.LookupIP(cname) fmt.Println("Connecting to", cname, " ", ips) fmt.Printf("Status %s Protocol %s TransferEncoding %v\n", resp.Status, resp.Proto, resp.TransferEncoding) fmt.Printf("Content Length: %d bytes\n", resp.ContentLength) fmt.Println() }
// goLookupCNAME is the native Go implementation of LookupCNAME. // Used only if cgoLookupCNAME refuses to handle the request // (that is, only if cgoLookupCNAME is the stub in cgo_stub.go). // Normally we let cgo use the C library resolver instead of // depending on our lookup code, so that Go and C get the same // answers. func LookupCNAME(name string, options *LookupOptions) (cname string, err error) { if nil == options || nil == options.DNSServers || len(options.DNSServers) == 0 { return net.LookupCNAME(name) } dnscfg, dnserr := dnsConfigWithOptions(options) if dnserr != nil || dnscfg == nil { err = dnserr return } _, rr, err := lookup(dnscfg, name, dnsTypeCNAME) if err != nil { return } cname = rr[0].(*dnsRR_CNAME).Cname return }
func resolvName(name string) ([]*Response, error) { var sets []*Response isCNAME := true // append a dot if there is none if !strings.HasSuffix(name, ".") { name = name + "." } // check if there name is a CNAME aptr, err := net.LookupCNAME(name) if err != nil { return sets, err } if aptr == name { isCNAME = false } // get IP address addrs, err := net.LookupHost(name) if err != nil { return sets, err } // return a DNSSet for each address for _, addr := range addrs { txt := resolvTXT(name) if isCNAME { sets = append(sets, &Response{ Addr: net.ParseIP(addr), A: aptr, CNAME: name, TXT: txt, }) } else { sets = append(sets, &Response{ Addr: net.ParseIP(addr), A: name, TXT: txt, }) } } return sets, nil }
func main() { if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "Usage: %s hostname\n", os.Args[0]) fmt.Println("Usage: ", os.Args[0], "hostname") os.Exit(1) } name := os.Args[1] addr, err := net.LookupCNAME(name) if err != nil { fmt.Println("Resolution error", err.Error()) os.Exit(1) } // for _, s := range addr { fmt.Println(addr) // } os.Exit(0) }
func getHostname() string { hostname, err := os.Hostname() if err != nil { log.Warnf("can't get local hostname: %v", err) return "-" } cname, err := net.LookupCNAME(hostname) if err != nil { log.Warnf("can't get FQDN for %s (using as is): %v", hostname, err) return hostname } if cname[len(cname)-1] == '.' { cname = cname[:len(cname)-1] } return cname }
func main() { if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "Usage: %s hostname\n", os.Args[0]) os.Exit(1) } name := os.Args[1] addr, err := net.LookupCNAME(name) if err != nil { fmt.Println("Error: ", err.Error()) os.Exit(2) } fmt.Println("Canonical address is", addr) os.Exit(0) }
func (a *App) ELBReady() bool { _, err := net.LookupCNAME(a.Outputs["BalancerHost"]) return err == nil }
func resolveSRV(uri *URI, proto string) (*e3x.Identity, error) { // ignore port host, _, _ := net.SplitHostPort(uri.Canonical) if host == "" { host = uri.Canonical } // normalize if !strings.HasSuffix(host, ".") { host += "." } // ignore .public if strings.HasSuffix(host, ".public.") { return nil, &net.DNSError{Name: host, Err: "cannot resolve .public hostnames using DNS"} } // lookup SRV records _, srvs, err := net.LookupSRV("mesh", proto, host) if err != nil { return nil, err } if len(srvs) > 1 { return nil, &net.DNSError{Name: host, Err: "too many SRV records"} } if len(srvs) == 0 { return nil, &net.DNSError{Name: host, Err: "no SRV records"} } var ( srv = srvs[0] port = srv.Port portStr = strconv.Itoa(int(port)) hn hashname.H keys cipherset.Keys ) { // detect valid target parts := strings.SplitN(srv.Target, ".", 2) if len(parts) != 2 || len(parts[0]) != 52 || len(parts[1]) == 0 { return nil, &net.DNSError{Name: host, Err: "SRV must target a <hashname>.<domain> domain"} } hn = hashname.H(parts[0]) if !hn.Valid() { return nil, &net.DNSError{Name: host, Err: "SRV must target a <hashname>.<domain> domain"} } } // detect CNAMEs (they are not allowed) cname, err := net.LookupCNAME(srv.Target) if err != nil { return nil, err } if cname != "" && cname != srv.Target { return nil, &net.DNSError{Name: host, Err: "CNAME record are not allowed"} } // lookup A AAAA records ips, err := net.LookupIP(srv.Target) if err != nil { return nil, err } if len(ips) == 0 { return nil, &net.DNSError{Name: host, Err: "no A or AAAA records"} } // lookup TXT txts, err := net.LookupTXT(srv.Target) if err != nil { return nil, err } if len(txts) == 0 { return nil, &net.DNSError{Name: host, Err: "no TXT records"} } // make addrs addrs := make([]net.Addr, 0, len(ips)) for _, ip := range ips { var ( addr net.Addr ) switch proto { case "udp": addr, _ = transports.ResolveAddr("udp4", net.JoinHostPort(ip.String(), portStr)) if addr == nil { addr, _ = transports.ResolveAddr("udp6", net.JoinHostPort(ip.String(), portStr)) } case "tcp": addr, _ = transports.ResolveAddr("tcp4", net.JoinHostPort(ip.String(), portStr)) if addr == nil { addr, _ = transports.ResolveAddr("tcp6", net.JoinHostPort(ip.String(), portStr)) } // case "http": // addr, _ = http.NewAddr(ip, port) } if addr != nil { addrs = append(addrs, addr) } } { // parse keys // Sort txts so they form ascending sequences of key parts sort.Strings(txts) keyData := make(map[uint8]string, 10) for len(txts) > 0 { var ( txt = txts[0] parts = strings.Split(txt, "=") ) if len(parts) != 2 { txts = txts[1:] continue } var ( label = parts[0] value = parts[1] csid uint8 ) if len(label) < 2 { txts = txts[1:] continue } // parse the CSID portion of the label i, err := strconv.ParseUint(label[:2], 16, 8) if err != nil { txts = txts[1:] continue } csid = uint8(i) // verify the key-part portion of the label if len(label) > 2 { _, err = strconv.ParseUint(label[2:], 10, 8) if err != nil { txts = txts[1:] continue } } keyData[csid] += value txts = txts[1:] } keys = make(cipherset.Keys, len(keyData)) for csid, str := range keyData { key, err := cipherset.DecodeKey(csid, str, "") if err != nil { continue } keys[csid] = key } } ident, err := e3x.NewIdentity(keys, nil, addrs) if err != nil { return nil, err } if hn != ident.Hashname() { return nil, &net.DNSError{Name: host, Err: "invalid keys"} } return ident, nil }