Skip to content

Commit 12cee0d

Browse files
giulio93cmaglielucarin91
authored
feat(router): split host and port parameter
Co-authored-by: Cristian Maglie <c.maglie@arduino.cc> Co-authored-by: Luca Rinaldi <lucarin@protonmail.com>
1 parent 4364c45 commit 12cee0d

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

network-api/network-api.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"net"
1010
"os"
11+
"strconv"
1112
"sync"
1213
"sync/atomic"
1314
"time"
@@ -54,13 +55,20 @@ func takeLockAndGenerateNextID() (newID uint, unlock func()) {
5455
}
5556

5657
func tcpConnect(ctx context.Context, rpc *msgpackrpc.Connection, params []any) (_result any, _err any) {
57-
if len(params) != 1 {
58-
return nil, []any{1, "Invalid number of parameters, expected server address"}
58+
if len(params) != 2 {
59+
return nil, []any{1, "Invalid number of parameters, expected server address and port"}
5960
}
6061
serverAddr, ok := params[0].(string)
6162
if !ok {
6263
return nil, []any{1, "Invalid parameter type, expected string for server address"}
6364
}
65+
serverPort, ok := msgpackrpc.ToUint(params[1])
66+
if !ok {
67+
return nil, []any{1, "Invalid parameter type, expected uint16 for server port"}
68+
}
69+
70+
serverAddr = net.JoinHostPort(serverAddr, strconv.FormatUint(uint64(serverPort), 10))
71+
6472
conn, err := net.Dial("tcp", serverAddr)
6573
if err != nil {
6674
return nil, []any{2, "Failed to connect to server: " + err.Error()}
@@ -235,16 +243,23 @@ func tcpWrite(ctx context.Context, rpc *msgpackrpc.Connection, params []any) (_r
235243

236244
func tcpConnectSSL(ctx context.Context, rpc *msgpackrpc.Connection, params []any) (_result any, _err any) {
237245
n := len(params)
238-
if n < 1 || n > 2 {
239-
return nil, []any{1, "Invalid number of parameters, expected server address and optional TLS cert"}
246+
if n < 1 || n > 3 {
247+
return nil, []any{1, "Invalid number of parameters, expected server address, port and optional TLS cert"}
240248
}
241249
serverAddr, ok := params[0].(string)
242250
if !ok {
243251
return nil, []any{1, "Invalid parameter type, expected string for server address"}
244252
}
253+
serverPort, ok := msgpackrpc.ToUint(params[1])
254+
if !ok {
255+
return nil, []any{1, "Invalid parameter type, expected uint16 for server port"}
256+
}
257+
258+
serverAddr = net.JoinHostPort(serverAddr, strconv.FormatUint(uint64(serverPort), 10))
259+
245260
var tlsConfig *tls.Config
246-
if n == 2 {
247-
cert, ok := params[1].(string)
261+
if n == 3 {
262+
cert, ok := params[2].(string)
248263
if !ok {
249264
return nil, []any{1, "Invalid parameter type, expected string for TLS cert"}
250265
}

network-api/network-api_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestNetworkAPI(t *testing.T) {
150150
var wg sync.WaitGroup
151151
wg.Add(1)
152152
go func() {
153-
connID, err := tcpConnect(ctx, rpc, []any{"localhost:9999"})
153+
connID, err := tcpConnect(ctx, rpc, []any{"localhost", uint16(9999)})
154154
require.Nil(t, err)
155155
require.Equal(t, uint(2), connID)
156156

@@ -200,7 +200,7 @@ func TestNetworkAPI(t *testing.T) {
200200
require.Nil(t, res)
201201

202202
// Test SSL connection
203-
connIDSSL, err := tcpConnectSSL(ctx, rpc, []any{"www.arduino.cc:443"})
203+
connIDSSL, err := tcpConnectSSL(ctx, rpc, []any{"www.arduino.cc", uint16(443)})
204204
require.Nil(t, err)
205205
require.Equal(t, uint(4), connIDSSL)
206206

@@ -209,7 +209,7 @@ func TestNetworkAPI(t *testing.T) {
209209
require.Equal(t, "", res)
210210

211211
// Test SSL connection with failing certificate verification
212-
connIDSSL, err = tcpConnectSSL(ctx, rpc, []any{"www.arduino.cc:443", testCert})
212+
connIDSSL, err = tcpConnectSSL(ctx, rpc, []any{"www.arduino.cc", uint16(443), testCert})
213213
require.Equal(t, []any{2, "Failed to connect to server: tls: failed to verify certificate: x509: certificate signed by unknown authority"}, err)
214214
require.Nil(t, connIDSSL)
215215
}

0 commit comments

Comments
 (0)