Source file src/net/tcpsock_plan9.go

     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  package net
     6  
     7  import (
     8  	"context"
     9  	"io"
    10  	"os"
    11  )
    12  
    13  func (c *TCPConn) readFrom(r io.Reader) (int64, error) {
    14  	return genericReadFrom(c, r)
    15  }
    16  
    17  func (sd *sysDialer) dialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
    18  	if h := sd.testHookDialTCP; h != nil {
    19  		return h(ctx, sd.network, laddr, raddr)
    20  	}
    21  	if h := testHookDialTCP; h != nil {
    22  		return h(ctx, sd.network, laddr, raddr)
    23  	}
    24  	return sd.doDialTCP(ctx, laddr, raddr)
    25  }
    26  
    27  func (sd *sysDialer) doDialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
    28  	switch sd.network {
    29  	case "tcp4":
    30  		// Plan 9 doesn't complain about [::]:0->127.0.0.1, so it's up to us.
    31  		if laddr != nil && len(laddr.IP) != 0 && laddr.IP.To4() == nil {
    32  			return nil, &AddrError{Err: "non-IPv4 local address", Addr: laddr.String()}
    33  		}
    34  	case "tcp", "tcp6":
    35  	default:
    36  		return nil, UnknownNetworkError(sd.network)
    37  	}
    38  	if raddr == nil {
    39  		return nil, errMissingAddress
    40  	}
    41  	fd, err := dialPlan9(ctx, sd.network, laddr, raddr)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return newTCPConn(fd), nil
    46  }
    47  
    48  func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil && ln.fd.ctl != nil }
    49  
    50  func (ln *TCPListener) accept() (*TCPConn, error) {
    51  	fd, err := ln.fd.acceptPlan9()
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	tc := newTCPConn(fd)
    56  	if ln.lc.KeepAlive >= 0 {
    57  		setKeepAlive(fd, true)
    58  		ka := ln.lc.KeepAlive
    59  		if ln.lc.KeepAlive == 0 {
    60  			ka = defaultTCPKeepAlive
    61  		}
    62  		setKeepAlivePeriod(fd, ka)
    63  	}
    64  	return tc, nil
    65  }
    66  
    67  func (ln *TCPListener) close() error {
    68  	if err := ln.fd.pfd.Close(); err != nil {
    69  		return err
    70  	}
    71  	if _, err := ln.fd.ctl.WriteString("hangup"); err != nil {
    72  		ln.fd.ctl.Close()
    73  		return err
    74  	}
    75  	if err := ln.fd.ctl.Close(); err != nil {
    76  		return err
    77  	}
    78  	return nil
    79  }
    80  
    81  func (ln *TCPListener) file() (*os.File, error) {
    82  	f, err := ln.dup()
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	return f, nil
    87  }
    88  
    89  func (sl *sysListener) listenTCP(ctx context.Context, laddr *TCPAddr) (*TCPListener, error) {
    90  	fd, err := listenPlan9(ctx, sl.network, laddr)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	return &TCPListener{fd: fd, lc: sl.ListenConfig}, nil
    95  }
    96  

View as plain text