Merge pull request #16 from D4-project/tcpreuse

chg: [main] tear down old connections for type 2
pull/19/head
Jean-Louis Huynen 2020-05-26 17:25:00 +02:00 committed by GitHub
commit 88269e3cb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 5 deletions

View File

@ -420,12 +420,43 @@ func setReaderWriters(d4 *d4S) bool {
} }
isn, dstnet := config.IsNet((*d4).conf.destination) isn, dstnet := config.IsNet((*d4).conf.destination)
if isn { if isn {
// First, we test whether a usable connection already exist // We test whether a usable connection already exist
// (case where the reader run out of data) // (case where the reader run out of data)
switch (*d4).dst.w.(type){ if _, ok := (*d4).dst.w.(net.Conn); !ok {
case net.Conn: // We need a connection
// in this case, it's already set up. dial := net.Dialer{
default: Timeout: (*d4).ct,
KeepAlive: (*d4).cka,
FallbackDelay: 0,
}
tlsc := tls.Config{
InsecureSkipVerify: true,
}
if (*d4).cc {
tlsc = tls.Config{
InsecureSkipVerify: false,
RootCAs: &(*d4).ca,
}
}
if (*d4).ce == true {
conn, errc := tls.DialWithDialer(&dial, "tcp", dstnet, &tlsc)
if errc != nil {
logger.Println(errc)
return false
}
(*d4).dst = newD4Writer(conn, (*d4).conf.key)
} else {
conn, errc := dial.Dial("tcp", dstnet)
if errc != nil {
return false
}
(*d4).dst = newD4Writer(conn, (*d4).conf.key)
}
}else{
// The connection can be reused, there is nothing to do
// Except for type 2 - we tear down the old connection,
(*d4).dst.w.(net.Conn).Close()
// and bring up a new one.
dial := net.Dialer{ dial := net.Dialer{
Timeout: (*d4).ct, Timeout: (*d4).ct,
KeepAlive: (*d4).cka, KeepAlive: (*d4).cka,