summaryrefslogtreecommitdiffstats
path: root/sender/sender.c
blob: 917e9d818be664624e0efb85e19f6dff62dbceb0 (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
#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;
	}
}