#include #include #include #include #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"); } }