#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include "trans.h"

/* proof-of-concept program */

/* 
 * This program 'thinks' it's communicating over IPv4, however instead
 * of 'sendto' it uses 'sendtov6', which transparently translates it to
 * IPv6.
 *
 * This program is used for testing the implementation of 'sendtov6'. In
 * the long run we're planning to use this function in a linux kernel module,
 * overloading 'sendto', allowing the kernel to transparently translate all
 * ipv4 traffic to ipv6.
 *
 * The advantage of this over ipv4-in-ipv6-tunneling is that no ipv4 address 
 * is needed for the client machines...
 *
 * It sends an IPv6 udp packet to the nameserver 131.174.60.21 asking for
 * the A record of www.bzzt.com. 
 */

int main ()
{
	int sock;
	int sent;
	struct sockaddr_in theiraddr;
	uint8_t data [30];
	data[0] = 0xec;
	data[1] = 0x48;
	data[2] = 0x01;
	data[3] = 0x00;
	data[4] = 0x00;
	data[5] = 0x01;
	data[6] = 0;
	data[7] = 0;
	data[8] = 0;
	data[9] = 0;
	data[10] = 0;
	data[11] = 0;
	data[12] = 0x03;
	data[13] = 0x77;
	data[14] = 0x77;
	data[15] = 0x77;
	data[16] = 0x04;
	data[17] = 0x62;
	data[18] = 0x65;
	data[19] = 0x65;
	data[20] = 0x72;
	data[21] = 0x03;
	data[22] = 0x63;
	data[23] = 0x6f;
	data[24] = 0x6d;
	data[25] = 0;
	data[26] = 0;
	data[27] = 1;
	data[28] = 0;
	data[29] = 1;

	if (initsocketv6() == -1)
	{
		fprintf(stderr, "error initializing v6 socket code\n");
	}

	/* create the IPv4 UDP socket */
	if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
	{
		perror("socket");
	}

	/* make the IPv4 sockaddr_in for the local side */
	bzero(&theiraddr, sizeof(struct sockaddr_in));
	theiraddr.sin_family = AF_INET;
	theiraddr.sin_port = htons(53);
	if ((theiraddr.sin_addr.s_addr = inet_addr("131.174.60.21")) == -1)
	{
		perror("Couldn't convert address to binary network byte order address");
	}

	/* send an UDP packet to an IPv4 host */
	if ((sent = sendtov6(sock, data, 30, 0, (struct sockaddr *)&theiraddr, sizeof(struct sockaddr))) == -1)
	{
		perror("sendto");
		return 0;
	}

	printf("Sent %d bytes\n", sent);
	return 0;
}

