/*******************************************************************************/
/*                                                                             */
/*  Copyright 2005 Pascal Gloor <pascal.gloor@spale.com>                       */
/*                                                                             */
/*  Licensed under the Apache License, Version 2.0 (the "License");            */
/*  you may not use this file except in compliance with the License.           */
/*  You may obtain a copy of the License at                                    */
/*                                                                             */
/*     http://www.apache.org/licenses/LICENSE-2.0                              */
/*                                                                             */
/*  Unless required by applicable law or agreed to in writing, software        */
/*  distributed under the License is distributed on an "AS IS" BASIS,          */
/*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/*  See the License for the specific language governing permissions and        */
/*  limitations under the License.                                             */
/*                                                                             */
/*******************************************************************************/

#include "defs.h"
#include "lookup.h"
#include "conf.h"


void syntax(char *prog);

int main(int argc, char *argv[])
{
	FILE *fh;
	int prg;
	int i = 0;

	if ( argc != 4 )
		syntax(argv[0]);

	if ( load_ipcheck(argv[1]) != 0 )
	{
		fprintf(stderr,"error opening '%s'\n",argv[1]);
		exit(EXIT_FAILURE);
	}
	if ( load_blcheck(argv[2]) != 0 )
	{
		fprintf(stderr,"error opening '%s'\n",argv[2]);
		exit(EXIT_FAILURE);
	}

	if ( memcmp(argv[3],"-",1) == 0 )
	{
		fh = stdout;
	}
	else if ( ( fh = fopen(argv[3],"w") ) == NULL )
	{
		fprintf(stderr,"error opening '%s' for writing\n",argv[3]);
		exit(EXIT_FAILURE);
	}

	if ( init_resolver() != 0 )
	{
		fprintf(stderr,"regexp init error\n");
		exit(EXIT_FAILURE);
	}

	if ( ( prg = find_resolver() ) == -1 )
	{
		fprintf(stderr,"no suitable program found. (nslookup,host)\n");
		exit(EXIT_FAILURE);
	}

	while(iplist[i].ip != 0)
	{
		int j = 0;
		while(bllist[j][0] != '\0')
		{
			struct in_addr addr;
			struct blinfo_t *blinfo;

			addr.s_addr = iplist[i].ip;

			fprintf(fh,"%s:%s:",inet_ntoa(addr), bllist[j]);

			blinfo = exec_resolver(prg, iplist[i].ip, bllist[j]);
			if ( blinfo == NULL )
			{
				if ( errno != 0 )
					fprintf(fh,"error");
				else
					fprintf(fh,"ok");
			}
			else
			{
				fprintf(fh,"blacklisted:%s:%s",blinfo->ip,blinfo->txt);
			}
			fprintf(fh,"\n");
			fflush(fh);
			j++;
		}
		i++;
	}

	fflush(fh);
	fclose(fh);

	return 0;
}

void syntax(char *prog)
{
	fprintf(stderr,"usage: %s <ip list file> <bl list file> <status file>\n",prog);
	fprintf(stderr,"note : 'status file' may be '-' for stdout.\n");
	exit(EXIT_FAILURE);
}