Merge pull request #1459 from wpaulino/identify-peer-failed-conn

brontide: identify remote address of failed connection
This commit is contained in:
Olaoluwa Osuntokun 2018-07-03 21:46:03 -05:00 committed by GitHub
commit 85526dfa46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,6 +2,7 @@ package brontide
import ( import (
"errors" "errors"
"fmt"
"io" "io"
"net" "net"
"time" "time"
@ -86,6 +87,13 @@ func (l *Listener) listen() {
} }
} }
// rejectedConnErr is a helper function that prepends the remote address of the
// failed connection attempt to the original error message.
func rejectedConnErr(err error, remoteAddr string) error {
return fmt.Errorf("unable to accept connection from %v: %v", remoteAddr,
err)
}
// doHandshake asynchronously performs the brontide handshake, so that it does // doHandshake asynchronously performs the brontide handshake, so that it does
// not block the main accept loop. This prevents peers that delay writing to the // not block the main accept loop. This prevents peers that delay writing to the
// connection from block other connection attempts. // connection from block other connection attempts.
@ -98,6 +106,8 @@ func (l *Listener) doHandshake(conn net.Conn) {
default: default:
} }
remoteAddr := conn.RemoteAddr().String()
brontideConn := &Conn{ brontideConn := &Conn{
conn: conn, conn: conn,
noise: NewBrontideMachine(false, l.localStatic, nil), noise: NewBrontideMachine(false, l.localStatic, nil),
@ -114,12 +124,12 @@ func (l *Listener) doHandshake(conn net.Conn) {
var actOne [ActOneSize]byte var actOne [ActOneSize]byte
if _, err := io.ReadFull(conn, actOne[:]); err != nil { if _, err := io.ReadFull(conn, actOne[:]); err != nil {
brontideConn.conn.Close() brontideConn.conn.Close()
l.rejectConn(err) l.rejectConn(rejectedConnErr(err, remoteAddr))
return return
} }
if err := brontideConn.noise.RecvActOne(actOne); err != nil { if err := brontideConn.noise.RecvActOne(actOne); err != nil {
brontideConn.conn.Close() brontideConn.conn.Close()
l.rejectConn(err) l.rejectConn(rejectedConnErr(err, remoteAddr))
return return
} }
@ -128,12 +138,12 @@ func (l *Listener) doHandshake(conn net.Conn) {
actTwo, err := brontideConn.noise.GenActTwo() actTwo, err := brontideConn.noise.GenActTwo()
if err != nil { if err != nil {
brontideConn.conn.Close() brontideConn.conn.Close()
l.rejectConn(err) l.rejectConn(rejectedConnErr(err, remoteAddr))
return return
} }
if _, err := conn.Write(actTwo[:]); err != nil { if _, err := conn.Write(actTwo[:]); err != nil {
brontideConn.conn.Close() brontideConn.conn.Close()
l.rejectConn(err) l.rejectConn(rejectedConnErr(err, remoteAddr))
return return
} }
@ -154,12 +164,12 @@ func (l *Listener) doHandshake(conn net.Conn) {
var actThree [ActThreeSize]byte var actThree [ActThreeSize]byte
if _, err := io.ReadFull(conn, actThree[:]); err != nil { if _, err := io.ReadFull(conn, actThree[:]); err != nil {
brontideConn.conn.Close() brontideConn.conn.Close()
l.rejectConn(err) l.rejectConn(rejectedConnErr(err, remoteAddr))
return return
} }
if err := brontideConn.noise.RecvActThree(actThree); err != nil { if err := brontideConn.noise.RecvActThree(actThree); err != nil {
brontideConn.conn.Close() brontideConn.conn.Close()
l.rejectConn(err) l.rejectConn(rejectedConnErr(err, remoteAddr))
return return
} }