rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/net/iprawsock_posix.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows 6 7 package net 8 9 import ( 10 "syscall" 11 "time" 12 ) 13 14 // BUG(mikio): On every POSIX platform, reads from the "ip4" network 15 // using the ReadFrom or ReadFromIP method might not return a complete 16 // IPv4 packet, including its header, even if there is space 17 // available. This can occur even in cases where Read or ReadMsgIP 18 // could return a complete packet. For this reason, it is recommended 19 // that you do not uses these methods if it is important to receive a 20 // full packet. 21 // 22 // The Go 1 compatibility guidelines make it impossible for us to 23 // change the behavior of these methods; use Read or ReadMsgIP 24 // instead. 25 26 func sockaddrToIP(sa syscall.Sockaddr) Addr { 27 switch sa := sa.(type) { 28 case *syscall.SockaddrInet4: 29 return &IPAddr{IP: sa.Addr[0:]} 30 case *syscall.SockaddrInet6: 31 return &IPAddr{IP: sa.Addr[0:], Zone: zoneToString(int(sa.ZoneId))} 32 } 33 return nil 34 } 35 36 func (a *IPAddr) family() int { 37 if a == nil || len(a.IP) <= IPv4len { 38 return syscall.AF_INET 39 } 40 if a.IP.To4() != nil { 41 return syscall.AF_INET 42 } 43 return syscall.AF_INET6 44 } 45 46 func (a *IPAddr) sockaddr(family int) (syscall.Sockaddr, error) { 47 if a == nil { 48 return nil, nil 49 } 50 return ipToSockaddr(family, a.IP, 0, a.Zone) 51 } 52 53 // IPConn is the implementation of the Conn and PacketConn interfaces 54 // for IP network connections. 55 type IPConn struct { 56 conn 57 } 58 59 func newIPConn(fd *netFD) *IPConn { return &IPConn{conn{fd}} } 60 61 // ReadFromIP reads an IP packet from c, copying the payload into b. 62 // It returns the number of bytes copied into b and the return address 63 // that was on the packet. 64 // 65 // ReadFromIP can be made to time out and return an error with 66 // Timeout() == true after a fixed time limit; see SetDeadline and 67 // SetReadDeadline. 68 func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error) { 69 if !c.ok() { 70 return 0, nil, syscall.EINVAL 71 } 72 // TODO(cw,rsc): consider using readv if we know the family 73 // type to avoid the header trim/copy 74 var addr *IPAddr 75 n, sa, err := c.fd.readFrom(b) 76 switch sa := sa.(type) { 77 case *syscall.SockaddrInet4: 78 addr = &IPAddr{IP: sa.Addr[0:]} 79 n = stripIPv4Header(n, b) 80 case *syscall.SockaddrInet6: 81 addr = &IPAddr{IP: sa.Addr[0:], Zone: zoneToString(int(sa.ZoneId))} 82 } 83 return n, addr, err 84 } 85 86 func stripIPv4Header(n int, b []byte) int { 87 if len(b) < 20 { 88 return n 89 } 90 l := int(b[0]&0x0f) << 2 91 if 20 > l || l > len(b) { 92 return n 93 } 94 if b[0]>>4 != 4 { 95 return n 96 } 97 copy(b, b[l:]) 98 return n - l 99 } 100 101 // ReadFrom implements the PacketConn ReadFrom method. 102 func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) { 103 if !c.ok() { 104 return 0, nil, syscall.EINVAL 105 } 106 n, addr, err := c.ReadFromIP(b) 107 if addr == nil { 108 return n, nil, err 109 } 110 return n, addr, err 111 } 112 113 // ReadMsgIP reads a packet from c, copying the payload into b and the 114 // associated out-of-band data into oob. It returns the number of 115 // bytes copied into b, the number of bytes copied into oob, the flags 116 // that were set on the packet and the source address of the packet. 117 func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) { 118 if !c.ok() { 119 return 0, 0, 0, nil, syscall.EINVAL 120 } 121 var sa syscall.Sockaddr 122 n, oobn, flags, sa, err = c.fd.readMsg(b, oob) 123 switch sa := sa.(type) { 124 case *syscall.SockaddrInet4: 125 addr = &IPAddr{IP: sa.Addr[0:]} 126 case *syscall.SockaddrInet6: 127 addr = &IPAddr{IP: sa.Addr[0:], Zone: zoneToString(int(sa.ZoneId))} 128 } 129 return 130 } 131 132 // WriteToIP writes an IP packet to addr via c, copying the payload 133 // from b. 134 // 135 // WriteToIP can be made to time out and return an error with 136 // Timeout() == true after a fixed time limit; see SetDeadline and 137 // SetWriteDeadline. On packet-oriented connections, write timeouts 138 // are rare. 139 func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) { 140 if !c.ok() { 141 return 0, syscall.EINVAL 142 } 143 if c.fd.isConnected { 144 return 0, &OpError{Op: "write", Net: c.fd.net, Addr: addr, Err: ErrWriteToConnected} 145 } 146 if addr == nil { 147 return 0, &OpError{Op: "write", Net: c.fd.net, Addr: nil, Err: errMissingAddress} 148 } 149 sa, err := addr.sockaddr(c.fd.family) 150 if err != nil { 151 return 0, &OpError{"write", c.fd.net, addr, err} 152 } 153 return c.fd.writeTo(b, sa) 154 } 155 156 // WriteTo implements the PacketConn WriteTo method. 157 func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) { 158 if !c.ok() { 159 return 0, syscall.EINVAL 160 } 161 a, ok := addr.(*IPAddr) 162 if !ok { 163 return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL} 164 } 165 return c.WriteToIP(b, a) 166 } 167 168 // WriteMsgIP writes a packet to addr via c, copying the payload from 169 // b and the associated out-of-band data from oob. It returns the 170 // number of payload and out-of-band bytes written. 171 func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error) { 172 if !c.ok() { 173 return 0, 0, syscall.EINVAL 174 } 175 if c.fd.isConnected { 176 return 0, 0, &OpError{Op: "write", Net: c.fd.net, Addr: addr, Err: ErrWriteToConnected} 177 } 178 if addr == nil { 179 return 0, 0, &OpError{Op: "write", Net: c.fd.net, Addr: nil, Err: errMissingAddress} 180 } 181 sa, err := addr.sockaddr(c.fd.family) 182 if err != nil { 183 return 0, 0, &OpError{"write", c.fd.net, addr, err} 184 } 185 return c.fd.writeMsg(b, oob, sa) 186 } 187 188 // DialIP connects to the remote address raddr on the network protocol 189 // netProto, which must be "ip", "ip4", or "ip6" followed by a colon 190 // and a protocol number or name. 191 func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error) { 192 return dialIP(netProto, laddr, raddr, noDeadline) 193 } 194 195 func dialIP(netProto string, laddr, raddr *IPAddr, deadline time.Time) (*IPConn, error) { 196 net, proto, err := parseNetwork(netProto) 197 if err != nil { 198 return nil, &OpError{Op: "dial", Net: netProto, Addr: raddr, Err: err} 199 } 200 switch net { 201 case "ip", "ip4", "ip6": 202 default: 203 return nil, &OpError{Op: "dial", Net: netProto, Addr: raddr, Err: UnknownNetworkError(netProto)} 204 } 205 if raddr == nil { 206 return nil, &OpError{Op: "dial", Net: netProto, Addr: nil, Err: errMissingAddress} 207 } 208 fd, err := internetSocket(net, laddr, raddr, deadline, syscall.SOCK_RAW, proto, "dial") 209 if err != nil { 210 return nil, &OpError{Op: "dial", Net: netProto, Addr: raddr, Err: err} 211 } 212 return newIPConn(fd), nil 213 } 214 215 // ListenIP listens for incoming IP packets addressed to the local 216 // address laddr. The returned connection's ReadFrom and WriteTo 217 // methods can be used to receive and send IP packets with per-packet 218 // addressing. 219 func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) { 220 net, proto, err := parseNetwork(netProto) 221 if err != nil { 222 return nil, &OpError{Op: "dial", Net: netProto, Addr: laddr, Err: err} 223 } 224 switch net { 225 case "ip", "ip4", "ip6": 226 default: 227 return nil, &OpError{Op: "listen", Net: netProto, Addr: laddr, Err: UnknownNetworkError(netProto)} 228 } 229 fd, err := internetSocket(net, laddr, nil, noDeadline, syscall.SOCK_RAW, proto, "listen") 230 if err != nil { 231 return nil, &OpError{Op: "listen", Net: netProto, Addr: laddr, Err: err} 232 } 233 return newIPConn(fd), nil 234 }