github.com/rakyll/go@v0.0.0-20170216000551-64c02460d703/src/net/net.go (about) 1 // 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 /* 6 Package net provides a portable interface for network I/O, including 7 TCP/IP, UDP, domain name resolution, and Unix domain sockets. 8 9 Although the package provides access to low-level networking 10 primitives, most clients will need only the basic interface provided 11 by the Dial, Listen, and Accept functions and the associated 12 Conn and Listener interfaces. The crypto/tls package uses 13 the same interfaces and similar Dial and Listen functions. 14 15 The Dial function connects to a server: 16 17 conn, err := net.Dial("tcp", "golang.org:80") 18 if err != nil { 19 // handle error 20 } 21 fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") 22 status, err := bufio.NewReader(conn).ReadString('\n') 23 // ... 24 25 The Listen function creates servers: 26 27 ln, err := net.Listen("tcp", ":8080") 28 if err != nil { 29 // handle error 30 } 31 for { 32 conn, err := ln.Accept() 33 if err != nil { 34 // handle error 35 } 36 go handleConnection(conn) 37 } 38 39 Name Resolution 40 41 The method for resolving domain names, whether indirectly with functions like Dial 42 or directly with functions like LookupHost and LookupAddr, varies by operating system. 43 44 On Unix systems, the resolver has two options for resolving names. 45 It can use a pure Go resolver that sends DNS requests directly to the servers 46 listed in /etc/resolv.conf, or it can use a cgo-based resolver that calls C 47 library routines such as getaddrinfo and getnameinfo. 48 49 By default the pure Go resolver is used, because a blocked DNS request consumes 50 only a goroutine, while a blocked C call consumes an operating system thread. 51 When cgo is available, the cgo-based resolver is used instead under a variety of 52 conditions: on systems that do not let programs make direct DNS requests (OS X), 53 when the LOCALDOMAIN environment variable is present (even if empty), 54 when the RES_OPTIONS or HOSTALIASES environment variable is non-empty, 55 when the ASR_CONFIG environment variable is non-empty (OpenBSD only), 56 when /etc/resolv.conf or /etc/nsswitch.conf specify the use of features that the 57 Go resolver does not implement, and when the name being looked up ends in .local 58 or is an mDNS name. 59 60 The resolver decision can be overridden by setting the netdns value of the 61 GODEBUG environment variable (see package runtime) to go or cgo, as in: 62 63 export GODEBUG=netdns=go # force pure Go resolver 64 export GODEBUG=netdns=cgo # force cgo resolver 65 66 The decision can also be forced while building the Go source tree 67 by setting the netgo or netcgo build tag. 68 69 A numeric netdns setting, as in GODEBUG=netdns=1, causes the resolver 70 to print debugging information about its decisions. 71 To force a particular resolver while also printing debugging information, 72 join the two settings by a plus sign, as in GODEBUG=netdns=go+1. 73 74 On Plan 9, the resolver always accesses /net/cs and /net/dns. 75 76 On Windows, the resolver always uses C library functions, such as GetAddrInfo and DnsQuery. 77 78 */ 79 package net 80 81 import ( 82 "context" 83 "errors" 84 "internal/poll" 85 "io" 86 "os" 87 "syscall" 88 "time" 89 ) 90 91 // netGo and netCgo contain the state of the build tags used 92 // to build this binary, and whether cgo is available. 93 // conf.go mirrors these into conf for easier testing. 94 var ( 95 netGo bool // set true in cgo_stub.go for build tag "netgo" (or no cgo) 96 netCgo bool // set true in conf_netcgo.go for build tag "netcgo" 97 ) 98 99 func init() { 100 sysInit() 101 supportsIPv4 = probeIPv4Stack() 102 supportsIPv6, supportsIPv4map = probeIPv6Stack() 103 } 104 105 // Addr represents a network end point address. 106 // 107 // The two methods Network and String conventionally return strings 108 // that can be passed as the arguments to Dial, but the exact form 109 // and meaning of the strings is up to the implementation. 110 type Addr interface { 111 Network() string // name of the network (for example, "tcp", "udp") 112 String() string // string form of address (for example, "192.0.2.1:25", "[2001:db8::1]:80") 113 } 114 115 // Conn is a generic stream-oriented network connection. 116 // 117 // Multiple goroutines may invoke methods on a Conn simultaneously. 118 type Conn interface { 119 // Read reads data from the connection. 120 // Read can be made to time out and return an Error with Timeout() == true 121 // after a fixed time limit; see SetDeadline and SetReadDeadline. 122 Read(b []byte) (n int, err error) 123 124 // Write writes data to the connection. 125 // Write can be made to time out and return an Error with Timeout() == true 126 // after a fixed time limit; see SetDeadline and SetWriteDeadline. 127 Write(b []byte) (n int, err error) 128 129 // Close closes the connection. 130 // Any blocked Read or Write operations will be unblocked and return errors. 131 Close() error 132 133 // LocalAddr returns the local network address. 134 LocalAddr() Addr 135 136 // RemoteAddr returns the remote network address. 137 RemoteAddr() Addr 138 139 // SetDeadline sets the read and write deadlines associated 140 // with the connection. It is equivalent to calling both 141 // SetReadDeadline and SetWriteDeadline. 142 // 143 // A deadline is an absolute time after which I/O operations 144 // fail with a timeout (see type Error) instead of 145 // blocking. The deadline applies to all future and pending 146 // I/O, not just the immediately following call to Read or 147 // Write. After a deadline has been exceeded, the connection 148 // can be refreshed by setting a deadline in the future. 149 // 150 // An idle timeout can be implemented by repeatedly extending 151 // the deadline after successful Read or Write calls. 152 // 153 // A zero value for t means I/O operations will not time out. 154 SetDeadline(t time.Time) error 155 156 // SetReadDeadline sets the deadline for future Read calls 157 // and any currently-blocked Read call. 158 // A zero value for t means Read will not time out. 159 SetReadDeadline(t time.Time) error 160 161 // SetWriteDeadline sets the deadline for future Write calls 162 // and any currently-blocked Write call. 163 // Even if write times out, it may return n > 0, indicating that 164 // some of the data was successfully written. 165 // A zero value for t means Write will not time out. 166 SetWriteDeadline(t time.Time) error 167 } 168 169 type conn struct { 170 fd *netFD 171 } 172 173 func (c *conn) ok() bool { return c != nil && c.fd != nil } 174 175 // Implementation of the Conn interface. 176 177 // Read implements the Conn Read method. 178 func (c *conn) Read(b []byte) (int, error) { 179 if !c.ok() { 180 return 0, syscall.EINVAL 181 } 182 n, err := c.fd.Read(b) 183 if err != nil && err != io.EOF { 184 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 185 } 186 return n, err 187 } 188 189 // Write implements the Conn Write method. 190 func (c *conn) Write(b []byte) (int, error) { 191 if !c.ok() { 192 return 0, syscall.EINVAL 193 } 194 n, err := c.fd.Write(b) 195 if err != nil { 196 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 197 } 198 return n, err 199 } 200 201 // Close closes the connection. 202 func (c *conn) Close() error { 203 if !c.ok() { 204 return syscall.EINVAL 205 } 206 err := c.fd.Close() 207 if err != nil { 208 err = &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 209 } 210 return err 211 } 212 213 // LocalAddr returns the local network address. 214 // The Addr returned is shared by all invocations of LocalAddr, so 215 // do not modify it. 216 func (c *conn) LocalAddr() Addr { 217 if !c.ok() { 218 return nil 219 } 220 return c.fd.laddr 221 } 222 223 // RemoteAddr returns the remote network address. 224 // The Addr returned is shared by all invocations of RemoteAddr, so 225 // do not modify it. 226 func (c *conn) RemoteAddr() Addr { 227 if !c.ok() { 228 return nil 229 } 230 return c.fd.raddr 231 } 232 233 // SetDeadline implements the Conn SetDeadline method. 234 func (c *conn) SetDeadline(t time.Time) error { 235 if !c.ok() { 236 return syscall.EINVAL 237 } 238 if err := c.fd.pfd.SetDeadline(t); err != nil { 239 return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err} 240 } 241 return nil 242 } 243 244 // SetReadDeadline implements the Conn SetReadDeadline method. 245 func (c *conn) SetReadDeadline(t time.Time) error { 246 if !c.ok() { 247 return syscall.EINVAL 248 } 249 if err := c.fd.pfd.SetReadDeadline(t); err != nil { 250 return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err} 251 } 252 return nil 253 } 254 255 // SetWriteDeadline implements the Conn SetWriteDeadline method. 256 func (c *conn) SetWriteDeadline(t time.Time) error { 257 if !c.ok() { 258 return syscall.EINVAL 259 } 260 if err := c.fd.pfd.SetWriteDeadline(t); err != nil { 261 return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err} 262 } 263 return nil 264 } 265 266 // SetReadBuffer sets the size of the operating system's 267 // receive buffer associated with the connection. 268 func (c *conn) SetReadBuffer(bytes int) error { 269 if !c.ok() { 270 return syscall.EINVAL 271 } 272 if err := setReadBuffer(c.fd, bytes); err != nil { 273 return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err} 274 } 275 return nil 276 } 277 278 // SetWriteBuffer sets the size of the operating system's 279 // transmit buffer associated with the connection. 280 func (c *conn) SetWriteBuffer(bytes int) error { 281 if !c.ok() { 282 return syscall.EINVAL 283 } 284 if err := setWriteBuffer(c.fd, bytes); err != nil { 285 return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err} 286 } 287 return nil 288 } 289 290 // File sets the underlying os.File to blocking mode and returns a copy. 291 // It is the caller's responsibility to close f when finished. 292 // Closing c does not affect f, and closing f does not affect c. 293 // 294 // The returned os.File's file descriptor is different from the connection's. 295 // Attempting to change properties of the original using this duplicate 296 // may or may not have the desired effect. 297 func (c *conn) File() (f *os.File, err error) { 298 f, err = c.fd.dup() 299 if err != nil { 300 err = &OpError{Op: "file", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 301 } 302 return 303 } 304 305 // PacketConn is a generic packet-oriented network connection. 306 // 307 // Multiple goroutines may invoke methods on a PacketConn simultaneously. 308 type PacketConn interface { 309 // ReadFrom reads a packet from the connection, 310 // copying the payload into b. It returns the number of 311 // bytes copied into b and the return address that 312 // was on the packet. 313 // ReadFrom can be made to time out and return 314 // an Error with Timeout() == true after a fixed time limit; 315 // see SetDeadline and SetReadDeadline. 316 ReadFrom(b []byte) (n int, addr Addr, err error) 317 318 // WriteTo writes a packet with payload b to addr. 319 // WriteTo can be made to time out and return 320 // an Error with Timeout() == true after a fixed time limit; 321 // see SetDeadline and SetWriteDeadline. 322 // On packet-oriented connections, write timeouts are rare. 323 WriteTo(b []byte, addr Addr) (n int, err error) 324 325 // Close closes the connection. 326 // Any blocked ReadFrom or WriteTo operations will be unblocked and return errors. 327 Close() error 328 329 // LocalAddr returns the local network address. 330 LocalAddr() Addr 331 332 // SetDeadline sets the read and write deadlines associated 333 // with the connection. It is equivalent to calling both 334 // SetReadDeadline and SetWriteDeadline. 335 // 336 // A deadline is an absolute time after which I/O operations 337 // fail with a timeout (see type Error) instead of 338 // blocking. The deadline applies to all future and pending 339 // I/O, not just the immediately following call to ReadFrom or 340 // WriteTo. After a deadline has been exceeded, the connection 341 // can be refreshed by setting a deadline in the future. 342 // 343 // An idle timeout can be implemented by repeatedly extending 344 // the deadline after successful ReadFrom or WriteTo calls. 345 // 346 // A zero value for t means I/O operations will not time out. 347 SetDeadline(t time.Time) error 348 349 // SetReadDeadline sets the deadline for future ReadFrom calls 350 // and any currently-blocked ReadFrom call. 351 // A zero value for t means ReadFrom will not time out. 352 SetReadDeadline(t time.Time) error 353 354 // SetWriteDeadline sets the deadline for future WriteTo calls 355 // and any currently-blocked WriteTo call. 356 // Even if write times out, it may return n > 0, indicating that 357 // some of the data was successfully written. 358 // A zero value for t means WriteTo will not time out. 359 SetWriteDeadline(t time.Time) error 360 } 361 362 var listenerBacklog = maxListenerBacklog() 363 364 // A Listener is a generic network listener for stream-oriented protocols. 365 // 366 // Multiple goroutines may invoke methods on a Listener simultaneously. 367 type Listener interface { 368 // Accept waits for and returns the next connection to the listener. 369 Accept() (Conn, error) 370 371 // Close closes the listener. 372 // Any blocked Accept operations will be unblocked and return errors. 373 Close() error 374 375 // Addr returns the listener's network address. 376 Addr() Addr 377 } 378 379 // An Error represents a network error. 380 type Error interface { 381 error 382 Timeout() bool // Is the error a timeout? 383 Temporary() bool // Is the error temporary? 384 } 385 386 // Various errors contained in OpError. 387 var ( 388 // For connection setup operations. 389 errNoSuitableAddress = errors.New("no suitable address found") 390 391 // For connection setup and write operations. 392 errMissingAddress = errors.New("missing address") 393 394 // For both read and write operations. 395 errCanceled = errors.New("operation was canceled") 396 ErrWriteToConnected = errors.New("use of WriteTo with pre-connected connection") 397 ) 398 399 // mapErr maps from the context errors to the historical internal net 400 // error values. 401 // 402 // TODO(bradfitz): get rid of this after adjusting tests and making 403 // context.DeadlineExceeded implement net.Error? 404 func mapErr(err error) error { 405 switch err { 406 case context.Canceled: 407 return errCanceled 408 case context.DeadlineExceeded: 409 return poll.ErrTimeout 410 default: 411 return err 412 } 413 } 414 415 // OpError is the error type usually returned by functions in the net 416 // package. It describes the operation, network type, and address of 417 // an error. 418 type OpError struct { 419 // Op is the operation which caused the error, such as 420 // "read" or "write". 421 Op string 422 423 // Net is the network type on which this error occurred, 424 // such as "tcp" or "udp6". 425 Net string 426 427 // For operations involving a remote network connection, like 428 // Dial, Read, or Write, Source is the corresponding local 429 // network address. 430 Source Addr 431 432 // Addr is the network address for which this error occurred. 433 // For local operations, like Listen or SetDeadline, Addr is 434 // the address of the local endpoint being manipulated. 435 // For operations involving a remote network connection, like 436 // Dial, Read, or Write, Addr is the remote address of that 437 // connection. 438 Addr Addr 439 440 // Err is the error that occurred during the operation. 441 Err error 442 } 443 444 func (e *OpError) Error() string { 445 if e == nil { 446 return "<nil>" 447 } 448 s := e.Op 449 if e.Net != "" { 450 s += " " + e.Net 451 } 452 if e.Source != nil { 453 s += " " + e.Source.String() 454 } 455 if e.Addr != nil { 456 if e.Source != nil { 457 s += "->" 458 } else { 459 s += " " 460 } 461 s += e.Addr.String() 462 } 463 s += ": " + e.Err.Error() 464 return s 465 } 466 467 var ( 468 // aLongTimeAgo is a non-zero time, far in the past, used for 469 // immediate cancelation of dials. 470 aLongTimeAgo = time.Unix(233431200, 0) 471 472 // nonDeadline and noCancel are just zero values for 473 // readability with functions taking too many parameters. 474 noDeadline = time.Time{} 475 noCancel = (chan struct{})(nil) 476 ) 477 478 type timeout interface { 479 Timeout() bool 480 } 481 482 func (e *OpError) Timeout() bool { 483 if ne, ok := e.Err.(*os.SyscallError); ok { 484 t, ok := ne.Err.(timeout) 485 return ok && t.Timeout() 486 } 487 t, ok := e.Err.(timeout) 488 return ok && t.Timeout() 489 } 490 491 type temporary interface { 492 Temporary() bool 493 } 494 495 func (e *OpError) Temporary() bool { 496 if ne, ok := e.Err.(*os.SyscallError); ok { 497 t, ok := ne.Err.(temporary) 498 return ok && t.Temporary() 499 } 500 t, ok := e.Err.(temporary) 501 return ok && t.Temporary() 502 } 503 504 // A ParseError is the error type of literal network address parsers. 505 type ParseError struct { 506 // Type is the type of string that was expected, such as 507 // "IP address", "CIDR address". 508 Type string 509 510 // Text is the malformed text string. 511 Text string 512 } 513 514 func (e *ParseError) Error() string { return "invalid " + e.Type + ": " + e.Text } 515 516 type AddrError struct { 517 Err string 518 Addr string 519 } 520 521 func (e *AddrError) Error() string { 522 if e == nil { 523 return "<nil>" 524 } 525 s := e.Err 526 if e.Addr != "" { 527 s = "address " + e.Addr + ": " + s 528 } 529 return s 530 } 531 532 func (e *AddrError) Timeout() bool { return false } 533 func (e *AddrError) Temporary() bool { return false } 534 535 type UnknownNetworkError string 536 537 func (e UnknownNetworkError) Error() string { return "unknown network " + string(e) } 538 func (e UnknownNetworkError) Timeout() bool { return false } 539 func (e UnknownNetworkError) Temporary() bool { return false } 540 541 type InvalidAddrError string 542 543 func (e InvalidAddrError) Error() string { return string(e) } 544 func (e InvalidAddrError) Timeout() bool { return false } 545 func (e InvalidAddrError) Temporary() bool { return false } 546 547 // DNSConfigError represents an error reading the machine's DNS configuration. 548 // (No longer used; kept for compatibility.) 549 type DNSConfigError struct { 550 Err error 551 } 552 553 func (e *DNSConfigError) Error() string { return "error reading DNS config: " + e.Err.Error() } 554 func (e *DNSConfigError) Timeout() bool { return false } 555 func (e *DNSConfigError) Temporary() bool { return false } 556 557 // Various errors contained in DNSError. 558 var ( 559 errNoSuchHost = errors.New("no such host") 560 ) 561 562 // DNSError represents a DNS lookup error. 563 type DNSError struct { 564 Err string // description of the error 565 Name string // name looked for 566 Server string // server used 567 IsTimeout bool // if true, timed out; not all timeouts set this 568 IsTemporary bool // if true, error is temporary; not all errors set this 569 } 570 571 func (e *DNSError) Error() string { 572 if e == nil { 573 return "<nil>" 574 } 575 s := "lookup " + e.Name 576 if e.Server != "" { 577 s += " on " + e.Server 578 } 579 s += ": " + e.Err 580 return s 581 } 582 583 // Timeout reports whether the DNS lookup is known to have timed out. 584 // This is not always known; a DNS lookup may fail due to a timeout 585 // and return a DNSError for which Timeout returns false. 586 func (e *DNSError) Timeout() bool { return e.IsTimeout } 587 588 // Temporary reports whether the DNS error is known to be temporary. 589 // This is not always known; a DNS lookup may fail due to a temporary 590 // error and return a DNSError for which Temporary returns false. 591 func (e *DNSError) Temporary() bool { return e.IsTimeout || e.IsTemporary } 592 593 type writerOnly struct { 594 io.Writer 595 } 596 597 // Fallback implementation of io.ReaderFrom's ReadFrom, when sendfile isn't 598 // applicable. 599 func genericReadFrom(w io.Writer, r io.Reader) (n int64, err error) { 600 // Use wrapper to hide existing r.ReadFrom from io.Copy. 601 return io.Copy(writerOnly{w}, r) 602 } 603 604 // Limit the number of concurrent cgo-using goroutines, because 605 // each will block an entire operating system thread. The usual culprit 606 // is resolving many DNS names in separate goroutines but the DNS 607 // server is not responding. Then the many lookups each use a different 608 // thread, and the system or the program runs out of threads. 609 610 var threadLimit = make(chan struct{}, 500) 611 612 func acquireThread() { 613 threadLimit <- struct{}{} 614 } 615 616 func releaseThread() { 617 <-threadLimit 618 } 619 620 // buffersWriter is the interface implemented by Conns that support a 621 // "writev"-like batch write optimization. 622 // writeBuffers should fully consume and write all chunks from the 623 // provided Buffers, else it should report a non-nil error. 624 type buffersWriter interface { 625 writeBuffers(*Buffers) (int64, error) 626 } 627 628 // Buffers contains zero or more runs of bytes to write. 629 // 630 // On certain machines, for certain types of connections, this is 631 // optimized into an OS-specific batch write operation (such as 632 // "writev"). 633 type Buffers [][]byte 634 635 var ( 636 _ io.WriterTo = (*Buffers)(nil) 637 _ io.Reader = (*Buffers)(nil) 638 ) 639 640 func (v *Buffers) WriteTo(w io.Writer) (n int64, err error) { 641 if wv, ok := w.(buffersWriter); ok { 642 return wv.writeBuffers(v) 643 } 644 for _, b := range *v { 645 nb, err := w.Write(b) 646 n += int64(nb) 647 if err != nil { 648 v.consume(n) 649 return n, err 650 } 651 } 652 v.consume(n) 653 return n, nil 654 } 655 656 func (v *Buffers) Read(p []byte) (n int, err error) { 657 for len(p) > 0 && len(*v) > 0 { 658 n0 := copy(p, (*v)[0]) 659 v.consume(int64(n0)) 660 p = p[n0:] 661 n += n0 662 } 663 if len(*v) == 0 { 664 err = io.EOF 665 } 666 return 667 } 668 669 func (v *Buffers) consume(n int64) { 670 for len(*v) > 0 { 671 ln0 := int64(len((*v)[0])) 672 if ln0 > n { 673 (*v)[0] = (*v)[0][n:] 674 return 675 } 676 n -= ln0 677 *v = (*v)[1:] 678 } 679 }