/* -*-  Mode:C; c-basic-offset:4; tab-width:8; indent-tabs-mode:t -*- */
/*
 * Copyright (C) 2004-2005 by the University of Southern California
 * $Id: scramble_ips.c,v 1.2 2005/09/20 18:27:46 yuri Exp $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 */

/* setup environment */
#define _GNU_SOURCE		1
#define _FILE_OFFSET_BITS	64

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <getopt.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

#include "scramble_crypt.h"

#ifndef lint
static const char rcsid[] =
"@(#) $Id: scramble_ips.c,v 1.2 2005/09/20 18:27:46 yuri Exp $";
#endif

#define PASS4		256
#define PASS6		257

void
usage(const char *pname) {
    fprintf(stderr, 
	    "Read IP addresses from stdin and print scrambled addresses on stdout\n"
	    "USAGE:\n"
	    "\t%s [--pass4=<num>] [--pass6=<num>] key_file\n"
	    "\tOptions:\n"
	    "\t\t--pass4=<num>	pass <num> higher bits of ipv4 addresses through unchanged\n"
	    "\t\t--pass6=<num>	pass <num> higher bits of ipv6 addresses through unchanged\n"
	    , pname);
    exit(1);
}


int
main(int argc, char *argv[]) 
{
    FILE *keyfile = NULL;
    const char *keyfn = NULL;
    const char *pname = argv[0];

    int opt;
    int pass_bits4 = 0;
    int pass_bits6 = 0;

    struct option long_options[] = {
	{"help",  0, NULL, 'h'},
	{"pass4", 1, NULL, PASS4},
	{"pass6", 1, NULL, PASS6}
    };

    while((opt = getopt_long(argc, argv, 
			     "h",
			     long_options, NULL)) != EOF) {
	switch(opt) {
	    /* long options first: */
	case PASS4:
	    pass_bits4 = atoi(optarg);
	    if (pass_bits4 < 0 || pass_bits4 > 32) {
		fprintf(stderr, "--pass4 option argument must be within [0..32]\n");
		exit(1);
	    }
	    break;
	case PASS6:
	    pass_bits6 = atoi(optarg);
	    if (pass_bits6 < 0 || pass_bits6 > 128) {
		fprintf(stderr, "--pass6 option argument must be within [0..128]\n");
		exit(1);
	    }
	    break;
	    /* short options: */
	case 'h':
	    usage(pname);
	    /* never returns */

	default:
	    usage(pname);
	}
    }
    argc -= optind;
    argv += optind;
    if (argc != 1) {
	usage(pname);
    }

    keyfn = argv[0];

    if ((keyfile = fopen(keyfn, "r")) == NULL) {
	fprintf(stderr, "cannot open the key_file: %s", strerror(errno));
	exit(1);
    }
    fclose(keyfile);
    
    if (scramble_init_from_file(keyfn, SCRAMBLE_BLOWFISH, SCRAMBLE_BLOWFISH, NULL) < 0) {
	fprintf(stderr, "error reading keyfile %s\n", keyfn);
	exit(1);
    }
    
    for (;;) {
	char cbuf[1024];
	int af;
	int i;
	struct in_addr	ip4, ip4s;
	struct in6_addr ip6, ip6s;
	void		*old, *new;
	
	if (fgets(cbuf, sizeof(cbuf), stdin) == NULL)
	    break;
	for (i = strlen(cbuf)-1; i >= 0; --i) {
	    if (!isgraph(cbuf[i]))
		cbuf[i] = '\0';
	}

	/* first try ipv4 */
	af= AF_INET;
	if (inet_pton(af, cbuf, &ip4) <= 0) {
	    /* next try ipv6 */
	    af= AF_INET6;
	    if (inet_pton(af, cbuf, &ip6) <= 0) {
		fprintf(stderr, "don't understand address (%s)\n", cbuf);
		continue;
	    }
	    ip6s = ip6;
	    scramble_ip6(&ip6s, pass_bits6);
	    old = &ip6;
	    new = &ip6s;
	} else {
	    ip4s.s_addr = scramble_ip4(ip4.s_addr, pass_bits4);
	    old = &ip4;
	    new = &ip4s;
	}

	if (cbuf != inet_ntop(af, new, cbuf, sizeof(cbuf))) {
	    perror("can't print new address");
	    exit(1);
	}
	printf("%s\n", cbuf);
    }
    return 0;
}
