diff options
author | Robert James Kaes <rjkaes@users.sourceforge.net> | 2001-12-15 05:58:30 +0000 |
---|---|---|
committer | Robert James Kaes <rjkaes@users.sourceforge.net> | 2001-12-15 05:58:30 +0000 |
commit | a4934e2232bcfd7fd306a0f75275094ff5d5643b (patch) | |
tree | 696c4ba823626a37deef5bbe54ebbb4218f0861a /src/sock.c | |
parent | b15be9de3e4189a21c8f9ff5ceb0afd87b6dd553 (diff) |
Added the host name lookup code (which use to be in the DNS caching
system.)
Diffstat (limited to 'src/sock.c')
-rw-r--r-- | src/sock.c | 49 |
1 files changed, 46 insertions, 3 deletions
@@ -1,4 +1,4 @@ -/* $Id: sock.c,v 1.18 2001-11-25 02:21:46 rjkaes Exp $ +/* $Id: sock.c,v 1.19 2001-12-15 05:58:30 rjkaes Exp $ * * Sockets are created and destroyed here. When a new connection comes in from * a client, we need to copy the socket and the create a second socket to the @@ -23,7 +23,6 @@ #include "tinyproxy.h" -#include "dnscache.h" #include "log.h" #include "sock.h" #include "utils.h" @@ -38,6 +37,50 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; #define LOCK() pthread_mutex_lock(&mutex); #define UNLOCK() pthread_mutex_unlock(&mutex); +/* + * The mutex is used for locking around accesses to gethostbyname() + * function. + */ +static pthread_mutex_t gethostbyname_mutex = PTHREAD_MUTEX_INITIALIZER; +#define LOOKUP_LOCK() pthread_mutex_lock(&gethostbyname_mutex); +#define LOOKUP_UNLOCK() pthread_mutex_unlock(&gethostbyname_mutex); + +/* + * Take a string host address and return a struct in_addr so we can connect + * to the remote host. + * + * Return a negative if there is a problem. + */ +static int +lookup_domain(struct in_addr *addr, char *domain) +{ + struct hostent *resolv; + + if (!addr || !domain) + return -1; + + /* + * First check to see if the domain is in dotted-decimal format. + */ + if (inet_aton(domain, (struct in_addr *)addr) != 0) + return 0; + + /* + * Okay, it's an alpha-numeric domain, so look it up. + */ + LOOKUP_LOCK(); + if (!(resolv = gethostbyname(domain))) { + LOOKUP_UNLOCK(); + return -1; + } + + memcpy(addr, resolv->h_addr_list[0], resolv->h_length); + + LOOKUP_UNLOCK(); + + return 0; +} + /* This routine is so old I can't even remember writing it. But I do * remember that it was an .h file because I didn't know putting code in a * header was bad magic yet. anyway, this routine opens a connection to a @@ -64,7 +107,7 @@ opensock(char *ip_addr, uint16_t port) port_info.sin_family = AF_INET; /* Lookup and return the address if possible */ - ret = dnscache(&port_info.sin_addr, ip_addr); + ret = lookup_domain(&port_info.sin_addr, ip_addr); if (ret < 0) { log_message(LOG_ERR, |