本文整理汇总了Golang中net.IP.IsUnspecified方法的典型用法代码示例。如果您正苦于以下问题:Golang IP.IsUnspecified方法的具体用法?Golang IP.IsUnspecified怎么用?Golang IP.IsUnspecified使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.IP
的用法示例。
在下文中一共展示了IP.IsUnspecified方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: validateRemoteAddr
func validateRemoteAddr(ip net.IP) bool {
if ip == nil {
return false
}
if ip.IsInterfaceLocalMulticast() {
return false
}
if ip.IsLinkLocalMulticast() {
return false
}
if ip.IsLinkLocalUnicast() {
return false
}
if ip.IsLoopback() {
return false
}
if ip.IsMulticast() {
return false
}
if ip.IsUnspecified() {
return false
}
if isBroadcasty(ip) {
return false
}
return true
}
示例2: getFirstLocalIPAddr
// getFirstLocalIPAddr returns the first available IP address of the local machine
// This is a fix for Beaglebone Black where net.LookupIP(hostname) return no IP address.
func getFirstLocalIPAddr() (net.IP, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() || ip.IsUnspecified() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip, nil
}
return nil, errors.New("Could not determine ip address")
}
示例3: GetFirstInterface
func GetFirstInterface() (name string, ip string) {
ifaces, _ := net.Interfaces()
for _, iface := range ifaces {
addrs, _ := iface.Addrs()
ipV4 := false
for _, addr := range addrs {
var ip net.IP
if ipnet, ok := addr.(*net.IPNet); ok {
ip = ipnet.IP
} else if ipaddr, ok := addr.(*net.IPAddr); ok {
ip = ipaddr.IP
}
if ip != nil && ip.To4() != nil && !ip.IsUnspecified() {
ipstr := addr.String()
idx := strings.Index(ipstr, "/")
if idx >= 0 {
ipstr = ipstr[:idx]
}
return iface.Name, ipstr
}
}
if !ipV4 {
continue
}
}
return "", "0.0.0.0"
}
示例4: forward
// adapted from https://github.com/docker/libnetwork/blob/master/iptables/iptables.go
func (p *portMapper) forward(action iptables.Action, ip net.IP, port int, proto, destAddr string, destPort int, srcIface, destIface string) error {
daddr := ip.String()
if ip == nil || ip.IsUnspecified() {
// iptables interprets "0.0.0.0" as "0.0.0.0/32", whereas we
// want "0.0.0.0/0". "0/0" is correctly interpreted as "any
// value" by both iptables and ip6tables.
daddr = "0/0"
}
args := []string{"-t", string(iptables.Nat), string(action), "VIC",
"-i", srcIface,
"-p", proto,
"-d", daddr,
"--dport", strconv.Itoa(port),
"-j", "DNAT",
"--to-destination", net.JoinHostPort(destAddr, strconv.Itoa(destPort))}
if output, err := iptables.Raw(args...); err != nil {
return err
} else if len(output) != 0 {
return iptables.ChainError{Chain: "FORWARD", Output: output}
}
ipStr := ""
if ip != nil && !ip.IsUnspecified() {
ipStr = ip.String()
}
switch action {
case iptables.Append:
p.bindings[bindKey{ipStr, port}] = nil
case iptables.Delete:
delete(p.bindings, bindKey{ipStr, port})
}
if output, err := iptables.Raw("-t", string(iptables.Filter), string(action), "VIC",
"-i", srcIface,
"-o", destIface,
"-p", proto,
"-d", destAddr,
"--dport", strconv.Itoa(destPort),
"-j", "ACCEPT"); err != nil {
return err
} else if len(output) != 0 {
return iptables.ChainError{Chain: "FORWARD", Output: output}
}
if output, err := iptables.Raw("-t", string(iptables.Nat), string(action), "POSTROUTING",
"-p", proto,
"-d", destAddr,
"--dport", strconv.Itoa(destPort),
"-j", "MASQUERADE"); err != nil {
return err
} else if len(output) != 0 {
return iptables.ChainError{Chain: "FORWARD", Output: output}
}
return nil
}
示例5: ValidPublicAddrForMaster
// If bind-address is usable, return it directly
// If bind-address is not usable (unset, 0.0.0.0, or loopback), we will use the host's default
// interface.
func ValidPublicAddrForMaster(bindAddress net.IP) (net.IP, error) {
if bindAddress == nil || bindAddress.IsUnspecified() || bindAddress.IsLoopback() {
hostIP, err := ChooseHostInterface()
if err != nil {
return nil, err
}
bindAddress = hostIP
}
return bindAddress, nil
}
示例6: validateIP
// Helper function to check if a given IP is not public (non-routable).
func validateIP(ip net.IP) (bool, error) {
if ip.IsLoopback() {
return false, IPisLoopbackError
}
if ip.IsUnspecified() {
return false, IPisUnspecifiedError
}
if isInAddressRange(ip, ipv4Nonpublic, ipv6Nonpublic) {
return false, IPisNotPublicError
}
return true, nil
}
示例7: InterfaceIPs
func InterfaceIPs() ([]*net.IPAddr, error) {
var (
addrs []*net.IPAddr
)
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range ifaces {
iaddrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, iaddr := range iaddrs {
var (
ip net.IP
zone string
)
switch x := iaddr.(type) {
case *net.IPAddr:
ip = x.IP
zone = x.Zone
case *net.IPNet:
ip = x.IP
zone = ""
}
if ip.IsMulticast() ||
ip.IsUnspecified() ||
ip.IsInterfaceLocalMulticast() ||
ip.IsLinkLocalMulticast() ||
ip.IsLinkLocalUnicast() {
continue
}
if ipv4 := ip.To4(); ipv4 != nil {
ip = ipv4
}
addrs = append(addrs, &net.IPAddr{
IP: ip,
Zone: zone,
})
}
}
return addrs, nil
}
示例8: Forward
func (c *Chain) Forward(action Action, ip net.IP, port, rangeEnd int, proto, destAddr string) error {
daddr := ip.String()
if ip.IsUnspecified() {
// iptables interprets "0.0.0.0" as "0.0.0.0/32", whereas we
// want "0.0.0.0/0". "0/0" is correctly interpreted as "any
// value" by both iptables and ip6tables.
daddr = "0/0"
}
if output, err := Raw("-t", "nat", fmt.Sprint(action), c.Name,
"-p", proto,
"-d", daddr,
"--dport", fmt.Sprintf("%d:%d", port, rangeEnd),
"-j", "DNAT",
"--to-destination", destAddr); err != nil && action != Delete {
return err
} else if len(output) != 0 && action != Delete {
return fmt.Errorf("Error iptables forward: %s", output)
}
fAction := action
if fAction == Add {
fAction = "-I"
}
if output, err := Raw(string(fAction), "FORWARD",
"!", "-i", c.Bridge,
"-o", c.Bridge,
"-p", proto,
"-d", destAddr,
"--dport", fmt.Sprintf("%d:%d", port, rangeEnd),
"-j", "ACCEPT"); err != nil && action != Delete {
return err
} else if len(output) != 0 && action != Delete {
return fmt.Errorf("Error iptables forward: %s", output)
}
if output, err := Raw("-t", "nat", string(fAction), "POSTROUTING",
"-p", proto,
"-s", destAddr,
"-d", destAddr,
"--dport", fmt.Sprintf("%d:%d", port, rangeEnd),
"-j", "MASQUERADE"); err != nil && action != Delete {
return err
} else if len(output) != 0 && action != Delete {
return fmt.Errorf("Error iptables forward: %s", output)
}
return nil
}
示例9: Forward
// Forward adds forwarding rule to 'filter' table and corresponding nat rule to 'nat' table.
func (c *ChainInfo) Forward(action Action, ip net.IP, port int, proto, destAddr string, destPort int, bridgeName string) error {
daddr := ip.String()
if ip.IsUnspecified() {
// iptables interprets "0.0.0.0" as "0.0.0.0/32", whereas we
// want "0.0.0.0/0". "0/0" is correctly interpreted as "any
// value" by both iptables and ip6tables.
daddr = "0/0"
}
args := []string{"-t", string(Nat), string(action), c.Name,
"-p", proto,
"-d", daddr,
"--dport", strconv.Itoa(port),
"-j", "DNAT",
"--to-destination", net.JoinHostPort(destAddr, strconv.Itoa(destPort))}
if !c.HairpinMode {
args = append(args, "!", "-i", bridgeName)
}
if output, err := Raw(args...); err != nil {
return err
} else if len(output) != 0 {
return ChainError{Chain: "FORWARD", Output: output}
}
if output, err := Raw("-t", string(Filter), string(action), c.Name,
"!", "-i", bridgeName,
"-o", bridgeName,
"-p", proto,
"-d", destAddr,
"--dport", strconv.Itoa(destPort),
"-j", "ACCEPT"); err != nil {
return err
} else if len(output) != 0 {
return ChainError{Chain: "FORWARD", Output: output}
}
if output, err := Raw("-t", string(Nat), string(action), "POSTROUTING",
"-p", proto,
"-s", destAddr,
"-d", destAddr,
"--dport", strconv.Itoa(destPort),
"-j", "MASQUERADE"); err != nil {
return err
} else if len(output) != 0 {
return ChainError{Chain: "FORWARD", Output: output}
}
return nil
}
示例10: ContainerByAddr
func (s *Scope) ContainerByAddr(addr net.IP) *Endpoint {
s.RLock()
defer s.RUnlock()
if addr == nil || addr.IsUnspecified() {
return nil
}
for _, e := range s.endpoints {
if addr.Equal(e.IP()) {
return e
}
}
return nil
}
示例11: Forward
// Forward adds forwarding rule to 'filter' table and corresponding nat rule to 'nat' table.
func (c *ChainInfo) Forward(action Action, ip net.IP, port int, proto, destAddr string, destPort int, bridgeName string) error {
daddr := ip.String()
if ip.IsUnspecified() {
// iptables interprets "0.0.0.0" as "0.0.0.0/32", whereas we
// want "0.0.0.0/0". "0/0" is correctly interpreted as "any
// value" by both iptables and ip6tables.
daddr = "0/0"
}
args := []string{
"-p", proto,
"-d", daddr,
"--dport", strconv.Itoa(port),
"-j", "DNAT",
"--to-destination", net.JoinHostPort(destAddr, strconv.Itoa(destPort))}
if !c.HairpinMode {
args = append(args, "!", "-i", bridgeName)
}
if err := ProgramRule(Nat, c.Name, action, args); err != nil {
return err
}
args = []string{
"!", "-i", bridgeName,
"-o", bridgeName,
"-p", proto,
"-d", destAddr,
"--dport", strconv.Itoa(destPort),
"-j", "ACCEPT",
}
if err := ProgramRule(Filter, c.Name, action, args); err != nil {
return err
}
args = []string{
"-p", proto,
"-s", destAddr,
"-d", destAddr,
"--dport", strconv.Itoa(destPort),
"-j", "MASQUERADE",
}
if err := ProgramRule(Nat, "POSTROUTING", action, args); err != nil {
return err
}
return nil
}
示例12: getClientConnectURLs
// getClientConnectURLs returns suitable URLs for clients to connect to the listen
// port based on the server options' Host and Port. If the Host corresponds to
// "any" interfaces, this call returns the list of resolved IP addresses.
func (s *Server) getClientConnectURLs() []string {
s.mu.Lock()
defer s.mu.Unlock()
sPort := strconv.Itoa(s.opts.Port)
urls := make([]string, 0, 1)
ipAddr, err := net.ResolveIPAddr("ip", s.opts.Host)
// If the host is "any" (0.0.0.0 or ::), get specific IPs from available
// interfaces.
if err == nil && ipAddr.IP.IsUnspecified() {
var ip net.IP
ifaces, _ := net.Interfaces()
for _, i := range ifaces {
addrs, _ := i.Addrs()
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
// Skip non global unicast addresses
if !ip.IsGlobalUnicast() || ip.IsUnspecified() {
ip = nil
continue
}
urls = append(urls, net.JoinHostPort(ip.String(), sPort))
}
}
}
if err != nil || len(urls) == 0 {
// We are here if s.opts.Host is not "0.0.0.0" nor "::", or if for some
// reason we could not add any URL in the loop above.
// We had a case where a Windows VM was hosed and would have err == nil
// and not add any address in the array in the loop above, and we
// ended-up returning 0.0.0.0, which is problematic for Windows clients.
// Check for 0.0.0.0 or :: specifically, and ignore if that's the case.
if s.opts.Host == "0.0.0.0" || s.opts.Host == "::" {
Errorf("Address %q can not be resolved properly", s.opts.Host)
} else {
urls = append(urls, net.JoinHostPort(s.opts.Host, sPort))
}
}
return urls
}
示例13: Forward
func (c *Chain) Forward(action Action, ip net.IP, port int, proto, dest_addr string, dest_port int) error {
daddr := ip.String()
if ip.IsUnspecified() {
// iptables interprets "0.0.0.0" as "0.0.0.0/32", whereas we
// want "0.0.0.0/0". "0/0" is correctly interpreted as "any
// value" by both iptables and ip6tables.
daddr = "0/0"
}
if output, err := Raw("-t", "nat", fmt.Sprint(action), c.Name,
"-p", proto,
"-d", daddr,
"--dport", strconv.Itoa(port),
"!", "-i", c.Bridge,
"-j", "DNAT",
"--to-destination", net.JoinHostPort(dest_addr, strconv.Itoa(dest_port))); err != nil {
return err
} else if len(output) != 0 {
return fmt.Errorf("Error iptables forward: %s", output)
}
return nil
}
示例14: RunKubelet
// RunKubelet starts the Kubelet.
func (c *NodeConfig) RunKubelet() {
var clusterDNS net.IP
if c.KubeletServer.ClusterDNS == "" {
if service, err := c.Client.Services(kapi.NamespaceDefault).Get("kubernetes"); err == nil {
if includesServicePort(service.Spec.Ports, 53, "dns") {
// Use master service if service includes "dns" port 53.
clusterDNS = net.ParseIP(service.Spec.ClusterIP)
}
}
}
if clusterDNS == nil {
if endpoint, err := c.Client.Endpoints(kapi.NamespaceDefault).Get("kubernetes"); err == nil {
if endpointIP, ok := firstEndpointIPWithNamedPort(endpoint, 53, "dns"); ok {
// Use first endpoint if endpoint includes "dns" port 53.
clusterDNS = net.ParseIP(endpointIP)
} else if endpointIP, ok := firstEndpointIP(endpoint, 53); ok {
// Test and use first endpoint if endpoint includes any port 53.
if err := cmdutil.WaitForSuccessfulDial(false, "tcp", fmt.Sprintf("%s:%d", endpointIP, 53), 50*time.Millisecond, 0, 2); err == nil {
clusterDNS = net.ParseIP(endpointIP)
}
}
}
}
if clusterDNS != nil && !clusterDNS.IsUnspecified() {
c.KubeletServer.ClusterDNS = clusterDNS.String()
}
c.KubeletDeps.DockerClient = c.DockerClient
// updated by NodeConfig.EnsureVolumeDir
c.KubeletServer.RootDirectory = c.VolumeDir
// hook for overriding the cadvisor interface for integration tests
c.KubeletDeps.CAdvisorInterface = defaultCadvisorInterface
// hook for overriding the container manager interface for integration tests
c.KubeletDeps.ContainerManager = defaultContainerManagerInterface
go func() {
glog.Fatal(kubeletapp.Run(c.KubeletServer, c.KubeletDeps))
}()
}
示例15: isPortAvailable
func (p *portMapper) isPortAvailable(proto string, ip net.IP, port int) bool {
addr := ""
if ip != nil && !ip.IsUnspecified() {
addr = ip.String()
}
if _, ok := p.bindings[bindKey{addr, port}]; ok {
return false
}
c, err := net.Dial(proto, net.JoinHostPort(addr, strconv.Itoa(port)))
defer func() {
if c != nil {
c.Close()
}
}()
if err != nil {
return true
}
return false
}