2 ucspi -- UCSPI-related utilities
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 var aLongTimeAgo = time.Unix(1, 0)
34 func (addr *Addr) Network() string { return "tcp" }
36 func (addr *Addr) String() string { return addr.ip + ":" + addr.port }
44 type ReadResult struct {
49 func (conn *Conn) Read(b []byte) (int, error) {
50 c := make(chan ReadResult)
52 n, err := conn.R.Read(b)
53 c <- ReadResult{n, err}
63 func (conn *Conn) Write(b []byte) (int, error) { return conn.W.Write(b) }
65 func (conn *Conn) Close() error {
66 if err := conn.R.Close(); err != nil {
69 return os.Stdin.Close()
72 func (conn *Conn) LocalAddr() net.Addr {
73 return &Addr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
76 func (conn *Conn) RemoteAddr() net.Addr {
77 return &Addr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
80 func (conn *Conn) SetDeadline(t time.Time) error {
81 if err := conn.R.SetReadDeadline(t); err != nil {
84 return conn.W.SetWriteDeadline(t)
87 func (conn *Conn) SetReadDeadline(t time.Time) error {
88 // An ugly hack to forcefully terminate pending read.
89 // net/http calls SetReadDeadline(aLongTimeAgo), but file
90 // descriptors are not capable to exit immediately that way.
91 if t.Equal(aLongTimeAgo) {
92 conn.eof <- struct{}{}
94 return conn.R.SetReadDeadline(t)
97 func (conn *Conn) SetWriteDeadline(t time.Time) error {
98 return conn.W.SetWriteDeadline(t)