Blame src/net/resolver.c

Packit Service 102f81
/*
Packit Service 102f81
 * resolver.c - name resolver library
Packit Service 102f81
 *
Packit Service 102f81
 * Copyright (C) 1999 the icecast team <team@icecast.org>
Packit Service 102f81
 *
Packit Service 102f81
 *  This library is free software; you can redistribute it and/or
Packit Service 102f81
 *  modify it under the terms of the GNU Library General Public
Packit Service 102f81
 *  License as published by the Free Software Foundation; either
Packit Service 102f81
 *  version 2 of the License, or (at your option) any later version.
Packit Service 102f81
 *
Packit Service 102f81
 *  This library is distributed in the hope that it will be useful,
Packit Service 102f81
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 102f81
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 102f81
 *  Library General Public License for more details.
Packit Service 102f81
 *
Packit Service 102f81
 *  You should have received a copy of the GNU Library General Public
Packit Service 102f81
 *  License along with this library; if not, write to the Free
Packit Service 102f81
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit Service 102f81
 */
Packit Service 102f81
#ifdef HAVE_CONFIG_H
Packit Service 102f81
 #include <config.h>
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
#include <stdio.h>
Packit Service 102f81
#include <sys/types.h>
Packit Service 102f81
#include <stdlib.h>
Packit Service 102f81
#include <string.h>
Packit Service 102f81
Packit Service 102f81
#ifndef _WIN32
Packit Service 102f81
#include <netdb.h>
Packit Service 102f81
#include <sys/socket.h>
Packit Service 102f81
#include <netinet/in.h>
Packit Service 102f81
#include <arpa/inet.h>
Packit Service 102f81
#else
Packit Service 102f81
#include <winsock2.h>
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
#ifndef NO_THREAD
Packit Service 102f81
#include <thread/thread.h>
Packit Service 102f81
#else
Packit Service 102f81
#define thread_mutex_create(x) do{}while(0)
Packit Service 102f81
#define thread_mutex_destroy(x) do{}while(0)
Packit Service 102f81
#define thread_mutex_lock(x) do{}while(0)
Packit Service 102f81
#define thread_mutex_unlock(x) do{}while(0)
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
#include "resolver.h"
Packit Service 102f81
#include "sock.h"
Packit Service 102f81
Packit Service 102f81
/* internal function */
Packit Service 102f81
Packit Service 102f81
static int _isip(const char *what);
Packit Service 102f81
Packit Service 102f81
/* internal data */
Packit Service 102f81
Packit Service 102f81
#ifndef NO_THREAD
Packit Service 102f81
static mutex_t _resolver_mutex;
Packit Service 102f81
#endif
Packit Service 102f81
static int _initialized = 0;
Packit Service 102f81
Packit Service 102f81
#ifdef HAVE_INET_PTON
Packit Service 102f81
static int _isip(const char *what)
Packit Service 102f81
{
Packit Service 102f81
    union {
Packit Service 102f81
        struct in_addr v4addr;
Packit Service 102f81
        struct in6_addr v6addr;
Packit Service 102f81
    } addr_u;
Packit Service 102f81
Packit Service 102f81
    if (inet_pton(AF_INET, what, &addr_u.v4addr) <= 0)
Packit Service 102f81
        return inet_pton(AF_INET6, what, &addr_u.v6addr) > 0 ? 1 : 0;
Packit Service 102f81
Packit Service 102f81
    return 1;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
#else
Packit Service 102f81
static int _isip(const char *what)
Packit Service 102f81
{
Packit Service 102f81
    struct in_addr inp;
Packit Service 102f81
Packit Service 102f81
    return inet_aton(what, &inp;;
Packit Service 102f81
}
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
Packit Service 102f81
#if defined (HAVE_GETNAMEINFO) && defined (HAVE_GETADDRINFO)
Packit Service 102f81
char *resolver_getname(const char *ip, char *buff, int len)
Packit Service 102f81
{
Packit Service 102f81
    struct addrinfo *head = NULL, hints;
Packit Service 102f81
    char *ret = NULL;
Packit Service 102f81
Packit Service 102f81
    if (!_isip(ip)) {
Packit Service 102f81
        strncpy(buff, ip, len);
Packit Service 102f81
        buff [len-1] = '\0';
Packit Service 102f81
        return buff;
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    memset (&hints, 0, sizeof (hints));
Packit Service 102f81
    hints.ai_family = AF_UNSPEC;
Packit Service 102f81
    hints.ai_socktype = SOCK_STREAM;
Packit Service 102f81
    hints.ai_flags = AI_CANONNAME;
Packit Service 102f81
    if (getaddrinfo (ip, NULL, &hints, &head))
Packit Service 102f81
        return NULL;
Packit Service 102f81
Packit Service 102f81
    if (head)
Packit Service 102f81
    {
Packit Service 102f81
        if (getnameinfo(head->ai_addr, head->ai_addrlen, buff, len, NULL, 
Packit Service 102f81
                    0, NI_NAMEREQD) == 0)
Packit Service 102f81
            ret = buff;
Packit Service 102f81
Packit Service 102f81
        freeaddrinfo (head);
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    return ret;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
Packit Service 102f81
char *resolver_getip(const char *name, char *buff, int len)
Packit Service 102f81
{
Packit Service 102f81
    struct addrinfo *head, hints;
Packit Service 102f81
    char *ret = NULL;
Packit Service 102f81
Packit Service 102f81
    if (_isip(name)) {
Packit Service 102f81
        strncpy(buff, name, len);
Packit Service 102f81
        buff [len-1] = '\0';
Packit Service 102f81
        return buff;
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    memset (&hints, 0, sizeof (hints));
Packit Service 102f81
    hints . ai_family = AF_UNSPEC;
Packit Service 102f81
    hints . ai_socktype = SOCK_STREAM;
Packit Service 102f81
    if (getaddrinfo (name, NULL, &hints, &head))
Packit Service 102f81
        return NULL;
Packit Service 102f81
Packit Service 102f81
    if (head)
Packit Service 102f81
    {
Packit Service 102f81
        if (getnameinfo(head->ai_addr, head->ai_addrlen, buff, len, NULL, 
Packit Service 102f81
                    0, NI_NUMERICHOST) == 0)
Packit Service 102f81
            ret = buff;
Packit Service 102f81
        freeaddrinfo (head);
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    return ret;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
#else
Packit Service 102f81
Packit Service 102f81
char *resolver_getname(const char *ip, char *buff, int len)
Packit Service 102f81
{
Packit Service 102f81
    struct hostent *host;
Packit Service 102f81
    char *ret = NULL;
Packit Service 102f81
    struct in_addr addr;
Packit Service 102f81
Packit Service 102f81
    if (! _isip(ip))
Packit Service 102f81
    {
Packit Service 102f81
        strncpy(buff, ip, len);
Packit Service 102f81
        buff [len-1] = '\0';
Packit Service 102f81
        return buff;
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    thread_mutex_lock(&_resolver_mutex);
Packit Service 102f81
    if (inet_aton (ip, &addr)) {
Packit Service 102f81
        if ((host=gethostbyaddr (&addr, sizeof (struct in_addr), AF_INET)))
Packit Service 102f81
        {
Packit Service 102f81
            ret = strncpy (buff, host->h_name, len);
Packit Service 102f81
            buff [len-1] = '\0';
Packit Service 102f81
        }
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    thread_mutex_unlock(&_resolver_mutex);
Packit Service 102f81
    return ret;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
char *resolver_getip(const char *name, char *buff, int len)
Packit Service 102f81
{
Packit Service 102f81
    struct hostent *host;
Packit Service 102f81
    char *ret = NULL;
Packit Service 102f81
Packit Service 102f81
    if (_isip(name))
Packit Service 102f81
    {
Packit Service 102f81
        strncpy(buff, name, len);
Packit Service 102f81
        buff [len-1] = '\0';
Packit Service 102f81
        return buff;
Packit Service 102f81
    }
Packit Service 102f81
    thread_mutex_lock(&_resolver_mutex);
Packit Service 102f81
    host = gethostbyname(name);
Packit Service 102f81
    if (host)
Packit Service 102f81
    {
Packit Service 102f81
        char * temp = inet_ntoa(*(struct in_addr *)host->h_addr);
Packit Service 102f81
        ret = strncpy(buff, temp, len);
Packit Service 102f81
        buff [len-1] = '\0';
Packit Service 102f81
    }
Packit Service 102f81
    thread_mutex_unlock(&_resolver_mutex);
Packit Service 102f81
Packit Service 102f81
    return ret;
Packit Service 102f81
}
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
Packit Service 102f81
void resolver_initialize()
Packit Service 102f81
{
Packit Service 102f81
    /* initialize the lib if we havne't done so already */
Packit Service 102f81
Packit Service 102f81
    if (!_initialized)
Packit Service 102f81
    {
Packit Service 102f81
        _initialized = 1;
Packit Service 102f81
        thread_mutex_create (&_resolver_mutex);
Packit Service 102f81
Packit Service 102f81
        /* keep dns connects (TCP) open */
Packit Service 102f81
#ifdef HAVE_SETHOSTENT
Packit Service 102f81
        sethostent(1);
Packit Service 102f81
#endif
Packit Service 102f81
    }
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
void resolver_shutdown(void)
Packit Service 102f81
{
Packit Service 102f81
    if (_initialized)
Packit Service 102f81
    {
Packit Service 102f81
        thread_mutex_destroy(&_resolver_mutex);
Packit Service 102f81
        _initialized = 0;
Packit Service 102f81
#ifdef HAVE_ENDHOSTENT
Packit Service 102f81
        endhostent();
Packit Service 102f81
#endif
Packit Service 102f81
    }
Packit Service 102f81
}
Packit Service 102f81