// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package socket

import (
	
	
	
	
)

// A Conn represents a raw connection.
type Conn struct {
	network string
	c       syscall.RawConn
}

// tcpConn is an interface implemented by net.TCPConn.
// It can be used for interface assertions to check if a net.Conn is a TCP connection.
type tcpConn interface {
	SyscallConn() (syscall.RawConn, error)
	SetLinger(int) error
}

var _ tcpConn = (*net.TCPConn)(nil)

// udpConn is an interface implemented by net.UDPConn.
// It can be used for interface assertions to check if a net.Conn is a UDP connection.
type udpConn interface {
	SyscallConn() (syscall.RawConn, error)
	ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error)
}

var _ udpConn = (*net.UDPConn)(nil)

// ipConn is an interface implemented by net.IPConn.
// It can be used for interface assertions to check if a net.Conn is an IP connection.
type ipConn interface {
	SyscallConn() (syscall.RawConn, error)
	ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *net.IPAddr, err error)
}

var _ ipConn = (*net.IPConn)(nil)

// NewConn returns a new raw connection.
func ( net.Conn) (*Conn, error) {
	var  error
	var  Conn
	switch c := .(type) {
	case tcpConn:
		.network = "tcp"
		.c,  = .SyscallConn()
	case udpConn:
		.network = "udp"
		.c,  = .SyscallConn()
	case ipConn:
		.network = "ip"
		.c,  = .SyscallConn()
	default:
		return nil, errors.New("unknown connection type")
	}
	if  != nil {
		return nil, 
	}
	return &, nil
}

func ( *Option) ( *Conn,  []byte) (int, error) {
	var  error
	var  int
	 := func( uintptr) {
		,  = getsockopt(, .Level, .Name, )
	}
	if  := .c.Control();  != nil {
		return 0, 
	}
	return , os.NewSyscallError("getsockopt", )
}

func ( *Option) ( *Conn,  []byte) error {
	var  error
	 := func( uintptr) {
		 = setsockopt(, .Level, .Name, )
	}
	if  := .c.Control();  != nil {
		return 
	}
	return os.NewSyscallError("setsockopt", )
}