{"modulePath":"code.cloudfoundry.org/cli-sapi","version":"v6.1.1+incompatible","isLatest":false,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package ipv4\n\nPackage ipv4 implements IP-level socket options for the Internet Protocol version 4.\n\nThe package provides IP-level socket options that allow manipulation of IPv4 facilities.  The IPv4 and basic host requirements for IPv4 are defined in RFC 791, RFC 1112 and RFC 1122.\n\n### Unicasting {#hdr-Unicasting}\n\nThe options for unicasting are available for net.TCPConn, net.UDPConn and net.IPConn which are created as network connections that use the IPv4 transport.  When a single TCP connection carrying a data flow of multiple packets needs to indicate the flow is important, ipv4.Conn is used to set the type-of-service field on the IPv4 header for each packet.\n\n\tln, err := net.Listen(\"tcp4\", \"0.0.0.0:1024\")\n\tif err != nil {\n\t\t// error handling\n\t}\n\tdefer ln.Close()\n\tfor {\n\t\tc, err := ln.Accept()\n\t\tif err != nil {\n\t\t\t// error handling\n\t\t}\n\t\tgo func(c net.Conn) {\n\t\t\tdefer c.Close()\n\nThe outgoing packets will be labeled DiffServ assured forwarding class 1 low drop precedence, as known as AF11 packets.\n\n\t\t\tif err := ipv4.NewConn(c).SetTOS(DiffServAF11); err != nil {\n\t\t\t\t// error handling\n\t\t\t}\n\t\t\tif _, err := c.Write(data); err != nil {\n\t\t\t\t// error handling\n\t\t\t}\n\t\t}(c)\n\t}\n\n### Multicasting {#hdr-Multicasting}\n\nThe options for multicasting are available for net.UDPConn and net.IPconn which are created as network connections that use the IPv4 transport.  A few network facilities must be prepared before you begin multicasting, at a minimum joining network interfaces and multicast groups.\n\n\ten0, err := net.InterfaceByName(\"en0\")\n\tif err != nil {\n\t\t// error handling\n\t}\n\ten1, err := net.InterfaceByIndex(911)\n\tif err != nil {\n\t\t// error handling\n\t}\n\tgroup := net.IPv4(224, 0, 0, 250)\n\nFirst, an application listens to an appropriate address with an appropriate service port.\n\n\tc, err := net.ListenPacket(\"udp4\", \"0.0.0.0:1024\")\n\tif err != nil {\n\t\t// error handling\n\t}\n\tdefer c.Close()\n\nSecond, the application joins multicast groups, starts listening to the groups on the specified network interfaces.  Note that the service port for transport layer protocol does not matter with this operation as joining groups affects only network and link layer protocols, such as IPv4 and Ethernet.\n\n\tp := ipv4.NewPacketConn(c)\n\tif err := p.JoinGroup(en0, \u0026net.UDPAddr{IP: group}); err != nil {\n\t\t// error handling\n\t}\n\tif err := p.JoinGroup(en1, \u0026net.UDPAddr{IP: group}); err != nil {\n\t\t// error handling\n\t}\n\nThe application might set per packet control message transmissions between the protocol stack within the kernel.  When the application needs a destination address on an incoming packet, SetControlMessage of ipv4.PacketConn is used to enable control message transmissons.\n\n\tif err := p.SetControlMessage(ipv4.FlagDst, true); err != nil {\n\t\t// error handling\n\t}\n\nThe application could identify whether the received packets are of interest by using the control message that contains the destination address of the received packet.\n\n\tb := make([]byte, 1500)\n\tfor {\n\t\tn, cm, src, err := p.ReadFrom(b)\n\t\tif err != nil {\n\t\t\t// error handling\n\t\t}\n\t\tif cm.Dst.IsMulticast() {\n\t\t\tif cm.Dst.Equal(group)\n\t\t\t\t// joined group, do something\n\t\t\t} else {\n\t\t\t\t// unknown group, discard\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\nThe application can also send both unicast and multicast packets.\n\n\t\tp.SetTOS(DiffServCS0)\n\t\tp.SetTTL(16)\n\t\tif _, err := p.WriteTo(data, nil, src); err != nil {\n\t\t\t// error handling\n\t\t}\n\t\tdst := \u0026net.UDPAddr{IP: group, Port: 1024}\n\t\tfor _, ifi := range []*net.Interface{en0, en1} {\n\t\t\tif err := p.SetMulticastInterface(ifi); err != nil {\n\t\t\t\t// error handling\n\t\t\t}\n\t\t\tp.SetMulticastTTL(2)\n\t\t\tif _, err := p.WriteTo(data, nil, dst); err != nil {\n\t\t\t\t// error handling\n\t\t\t}\n\t\t}\n\t}\n\n### More multicasting {#hdr-More_multicasting}\n\nAn application that uses PacketConn or RawConn may join multiple multicast groups.  For example, a UDP listener with port 1024 might join two different groups across over two different network interfaces by using:\n\n\tc, err := net.ListenPacket(\"udp4\", \"0.0.0.0:1024\")\n\tif err != nil {\n\t\t// error handling\n\t}\n\tdefer c.Close()\n\tp := ipv4.NewPacketConn(c)\n\tif err := p.JoinGroup(en0, \u0026net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {\n\t\t// error handling\n\t}\n\tif err := p.JoinGroup(en0, \u0026net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}); err != nil {\n\t\t// error handling\n\t}\n\tif err := p.JoinGroup(en1, \u0026net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}); err != nil {\n\t\t// error handling\n\t}\n\nIt is possible for multiple UDP listeners that listen on the same UDP port to join the same multicast group.  The net package will provide a socket that listens to a wildcard address with reusable UDP port when an appropriate multicast address prefix is passed to the net.ListenPacket or net.ListenUDP.\n\n\tc1, err := net.ListenPacket(\"udp4\", \"224.0.0.0:1024\")\n\tif err != nil {\n\t\t// error handling\n\t}\n\tdefer c1.Close()\n\tc2, err := net.ListenPacket(\"udp4\", \"224.0.0.0:1024\")\n\tif err != nil {\n\t\t// error handling\n\t}\n\tdefer c2.Close()\n\tp1 := ipv4.NewPacketConn(c1)\n\tif err := p1.JoinGroup(en0, \u0026net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {\n\t\t// error handling\n\t}\n\tp2 := ipv4.NewPacketConn(c2)\n\tif err := p2.JoinGroup(en0, \u0026net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {\n\t\t// error handling\n\t}\n\nAlso it is possible for the application to leave or rejoin a multicast group on the network interface.\n\n\tif err := p.LeaveGroup(en0, \u0026net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {\n\t\t// error handling\n\t}\n\tif err := p.JoinGroup(en0, \u0026net.UDPAddr{IP: net.IPv4(224, 0, 0, 250)}); err != nil {\n\t\t// error handling\n\t}\n\n## Constants\n\n```go\nconst (\n\tVersion   = 4  // protocol version\n\tHeaderLen = 20 // header length without extension headers\n\n)\n```\n\n## Types\n\n```go\ntype Conn struct {\n\t// contains filtered or unexported fields\n}\n```\nA Conn represents a network endpoint that uses the IPv4 transport. It is used to control basic IP-level socket options such as TOS and TTL.\n\n```go\nfunc NewConn(c net.Conn) *Conn\n```\nNewConn returns a new Conn.\n\n```go\nfunc (c *Conn) SetTOS(tos int) error\n```\nSetTOS sets the type-of-service field value for future outgoing packets.\n\n```go\nfunc (c *Conn) SetTTL(ttl int) error\n```\nSetTTL sets the time-to-live field value for future outgoing packets.\n\n```go\nfunc (c *Conn) TOS() (int, error)\n```\nTOS returns the type-of-service field value for outgoing packets.\n\n```go\nfunc (c *Conn) TTL() (int, error)\n```\nTTL returns the time-to-live field value for outgoing packets.\n\n```go\ntype ControlFlags uint\n```\n\n```go\nconst (\n\tFlagTTL       ControlFlags = 1 \u003c\u003c iota // pass the TTL on the received packet\n\tFlagSrc                                // pass the source address on the received packet\n\tFlagDst                                // pass the destination address on the received packet\n\tFlagInterface                          // pass the interface index on the received packet\n)\n```\n\n```go\ntype ControlMessage struct {\n\t// Receiving socket options: SetControlMessage allows to\n\t// receive the options from the protocol stack using ReadFrom\n\t// method of PacketConn or RawConn.\n\t//\n\t// Specifying socket options: ControlMessage for WriteTo\n\t// method of PacketConn or RawConn allows to send the options\n\t// to the protocol stack.\n\t//\n\tTTL     int    // time-to-live, receiving only\n\tSrc     net.IP // source address, specifying only\n\tDst     net.IP // destination address, receiving only\n\tIfIndex int    // interface index, must be 1 \u003c= value when specifying\n}\n```\nA ControlMessage represents per packet basis IP-level socket options.\n\n```go\nfunc (cm *ControlMessage) String() string\n```\n\n```go\ntype Header struct {\n\tVersion  int         // protocol version\n\tLen      int         // header length\n\tTOS      int         // type-of-service\n\tTotalLen int         // packet total length\n\tID       int         // identification\n\tFlags    HeaderFlags // flags\n\tFragOff  int         // fragment offset\n\tTTL      int         // time-to-live\n\tProtocol int         // next protocol\n\tChecksum int         // checksum\n\tSrc      net.IP      // source address\n\tDst      net.IP      // destination address\n\tOptions  []byte      // options, extension headers\n}\n```\nA Header represents an IPv4 header.\n\n```go\nfunc ParseHeader(b []byte) (*Header, error)\n```\nParseHeader parses b as an IPv4 header.\n\n```go\nfunc (h *Header) Marshal() ([]byte, error)\n```\nMarshal returns the binary encoding of the IPv4 header h.\n\n```go\nfunc (h *Header) String() string\n```\n\n```go\ntype HeaderFlags int\n```\n\n```go\nconst (\n\tMoreFragments HeaderFlags = 1 \u003c\u003c iota // more fragments flag\n\tDontFragment                          // don't fragment flag\n)\n```\n\n```go\ntype ICMPType int\n```\nAn ICMPType represents a type of ICMP message.\n\n```go\nconst (\n\tICMPTypeEchoReply              ICMPType = 0  // Echo Reply\n\tICMPTypeDestinationUnreachable ICMPType = 3  // Destination Unreachable\n\tICMPTypeRedirect               ICMPType = 5  // Redirect\n\tICMPTypeEcho                   ICMPType = 8  // Echo\n\tICMPTypeRouterAdvertisement    ICMPType = 9  // Router Advertisement\n\tICMPTypeRouterSolicitation     ICMPType = 10 // Router Solicitation\n\tICMPTypeTimeExceeded           ICMPType = 11 // Time Exceeded\n\tICMPTypeParameterProblem       ICMPType = 12 // Parameter Problem\n\tICMPTypeTimestamp              ICMPType = 13 // Timestamp\n\tICMPTypeTimestampReply         ICMPType = 14 // Timestamp Reply\n\tICMPTypePhoturis               ICMPType = 40 // Photuris\n)\n```\nInternet Control Message Protocol (ICMP) Parameters, Updated: 2013-04-19\n\n```go\nfunc (typ ICMPType) String() string\n```\n\n```go\ntype PacketConn struct {\n\t// contains filtered or unexported fields\n}\n```\nA PacketConn represents a packet network endpoint that uses the IPv4 transport.  It is used to control several IP-level socket options including multicasting.  It also provides datagram based network I/O methods specific to the IPv4 and higher layer protocols such as UDP.\n\n```go\nfunc NewPacketConn(c net.PacketConn) *PacketConn\n```\nNewPacketConn returns a new PacketConn using c as its underlying transport.\n\n```go\nfunc (c *PacketConn) Close() error\n```\nClose closes the endpoint.\n\n```go\nfunc (c *PacketConn) JoinGroup(ifi *net.Interface, group net.Addr) error\n```\nJoinGroup joins the group address group on the interface ifi. It uses the system assigned multicast interface when ifi is nil, although this is not recommended because the assignment depends on platforms and sometimes it might require routing configuration.\n\n```go\nfunc (c *PacketConn) LeaveGroup(ifi *net.Interface, group net.Addr) error\n```\nLeaveGroup leaves the group address group on the interface ifi.\n\n```go\nfunc (c *PacketConn) MulticastInterface() (*net.Interface, error)\n```\nMulticastInterface returns the default interface for multicast packet transmissions.\n\n```go\nfunc (c *PacketConn) MulticastLoopback() (bool, error)\n```\nMulticastLoopback reports whether transmitted multicast packets should be copied and send back to the originator.\n\n```go\nfunc (c *PacketConn) MulticastTTL() (int, error)\n```\nMulticastTTL returns the time-to-live field value for outgoing multicast packets.\n\n```go\nfunc (c *PacketConn) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error)\n```\nReadFrom reads a payload of the received IPv4 datagram, from the endpoint c, copying the payload into b.  It returns the number of bytes copied into b, the control message cm and the source address src of the received datagram.\n\n```go\nfunc (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error\n```\nSetControlMessage sets the per packet IP-level socket options.\n\n```go\nfunc (c *PacketConn) SetDeadline(t time.Time) error\n```\nSetDeadline sets the read and write deadlines associated with the endpoint.\n\n```go\nfunc (c *PacketConn) SetMulticastInterface(ifi *net.Interface) error\n```\nSetMulticastInterface sets the default interface for future multicast packet transmissions.\n\n```go\nfunc (c *PacketConn) SetMulticastLoopback(on bool) error\n```\nSetMulticastLoopback sets whether transmitted multicast packets should be copied and send back to the originator.\n\n```go\nfunc (c *PacketConn) SetMulticastTTL(ttl int) error\n```\nSetMulticastTTL sets the time-to-live field value for future outgoing multicast packets.\n\n```go\nfunc (c *PacketConn) SetReadDeadline(t time.Time) error\n```\nSetReadDeadline sets the read deadline associated with the endpoint.\n\n```go\nfunc (c *PacketConn) SetTOS(tos int) error\n```\nSetTOS sets the type-of-service field value for future outgoing packets.\n\n```go\nfunc (c *PacketConn) SetTTL(ttl int) error\n```\nSetTTL sets the time-to-live field value for future outgoing packets.\n\n```go\nfunc (c *PacketConn) SetWriteDeadline(t time.Time) error\n```\nSetWriteDeadline sets the write deadline associated with the endpoint.\n\n```go\nfunc (c *PacketConn) TOS() (int, error)\n```\nTOS returns the type-of-service field value for outgoing packets.\n\n```go\nfunc (c *PacketConn) TTL() (int, error)\n```\nTTL returns the time-to-live field value for outgoing packets.\n\n```go\nfunc (c *PacketConn) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error)\n```\nWriteTo writes a payload of the IPv4 datagram, to the destination address dst through the endpoint c, copying the payload from b.  It returns the number of bytes written.  The control message cm allows the datagram path and the outgoing interface to be specified. Currently only Linux supports this.  The cm may be nil if control of the outgoing datagram is not required.\n\n```go\ntype RawConn struct {\n\t// contains filtered or unexported fields\n}\n```\nA RawConn represents a packet network endpoint that uses the IPv4 transport.  It is used to control several IP-level socket options including IPv4 header manipulation.  It also provides datagram based network I/O methods specific to the IPv4 and higher layer protocols that handle IPv4 datagram directly such as OSPF, GRE.\n\n```go\nfunc NewRawConn(c net.PacketConn) (*RawConn, error)\n```\nNewRawConn returns a new RawConn using c as its underlying transport.\n\n```go\nfunc (c *RawConn) Close() error\n```\nClose closes the endpoint.\n\n```go\nfunc (c *RawConn) JoinGroup(ifi *net.Interface, group net.Addr) error\n```\nJoinGroup joins the group address group on the interface ifi. It uses the system assigned multicast interface when ifi is nil, although this is not recommended because the assignment depends on platforms and sometimes it might require routing configuration.\n\n```go\nfunc (c *RawConn) LeaveGroup(ifi *net.Interface, group net.Addr) error\n```\nLeaveGroup leaves the group address group on the interface ifi.\n\n```go\nfunc (c *RawConn) MulticastInterface() (*net.Interface, error)\n```\nMulticastInterface returns the default interface for multicast packet transmissions.\n\n```go\nfunc (c *RawConn) MulticastLoopback() (bool, error)\n```\nMulticastLoopback reports whether transmitted multicast packets should be copied and send back to the originator.\n\n```go\nfunc (c *RawConn) MulticastTTL() (int, error)\n```\nMulticastTTL returns the time-to-live field value for outgoing multicast packets.\n\n```go\nfunc (c *RawConn) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error)\n```\nReadFrom reads an IPv4 datagram from the endpoint c, copying the datagram into b.  It returns the received datagram as the IPv4 header h, the payload p and the control message cm.\n\n```go\nfunc (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error\n```\nSetControlMessage sets the per packet IP-level socket options.\n\n```go\nfunc (c *RawConn) SetDeadline(t time.Time) error\n```\nSetDeadline sets the read and write deadlines associated with the endpoint.\n\n```go\nfunc (c *RawConn) SetMulticastInterface(ifi *net.Interface) error\n```\nSetMulticastInterface sets the default interface for future multicast packet transmissions.\n\n```go\nfunc (c *RawConn) SetMulticastLoopback(on bool) error\n```\nSetMulticastLoopback sets whether transmitted multicast packets should be copied and send back to the originator.\n\n```go\nfunc (c *RawConn) SetMulticastTTL(ttl int) error\n```\nSetMulticastTTL sets the time-to-live field value for future outgoing multicast packets.\n\n```go\nfunc (c *RawConn) SetReadDeadline(t time.Time) error\n```\nSetReadDeadline sets the read deadline associated with the endpoint.\n\n```go\nfunc (c *RawConn) SetTOS(tos int) error\n```\nSetTOS sets the type-of-service field value for future outgoing packets.\n\n```go\nfunc (c *RawConn) SetTTL(ttl int) error\n```\nSetTTL sets the time-to-live field value for future outgoing packets.\n\n```go\nfunc (c *RawConn) SetWriteDeadline(t time.Time) error\n```\nSetWriteDeadline sets the write deadline associated with the endpoint.\n\n```go\nfunc (c *RawConn) TOS() (int, error)\n```\nTOS returns the type-of-service field value for outgoing packets.\n\n```go\nfunc (c *RawConn) TTL() (int, error)\n```\nTTL returns the time-to-live field value for outgoing packets.\n\n```go\nfunc (c *RawConn) WriteTo(h *Header, p []byte, cm *ControlMessage) error\n```\nWriteTo writes an IPv4 datagram through the endpoint c, copying the datagram from the IPv4 header h and the payload p.  The control message cm allows the datagram path and the outgoing interface to be specified.  Currently only Linux supports this.  The cm may be nil if control of the outgoing datagram is not required.\n\nThe IPv4 header h must contain appropriate fields that include:\n\n\tVersion       = ipv4.Version\n\tLen           = \u003cmust be specified\u003e\n\tTOS           = \u003cmust be specified\u003e\n\tTotalLen      = \u003cmust be specified\u003e\n\tID            = platform sets an appropriate value if ID is zero\n\tFragOff       = \u003cmust be specified\u003e\n\tTTL           = \u003cmust be specified\u003e\n\tProtocol      = \u003cmust be specified\u003e\n\tChecksum      = platform sets an appropriate value if Checksum is zero\n\tSrc           = platform sets an appropriate value if Src is nil\n\tDst           = \u003cmust be specified\u003e\n\tOptions       = optional\n\n","path":"code.cloudfoundry.org/cli-sapi/src/code.google.com/p/go.net/ipv4","name":"ipv4","synopsis":"Package ipv4 implements IP-level socket options for the Internet Protocol version 4.","isRedistributable":true}
