Skip to content

Commit

Permalink
Rebase master at f0df9
Browse files Browse the repository at this point in the history
Switched cofg.Software to a string
Fixed errors introduced by the last rebase
Resolves #74
  • Loading branch information
enobufs committed Jul 15, 2019
1 parent a50906b commit 2c92c03
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 61 deletions.
30 changes: 15 additions & 15 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ClientConfig struct {
Username string
Password string
Realm string
Software *stun.Software
Software string
RTO time.Duration
Conn net.PacketConn // Listening socket (net.PacketConn)
LoggerFactory logging.LoggerFactory
Expand All @@ -48,16 +48,16 @@ type ClientConfig struct {

// Client is a STUN server client
type Client struct {
conn net.PacketConn // read-only
stunServ net.Addr // read-only
turnServ net.Addr // read-only
stunServStr string // read-only, used for dmuxing
turnServStr string // read-only, used for dmuxing
username stun.Username // read-only
password string // read-only
realm stun.Realm // read-only
integrity stun.MessageIntegrity // read-only
software *stun.Software
conn net.PacketConn // read-only
stunServ net.Addr // read-only
turnServ net.Addr // read-only
stunServStr string // read-only, used for dmuxing
turnServStr string // read-only, used for dmuxing
username stun.Username // read-only
password string // read-only
realm stun.Realm // read-only
integrity stun.MessageIntegrity // read-only
software stun.Software // read-only
trMap *client.TransactionMap // thread-safe
rto time.Duration // read-only
relayedConn *client.UDPConn // protected by mutex ***
Expand Down Expand Up @@ -113,7 +113,7 @@ func NewClient(config *ClientConfig) (*Client, error) {
username: stun.NewUsername(config.Username),
password: config.Password,
realm: stun.NewRealm(config.Realm),
software: config.Software,
software: stun.NewSoftware(config.Software),
net: config.Net,
trMap: client.NewTransactionMap(),
rto: defaultRTO,
Expand Down Expand Up @@ -188,8 +188,8 @@ func (c *Client) Close() {
// SendBindingRequestTo sends a new STUN request to the given transport address
func (c *Client) SendBindingRequestTo(to net.Addr) (net.Addr, error) {
attrs := []stun.Setter{stun.TransactionID, stun.BindingRequest}
if c.software != nil {
attrs = append(attrs, *c.software)
if len(c.software) > 0 {
attrs = append(attrs, c.software)
}

msg, err := stun.Build(attrs...)
Expand Down Expand Up @@ -482,7 +482,7 @@ func (c *Client) handleChannelData(data []byte) error {
}

relayedConn := c.relayedUDPConn()
if relayedConn != nil {
if relayedConn == nil {
c.log.Debug("no relayed conn allocated")
return nil // silently discard
}
Expand Down
6 changes: 1 addition & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"testing"
"time"

"github.com/pion/stun"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

"github.com/pion/logging"
"github.com/stretchr/testify/assert"
)
Expand All @@ -20,7 +16,7 @@ func createListeningTestClient(t *testing.T, loggerFactory logging.LoggerFactory
}
c, err := NewClient(&ClientConfig{
Conn: conn,
Software: &stun.NewSoftware("TEST SOFTWARE"),
Software: "TEST SOFTWARE",
LoggerFactory: loggerFactory,
})
if !assert.NoError(t, err, "should succeed") {
Expand Down
7 changes: 1 addition & 6 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net"

"github.com/pion/logging"
"github.com/pion/stun"
"github.com/pion/turn"
)

Expand All @@ -29,15 +28,11 @@ func main() {

cfg := &turn.ClientConfig{
STUNServerAddr: fmt.Sprintf("%s:%d", *host, *port),
Software: *software,
Conn: conn,
LoggerFactory: logging.NewDefaultLoggerFactory(),
}

if *software != "" {
attr := stun.NewSoftware(*software)
cfg.Software = &attr
}

c, err := turn.NewClient(cfg)
if err != nil {
panic(err)
Expand Down
9 changes: 1 addition & 8 deletions cmd/simple-turn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/pion/logging"
"github.com/pion/stun"
"github.com/pion/turn"
)

Expand Down Expand Up @@ -61,19 +60,13 @@ func main() {
}
}

var software *stun.Software
if sw := os.Getenv("SOFTWARE"); sw != "" {
attr := stun.NewSoftware(sw)
software = &attr
}

s := turn.NewServer(&turn.ServerConfig{
Realm: realm,
AuthHandler: createAuthHandler(usersMap),
ChannelBindTimeout: channelBindTimeout,
ListeningPort: udpPort,
LoggerFactory: logging.NewDefaultLoggerFactory(),
Software: software,
Software: os.Getenv("SOFTWARE"),
})

err = s.Start()
Expand Down
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ServerConfig struct {
// Net is used by pion developers. Do not use in your application.
Net *vnet.Net
// Software is the STUN SOFTWARE attribute. Useful for debugging purpose.
Software *stun.Software
Software string
// Sender is a custom implementation of the request Sender.
Sender Sender
}
Expand All @@ -65,7 +65,7 @@ type Server struct {
channelBindTimeout time.Duration
log logging.LeveledLogger
net *vnet.Net
software *stun.Software
software stun.Software
sender Sender
}

Expand Down Expand Up @@ -107,7 +107,7 @@ func NewServer(config *ServerConfig) *Server {
channelBindTimeout: channelBindTimeout,
log: log,
net: config.Net,
software: config.Software,
software: stun.NewSoftware(config.Software),
sender: config.Sender,
}
}
Expand Down
40 changes: 18 additions & 22 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"testing"
"time"

"github.com/pion/stun"
"github.com/pkg/errors"

"github.com/gortc/turn"
"github.com/pion/logging"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -161,24 +158,9 @@ func TestServer(t *testing.T) {
return "", false
},
Realm: "pion.ly",
Software: testSoftware,
LoggerFactory: loggerFactory,
Sender: func(conn net.PacketConn, addr net.Addr, attrs ...stun.Setter) error {
msg, err := stun.Build(attrs...)
if err != nil {
return errors.Wrap(err, "could not build message")
}
var software stun.Software
if err = software.GetFrom(msg); err != nil {
return errors.Wrap(err, "could not get SOFTWARE attribute")
}

assert.Equal(t, testSoftware, software.String())
// just forward to the default sender.
return defaultBuildAndSend(conn, addr, attrs...)
},
}
software := stun.NewSoftware(testSoftware)
cfg.Software = &software

server := NewServer(cfg)

Expand All @@ -193,17 +175,31 @@ func TestServer(t *testing.T) {
// the client.
time.Sleep(100 * time.Microsecond)

lconn, err := net.ListenPacket("udp4", "0.0.0.0:0")
if !assert.NoError(t, err, "should succeed") {
return
}
defer lconn.Close() // nolint:errcheck,gosec

log.Debug("creating a client.")
client, err := NewClient(&ClientConfig{
ListeningAddress: "0.0.0.0:0",
LoggerFactory: loggerFactory,
Conn: lconn,
LoggerFactory: loggerFactory,
})
if !assert.NoError(t, err, "should succeed") {
return
}
err = client.Listen()
if !assert.NoError(t, err, "should succeed") {
return
}
defer client.Close()

log.Debug("sending a binding request.")
resp, err := client.SendSTUNRequest(net.IPv4(127, 0, 0, 1), 3478)
resp, err := client.SendBindingRequestTo(&net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 3478,
})
assert.NoError(t, err, "should succeed")
t.Logf("resp: %v", resp)

Expand Down
2 changes: 2 additions & 0 deletions server_vnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func TestServerVNet(t *testing.T) {
if !assert.NoError(t, err, "should succeed") {
return
}
defer lconn.Close() // nolint:errcheck,gosec

log.Debug("creating a client.")
client, err := NewClient(&ClientConfig{
Expand Down Expand Up @@ -193,6 +194,7 @@ func TestServerVNet(t *testing.T) {
if !assert.NoError(t, err, "should succeed") {
return
}
defer lconn.Close() // nolint:errcheck,gosec

log.Debug("creating a client.")
client, err := NewClient(&ClientConfig{
Expand Down
4 changes: 2 additions & 2 deletions turn.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ func (s *Server) handleChannelData(conn net.PacketConn, srcAddr net.Addr, c *tur

func (s *Server) makeAttrs(transactionID [stun.TransactionIDSize]byte, msgType stun.MessageType, additional ...stun.Setter) []stun.Setter {
attrs := append([]stun.Setter{&stun.Message{TransactionID: transactionID}, msgType}, additional...)
if s.software != nil {
attrs = append(attrs, *s.software)
if len(s.software) > 0 {
attrs = append(attrs, s.software)
}
return attrs
}
Expand Down

0 comments on commit 2c92c03

Please sign in to comment.