OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [ipsock_posix.go] - Blame information for rev 761

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors.  All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// +build darwin freebsd linux netbsd openbsd windows
6
 
7
package net
8
 
9
import "syscall"
10
 
11
// Should we try to use the IPv4 socket interface if we're
12
// only dealing with IPv4 sockets?  As long as the host system
13
// understands IPv6, it's okay to pass IPv4 addresses to the IPv6
14
// interface.  That simplifies our code and is most general.
15
// Unfortunately, we need to run on kernels built without IPv6
16
// support too.  So probe the kernel to figure it out.
17
//
18
// probeIPv6Stack probes both basic IPv6 capability and IPv6 IPv4-
19
// mapping capability which is controlled by IPV6_V6ONLY socket
20
// option and/or kernel state "net.inet6.ip6.v6only".
21
// It returns two boolean values.  If the first boolean value is
22
// true, kernel supports basic IPv6 functionality.  If the second
23
// boolean value is true, kernel supports IPv6 IPv4-mapping.
24
func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
25
        var probes = []struct {
26
                la TCPAddr
27
                ok bool
28
        }{
29
                // IPv6 communication capability
30
                {TCPAddr{IP: ParseIP("::1")}, false},
31
                // IPv6 IPv4-mapped address communication capability
32
                {TCPAddr{IP: IPv4(127, 0, 0, 1)}, false},
33
        }
34
 
35
        for i := range probes {
36
                s, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
37
                if err != nil {
38
                        continue
39
                }
40
                defer closesocket(s)
41
                sa, err := probes[i].la.toAddr().sockaddr(syscall.AF_INET6)
42
                if err != nil {
43
                        continue
44
                }
45
                err = syscall.Bind(s, sa)
46
                if err != nil {
47
                        continue
48
                }
49
                probes[i].ok = true
50
        }
51
 
52
        return probes[0].ok, probes[1].ok
53
}
54
 
55
// favoriteAddrFamily returns the appropriate address family to
56
// the given net, raddr, laddr and mode.  At first it figures
57
// address family out from the net.  If mode indicates "listen"
58
// and laddr.(type).IP is nil, it assumes that the user wants to
59
// make a passive connection with wildcard address family, both
60
// INET and INET6, and wildcard address.  Otherwise guess: if the
61
// addresses are IPv4 then returns INET, or else returns INET6.
62
func favoriteAddrFamily(net string, raddr, laddr sockaddr, mode string) int {
63
        switch net[len(net)-1] {
64
        case '4':
65
                return syscall.AF_INET
66
        case '6':
67
                return syscall.AF_INET6
68
        }
69
 
70
        if mode == "listen" {
71
                switch a := laddr.(type) {
72
                case *TCPAddr:
73
                        if a.IP == nil && supportsIPv6 {
74
                                return syscall.AF_INET6
75
                        }
76
                case *UDPAddr:
77
                        if a.IP == nil && supportsIPv6 {
78
                                return syscall.AF_INET6
79
                        }
80
                case *IPAddr:
81
                        if a.IP == nil && supportsIPv6 {
82
                                return syscall.AF_INET6
83
                        }
84
                }
85
        }
86
 
87
        if (laddr == nil || laddr.family() == syscall.AF_INET) &&
88
                (raddr == nil || raddr.family() == syscall.AF_INET) {
89
                return syscall.AF_INET
90
        }
91
        return syscall.AF_INET6
92
}
93
 
94
// Internet sockets (TCP, UDP)
95
 
96
// A sockaddr represents a TCP or UDP network address that can
97
// be converted into a syscall.Sockaddr.
98
type sockaddr interface {
99
        Addr
100
        sockaddr(family int) (syscall.Sockaddr, error)
101
        family() int
102
}
103
 
104
func internetSocket(net string, laddr, raddr sockaddr, sotype, proto int, mode string, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err error) {
105
        var oserr error
106
        var la, ra syscall.Sockaddr
107
        family := favoriteAddrFamily(net, raddr, laddr, mode)
108
        if laddr != nil {
109
                if la, oserr = laddr.sockaddr(family); oserr != nil {
110
                        goto Error
111
                }
112
        }
113
        if raddr != nil {
114
                if ra, oserr = raddr.sockaddr(family); oserr != nil {
115
                        goto Error
116
                }
117
        }
118
        fd, oserr = socket(net, family, sotype, proto, la, ra, toAddr)
119
        if oserr != nil {
120
                goto Error
121
        }
122
        return fd, nil
123
 
124
Error:
125
        addr := raddr
126
        if mode == "listen" {
127
                addr = laddr
128
        }
129
        return nil, &OpError{mode, net, addr, oserr}
130
}
131
 
132
func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, error) {
133
        switch family {
134
        case syscall.AF_INET:
135
                if len(ip) == 0 {
136
                        ip = IPv4zero
137
                }
138
                if ip = ip.To4(); ip == nil {
139
                        return nil, InvalidAddrError("non-IPv4 address")
140
                }
141
                s := new(syscall.SockaddrInet4)
142
                for i := 0; i < IPv4len; i++ {
143
                        s.Addr[i] = ip[i]
144
                }
145
                s.Port = port
146
                return s, nil
147
        case syscall.AF_INET6:
148
                if len(ip) == 0 {
149
                        ip = IPv6zero
150
                }
151
                // IPv4 callers use 0.0.0.0 to mean "announce on any available address".
152
                // In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0",
153
                // which it refuses to do.  Rewrite to the IPv6 all zeros.
154
                if ip.Equal(IPv4zero) {
155
                        ip = IPv6zero
156
                }
157
                if ip = ip.To16(); ip == nil {
158
                        return nil, InvalidAddrError("non-IPv6 address")
159
                }
160
                s := new(syscall.SockaddrInet6)
161
                for i := 0; i < IPv6len; i++ {
162
                        s.Addr[i] = ip[i]
163
                }
164
                s.Port = port
165
                return s, nil
166
        }
167
        return nil, InvalidAddrError("unexpected socket family")
168
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.