2011-09-16 17:47:21 +02: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.
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// TCP sockets for Plan 9
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
import "syscall"
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// TCPConn is an implementation of the Conn interface for TCP network
|
|
|
|
// connections.
|
2011-09-16 17:47:21 +02:00
|
|
|
type TCPConn struct {
|
|
|
|
plan9Conn
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
// CloseRead shuts down the reading side of the TCP connection.
|
|
|
|
// Most callers should just use Close.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *TCPConn) CloseRead() error {
|
2011-12-02 20:34:41 +01:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-12-02 20:34:41 +01:00
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EPLAN9
|
2011-12-02 20:34:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloseWrite shuts down the writing side of the TCP connection.
|
|
|
|
// Most callers should just use Close.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *TCPConn) CloseWrite() error {
|
2011-12-02 20:34:41 +01:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-12-02 20:34:41 +01:00
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EPLAN9
|
2011-12-02 20:34:41 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// DialTCP connects to the remote address raddr on the network net,
|
2012-10-23 06:31:11 +02:00
|
|
|
// which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is
|
|
|
|
// used as the local address for the connection.
|
2011-12-03 03:17:34 +01:00
|
|
|
func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if raddr == nil {
|
2012-02-01 20:26:59 +01:00
|
|
|
return nil, &OpError{"dial", net, nil, errMissingAddress}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
c1, err := dialPlan9(net, laddr, raddr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return &TCPConn{*c1}, nil
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// TCPListener is a TCP network listener. Clients should typically
|
|
|
|
// use variables of type Listener instead of assuming TCP.
|
2011-09-16 17:47:21 +02:00
|
|
|
type TCPListener struct {
|
|
|
|
plan9Listener
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
func (l *TCPListener) Close() error {
|
|
|
|
if l == nil || l.ctl == nil {
|
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
|
|
|
if _, err := l.ctl.WriteString("hangup"); err != nil {
|
|
|
|
l.ctl.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return l.ctl.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenTCP announces on the TCP address laddr and returns a TCP
|
|
|
|
// listener. Net must be "tcp", "tcp4", or "tcp6". If laddr has a
|
|
|
|
// port of 0, it means to listen on some available port. The caller
|
|
|
|
// can use l.Addr() to retrieve the chosen address.
|
2011-12-03 03:17:34 +01:00
|
|
|
func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if laddr == nil {
|
2012-02-01 20:26:59 +01:00
|
|
|
return nil, &OpError{"listen", net, nil, errMissingAddress}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
l1, err := listenPlan9(net, laddr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return &TCPListener{*l1}, nil
|
|
|
|
}
|