[PATCH v2 07/14] net-lwip: add dns command

Jerome Forissier jerome.forissier at linaro.org
Thu Jun 6 14:19:06 CEST 2024



On 6/6/24 08:29, Ilias Apalodimas wrote:
> Hi Jerome,
> 
> [...]
> 
>> +
>> +static ulong start;
>> +static ip_addr_t host_ipaddr;
>> +static bool done;
>> +
>> +static void do_dns_tmr(void *arg)
>> +{
>> +     dns_tmr();
>> +}
>> +
>> +static void dns_cb(const char *name, const ip_addr_t *ipaddr, void *arg)
>> +{
>> +     char *var = (char *)arg;
> 
> const char *

Fixed in v3.

> 
>> +     char *ipstr = ip4addr_ntoa(ipaddr);
>> +
>> +     done = true;
>> +
>> +     if (!ipaddr) {
>> +             printf("DNS: host not found\n");
>> +             host_ipaddr.addr = 0;
>> +             return;
>> +     }
>> +
>> +     if (var)
>> +             env_set(var, ipstr);
>> +
>> +     printf("%s\n", ipstr);
>> +}
>> +
>> +int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
>> +{
>> +     bool has_server = false;
>> +     ip_addr_t ipaddr;
>> +     ip_addr_t ns;
>> +     char *nsenv;
>> +     char *name;
>> +     char *var;
>> +     int ret;
>> +
>> +     if (argc == 1 || argc > 3)
>> +             return CMD_RET_USAGE;
>> +
>> +     if (argc >= 2)
>> +             name = argv[1];
>> +
>> +     if (argc == 3)
>> +             var = argv[2];
>> +
>> +     dns_init();
>> +
>> +     nsenv = env_get("dnsip");
>> +     if (nsenv && ipaddr_aton(nsenv, &ns)) {
>> +             dns_setserver(0, &ns);
>> +             has_server = true;
>> +     }
>> +
>> +     nsenv = env_get("dnsip2");
>> +     if (nsenv && ipaddr_aton(nsenv, &ns)) {
>> +             dns_setserver(1, &ns);
>> +             has_server = true;
>> +     }
>> +
>> +     if (!has_server) {
>> +             log_err("No valid name server (dnsip/dnsip2)\n");
>> +             return CMD_RET_FAILURE;
>> +     }
>> +
>> +     done = false;
>> +
>> +     ret = dns_gethostbyname(name, &ipaddr, dns_cb, var);
>> +
>> +     if (ret == ERR_OK) {
>> +             dns_cb(name, &ipaddr, var);
>> +     } else if (ret == ERR_INPROGRESS) {
>> +             start = get_timer(0);
>> +             sys_timeout(DNS_RESEND_MS, do_dns_tmr, NULL);
>> +             do {
>> +                     eth_rx();
>> +                     if (done)
>> +                             break;
>> +                     sys_check_timeouts();
>> +                     if (ctrlc()) {
>> +                             printf("\nAbort\n");
>> +                             break;
>> +                     }
>> +             } while (get_timer(start) < DNS_TIMEOUT_MS);
>> +             sys_untimeout(do_dns_tmr, NULL);
>> +     }
> 
> Looking at the lwip code there are other ret values than just ERR_OK,
> ERR_INPROGRESS. Should we have an 'else' handling the rest?

Not needed, since in this case we will reach the next line
'if (done && host_ipaddr.addr != 0)' which is the true success condition.

Thanks,
-- 
Jerome


More information about the U-Boot mailing list