1// socket_bsd.go -- Socket handling specific to *BSD based systems.
2
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.
6
7// +build darwin dragonfly freebsd hurd openbsd netbsd
8
9package syscall
10
11import "unsafe"
12
13const SizeofSockaddrInet4 = 16
14const SizeofSockaddrInet6 = 28
15const SizeofSockaddrUnix = 110
16
17type RawSockaddrInet4 struct {
18	Len    uint8
19	Family uint8
20	Port   uint16
21	Addr   [4]byte /* in_addr */
22	Zero   [8]uint8
23}
24
25func (sa *RawSockaddrInet4) setLen() Socklen_t {
26	sa.Len = SizeofSockaddrInet4
27	return SizeofSockaddrInet4
28}
29
30type RawSockaddrInet6 struct {
31	Len      uint8
32	Family   uint8
33	Port     uint16
34	Flowinfo uint32
35	Addr     [16]byte /* in6_addr */
36	Scope_id uint32
37}
38
39func (sa *RawSockaddrInet6) setLen() Socklen_t {
40	sa.Len = SizeofSockaddrInet6
41	return SizeofSockaddrInet6
42}
43
44type RawSockaddrUnix struct {
45	Len    uint8
46	Family uint8
47	Path   [108]int8
48}
49
50func (sa *RawSockaddrUnix) setLen(n int) {
51	sa.Len = uint8(3 + n) // 2 for Family, Len; 1 for NUL.
52}
53
54func (sa *RawSockaddrUnix) getLen() (int, error) {
55	if sa.Len < 3 || sa.Len > SizeofSockaddrUnix {
56		return 0, EINVAL
57	}
58	n := int(sa.Len) - 3 // subtract leading Family, Len, terminating NUL.
59	for i := 0; i < n; i++ {
60		if sa.Path[i] == 0 {
61			// found early NUL; assume Len is overestimating.
62			n = i
63			break
64		}
65	}
66	return n, nil
67}
68
69func (sa *RawSockaddrUnix) adjustAbstract(sl Socklen_t) Socklen_t {
70	return sl
71}
72
73type RawSockaddr struct {
74	Len    uint8
75	Family uint8
76	Data   [14]int8
77}
78
79// BindToDevice binds the socket associated with fd to device.
80func BindToDevice(fd int, device string) (err error) {
81	return ENOSYS
82}
83
84func anyToSockaddrOS(rsa *RawSockaddrAny) (Sockaddr, error) {
85	return nil, EAFNOSUPPORT
86}
87
88func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
89	var value IPv6MTUInfo
90	vallen := Socklen_t(SizeofIPv6MTUInfo)
91	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
92	return &value, err
93}
94