]> Cypherpunks.ru repositories - ucspi.git/blobdiff - conn.go
Unify copyright comment format
[ucspi.git] / conn.go
diff --git a/conn.go b/conn.go
index ad9c697251a7758754aee4dc90bdd481f4d50e2e..c1acd516c75afdb0f782c17019fd262965f817ca 100644 (file)
--- a/conn.go
+++ b/conn.go
@@ -1,80 +1,72 @@
-/*
-ucspi -- UCSPI-related utilities
-Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, version 3 of the License.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
+// ucspi -- UCSPI-related utilities
+// Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 package ucspi
 
 import (
-       "io"
+       "errors"
        "net"
        "os"
        "time"
 )
 
-var aLongTimeAgo = time.Unix(1, 0)
-
-type UCSPIAddr struct {
+type Addr struct {
        ip   string
        port string
 }
 
-func (addr *UCSPIAddr) Network() string { return "tcp" }
+func (addr *Addr) Network() string { return "tcp" }
 
-func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
+func (addr *Addr) String() string { return addr.ip + ":" + addr.port }
 
 type Conn struct {
-       R   *os.File
-       W   *os.File
-       eof chan struct{}
+       R *os.File
+       W *os.File
 }
 
-type ReadResult struct {
-       n   int
-       err error
+func NewConn(r, w *os.File) (*Conn, error) {
+       if r == nil {
+               return nil, errors.New("no R file descriptor")
+       }
+       if w == nil {
+               return nil, errors.New("no W file descriptor")
+       }
+       return &Conn{R: r, W: w}, nil
 }
 
 func (conn *Conn) Read(b []byte) (int, error) {
-       c := make(chan ReadResult)
-       go func() {
-               n, err := conn.R.Read(b)
-               c <- ReadResult{n, err}
-       }()
-       select {
-       case res := <-c:
-               return res.n, res.err
-       case <-conn.eof:
-               return 0, io.EOF
-       }
+       return conn.R.Read(b)
 }
 
 func (conn *Conn) Write(b []byte) (int, error) { return conn.W.Write(b) }
 
 func (conn *Conn) Close() error {
-       if err := conn.R.Close(); err != nil {
-               return err
+       errR := conn.R.Close()
+       errW := conn.W.Close()
+       if errR != nil {
+               return errR
        }
-       return os.Stdin.Close()
+       return errW
 }
 
 func (conn *Conn) LocalAddr() net.Addr {
-       return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
+       return &Addr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
 }
 
 func (conn *Conn) RemoteAddr() net.Addr {
-       return &UCSPIAddr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
+       return &Addr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
 }
 
 func (conn *Conn) SetDeadline(t time.Time) error {
@@ -85,12 +77,6 @@ func (conn *Conn) SetDeadline(t time.Time) error {
 }
 
 func (conn *Conn) SetReadDeadline(t time.Time) error {
-       // An ugly hack to forcefully terminate pending read.
-       // net/http calls SetReadDeadline(aLongTimeAgo), but file
-       // descriptors are not capable to exit immediately that way.
-       if t.Equal(aLongTimeAgo) {
-               conn.eof <- struct{}{}
-       }
        return conn.R.SetReadDeadline(t)
 }