Files
go-i2cp/tcp.go

115 lines
2.3 KiB
Go
Raw Normal View History

2018-03-04 17:52:55 -05:00
package go_i2cp
2018-03-11 11:52:57 -04:00
import (
"crypto/tls"
"crypto/x509"
"io"
"net"
"time"
)
2018-03-04 17:52:55 -05:00
2018-03-11 11:52:57 -04:00
type TcpProperty int
2018-03-04 17:52:55 -05:00
2018-03-11 11:52:57 -04:00
const (
TCP_PROP_ADDRESS TcpProperty = iota
TCP_PROP_PORT
TCP_PROP_USE_TLS
TCP_PROP_TLS_CLIENT_CERTIFICATE
NR_OF_TCP_PROPERTIES
)
2018-03-04 17:52:55 -05:00
2018-03-11 11:52:57 -04:00
var CAFile = "/etc/ssl/certs/ca-certificates.crt"
var defaultRouterAddress = "127.0.0.1:7654"
2018-03-04 17:52:55 -05:00
2018-03-12 19:46:44 -04:00
const USE_TLS = false
2018-03-11 11:52:57 -04:00
func (tcp *Tcp) Init() (err error) {
tcp.address, err = net.ResolveTCPAddr("tcp", defaultRouterAddress)
return
}
2018-03-04 17:52:55 -05:00
func (tcp *Tcp) Connect() (err error) {
2018-03-11 11:52:57 -04:00
if USE_TLS {
roots, _ := x509.SystemCertPool()
2018-03-11 11:52:57 -04:00
tcp.tlsConn, err = tls.Dial("tcp", tcp.address.String(), &tls.Config{RootCAs: roots})
} else {
tcp.conn, err = net.DialTCP("tcp", nil, tcp.address)
if err == nil {
err = tcp.conn.SetKeepAlive(true)
}
}
_ = err // currently unused
2018-03-04 17:52:55 -05:00
return
}
func (tcp *Tcp) Send(buf *Stream) (i int, err error) {
2018-03-11 11:52:57 -04:00
if USE_TLS {
i, err = tcp.tlsConn.Write(buf.Bytes())
} else {
i, err = tcp.conn.Write(buf.Bytes())
}
2018-03-04 17:52:55 -05:00
return
}
func (tcp *Tcp) Receive(buf *Stream) (i int, err error) {
2018-03-11 11:52:57 -04:00
if USE_TLS {
i, err = tcp.tlsConn.Read(buf.Bytes())
} else {
i, err = tcp.conn.Read(buf.Bytes())
}
return
2018-03-04 17:52:55 -05:00
}
2018-03-11 11:52:57 -04:00
func (tcp *Tcp) CanRead() bool {
var one []byte
if USE_TLS {
tcp.tlsConn.SetReadDeadline(time.Now())
if _, err := tcp.tlsConn.Read(one); err == io.EOF {
Debug(TCP, "%s detected closed LAN connection", tcp.address.String())
defer tcp.Disconnect()
return false
} else {
var zero time.Time
tcp.tlsConn.SetReadDeadline(zero)
return tcp.tlsConn.ConnectionState().HandshakeComplete
}
} else {
tcp.conn.SetReadDeadline(time.Now())
if _, err := tcp.conn.Read(one); err == io.EOF {
Debug(TCP, "%s detected closed LAN connection", tcp.address.String())
defer tcp.Disconnect()
return false
} else {
var zero time.Time
tcp.conn.SetReadDeadline(zero)
return true
}
}
}
func (tcp *Tcp) Disconnect() {
if USE_TLS {
tcp.tlsConn.Close()
} else {
tcp.conn.Close()
}
}
func (tcp *Tcp) IsConnected() bool {
return tcp.CanRead()
}
2018-03-04 17:52:55 -05:00
2018-03-11 11:52:57 -04:00
func (tcp *Tcp) SetProperty(property TcpProperty, value string) {
tcp.properties[property] = value
}
func (tcp *Tcp) GetProperty(property TcpProperty) string {
return tcp.properties[property]
}
type Tcp struct {
address *net.TCPAddr
conn *net.TCPConn
tlsConn *tls.Conn
properties [NR_OF_TCP_PROPERTIES]string
}