#include <stdio.h>
#include <string.h>

#define BUFLEN	512
#define FIXED	3
#define SUBNET  256

typedef struct 
{
	char ip[BUFLEN];
	char mac[BUFLEN];
} entry;

int main ( int argc, char ** argv)
{
	int i, k, match, count;
	char buf[BUFLEN];
	entry allowed[SUBNET];
	
	FILE * allowed_file;
	
	if ( argc < FIXED)
	{
		printf("Usage: %s prefix dummy_mac [file]\n", argv[0]);
		return 1;
	}
	
	/* open stdin if no file was specified */
	if ( argc == 4 )
	{
		allowed_file = fopen(argv[3], "r");
	}
	else
	{
		allowed_file = stdin;
	}
	
	/* read in a table of allowed mac and mapped ips */
	for ( count = 0; count < SUBNET; ++count)
	{
		if ( fscanf(allowed_file, "%s %s", allowed[count].ip, allowed[count].mac) != 2)
			break;
	}
	
	/* remove the last . if present */
	if ( argv[1][strlen(argv[1])-1] == '.')
		argv[1][strlen(argv[1])-1] = '\0';
		
	/* loop through the subnet and print out any denied ip/mac mappings */
	for( i = 0; i < 255; ++i)
	{
		sprintf(buf, "%s.%d", argv[1], i);
		
		for ( match = 0, k = 0; k < count; ++k)
		{
			if ( !strcmp(buf, allowed[k].ip) )
				match = 1;	
		}

		if ( !match )
			printf("%s %s pub\n", buf, argv[2]);
	}
	
	/* print out the given allowed ips */
	for ( i = 0; i < count; ++i)
	{
		printf("%s %s pub\n", allowed[i].ip, allowed[i].mac);
	}

	return 0;
}
	
