diff options
| author | Nick Moore <nick@zoic.org> | 2019-05-10 13:56:33 +1000 |
|---|---|---|
| committer | Nick Moore <nick@zoic.org> | 2019-05-10 13:56:33 +1000 |
| commit | 0d08dde62e79e293a56b3d64f4e73a25beb75d76 (patch) | |
| tree | b7546674cab93aab253818a7371447707981d432 /shared-module | |
| parent | af0bba062275cd06577018e276ebe6c5ee0428ce (diff) | |
randomize tcp source port for adafruit/circuitpython#1800
Diffstat (limited to 'shared-module')
| -rw-r--r-- | shared-module/network/__init__.c | 4 | ||||
| -rw-r--r-- | shared-module/network/__init__.h | 5 | ||||
| -rw-r--r-- | shared-module/wiznet/wiznet5k.c | 6 |
3 files changed, 12 insertions, 3 deletions
diff --git a/shared-module/network/__init__.c b/shared-module/network/__init__.c index bf33fef7a..96648b260 100644 --- a/shared-module/network/__init__.c +++ b/shared-module/network/__init__.c @@ -99,3 +99,7 @@ void network_module_create_random_mac_address(uint8_t *mac) { mac[4] = (uint8_t)(rb2 >> 8); mac[5] = (uint8_t)(rb2); } + +uint16_t network_module_create_random_source_tcp_port(void) { + return 0xc000 | shared_modules_random_getrandbits(14); +} diff --git a/shared-module/network/__init__.h b/shared-module/network/__init__.h index 504065979..f4eb05bb5 100644 --- a/shared-module/network/__init__.h +++ b/shared-module/network/__init__.h @@ -25,11 +25,12 @@ * THE SOFTWARE. */ -void network_module_create_random_mac_address(uint8_t *mac); - #ifndef MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H #define MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H +void network_module_create_random_mac_address(uint8_t *mac); +uint16_t network_module_create_random_source_tcp_port(void); + #define MOD_NETWORK_IPADDR_BUF_SIZE (4) #define MOD_NETWORK_AF_INET (2) diff --git a/shared-module/wiznet/wiznet5k.c b/shared-module/wiznet/wiznet5k.c index dbd483451..3c5c5f0c8 100644 --- a/shared-module/wiznet/wiznet5k.c +++ b/shared-module/wiznet/wiznet5k.c @@ -203,8 +203,12 @@ int wiznet5k_socket_accept(mod_network_socket_obj_t *socket, mod_network_socket_ } int wiznet5k_socket_connect(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) { + uint16_t src_port = network_module_create_random_source_tcp_port(); + // make sure same outgoing port number can't be in use by two different sockets. + src_port = (src_port & ~(_WIZCHIP_SOCK_NUM_ - 1)) | socket->u_param.fileno; + // use "bind" function to open the socket in client mode - if (wiznet5k_socket_bind(socket, ip, 0, _errno) != 0) { + if (wiznet5k_socket_bind(socket, NULL, src_port, _errno) != 0) { return -1; } |
