本文整理匯總了Golang中C.struct_addrinfo.ai_protocol方法的典型用法代碼示例。如果您正苦於以下問題:Golang struct_addrinfo.ai_protocol方法的具體用法?Golang struct_addrinfo.ai_protocol怎麽用?Golang struct_addrinfo.ai_protocol使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類C.struct_addrinfo
的用法示例。
在下文中一共展示了struct_addrinfo.ai_protocol方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: cgoLookupPort
func cgoLookupPort(network, service string) (port int, err error, completed bool) {
acquireThread()
defer releaseThread()
var hints C.struct_addrinfo
switch network {
case "": // no hints
case "tcp", "tcp4", "tcp6":
hints.ai_socktype = C.SOCK_STREAM
hints.ai_protocol = C.IPPROTO_TCP
case "udp", "udp4", "udp6":
hints.ai_socktype = C.SOCK_DGRAM
hints.ai_protocol = C.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 = C.AF_INET
case '6':
hints.ai_family = C.AF_INET6
}
}
s := C.CString(service)
var res *C.struct_addrinfo
defer C.free(unsafe.Pointer(s))
gerrno, err := C.getaddrinfo(nil, s, &hints, &res)
if gerrno != 0 {
switch gerrno {
case C.EAI_SYSTEM:
if err == nil { // see golang.org/issue/6232
err = syscall.EMFILE
}
default:
err = addrinfoErrno(gerrno)
}
return 0, &DNSError{Err: err.Error(), Name: network + "/" + service}, true
}
defer C.freeaddrinfo(res)
for r := res; r != nil; r = r.ai_next {
switch r.ai_family {
case C.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 C.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
}
示例2: cgoLookupPort
func cgoLookupPort(net, service string) (port int, err error, completed bool) {
acquireThread()
defer releaseThread()
var res *C.struct_addrinfo
var hints C.struct_addrinfo
switch net {
case "":
// no hints
case "tcp", "tcp4", "tcp6":
hints.ai_socktype = C.SOCK_STREAM
hints.ai_protocol = C.IPPROTO_TCP
case "udp", "udp4", "udp6":
hints.ai_socktype = C.SOCK_DGRAM
hints.ai_protocol = C.IPPROTO_UDP
default:
return 0, UnknownNetworkError(net), true
}
if len(net) >= 4 {
switch net[3] {
case '4':
hints.ai_family = C.AF_INET
case '6':
hints.ai_family = C.AF_INET6
}
}
s := C.CString(service)
defer C.free(unsafe.Pointer(s))
if C.getaddrinfo(nil, s, &hints, &res) == 0 {
defer C.freeaddrinfo(res)
for r := res; r != nil; r = r.ai_next {
switch r.ai_family {
default:
continue
case C.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 C.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
}
示例3: cgoLookupPort
func cgoLookupPort(ctx context.Context, network, service string) (port int, err error, completed bool) {
var hints C.struct_addrinfo
switch network {
case "": // no hints
case "tcp", "tcp4", "tcp6":
hints.ai_socktype = C.SOCK_STREAM
hints.ai_protocol = C.IPPROTO_TCP
case "udp", "udp4", "udp6":
hints.ai_socktype = C.SOCK_DGRAM
hints.ai_protocol = C.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 = C.AF_INET
case '6':
hints.ai_family = C.AF_INET6
}
}
if ctx.Done() == nil {
port, err := cgoLookupServicePort(&hints, network, service)
return port, err, true
}
result := make(chan portLookupResult, 1)
go cgoPortLookup(result, &hints, network, service)
select {
case r := <-result:
return r.port, r.err, true
case <-ctx.Done():
// Since there isn't a portable way to cancel the lookup,
// we just let it finish and write to the buffered channel.
return 0, mapErr(ctx.Err()), false
}
}