Hi,
Here is a revised API proposal and mock code.
<
http://codereview.appspot.com/6474044/>
I added a few network I/O methods, had tests on linux, freebsd, darwin,
addressed issues should be fixed and added test codes especially IP
multicast stuff.
Comments welcome.
-- Mikio
package ipv4
import "net/ipv4"
Package ipv4 implements IP-level socket options for the Internet
Protocol version 4.
CONSTANTS
const (
ProtocolVersion = 4 // protocol version
HeaderLen = 20 // header length without extension headers
)
TYPES
type Conn struct {
// contains filtered or unexported fields
}
A Conn represents a network endpoint that uses IPv4 transport. It allows
to set basic IP-level socket options such as TOS and TTL.
func NewConn(c net.Conn) *Conn
NewConn returns a new Conn.
func (c *Conn) SetTOS(tos int) error
SetTOS sets the type-of-service field value for future outgoing packets.
func (c *Conn) SetTTL(ttl int) error
SetTTL sets the time-to-live field value for future outgoing packets.
func (c *Conn) TOS() (int, error)
TOS returns the type-of-service field value for outgoing packets.
func (c *Conn) TTL() (int, error)
TTL returns the time-to-live field value for outgoing packets.
type ControlFlags uint
const (
FlagTTL ControlFlags = 1 << iota // pass the TTL on the
received packet
FlagSrc // pass the source address
on the received packet
FlagDst // pass the destination
address on the received packet
FlagInterface // pass the interface index
on the received packet or outgoing packet
)
type ControlMessage struct {
TTL int // time-to-live
Src net.IP // source address
Dst net.IP // destination address
IfIndex int // interface index
}
A ControlMessage represents control information that contains per packet
basis IP-level option data.
func (cm *ControlMessage) String() string
type Header struct {
Version int // protocol version
Len int // header length
TOS int // type-of-service
TotalLen int // packet total length
ID int // identification
FragOff int // fragment offset
TTL int // time-to-live
Protocol int // next protocol
Checksum int // checksum
Src net.IP // source address
Dst net.IP // destination address
Options []byte // options, extension headers
}
A Header represents an IPv4 header.
func ParseHeader(b []byte) (*Header, error)
ParseHeader parses b as an IPv4 header.
func (h *Header) Marshal() ([]byte, error)
Marshal returns the binary encoding of the IPv4 header h.
func (h *Header) String() string
type HeaderField int
const (
TOS HeaderField = 1 // type-of-service
TotalLen = 2 // packet tolal length
ID = 4 // identification
FragOff = 6 // fragment offset
TTL = 8 // time-to-live
Protocol = 9 // next protocol
Checksum = 10 // checksum
Src = 12 // source address
Dst = 16 // destination address
)
func (f HeaderField) String() string
type PacketConn struct {
// contains filtered or unexported fields
}
A PacketConn represents a packet network endpoint that uses IPv4
transport. It allows to set several IP-level socket options that include
the control of multicasting. It also provides datagram based network I/O
methods specific to the IPv4 and higher layer protocols such as UDP.
func NewPacketConn(c net.PacketConn) *PacketConn
NewPacketConn returns a new PacketConn using c for network I/O.
func (c *PacketConn) Close() error
Close closes the endpoint.
func (c *PacketConn) JoinGroup(ifi *net.Interface, group, source net.Addr) error
JoinGroup joins the group address group with the source address source
on the interface ifi. It uses the system assigned multicast interface if
ifi and source is nil.
func (c *PacketConn) LeaveGroup(ifi *net.Interface, group, source
net.Addr) error
LeaveGroup leaves the group address group with the source address source
on the interface ifi.
func (c *PacketConn) MulticastInterface() (*net.Interface, error)
MulticastInterface returns the interface for multicast packet
transmission.
func (c *PacketConn) MulticastLoopback() (bool, error)
MulticastLoopback reports whether transmitted multicast packets should
be copied, sent back to the originator.
func (c *PacketConn) MulticastTTL() (int, error)
MulticastTTL returns the time-to-live field value for outgoing multicast
packets.
func (c *PacketConn) Read(b []byte) (n int, cm *ControlMessage, src
net.Addr, err error)
Read 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 on the received
datagram.
func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error
SetControlMessage sets the per packet basis IP-level socket options.
func (c *PacketConn) SetMulticastInterface(ifi *net.Interface) error
SetMulticastInterface sets the interface for future multicast packet
transmission.
func (c *PacketConn) SetMulticastLoopback(on bool) error
SetMulticastLoopback sets whether transmitted multicast packets should
be copied, sent back to the originator.
func (c *PacketConn) SetMulticastTTL(ttl int) error
SetMulticastTTL sets the time-to-live field value for future outgoing
multicast packets.
func (c *PacketConn) SetTOS(tos int) error
SetTOS sets the type-of-service field value for future outgoing packets.
func (c *PacketConn) SetTTL(ttl int) error
SetTTL sets the time-to-live field value for future outgoing packets.
func (c *PacketConn) TOS() (int, error)
TOS returns the type-of-service field value for outgoing packets.
func (c *PacketConn) TTL() (int, error)
TTL returns the time-to-live field value for outgoing packets.
func (c *PacketConn) Write(b []byte, cm *ControlMessage, dst net.Addr)
(n int, err error)
Write 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 to specify the
datagram path, outgoing interface, but currently only Linux supports
that control. The cm could be nil unless the control of the outgoing
datagram is required.
type RawConn struct {
// contains filtered or unexported fields
}
A RawConn represents a packet network endpoint that uses IPv4 transport.
It allows to set several IP-level socket options that include IPv4
header manipulation. It also provides datagram based network I/O methods
specific to the protocols that handle IPv4 datagram directly such as
OSPF, GRE.
func NewRawConn(c net.PacketConn) (*RawConn, error)
NewRawConn returns a new RawConn using c for network I/O.
func (c *RawConn) Close() error
Close closes the endpoint.
func (c *RawConn) JoinGroup(ifi *net.Interface, group, source net.Addr) error
JoinGroup joins the group address group with the source address source
on the interface ifi. It uses the system assigned multicast interface if
ifi and source is nil.
func (c *RawConn) LeaveGroup(ifi *net.Interface, group, source net.Addr) error
LeaveGroup leaves the group address group with the source address source
on the interface ifi.
func (c *RawConn) MulticastInterface() (*net.Interface, error)
MulticastInterface returns the interface for multicast packet
transmission.
func (c *RawConn) MulticastLoopback() (bool, error)
MulticastLoopback reports whether transmitted multicast packets should
be copied, sent back to the originator.
func (c *RawConn) MulticastTTL() (int, error)
MulticastTTL returns the time-to-live field value for outgoing multicast
packets.
func (c *RawConn) Read(b []byte) (h *Header, p []byte, cm
*ControlMessage, err error)
Read reads an IPv4 datagram from the endpoint c, copying the datagram
into b. It returns the receivd datagram as the IPv4 header h, the
payload p and the control message cm.
func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error
SetControlMessage sets the per packet basis IP-level socket options.
func (c *RawConn) SetMulticastInterface(ifi *net.Interface) error
SetMulticastInterface sets the interface for future multicast packet
transmission.
func (c *RawConn) SetMulticastLoopback(on bool) error
SetMulticastLoopback sets whether transmitted multicast packets should
be copied, sent back to the originator.
func (c *RawConn) SetMulticastTTL(ttl int) error
SetMulticastTTL sets the time-to-live field value for future outgoing
multicast packets.
func (c *RawConn) SetTOS(tos int) error
SetTOS sets the type-of-service field value for future outgoing packets.
func (c *RawConn) SetTTL(ttl int) error
SetTTL sets the time-to-live field value for future outgoing packets.
func (c *RawConn) TOS() (int, error)
TOS returns the type-of-service field value for outgoing packets.
func (c *RawConn) TTL() (int, error)
TTL returns the time-to-live field value for outgoing packets.
func (c *RawConn) Write(h *Header, p []byte, cm *ControlMessage) (err error)
Write 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 to specify the datagram path, outgoing interface, but
currently only Linux supports that control. The cm could be nil unless
the control of the outgoing datagram is required.
The IPv4 header h must contain appropriate fields that include:
Version = don't care
Len = <must be specified>
TOS = <must be specified>
TotalLen = <must be specified>
ID = platform sets an appropriate value if ID is zero
FragOff = <must be specified>
TTL = <must be specified>
Protocol = <must be specified>
Checksum = platform sets an appropriate value if Checksum is zero
Src = platform sets an appropriate value if Src is nil
Dst = <must be specified>
h.Options = optional