|
72 | 72 | ******************************************************************************/
|
73 | 73 | #define MODUSOCKET_MAX_SOCKETS 15
|
74 | 74 | #define MODUSOCKET_CONN_TIMEOUT -2
|
| 75 | +#define MODUSOCKET_MAX_DNS_SERV 2 |
75 | 76 | /******************************************************************************
|
76 | 77 | DEFINE PRIVATE TYPES
|
77 | 78 | ******************************************************************************/
|
@@ -891,11 +892,115 @@ STATIC mp_obj_t mod_usocket_getaddrinfo(size_t n_args, const mp_obj_t *args) {
|
891 | 892 | }
|
892 | 893 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_usocket_getaddrinfo_obj, 2, 6, mod_usocket_getaddrinfo);
|
893 | 894 |
|
| 895 | +STATIC mp_obj_t mod_usocket_dnsserver(size_t n_args, const mp_obj_t *args) |
| 896 | +{ |
| 897 | + if(n_args == 1) |
| 898 | + { |
| 899 | + mp_obj_t tuple[2]; |
| 900 | + 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 | + } |
| 914 | + |
| 915 | + ipaddr = dns_getserver(numdns); |
| 916 | + if(ipaddr.type == 0) |
| 917 | + { |
| 918 | + tuple[0] = mp_obj_new_int(numdns); |
| 919 | + tuple[1] = netutils_format_ipv4_addr((uint8_t *)&ipaddr.u_addr.ip4.addr, NETUTILS_BIG); |
| 920 | + } |
| 921 | + else |
| 922 | + { |
| 923 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Only IPv4 addresses are currently supported\n")); |
| 924 | + } |
| 925 | + return mp_obj_new_tuple(2, tuple); |
| 926 | + } |
| 927 | + else if(n_args > 1) |
| 928 | + { |
| 929 | + 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 | + } |
| 944 | + //parse dns Server IP |
| 945 | + netutils_parse_ipv4_addr(args[1], (uint8_t *)&dnsserver.u_addr.ip4.addr, NETUTILS_BIG); |
| 946 | + //IPv4 |
| 947 | + dnsserver.type = 0; |
| 948 | + |
| 949 | + //set DNS Server |
| 950 | + dns_setserver(numdns, &dnsserver); |
| 951 | + |
| 952 | + return mp_const_none; |
| 953 | + |
| 954 | + } |
| 955 | + else |
| 956 | + { |
| 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); |
| 991 | + } |
| 992 | + } |
| 993 | + return mp_const_none; |
| 994 | +} |
| 995 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_usocket_dnsclear_obj, 0, 1, mod_usocket_dnsclear); |
| 996 | + |
894 | 997 | STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = {
|
895 | 998 | { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) },
|
896 | 999 |
|
897 | 1000 | { MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&socket_type },
|
898 | 1001 | { MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&mod_usocket_getaddrinfo_obj },
|
| 1002 | + { 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 }, |
899 | 1004 |
|
900 | 1005 | // class exceptions
|
901 | 1006 | { MP_OBJ_NEW_QSTR(MP_QSTR_error), (mp_obj_t)&mp_type_OSError },
|
|
0 commit comments