2 ucspi -- UCSPI-related utilities
3 Copyright (C) 2021-2023 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/>.
32 func (addr *Addr) Network() string { return "tcp" }
34 func (addr *Addr) String() string { return addr.ip + ":" + addr.port }
41 func NewConn(r, w *os.File) (*Conn, error) {
43 return nil, errors.New("no R file descriptor")
46 return nil, errors.New("no W file descriptor")
48 return &Conn{R: r, W: w}, nil
51 func (conn *Conn) Read(b []byte) (int, error) {
55 func (conn *Conn) Write(b []byte) (int, error) { return conn.W.Write(b) }
57 func (conn *Conn) Close() error {
58 errR := conn.R.Close()
59 errW := conn.W.Close()
66 func (conn *Conn) LocalAddr() net.Addr {
67 return &Addr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
70 func (conn *Conn) RemoteAddr() net.Addr {
71 return &Addr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
74 func (conn *Conn) SetDeadline(t time.Time) error {
75 if err := conn.R.SetReadDeadline(t); err != nil {
78 return conn.W.SetWriteDeadline(t)
81 func (conn *Conn) SetReadDeadline(t time.Time) error {
82 return conn.R.SetReadDeadline(t)
85 func (conn *Conn) SetWriteDeadline(t time.Time) error {
86 return conn.W.SetWriteDeadline(t)