socket_linux.go 4.09 KB
Newer Older
1
// socket_linux.go -- Socket handling specific to GNU/Linux.
2 3 4 5 6 7 8

// Copyright 2010 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 syscall

9 10
import "unsafe"

11 12 13
const SizeofSockaddrInet4 = 16
const SizeofSockaddrInet6 = 28
const SizeofSockaddrUnix = 110
14 15 16 17 18 19 20 21 22 23 24 25 26
const SizeofSockaddrLinklayer = 20
const SizeofSockaddrNetlink = 12

type SockaddrLinklayer struct {
	Protocol uint16
	Ifindex  int
	Hatype   uint16
	Pkttype  uint8
	Halen    uint8
	Addr     [8]byte
	raw      RawSockaddrLinklayer
}

27
func (sa *SockaddrLinklayer) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
28 29 30 31 32 33 34 35 36 37 38 39
	if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff {
		return nil, 0, EINVAL
	}
	sa.raw.Family = AF_PACKET
	sa.raw.Protocol = sa.Protocol
	sa.raw.Ifindex = int32(sa.Ifindex)
	sa.raw.Hatype = sa.Hatype
	sa.raw.Pkttype = sa.Pkttype
	sa.raw.Halen = sa.Halen
	for i := 0; i < len(sa.Addr); i++ {
		sa.raw.Addr[i] = sa.Addr[i]
	}
40
	return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), SizeofSockaddrLinklayer, nil
41 42 43 44 45 46 47 48 49 50
}

type SockaddrNetlink struct {
	Family uint16
	Pad    uint16
	Pid    uint32
	Groups uint32
	raw    RawSockaddrNetlink
}

51
func (sa *SockaddrNetlink) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
52 53 54 55
	sa.raw.Family = AF_NETLINK
	sa.raw.Pad = sa.Pad
	sa.raw.Pid = sa.Pid
	sa.raw.Groups = sa.Groups
56
	return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), SizeofSockaddrNetlink, nil
57
}
58 59

type RawSockaddrInet4 struct {
60
	Family uint16
61 62 63
	Port   uint16
	Addr   [4]byte /* in_addr */
	Zero   [8]uint8
64 65
}

66 67 68 69
func (sa *RawSockaddrInet4) setLen() Socklen_t {
	return SizeofSockaddrInet4
}

70
type RawSockaddrInet6 struct {
71 72
	Family   uint16
	Port     uint16
73
	Flowinfo uint32
74
	Addr     [16]byte /* in6_addr */
75
	Scope_id uint32
76 77
}

78 79
func (sa *RawSockaddrInet6) setLen() Socklen_t {
	return SizeofSockaddrInet6
80 81
}

82
type RawSockaddrUnix struct {
83
	Family uint16
84
	Path   [108]int8
85 86
}

87
func (sa *RawSockaddrUnix) setLen(int) {
88 89
}

90
func (sa *RawSockaddrUnix) getLen() (int, error) {
91 92 93 94 95 96
	if sa.Path[0] == 0 {
		// "Abstract" Unix domain socket.
		// Rewrite leading NUL as @ for textual display.
		// (This is the standard convention.)
		// Not friendly to overwrite in place,
		// but the callers below don't care.
97
		sa.Path[0] = '@'
98 99
	}

100
	// Assume path ends at NUL.
101
	// This is not technically the GNU/Linux semantics for
102 103 104
	// abstract Unix domain sockets--they are supposed
	// to be uninterpreted fixed-size binary blobs--but
	// everyone uses this convention.
105
	n := 0
106
	for n < len(sa.Path) && sa.Path[n] != 0 {
107
		n++
108 109
	}

110
	return n, nil
111 112
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
type RawSockaddrLinklayer struct {
	Family   uint16
	Protocol uint16
	Ifindex  int32
	Hatype   uint16
	Pkttype  uint8
	Halen    uint8
	Addr     [8]uint8
}

type RawSockaddrNetlink struct {
	Family uint16
	Pad    uint16
	Pid    uint32
	Groups uint32
}

130
type RawSockaddr struct {
131
	Family uint16
132
	Data   [14]int8
133 134 135
}

// BindToDevice binds the socket associated with fd to device.
136
func BindToDevice(fd int, device string) (err error) {
137 138
	return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device)
}
139

140
func anyToSockaddrOS(rsa *RawSockaddrAny) (Sockaddr, error) {
141 142 143 144 145 146 147 148
	switch rsa.Addr.Family {
	case AF_NETLINK:
		pp := (*RawSockaddrNetlink)(unsafe.Pointer(rsa))
		sa := new(SockaddrNetlink)
		sa.Family = pp.Family
		sa.Pad = pp.Pad
		sa.Pid = pp.Pid
		sa.Groups = pp.Groups
149
		return sa, nil
150 151 152 153 154 155 156 157 158 159 160 161

	case AF_PACKET:
		pp := (*RawSockaddrLinklayer)(unsafe.Pointer(rsa))
		sa := new(SockaddrLinklayer)
		sa.Protocol = pp.Protocol
		sa.Ifindex = int(pp.Ifindex)
		sa.Hatype = pp.Hatype
		sa.Pkttype = pp.Pkttype
		sa.Halen = pp.Halen
		for i := 0; i < len(sa.Addr); i++ {
			sa.Addr[i] = pp.Addr[i]
		}
162
		return sa, nil
163
	}
164 165 166
	return nil, EAFNOSUPPORT
}

167
//sysnb	EpollCreate(size int) (fd int, err error)
168
//epoll_create(size _C_int) _C_int
169

170
//sysnb EpollCreate1(flags int) (fd int, err error)
171
//epoll_create1(flags _C_int) _C_int
172

173
//sysnb	EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error)
174
//epoll_ctl(epfd _C_int, op _C_int, fd _C_int, event *EpollEvent) _C_int
175

176
//sys	EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
177
//epoll_wait(epfd _C_int, events *EpollEvent, maxevents _C_int, timeout _C_int) _C_int