2001-09-09 22:24:39 -04:00
|
|
|
/*
|
2003-07-13 22:14:59 -04:00
|
|
|
* resolver.c - name resolver library
|
|
|
|
*
|
2018-10-12 06:28:09 -04:00
|
|
|
* Copyright (C) 2014 Michael Smith <msmith@icecast.org>,
|
|
|
|
* Brendan Cully <brendan@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>,
|
|
|
|
* Jack Moffitt <jack@icecast.org>,
|
|
|
|
* Copyright (C) 2012-2018 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
|
2003-07-13 22:14:59 -04:00
|
|
|
*
|
2014-12-05 04:31:08 -05:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2003-07-13 22:14:59 -04:00
|
|
|
*
|
2014-12-05 04:31:08 -05:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2003-07-13 22:14:59 -04:00
|
|
|
*
|
|
|
|
*/
|
2003-03-08 11:05:38 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2002-02-06 20:04:09 -05:00
|
|
|
#include <netdb.h>
|
2001-10-20 01:57:28 -04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#else
|
2002-02-06 20:04:09 -05:00
|
|
|
#include <winsock2.h>
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
2003-03-05 21:35:12 -05:00
|
|
|
#ifndef NO_THREAD
|
2003-03-09 17:56:46 -05:00
|
|
|
#include <thread/thread.h>
|
2003-03-05 20:55:20 -05:00
|
|
|
#else
|
2003-03-05 21:35:12 -05:00
|
|
|
#define thread_mutex_create(x) do{}while(0)
|
|
|
|
#define thread_mutex_destroy(x) do{}while(0)
|
|
|
|
#define thread_mutex_lock(x) do{}while(0)
|
|
|
|
#define thread_mutex_unlock(x) do{}while(0)
|
2003-03-05 20:55:20 -05:00
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
#include "resolver.h"
|
|
|
|
#include "sock.h"
|
|
|
|
|
|
|
|
/* internal function */
|
|
|
|
|
|
|
|
static int _isip(const char *what);
|
|
|
|
|
|
|
|
/* internal data */
|
|
|
|
|
2003-03-05 21:35:12 -05:00
|
|
|
#ifndef NO_THREAD
|
2001-09-09 22:24:39 -04:00
|
|
|
static mutex_t _resolver_mutex;
|
2003-03-05 20:55:20 -05:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
static int _initialized = 0;
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
#ifdef HAVE_INET_PTON
|
|
|
|
static int _isip(const char *what)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2002-11-22 08:02:51 -05:00
|
|
|
union {
|
|
|
|
struct in_addr v4addr;
|
|
|
|
struct in6_addr v6addr;
|
|
|
|
} addr_u;
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, what, &addr_u.v4addr) <= 0)
|
|
|
|
return inet_pton(AF_INET6, what, &addr_u.v6addr) > 0 ? 1 : 0;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
return 1;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
#else
|
|
|
|
static int _isip(const char *what)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2002-11-22 08:02:51 -05:00
|
|
|
struct in_addr inp;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
return inet_aton(what, &inp);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
2002-11-22 08:02:51 -05:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
|
|
|
|
#if defined (HAVE_GETNAMEINFO) && defined (HAVE_GETADDRINFO)
|
|
|
|
char *resolver_getname(const char *ip, char *buff, int len)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2002-11-22 08:02:51 -05:00
|
|
|
struct addrinfo *head = NULL, hints;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
if (!_isip(ip)) {
|
|
|
|
strncpy(buff, ip, len);
|
|
|
|
buff [len-1] = '\0';
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (&hints, 0, sizeof (hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_CANONNAME;
|
|
|
|
if (getaddrinfo (ip, NULL, &hints, &head))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (head)
|
|
|
|
{
|
|
|
|
if (getnameinfo(head->ai_addr, head->ai_addrlen, buff, len, NULL,
|
|
|
|
0, NI_NAMEREQD) == 0)
|
|
|
|
ret = buff;
|
|
|
|
|
|
|
|
freeaddrinfo (head);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
char *resolver_getip(const char *name, char *buff, int len)
|
|
|
|
{
|
|
|
|
struct addrinfo *head, hints;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
if (_isip(name)) {
|
|
|
|
strncpy(buff, name, len);
|
|
|
|
buff [len-1] = '\0';
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (&hints, 0, sizeof (hints));
|
|
|
|
hints . ai_family = AF_UNSPEC;
|
|
|
|
hints . ai_socktype = SOCK_STREAM;
|
|
|
|
if (getaddrinfo (name, NULL, &hints, &head))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (head)
|
|
|
|
{
|
|
|
|
if (getnameinfo(head->ai_addr, head->ai_addrlen, buff, len, NULL,
|
|
|
|
0, NI_NUMERICHOST) == 0)
|
|
|
|
ret = buff;
|
|
|
|
freeaddrinfo (head);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
char *resolver_getname(const char *ip, char *buff, int len)
|
|
|
|
{
|
|
|
|
struct hostent *host;
|
|
|
|
char *ret = NULL;
|
|
|
|
struct in_addr addr;
|
|
|
|
|
|
|
|
if (! _isip(ip))
|
|
|
|
{
|
|
|
|
strncpy(buff, ip, len);
|
|
|
|
buff [len-1] = '\0';
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_mutex_lock(&_resolver_mutex);
|
|
|
|
if (inet_aton (ip, &addr)) {
|
2014-11-07 18:14:29 -05:00
|
|
|
/* casting &addr to const char* as it is recommended on win* */
|
|
|
|
if ((host=gethostbyaddr ((const char *)&addr, sizeof (struct in_addr), AF_INET)))
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
|
|
|
ret = strncpy (buff, host->h_name, len);
|
|
|
|
buff [len-1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_mutex_unlock(&_resolver_mutex);
|
|
|
|
return ret;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
char *resolver_getip(const char *name, char *buff, int len)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2002-11-22 08:02:51 -05:00
|
|
|
struct hostent *host;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
if (_isip(name))
|
|
|
|
{
|
|
|
|
strncpy(buff, name, len);
|
|
|
|
buff [len-1] = '\0';
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
thread_mutex_lock(&_resolver_mutex);
|
|
|
|
host = gethostbyname(name);
|
|
|
|
if (host)
|
|
|
|
{
|
|
|
|
char * temp = inet_ntoa(*(struct in_addr *)host->h_addr);
|
|
|
|
ret = strncpy(buff, temp, len);
|
|
|
|
buff [len-1] = '\0';
|
|
|
|
}
|
|
|
|
thread_mutex_unlock(&_resolver_mutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void resolver_initialize()
|
|
|
|
{
|
2002-11-22 08:02:51 -05:00
|
|
|
/* initialize the lib if we havne't done so already */
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
if (!_initialized)
|
|
|
|
{
|
|
|
|
_initialized = 1;
|
|
|
|
thread_mutex_create (&_resolver_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
/* keep dns connects (TCP) open */
|
|
|
|
#ifdef HAVE_SETHOSTENT
|
|
|
|
sethostent(1);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
void resolver_shutdown(void)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2002-11-22 08:02:51 -05:00
|
|
|
if (_initialized)
|
|
|
|
{
|
|
|
|
thread_mutex_destroy(&_resolver_mutex);
|
|
|
|
_initialized = 0;
|
|
|
|
#ifdef HAVE_ENDHOSTENT
|
|
|
|
endhostent();
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|