|
@@ -38,6 +38,12 @@ func (e ProtocolError) Error() string {
|
38
|
38
|
|
39
|
39
|
// Query makes an Ident query, if timeout is >0 the query is timed out after that many seconds.
|
40
|
40
|
func Query(ip string, portOnServer, portOnClient int, timeout time.Duration) (response Response, err error) {
|
|
41
|
+ // if a timeout is set, respect it from the beginning of the query, including the dial time
|
|
42
|
+ var deadline time.Time
|
|
43
|
+ if timeout > 0 {
|
|
44
|
+ deadline = time.Now().Add(timeout)
|
|
45
|
+ }
|
|
46
|
+
|
41
|
47
|
var conn net.Conn
|
42
|
48
|
if timeout > 0 {
|
43
|
49
|
conn, err = net.DialTimeout("tcp", net.JoinHostPort(ip, "113"), timeout)
|
|
@@ -47,13 +53,12 @@ func Query(ip string, portOnServer, portOnClient int, timeout time.Duration) (re
|
47
|
53
|
if err != nil {
|
48
|
54
|
return
|
49
|
55
|
}
|
|
56
|
+ defer conn.Close()
|
50
|
57
|
|
51
|
|
- // stop the ident read after <timeout> seconds
|
52
|
|
- if timeout > 0 {
|
53
|
|
- conn.SetDeadline(time.Now().Add(timeout))
|
54
|
|
- }
|
|
58
|
+ // if timeout is 0, `deadline` is the empty time.Time{} which means no deadline:
|
|
59
|
+ conn.SetDeadline(deadline)
|
55
|
60
|
|
56
|
|
- _, err = conn.Write([]byte(fmt.Sprintf("%d, %d", portOnClient, portOnServer) + "\r\n"))
|
|
61
|
+ _, err = conn.Write([]byte(fmt.Sprintf("%d, %d\r\n", portOnClient, portOnServer)))
|
57
|
62
|
if err != nil {
|
58
|
63
|
return
|
59
|
64
|
}
|