aboutsummaryrefslogtreecommitdiff
path: root/src/lib/net/net_linux.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2008-09-26 14:11:26 -0700
committerRuss Cox <rsc@golang.org>2008-09-26 14:11:26 -0700
commit6201a963f1ea19e1473dc13f4d5f397a9eac543a (patch)
tree26c67a72dec44faba761ffd9fac52a927ec58ffe /src/lib/net/net_linux.go
parent2662aad7b788cd0bda630d45f659dc12211adc8a (diff)
downloadgo-6201a963f1ea19e1473dc13f4d5f397a9eac543a.tar.xz
move src/syscall to src/lib/syscall.
enforce rule: all kernel data structures and constants go in syscall module. move things that should be in syscall out of net. make net a single package. R=r OCL=15985 CL=15994
Diffstat (limited to 'src/lib/net/net_linux.go')
-rw-r--r--src/lib/net/net_linux.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/lib/net/net_linux.go b/src/lib/net/net_linux.go
new file mode 100644
index 0000000000..bc0b8f6d3a
--- /dev/null
+++ b/src/lib/net/net_linux.go
@@ -0,0 +1,70 @@
+// 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 (
+ "os";
+ "syscall";
+ "net"
+)
+
+export func IPv4ToSockaddr(p *[]byte, port int) (sa1 *syscall.Sockaddr, err *os.Error) {
+ p = ToIPv4(p)
+ 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.SockaddrInet4ToSockaddr(sa), nil
+}
+
+export func IPv6ToSockaddr(p *[]byte, port int) (sa1 *syscall.Sockaddr, err *os.Error) {
+ p = ToIPv6(p)
+ if p == nil || port < 0 || port > 0xFFFF {
+ return nil, os.EINVAL
+ }
+ 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.SockaddrInet6ToSockaddr(sa), nil
+}
+
+export func SockaddrToIP(sa1 *syscall.Sockaddr) (p *[]byte, port int, err *os.Error) {
+ switch sa1.family {
+ case syscall.AF_INET:
+ sa := syscall.SockaddrToSockaddrInet4(sa1);
+ a := ToIPv6(&sa.addr)
+ 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.SockaddrToSockaddrInet6(sa1);
+ a := ToIPv6(&sa.addr)
+ 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
+}
+
+export 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
+}
+