當前位置: 首頁>>代碼示例>>Golang>>正文


Golang net.LookupAddr函數代碼示例

本文整理匯總了Golang中net.LookupAddr函數的典型用法代碼示例。如果您正苦於以下問題:Golang LookupAddr函數的具體用法?Golang LookupAddr怎麽用?Golang LookupAddr使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了LookupAddr函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Defined

func (x *Imp) Defined(s string) bool {
	//
	x.Clr()
	if x.fmt == Hostname {
		if ip, err := net.LookupHost(s); err == nil {
			x.ip = net.ParseIP(ip[0]) // default
			for _, n := range ip {    // get first IPv4-number, if such exists
				if len(n) == 4 {
					x.ip = net.ParseIP(n)
					break
				}
			}
			if h, err1 := net.LookupAddr(s); err1 == nil {
				x.name = h
			} else {
				x.name = []string{s}
			}
			return true
		}
	} else { // x.fmt == IPnumber
		if h, err := net.LookupAddr(s); err == nil {
			x.ip = net.ParseIP(s)
			x.name = h
			return true
		}
	}
	return false
}
開發者ID:CaptainSoOmA,項目名稱:Uni,代碼行數:28,代碼來源:imp.go

示例2: Fqdn

// Return the current machines fully qualified domain name.
func Fqdn() (string, error) {
	hostname, err := os.Hostname()
	if err != nil {
		return "", err
	}

	addrs, err := net.LookupHost(hostname)
	if err != nil {
		return "", err
	}

	for _, addr := range addrs {
		names, err := net.LookupAddr(addr)
		if err != nil {
			return "", err
		}
		for _, name := range names {
			if strings.Contains(name, ".") {
				return name, nil
			}
		}
	}

	return hostname, nil
}
開發者ID:cloudbuy,項目名稱:pkcs10,代碼行數:26,代碼來源:pkcs10_test.go

示例3: ServeHTTP

// ServeHTTP is an http.Handler ServeHTTP method
func (h *bandwidthQuotaHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	host, _, _ := net.SplitHostPort(req.RemoteAddr)
	longIP := longIP{net.ParseIP(host)}.IptoUint32()
	if h.quotas.WillExceedQuota(longIP, req.ContentLength) {
		hosts, _ := net.LookupAddr(uint32ToIP(longIP).String())
		log.Debug.Printf("Offending Host: %s, BandwidthUsed: %d", hosts, h.quotas.GetQuotaUsed(longIP))
		writeErrorResponse(w, req, BandWidthInsufficientToProceed, req.URL.Path)
		return
	}
	qr := &quotaReader{
		ReadCloser: req.Body,
		quotas:     h.quotas,
		ip:         longIP,
		w:          w,
		req:        req,
		lock:       &sync.RWMutex{},
	}
	req.Body = qr
	w = &quotaWriter{
		ResponseWriter: w,
		quotas:         h.quotas,
		ip:             longIP,
		quotaReader:    qr,
	}
	h.handler.ServeHTTP(w, req)
}
開發者ID:yubobo,項目名稱:minio,代碼行數:27,代碼來源:bandwidth_cap.go

示例4: Connect

func (milter *milter) Connect(ctx uintptr, hostname string, ip net.IP) (sfsistat int8) {
	defer milterHandleError(ctx, &sfsistat)

	sess := &milterSession{
		id:        milterGetNewSessionId(),
		timeStart: time.Now(),
		persisted: false,
	}
	sess.Hostname = hostname
	sess.Ip = ip.String()
	sess.MtaHostName = m.GetSymVal(ctx, "j")
	sess.MtaDaemonName = m.GetSymVal(ctx, "{daemon_name}")

	if reverse, _ := net.LookupAddr(ip.String()); len(reverse) != 0 {
		sess.ReverseDns = reverse[0]
	}

	MilterDataIndex.addNewSession(sess)
	sessId := sess.getId()
	res := m.SetPriv(ctx, sessId)
	if res != 0 {
		panic(fmt.Sprintf("Session could not be stored in milterDataIndex"))
	}

	StatsCounters["MilterCallbackConnect"].increase(1)
	Log.Debug("%s Milter.Connect() called: ip = %s, hostname = %s", sess.milterGetDisplayId(), ip, sess.ReverseDns)

	return m.Continue
}
開發者ID:EaterOfCode,項目名稱:ClueGetter,代碼行數:29,代碼來源:milter.go

示例5: lookupAddr

func lookupAddr(ip net.IP) ([]string, error) {
	names, err := net.LookupAddr(ip.String())
	for i, _ := range names {
		names[i] = strings.TrimRight(names[i], ".") // Always return unrooted name
	}
	return names, err
}
開發者ID:TheBigBear,項目名稱:ifconfigd,代碼行數:7,代碼來源:oracle.go

示例6: TestPingMonitor

func TestPingMonitor(t *testing.T) {
	conn, _ := rados.NewConn()
	conn.ReadDefaultConfigFile()
	conn.Connect()

	// mon id that should work with vstart.sh
	reply, err := conn.PingMonitor("a")
	if err == nil {
		assert.NotEqual(t, reply, "")
		return
	}

	// mon id that should work with micro-osd.sh
	reply, err = conn.PingMonitor("0")
	if err == nil {
		assert.NotEqual(t, reply, "")
		return
	}

	// try to use a hostname as the monitor id
	mon_addr, _ := conn.GetConfigOption("mon_host")
	hosts, _ := net.LookupAddr(mon_addr)
	for _, host := range hosts {
		reply, err := conn.PingMonitor(host)
		if err == nil {
			assert.NotEqual(t, reply, "")
			return
		}
	}

	t.Error("Could not find a valid monitor id")

	conn.Shutdown()
}
開發者ID:omnicube,項目名稱:go-ceph,代碼行數:34,代碼來源:rados_test.go

示例7: requestFilter

func (a *API) requestFilter(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ip, err := ipFromRequest(r)
		if err != nil {
			r.Header.Set(IP_HEADER, err.Error())
		} else {
			r.Header.Set(IP_HEADER, ip.String())
			country, err := a.lookupCountry(ip)
			if err != nil {
				r.Header.Set(COUNTRY_HEADER, err.Error())
			} else {
				r.Header.Set(COUNTRY_HEADER, country)
			}
		}
		hostname, err := net.LookupAddr(ip.String())
		if err != nil {
			r.Header.Set(HOSTNAME_HEADER, err.Error())
		} else {
			r.Header.Set(HOSTNAME_HEADER, strings.Join(hostname, ", "))
		}
		if a.CORS {
			w.Header().Set("Access-Control-Allow-Methods", "GET")
			w.Header().Set("Access-Control-Allow-Origin", "*")
		}
		next.ServeHTTP(w, r)
	})
}
開發者ID:twouters,項目名稱:ifconfigd,代碼行數:27,代碼來源:api.go

示例8: getIP

func getIP(s string) string {
	a, err := net.LookupAddr(s)
	if err != nil {
		return ""
	}
	return a[0]
}
開發者ID:Christeefym,項目名稱:lantern,代碼行數:7,代碼來源:xfr_test.go

示例9: LookupAddress

func LookupAddress(ip string) chan string {
	r := make(chan string)

	go func() {
		names, error := net.LookupAddr(ip)

		if error != nil {
			// Error? Use the IP.
			r <- ip
		}

		if len(names) > 0 {
			// Use the first value, but strip the leading ".".
			host := names[0]
			l := len(host)

			if host[l-1] == '.' {
				r <- host[0 : l-1]
			} else {
				r <- host
			}
		} else {
			// Use the IP
			r <- ip
		}
	}()

	return r
}
開發者ID:ahf,項目名稱:ircd-novo,代碼行數:29,代碼來源:dns.go

示例10: SetBasicInfo

func (info *Info) SetBasicInfo(url string, response *http.Response) {
	info.Url = url
	for key, values := range response.Header {
		info.RawHeaders[key] = values
	}
	cookies := response.Cookies()
	for _, cookie := range cookies {
		info.Cookies[cookie.Name] = cookie
	}
	u, err := urlLib.Parse(url)
	if err != nil {
		panic(err)
	}
	info.Host = u.Host
	ips, err := net.LookupHost(u.Host)
	if err == nil {
		info.Ip = ips
		for _, ip := range info.Ip {
			hosts, err := net.LookupAddr(ip)
			if err == nil {
				info.RealHost = hosts
			}
		}

	}
}
開發者ID:takaaki-mizuno,項目名稱:web-architecture-analyzer,代碼行數:26,代碼來源:info.go

示例11: String

func (msg milterMessage) String() []byte {
	sess := *msg.getSession()
	fqdn, err := os.Hostname()
	if err != nil {
		Log.Error("Could not determine FQDN")
		fqdn = sess.getMtaHostName()
	}
	revdns, err := net.LookupAddr(sess.getIp())
	revdnsStr := "unknown"
	if err == nil {
		revdnsStr = strings.Join(revdns, "")
	}

	body := make([]string, 0)

	body = append(body, fmt.Sprintf("Received: from %s (%s [%s])\r\n\tby %s with SMTP id %[email protected]%s; %s\r\n",
		sess.getHelo(),
		revdnsStr,
		sess.getIp(),
		fqdn,
		sess.getId(),
		fqdn,
		time.Now().Format(time.RFC1123Z)))

	for _, header := range msg.getHeaders() {
		body = append(body, (*header).getKey()+": "+(*header).getValue()+"\r\n")
	}

	body = append(body, "\r\n")
	for _, bodyChunk := range msg.getBody() {
		body = append(body, bodyChunk)
	}

	return []byte(strings.Join(body, ""))
}
開發者ID:rayyang2000,項目名稱:ClueGetter,代碼行數:35,代碼來源:message.go

示例12: getHostNamesFromIP

func getHostNamesFromIP(ip string) ([]string, error) {
	hostNames, err := net.LookupAddr(ip)
	if err != nil {
		return nil, err
	}
	return hostNames, nil
}
開發者ID:golang-devops,項目名稱:go-psexec,代碼行數:7,代碼來源:util.go

示例13: NewConn

func NewConn(p *PeerConnection) (*Conn, error) {
	c := &Conn{
		Host: p.Host,
		Port: p.Port,
		conn: p.Conn,
	}
	names, err := net.LookupAddr(c.Host)
	if err == nil {
		c.Resolved = names[0]
	} else {
		c.Resolved = "No reverse DNS possible"
	}
	if c.conn == nil {
		var err error
		if c.conn, err = net.DialTimeout("tcp", c.String(), peerTimeout); err != nil {
			return c, fmt.Errorf("Connect: %s", err.Error())
		}
	}
	config := &sslconn.Config{
		CipherList: defaultCipher,
	}
	if c.Conn, err = sslconn.NewConn(c.conn, c.conn, config, false); err != nil {
		return c, fmt.Errorf("Connect: %s", err.Error())
	}
	if err := c.Handshake(); err != nil {
		return c, fmt.Errorf("Connect: %s", err.Error())
	}
	return c, nil
}
開發者ID:Zoramite,項目名稱:ripple,代碼行數:29,代碼來源:conn.go

示例14: handlePost

func handlePost(w http.ResponseWriter, req *http.Request) {
	logRequest(req)
	author, _, _ := net.SplitHostPort(req.RemoteAddr)
	addrs, err := net.LookupAddr(author)
	if err != nil && len(addrs) > 0 {
		author = addrs[0]
	}

	title := req.FormValue("title")
	text := req.FormValue("text")
	title = strings.TrimSpace(title)
	text = strings.TrimSpace(text)
	if title == "" || text == "" {
		err = fmt.Errorf("empty title or text")
		logError(w, req, http.StatusBadRequest, err)
		return
	}
	post := Post{
		Title:  title,
		Text:   text,
		Author: author,
		When:   time.Now(),
	}
	err = savePost(post)
	if err != nil {
		logError(w, req, http.StatusInternalServerError, err)
		return
	}
	w.Write([]byte("OK"))
}
開發者ID:lyuyun,項目名稱:loggregator,代碼行數:30,代碼來源:tinyblog.go

示例15: resolveIP

// Resolve an IP to a domain name using the system DNS settings first, then HypeDNS
func resolveIP(ip string) (hostname string, err error) {
	var try2 string

	// try the system DNS setup
	result, _ := net.LookupAddr(ip)
	if len(result) > 0 {
		goto end
	}

	// Try HypeDNS
	try2, err = reverseHypeDNSLookup(ip)
	if try2 == "" || err != nil {
		err = fmt.Errorf("Unable to resolve IP address. This is usually caused by not having a route to hypedns. Please try again in a few seconds.")
		return
	}
	result = append(result, try2)
end:
	for _, addr := range result {
		hostname = addr
	}

	// Trim the trailing period becuase it annoys me
	if hostname[len(hostname)-1] == '.' {
		hostname = hostname[:len(hostname)-1]
	}
	return
}
開發者ID:ryansb,項目名稱:cjdcmd,代碼行數:28,代碼來源:dns.go


注:本文中的net.LookupAddr函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。