2009-11-01 12:15:34 -07:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
// IP sockets
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os";
|
|
|
|
"syscall";
|
|
|
|
)
|
|
|
|
|
|
|
|
// Should we try to use the IPv4 socket interface if we're
|
|
|
|
// only dealing with IPv4 sockets? As long as the host system
|
|
|
|
// understands IPv6, it's okay to pass IPv4 addresses to the IPv6
|
|
|
|
// interface. That simplifies our code and is most general.
|
|
|
|
// Unfortunately, we need to run on kernels built without IPv6 support too.
|
|
|
|
// So probe the kernel to figure it out.
|
|
|
|
func kernelSupportsIPv6() bool {
|
|
|
|
fd, e := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP);
|
|
|
|
if fd >= 0 {
|
2009-11-05 00:16:46 -07:00
|
|
|
syscall.Close(fd);
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-11-05 00:16:46 -07:00
|
|
|
return e == 0;
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var preferIPv4 = !kernelSupportsIPv6()
|
|
|
|
|
|
|
|
// TODO(rsc): if syscall.OS == "linux", we're supposd to read
|
|
|
|
// /proc/sys/net/core/somaxconn,
|
|
|
|
// to take advantage of kernels that have raised the limit.
|
2009-11-06 15:24:38 -07:00
|
|
|
func listenBacklog() int { return syscall.SOMAXCONN }
|
2009-11-01 12:15:34 -07:00
|
|
|
|
|
|
|
// Internet sockets (TCP, UDP)
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// A sockaddr represents a TCP or UDP network address that can
|
|
|
|
// be converted into a syscall.Sockaddr.
|
|
|
|
type sockaddr interface {
|
|
|
|
Addr;
|
|
|
|
sockaddr(family int) (syscall.Sockaddr, os.Error);
|
|
|
|
family() int;
|
|
|
|
}
|
2009-11-01 12:15:34 -07:00
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
func internetSocket(net string, laddr, raddr sockaddr, proto int, mode string, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err os.Error) {
|
2009-11-01 12:15:34 -07:00
|
|
|
// Figure out IP version.
|
|
|
|
// If network has a suffix like "tcp4", obey it.
|
2009-11-02 19:37:30 -07:00
|
|
|
family := syscall.AF_INET6;
|
2009-11-01 12:15:34 -07:00
|
|
|
switch net[len(net)-1] {
|
|
|
|
case '4':
|
2009-11-05 00:16:46 -07:00
|
|
|
family = syscall.AF_INET;
|
2009-11-01 12:15:34 -07:00
|
|
|
case '6':
|
2009-11-02 19:37:30 -07:00
|
|
|
// nothing to do
|
2009-11-01 12:15:34 -07:00
|
|
|
default:
|
|
|
|
// Otherwise, guess.
|
|
|
|
// If the addresses are IPv4 and we prefer IPv4, use 4; else 6.
|
2009-11-02 19:37:30 -07:00
|
|
|
if preferIPv4 &&
|
|
|
|
(laddr == nil || laddr.family() == syscall.AF_INET) &&
|
|
|
|
(raddr == nil || raddr.family() == syscall.AF_INET) {
|
|
|
|
family = syscall.AF_INET;
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var la, ra syscall.Sockaddr;
|
2009-11-02 19:37:30 -07:00
|
|
|
if laddr != nil {
|
|
|
|
if la, err = laddr.sockaddr(family); err != nil {
|
2009-11-01 12:15:34 -07:00
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
if raddr != nil {
|
|
|
|
if ra, err = raddr.sockaddr(family); err != nil {
|
2009-11-01 12:15:34 -07:00
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
fd, err = socket(net, family, proto, 0, la, ra, toAddr);
|
2009-11-01 12:15:34 -07:00
|
|
|
if err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
return fd, nil;
|
|
|
|
|
|
|
|
Error:
|
|
|
|
addr := raddr;
|
|
|
|
if mode == "listen" {
|
|
|
|
addr = laddr;
|
|
|
|
}
|
|
|
|
return nil, &OpError{mode, net, addr, err};
|
|
|
|
}
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
func getip(fd int, remote bool) (ip []byte, port int, ok bool) {
|
|
|
|
// No attempt at error reporting because
|
|
|
|
// there are no possible errors, and the
|
|
|
|
// caller won't report them anyway.
|
|
|
|
var sa syscall.Sockaddr;
|
|
|
|
if remote {
|
|
|
|
sa, _ = syscall.Getpeername(fd);
|
|
|
|
} else {
|
|
|
|
sa, _ = syscall.Getsockname(fd);
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
|
|
|
return &sa.Addr, sa.Port, true;
|
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
return &sa.Addr, sa.Port, true;
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
return;
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, os.Error) {
|
|
|
|
switch family {
|
|
|
|
case syscall.AF_INET:
|
2009-11-02 19:37:30 -07:00
|
|
|
if len(ip) == 0 {
|
|
|
|
ip = IPv4zero;
|
|
|
|
}
|
2009-11-01 12:15:34 -07:00
|
|
|
if ip = ip.To4(); ip == nil {
|
2009-11-05 00:16:46 -07:00
|
|
|
return nil, os.EINVAL;
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
s := new(syscall.SockaddrInet4);
|
|
|
|
for i := 0; i < IPv4len; i++ {
|
|
|
|
s.Addr[i] = ip[i];
|
|
|
|
}
|
|
|
|
s.Port = port;
|
|
|
|
return s, nil;
|
|
|
|
case syscall.AF_INET6:
|
2009-11-02 19:37:30 -07:00
|
|
|
if len(ip) == 0 {
|
|
|
|
ip = IPzero;
|
|
|
|
}
|
2009-11-01 12:15:34 -07:00
|
|
|
// 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 := ip.To4(); p4 != nil && p4[0] == 0 && p4[1] == 0 && p4[2] == 0 && p4[3] == 0 {
|
|
|
|
ip = IPzero;
|
|
|
|
}
|
|
|
|
if ip = ip.To16(); ip == nil {
|
2009-11-05 00:16:46 -07:00
|
|
|
return nil, os.EINVAL;
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
s := new(syscall.SockaddrInet6);
|
|
|
|
for i := 0; i < IPv6len; i++ {
|
|
|
|
s.Addr[i] = ip[i];
|
|
|
|
}
|
|
|
|
s.Port = port;
|
|
|
|
return s, nil;
|
|
|
|
}
|
|
|
|
return nil, os.EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split "host:port" into "host" and "port".
|
|
|
|
// Host cannot contain colons unless it is bracketed.
|
|
|
|
func splitHostPort(hostport string) (host, port string, err os.Error) {
|
|
|
|
// The port starts after the last colon.
|
|
|
|
i := last(hostport, ':');
|
|
|
|
if i < 0 {
|
|
|
|
err = &AddrError{"missing port in address", hostport};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-05 00:16:46 -07:00
|
|
|
host, port = hostport[0:i], hostport[i+1 : len(hostport)];
|
2009-11-01 12:15:34 -07:00
|
|
|
|
|
|
|
// Can put brackets around host ...
|
|
|
|
if len(host) > 0 && host[0] == '[' && host[len(host)-1] == ']' {
|
2009-11-05 00:16:46 -07:00
|
|
|
host = host[1 : len(host)-1];
|
2009-11-01 12:15:34 -07:00
|
|
|
} else {
|
|
|
|
// ... but if there are no brackets, no colons.
|
|
|
|
if byteIndex(host, ':') >= 0 {
|
|
|
|
err = &AddrError{"too many colons in address", hostport};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join "host" and "port" into "host:port".
|
|
|
|
// If host contains colons, will join into "[host]:port".
|
|
|
|
func joinHostPort(host, port string) string {
|
|
|
|
// If host has colons, have to bracket it.
|
|
|
|
if byteIndex(host, ':') >= 0 {
|
2009-11-05 00:16:46 -07:00
|
|
|
return "["+host+"]:"+port;
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-11-05 00:16:46 -07:00
|
|
|
return host+":"+port;
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert "host:port" into IP address and port.
|
2009-11-02 19:37:30 -07:00
|
|
|
func hostPortToIP(net, hostport string) (ip IP, iport int, err os.Error) {
|
2009-11-01 12:15:34 -07:00
|
|
|
host, port, err := splitHostPort(hostport);
|
|
|
|
if err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
var addr IP;
|
2009-11-02 19:37:30 -07:00
|
|
|
if host != "" {
|
|
|
|
// Try as an IP address.
|
2009-11-01 12:15:34 -07:00
|
|
|
addr = ParseIP(host);
|
|
|
|
if addr == nil {
|
2009-11-02 19:37:30 -07:00
|
|
|
// Not an IP address. Try as a DNS name.
|
|
|
|
_, addrs, err1 := LookupHost(host);
|
|
|
|
if err1 != nil {
|
|
|
|
err = err1;
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
addr = ParseIP(addrs[0]);
|
|
|
|
if addr == nil {
|
|
|
|
// should not happen
|
|
|
|
err = &AddrError{"LookupHost returned invalid address", addrs[0]};
|
|
|
|
goto Error;
|
|
|
|
}
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p, i, ok := dtoi(port, 0);
|
|
|
|
if !ok || i != len(port) {
|
|
|
|
p, err = LookupPort(net, port);
|
|
|
|
if err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p < 0 || p > 0xFFFF {
|
|
|
|
err = &AddrError{"invalid port", port};
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr, p, nil;
|
|
|
|
|
|
|
|
Error:
|
|
|
|
return nil, 0, err;
|
|
|
|
}
|