* gcc-interface/gigi.h (fill_vms_descriptor): Take GNU_TYPE instead of
[official-gcc.git] / libgo / go / net / dial.go
blob03b9d87be33d58e36096949978e21dc47355571a
1 // Copyright 2010 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.
5 package net
7 import "os"
9 // Dial connects to the remote address raddr on the network net.
10 // If the string laddr is not empty, it is used as the local address
11 // for the connection.
13 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
14 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
15 // (IPv4-only), "ip6" (IPv6-only), "unix" and "unixgram".
17 // For IP networks, addresses have the form host:port. If host is
18 // a literal IPv6 address, it must be enclosed in square brackets.
20 // Examples:
21 // Dial("tcp", "", "12.34.56.78:80")
22 // Dial("tcp", "", "google.com:80")
23 // Dial("tcp", "", "[de:ad:be:ef::ca:fe]:80")
24 // Dial("tcp", "127.0.0.1:123", "127.0.0.1:88")
26 func Dial(net, laddr, raddr string) (c Conn, err os.Error) {
27 switch prefixBefore(net, ':') {
28 case "tcp", "tcp4", "tcp6":
29 var la, ra *TCPAddr
30 if laddr != "" {
31 if la, err = ResolveTCPAddr(laddr); err != nil {
32 goto Error
35 if raddr != "" {
36 if ra, err = ResolveTCPAddr(raddr); err != nil {
37 goto Error
40 c, err := DialTCP(net, la, ra)
41 if err != nil {
42 return nil, err
44 return c, nil
45 case "udp", "udp4", "udp6":
46 var la, ra *UDPAddr
47 if laddr != "" {
48 if la, err = ResolveUDPAddr(laddr); err != nil {
49 goto Error
52 if raddr != "" {
53 if ra, err = ResolveUDPAddr(raddr); err != nil {
54 goto Error
57 c, err := DialUDP(net, la, ra)
58 if err != nil {
59 return nil, err
61 return c, nil
62 case "unix", "unixgram", "unixpacket":
63 var la, ra *UnixAddr
64 if raddr != "" {
65 if ra, err = ResolveUnixAddr(net, raddr); err != nil {
66 goto Error
69 if laddr != "" {
70 if la, err = ResolveUnixAddr(net, laddr); err != nil {
71 goto Error
74 c, err = DialUnix(net, la, ra)
75 if err != nil {
76 return nil, err
78 return c, nil
79 case "ip", "ip4", "ip6":
80 var la, ra *IPAddr
81 if laddr != "" {
82 if la, err = ResolveIPAddr(laddr); err != nil {
83 goto Error
86 if raddr != "" {
87 if ra, err = ResolveIPAddr(raddr); err != nil {
88 goto Error
91 c, err := DialIP(net, la, ra)
92 if err != nil {
93 return nil, err
95 return c, nil
98 err = UnknownNetworkError(net)
99 Error:
100 return nil, &OpError{"dial", net + " " + raddr, nil, err}
103 // Listen announces on the local network address laddr.
104 // The network string net must be a stream-oriented
105 // network: "tcp", "tcp4", "tcp6", or "unix", or "unixpacket".
106 func Listen(net, laddr string) (l Listener, err os.Error) {
107 switch net {
108 case "tcp", "tcp4", "tcp6":
109 var la *TCPAddr
110 if laddr != "" {
111 if la, err = ResolveTCPAddr(laddr); err != nil {
112 return nil, err
115 l, err := ListenTCP(net, la)
116 if err != nil {
117 return nil, err
119 return l, nil
120 case "unix", "unixpacket":
121 var la *UnixAddr
122 if laddr != "" {
123 if la, err = ResolveUnixAddr(net, laddr); err != nil {
124 return nil, err
127 l, err := ListenUnix(net, la)
128 if err != nil {
129 return nil, err
131 return l, nil
133 return nil, UnknownNetworkError(net)
136 // ListenPacket announces on the local network address laddr.
137 // The network string net must be a packet-oriented network:
138 // "udp", "udp4", "udp6", or "unixgram".
139 func ListenPacket(net, laddr string) (c PacketConn, err os.Error) {
140 switch prefixBefore(net, ':') {
141 case "udp", "udp4", "udp6":
142 var la *UDPAddr
143 if laddr != "" {
144 if la, err = ResolveUDPAddr(laddr); err != nil {
145 return nil, err
148 c, err := ListenUDP(net, la)
149 if err != nil {
150 return nil, err
152 return c, nil
153 case "unixgram":
154 var la *UnixAddr
155 if laddr != "" {
156 if la, err = ResolveUnixAddr(net, laddr); err != nil {
157 return nil, err
160 c, err := DialUnix(net, la, nil)
161 if err != nil {
162 return nil, err
164 return c, nil
165 case "ip", "ip4", "ip6":
166 var la *IPAddr
167 if laddr != "" {
168 if la, err = ResolveIPAddr(laddr); err != nil {
169 return nil, err
172 c, err := ListenIP(net, la)
173 if err != nil {
174 return nil, err
176 return c, nil
178 return nil, UnknownNetworkError(net)