func (a AddrVer) AddrAllowed(ip string) bool { if utilNet.IsValidIpv6(ip) && (a == Any || a == Ipv6) { return true } else if utilNet.IsValidIpv4(ip) && (a == Any || a == Ipv4) { return true } return false }
func (s *Server) ipver(addr net.Addr) { a := addr.String() if utilNet.IsValidIpv6(a) { s.AddrVer = Ipv6 } else if utilNet.IsValidIpv4(a) { s.AddrVer = Ipv4 } }
func lookupHost(host string, config *dns.ClientConfig) (addrs []string, err error) { if utilNet.IsValidIpv4(host) || utilNet.IsValidIpv6(host) { return []string{host}, nil } if host == "localhost" { return []string{"127.0.0.1", "::1"}, nil } c := new(dns.Client) c.DialTimeout = DialTimeout c.ReadTimeout = ReadTimeout c.WriteTimeout = WriteTimeout m := new(dns.Msg) m.SetQuestion(dns.Fqdn(host), dns.TypeA) var r *dns.Msg for i := 0; i < len(config.Servers); i++ { r, _, err = c.Exchange(m, config.Servers[i]+":"+config.Port) if err != nil { continue } err = nil } if err != nil { return nil, e.Forward(err) } if r.Rcode != dns.RcodeSuccess { return nil, e.New("can't resolve %v", host) } addrs = make([]string, 0, 10) for _, a := range r.Answer { if addr, ok := a.(*dns.A); ok { addrs = append(addrs, addr.A.String()) } } m.SetQuestion(dns.Fqdn(host), dns.TypeAAAA) for i := 0; i < len(config.Servers); i++ { r, _, err = c.Exchange(m, config.Servers[0]+":"+config.Port) if err != nil { continue } err = nil } if err != nil { return nil, e.Forward(err) } if r.Rcode != dns.RcodeSuccess { return nil, e.New("no success") } for _, a := range r.Answer { if addr, ok := a.(*dns.AAAA); ok { addrs = append(addrs, addr.AAAA.String()) } } return }
func ipport(in, ip, port string) (string, error) { if utilNet.IsValidIpv4(ip) { return ip + ":" + port, nil } else if utilNet.IsValidIpv6(ip) { return "[" + ip + "%" + in + "]:" + port, nil } else { return "", e.New("invalid ip address") } }
func LookupIp(ip string) (host string, err error) { if !utilNet.IsValidIpv4(ip) && !utilNet.IsValidIpv6(ip) { return "", e.New("not a valid ip address") } if ip == "127.0.0.1" || ip == "::1" { return "localhost", nil } config, err := dns.ClientConfigFromFile(ConfigurationFile) if err != nil { return "", e.Forward(err) } config.Timeout = Timeout c := new(dns.Client) c.DialTimeout = DialTimeout c.ReadTimeout = ReadTimeout c.WriteTimeout = WriteTimeout m := new(dns.Msg) rev, err := dns.ReverseAddr(ip) if err != nil { return "", e.Forward(err) } m.SetQuestion(rev, dns.TypePTR) var r *dns.Msg for i := 0; i < len(config.Servers); i++ { r, _, err = c.Exchange(m, config.Servers[i]+":"+config.Port) if err != nil { continue } err = nil } if err != nil { return "", e.Forward(err) } if r.Rcode != dns.RcodeSuccess { return "", e.New("can't resolve %v", ip) } for _, a := range r.Answer { if ptr, ok := a.(*dns.PTR); ok { return strings.TrimSuffix(ptr.Ptr, "."), nil } } return "", e.New("no ptr available") }
func broadcast(addr net.Addr, port string) (*net.UDPAddr, error) { p, err := strconv.ParseInt(port, 10, 32) if err != nil { return nil, e.New(err) } udpAddr := &net.UDPAddr{Port: int(p)} a, ok := addr.(*net.UDPAddr) if !ok { return nil, e.New("addr isn't an *UDPAddr") } if utilNet.IsValidIpv4(a.IP.String()) { udpAddr.IP = net.IPv4bcast } else if utilNet.IsValidIpv6(a.IP.String()) { udpAddr.IP = net.IPv6linklocalallnodes udpAddr.Zone = a.Zone } else { return nil, e.New("invalid ip address") } return udpAddr, nil }
func (c *Client) multicast(addr net.Addr) (*net.UDPAddr, error) { host, _, err := utilNet.SplitHostPort(addr.String()) if err != nil { return nil, e.Forward(err) } if utilNet.IsValidIpv4(host) { addr, err := net.ResolveUDPAddr("udp", c.McIpv4+":"+c.Port) if err != nil { return nil, e.Forward(err) } return addr, nil } else if utilNet.IsValidIpv6(host) { addr, err := net.ResolveUDPAddr("udp", c.McIpv6+":"+c.Port) if err != nil { return nil, e.Forward(err) } return addr, nil } else { return nil, e.New("invalid ip address") } }
func (a *Server) haveIpv6() (bool, error) { ipv4 := false addrs, err := a.iface.Addrs() if err != nil { return false, e.New(err) } for _, addr := range addrs { a := addr.String() i := strings.Index(a, "/") if i != -1 { a = a[:i] } if utilNet.IsValidIpv6(a) { return true, nil } else if utilNet.IsValidIpv4(a) { ipv4 = true } } if ipv4 { return false, nil } return false, e.New("no valid ip address") }