1 // ucspi -- UCSPI-related utilities
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
30 func (addr *Addr) Network() string { return "tcp" }
32 func (addr *Addr) String() string { return addr.ip + ":" + addr.port }
39 func NewConn(r, w *os.File) (*Conn, error) {
41 return nil, errors.New("no R file descriptor")
44 return nil, errors.New("no W file descriptor")
46 return &Conn{R: r, W: w}, nil
49 func (conn *Conn) Read(b []byte) (int, error) {
53 func (conn *Conn) Write(b []byte) (int, error) { return conn.W.Write(b) }
55 func (conn *Conn) Close() error {
56 errR := conn.R.Close()
57 errW := conn.W.Close()
64 func (conn *Conn) LocalAddr() net.Addr {
65 return &Addr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
68 func (conn *Conn) RemoteAddr() net.Addr {
69 return &Addr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
72 func (conn *Conn) SetDeadline(t time.Time) error {
73 if err := conn.R.SetReadDeadline(t); err != nil {
76 return conn.W.SetWriteDeadline(t)
79 func (conn *Conn) SetReadDeadline(t time.Time) error {
80 return conn.R.SetReadDeadline(t)
83 func (conn *Conn) SetWriteDeadline(t time.Time) error {
84 return conn.W.SetWriteDeadline(t)