8000 Update modusocket.c · pycom/pycom-micropython-sigfox@b3e3a8c · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit b3e3a8c

Browse files
committed
Update modusocket.c
Small updates & bugfixes
1 parent 258876e commit b3e3a8c

File tree

1 file changed

+23
-64
lines changed

1 file changed

+23
-64
lines changed

esp32/mods/modusocket.c

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
******************************************************************************/
7373
#define MODUSOCKET_MAX_SOCKETS 15
7474
#define MODUSOCKET_CONN_TIMEOUT -2
75-
#define MODUSOCKET_MAX_DNS_SERV 2
75+
#define MODUSOCKET_MAX_DNS_SERV 2
7676
/******************************************************************************
7777
DEFINE PRIVATE TYPES
7878
******************************************************************************/
@@ -127,6 +127,18 @@ void modusocket_socket_add (int32_t sd, bool user) {
127127
// sl_LockObjUnlock (&modusocket_LockObj);
128128
}
129129

130+
void modusocket_check_numdns (mp_obj_t numdns) {
131+
// Check if the index is not numeric
132+
if (!MP_OBJ_IS_SMALL_INT(numdns)) {
133+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_num_type_invalid_arguments));
134+
}
135+
// Check if the index is numeric and exceeds MODUSOCKET_MAX_DNS_SERV (index starts at 0!)
136+
if (mp_obj_get_int(numdns) >= MODUSOCKET_MAX_DNS_SERV) {
137+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Index out of range!\n"));
138+
}
139+
}
140+
141+
130142
void modusocket_socket_delete (int32_t sd) {
131143
// sl_LockObjLock (&modusocket_LockObj, SL_OS_WAIT_FOREVER);
132144
for (int i = 0; i < MODUSOCKET_MAX_SOCKETS; i++) {
@@ -898,19 +910,8 @@ STATIC mp_obj_t mod_usocket_dnsserver(size_t n_args, const mp_obj_t *args)
898910
{
899911
mp_obj_t tuple[2];
900912
ip_addr_t ipaddr;
901-
uint8_t numdns;
902-
if(MP_OBJ_IS_INT(args[0]))
903-
{
904-
numdns = mp_obj_get_int(args[0]);
905-
if (numdns > MODUSOCKET_MAX_DNS_SERV)
906-
{
907-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "DNS index Greater than MAX DNS Servers Possible to Config!\n"));
908-
}
909-
}
910-
else
911-
{
912-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Please Specify Valid DNS index\n"));
913-
}
913+
modusocket_check_numdns(args[0]);
914+
uint8_t numdns = mp_obj_get_int(args[0]);
914915

915916
ipaddr = dns_getserver(numdns);
916917
if(ipaddr.type == 0)
@@ -927,20 +928,8 @@ STATIC mp_obj_t mod_usocket_dnsserver(size_t n_args, const mp_obj_t *args)
927928
else if(n_args > 1)
928929
{
929930
ip_addr_t dnsserver;
930-
uint8_t numdns;
931-
//get DNS Server index
932-
if(mp_obj_is_integer(args[0]))
933-
{
934-
numdns = mp_obj_get_int(args[0]);
935-
if (numdns > MODUSOCKET_MAX_DNS_SERV)
936-
{
937-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "DNS index Greater than MAX DNS Servers Possible to Config!\n"));
938-
}
939-
}
940-
else
941-
{
942-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Please Specify Valid DNS index\n"));
943-
}
931+
modusocket_check_numdns(args[0]);
932+
uint8_t numdns = mp_obj_get_int(args[0]);
944933
//parse dns Server IP
945934
netutils_parse_ipv4_addr(args[1], (uint8_t *)&dnsserver.u_addr.ip4.addr, NETUTILS_BIG);
946935
//IPv4
@@ -954,53 +943,23 @@ STATIC mp_obj_t mod_usocket_dnsserver(size_t n_args, const mp_obj_t *args)
954943
}
955944
else
956945
{
957-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
958-
}
959-
return mp_const_none;
960-
}
961-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_usocket_dnsserver_obj, 1, 2, mod_usocket_dnsserver);
962-
963-
STATIC mp_obj_t mod_usocket_dnsclear(size_t n_args, const mp_obj_t *args)
964-
{
965-
uint8_t numdns;
966-
if(n_args > 0)
967-
{
968-
if(MP_OBJ_IS_INT(args[0]))
969-
{
970-
numdns = mp_obj_get_int(args[0]);
971-
if (numdns > MODUSOCKET_MAX_DNS_SERV)
972-
{
973-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "DNS index Greater than MAX DNS Servers Possible to Config!\n"));
974-
}
975-
}
976-
else
977-
{
978-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Please Specify Valid DNS index\n"));
979-
}
980-
981-
//clear DNS server specified
982-
dns_setserver(numdns, NULL);
983-
}
984-
else
985-
{
986-
uint8_t index;
987-
// clear all DNS servers
988-
for(index = 0; index < MODUSOCKET_MAX_DNS_SERV; index++)
989-
{
990-
dns_setserver(index, NULL);
946+
mp_obj_t tuple[MODUSOCKET_MAX_DNS_SERV];
947+
for(int i=0; i < MODUSOCKET_MAX_DNS_SERV; i++) {
948+
ip_addr_t ipaddr = dns_getserver(i);
949+
tuple[i] = netutils_format_ipv4_addr((uint8_t *)&ipaddr.u_addr.ip4.addr, NETUTILS_BIG);
991950
}
951+
return mp_obj_new_tuple(MODUSOCKET_MAX_DNS_SERV, tuple);
992952
}
993953
return mp_const_none;
994954
}
995-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_usocket_dnsclear_obj, 0, 1, mod_usocket_dnsclear);
955+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_usocket_dnsserver_obj, 0, 2, mod_usocket_dnsserver);
996956

997957
STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = {
998958
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) },
999959

1000960
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&socket_type },
1001961
{ MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&mod_usocket_getaddrinfo_obj },
1002962
{ MP_OBJ_NEW_QSTR(MP_QSTR_dnsserver), (mp_obj_t)&mod_usocket_dnsserver_obj },
1003-
{ MP_OBJ_NEW_QSTR(MP_QSTR_dns_clear), (mp_obj_t)&mod_usocket_dnsclear_obj },
1004963

1005964
// class exceptions
1006965
{ MP_OBJ_NEW_QSTR(MP_QSTR_error), (mp_obj_t)&mp_type_OSError },

0 commit comments

Comments
 (0)
0