summaryrefslogtreecommitdiffstats
path: root/friendfinder/sender.c
blob: 012df129b4d2fe60a8dfd9361893d78edf02623a (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#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_sender;
irc_callbacks_t callbacks;

int lat_first_set = 0;

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

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

int first_send = 0;

BF_KEY key;

#define CRYPT_LENGTH 10
#define BASE64_LENGTH 60

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_sender = irc_create_session(&callbacks);
	int con = irc_connect(session_sender, server_ip, 6669, NULL, user, user, user);
	
	if (irc_is_connected(session_sender) == 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_sender);
	irc_destroy_session(session_sender);

	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_sender, const char * event, const char * origin, const char ** params, unsigned int count)
{
	unsigned char *crypted_lat_first = (char*) malloc(sizeof(char) * CRYPT_LENGTH);
	unsigned char *crypted_lat_second = (char*) malloc(sizeof(char) * CRYPT_LENGTH);
	unsigned char *crypted_lon_first = (char*) malloc(sizeof(char) * CRYPT_LENGTH);
	unsigned char *crypted_lon_second = (char*) malloc(sizeof(char) * CRYPT_LENGTH);

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

	

	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_sender, "#test", NULL);
		irc_cmd_msg(session_sender, "#test", "connected");
		
		first_send = 1;

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

	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);

/*	crypted_lat_first[CRYPT_LENGTH] = '\0';
	crypted_lat_second[CRYPT_LENGTH] = '\0';
	crypted_lon_first[CRYPT_LENGTH] = '\0';
	crypted_lon_second[CRYPT_LENGTH] = '\0';*/
	
	to_base64(crypted_lat_first, CRYPT_LENGTH, crypted_lat_first_base64, BASE64_LENGTH);
	to_base64(crypted_lat_second, CRYPT_LENGTH, crypted_lat_second_base64, BASE64_LENGTH);

	to_base64(crypted_lon_first, CRYPT_LENGTH, crypted_lon_first_base64, BASE64_LENGTH);
	to_base64(crypted_lon_second, CRYPT_LENGTH, crypted_lon_second_base64, BASE64_LENGTH);
	
	irc_cmd_msg(session_sender, "#test", crypted_lat_first_base64);

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

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

	msg_count = msg_count + 4;

	if (resend == 1)
	{
		get_acknowledge(session_sender, 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_acknowledge(irc_session_t * session_sender, const char * event, const char * origin, const char ** params, unsigned int count)
{
	char* ack = (char*) malloc(sizeof(char) * 20);
	char* ack_base64 = (char*) malloc(sizeof(char) * BASE64_LENGTH);

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

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

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

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

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

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

	callbacks.event_connect = send_position;
	callbacks.event_channel = get_acknowledge;

	char* _user = (char*) user;
	int len = strlen(_user) + 1;
	
	username = (char*) malloc(len + 2);
	suffix_username = (char*) malloc(len + 2);

	memcpy(username, user, len);
	memcpy(suffix_username, user, len);

	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_sender);
		}
	}
	
	else
	{
		printf("SENDER: error ip not set! Sender can't be started\n");
	}
}