| |
| |
| |
|
|
| package net |
|
|
| import ( |
| "cmp" |
| "internal/bytealg" |
| "internal/strconv" |
| "slices" |
| _ "unsafe" |
|
|
| "golang.org/x/net/dns/dnsmessage" |
| ) |
|
|
| |
| |
| |
| func runtime_rand() uint64 |
|
|
| func randInt() int { |
| return int(uint(runtime_rand()) >> 1) |
| } |
|
|
| func randIntn(n int) int { |
| return randInt() % n |
| } |
|
|
| |
| |
| |
| func reverseaddr(addr string) (arpa string, err error) { |
| ip := ParseIP(addr) |
| if ip == nil { |
| return "", &DNSError{Err: "unrecognized address", Name: addr} |
| } |
| if ip.To4() != nil { |
| return strconv.Itoa(int(ip[15])) + "." + strconv.Itoa(int(ip[14])) + "." + strconv.Itoa(int(ip[13])) + "." + strconv.Itoa(int(ip[12])) + ".in-addr.arpa.", nil |
| } |
| |
| buf := make([]byte, 0, len(ip)*4+len("ip6.arpa.")) |
| |
| for i := len(ip) - 1; i >= 0; i-- { |
| v := ip[i] |
| buf = append(buf, hexDigit[v&0xF], |
| '.', |
| hexDigit[v>>4], |
| '.') |
| } |
| |
| buf = append(buf, "ip6.arpa."...) |
| return string(buf), nil |
| } |
|
|
| func equalASCIIName(x, y dnsmessage.Name) bool { |
| if x.Length != y.Length { |
| return false |
| } |
| for i := 0; i < int(x.Length); i++ { |
| a := x.Data[i] |
| b := y.Data[i] |
| if 'A' <= a && a <= 'Z' { |
| a += 0x20 |
| } |
| if 'A' <= b && b <= 'Z' { |
| b += 0x20 |
| } |
| if a != b { |
| return false |
| } |
| } |
| return true |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func isDomainName(s string) bool { |
| |
| if s == "." { |
| return true |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| l := len(s) |
| if l == 0 || l > 254 || l == 254 && s[l-1] != '.' { |
| return false |
| } |
|
|
| last := byte('.') |
| nonNumeric := false |
| partlen := 0 |
| for i := 0; i < len(s); i++ { |
| c := s[i] |
| switch { |
| default: |
| return false |
| case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_': |
| nonNumeric = true |
| partlen++ |
| case '0' <= c && c <= '9': |
| |
| partlen++ |
| case c == '-': |
| |
| if last == '.' { |
| return false |
| } |
| partlen++ |
| nonNumeric = true |
| case c == '.': |
| |
| if last == '.' || last == '-' { |
| return false |
| } |
| if partlen > 63 || partlen == 0 { |
| return false |
| } |
| partlen = 0 |
| } |
| last = c |
| } |
| if last == '-' || partlen > 63 { |
| return false |
| } |
|
|
| return nonNumeric |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func absDomainName(s string) string { |
| if bytealg.IndexByteString(s, '.') != -1 && s[len(s)-1] != '.' { |
| s += "." |
| } |
| return s |
| } |
|
|
| |
| type SRV struct { |
| Target string |
| Port uint16 |
| Priority uint16 |
| Weight uint16 |
| } |
|
|
| |
| type byPriorityWeight []*SRV |
|
|
| |
| |
| func (addrs byPriorityWeight) shuffleByWeight() { |
| sum := 0 |
| for _, addr := range addrs { |
| sum += int(addr.Weight) |
| } |
| for sum > 0 && len(addrs) > 1 { |
| s := 0 |
| n := randIntn(sum) |
| for i := range addrs { |
| s += int(addrs[i].Weight) |
| if s > n { |
| if i > 0 { |
| addrs[0], addrs[i] = addrs[i], addrs[0] |
| } |
| break |
| } |
| } |
| sum -= int(addrs[0].Weight) |
| addrs = addrs[1:] |
| } |
| } |
|
|
| |
| func (addrs byPriorityWeight) sort() { |
| slices.SortFunc(addrs, func(a, b *SRV) int { |
| if r := cmp.Compare(a.Priority, b.Priority); r != 0 { |
| return r |
| } |
| return cmp.Compare(a.Weight, b.Weight) |
| }) |
| i := 0 |
| for j := 1; j < len(addrs); j++ { |
| if addrs[i].Priority != addrs[j].Priority { |
| addrs[i:j].shuffleByWeight() |
| i = j |
| } |
| } |
| addrs[i:].shuffleByWeight() |
| } |
|
|
| |
| type MX struct { |
| Host string |
| Pref uint16 |
| } |
|
|
| |
| type byPref []*MX |
|
|
| |
| func (s byPref) sort() { |
| for i := range s { |
| j := randIntn(i + 1) |
| s[i], s[j] = s[j], s[i] |
| } |
| slices.SortFunc(s, func(a, b *MX) int { |
| return cmp.Compare(a.Pref, b.Pref) |
| }) |
| } |
|
|
| |
| type NS struct { |
| Host string |
| } |
|
|