#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <iptables.h>
#include <linux/netfilter_ipv4/ip_tables.h>
#include "ipt_SIGN.h"


/* Function which prints out usage message. */
static void
help(void)
{
	printf(
	       "SIGN options:\n"
	       "--sign-with-key filename                  sign packets with secret key\n"
	       "                                          contained in this file\n"
	       "--sign-with-secret secret                 sign packets with this secret\n");
}

static struct option opts[] = {
	{ "sign-with-key", 1, 0, '1' },
	{ "sign-with-secret", 1, 0, '2' },
	{ 0 }
};

/* Allocate and initialize the target. */
static void
init(struct ipt_entry_target *t, unsigned int *nfcache)
{
	struct ipt_sign_info *sign = (struct ipt_sign_info *)t->data;

	/* default */
	*sign->key=0;
	*sign->secret=0;

	/* Can't cache this */ /* ?? */
	*nfcache |= NFC_UNKNOWN;
}

/* Function which parses command options; returns true if it
   ate an option */
static int
parse(int c, char **argv, int invert, unsigned int *flags,
      const struct ipt_entry *entry,
      struct ipt_entry_target **target)
{
	struct ipt_sign_info *sign = (struct ipt_sign_info *)(*target)->data;


	switch(c) {
	case '1':
		if (check_inverse(optarg, &invert))
			exit_error(PARAMETER_PROBLEM,
				   "Unexpected `!' after --sign-with-key");
		
		/* key file name is in optarg */
		/* read & parse it */
		/* if error -> exit_error(PARAMETER_PROBLEM,"dssdf %s",optarg); */
		/* fill ipt_sign_info struct */

		exit_error(PARAMETER_PROBLEM,"--sign-with-key not implemented");

		return 1;

	case '2':
		if (check_inverse(optarg, &invert))
			exit_error(PARAMETER_PROBLEM,
				   "Unexpected `!' after --sign-with-secret");

		/* insert secret into ipt_sign_info struct */
		bzero(sign->secret,IPT_SIGN_MAX_SECRET_LENGTH);
		strncpy(sign->secret,optarg,IPT_SIGN_MAX_SECRET_LENGTH);
		return 1;

	default:
		/* Fall through */
	}
	return 0;
}

/* Final check; nothing. */
static void final_check(unsigned int flags)
{
}

/* Prints out ipt_sign_info. */
static void
print(const struct ipt_ip *ip,
      const struct ipt_entry_target *target,
      int numeric)
{
	const struct ipt_sign_info *sign
		= (const struct ipt_sign_info *)target->data;

	if(sign->key && *sign->key) 
	  printf("sign with key in file %s\n", sign->key);
	else if(sign->secret && *sign->secret)
	  printf("sign with secret '%s'\n", sign->secret);
	else
	  printf("unknown signature method?!\n");

}

/* Saves ipt_sign_info in parsable form to stdout. */
static void save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
{
	const struct ipt_sign_info *sign
		= (const struct ipt_sign_info *)target->data;

	if(sign->key && *sign->key) 
	  printf("--sign-with-key %s ", sign->key);
	else if(sign->secret && *sign->secret)
	  printf("--sign-with-secret %s " , sign->secret);
	else
	  printf("--sign-with-unknown-method ");

}

static
struct iptables_target sign
= { NULL,
    "SIGN",
    NETFILTER_VERSION,
    IPT_ALIGN(sizeof(struct ipt_sign_info)),
    IPT_ALIGN(sizeof(struct ipt_sign_info)),
    &help,
    &init,
    &parse,
    &final_check,
    &print,
    &save,
    opts
};

void _init(void)
{
	register_target(&sign);
}

