/*
* Copyright (c) 2001 Fenris, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* File: resolver.c
*
* Purpose: This source file...
*
*/
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <setjmp.h>
#include <signal.h>
#include <errno.h>
#include "cache_list.h"
#include "globals.h"
#include "resolver.h"
#define MMAX(a, b) (((a) < (b)) ? (b) : (a))
#define MMIN(a, b) (((a) > (b)) ? (b) : (a))
jmp_buf resolver_env;
void nohostname(int signo)
{
longjmp(resolver_env, 1);
/* NOTREACHED */
}
int main(int argc, char *argv[])
{
unsigned char *name;
Name_Obj *obj;
struct hostent *hp;
unsigned char msg[MMAX(NET_TO_RSLV_LEN, RSLV_TO_NET_LEN)];
int rcvd;
int len;
unsigned int addr;
if ((hp = malloc(sizeof(struct hostent))) == NULL)
die("malloc");
for (;;) {
len = 0;
do {
if ((rcvd = read(0, &msg + len, NET_TO_RSLV_LEN - len)) == -1)
die("read");
len += rcvd;
} while (len < NET_TO_RSLV_LEN);
addr = msg[1];
addr <<= 8;
addr += msg[2];
addr <<= 8;
addr += msg[3];
addr <<= 8;
addr += msg[4];
addr = htonl(addr);
if (!setjmp(resolver_env)) {
signal(SIGALRM, nohostname);
alarm(1);
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
alarm(0);
} else {
hp = NULL;
}
if (hp != NULL) {
len = MMIN(strlen(hp->h_name), MAX_HOSTNAME_LEN - 1);
memcpy(msg + NET_TO_RSLV_LEN, hp->h_name, len);
msg[NET_TO_RSLV_LEN + len] = '\0';
if (write(1, msg, RSLV_TO_NET_LEN) == -1)
die("write");
}
}
return 0;
}
syntax highlighted by Code2HTML, v. 0.9.1