Resolv-0.9.c is a program which resolves a range of IP addresses. Includes logging, search patterns, and displays aliases.
7ca810204ccdad6b8cb4b506c50b326655258bd8615c43388a832fd9f7e368ee
/*
* Copyright (c) 2001 - s23a <s23a@mail.ru>
*
* [ resolver.c ]
*
* 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.
*
*/
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define MAX_FILE 25
#define MAX_PATTERN 40
#define LOG_FILE "pattern"
#define FORMAT_ADDR "%s"
#define VERSION "0.9"
#define TITLE "Resolver v"VERSION" by s23a <s23a@mail.ru>"
char address[35]; /* IP address */
int usage(const char*);
int check_addr(int *);
int extract_addr(int *);
int search_pattern(char *, char*);
int
main(argc, argv)
int argc;
char *argv[];
{
struct hostent *host, *gethostbyaddr();
char oval[4], *file = NULL, *pattern = NULL, **ptr;
int final[8], opts, lflag, sflag, aflag, s;
register int acount, bcount, ccount, a_single, b_single, c_single, d_single;
FILE *fildes = stdout, *sfile;
a_single = b_single = c_single = d_single = 0;
acount = bcount = ccount = 0;
lflag = sflag = aflag = 0;
while ((opts = getopt(argc, argv, "hf:s:a")) != EOF) {
switch (opts) {
case 'f':
if (strlen(optarg) > MAX_FILE) {
printf("logfile too long.\n");
exit(-1);
}
file = optarg;
++lflag;
break;
case 's':
if (strlen(optarg) > MAX_PATTERN) {
printf("pattern too long.\n");
exit(-1);
}
pattern = optarg;
++sflag;
break;
case 'a':
++aflag;
break;
case 'h':
default:
usage(argv[0]);
break;
}
}
if (argc == 1)
usage(argv[0]);
argc -= optind;
argv += optind;
if (argc != 1)
usage(argv[0]);
sscanf(*argv, FORMAT_ADDR, address);
extract_addr(final);
check_addr(final);
if (lflag) {
if ((fildes = fopen(file, "a")) != 0) {
if ((s = open("/dev/tty", O_RDWR)) != -1) {
ioctl(s, TIOCNOTTY, (char*) NULL);
close(s);
}
}
else {
perror("open()");
exit(-1);
}
}
if (sflag)
if ((sfile = fopen(LOG_FILE, "a")) == 0) {
perror("open()");
exit(-1);
}
if(!final[1])
++a_single;
if(!final[3])
++b_single;
if(!final[5])
++c_single;
if(!final[7])
++d_single;
if(a_single)
goto A_Sing;
while (final[0] < final[1]) {
final[0]+=1;
A_Sing:
fprintf((lflag)? fildes: stdout, " |--[A]=[**] %d-(%d)...[**]\n", final[0], final[1]);
fprintf((lflag)? fildes: stdout, " | |\n");
acount=final[2];
if(b_single)
goto B_Sing;
while (final[2] < final[3]) {
final[2]+=1;
B_Sing:
fprintf((lflag)? fildes: stdout ," | |--[B]: %d.%d-(%d)...\n", final[0], final[2], final[3]);
fprintf((lflag)? fildes: stdout, " | | |\n");
bcount=final[4];
if(c_single)
goto C_Sing;
while (final[4] < final[5]) {
final[4]+=1;
C_Sing:
fprintf((lflag)? fildes: stdout," | | |--[C]: %d.%d.%d-(%d)...\n", final[0], final[2], final[4], final[5]);
ccount=final[6];
if (d_single)
goto D_Sing;
while (final[6] < final[7]) {
final[6]+=1;
D_Sing:
oval[0]=(unsigned char) final[0];
oval[1]=(unsigned char) final[2];
oval[2]=(unsigned char) final[4];
oval[3]=(unsigned char) final[6];
if((host = gethostbyaddr(oval, 4, AF_INET)) != NULL) {
if (sflag) {
if(search_pattern(pattern, host->h_name)) {
fprintf(sfile, "%d.%d.%d.%d => %s\n", final[0], final[2],
final[4], final[6], host->h_name);
fflush(sfile);
}
}
fprintf(((lflag)?fildes:stdout), " | | | |-- %d.%d.%d.%d => %s\n",
final[0], final[2], final[4], final[6], host->h_name);
if (aflag) {
ptr = host->h_aliases;
while (*ptr != NULL) {
fprintf(((lflag)?fildes:stdout)," | | | | + %d.%d.%d.%d => %s [alias]\n", final[0], final[2], final[4], final[6], *ptr);
++ptr;
if (*ptr == NULL)
fprintf(((lflag)?fildes: stdout), " | | | |\n");
}
}
fflush(fildes);
}
}
final[6]=ccount;
fprintf(((lflag)?fildes: stdout), " | | |\n");
}
final[4]=bcount;
fprintf(((lflag)?fildes: stdout), " | |\n");
}
final[2]=acount;
fprintf(((lflag)?fildes: stdout), " |\n");
}
return (0);
}
int
search_pattern(s1, s2)
char *s1;
char *s2;
{
register int i, j;
int x, y, count, yes;
i = j = x = y = count = 0;
if(strlen(s1) > strlen(s2)) {
printf("warning: pattern > name\n");
exit(-1);
}
x = ((strlen(s2) / strlen(s1)) -1);
y = strlen(s2) % strlen(s1);
for (i=0; i<(strlen(s1) * x + 1 + y); i++, s2++) {
yes=1;
for (j=0; j<(strlen(s1)); j++)
if (*(s1 + j) != *(s2 + j))
yes=0;
if (yes)
++count;
}
return (count);
}
int
extract_addr(fin)
int *fin;
{
char *p=NULL, a[4][8], *pa=NULL;
register int i, j, num=0;
pa = (char*)a;
bzero((char*)a, 32);
for(i=0; i<8; i++)
*(fin+i)=0;
for (i=0, (p = strtok(address, ".")); p; (p = strtok(NULL, ".")), ++i) {
if (i == 4) {
printf("error: format address incorrect.\n");
exit(-1);
}
strncpy((pa+(i*8)+0), p, 7);
*(pa+(i*8)+8)='\0';
}
p=NULL;
for (j=0; j<4; j++) {
for (i=0, (p = strtok((pa+(j*8)+0), "-")); p; (p = strtok(NULL,"-")), i++, num++) {
if((num == 1) || (num == 3) || (num == 5)) {
if(i==0) {
*(fin+num)=0;
*(fin+(num+1))=atoi(p);
++num;
}
else
*(fin+num)=atoi(p);
}
else
*(fin+num)=atoi(p);
}
}
return (0);
}
int
check_addr(fin)
int *fin;
{
register int i;
for (i=0; i<8; i++)
if(*(fin+i)<0 || *(fin+i)>255) {
(void)printf("error: addres format incorrect\n");
exit(-1);
}
return (0);
}
int
usage(progname)
const char *progname;
{
(void)printf("\n%s\n", TITLE);
(void)printf("\nusage: %s [options] ip
-f <file> - output to a logfile
-s <pattern> - search pattern and output to a logfile
-a - show hostname aliases\n", progname);
exit(1);
}