Home | History | Annotate | Download | only in net

Lines Matching refs:IP

5 // IP address manipulations
15 // IP address lengths (bytes).
21 // An IP is a single IP address, a slice of bytes.
26 // IP address as an IPv4 address or an IPv6 address
30 type IP []byte
32 // An IP mask is an IP address.
35 // An IPNet represents an IP network.
37 IP IP // network number
41 // IPv4 returns the IP address (in 16-byte form) of the
43 func IPv4(a, b, c, d byte) IP {
44 p := make(IP, IPv6len)
55 // IPv4Mask returns the IP mask (in 4-byte form) of the
101 IPv6zero = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
102 IPv6unspecified = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
103 IPv6loopback = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
104 IPv6interfacelocalallnodes = IP{0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
105 IPv6linklocalallnodes = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
106 IPv6linklocalallrouters = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02}
109 // IsUnspecified reports whether ip is an unspecified address.
110 func (ip IP) IsUnspecified() bool {
111 return ip.Equal(IPv4zero) || ip.Equal(IPv6unspecified)
114 // IsLoopback reports whether ip is a loopback address.
115 func (ip IP) IsLoopback() bool {
116 if ip4 := ip.To4(); ip4 != nil {
119 return ip.Equal(IPv6loopback)
122 // IsMulticast reports whether ip is a multicast address.
123 func (ip IP) IsMulticast() bool {
124 if ip4 := ip.To4(); ip4 != nil {
127 return len(ip) == IPv6len && ip[0] == 0xff
130 // IsInterfaceLocalMulticast reports whether ip is
132 func (ip IP) IsInterfaceLocalMulticast() bool {
133 return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x01
136 // IsLinkLocalMulticast reports whether ip is a link-local
138 func (ip IP) IsLinkLocalMulticast() bool {
139 if ip4 := ip.To4(); ip4 != nil {
142 return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x02
145 // IsLinkLocalUnicast reports whether ip is a link-local
147 func (ip IP) IsLinkLocalUnicast() bool {
148 if ip4 := ip.To4(); ip4 != nil {
151 return len(ip) == IPv6len && ip[0] == 0xfe && ip[1]&0xc0 == 0x80
154 // IsGlobalUnicast reports whether ip is a global unicast
160 // It returns true even if ip is in IPv4 private address space or
162 func (ip IP) IsGlobalUnicast() bool {
163 return (len(ip) == IPv4len || len(ip) == IPv6len) &&
164 !ip.Equal(IPv4bcast) &&
165 !ip.IsUnspecified() &&
166 !ip.IsLoopback() &&
167 !ip.IsMulticast() &&
168 !ip.IsLinkLocalUnicast()
172 func isZeros(p IP) bool {
181 // To4 converts the IPv4 address ip to a 4-byte representation.
182 // If ip is not an IPv4 address, To4 returns nil.
183 func (ip IP) To4() IP {
184 if len(ip) == IPv4len {
185 return ip
187 if len(ip) == IPv6len &&
188 isZeros(ip[0:10]) &&
189 ip[10] == 0xff &&
190 ip[11] == 0xff {
191 return ip[12:16]
196 // To16 converts the IP address ip to a 16-byte representation.
197 // If ip is not an IP address (it is the wrong length), To16 returns nil.
198 func (ip IP) To16() IP {
199 if len(ip) == IPv4len {
200 return IPv4(ip[0], ip[1], ip[2], ip[3])
202 if len(ip) == IPv6len {
203 return ip
215 // DefaultMask returns the default IP mask for the IP address ip.
217 // nil if ip is not a valid IPv4 address.
218 func (ip IP) DefaultMask() IPMask {
219 if ip = ip.To4(); ip == nil {
223 case ip[0] < 0x80:
225 case ip[0] < 0xC0:
241 // Mask returns the result of masking the IP address ip with mask.
242 func (ip IP) Mask(mask IPMask) IP {
243 if len(mask) == IPv6len && len(ip) == IPv4len && allFF(mask[:12]) {
246 if len(mask) == IPv4len && len(ip) == IPv6len && bytesEqual(ip[:12], v4InV6Prefix) {
247 ip = ip[12:]
249 n := len(ip)
253 out := make(IP, n)
255 out[i] = ip[i] & mask[i]
260 // String returns the string form of the IP address ip.
262 // - "<nil>", if ip has length 0
263 // - dotted decimal ("192.0.2.1"), if ip is an IPv4 or IP4-mapped IPv6 address
264 // - IPv6 ("2001:db8::1"), if ip is a valid IPv6 address
265 // - the hexadecimal form of ip, without punctuation, if no other cases apply
266 func (ip IP) String() string {
267 p := ip
269 if len(ip) == 0 {
281 return "?" + hexString(ip)
331 // ipEmptyString is like ip.String except that it returns
332 // an empty string when ip is unset.
333 func ipEmptyString(ip IP) string {
334 if len(ip) == 0 {
337 return ip.String()
342 func (ip IP) MarshalText() ([]byte, error) {
343 if len(ip) == 0 {
346 if len(ip) != IPv4len && len(ip) != IPv6len {
347 return nil, &AddrError{Err: "invalid IP address", Addr: hexString(ip)}
349 return []byte(ip.String()), nil
353 // The IP address is expected in a form accepted by ParseIP.
354 func (ip *IP) UnmarshalText(text []byte) error {
356 *ip = nil
362 return &ParseError{Type: "IP address", Text: s}
364 *ip = x
368 // Equal reports whether ip and x are the same IP address.
371 func (ip IP) Equal(x IP) bool {
372 if len(ip) == len(x) {
373 return bytesEqual(ip, x)
375 if len(ip) == IPv4len && len(x) == IPv6len {
376 return bytesEqual(x[0:12], v4InV6Prefix) && bytesEqual(ip, x[12:])
378 if len(ip) == IPv6len && len(x) == IPv4len {
379 return bytesEqual(ip[0:12], v4InV6Prefix) && bytesEqual(ip[12:], x)
396 func (ip IP) matchAddrFamily(x IP) bool {
397 return ip.To4() != nil && x.To4() != nil || ip.To16() != nil && ip.To4() == nil && x.To16() != nil && x.To4() == nil
448 func networkNumberAndMask(n *IPNet) (ip IP, m IPMask) {
449 if ip = n.IP.To4(); ip == nil {
450 ip = n.IP
451 if len(ip) != IPv6len {
458 if len(ip) != IPv4len {
462 if len(ip) == IPv4len {
471 // Contains reports whether the network includes ip.
472 func (n *IPNet) Contains(ip IP) bool {
474 if x := ip.To4(); x != nil {
475 ip = x
477 l := len(ip)
482 if nn[i]&m[i] != ip[i]&m[i] {
489 // Network returns the address's network name, "ip+net".
490 func (n *IPNet) Network() string { return "ip+net" }
495 // string which consists of an IP address, followed by a slash
511 func parseIPv4(s string) IP {
541 func parseIPv6(s string, zoneAllowed bool) (ip IP, zone string) {
542 ip = make(IP, IPv6len)
543 ellipsis := -1 // position of ellipsis in ip
555 return ip, zone
582 ip[i] = ip4[12]
583 ip[i+1] = ip4[13]
584 ip[i+2] = ip4[14]
585 ip[i+3] = ip4[15]
592 ip[i] = byte(n >> 8)
593 ip[i+1] = byte(n)
633 ip[j+n] = ip[j]
636 ip[j] = 0
642 return ip, zone
645 // ParseIP parses s as an IP address, returning the result.
648 // If s is not a valid textual representation of an IP address,
650 func ParseIP(s string) IP {
656 ip, _ := parseIPv6(s, false)
657 return ip
663 // ParseCIDR parses s as a CIDR notation IP address and prefix length,
667 // It returns the IP address and the network implied by the IP and
669 // For example, ParseCIDR("192.0.2.1/24") returns the IP address
671 func ParseCIDR(s string) (IP, *IPNet, error) {
678 ip := parseIPv4(addr)
679 if ip == nil {
681 ip, _ = parseIPv6(addr, false)
684 if ip == nil || !ok || i != len(mask) || n < 0 || n > 8*iplen {
688 return ip, &IPNet{IP: ip.Mask(m), Mask: m}, nil