本文整理汇总了Golang中syscall.Addrinfo类的典型用法代码示例。如果您正苦于以下问题:Golang Addrinfo类的具体用法?Golang Addrinfo怎么用?Golang Addrinfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Addrinfo类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: cgoLookupIPCNAME
func cgoLookupIPCNAME(name string) (addrs []IP, cname string, err error, completed bool) {
var res *syscall.Addrinfo
var hints syscall.Addrinfo
// NOTE(rsc): In theory there are approximately balanced
// arguments for and against including AI_ADDRCONFIG
// in the flags (it includes IPv4 results only on IPv4 systems,
// and similarly for IPv6), but in practice setting it causes
// getaddrinfo to return the wrong canonical name on Linux.
// So definitely leave it out.
hints.Ai_flags = int32((syscall.AI_ALL | syscall.AI_V4MAPPED | syscall.AI_CANONNAME) & cgoAddrInfoMask())
h := syscall.StringBytePtr(name)
syscall.Entersyscall()
gerrno := libc_getaddrinfo(h, nil, &hints, &res)
syscall.Exitsyscall()
if gerrno != 0 {
var str string
if gerrno == syscall.EAI_NONAME {
str = noSuchHost
} else if gerrno == syscall.EAI_SYSTEM {
str = syscall.GetErrno().Error()
} else {
str = bytePtrToString(libc_gai_strerror(gerrno))
}
return nil, "", &DNSError{Err: str, Name: name}, true
}
defer libc_freeaddrinfo(res)
if res != nil {
cname = bytePtrToString((*byte)(unsafe.Pointer(res.Ai_canonname)))
if cname == "" {
cname = name
}
if len(cname) > 0 && cname[len(cname)-1] != '.' {
cname += "."
}
}
for r := res; r != nil; r = r.Ai_next {
// Everything comes back twice, once for UDP and once for TCP.
if r.Ai_socktype != syscall.SOCK_STREAM {
continue
}
switch r.Ai_family {
default:
continue
case syscall.AF_INET:
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.Ai_addr))
addrs = append(addrs, copyIP(sa.Addr[:]))
case syscall.AF_INET6:
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.Ai_addr))
addrs = append(addrs, copyIP(sa.Addr[:]))
}
}
return addrs, cname, nil, true
}
示例2: cgoLookupIPCNAME
func cgoLookupIPCNAME(name string) (addrs []IP, cname string, err error, completed bool) {
var res *syscall.Addrinfo
var hints syscall.Addrinfo
hints.Ai_flags = int32(cgoAddrInfoFlags())
hints.Ai_socktype = syscall.SOCK_STREAM
h := syscall.StringBytePtr(name)
syscall.Entersyscall()
gerrno := libc_getaddrinfo(h, nil, &hints, &res)
syscall.Exitsyscall()
if gerrno != 0 {
var str string
if gerrno == syscall.EAI_NONAME {
str = noSuchHost
} else if gerrno == syscall.EAI_SYSTEM {
str = syscall.GetErrno().Error()
} else {
str = bytePtrToString(libc_gai_strerror(gerrno))
}
return nil, "", &DNSError{Err: str, Name: name}, true
}
defer libc_freeaddrinfo(res)
if res != nil {
cname = bytePtrToString((*byte)(unsafe.Pointer(res.Ai_canonname)))
if cname == "" {
cname = name
}
if len(cname) > 0 && cname[len(cname)-1] != '.' {
cname += "."
}
}
for r := res; r != nil; r = r.Ai_next {
// We only asked for SOCK_STREAM, but check anyhow.
if r.Ai_socktype != syscall.SOCK_STREAM {
continue
}
switch r.Ai_family {
default:
continue
case syscall.AF_INET:
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.Ai_addr))
addrs = append(addrs, copyIP(sa.Addr[:]))
case syscall.AF_INET6:
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.Ai_addr))
addrs = append(addrs, copyIP(sa.Addr[:]))
}
}
return addrs, cname, nil, true
}
示例3: cgoLookupPort
func cgoLookupPort(network, service string) (port int, err error, completed bool) {
acquireThread()
defer releaseThread()
var hints syscall.Addrinfo
switch network {
case "": // no hints
case "tcp", "tcp4", "tcp6":
hints.Ai_socktype = syscall.SOCK_STREAM
hints.Ai_protocol = syscall.IPPROTO_TCP
case "udp", "udp4", "udp6":
hints.Ai_socktype = syscall.SOCK_DGRAM
hints.Ai_protocol = syscall.IPPROTO_UDP
default:
return 0, &DNSError{Err: "unknown network", Name: network + "/" + service}, true
}
if len(network) >= 4 {
switch network[3] {
case '4':
hints.Ai_family = syscall.AF_INET
case '6':
hints.Ai_family = syscall.AF_INET6
}
}
s := syscall.StringBytePtr(service)
var res *syscall.Addrinfo
syscall.Entersyscall()
gerrno := libc_getaddrinfo(nil, s, &hints, &res)
syscall.Exitsyscall()
if gerrno != 0 {
switch gerrno {
case syscall.EAI_SYSTEM:
errno := syscall.GetErrno()
if errno == 0 { // see golang.org/issue/6232
errno = syscall.EMFILE
}
err = errno
default:
err = addrinfoErrno(gerrno)
}
return 0, &DNSError{Err: err.Error(), Name: network + "/" + service}, true
}
defer libc_freeaddrinfo(res)
for r := res; r != nil; r = r.Ai_next {
switch r.Ai_family {
case syscall.AF_INET:
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.Ai_addr))
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
return int(p[0])<<8 | int(p[1]), nil, true
case syscall.AF_INET6:
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.Ai_addr))
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
return int(p[0])<<8 | int(p[1]), nil, true
}
}
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service}, true
}
示例4: cgoLookupPort
func cgoLookupPort(net, service string) (port int, err error, completed bool) {
acquireThread()
defer releaseThread()
var res *syscall.Addrinfo
var hints syscall.Addrinfo
switch net {
case "":
// no hints
case "tcp", "tcp4", "tcp6":
hints.Ai_socktype = syscall.SOCK_STREAM
hints.Ai_protocol = syscall.IPPROTO_TCP
case "udp", "udp4", "udp6":
hints.Ai_socktype = syscall.SOCK_DGRAM
hints.Ai_protocol = syscall.IPPROTO_UDP
default:
return 0, UnknownNetworkError(net), true
}
if len(net) >= 4 {
switch net[3] {
case '4':
hints.Ai_family = syscall.AF_INET
case '6':
hints.Ai_family = syscall.AF_INET6
}
}
s := syscall.StringBytePtr(service)
syscall.Entersyscall()
gerrno := libc_getaddrinfo(nil, s, &hints, &res)
syscall.Exitsyscall()
if gerrno == 0 {
defer libc_freeaddrinfo(res)
for r := res; r != nil; r = r.Ai_next {
switch r.Ai_family {
default:
continue
case syscall.AF_INET:
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.Ai_addr))
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
return int(p[0])<<8 | int(p[1]), nil, true
case syscall.AF_INET6:
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.Ai_addr))
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
return int(p[0])<<8 | int(p[1]), nil, true
}
}
}
return 0, &AddrError{"unknown port", net + "/" + service}, true
}
示例5: cgoLookupIPCNAME
func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, completed bool) {
acquireThread()
defer releaseThread()
var hints syscall.Addrinfo
hints.Ai_flags = int32(cgoAddrInfoFlags)
hints.Ai_socktype = syscall.SOCK_STREAM
h := syscall.StringBytePtr(name)
var res *syscall.Addrinfo
syscall.Entersyscall()
gerrno := libc_getaddrinfo(h, nil, &hints, &res)
syscall.Exitsyscall()
if gerrno != 0 {
switch gerrno {
case syscall.EAI_SYSTEM:
errno := syscall.GetErrno()
if errno == 0 {
// err should not be nil, but sometimes getaddrinfo returns
// gerrno == C.EAI_SYSTEM with err == nil on Linux.
// The report claims that it happens when we have too many
// open files, so use syscall.EMFILE (too many open files in system).
// Most system calls would return ENFILE (too many open files),
// so at the least EMFILE should be easy to recognize if this
// comes up again. golang.org/issue/6232.
errno = syscall.EMFILE
}
err = errno
case syscall.EAI_NONAME:
err = errNoSuchHost
default:
err = addrinfoErrno(gerrno)
}
return nil, "", &DNSError{Err: err.Error(), Name: name}, true
}
defer libc_freeaddrinfo(res)
if res != nil {
cname = bytePtrToString((*byte)(unsafe.Pointer(res.Ai_canonname)))
if cname == "" {
cname = name
}
if len(cname) > 0 && cname[len(cname)-1] != '.' {
cname += "."
}
}
for r := res; r != nil; r = r.Ai_next {
// We only asked for SOCK_STREAM, but check anyhow.
if r.Ai_socktype != syscall.SOCK_STREAM {
continue
}
switch r.Ai_family {
case syscall.AF_INET:
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.Ai_addr))
addr := IPAddr{IP: copyIP(sa.Addr[:])}
addrs = append(addrs, addr)
case syscall.AF_INET6:
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.Ai_addr))
addr := IPAddr{IP: copyIP(sa.Addr[:]), Zone: zoneToString(int(sa.Scope_id))}
addrs = append(addrs, addr)
}
}
return addrs, cname, nil, true
}