Commit 9d071032 authored by Mikio Hara's avatar Mikio Hara

go.net/ipv4: make use of net.PacketConn embedding

R=dave
CC=golang-dev
https://golang.org/cl/10319043
parent c94a7ef8
......@@ -18,15 +18,15 @@ type Conn struct {
}
type genericOpt struct {
c net.Conn
net.Conn
}
func (c *genericOpt) ok() bool { return c != nil && c.c != nil }
func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
// NewConn returns a new Conn.
func NewConn(c net.Conn) *Conn {
return &Conn{
genericOpt: genericOpt{c},
genericOpt: genericOpt{Conn: c},
}
}
......@@ -42,10 +42,10 @@ type PacketConn struct {
}
type dgramOpt struct {
c net.PacketConn
net.PacketConn
}
func (c *dgramOpt) ok() bool { return c != nil && c.c != nil }
func (c *dgramOpt) ok() bool { return c != nil && c.PacketConn != nil }
// SetControlMessage sets the per packet IP-level socket options.
func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
......@@ -65,7 +65,7 @@ func (c *PacketConn) SetDeadline(t time.Time) error {
if !c.payloadHandler.ok() {
return syscall.EINVAL
}
return c.payloadHandler.c.SetDeadline(t)
return c.payloadHandler.PacketConn.SetDeadline(t)
}
// SetReadDeadline sets the read deadline associated with the
......@@ -74,7 +74,7 @@ func (c *PacketConn) SetReadDeadline(t time.Time) error {
if !c.payloadHandler.ok() {
return syscall.EINVAL
}
return c.payloadHandler.c.SetReadDeadline(t)
return c.payloadHandler.PacketConn.SetReadDeadline(t)
}
// SetWriteDeadline sets the write deadline associated with the
......@@ -83,7 +83,7 @@ func (c *PacketConn) SetWriteDeadline(t time.Time) error {
if !c.payloadHandler.ok() {
return syscall.EINVAL
}
return c.payloadHandler.c.SetWriteDeadline(t)
return c.payloadHandler.PacketConn.SetWriteDeadline(t)
}
// Close closes the endpoint.
......@@ -91,16 +91,16 @@ func (c *PacketConn) Close() error {
if !c.payloadHandler.ok() {
return syscall.EINVAL
}
return c.payloadHandler.c.Close()
return c.payloadHandler.PacketConn.Close()
}
// NewPacketConn returns a new PacketConn using c as its underlying
// transport.
func NewPacketConn(c net.PacketConn) *PacketConn {
return &PacketConn{
genericOpt: genericOpt{c.(net.Conn)},
dgramOpt: dgramOpt{c},
payloadHandler: payloadHandler{c: c},
genericOpt: genericOpt{Conn: c.(net.Conn)},
dgramOpt: dgramOpt{PacketConn: c},
payloadHandler: payloadHandler{PacketConn: c},
}
}
......@@ -166,8 +166,8 @@ func (c *RawConn) Close() error {
// transport.
func NewRawConn(c net.PacketConn) (*RawConn, error) {
r := &RawConn{
genericOpt: genericOpt{c.(net.Conn)},
dgramOpt: dgramOpt{c},
genericOpt: genericOpt{Conn: c.(net.Conn)},
dgramOpt: dgramOpt{PacketConn: c},
packetHandler: packetHandler{c: c.(*net.IPConn)},
}
fd, err := r.packetHandler.sysfd()
......
......@@ -4,9 +4,7 @@
package ipv4
import (
"syscall"
)
import "syscall"
func (c *genericOpt) sysfd() (int, error) {
// TODO(mikio): Implement this
......
......@@ -12,7 +12,7 @@ import (
)
func (c *genericOpt) sysfd() (int, error) {
switch p := c.c.(type) {
switch p := c.Conn.(type) {
case *net.TCPConn, *net.UDPConn, *net.IPConn:
return sysfd(p)
}
......@@ -20,7 +20,7 @@ func (c *genericOpt) sysfd() (int, error) {
}
func (c *dgramOpt) sysfd() (int, error) {
switch p := c.c.(type) {
switch p := c.PacketConn.(type) {
case *net.UDPConn, *net.IPConn:
return sysfd(p.(net.Conn))
}
......@@ -28,7 +28,7 @@ func (c *dgramOpt) sysfd() (int, error) {
}
func (c *payloadHandler) sysfd() (int, error) {
return sysfd(c.c.(net.Conn))
return sysfd(c.PacketConn.(net.Conn))
}
func (c *packetHandler) sysfd() (int, error) {
......
......@@ -11,7 +11,7 @@ import (
)
func (c *genericOpt) sysfd() (syscall.Handle, error) {
switch p := c.c.(type) {
switch p := c.Conn.(type) {
case *net.TCPConn, *net.UDPConn, *net.IPConn:
return sysfd(p)
}
......@@ -19,7 +19,7 @@ func (c *genericOpt) sysfd() (syscall.Handle, error) {
}
func (c *dgramOpt) sysfd() (syscall.Handle, error) {
switch p := c.c.(type) {
switch p := c.PacketConn.(type) {
case *net.UDPConn, *net.IPConn:
return sysfd(p.(net.Conn))
}
......@@ -27,7 +27,7 @@ func (c *dgramOpt) sysfd() (syscall.Handle, error) {
}
func (c *payloadHandler) sysfd() (syscall.Handle, error) {
return sysfd(c.c.(net.Conn))
return sysfd(c.PacketConn.(net.Conn))
}
func (c *packetHandler) sysfd() (syscall.Handle, error) {
......
......@@ -11,11 +11,11 @@ import (
// A payloadHandler represents the IPv4 datagram payload handler.
type payloadHandler struct {
c net.PacketConn
net.PacketConn
rawOpt
}
func (c *payloadHandler) ok() bool { return c != nil && c.c != nil }
func (c *payloadHandler) ok() bool { return c != nil && c.PacketConn != nil }
// ReadFrom reads a payload of the received IPv4 datagram, from the
// endpoint c, copying the payload into b. It returns the number of
......@@ -27,14 +27,14 @@ func (c *payloadHandler) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.
}
oob := newControlMessage(&c.rawOpt)
var oobn int
switch rd := c.c.(type) {
switch c := c.PacketConn.(type) {
case *net.UDPConn:
if n, oobn, _, src, err = rd.ReadMsgUDP(b, oob); err != nil {
if n, oobn, _, src, err = c.ReadMsgUDP(b, oob); err != nil {
return 0, nil, nil, err
}
case *net.IPConn:
nb := make([]byte, maxHeaderLen+len(b))
if n, oobn, _, src, err = rd.ReadMsgIP(nb, oob); err != nil {
if n, oobn, _, src, err = c.ReadMsgIP(nb, oob); err != nil {
return 0, nil, nil, err
}
hdrlen := int(nb[0]&0x0f) << 2
......@@ -66,11 +66,11 @@ func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n
if dst == nil {
return 0, errMissingAddress
}
switch wr := c.c.(type) {
switch c := c.PacketConn.(type) {
case *net.UDPConn:
n, _, err = wr.WriteMsgUDP(b, oob, dst.(*net.UDPAddr))
n, _, err = c.WriteMsgUDP(b, oob, dst.(*net.UDPAddr))
case *net.IPConn:
n, _, err = wr.WriteMsgIP(b, oob, dst.(*net.IPAddr))
n, _, err = c.WriteMsgIP(b, oob, dst.(*net.IPAddr))
default:
return 0, errInvalidConnType
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment