summaryrefslogblamecommitdiffstats
path: root/sender/sender.c
blob: 917e9d818be664624e0efb85e19f6dff62dbceb0 (plain) (tree)















































































































































































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

#include "libircclient.h"
#include "openssl/blowfish.h"

static irc_session_t *session;
irc_callbacks_t callbacks;
int msg_count = 0;
int ack_count = 0;
int resend = 0;
char* nick;

int first_send = 0;

BF_KEY key;

int init_connection(char* server_ip, char* user)
{
	printf("initialising connection...\n");

	session = irc_create_session(&callbacks);
	int con = irc_connect(session, server_ip, 6667, NULL, user, user, user);
	
	if (irc_is_connected(session) == 1)
	{
		printf("connected... \n");
		return 0;
	
	}

	if (con != 0)
	{
		printf("connection error-code: %i \n", con);
		return 1;
	}
}

void disconnect()
{
	printf("disconnected...\n");
	irc_disconnect(session);
	irc_destroy_session(session);
}

//create random position between two y coordinates. bb_y_left < bb_y_right, bb_y_left = 0!
int random_position_y(int bb_y_right)
{
	return rand() % bb_y_right;
}

//create random position between two x coordinates. bb_x_lower < bb_x_upper, bb_x_lower = 0!
int random_position_x(int bb_x_upper)
{
	return rand() % bb_x_upper;
}

void send_position(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{

	unsigned char crypted_lat[64];
	unsigned char crypted_lon[64];

	if (first_send == 0)
	{
		BF_set_key(&key, 5, "abcde");

		irc_cmd_join(session, "#test", NULL);	
	}

	for(int i = 0; i < 2; i++)
	{
		int lat = random_position_x(1000);
		printf("lat position %i \n", lat);
		int lon = random_position_y(1000);
		printf("lon position %i \n", lon);

                unsigned char lat_char[8];
                sprintf(lat_char, "%i", lat);

                unsigned char lon_char[8];
                sprintf(lon_char, "%i", lon);

 		BF_ecb_encrypt(lat_char, crypted_lat, &key, BF_ENCRYPT);
		BF_ecb_encrypt(lon_char, crypted_lon, &key, BF_ENCRYPT);
		
		BF_ecb_encrypt(crypted_lat, lat_char, &key, BF_DECRYPT);
		printf("self-test: %s \n", lat_char);
			
		int _x = irc_cmd_msg(session, "#test", crypted_lat);
		int _y = irc_cmd_msg(session, "#test", crypted_lon); 	
	
		//increase counter variable, which counts number of send positions!
		msg_count = msg_count + 2;

		if (_x == 0 && _y == 0)
		{
			printf("send succesfull %i %i \n", lat, lon);
		}

		if (_y != 0)
		{	
			printf("error code due sending: %i \n", _y);
		}

		if (_x != 0)
		{
			printf("error code due sending: %i \n", _x);
		}
	
		first_send = 1;
	}
	
	if(resend == 1)
	{
		get_aknowledge(session, event, origin, params, count);
		resend = 0;
		ack_count = 0;
	}
}

void get_aknowledge(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
	if(strcmp(params[1], nick) == 0)
	{	
		//use count variable, to count number of aknowledged positions! second number has to be twice as big, as the number of 
		//for-runs in send_position
		if (ack_count <= 3 && msg_count > 0)
		{	
			ack_count++;
			msg_count--;
		}
		
		if (ack_count == 4 && msg_count == 0)
		{	
			resend = 1;
			send_position(session, event, origin, params, count);
		}
	}
}

int main (int argc, char **argv)
{
        memset(&callbacks, 0, sizeof(callbacks));

	callbacks.event_connect = send_position;
	callbacks.event_channel = get_aknowledge;
	
	nick = argv[1];

 	if (init_connection("127.0.0.1", argv[1]) == 0)
	{
		int ses = irc_run(session);
		if (ses == 0)
		{
			printf("session started...\n");
		}	
		else
		{
			printf("error due starting session: %i \n", ses);
		}
		printf("done \n");

		return 0;
	}

	else 
	{
		printf("aborting...\n");
		return 1;
	}
}