8000 extmod/modlwip: Implement setsockopt(IP_ADD_MEMBERSHIP). · lable/micropython@ee04525 · GitHub
[go: up one dir, main page]

Skip to content

Commit ee04525

Browse files
committed
extmod/modlwip: Implement setsockopt(IP_ADD_MEMBERSHIP).
Allows to join multicast groups.
1 parent 55f3324 commit ee04525

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

extmod/modlwip.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@
4545
//#include "lwip/raw.h"
4646
#include "lwip/dns.h"
4747
#include "lwip/tcp_impl.h"
48+
#include "lwip/igmp.h"
4849

4950
#if 0 // print debugging info
5051
#define DEBUG_printf DEBUG_printf
5152
#else // don't print debugging info
5253
#define DEBUG_printf(...) (void)0
5354
#endif
5455

56+
// All socket options should be globally distinct,
57+
// because we ignore option levels for efficiency.
58+
#define IP_ADD_MEMBERSHIP 0x400
59+
5560
// For compatibilily with older lwIP versions.
5661
#ifndef ip_set_option
5762
#define ip_set_option(pcb, opt) ((pcb)->so_options |= (opt))
@@ -1079,17 +1084,35 @@ STATIC mp_obj_t lwip_socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) {
10791084
return mp_const_none;
10801085
}
10811086

1082-
// Integer options
1083-
mp_int_t val = mp_obj_get_int(args[3]);
10841087
switch (opt) {
1085-
case SOF_REUSEADDR:
1088+
// level: SOL_SOCKET
1089+
case SOF_REUSEADDR: {
1090+
mp_int_t val = mp_obj_get_int(args[3]);
10861091
// Options are common for UDP and TCP pcb's.
10871092
if (val) {
10881093
ip_set_option(socket->pcb.tcp, SOF_REUSEADDR);
10891094
} else {
10901095
ip_reset_option(socket->pcb.tcp, SOF_REUSEADDR);
10911096
}
10921097
break;
1098+
}
1099+
1100+
// level: IPPROTO_IP
1101+
case IP_ADD_MEMBERSHIP: {
1102+
mp_buffer_info_t bufinfo;
1103+
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
1104+
if (bufinfo.len != sizeof(ip_addr_t) * 2) {
1105+
mp_raise_ValueError(NULL);
1106+
}
1107+
1108+
// POSIX setsockopt has order: group addr, if addr, lwIP has it vice-versa
1109+
err_t err = igmp_joingroup((ip_addr_t*)bufinfo.buf + 1, bufinfo.buf);
1110+
if (err != ERR_OK) {
1111+
mp_raise_OSError(error_lookup_table[-err]);
1112+
}
1113+
break;
1114+
}
1115+
10931116
default:
10941117
printf("Warning: lwip.setsockopt() not implemented\n");
10951118
}
@@ -1342,6 +1365,9 @@ STATIC const mp_rom_map_elem_t mp_module_lwip_globals_table[] = {
13421365

13431366
{ MP_ROM_QSTR(MP_QSTR_SOL_SOCKET), MP_ROM_INT(1) },
13441367
{ MP_ROM_QSTR(MP_QSTR_SO_REUSEADDR), MP_ROM_INT(SOF_REUSEADDR) },
1368+
1369+
{ MP_ROM_QSTR(MP_QSTR_IPPROTO_IP), MP_ROM_INT(0) },
1370+
{ MP_ROM_QSTR(MP_QSTR_IP_ADD_MEMBERSHIP), MP_ROM_INT(IP_ADD_MEMBERSHIP) },
13451371
};
13461372

13471373
STATIC MP_DEFINE_CONST_DICT(mp_module_lwip_globals, mp_module_lwip_globals_table);

0 commit comments

Comments
 (0)
0