Skip to content

Commit be500fd

Browse files
authored
Merge pull request #4136 from hierophect/socket-portmax
Socket: fix inconsistencies in argument types
2 parents c8cf125 + 857bb8a commit be500fd

File tree

7 files changed

+57
-38
lines changed

7 files changed

+57
-38
lines changed

locale/circuitpython.pot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3622,6 +3622,10 @@ msgstr ""
36223622
msgid "pop from empty %q"
36233623
msgstr ""
36243624

3625+
#: shared-bindings/socketpool/Socket.c shared-bindings/ssl/SSLSocket.c
3626+
msgid "port must be >= 0"
3627+
msgstr ""
3628+
36253629
#: py/objint_mpz.c
36263630
msgid "pow() 3rd argument cannot be 0"
36273631
msgstr ""

ports/esp32s2/common-hal/socketpool/Socket.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bool register_open_socket(socketpool_socket_obj_t* self) {
6363
}
6464

6565
socketpool_socket_obj_t* common_hal_socketpool_socket_accept(socketpool_socket_obj_t* self,
66-
uint8_t* ip, uint *port) {
66+
uint8_t* ip, uint32_t *port) {
6767
struct sockaddr_in accept_addr;
6868
socklen_t socklen = sizeof(accept_addr);
6969
int newsoc = -1;
@@ -113,7 +113,7 @@ socketpool_socket_obj_t* common_hal_socketpool_socket_accept(socketpool_socket_o
113113
}
114114

115115
bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t* self,
116-
const char* host, size_t hostlen, uint8_t port) {
116+
const char* host, size_t hostlen, uint32_t port) {
117117
struct sockaddr_in bind_addr;
118118
bind_addr.sin_addr.s_addr = inet_addr(host);
119119
bind_addr.sin_family = AF_INET;
@@ -144,7 +144,7 @@ void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self) {
144144
}
145145

146146
bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self,
147-
const char* host, mp_uint_t hostlen, mp_int_t port) {
147+
const char* host, size_t hostlen, uint32_t port) {
148148
const struct addrinfo hints = {
149149
.ai_family = AF_INET,
150150
.ai_socktype = SOCK_STREAM,
@@ -202,7 +202,7 @@ bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t* self, int back
202202
}
203203

204204
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* self,
205-
uint8_t* buf, mp_uint_t len, uint8_t* ip, uint *port) {
205+
uint8_t* buf, uint32_t len, uint8_t* ip, uint *port) {
206206

207207
struct sockaddr_in source_addr;
208208
socklen_t socklen = sizeof(source_addr);
@@ -241,7 +241,7 @@ mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* se
241241
return received;
242242
}
243243

244-
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
244+
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, uint32_t len) {
245245
int received = 0;
246246
bool timed_out = false;
247247

@@ -273,7 +273,7 @@ mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self,
273273
return received;
274274
}
275275

276-
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
276+
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, uint32_t len) {
277277
int sent = -1;
278278
if (self->num != -1) {
279279
// LWIP Socket
@@ -290,7 +290,7 @@ mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const
290290
}
291291

292292
mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t* self,
293-
const char* host, size_t hostlen, uint8_t port, const uint8_t* buf, mp_uint_t len) {
293+
const char* host, size_t hostlen, uint32_t port, const uint8_t* buf, uint32_t len) {
294294

295295
// Set parameters
296296
const struct addrinfo hints = {
@@ -322,6 +322,6 @@ mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t* self,
322322
return bytes_sent;
323323
}
324324

325-
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms) {
325+
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, uint32_t timeout_ms) {
326326
self->timeout_ms = timeout_ms;
327327
}

ports/esp32s2/common-hal/ssl/SSLSocket.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,15 @@
3535
#include "py/runtime.h"
3636
#include "supervisor/shared/tick.h"
3737

38-
void common_hal_ssl_sslsocket_settimeout(ssl_sslsocket_obj_t* self, mp_uint_t timeout_ms) {
39-
self->sock->timeout_ms = timeout_ms;
40-
}
41-
4238
ssl_sslsocket_obj_t* common_hal_ssl_sslsocket_accept(ssl_sslsocket_obj_t* self,
43-
uint8_t* ip, uint *port) {
39+
uint8_t* ip, uint32_t *port) {
4440
socketpool_socket_obj_t * sock = common_hal_socketpool_socket_accept(self->sock, ip, port);
4541
ssl_sslsocket_obj_t * sslsock = common_hal_ssl_sslcontext_wrap_socket(self->ssl_context, sock, false, NULL);
4642
return sslsock;
4743
}
4844

4945
bool common_hal_ssl_sslsocket_bind(ssl_sslsocket_obj_t* self,
50-
const char* host, size_t hostlen, uint8_t port) {
46+
const char* host, size_t hostlen, uint32_t port) {
5147
return common_hal_socketpool_socket_bind(self->sock, host, hostlen, port);
5248
}
5349

@@ -58,7 +54,7 @@ void common_hal_ssl_sslsocket_close(ssl_sslsocket_obj_t* self) {
5854
}
5955

6056
bool common_hal_ssl_sslsocket_connect(ssl_sslsocket_obj_t* self,
61-
const char* host, mp_uint_t hostlen, mp_int_t port) {
57+
const char* host, size_t hostlen, uint32_t port) {
6258
esp_tls_cfg_t* tls_config = NULL;
6359
tls_config = &self->ssl_context->ssl_config;
6460
int result = esp_tls_conn_new_sync(host, hostlen, port, tls_config, self->tls);
@@ -104,7 +100,7 @@ bool common_hal_ssl_sslsocket_listen(ssl_sslsocket_obj_t* self, int backlog) {
104100
return common_hal_socketpool_socket_listen(self->sock, backlog);
105101
}
106102

107-
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
103+
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t* self, const uint8_t* buf, uint32_t len) {
108104
int received = 0;
109105
bool timed_out = false;
110106
int status = 0;
@@ -155,7 +151,7 @@ mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t* self, const ui
155151
return received;
156152
}
157153

158-
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
154+
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t* self, const uint8_t* buf, uint32_t len) {
159155
int sent = -1;
160156
sent = esp_tls_conn_write(self->tls, buf, len);
161157

@@ -174,3 +170,7 @@ mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t* self, const uint8_t
174170
}
175171
return sent;
176172
}
173+
174+
void common_hal_ssl_sslsocket_settimeout(ssl_sslsocket_obj_t* self, uint32_t timeout_ms) {
175+
self->sock->timeout_ms = timeout_ms;
176+
}

shared-bindings/socketpool/Socket.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket___exit___obj, 4, 4,
7272
STATIC mp_obj_t socketpool_socket_accept(mp_obj_t self_in) {
7373
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
7474
uint8_t ip[4];
75-
uint port;
75+
uint32_t port;
7676

7777
socketpool_socket_obj_t * sock = common_hal_socketpool_socket_accept(self, ip, &port);
7878

@@ -98,8 +98,11 @@ STATIC mp_obj_t socketpool_socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
9898
size_t hostlen;
9999
const char* host = mp_obj_str_get_data(addr_items[0], &hostlen);
100100
mp_int_t port = mp_obj_get_int(addr_items[1]);
101+
if (port < 0) {
102+
mp_raise_ValueError(translate("port must be >= 0"));
103+
}
101104

102-
bool ok = common_hal_socketpool_socket_bind(self, host, hostlen, port);
105+
bool ok = common_hal_socketpool_socket_bind(self, host, hostlen, (uint32_t)port);
103106
if (!ok) {
104107
mp_raise_ValueError(translate("Error: Failure to bind"));
105108
}
@@ -133,8 +136,11 @@ STATIC mp_obj_t socketpool_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
133136
size_t hostlen;
134137
const char* host = mp_obj_str_get_data(addr_items[0], &hostlen);
135138
mp_int_t port = mp_obj_get_int(addr_items[1]);
139+
if (port < 0) {
140+
mp_raise_ValueError(translate("port must be >= 0"));
141+
}
136142

137-
bool ok = common_hal_socketpool_socket_connect(self, host, hostlen, port);
143+
bool ok = common_hal_socketpool_socket_connect(self, host, hostlen, (uint32_t)port);
138144
if (!ok) {
139145
mp_raise_OSError(0);
140146
}
@@ -278,8 +284,11 @@ STATIC mp_obj_t socketpool_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_
278284
size_t hostlen;
279285
const char* host = mp_obj_str_get_data(addr_items[0], &hostlen);
280286
mp_int_t port = mp_obj_get_int(addr_items[1]);
287+
if (port < 0) {
288+
mp_raise_ValueError(translate("port must be >= 0"));
289+
}
281290

282-
mp_int_t ret = common_hal_socketpool_socket_sendto(self, host, hostlen, port, bufinfo.buf, bufinfo.len);
291+
mp_int_t ret = common_hal_socketpool_socket_sendto(self, host, hostlen, (uint32_t)port, bufinfo.buf, bufinfo.len);
283292
if (!ret) {
284293
mp_raise_OSError(0);
285294
}

shared-bindings/socketpool/Socket.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@
3131

3232
extern const mp_obj_type_t socketpool_socket_type;
3333

34-
socketpool_socket_obj_t * common_hal_socketpool_socket_accept(socketpool_socket_obj_t* self, uint8_t* ip, uint *port);
35-
bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t* self, const char* host, size_t hostlen, uint8_t port);
34+
socketpool_socket_obj_t * common_hal_socketpool_socket_accept(socketpool_socket_obj_t* self, uint8_t* ip, uint32_t *port);
35+
bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t* self, const char* host, size_t hostlen, uint32_t port);
3636
void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self);
37-
bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const char* host, size_t hostlen, mp_int_t port);
37+
bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const char* host, size_t hostlen, uint32_t port);
3838
bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t* self);
3939
bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t* self);
4040
mp_uint_t common_hal_socketpool_socket_get_timeout(socketpool_socket_obj_t* self);
4141
bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t* self, int backlog);
4242
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* self,
43-
uint8_t* buf, mp_uint_t len, uint8_t* ip, uint *port);
44-
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len);
45-
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len);
43+
uint8_t* buf, uint32_t len, uint8_t* ip, uint32_t *port);
44+
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, uint32_t len);
45+
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, uint32_t len);
4646
mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t* self,
47-
const char* host, size_t hostlen, uint8_t port, const uint8_t* buf, mp_uint_t len);
48-
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms);
47+
const char* host, size_t hostlen, uint32_t port, const uint8_t* buf, uint32_t len);
48+
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, uint32_t timeout_ms);
4949

5050
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKET_H

shared-bindings/ssl/SSLSocket.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ssl_sslsocket___exit___obj, 4, 4, ssl
7171
STATIC mp_obj_t ssl_sslsocket_accept(mp_obj_t self_in) {
7272
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
7373
uint8_t ip[4];
74-
uint port;
74+
uint32_t port;
7575

7676
ssl_sslsocket_obj_t * sslsock = common_hal_ssl_sslsocket_accept(self, ip, &port);
7777

@@ -97,8 +97,11 @@ STATIC mp_obj_t ssl_sslsocket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
9797
size_t hostlen;
9898
const char* host = mp_obj_str_get_data(addr_items[0], &hostlen);
9999
mp_int_t port = mp_obj_get_int(addr_items[1]);
100+
if (port < 0) {
101+
mp_raise_ValueError(translate("port must be >= 0"));
102+
}
100103

101-
bool ok = common_hal_ssl_sslsocket_bind(self, host, hostlen, port);
104+
bool ok = common_hal_ssl_sslsocket_bind(self, host, hostlen, (uint32_t)port);
102105
if (!ok) {
103106
mp_raise_ValueError(translate("Error: Failure to bind"));
104107
}
@@ -132,8 +135,11 @@ STATIC mp_obj_t ssl_sslsocket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
132135
size_t hostlen;
133136
const char* host = mp_obj_str_get_data(addr_items[0], &hostlen);
134137
mp_int_t port = mp_obj_get_int(addr_items[1]);
138+
if (port < 0) {
139+
mp_raise_ValueError(translate("port must be >= 0"));
140+
}
135141

136-
bool ok = common_hal_ssl_sslsocket_connect(self, host, hostlen, port);
142+
bool ok = common_hal_ssl_sslsocket_connect(self, host, hostlen, (uint32_t)port);
137143
if (!ok) {
138144
mp_raise_OSError(0);
139145
}

shared-bindings/ssl/SSLSocket.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131

3232
extern const mp_obj_type_t ssl_sslsocket_type;
3333

34-
ssl_sslsocket_obj_t * common_hal_ssl_sslsocket_accept(ssl_sslsocket_obj_t* self, uint8_t* ip, uint *port);
35-
bool common_hal_ssl_sslsocket_bind(ssl_sslsocket_obj_t* self, const char* host, size_t hostlen, uint8_t port);
34+
ssl_sslsocket_obj_t * common_hal_ssl_sslsocket_accept(ssl_sslsocket_obj_t* self, uint8_t* ip, uint32_t *port);
35+
bool common_hal_ssl_sslsocket_bind(ssl_sslsocket_obj_t* self, const char* host, size_t hostlen, uint32_t port);
3636
void common_hal_ssl_sslsocket_close(ssl_sslsocket_obj_t* self);
37-
bool common_hal_ssl_sslsocket_connect(ssl_sslsocket_obj_t* self, const char* host, size_t hostlen, mp_int_t port);
37+
bool common_hal_ssl_sslsocket_connect(ssl_sslsocket_obj_t* self, const char* host, size_t hostlen, uint32_t port);
3838
bool common_hal_ssl_sslsocket_get_closed(ssl_sslsocket_obj_t* self);
3939
bool common_hal_ssl_sslsocket_get_connected(ssl_sslsocket_obj_t* self);
4040
bool common_hal_ssl_sslsocket_listen(ssl_sslsocket_obj_t* self, int backlog);
41-
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t* self, const uint8_t* buf, mp_uint_t len);
42-
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t* self, const uint8_t* buf, mp_uint_t len);
43-
void common_hal_ssl_sslsocket_settimeout(ssl_sslsocket_obj_t* self, mp_uint_t timeout_ms);
41+
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t* self, const uint8_t* buf, uint32_t len);
42+
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t* self, const uint8_t* buf, uint32_t len);
43+
void common_hal_ssl_sslsocket_settimeout(ssl_sslsocket_obj_t* self, uint32_t timeout_ms);
4444

4545
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SSL_SSLSOCKET_H

0 commit comments

Comments
 (0)