Dmitri Shuralyov | 5d99779 | 2016-11-07 15:05:57 -0800 | [diff] [blame] | 1 | // Copyright 2012 The Go Authors. All rights reserved. |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 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 ipv4 |
| 6 | |
| 7 | import ( |
| 8 | "net" |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 9 | "time" |
Mikio Hara | 7625277 | 2016-08-25 06:13:12 +0900 | [diff] [blame] | 10 | |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 11 | "golang.org/x/net/internal/socket" |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 12 | ) |
| 13 | |
Mikio Hara | 0cc8884 | 2016-12-15 07:16:17 +0900 | [diff] [blame] | 14 | // BUG(mikio): On Windows, the JoinSourceSpecificGroup, |
| 15 | // LeaveSourceSpecificGroup, ExcludeSourceSpecificGroup and |
| 16 | // IncludeSourceSpecificGroup methods of PacketConn and RawConn are |
| 17 | // not implemented. |
| 18 | |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 19 | // A Conn represents a network endpoint that uses the IPv4 transport. |
| 20 | // It is used to control basic IP-level socket options such as TOS and |
| 21 | // TTL. |
| 22 | type Conn struct { |
| 23 | genericOpt |
| 24 | } |
| 25 | |
| 26 | type genericOpt struct { |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 27 | *socket.Conn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 28 | } |
| 29 | |
Mikio Hara | 9d07103 | 2013-06-17 00:37:19 +0900 | [diff] [blame] | 30 | func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil } |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 31 | |
| 32 | // NewConn returns a new Conn. |
| 33 | func NewConn(c net.Conn) *Conn { |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 34 | cc, _ := socket.NewConn(c) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 35 | return &Conn{ |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 36 | genericOpt: genericOpt{Conn: cc}, |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | // A PacketConn represents a packet network endpoint that uses the |
Dmitri Shuralyov | 357296a | 2016-11-07 15:02:51 -0800 | [diff] [blame] | 41 | // IPv4 transport. It is used to control several IP-level socket |
| 42 | // options including multicasting. It also provides datagram based |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 43 | // network I/O methods specific to the IPv4 and higher layer protocols |
| 44 | // such as UDP. |
| 45 | type PacketConn struct { |
| 46 | genericOpt |
| 47 | dgramOpt |
| 48 | payloadHandler |
| 49 | } |
| 50 | |
| 51 | type dgramOpt struct { |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 52 | *socket.Conn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 53 | } |
| 54 | |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 55 | func (c *dgramOpt) ok() bool { return c != nil && c.Conn != nil } |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 56 | |
| 57 | // SetControlMessage sets the per packet IP-level socket options. |
| 58 | func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error { |
| 59 | if !c.payloadHandler.ok() { |
Mikio Hara | c0ac1a5 | 2018-07-02 02:08:19 +0900 | [diff] [blame] | 60 | return errInvalidConn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 61 | } |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 62 | return setControlMessage(c.dgramOpt.Conn, &c.payloadHandler.rawOpt, cf, on) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // SetDeadline sets the read and write deadlines associated with the |
| 66 | // endpoint. |
| 67 | func (c *PacketConn) SetDeadline(t time.Time) error { |
| 68 | if !c.payloadHandler.ok() { |
Mikio Hara | c0ac1a5 | 2018-07-02 02:08:19 +0900 | [diff] [blame] | 69 | return errInvalidConn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 70 | } |
Mikio Hara | 9d07103 | 2013-06-17 00:37:19 +0900 | [diff] [blame] | 71 | return c.payloadHandler.PacketConn.SetDeadline(t) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // SetReadDeadline sets the read deadline associated with the |
| 75 | // endpoint. |
| 76 | func (c *PacketConn) SetReadDeadline(t time.Time) error { |
| 77 | if !c.payloadHandler.ok() { |
Mikio Hara | c0ac1a5 | 2018-07-02 02:08:19 +0900 | [diff] [blame] | 78 | return errInvalidConn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 79 | } |
Mikio Hara | 9d07103 | 2013-06-17 00:37:19 +0900 | [diff] [blame] | 80 | return c.payloadHandler.PacketConn.SetReadDeadline(t) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // SetWriteDeadline sets the write deadline associated with the |
| 84 | // endpoint. |
| 85 | func (c *PacketConn) SetWriteDeadline(t time.Time) error { |
| 86 | if !c.payloadHandler.ok() { |
Mikio Hara | c0ac1a5 | 2018-07-02 02:08:19 +0900 | [diff] [blame] | 87 | return errInvalidConn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 88 | } |
Mikio Hara | 9d07103 | 2013-06-17 00:37:19 +0900 | [diff] [blame] | 89 | return c.payloadHandler.PacketConn.SetWriteDeadline(t) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Close closes the endpoint. |
| 93 | func (c *PacketConn) Close() error { |
| 94 | if !c.payloadHandler.ok() { |
Mikio Hara | c0ac1a5 | 2018-07-02 02:08:19 +0900 | [diff] [blame] | 95 | return errInvalidConn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 96 | } |
Mikio Hara | 9d07103 | 2013-06-17 00:37:19 +0900 | [diff] [blame] | 97 | return c.payloadHandler.PacketConn.Close() |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // NewPacketConn returns a new PacketConn using c as its underlying |
| 101 | // transport. |
| 102 | func NewPacketConn(c net.PacketConn) *PacketConn { |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 103 | cc, _ := socket.NewConn(c.(net.Conn)) |
Mikio Hara | 5c9495a | 2014-11-29 10:17:57 +0900 | [diff] [blame] | 104 | p := &PacketConn{ |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 105 | genericOpt: genericOpt{Conn: cc}, |
| 106 | dgramOpt: dgramOpt{Conn: cc}, |
Mikio Hara | b7a1f62 | 2017-03-18 18:28:25 +0900 | [diff] [blame] | 107 | payloadHandler: payloadHandler{PacketConn: c, Conn: cc}, |
Mikio Hara | 5c9495a | 2014-11-29 10:17:57 +0900 | [diff] [blame] | 108 | } |
| 109 | return p |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // A RawConn represents a packet network endpoint that uses the IPv4 |
Dmitri Shuralyov | 357296a | 2016-11-07 15:02:51 -0800 | [diff] [blame] | 113 | // transport. It is used to control several IP-level socket options |
| 114 | // including IPv4 header manipulation. It also provides datagram |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 115 | // based network I/O methods specific to the IPv4 and higher layer |
| 116 | // protocols that handle IPv4 datagram directly such as OSPF, GRE. |
| 117 | type RawConn struct { |
| 118 | genericOpt |
| 119 | dgramOpt |
| 120 | packetHandler |
| 121 | } |
| 122 | |
| 123 | // SetControlMessage sets the per packet IP-level socket options. |
| 124 | func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error { |
| 125 | if !c.packetHandler.ok() { |
Mikio Hara | c0ac1a5 | 2018-07-02 02:08:19 +0900 | [diff] [blame] | 126 | return errInvalidConn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 127 | } |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 128 | return setControlMessage(c.dgramOpt.Conn, &c.packetHandler.rawOpt, cf, on) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // SetDeadline sets the read and write deadlines associated with the |
| 132 | // endpoint. |
| 133 | func (c *RawConn) SetDeadline(t time.Time) error { |
| 134 | if !c.packetHandler.ok() { |
Mikio Hara | c0ac1a5 | 2018-07-02 02:08:19 +0900 | [diff] [blame] | 135 | return errInvalidConn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 136 | } |
Mikio Hara | b7a1f62 | 2017-03-18 18:28:25 +0900 | [diff] [blame] | 137 | return c.packetHandler.IPConn.SetDeadline(t) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // SetReadDeadline sets the read deadline associated with the |
| 141 | // endpoint. |
| 142 | func (c *RawConn) SetReadDeadline(t time.Time) error { |
| 143 | if !c.packetHandler.ok() { |
Mikio Hara | c0ac1a5 | 2018-07-02 02:08:19 +0900 | [diff] [blame] | 144 | return errInvalidConn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 145 | } |
Mikio Hara | b7a1f62 | 2017-03-18 18:28:25 +0900 | [diff] [blame] | 146 | return c.packetHandler.IPConn.SetReadDeadline(t) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // SetWriteDeadline sets the write deadline associated with the |
| 150 | // endpoint. |
| 151 | func (c *RawConn) SetWriteDeadline(t time.Time) error { |
| 152 | if !c.packetHandler.ok() { |
Mikio Hara | c0ac1a5 | 2018-07-02 02:08:19 +0900 | [diff] [blame] | 153 | return errInvalidConn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 154 | } |
Mikio Hara | b7a1f62 | 2017-03-18 18:28:25 +0900 | [diff] [blame] | 155 | return c.packetHandler.IPConn.SetWriteDeadline(t) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // Close closes the endpoint. |
| 159 | func (c *RawConn) Close() error { |
| 160 | if !c.packetHandler.ok() { |
Mikio Hara | c0ac1a5 | 2018-07-02 02:08:19 +0900 | [diff] [blame] | 161 | return errInvalidConn |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 162 | } |
Mikio Hara | b7a1f62 | 2017-03-18 18:28:25 +0900 | [diff] [blame] | 163 | return c.packetHandler.IPConn.Close() |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 164 | } |
| 165 | |
Mikio Hara | cb3df7e | 2013-05-30 20:22:16 +0900 | [diff] [blame] | 166 | // NewRawConn returns a new RawConn using c as its underlying |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 167 | // transport. |
| 168 | func NewRawConn(c net.PacketConn) (*RawConn, error) { |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 169 | cc, err := socket.NewConn(c.(net.Conn)) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 170 | if err != nil { |
| 171 | return nil, err |
| 172 | } |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 173 | r := &RawConn{ |
| 174 | genericOpt: genericOpt{Conn: cc}, |
| 175 | dgramOpt: dgramOpt{Conn: cc}, |
Mikio Hara | b7a1f62 | 2017-03-18 18:28:25 +0900 | [diff] [blame] | 176 | packetHandler: packetHandler{IPConn: c.(*net.IPConn), Conn: cc}, |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 177 | } |
| 178 | so, ok := sockOpts[ssoHeaderPrepend] |
| 179 | if !ok { |
Mikio Hara | 511f794 | 2019-03-13 15:30:34 +0900 | [diff] [blame] | 180 | return nil, errNotImplemented |
Mikio Hara | c4fa6e0 | 2017-02-15 06:52:17 +0900 | [diff] [blame] | 181 | } |
| 182 | if err := so.SetInt(r.dgramOpt.Conn, boolint(true)); err != nil { |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 183 | return nil, err |
| 184 | } |
| 185 | return r, nil |
| 186 | } |