1 // Copyright 2017 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.
17 // A Conn represents a raw connection.
22 // NewConn returns a new raw connection.
23 func NewConn(c net.Conn) (*Conn, error) {
24 return &Conn{c: c}, nil
27 func (o *Option) get(c *Conn, b []byte) (int, error) {
28 s, err := socketOf(c.c)
32 n, err := getsockopt(s, o.Level, o.Name, b)
33 return n, os.NewSyscallError("getsockopt", err)
36 func (o *Option) set(c *Conn, b []byte) error {
37 s, err := socketOf(c.c)
41 return os.NewSyscallError("setsockopt", setsockopt(s, o.Level, o.Name, b))
44 func socketOf(c net.Conn) (uintptr, error) {
46 case *net.TCPConn, *net.UDPConn, *net.IPConn:
47 v := reflect.ValueOf(c)
48 switch e := v.Elem(); e.Kind() {
50 fd := e.FieldByName("conn").FieldByName("fd")
51 switch e := fd.Elem(); e.Kind() {
53 sysfd := e.FieldByName("sysfd")
54 if runtime.GOOS == "windows" {
55 return uintptr(sysfd.Uint()), nil
57 return uintptr(sysfd.Int()), nil
61 return 0, errors.New("invalid type")