Skip to content

Commit 7d30ef3

Browse files
authored
Merge pull request #9084 from jepler/rpi-setsockopt-90x
raspberrypi: implement setsockopt(SOL_SOCKET, SO_REUSEADDR)
2 parents 8feaec4 + b0de663 commit 7d30ef3

File tree

1 file changed

+29
-9
lines changed
  • ports/raspberrypi/common-hal/socketpool

1 file changed

+29
-9
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,15 +1184,35 @@ void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t *self, uint
11841184
}
11851185

11861186
int common_hal_socketpool_socket_setsockopt(socketpool_socket_obj_t *self, int level, int optname, const void *value, size_t optlen) {
1187-
if (level == SOCKETPOOL_IPPROTO_TCP && optname == SOCKETPOOL_TCP_NODELAY) {
1188-
int one = 1;
1189-
bool enable = optlen == sizeof(&one) && memcmp(value, &one, optlen);
1190-
if (enable) {
1191-
tcp_set_flags(self->pcb.tcp, TF_NODELAY);
1192-
} else {
1193-
tcp_clear_flags(self->pcb.tcp, TF_NODELAY);
1194-
}
1195-
return 0;
1187+
int zero = 0;
1188+
bool enable = optlen == sizeof(&zero) && memcmp(value, &zero, optlen);
1189+
1190+
switch (level) {
1191+
case SOCKETPOOL_IPPROTO_TCP:
1192+
switch (optname) {
1193+
case SOCKETPOOL_TCP_NODELAY:
1194+
if (enable) {
1195+
tcp_set_flags(self->pcb.tcp, TF_NODELAY);
1196+
} else {
1197+
tcp_clear_flags(self->pcb.tcp, TF_NODELAY);
1198+
}
1199+
return 0;
1200+
break;
1201+
}
1202+
break;
1203+
1204+
case SOCKETPOOL_SOL_SOCKET:
1205+
switch (optname) {
1206+
case SOCKETPOOL_SO_REUSEADDR:
1207+
if (enable) {
1208+
ip_set_option(self->pcb.ip, SOF_REUSEADDR);
1209+
} else {
1210+
ip_reset_option(self->pcb.ip, SOF_REUSEADDR);
1211+
}
1212+
return 0;
1213+
break;
1214+
}
1215+
break;
11961216
}
11971217
return -MP_EOPNOTSUPP;
11981218
}

0 commit comments

Comments
 (0)