summaryrefslogblamecommitdiffstats
path: root/friendfinder/sender.c
blob: 4daeb33852475816481370584035f94aefd27485 (plain) (tree)
1
2
3
4
5
6
7
8
9






                             
                   
                      
                      


                              
 


                      


                  
 
                                                    
                        

                   


           




                                             
                                                       
 
                                                       
        
                                                 
                                                                                


                                           
                                                  
                                



                     
                                                                    



                         
                        
 

                                     

                                            

 
                                                
                












                                                                                                                                     





                                                                                           

                                                           
        

                                                           






























                                                                                                     

                                  
 

                                                             

                        

 

                                                                                                                              
                                                                            
                                                                             
                                                                            
                                                                             
 





                                                                                     


                            


                                                                                           
 

                                                                
                                                     
                                                           

                               


                                

         


                                    
                                                                          
                                         
 
                                                                          
                                         
 








                                                                                           







                                                                                                 
 
                                                                 
 
                                                                        
        
                                                                 
 

                                                                           
 





                                                                      


                       





                                 





                              



                                                                                                                               









                                                                       


                                                          

                                                                      
                                                                                                                                  


                                              

                                                                                                                                       
                                                    




                                    
                                                     

                                   
                
                                 
 




                                                                             
                            





                                                 




                                                            
 
                                     
         
                                              
 
                                                                                   

                                                                     






                                                                              
         

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

#include "libircclient.h"
#include "openssl/blowfish.h"
#include "sender.h"
#include "read_file.h"
#include "cryptossl.h"

static irc_session_t *session;
irc_callbacks_t callbacks;

int exit_ = 0;
int lat_first_set = 0;

int msg_count = 0;
int ack_count = 0;
int resend = 0;

char *sender_server_ip, *username, *suffix_username;
double own_lat, own_lon;

int first_send = 0;

BF_KEY key;

void sender_set_ip(char *_sender_server_ip)
{
	sender_server_ip = _sender_server_ip;
}

int init_connection_sender(char* server_ip, char* user)
{
	printf("SENDER: initialising connection...\n");
	
	session = irc_create_session(&callbacks);
	int con = irc_connect(session, server_ip, 6669, NULL, user, user, user);
	
	if (irc_is_connected(session) == 1)
	{
		printf("SENDER: connected... \n");
		return 0;	
	}

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

void disconnect_sender()
{
	irc_disconnect(session);
	irc_destroy_session(session);

	printf("SENDER: disconnected...\n");
}

void set_sender_position(double lat, double lon)
{		
	if (lat_first_set = 0)
	{
		own_lat = lat;
		own_lon = lon;
	
		lat_first_set = 1;
	}

	if (((own_lat - lat) > 0.000130 || (own_lat - lat) < 0.000130) || ((own_lon - lon) > 0.000130 || (own_lon - lon) < 0.000130))
	{
		own_lat = lat;
		own_lon = lon;
	}
}

struct sender_data* prepare_position(const unsigned char* lat, const unsigned char* lon)
{
	struct sender_data *pos = (struct sender_data*) malloc(sizeof(struct sender_data));

	pos->lat_first = (char*) malloc(sizeof(char) * 8);
	pos->lat_second = (char*) malloc(sizeof(char) * 8);
	
	pos->lon_first = (char*) malloc(sizeof(char) * 8);
	pos->lon_second = (char*) malloc(sizeof(char) * 8);

	for (int i = 0; i < 9; i++)
	{
		if (i < 5)		
			*(pos->lat_first + i) =  *(lat + i);

		if (i >= 5 && i < 9)
			*(pos->lat_second + i - 5) =  *(lat + i);
	}

//TODO: variable position mit strlen einfügen und fehler fixen das manchmal nichts drangehängt wird
	pos->lat_first[5] = 'a';
	pos->lat_first[6] = '\0';

	pos->lat_second[4] = 'b';
	pos->lat_second[5] = '\0';


	for (int j = 0; j < 9; j++)
	{
		if (j < 5)	
			*(pos->lon_first + j) = *(lon + j);

		if (j >=5 && j < 9)
			*(pos->lon_second + j - 5) = *(lon + j);		
	}

//TODO: variable position mit strlen einfügen
	pos->lon_first[5] = 'c';
	pos->lon_first[6] = '\0';

	pos->lon_second[3] = 'd';
	pos->lon_second[4] = '\0';

//	printf(" %s %s \n", pos->lat_first, pos->lat_second);
//	printf(" %s %s \n", pos->lon_first, pos->lon_second);

	return pos;	
}

void send_position(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
	unsigned char *crypted_lat_first = (char*) malloc(sizeof(char) * 9);
	unsigned char *crypted_lat_second = (char*) malloc(sizeof(char) * 9);
	unsigned char *crypted_lon_first = (char*) malloc(sizeof(char) * 9);
	unsigned char *crypted_lon_second = (char*) malloc(sizeof(char) * 9);

	unsigned char *crypted_lat_first_base64 = (char*) malloc(sizeof(char) * 50);
	unsigned char *crypted_lat_second_base64 = (char*) malloc(sizeof(char) * 50);
	unsigned char *crypted_lon_first_base64 = (char*) malloc(sizeof(char) * 50);
	unsigned char *crypted_lon_second_base64 = (char*) malloc(sizeof(char) * 50);

	

	if (first_send == 0)
	{
		struct key_data *keyd = (struct key_data*) malloc(sizeof(struct key_data));
		keyd->key = (char*) malloc(sizeof(char) * 300);	
		keyd = read_key();

		BF_set_key(&key, keyd->key_length, keyd->key);	
	
		irc_cmd_join(session, "#test", NULL);
		irc_cmd_msg(session, "#test", "connected");
		
		first_send = 1;

		free(keyd->key);
		free(keyd);
	}

	if (exit_ == 1)
		disconnect_sender();

	const unsigned char *lat_char = (char*) malloc(sizeof(char) * 10);
        sprintf(lat_char, "%f", own_lat);

	const unsigned char *lon_char = (char*) malloc(sizeof(char) * 10);
	sprintf(lon_char, "%f", own_lon);

	struct sender_data *pos = (struct sender_data*) malloc(sizeof(struct sender_data));
	
	pos = prepare_position(lat_char, lon_char);

 	BF_ecb_encrypt(pos->lat_first, crypted_lat_first, &key, BF_ENCRYPT);
 	BF_ecb_encrypt(pos->lat_second, crypted_lat_second, &key, BF_ENCRYPT);

 	BF_ecb_encrypt(pos->lon_first, crypted_lon_first, &key, BF_ENCRYPT);
	BF_ecb_encrypt(pos->lon_second, crypted_lon_second, &key, BF_ENCRYPT);
	
	to_base64(crypted_lat_first, strlen(crypted_lat_first), crypted_lat_first_base64, 50);
	to_base64(crypted_lat_second, strlen(crypted_lat_second), crypted_lat_second_base64, 50);

	to_base64(crypted_lon_first, strlen(crypted_lon_first), crypted_lon_first_base64, 50);
	to_base64(crypted_lon_second, strlen(crypted_lon_second), crypted_lon_second_base64, 50);
	
	irc_cmd_msg(session, "#test", crypted_lat_first_base64);

	irc_cmd_msg(session, "#test", crypted_lat_second_base64);

	irc_cmd_msg(session, "#test", crypted_lon_first_base64); 	
	
	irc_cmd_msg(session, "#test", crypted_lon_second_base64);

	//increase counter variable, which counts number of send positions!
	msg_count = msg_count + 2;

	if(resend == 1)
	{
		get_aknowledge(session, event, origin, params, count);
		resend = 0;
		ack_count = 0;
	}

	free(lat_char);
	free(lon_char);

	free(crypted_lat_first);
	free(crypted_lat_second);
	
	free(crypted_lon_first);
	free(crypted_lon_second);

	free(pos->lon_first);
	free(pos->lon_second);
	free(pos->lat_first);
	free(pos->lat_second);
	free(pos);
}

void get_aknowledge(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
	char* ack = (char*) malloc(sizeof("ack"));
	char* ack_base64 = (char*) malloc(sizeof(char) * 50);

	char *sender_name = (char*) malloc(sizeof(char) * 50);
	char *own_receiver_name = (char*) malloc(sizeof(username) + 2);

	memcpy(own_receiver_name, username, strlen(username));
	strcat(own_receiver_name, "_r");

	from_base64(params[1], strlen(params[1]), ack_base64, 50);

	BF_ecb_encrypt(ack_base64, ack, &key, BF_DECRYPT);
	
	irc_target_get_nick(origin, sender_name, sizeof(sender_name));

	if(strcmp(suffix_username, sender_name) != 0 && strcmp(ack, "ack") == 0 && strcmp(sender_name, own_receiver_name) != 0 && 
		strcmp(sender_name, "_r") > 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 <= 1 && msg_count > 0)
		{	
			ack_count++;
			msg_count--;
		}
		
		if (ack_count == 2 && msg_count == 0)
		{	
			resend = 1;
		
			sleep(3);

			send_position(session, event, origin, params, count);
		}
	}
}

void sender_main(void *user)
{
        memset(&callbacks, 0, sizeof(callbacks));

	callbacks.event_connect = send_position;
	callbacks.event_channel = get_aknowledge;
	
	username = (char*) malloc(sizeof(char) * 50);
	suffix_username = (char*) malloc(sizeof(char) * 50);

	memcpy(username, (char*) user, sizeof(user));
	suffix_username = (char*) user;

	if (sender_server_ip != NULL)
	{
		strcat(suffix_username, "_s");

		if (init_connection_sender(sender_server_ip, suffix_username) == 0)
		{
			printf("SENDER: connection succesfull...\n");
			irc_run(session);
		}
	}
	
	else
	{
		printf("SENDER: error ip not set! Sender can't be started\n");
	}
}