blob: 04eee35faaa08a29c2d9e773139fcbdf8e98d53e [file] [log] [blame]
Mikio Haracdfc4ce2013-06-04 17:42:58 +09001// 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
5package ipv6
6
7import (
8 "net"
9 "syscall"
Mikio Hara2c084522013-06-15 13:05:36 +090010 "time"
Mikio Haracdfc4ce2013-06-04 17:42:58 +090011)
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.
16type Conn struct {
17 genericOpt
18}
19
20type genericOpt struct {
21 net.Conn
22}
23
24func (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.
28func (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.
40func 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.
51type PacketConn struct {
52 genericOpt
53 dgramOpt
54 payloadHandler
55}
56
57type dgramOpt struct {
58 net.PacketConn
59}
60
61func (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.
65func (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 Hara2c084522013-06-15 13:05:36 +090076// SetDeadline sets the read and write deadlines associated with the
77// endpoint.
78func (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.
87func (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.
96func (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 Hara5b599ab2013-06-05 20:08:10 +0900103// Close closes the endpoint.
104func (c *PacketConn) Close() error {
105 if !c.payloadHandler.ok() {
106 return syscall.EINVAL
107 }
Mikio Hara2c084522013-06-15 13:05:36 +0900108 return c.payloadHandler.Close()
Mikio Hara5b599ab2013-06-05 20:08:10 +0900109}
110
Mikio Haracdfc4ce2013-06-04 17:42:58 +0900111// NewPacketConn returns a new PacketConn using c as its underlying
112// transport.
113func 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}