Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add read timeout #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type Client struct {
// retry).
Retry time.Duration

// Interval on which the response is timed out (zero or negative value means no
// timeout).
ResponseTimeout time.Duration

// MaxPacketErrors controls how many packet parsing and validation errors
// the client will ignore before returning the error from Exchange.
//
Expand All @@ -32,6 +36,7 @@ type Client struct {
// DefaultClient is the RADIUS client used by the Exchange function.
var DefaultClient = &Client{
Retry: time.Second,
ResponseTimeout: 0 * time.Second,
MaxPacketErrors: 10,
}

Expand Down Expand Up @@ -95,6 +100,12 @@ func (c *Client) Exchange(ctx context.Context, packet *Packet, addr string) (*Pa
}()

var packetErrorCount int
if c.ResponseTimeout > 0 {
err = conn.SetReadDeadline(time.Now().Add(c.ResponseTimeout))
if err != nil {
return nil, err
}
}

var incoming [MaxPacketLength]byte
for {
Expand Down
30 changes: 30 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package radius

import (
"context"
"net"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -160,3 +161,32 @@ func TestClient_Exchange_nilContext(t *testing.T) {
//lint:ignore SA1012 This test is specifically checking for a nil context
Exchange(nil, req, "")
}

func TestClient_Exchange_readTimeout(t *testing.T) {
secret := []byte(`12345`)

var server *TestServer
handler := HandlerFunc(func(w ResponseWriter, r *Request) {
time.Sleep(time.Minute)
resp := r.Response(CodeAccessAccept)
w.Write(resp)
})
server = NewTestServer(handler, StaticSecretSource(secret))
defer server.Close()

req := New(CodeAccessRequest, secret)

client := Client{
ResponseTimeout: time.Second,
}
resp, err := client.Exchange(context.Background(), req, server.Addr)
if resp != nil {
t.Fatalf("got non-nil response (%v); expected nil", resp)
}
if err == nil {
t.Fatal("got nil error; expected one")
}
if !err.(net.Error).Timeout() {
t.Fatalf("got err = %v; expected net.Error.Timeout()", err)
}
}
4 changes: 2 additions & 2 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func Parse(b, secret []byte) (*Packet, error) {
}

length := int(binary.BigEndian.Uint16(b[2:4]))
if length < 20 || length > MaxPacketLength || len(b) != length {
if length < 20 || length > MaxPacketLength || len(b) < length {
return nil, errors.New("radius: invalid packet length")
}

attrs, err := ParseAttributes(b[20:])
attrs, err := ParseAttributes(b[20:length])
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func TestParse_invalid(t *testing.T) {
"invalid packet length",
},
{
"\x00\xff\x00\x14\x01\x01\x01\x01\x01\x01" +
"\x00\xff\x00\x16\x01\x01\x01\x01\x01\x01" +
"\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01" +
"\x00",
"invalid packet length",
Expand Down