#include "portab.h"
-static char UNUSED id[] = "$Id: resolve.c,v 1.22 2006/02/08 15:20:21 fw Exp $";
+static char UNUSED id[] = "$Id: resolve.c,v 1.27 2007/11/25 18:42:37 fw Exp $";
#include "imp.h"
#include <assert.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
-#include <arpa/inet.h>
#include <netdb.h>
#ifdef IDENTAUTH
#include "conn.h"
#include "defines.h"
#include "log.h"
+#include "tool.h"
#include "exp.h"
#include "resolve.h"
static char *Get_Error PARAMS(( int H_Error ));
#endif
-static int
+static pid_t
Resolver_fork(int *pipefds)
{
- int pid;
+ pid_t pid;
+
if (pipe(pipefds) != 0) {
Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
return -1;
}
+/**
+ * Resolve IP (asynchronous!).
+ */
GLOBAL bool
-Resolve_Addr( RES_STAT *s, struct sockaddr_in *Addr, int identsock, void (*cbfunc)(int, short))
+Resolve_Addr(RES_STAT * s, struct sockaddr_in *Addr, int identsock,
+ void (*cbfunc) (int, short))
{
- /* Resolve IP (asynchronous!). */
- int pid, pipefd[2];
+ int pipefd[2];
+ pid_t pid;
+
assert(s != NULL);
- s->success = false;
pid = Resolver_fork(pipefd);
if (pid > 0) {
} /* Resolve_Addr */
+/**
+ * Resolve hostname (asynchronous!).
+ */
GLOBAL bool
Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short))
{
- /* Resolve hostname (asynchronous!). */
- int pid, pipefd[2];
+ int pipefd[2];
+ pid_t pid;
+
assert(s != NULL);
- s->success = false;
pid = Resolver_fork(pipefd);
if (pid > 0) {
assert(s != NULL);
s->resolver_fd = -1;
s->pid = 0;
- /* s->success must not be changed -- it will be set by other Resolve_*() functions */
}
char ip[16];
struct hostent *h;
struct in_addr *addr;
- int len;
+ size_t len;
Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
#endif
/* Write result into pipe to parent */
len = strlen( ip );
- if( write( w_fd, ip, len ) != len) {
+ if ((size_t)write( w_fd, ip, len ) != len) {
Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
close( w_fd );
}
static bool
register_callback( RES_STAT *s, void (*cbfunc)(int, short))
{
- assert(cbfunc);
+ assert(cbfunc != NULL);
assert(s != NULL);
assert(s->resolver_fd >= 0);
return true;
Log( LOG_CRIT, "Resolver: Could not register callback function: %s!", strerror(errno));
- Resolve_Shutdown(s);
+ close(s->resolver_fd);
+ Resolve_Init(s);
return false;
}
return ret;
}
-
+
+/**
+ * Read result of resolver sub-process from pipe
+ */
GLOBAL size_t
Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen)
{
- /* Read result of resolver sub-process from pipe */
- int err, bytes_read;
+ ssize_t bytes_read;
+
assert(buflen > 0);
/* Read result from pipe */
- errno = 0;
bytes_read = read(s->resolver_fd, readbuf, buflen);
if (bytes_read < 0) {
- if (errno != EAGAIN) {
- err = errno;
- Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(err));
- Resolve_Shutdown(s);
- errno = err;
+ if (errno == EAGAIN)
return 0;
- }
- return 0;
- }
- Resolve_Shutdown(s);
- if (bytes_read == 0) { /* EOF: lookup failed */
+ Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(errno));
+ bytes_read = 0;
+ }
#ifdef DEBUG
+ else if (bytes_read == 0)
Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
#endif
- return 0;
- }
-
- s->success = true;
- return bytes_read;
+ Resolve_Shutdown(s);
+ return (size_t)bytes_read;
}
/* -eof- */