Golang in net package usage (ii)--IP

Source: Internet
Author: User
Tags rfc
This is a creation in Article, where the information may have evolved or changed.

The IP address (the InternetProtocoladdress, known as the Internet Protocol addresses), is a way of addressing the host on the Internet.

The Type IP//IP represents a simple IP address, which is a byte-type slice, capable of accepting 4-byte (IPV4) or 16-byte (IPV6) input. Note that the IP address is an IPV4 address or an IPV6 address that is semantically characteristic, not depending on the length of the slice: a 16-byte slice can also be a IPv4 address.

Type IP []byte
Func IPv4 (A, B, C, D byte) IP//ipv4 returns the address of an IPv4 address format (A.B.C.D), which is 16-byte

Func Parsecidr (S string) (IP, *ipnet, error)//parsecidr parses the string s into an IP address and subnet mask in the struct, where the string format must be a string of IP address and subnet mask, such as: " 192.168.100.1/24 "or" 2001:db8::/48 "and so on.

The Func Parseip (s string) IP//parseip resolves s to an IP address and returns that address. If s is not a valid IP address, PARSEIP returns nil. The string can be in a decimal-delimited IPv4 format (such as "74.125.19.99") or in IPV6 format (such as "2001:4860:0:2001::68").

Func (IP IP) defaultmask () IPMask//Returns the default subnet mask for IP, only IPv4 has a default subnet mask, and if the IP is not a valid IPv4 address, the default subnet mask returns nil
Func (IP IP) Equal (x IP) bool//Determine if two IP addresses are equal, where one IPv4 address and the same address with the same IPv6 format are considered equal and return true
Func (IP IP) isglobalunicast () bool//Determine if it is a global unicast address
Func (IP IP) isinterfacelocalmulticast () bool//judgment is not a local multicast address
Func (IP IP) islinklocalmulticast () bool//Determine if the link is a local multicast address
Func (IP IP) islinklocalunicast () bool//Determine if link local unicast address
Func (IP IP) isloopback () bool//Determine if it is a loopback address
Func (IP IP) ismulticast () bool//Determine if it is a multicast address
Func (IP IP) isunspecified () bool//Determine if the address is unspecified
Func (IP IP) marshaltext () ([]byte, error)//implements the encoding. The Textmarshaler interface, which is encoded in the same way as the string () function's return value.
Func (IP IP) mask (mask IPMask) IP//mask to get its IP address as subnet mask for IP
Func (IP IP) string () string///Gets the IP address of the IPV4, if the IP is an address, the format of the return value is point-delimited, such as "74.125.19.99"; otherwise it is represented as IPV6 format, such as "2001:4860:0:2001: : 68 ".
Func (IP IP) To16 () IP//will be a IP AddressConverted to a 16-byte representation. If IP is not an IP address (length error), TO16 returns nil. To16 can convert IP addresses, including IPV4 and IPV6, and To4 can only convert IPV4 addresses, which is the difference between To16 and To4.
Func (IP IP) To4 () IP//will be a IPV4The address translates to 4 bytes, and returns nil if the IP is not an IPv4 address
The func (IP *ip) unmarshaltext (text []byte) error//IP is deserialized, which implements the encoding. Textunmarshaler interface, the IP address string should be a format acceptable to the PARSEIP function.


Type ipaddr//Indicates the address of an IP terminal

Type ipaddr struct {IP   ipzone string//IPV6 addressing range}

Func resolveipaddr (NET, addr string) (*IPADDR, error)//IP address parsing formed as "host" or "Ipv6-host%zone" address form, resolving the domain name must be in the specified network, Specified network includes IP,IP4 or IP6
Func (a *ipaddr) network () string//return address net type "IP"
Func (a *ipaddr) string () string


Type Ipconn//ipconn types represent IP network connections, conn and Packetconn interfaces are implemented
Func Dialip (Netproto string, laddr, Raddr *ipaddr) (*ipconn, error)//dialip connect the local address Netproto and the remote address LADDR on the network protocol RADDR, Netproto must be "IP", "IP4", or "IP6" followed by a colon and protocol name or protocol number.
Func Listenip (Netproto string, laddr *ipaddr) (*ipconn, error)//listens to packets transmitted to the local IP address, and the Readfrom and WriteTo methods returned can be used to send and receive IP packets
Func (c *ipconn) close () error//close connection
Func (c *ipconn) File () (F *os. File, err Error)//file set the underlying os.file to block mode and return a copy copy, when the end, the caller needs to close the file, where the original file and the copy does not affect each other, the returned Os.file file descriptor is different from the file in the network connection, Using this copy to modify the properties of the ontology may or may not get the desired effect.
Func (c *ipconn) localaddr () ADDR//Return to local network address
Func (c *ipconn) read (b []byte) (int, error)//Implement Conn interface reading method, read data into B
Func (c *ipconn) readfrom (b []byte) (int, Addr, error)//conn method for Readfrom
Func (c *ipconn) readfromip (b []byte) (int, *ipaddr, error)//reads an IP packet from c, copies valid information to B, returns the number of bytes copied and the source address of the packet. Can be passed timeout () Causes the function to time out and return an error.
Func (c *ipconn) Readmsgip (b, Oob []byte) (n, oobn, flags int, addr *ipaddr, err Error)//read an IP packet from c, copy the valid data B, copy the relevant additional information into the OOB , returns the number of bytes copied into B and OOB, the flag of the packet, the source address of the packet, and the error that may have occurred.
Func (c *ipconn) remoteaddr () ADDR//Return to remote network address
Func (c *ipconn) Setdeadline (t time. Time) error//set the absolute expiration time for read and write operations, is a point
Func (c *ipconn) setreadbuffer (bytes int) error//Sets the size of the accept cache for the connection
Func (c *ipconn) Setreaddeadline (t time. Time) error//set the absolute expiration of the read operation
Func (c *ipconn) setwritebuffer (bytes int) error//Set the Send cache size for this connection
Func (c *ipconn) Setwritedeadline (t time. Time) error//set the absolute expiration of write operations
Func (c *ipconn) write (b []byte) (int, error)//writes data in B to C and returns the number of bytes written
Func (c *ipconn) Writemsgip (b, Oob []byte, addr *ipaddr) (n, oobn int, err Error)//writes valid information in B and OOB to address in C, returns the number of bytes written
Func (c *ipconn) WriteTo (b []byte, addr addr) (int, error)//writeto implement Packetconn interface WriteTo method
Func (c *ipconn) Writetoip (b []byte, addr *ipaddr) (int, error)//write an IP packet through C to add and copy valid information from B. You can expire the timeout value by setting it.


Type IPMask//ipmask represents an IP address

Type IPMask []byte

Func cidrmask (ones, bits int) IPMask//Returns a cidrmask where the total number of cidrmask bits is bits, the money ones bit is 1, and the remaining bits are 0.
Func Ipv4mask (A, B, C, D byte) ipmask//returns the IP mask, where the IP mask form is the IPv4 mask (4 byte mode) a.b.c.d
Func (M IPMask) Size (ones, bits int)//Returns the number of front 1 of the mask and the total number, if M is not the canonical subnet mask (preceded by 1 followed by 0), returns 0,0
Func (M IPMask) String () string//return mask m 16 mechanism representation, no punctuation


Type ipnet//represents an IP network

Type ipnet struct {IP   IP//     network Address Mask ipmask//Subnet Mask}


Func (n *ipnet) Contains (IP IP) bool//determines if n contains IP
Func (n *ipnet) network () string//returns the net name of the address, in the form of ip+net

The func (n *ipnet) String () string//returns the CIDR pattern of ipnet N, as defined in RFC 4632 and RFC 4291, "192.168.100.1/24" or "2001:DB8::/48", If the mask is not in canonical mode, it will return a string of the following form: IP address/A string consisting of 16 characters without punctuation, such as "192.168.100.1/c000ff00".


Reference:

http://docscn.studygolang.com/pkg/net/#pkg-constants



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.