2016-01-14 22:12:42 +01:00
|
|
|
/****************************************************************************
|
2018-06-08 15:07:21 +02:00
|
|
|
* libs/libc/netdb/lib_dnsbind.c
|
2016-01-14 22:12:42 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-01-14 22:12:42 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-01-14 22:12:42 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-01-14 22:12:42 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/dns.h>
|
|
|
|
|
|
|
|
#include "netdb/lib_dns.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-28 18:08:10 +01:00
|
|
|
#ifndef CONFIG_NET_SOCKOPTS
|
2016-01-14 22:12:42 +01:00
|
|
|
# error CONFIG_NET_SOCKOPTS required by this logic
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the DNS resolver and return a socket bound to the DNS name
|
|
|
|
* server. The name server was previously selected via dns_server().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, the bound, non-negative socket descriptor is returned. A
|
|
|
|
* negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int dns_bind(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int sd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Has the DNS client been properly initialized? */
|
|
|
|
|
|
|
|
if (!dns_initialize())
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: DNS client has not been initialized\n");
|
2016-01-14 22:12:42 +01:00
|
|
|
return -EDESTADDRREQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new socket */
|
|
|
|
|
|
|
|
sd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sd < 0)
|
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
ret = -get_errno();
|
2020-03-28 18:08:10 +01:00
|
|
|
nerr("ERROR: socket() failed: %d\n", ret);
|
|
|
|
return ret;
|
2016-01-14 22:12:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up a receive timeout */
|
|
|
|
|
2018-11-08 15:33:13 +01:00
|
|
|
tv.tv_sec = CONFIG_NETDB_DNSCLIENT_RECV_TIMEOUT;
|
2016-01-14 22:12:42 +01:00
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
ret = setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
ret = -get_errno();
|
2020-03-28 18:08:10 +01:00
|
|
|
nerr("ERROR: setsockopt() failed: %d\n", ret);
|
2016-01-14 22:12:42 +01:00
|
|
|
close(sd);
|
2020-03-28 18:08:10 +01:00
|
|
|
return ret;
|
2016-01-14 22:12:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return sd;
|
|
|
|
}
|