本文整理汇总了Golang中net.LookupPort函数的典型用法代码示例。如果您正苦于以下问题:Golang LookupPort函数的具体用法?Golang LookupPort怎么用?Golang LookupPort使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LookupPort函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Port
func Port(networkType, service string) {
port, err := net.LookupPort(networkType, service)
if err != nil {
log.Fatal("Error: ", err.Error())
}
fmt.Println("Service port ", port)
}
示例2: FuzzySplitHostPortI
// Like FuzzySplitHostPort, but returns an integer for the port. Supports
// symbolic port names (e.g. "http"). If the port name is unknown, returns error.
//
// "net" must be the port namespace (e.g. "tcp" or "udp"). If "", the port must
// be given as an integer.
//
// If a port was not specified, port is 0.
func FuzzySplitHostPortI(network, hostport string) (host string, port uint16, err error) {
host, ports, err := FuzzySplitHostPort(hostport)
if err != nil {
return
}
if ports == "" {
return
}
px, err := strconv.ParseUint(ports, 10, 16)
p := int(px)
if err != nil {
if network == "" {
err = &net.AddrError{Err: "port must be numeric", Addr: ports}
return
}
p, err = net.LookupPort(network, ports)
if err != nil {
return
}
}
if p < 0 || p > 0xFFFF {
err = &net.AddrError{Err: "invalid port", Addr: ports}
return
}
port = uint16(p)
return
}
示例3: Set
func (bv *bindValue) Set(input string) error {
if input == "" {
return nil
}
if !strings.Contains(input, ":") {
input = ":" + input
}
var err error
bv.Host, bv.Port, err = net.SplitHostPort(input)
if err != nil {
return err
}
if bv.Host == "*" {
bv.Host = ""
} else if bv.Port == "127" {
bv.Host = "127.0.0.1"
bv.Port = bv.defport
}
if _, err = net.LookupPort("tcp", bv.Port); err != nil {
if bv.Host != "" {
return err
}
bv.Host, bv.Port = bv.Port, bv.defport
}
bv.string = bv.Host + ":" + bv.Port
return nil
}
示例4: Set
func (b *Bind) Set(input string) error {
if input == "" {
b.Port = b.defport
} else {
if !strings.Contains(input, ":") {
input = ":" + input
}
var err error
b.Host, b.Port, err = net.SplitHostPort(input)
if err != nil {
return err
}
if b.Host == "*" {
b.Host = ""
} else if b.Port == "127" {
b.Host = "127.0.0.1"
b.Port = b.defport
}
if _, err = net.LookupPort("tcp", b.Port); err != nil {
if b.Host != "" {
return err
}
b.Host, b.Port = b.Port, b.defport
}
}
b.string = b.Host + ":" + b.Port
return nil
}
示例5: parseaddr
func parseaddr(bind_spec, defport string) (string, error) {
if !strings.Contains(bind_spec, ":") {
bind_spec = ":" + bind_spec
}
host, port, err := net.SplitHostPort(bind_spec)
if err != nil {
return "", err
}
if host == "*" {
host = ""
} else if port == "127" {
host = "127.0.0.1"
port = defport
}
if _, err = net.LookupPort("tcp", port); err != nil {
if host != "" {
return "", err
}
host = port
port = defport
}
if err = os.Setenv("HOST", host); err != nil {
return "", err
}
if err = os.Setenv("PORT", port); err != nil {
return "", err
}
return host + ":" + port, nil
}
示例6: GetOptions
func GetOptions() *Options {
flag.Parse()
self := &Options{}
self.AgentConf = *flagAgentConf
self.ApiPasswordPath = *flagApiPasswordPath
self.CfTenantId = *flagCfTenantId
self.ListenAddress = *flagListenAddress
host, port, err := net.SplitHostPort(self.ListenAddress)
if err != nil {
log.Warn("Cannot parse listen address: %v", self.ListenAddress)
return nil
}
var portNum int
if port == "" {
portNum = 8080
} else {
portNum, err = net.LookupPort("tcp", port)
if err != nil {
log.Warn("Cannot resolve port: %v", port)
return nil
}
}
privateUrl := *flagPrivateUrl
if privateUrl == "" {
privateHost := host
if privateHost == "" {
ip, err := localIP()
if err != nil {
log.Warn("Error finding local IP", err)
return nil
}
privateHost = ip.String()
}
privateUrl = fmt.Sprintf("http://%v:%v/xaasprivate", privateHost, portNum)
log.Info("Chose private url: %v", privateUrl)
}
self.PrivateUrl = privateUrl
authMode := *flagAuth
authMode = strings.TrimSpace(authMode)
authMode = strings.ToLower(authMode)
if authMode == "openstack" {
keystoneUrl := *flagKeystoneUrl
self.Authenticator = auth.NewOpenstackMultiAuthenticator(keystoneUrl)
} else if authMode == "development" {
self.Authenticator = auth.NewDevelopmentAuthenticator()
} else {
log.Warn("Unknown authentication mode: %v", authMode)
return nil
}
return self
}
示例7: main
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s service [network-type] \n", os.Args[0])
os.Exit(1)
}
service := os.Args[1]
var networkType string
if len(os.Args) == 3 {
networkType = os.Args[2]
port, err := net.LookupPort(networkType, service)
if err != nil {
fmt.Println("Error: ", err.Error())
os.Exit(2)
}
fmt.Printf("Service %s network Type %s port %d\n", service, networkType, port)
} else {
networkTypes := []string{"tcp", "udp"}
var output string
for _, networkType := range networkTypes {
port, err := net.LookupPort(networkType, service)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
continue
} else {
output = fmt.Sprintf("%s %s/%d", output, networkType, port)
}
}
if len(output) > 0 {
fmt.Printf("Service %s port(s): %s\n", service, output)
}
}
os.Exit(0)
}
示例8: setWKS
func setWKS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
rr := new(WKS)
rr.Hdr = h
l := <-c
if l.length == 0 {
return rr, nil, l.comment
}
rr.Address = net.ParseIP(l.token)
if rr.Address == nil {
return nil, &ParseError{f, "bad WKS Address", l}, ""
}
<-c // zBlank
l = <-c
proto := "tcp"
i, e := strconv.Atoi(l.token)
if e != nil {
return nil, &ParseError{f, "bad WKS Protocol", l}, ""
}
rr.Protocol = uint8(i)
switch rr.Protocol {
case 17:
proto = "udp"
case 6:
proto = "tcp"
default:
return nil, &ParseError{f, "bad WKS Protocol", l}, ""
}
<-c
l = <-c
rr.BitMap = make([]uint16, 0)
var (
k int
err error
)
for l.value != zNewline && l.value != zEOF {
switch l.value {
case zBlank:
// Ok
case zString:
if k, err = net.LookupPort(proto, l.token); err != nil {
if i, e := strconv.Atoi(l.token); e != nil { // If a number use that
return nil, &ParseError{f, "bad WKS BitMap", l}, ""
} else {
rr.BitMap = append(rr.BitMap, uint16(i))
}
}
rr.BitMap = append(rr.BitMap, uint16(k))
default:
return nil, &ParseError{f, "bad WKS BitMap", l}, ""
}
l = <-c
}
return rr, nil, l.comment
}
示例9: TLSAName
// TLSAName returns the ownername of a TLSA resource record as per the
// rules specified in RFC 6698, Section 3.
func TLSAName(name, service, network string) (string, error) {
if !IsFqdn(name) {
return "", ErrFqdn
}
p, e := net.LookupPort(network, service)
if e != nil {
return "", e
}
return "_" + strconv.Itoa(p) + "._" + network + "." + name, nil
}
示例10: TLSAName
// TLSAName returns the ownername of a TLSA resource record as per the
// rules specified in RFC 6698, Section 3. When an error occurs the
// empty string is returned.
func TLSAName(name, service, network string) string {
if !IsFqdn(name) {
return ""
}
p, e := net.LookupPort(network, service)
if e != nil {
return ""
}
return "_" + strconv.Itoa(p) + "_" + network + "." + name
}
示例11: validateFlags
func validateFlags(logger lager.Logger) {
_, portString, err := net.SplitHostPort(*listenAddress)
if err != nil {
logger.Fatal("failed-invalid-listen-address", err)
}
_, err = net.LookupPort("tcp", portString)
if err != nil {
logger.Fatal("failed-invalid-listen-port", err)
}
}
示例12: Set
func (p *policyPortValue) Set(value string) error {
port, err := strconv.Atoi(value)
if err != nil {
port, err = net.LookupPort("", value)
if err != nil {
return err
}
}
*p = policyPortValue(port)
return nil
}
示例13: setWKS
func setWKS(h RR_Header, c chan lex, f string) (RR, *ParseError) {
rr := new(RR_WKS)
rr.Hdr = h
l := <-c
rr.Address = net.ParseIP(l.token)
if rr.Address == nil {
return nil, &ParseError{f, "bad WKS Adress", l}
}
<-c // _BLANK
l = <-c
proto := "tcp"
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad WKS Protocol", l}
} else {
rr.Protocol = uint8(i)
switch rr.Protocol {
case 17:
proto = "udp"
case 6:
proto = "tcp"
default:
return nil, &ParseError{f, "bad WKS Protocol", l}
}
}
<-c
l = <-c
rr.BitMap = make([]uint16, 0)
var (
k int
err error
)
for l.value != _NEWLINE && l.value != _EOF {
switch l.value {
case _BLANK:
// Ok
case _STRING:
if k, err = net.LookupPort(proto, l.token); err != nil {
if i, e := strconv.Atoi(l.token); e != nil { // If a number use that
rr.BitMap = append(rr.BitMap, uint16(i))
} else {
return nil, &ParseError{f, "bad WKS BitMap", l}
}
}
rr.BitMap = append(rr.BitMap, uint16(k))
default:
return nil, &ParseError{f, "bad WKS BitMap", l}
}
l = <-c
}
return rr, nil
}
示例14: main
func main() {
if len(os.Args) != 3 {
log.Fatalf("Usage %s network-type service", os.Args[0])
}
networkType := os.Args[1]
service := os.Args[2]
port, err := net.LookupPort(networkType, service)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Service port %d", port)
}
示例15: WriteAt
func (h *csHandle) WriteAt(p []byte, _ int64) (int, error) {
h.buflock.Lock()
defer h.buflock.Unlock()
cmd := strings.Split(strings.TrimSpace(string(p)), "!")
switch len(cmd) {
default:
return len(p), errors.New("invalid query")
case 2:
h.buf = append(h.buf, []byte(fmt.Sprintf("/net/%s/clone %s", cmd[0], cmd[1])))
return len(p), nil
case 3:
break
}
if cmd[0] == "net" {
// Whatever
cmd[0] = "tcp"
}
switch cmd[2] {
case "9fs", "9pfs":
cmd[2] = "564"
}
port, err := net.LookupPort(cmd[0], cmd[2])
if err != nil {
return len(p), err
}
var addrs []string
if cmd[1] == "*" {
addrs = []string{""}
} else {
addrs, err = net.LookupHost(cmd[1])
if err != nil {
return len(p), err
}
}
for _, addr := range addrs {
var a string
if addr == "" {
a = fmt.Sprintf("%d", port)
} else {
a = fmt.Sprintf("%s!%d", addr, port)
}
h.buf = append(h.buf, []byte(fmt.Sprintf("/net/%s/clone %s", cmd[0], a)))
}
return len(p), err
}