1// socket.go -- Socket handling.
2
3// Copyright 2009 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.
6
7// Low-level socket interface.
8// Only for implementing net package.
9// DO NOT USE DIRECTLY.
10
11package syscall
12
13import "unsafe"
14
15// For testing: clients can set this flag to force
16// creation of IPv6 sockets to return EAFNOSUPPORT.
17var SocketDisableIPv6 bool
18
19type Sockaddr interface {
20	sockaddr() (ptr *RawSockaddrAny, len Socklen_t, err error) // lowercase; only we can define Sockaddrs
21}
22
23type RawSockaddrAny struct {
24	Addr RawSockaddr
25	Pad  [96]int8
26}
27
28const SizeofSockaddrAny = 0x6c
29
30type SockaddrInet4 struct {
31	Port int
32	Addr [4]byte
33	raw  RawSockaddrInet4
34}
35
36func (sa *SockaddrInet4) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
37	if sa.Port < 0 || sa.Port > 0xFFFF {
38		return nil, 0, EINVAL
39	}
40	sa.raw.Family = AF_INET
41	n := sa.raw.setLen()
42	p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
43	p[0] = byte(sa.Port >> 8)
44	p[1] = byte(sa.Port)
45	for i := 0; i < len(sa.Addr); i++ {
46		sa.raw.Addr[i] = sa.Addr[i]
47	}
48	return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), n, nil
49}
50
51type SockaddrInet6 struct {
52	Port   int
53	ZoneId uint32
54	Addr   [16]byte
55	raw    RawSockaddrInet6
56}
57
58func (sa *SockaddrInet6) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
59	if sa.Port < 0 || sa.Port > 0xFFFF {
60		return nil, 0, EINVAL
61	}
62	sa.raw.Family = AF_INET6
63	n := sa.raw.setLen()
64	p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
65	p[0] = byte(sa.Port >> 8)
66	p[1] = byte(sa.Port)
67	sa.raw.Scope_id = sa.ZoneId
68	for i := 0; i < len(sa.Addr); i++ {
69		sa.raw.Addr[i] = sa.Addr[i]
70	}
71	return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), n, nil
72}
73
74type SockaddrUnix struct {
75	Name string
76	raw  RawSockaddrUnix
77}
78
79func (sa *SockaddrUnix) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
80	name := sa.Name
81	n := len(name)
82	if n > len(sa.raw.Path) {
83		return nil, 0, EINVAL
84	}
85	sa.raw.Family = AF_UNIX
86	sa.raw.setLen(n)
87	for i := 0; i < n; i++ {
88		sa.raw.Path[i] = int8(name[i])
89	}
90	// length is family (uint16), name, NUL.
91	sl := Socklen_t(2)
92	if n > 0 {
93		sl += Socklen_t(n) + 1
94	}
95	sl = sa.raw.adjustAbstract(sl)
96	// Check again after adjustAbstract adjusts the length.
97	// This is testing whether the +1 for NUL puts us out of range.
98	if sl-2 > Socklen_t(len(sa.raw.Path)) {
99		return nil, 0, EINVAL
100	}
101
102	// length is family (uint16), name, NUL.
103	return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), sl, nil
104}
105
106func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
107	switch rsa.Addr.Family {
108	case AF_UNIX:
109		pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
110		sa := new(SockaddrUnix)
111		n, err := pp.getLen()
112		if err != nil {
113			return nil, err
114		}
115		bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))
116		sa.Name = string(bytes[0:n])
117		return sa, nil
118
119	case AF_INET:
120		pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
121		sa := new(SockaddrInet4)
122		p := (*[2]byte)(unsafe.Pointer(&pp.Port))
123		sa.Port = int(p[0])<<8 + int(p[1])
124		for i := 0; i < len(sa.Addr); i++ {
125			sa.Addr[i] = pp.Addr[i]
126		}
127		return sa, nil
128
129	case AF_INET6:
130		pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
131		sa := new(SockaddrInet6)
132		p := (*[2]byte)(unsafe.Pointer(&pp.Port))
133		sa.Port = int(p[0])<<8 + int(p[1])
134		for i := 0; i < len(sa.Addr); i++ {
135			sa.Addr[i] = pp.Addr[i]
136		}
137		return sa, nil
138	}
139	return anyToSockaddrOS(rsa)
140}
141
142//sys	accept(fd int, sa *RawSockaddrAny, len *Socklen_t) (nfd int, err error)
143//accept(fd _C_int, sa *RawSockaddrAny, len *Socklen_t) _C_int
144
145func Accept(fd int) (nfd int, sa Sockaddr, err error) {
146	var rsa RawSockaddrAny
147	var len Socklen_t = SizeofSockaddrAny
148	nfd, err = accept(fd, &rsa, &len)
149	if err != nil {
150		return
151	}
152	sa, err = anyToSockaddr(&rsa)
153	if err != nil {
154		Close(nfd)
155		nfd = 0
156	}
157	return
158}
159
160//sysnb	getsockname(fd int, sa *RawSockaddrAny, len *Socklen_t) (err error)
161//getsockname(fd _C_int, sa *RawSockaddrAny, len *Socklen_t) _C_int
162
163func Getsockname(fd int) (sa Sockaddr, err error) {
164	var rsa RawSockaddrAny
165	var len Socklen_t = SizeofSockaddrAny
166	if err = getsockname(fd, &rsa, &len); err != nil {
167		return
168	}
169	return anyToSockaddr(&rsa)
170}
171
172//sysnb getpeername(fd int, sa *RawSockaddrAny, len *Socklen_t) (err error)
173//getpeername(fd _C_int, sa *RawSockaddrAny, len *Socklen_t) _C_int
174
175func Getpeername(fd int) (sa Sockaddr, err error) {
176	var rsa RawSockaddrAny
177	var len Socklen_t = SizeofSockaddrAny
178	if err = getpeername(fd, &rsa, &len); err != nil {
179		return
180	}
181	return anyToSockaddr(&rsa)
182}
183
184func Bind(fd int, sa Sockaddr) (err error) {
185	ptr, n, err := sa.sockaddr()
186	if err != nil {
187		return err
188	}
189	return bind(fd, ptr, n)
190}
191
192func Connect(fd int, sa Sockaddr) (err error) {
193	ptr, n, err := sa.sockaddr()
194	if err != nil {
195		return err
196	}
197	return connect(fd, ptr, n)
198}
199
200func Socket(domain, typ, proto int) (fd int, err error) {
201	if domain == AF_INET6 && SocketDisableIPv6 {
202		return -1, EAFNOSUPPORT
203	}
204	fd, err = socket(domain, typ, proto)
205	return
206}
207
208func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
209	var fdx [2]_C_int
210	err = socketpair(domain, typ, proto, &fdx)
211	if err == nil {
212		fd[0] = int(fdx[0])
213		fd[1] = int(fdx[1])
214	}
215	return
216}
217
218func GetsockoptByte(fd, level, opt int) (value byte, err error) {
219	var n byte
220	vallen := Socklen_t(1)
221	err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
222	return n, err
223}
224
225func GetsockoptInt(fd, level, opt int) (value int, err error) {
226	var n int32
227	vallen := Socklen_t(4)
228	err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
229	return int(n), err
230}
231
232func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
233	vallen := Socklen_t(4)
234	err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
235	return value, err
236}
237
238func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
239	var value IPMreq
240	vallen := Socklen_t(SizeofIPMreq)
241	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
242	return &value, err
243}
244
245func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) {
246	var value IPMreqn
247	vallen := Socklen_t(SizeofIPMreqn)
248	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
249	return &value, err
250}
251
252func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
253	var value IPv6Mreq
254	vallen := Socklen_t(SizeofIPv6Mreq)
255	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
256	return &value, err
257}
258
259func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
260	var value ICMPv6Filter
261	vallen := Socklen_t(SizeofICMPv6Filter)
262	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
263	return &value, err
264}
265
266//sys	setsockopt(s int, level int, name int, val unsafe.Pointer, vallen Socklen_t) (err error)
267//setsockopt(s _C_int, level _C_int, optname _C_int, val *byte, vallen Socklen_t) _C_int
268
269func SetsockoptByte(fd, level, opt int, value byte) (err error) {
270	var n = byte(value)
271	return setsockopt(fd, level, opt, unsafe.Pointer(&n), 1)
272}
273
274func SetsockoptInt(fd, level, opt int, value int) (err error) {
275	var n = int32(value)
276	return setsockopt(fd, level, opt, unsafe.Pointer(&n), 4)
277}
278
279func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) {
280	return setsockopt(fd, level, opt, unsafe.Pointer(&value[0]), 4)
281}
282
283func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) {
284	return setsockopt(fd, level, opt, unsafe.Pointer(tv), Socklen_t(unsafe.Sizeof(*tv)))
285}
286
287func SetsockoptICMPv6Filter(fd, level, opt int, filter *ICMPv6Filter) error {
288	return setsockopt(fd, level, opt, unsafe.Pointer(filter), SizeofICMPv6Filter)
289}
290
291type Linger struct {
292	Onoff  int32
293	Linger int32
294}
295
296func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) {
297	return setsockopt(fd, level, opt, unsafe.Pointer(l), Socklen_t(unsafe.Sizeof(*l)))
298}
299
300func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) {
301	return setsockopt(fd, level, opt, unsafe.Pointer(mreq), Socklen_t(unsafe.Sizeof(*mreq)))
302}
303
304func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) {
305	return setsockopt(fd, level, opt, unsafe.Pointer(mreq), Socklen_t(unsafe.Sizeof(*mreq)))
306}
307
308func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) {
309	return setsockopt(fd, level, opt, unsafe.Pointer(mreq), Socklen_t(unsafe.Sizeof(*mreq)))
310}
311
312func SetsockoptString(fd, level, opt int, s string) (err error) {
313	var p unsafe.Pointer
314	if len(s) > 0 {
315		p = unsafe.Pointer(&[]byte(s)[0])
316	}
317	return setsockopt(fd, level, opt, p, Socklen_t(len(s)))
318}
319
320//sys	recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *Socklen_t) (n int, err error)
321//recvfrom(fd _C_int, buf *byte, len Size_t, flags _C_int, from *RawSockaddrAny, fromlen *Socklen_t) Ssize_t
322
323func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
324	var rsa RawSockaddrAny
325	var len Socklen_t = SizeofSockaddrAny
326	if n, err = recvfrom(fd, p, flags, &rsa, &len); err != nil {
327		return
328	}
329	if rsa.Addr.Family != AF_UNSPEC {
330		from, err = anyToSockaddr(&rsa)
331	}
332	return
333}
334
335func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
336	ptr, n, err := to.sockaddr()
337	if err != nil {
338		return err
339	}
340	return sendto(fd, p, flags, ptr, n)
341}
342
343func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
344	var msg Msghdr
345	var rsa RawSockaddrAny
346	msg.Name = (*byte)(unsafe.Pointer(&rsa))
347	msg.Namelen = uint32(SizeofSockaddrAny)
348	var iov Iovec
349	if len(p) > 0 {
350		iov.Base = (*byte)(unsafe.Pointer(&p[0]))
351		iov.SetLen(len(p))
352	}
353	var dummy byte
354	if len(oob) > 0 {
355		var sockType int
356		sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE)
357		if err != nil {
358			return
359		}
360		// receive at least one normal byte
361		if sockType != SOCK_DGRAM && len(p) == 0 {
362			iov.Base = &dummy
363			iov.SetLen(1)
364		}
365		msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
366		msg.SetControllen(len(oob))
367	}
368	msg.Iov = &iov
369	msg.Iovlen = 1
370	if n, err = recvmsg(fd, &msg, flags); err != nil {
371		return
372	}
373	oobn = int(msg.Controllen)
374	recvflags = int(msg.Flags)
375	// source address is only specified if the socket is unconnected
376	if rsa.Addr.Family != AF_UNSPEC {
377		from, err = anyToSockaddr(&rsa)
378	}
379	return
380}
381
382func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
383	_, err = SendmsgN(fd, p, oob, to, flags)
384	return
385}
386
387func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
388	var ptr *RawSockaddrAny
389	var salen Socklen_t
390	if to != nil {
391		var err error
392		ptr, salen, err = to.sockaddr()
393		if err != nil {
394			return 0, err
395		}
396	}
397	var msg Msghdr
398	msg.Name = (*byte)(unsafe.Pointer(ptr))
399	msg.Namelen = uint32(salen)
400	var iov Iovec
401	if len(p) > 0 {
402		iov.Base = (*byte)(unsafe.Pointer(&p[0]))
403		iov.SetLen(len(p))
404	}
405	var dummy byte
406	if len(oob) > 0 {
407		var sockType int
408		sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE)
409		if err != nil {
410			return 0, err
411		}
412		// send at least one normal byte
413		if sockType != SOCK_DGRAM && len(p) == 0 {
414			iov.Base = &dummy
415			iov.SetLen(1)
416		}
417		msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
418		msg.SetControllen(len(oob))
419	}
420	msg.Iov = &iov
421	msg.Iovlen = 1
422	if n, err = sendmsg(fd, &msg, flags); err != nil {
423		return 0, err
424	}
425	if len(oob) > 0 && len(p) == 0 {
426		n = 0
427	}
428	return n, nil
429}
430
431//sys	Listen(fd int, n int) (err error)
432//listen(fd _C_int, n _C_int) _C_int
433
434//sys	Shutdown(fd int, how int) (err error)
435//shutdown(fd _C_int, how _C_int) _C_int
436
437func (iov *Iovec) SetLen(length int) {
438	iov.Len = Iovec_len_t(length)
439}
440
441func (msghdr *Msghdr) SetControllen(length int) {
442	msghdr.Controllen = Msghdr_controllen_t(length)
443}
444
445func (cmsg *Cmsghdr) SetLen(length int) {
446	cmsg.Len = Cmsghdr_len_t(length)
447}
448