drivers: wifi: eswifi: Implement dns offload callbacks
Implement getaddrinfo and freeaddrinfo callbacks. Signed-off-by: Loic Poulain <loic.poulain@linaro.org>
This commit is contained in:
parent
733af9a709
commit
4eba946e2b
1 changed files with 94 additions and 0 deletions
|
@ -575,8 +575,102 @@ NET_SOCKET_REGISTER(eswifi, AF_UNSPEC, eswifi_socket_is_supported,
|
|||
eswifi_socket_create);
|
||||
#endif
|
||||
|
||||
static int eswifi_off_getaddrinfo(const char *node, const char *service,
|
||||
const struct zsock_addrinfo *hints,
|
||||
struct zsock_addrinfo **res)
|
||||
{
|
||||
struct sockaddr_in *ai_addr;
|
||||
struct zsock_addrinfo *ai;
|
||||
unsigned long port = 0;
|
||||
char *rsp;
|
||||
int err;
|
||||
|
||||
if (!node) {
|
||||
return DNS_EAI_NONAME;
|
||||
}
|
||||
|
||||
if (service) {
|
||||
port = strtol(service, NULL, 10);
|
||||
if (port < 1 || port > USHRT_MAX) {
|
||||
return DNS_EAI_SERVICE;
|
||||
}
|
||||
}
|
||||
|
||||
if (!res) {
|
||||
return DNS_EAI_NONAME;
|
||||
}
|
||||
|
||||
if (hints && hints->ai_family != AF_INET) {
|
||||
return DNS_EAI_FAIL;
|
||||
}
|
||||
|
||||
eswifi_lock(eswifi);
|
||||
|
||||
/* DNS lookup */
|
||||
snprintk(eswifi->buf, sizeof(eswifi->buf), "D0=%s\r", node);
|
||||
err = eswifi_at_cmd_rsp(eswifi, eswifi->buf, &rsp);
|
||||
if (err < 0) {
|
||||
err = DNS_EAI_FAIL;
|
||||
goto done_unlock;
|
||||
}
|
||||
|
||||
/* Allocate out res (addrinfo) struct. Just one. */
|
||||
*res = calloc(1, sizeof(struct zsock_addrinfo));
|
||||
ai = *res;
|
||||
if (!ai) {
|
||||
err = EAI_MEMORY;
|
||||
goto done_unlock;
|
||||
}
|
||||
|
||||
/* Now, alloc the embedded sockaddr struct: */
|
||||
ai_addr = calloc(1, sizeof(*ai_addr));
|
||||
if (!ai_addr) {
|
||||
free(*res);
|
||||
err = EAI_MEMORY;
|
||||
goto done_unlock;
|
||||
}
|
||||
|
||||
ai->ai_family = AF_INET;
|
||||
ai->ai_socktype = hints ? hints->ai_socktype : SOCK_STREAM;
|
||||
ai->ai_protocol = ai->ai_socktype == SOCK_STREAM ? IPPROTO_UDP : IPPROTO_TCP;
|
||||
|
||||
ai_addr->sin_family = ai->ai_family;
|
||||
ai_addr->sin_port = htons(port);
|
||||
|
||||
if (!net_ipaddr_parse(rsp, strlen(rsp), (struct sockaddr *)ai_addr)) {
|
||||
free(ai_addr);
|
||||
free(*res);
|
||||
err = DNS_EAI_FAIL;
|
||||
goto done_unlock;
|
||||
}
|
||||
|
||||
ai->ai_addrlen = sizeof(*ai_addr);
|
||||
ai->ai_addr = (struct sockaddr *)ai_addr;
|
||||
err = 0;
|
||||
|
||||
done_unlock:
|
||||
eswifi_unlock(eswifi);
|
||||
return err;
|
||||
}
|
||||
|
||||
static void eswifi_off_freeaddrinfo(struct zsock_addrinfo *res)
|
||||
{
|
||||
__ASSERT_NO_MSG(res);
|
||||
|
||||
free(res->ai_addr);
|
||||
free(res);
|
||||
}
|
||||
|
||||
const struct socket_dns_offload eswifi_dns_ops = {
|
||||
.getaddrinfo = eswifi_off_getaddrinfo,
|
||||
.freeaddrinfo = eswifi_off_freeaddrinfo,
|
||||
};
|
||||
|
||||
int eswifi_socket_offload_init(struct eswifi_dev *leswifi)
|
||||
{
|
||||
eswifi = leswifi;
|
||||
|
||||
socket_offload_dns_register(&eswifi_dns_ops);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue