/*
 * 	send.c
 * 
 * 2006 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
 * All rights reserved.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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
 */
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

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

#include <netinet/ip.h>

#include "include/linux/types.h"
#include "net/core/alloc/avl.h"

#include "control.h"

static inline __u32 num2ip(__u8 a0, __u8 a1, __u8 a2, __u8 a3)
{
	__u32 ret = 0;

	ret |= a0;
	ret <<= 8;
	ret |= a1;
	ret <<= 8;
	ret |= a2;
	ret <<= 8;
	ret |= a3;

	return ret;
}

static void zsend_usage(char *p)
{
	fprintf(stderr, "Usage: %s -f ctl_file -o output_file -c nr_cpus -s size -r reserved_size -h\n", p);
}

int main(int argc, char *argv[])
{
	int ch, err, size, res_len;
	unsigned int nr_cpus;
	char *ctl_file;
	struct zc_data *e;
	struct zc_control *zc_ctl, *ctl;
	struct zc_alloc_ctl alloc_ctl;
	void *ptr;
	struct iphdr *iph;

	ctl_file = NULL;
	size = 1400;
	res_len = 256;
	nr_cpus = 2;

	while ((ch = getopt(argc, argv, "r:c:s:f:h")) != -1) {
		switch (ch) {
			case 'c':
				nr_cpus = atoi(optarg);
				break;
			case 'r':
				res_len = atoi(optarg);
				break;
			case 's':
				size = atoi(optarg);
				break;
			case 'f':
				ctl_file = optarg;
				break;
			case 'h':
			default:
				zsend_usage(argv[0]);
				return 0;
		}
	}

	if (!ctl_file) {
		fprintf(stderr, "You must call %s with -f parameters.\n", argv[0]);
		zsend_usage(argv[0]);
		return -1;
	}

	zc_ctl = zc_ctl_init(nr_cpus, ctl_file);
	if (!zc_ctl)
		return -1;

	memset(&alloc_ctl, 0, sizeof(struct zc_alloc_ctl));

	while (1) {
		struct zc_data *z;

		alloc_ctl.zc.size = size;
		alloc_ctl.type = 0;

		err = ioctl(zc_ctl[0].fd, ZC_ALLOC, &alloc_ctl);
		if (err) {
			fprintf(stderr, "Failed to alloc %d bytes: %s [%d].\n", size, strerror(errno), errno);
			return err;
		}

		z = &alloc_ctl.zc;

		printf("cpu: %d, ptr: %p, size: %u [%u], reserve: %u, off: %u: entry: %u.\n", 
			z->cpu, z->data.ptr, z->size, size, res_len, z->off, z->entry);
				
		if (z->entry >= ZC_MAX_ENTRY_NUM || z->cpu >= nr_cpus) {
			printf("Wrong entry, exiting.\n");
			break;
		}
		
		ctl = &zc_ctl[z->cpu];
		if (zc_prepare(ctl, z->entry))
			break;

		e = &ctl->e[z->entry];
		ptr = e->data.ptr + z->off;

		memset(ptr, 0xab, size);

		iph = ptr + res_len;
		iph->saddr = htonl(num2ip(192, 168, 4, 78));
		//iph->daddr = htonl(num2ip(192, 168, 0, 48));
		iph->daddr = htonl(num2ip(127, 0, 0, 1));
		iph->protocol = IPPROTO_UDP;
		iph->tot_len = htons(size-res_len);

		alloc_ctl.res_len = res_len;
		
		err = ioctl(zc_ctl[0].fd, ZC_COMMIT, &alloc_ctl);
		if (err) {
			fprintf(stderr, "Failed to commit %d bytes: %s [%d].\n", size, strerror(errno), errno);
			return err;
		}

		printf("Allocated and commited %d bytes, reserved %d bytes.\n", size, res_len);
		break;
	}

	return 0;
}

