Commit 7b4730cb authored by Michael Smith's avatar Michael Smith

Karl Heyes: patches for better networking code. IPv6 support (complete? Not

sure).

svn path=/trunk/net/; revision=4114
parent 89bf546a
......@@ -13,174 +13,188 @@
#ifndef _WIN32
#include <netdb.h>
#include <sys/socket.h>
#include <pthread.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#define sethostent(x)
#endif
#include "../thread/thread.h"
#include "resolver.h"
#include "sock.h"
/* internal function */
static void _lock_resolver(void);
static void _unlock_resolver(void);
static char *_lookup(const char *what, char *buff, int len);
static int _isip(const char *what);
/* internal data */
#ifdef _WIN32
#define mutex_t CRITICAL_SECTION
#else
#define mutex_t pthread_mutex_t
#endif
static mutex_t _resolver_mutex;
static int _initialized = 0;
char *resolver_getname(const char *ip, char *buff, int len)
#ifdef HAVE_INET_PTON
static int _isip(const char *what)
{
if (!_isip(ip)) {
strncpy(buff, ip, len);
return buff;
}
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;
return _lookup(ip, buff, len);
return 1;
}
char *resolver_getip(const char *name, char *buff, int len)
#else
static int _isip(const char *what)
{
if (_isip(name)) {
strncpy(buff, name, len);
return buff;
}
struct in_addr inp;
return _lookup(name, buff, len);
return inet_aton(what, &inp);
}
#endif
static int _isip(const char *what)
#if defined (HAVE_GETNAMEINFO) && defined (HAVE_GETADDRINFO)
char *resolver_getname(const char *ip, char *buff, int len)
{
#ifdef HAVE_IPV6
union {
struct in_addr v4addr;
struct in6_addr v6addr;
} addr_u;
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;
}
if (inet_pton(AF_INET, what, &addr_u.v4addr) <= 0)
return inet_pton(AF_INET6, what, &addr_u.v6addr) > 0 ? 1 : 0;
return 1;
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;
}
#else
struct in_addr inp;
return inet_aton(what, &inp);
#endif
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)) {
if ((host=gethostbyaddr (&addr, sizeof (struct in_addr), AF_INET)))
{
ret = strncpy (buff, host->h_name, len);
buff [len-1] = '\0';
}
}
thread_mutex_unlock(&_resolver_mutex);
return ret;
}
static char *_lookup(const char *what, char *buff, int len)
char *resolver_getip(const char *name, char *buff, int len)
{
/* linux doesn't appear to have getipnodebyname as of glibc-2.2.3, so the IPV6 lookup is untested */
#ifdef HAVE_GETIPNODEBYNAME
int err;
#else
struct in_addr inp;
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;
}
#endif
struct hostent *host = NULL;
char *temp;
/* do a little sanity checking */
if (what == NULL || buff == NULL || len <= 0)
return NULL;
#ifdef HAVE_GETIPNODEBYNAME
host = getipnodebyname(what, AF_INET6, AI_DEFAULT, &err);
if (host) {
if (_isip(what))
strncpy(buff, host->h_name, len);
else
inet_ntop(host->h_addrtype, host->h_addr_list[0], buff, len);
freehostent(host);
} else
buff = NULL;
#else
if (_isip(what)) {
/* gotta lock calls for now, since gethostbyname and such
* aren't threadsafe */
_lock_resolver();
host = gethostbyaddr((char *)&inp, sizeof(struct in_addr), AF_INET);
_unlock_resolver();
if (host == NULL) {
buff = NULL;
} else {
strncpy(buff, host->h_name, len);
}
} else {
_lock_resolver();
host = gethostbyname(what);
_unlock_resolver();
if (host == NULL) {
buff = NULL;
} else {
// still need to be locked here?
temp = inet_ntoa(*(struct in_addr *)host->h_addr);
strncpy(buff, temp, len);
}
}
#endif
return buff;
}
void resolver_initialize()
{
/* initialize the lib if we havne't done so already */
/* initialize the lib if we havne't done so already */
if (!_initialized) {
_initialized = 1;
#ifndef _WIN32
pthread_mutex_init(&_resolver_mutex, NULL);
#else
InitializeCriticalSection(&_resolver_mutex);
#endif
if (!_initialized)
{
_initialized = 1;
thread_mutex_create (&_resolver_mutex);
/* keep dns connects (TCP) open */
sethostent(1);
}
}
void resolver_shutdown(void)
{
if (_initialized) {
#ifndef _WIN32
pthread_mutex_destroy(&_resolver_mutex);
#else
DeleteCriticalSection(&_resolver_mutex);
/* keep dns connects (TCP) open */
#ifdef HAVE_SETHOSTENT
sethostent(1);
#endif
_initialized = 0;
}
}
}
static void _lock_resolver()
void resolver_shutdown(void)
{
#ifndef _WIN32
pthread_mutex_lock(&_resolver_mutex);
#else
EnterCriticalSection(&_resolver_mutex);
if (_initialized)
{
thread_mutex_destroy(&_resolver_mutex);
_initialized = 0;
#ifdef HAVE_ENDHOSTENT
endhostent();
#endif
}
}
static void _unlock_resolver()
{
#ifndef _WIN32
pthread_mutex_unlock(&_resolver_mutex);
#else
LeaveCriticalSection(&_resolver_mutex);
#endif
}
This diff is collapsed.
......@@ -26,9 +26,25 @@
#include <winsock2.h>
#endif
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#else
#ifndef _SYS_UIO_H
struct iovec
{
void *iov_base;
size_t iov_len;
};
#endif
#endif
typedef int sock_t;
/* The following values are based on unix avoiding errno value clashes */
#define SOCK_SUCCESS 0
#define SOCK_ERROR -1
#define SOCK_TIMEOUT -2
#define SOCK_BLOCK 0
#define SOCK_NONBLOCK 1
......@@ -41,6 +57,7 @@ void sock_shutdown(void);
char *sock_get_localip(char *buff, int len);
int sock_error(void);
int sock_recoverable(int error);
int sock_stalled (int error);
int sock_valid_socket(sock_t sock);
int sock_set_blocking(sock_t sock, const int block);
int sock_set_nolinger(sock_t sock);
......@@ -49,11 +66,15 @@ int sock_close(sock_t sock);
/* Connection related socket functions */
sock_t sock_connect_wto(const char *hostname, const int port, const int timeout);
int sock_connect_non_blocking (const char *host, const unsigned port);
int sock_connected (int sock, unsigned timeout);
/* Socket write functions */
int sock_write_bytes(sock_t sock, const char *buff, const int len);
int sock_write_bytes(sock_t sock, const void *buff, const size_t len);
int sock_write(sock_t sock, const char *fmt, ...);
int sock_write_string(sock_t sock, const char *buff);
ssize_t sock_writev (int sock, const struct iovec *iov, const size_t count);
/* Socket read functions */
int sock_read_bytes(sock_t sock, char *buff, const int len);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment