summaryrefslogtreecommitdiffstats
path: root/friendfinder/sender.c
blob: 3f9c6835c2fcfc480212148ac46b0a4ca1503a9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#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"

static irc_session_t *session;
irc_callbacks_t callbacks;

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

char *nick;
char *sender_server_ip = NULL;
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()
{
	printf("SENDER: disconnected...\n");
	irc_disconnect(session);
	irc_destroy_session(session);
}

void set_sender_position(double lat, double lon)
{		
	own_lat = lat;
	own_lon = lon;
}

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)
	{
		char *_key = (char*) malloc(sizeof(char) * 300);
		_key = read_key();

		BF_set_key(&key, strlen(_key) - 1, _key);

		irc_cmd_join(session, "#test", NULL);
		irc_cmd_msg(session, "#test", "connected");
		
		first_send = 1;
	}

       	unsigned char lat_char[16];
        sprintf(lat_char, "%f", own_lat);

        unsigned char lon_char[16];
	sprintf(lon_char, "%f", own_lon);

 	BF_ecb_encrypt(lat_char, crypted_lat, &key, BF_ENCRYPT);
	BF_ecb_encrypt(lon_char, crypted_lon, &key, BF_ENCRYPT);
			
	int _x = NULL;
	_x = irc_cmd_msg(session, "#test", crypted_lat);
	int _y = NULL;
	_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("SENDER: send succesfull %s %s \n", lat_char, lon_char);
	}

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

	if (_x != 0)
	{
		printf("SENDER: error code due sending: %i \n", _x);
	}
	
	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], "_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;
			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;
	
	char username[100];
	sprintf(username, "%s", (char*) user);

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

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