Skip to content

Commit

Permalink
drivers: modem: ublox-sara-r4: fix getaddrinfo
Browse files Browse the repository at this point in the history
The implementation of offload_getaddrinfo in this driver failed
when the node it was called with was an IP address. This condition
was never detected, and as a consequence a DNS query was done on
the IP address instead of returning it directly.

Also, the port was set first after running the DNS query.
As a consequence, if the IP address would have been returned directly,
this would have been done without a port been set.

Both errors are fixed in this patch.

Signed-off-by: Hans Wilmers <hans@wilmers.no>
  • Loading branch information
hwilmers authored and carlescufi committed May 27, 2020
1 parent cd7a73c commit 2972cdc
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions drivers/modem/ublox-sara-r4.c
Original file line number Diff line number Diff line change
Expand Up @@ -1579,10 +1579,24 @@ static int offload_getaddrinfo(const char *node, const char *service,
result.ai_canonname = result_canonname;
result_canonname[0] = '\0';

if (service) {
port = ATOI(service, 0U, "port");
if (port < 1 || port > USHRT_MAX) {
return EAI_SERVICE;
}
}

if (port > 0U) {
/* FIXME: DNS is hard-coded to return only IPv4 */
if (result.ai_family == AF_INET) {
net_sin(&result_addr)->sin_port = htons(port);
}
}

/* check to see if node is an IP address */
if (net_addr_pton(result.ai_family, node,
&((struct sockaddr_in *)&result_addr)->sin_addr)
== 1) {
== 0) {
*res = &result;
return 0;
}
Expand All @@ -1592,13 +1606,6 @@ static int offload_getaddrinfo(const char *node, const char *service,
return EAI_NONAME;
}

if (service) {
port = ATOI(service, 0U, "port");
if (port < 1 || port > USHRT_MAX) {
return EAI_SERVICE;
}
}

snprintk(sendbuf, sizeof(sendbuf), "AT+UDNSRN=0,\"%s\"", node);
ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler,
&cmd, 1U, sendbuf, &mdata.sem_response,
Expand All @@ -1607,13 +1614,6 @@ static int offload_getaddrinfo(const char *node, const char *service,
return ret;
}

if (port > 0U) {
/* FIXME: DNS is hard-coded to return only IPv4 */
if (result.ai_family == AF_INET) {
net_sin(&result_addr)->sin_port = htons(port);
}
}

LOG_DBG("DNS RESULT: %s",
log_strdup(net_addr_ntop(result.ai_family,
&net_sin(&result_addr)->sin_addr,
Expand Down

0 comments on commit 2972cdc

Please sign in to comment.