#include #include #include #include #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_lat = (char*) malloc(sizeof(char) * 9); // unsigned char crypted_lon[64]; unsigned char *crypted_lon = (char*) malloc(sizeof(char) * 9); 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; //free(key); } // unsigned char lat_char[16]; unsigned char *lat_char = (char*) malloc(sizeof(char) * 9); sprintf(lat_char, "%f", own_lat); // unsigned char lon_char[16]; unsigned char *lon_char = (char*) malloc(sizeof(char) * 9); 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; } free(lat_char); free(lon_char); free(crypted_lat); free(crypted_lon); } 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"); } }