1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
// Copyright 2009 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 net
import (
"net";
"os";
"syscall";
"unsafe";
)
func v4ToSockaddr(p IP, port int) (sa1 *syscall.Sockaddr, err os.Error) {
p = p.To4();
if p == nil || port < 0 || port > 0xFFFF {
return nil, os.EINVAL
}
sa := new(syscall.SockaddrInet4);
sa.Family = syscall.AF_INET;
sa.Port[0] = byte(port>>8);
sa.Port[1] = byte(port);
for i := 0; i < IPv4len; i++ {
sa.Addr[i] = p[i]
}
return (*syscall.Sockaddr)(unsafe.Pointer(sa)), nil
}
func v6ToSockaddr(p IP, port int) (sa1 *syscall.Sockaddr, err os.Error) {
p = p.To16();
if p == nil || port < 0 || port > 0xFFFF {
return nil, os.EINVAL
}
// IPv4 callers use 0.0.0.0 to mean "announce on any available address".
// In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0",
// which it refuses to do. Rewrite to the IPv6 all zeros.
if p4 := p.To4(); p4 != nil && p4[0] == 0 && p4[1] == 0 && p4[2] == 0 && p4[3] == 0 {
p = IPzero;
}
sa := new(syscall.SockaddrInet6);
sa.Family = syscall.AF_INET6;
sa.Port[0] = byte(port>>8);
sa.Port[1] = byte(port);
for i := 0; i < IPv6len; i++ {
sa.Addr[i] = p[i]
}
return (*syscall.Sockaddr)(unsafe.Pointer(sa)), nil
}
func sockaddrToIP(sa1 *syscall.Sockaddr) (p IP, port int, err os.Error) {
switch sa1.Family {
case syscall.AF_INET:
sa := (*syscall.SockaddrInet4)(unsafe.Pointer(sa1));
a := IP(&sa.Addr).To16();
if a == nil {
return nil, 0, os.EINVAL
}
return a, int(sa.Port[0])<<8 + int(sa.Port[1]), nil;
case syscall.AF_INET6:
sa := (*syscall.SockaddrInet6)(unsafe.Pointer(sa1));
a := IP(&sa.Addr).To16();
if a == nil {
return nil, 0, os.EINVAL
}
return a, int(sa.Port[0])<<8 + int(sa.Port[1]), nil;
default:
return nil, 0, os.EINVAL
}
return nil, 0, nil // not reached
}
func listenBacklog() int64 {
// TODO: Read the limit from /proc/sys/net/core/somaxconn,
// to take advantage of kernels that have raised the limit.
return syscall.SOMAXCONN
}
func unixToSockaddr(name string) (sa1 *syscall.Sockaddr, err os.Error) {
sa := new(syscall.SockaddrUnix);
n := len(name);
if n >= len(sa.Path) || n == 0 {
return nil, os.EINVAL;
}
sa.Family = syscall.AF_UNIX;
for i := 0; i < len(name); i++ {
sa.Path[i] = name[i];
}
// Special case: @ in first position indicates
// an abstract socket, which has no file system
// representation and starts with a NUL byte
// when talking to the kernel about it.
if sa.Path[0] == '@' {
sa.Path[0] = 0;
}
sa.Length = 1 + int64(n) + 1; // family, name, \0
return (*syscall.Sockaddr)(unsafe.Pointer(sa)), nil;
}
func sockaddrToUnix(sa1 *syscall.Sockaddr) (string, os.Error) {
if sa1.Family != syscall.AF_UNIX {
return "", os.EINVAL;
}
sa := (*syscall.SockaddrUnix)(unsafe.Pointer(sa1));
// @ special case (see comment in unixToSockaddr).
if sa.Path[0] == 0 {
// Not friendly to overwrite in place but
// okay in an internal function.
// The caller doesn't care if we do.
sa.Path[0] = '@';
}
// count length of path
n := 0;
for n < len(sa.Path) && sa.Path[n] != 0 {
n++;
}
return string(sa.Path[0:n]), nil;
}
|