Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 1 | // Copyright 2013 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 ipv6 |
| 6 | |
| 7 | import ( |
| 8 | "net" |
| 9 | "syscall" |
Mikio Hara | 2c08452 | 2013-06-15 13:05:36 +0900 | [diff] [blame] | 10 | "time" |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | // A Conn represents a network endpoint that uses IPv6 transport. |
| 14 | // It allows to set basic IP-level socket options such as traffic |
| 15 | // class and hop limit. |
| 16 | type Conn struct { |
| 17 | genericOpt |
| 18 | } |
| 19 | |
| 20 | type genericOpt struct { |
| 21 | net.Conn |
| 22 | } |
| 23 | |
| 24 | func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil } |
| 25 | |
| 26 | // PathMTU returns a path MTU value for the destination associated |
| 27 | // with the endpoint. |
| 28 | func (c *Conn) PathMTU() (int, error) { |
| 29 | if !c.genericOpt.ok() { |
| 30 | return 0, syscall.EINVAL |
| 31 | } |
| 32 | fd, err := c.genericOpt.sysfd() |
| 33 | if err != nil { |
| 34 | return 0, err |
| 35 | } |
| 36 | return ipv6PathMTU(fd) |
| 37 | } |
| 38 | |
| 39 | // NewConn returns a new Conn. |
| 40 | func NewConn(c net.Conn) *Conn { |
| 41 | return &Conn{ |
| 42 | genericOpt: genericOpt{Conn: c}, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // A PacketConn represents a packet network endpoint that uses IPv6 |
| 47 | // transport. It is used to control several IP-level socket options |
| 48 | // including IPv6 header manipulation. It also provides datagram |
| 49 | // based network I/O methods specific to the IPv6 and higher layer |
| 50 | // protocols such as OSPF, GRE, and UDP. |
| 51 | type PacketConn struct { |
| 52 | genericOpt |
| 53 | dgramOpt |
| 54 | payloadHandler |
| 55 | } |
| 56 | |
| 57 | type dgramOpt struct { |
| 58 | net.PacketConn |
| 59 | } |
| 60 | |
| 61 | func (c *dgramOpt) ok() bool { return c != nil && c.PacketConn != nil } |
| 62 | |
| 63 | // SetControlMessage allows to receive the per packet basis IP-level |
| 64 | // socket options. |
| 65 | func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error { |
| 66 | if !c.payloadHandler.ok() { |
| 67 | return syscall.EINVAL |
| 68 | } |
| 69 | fd, err := c.payloadHandler.sysfd() |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | return setControlMessage(fd, &c.payloadHandler.rawOpt, cf, on) |
| 74 | } |
| 75 | |
Mikio Hara | 2c08452 | 2013-06-15 13:05:36 +0900 | [diff] [blame] | 76 | // SetDeadline sets the read and write deadlines associated with the |
| 77 | // endpoint. |
| 78 | func (c *PacketConn) SetDeadline(t time.Time) error { |
| 79 | if !c.payloadHandler.ok() { |
| 80 | return syscall.EINVAL |
| 81 | } |
| 82 | return c.payloadHandler.SetDeadline(t) |
| 83 | } |
| 84 | |
| 85 | // SetReadDeadline sets the read deadline associated with the |
| 86 | // endpoint. |
| 87 | func (c *PacketConn) SetReadDeadline(t time.Time) error { |
| 88 | if !c.payloadHandler.ok() { |
| 89 | return syscall.EINVAL |
| 90 | } |
| 91 | return c.payloadHandler.SetReadDeadline(t) |
| 92 | } |
| 93 | |
| 94 | // SetWriteDeadline sets the write deadline associated with the |
| 95 | // endpoint. |
| 96 | func (c *PacketConn) SetWriteDeadline(t time.Time) error { |
| 97 | if !c.payloadHandler.ok() { |
| 98 | return syscall.EINVAL |
| 99 | } |
| 100 | return c.payloadHandler.SetWriteDeadline(t) |
| 101 | } |
| 102 | |
Mikio Hara | 5b599ab | 2013-06-05 20:08:10 +0900 | [diff] [blame] | 103 | // Close closes the endpoint. |
| 104 | func (c *PacketConn) Close() error { |
| 105 | if !c.payloadHandler.ok() { |
| 106 | return syscall.EINVAL |
| 107 | } |
Mikio Hara | 2c08452 | 2013-06-15 13:05:36 +0900 | [diff] [blame] | 108 | return c.payloadHandler.Close() |
Mikio Hara | 5b599ab | 2013-06-05 20:08:10 +0900 | [diff] [blame] | 109 | } |
| 110 | |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 111 | // NewPacketConn returns a new PacketConn using c as its underlying |
| 112 | // transport. |
| 113 | func NewPacketConn(c net.PacketConn) *PacketConn { |
| 114 | return &PacketConn{ |
| 115 | genericOpt: genericOpt{Conn: c.(net.Conn)}, |
| 116 | dgramOpt: dgramOpt{PacketConn: c}, |
| 117 | payloadHandler: payloadHandler{PacketConn: c}, |
| 118 | } |
| 119 | } |