github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/net/iprawsock.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 package net 6 7 import ( 8 "context" 9 "syscall" 10 ) 11 12 // IPAddr represents the address of an IP end point. 13 type IPAddr struct { 14 IP IP 15 Zone string // IPv6 scoped addressing zone 16 } 17 18 // Network returns the address's network name, "ip". 19 func (a *IPAddr) Network() string { return "ip" } 20 21 func (a *IPAddr) String() string { 22 if a == nil { 23 return "<nil>" 24 } 25 ip := ipEmptyString(a.IP) 26 if a.Zone != "" { 27 return ip + "%" + a.Zone 28 } 29 return ip 30 } 31 32 func (a *IPAddr) isWildcard() bool { 33 if a == nil || a.IP == nil { 34 return true 35 } 36 return a.IP.IsUnspecified() 37 } 38 39 func (a *IPAddr) opAddr() Addr { 40 if a == nil { 41 return nil 42 } 43 return a 44 } 45 46 // ResolveIPAddr parses addr as an IP address of the form "host" or 47 // "ipv6-host%zone" and resolves the domain name on the network net, 48 // which must be "ip", "ip4" or "ip6". 49 func ResolveIPAddr(net, addr string) (*IPAddr, error) { 50 if net == "" { // a hint wildcard for Go 1.0 undocumented behavior 51 net = "ip" 52 } 53 afnet, _, err := parseNetwork(context.Background(), net) 54 if err != nil { 55 return nil, err 56 } 57 switch afnet { 58 case "ip", "ip4", "ip6": 59 default: 60 return nil, UnknownNetworkError(net) 61 } 62 addrs, err := internetAddrList(context.Background(), afnet, addr) 63 if err != nil { 64 return nil, err 65 } 66 return addrs.first(isIPv4).(*IPAddr), nil 67 } 68 69 // IPConn is the implementation of the Conn and PacketConn interfaces 70 // for IP network connections. 71 type IPConn struct { 72 conn 73 } 74 75 // ReadFromIP reads an IP packet from c, copying the payload into b. 76 // It returns the number of bytes copied into b and the return address 77 // that was on the packet. 78 // 79 // ReadFromIP can be made to time out and return an error with 80 // Timeout() == true after a fixed time limit; see SetDeadline and 81 // SetReadDeadline. 82 func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error) { 83 if !c.ok() { 84 return 0, nil, syscall.EINVAL 85 } 86 n, addr, err := c.readFrom(b) 87 if err != nil { 88 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 89 } 90 return n, addr, err 91 } 92 93 // ReadFrom implements the PacketConn ReadFrom method. 94 func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) { 95 if !c.ok() { 96 return 0, nil, syscall.EINVAL 97 } 98 n, addr, err := c.readFrom(b) 99 if err != nil { 100 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 101 } 102 if addr == nil { 103 return n, nil, err 104 } 105 return n, addr, err 106 } 107 108 // ReadMsgIP reads a packet from c, copying the payload into b and the 109 // associated out-of-band data into oob. It returns the number of 110 // bytes copied into b, the number of bytes copied into oob, the flags 111 // that were set on the packet and the source address of the packet. 112 func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) { 113 if !c.ok() { 114 return 0, 0, 0, nil, syscall.EINVAL 115 } 116 n, oobn, flags, addr, err = c.readMsg(b, oob) 117 if err != nil { 118 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 119 } 120 return 121 } 122 123 // WriteToIP writes an IP packet to addr via c, copying the payload 124 // from b. 125 // 126 // WriteToIP can be made to time out and return an error with 127 // Timeout() == true after a fixed time limit; see SetDeadline and 128 // SetWriteDeadline. On packet-oriented connections, write timeouts 129 // are rare. 130 func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) { 131 if !c.ok() { 132 return 0, syscall.EINVAL 133 } 134 n, err := c.writeTo(b, addr) 135 if err != nil { 136 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err} 137 } 138 return n, err 139 } 140 141 // WriteTo implements the PacketConn WriteTo method. 142 func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) { 143 if !c.ok() { 144 return 0, syscall.EINVAL 145 } 146 a, ok := addr.(*IPAddr) 147 if !ok { 148 return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL} 149 } 150 n, err := c.writeTo(b, a) 151 if err != nil { 152 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err} 153 } 154 return n, err 155 } 156 157 // WriteMsgIP writes a packet to addr via c, copying the payload from 158 // b and the associated out-of-band data from oob. It returns the 159 // number of payload and out-of-band bytes written. 160 func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error) { 161 if !c.ok() { 162 return 0, 0, syscall.EINVAL 163 } 164 n, oobn, err = c.writeMsg(b, oob, addr) 165 if err != nil { 166 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err} 167 } 168 return 169 } 170 171 func newIPConn(fd *netFD) *IPConn { return &IPConn{conn{fd}} } 172 173 // DialIP connects to the remote address raddr on the network protocol 174 // netProto, which must be "ip", "ip4", or "ip6" followed by a colon 175 // and a protocol number or name. 176 func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error) { 177 c, err := dialIP(context.Background(), netProto, laddr, raddr) 178 if err != nil { 179 return nil, &OpError{Op: "dial", Net: netProto, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err} 180 } 181 return c, nil 182 } 183 184 // ListenIP listens for incoming IP packets addressed to the local 185 // address laddr. The returned connection's ReadFrom and WriteTo 186 // methods can be used to receive and send IP packets with per-packet 187 // addressing. 188 func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) { 189 c, err := listenIP(context.Background(), netProto, laddr) 190 if err != nil { 191 return nil, &OpError{Op: "listen", Net: netProto, Source: nil, Addr: laddr.opAddr(), Err: err} 192 } 193 return c, nil 194 }