1 // socket_linux.go -- Socket handling specific to GNU/Linux.
3 // Copyright 2010 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 const SizeofSockaddrInet4 = 16
12 const SizeofSockaddrInet6 = 28
13 const SizeofSockaddrUnix = 110
14 const SizeofSockaddrLinklayer = 20
15 const SizeofSockaddrNetlink = 12
17 type SockaddrLinklayer struct {
24 raw RawSockaddrLinklayer
27 func (sa *SockaddrLinklayer) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
28 if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff {
31 sa.raw.Family = AF_PACKET
32 sa.raw.Protocol = sa.Protocol
33 sa.raw.Ifindex = int32(sa.Ifindex)
34 sa.raw.Hatype = sa.Hatype
35 sa.raw.Pkttype = sa.Pkttype
36 sa.raw.Halen = sa.Halen
37 for i := 0; i < len(sa.Addr); i++ {
38 sa.raw.Addr[i] = sa.Addr[i]
40 return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), SizeofSockaddrLinklayer, nil
43 type SockaddrNetlink struct {
48 raw RawSockaddrNetlink
51 func (sa *SockaddrNetlink) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
52 sa.raw.Family = AF_NETLINK
55 sa.raw.Groups = sa.Groups
56 return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), SizeofSockaddrNetlink, nil
59 type RawSockaddrInet4 struct {
62 Addr [4]byte /* in_addr */
66 func (sa *RawSockaddrInet4) setLen() Socklen_t {
67 return SizeofSockaddrInet4
70 type RawSockaddrInet6 struct {
74 Addr [16]byte /* in6_addr */
78 func (sa *RawSockaddrInet6) setLen() Socklen_t {
79 return SizeofSockaddrInet6
82 type RawSockaddrUnix struct {
87 func (sa *RawSockaddrUnix) setLen(int) {
90 func (sa *RawSockaddrUnix) getLen() (int, error) {
92 // "Abstract" Unix domain socket.
93 // Rewrite leading NUL as @ for textual display.
94 // (This is the standard convention.)
95 // Not friendly to overwrite in place,
96 // but the callers below don't care.
100 // Assume path ends at NUL.
101 // This is not technically the GNU/Linux semantics for
102 // abstract Unix domain sockets--they are supposed
103 // to be uninterpreted fixed-size binary blobs--but
104 // everyone uses this convention.
106 for n < len(sa.Path) && sa.Path[n] != 0 {
113 type RawSockaddrLinklayer struct {
123 type RawSockaddrNetlink struct {
130 type RawSockaddr struct {
135 // BindToDevice binds the socket associated with fd to device.
136 func BindToDevice(fd int, device string) (err error) {
137 return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device)
140 func anyToSockaddrOS(rsa *RawSockaddrAny) (Sockaddr, error) {
141 switch rsa.Addr.Family {
143 pp := (*RawSockaddrNetlink)(unsafe.Pointer(rsa))
144 sa := new(SockaddrNetlink)
145 sa.Family = pp.Family
148 sa.Groups = pp.Groups
152 pp := (*RawSockaddrLinklayer)(unsafe.Pointer(rsa))
153 sa := new(SockaddrLinklayer)
154 sa.Protocol = pp.Protocol
155 sa.Ifindex = int(pp.Ifindex)
156 sa.Hatype = pp.Hatype
157 sa.Pkttype = pp.Pkttype
159 for i := 0; i < len(sa.Addr); i++ {
160 sa.Addr[i] = pp.Addr[i]
164 return nil, EAFNOSUPPORT
167 //sysnb EpollCreate(size int) (fd int, err error)
168 //epoll_create(size int) int
170 //sysnb EpollCreate1(flags int) (fd int, err error)
171 //epoll_create1(flags int) int
173 //sysnb EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error)
174 //epoll_ctl(epfd int, op int, fd int, event *EpollEvent) int
176 //sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
177 //epoll_wait(epfd int, events *EpollEvent, maxevents int, timeout int) int