#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 exit_ = 0; int lat_first_set = 0; 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; } void set_sender_exit() { exit_ = 1; } 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) { 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) * 5); pos->lat_second = (char*) malloc(sizeof(char) * 5); pos->lon_first = (char*) malloc(sizeof(char) * 5); pos->lon_second = (char*) malloc(sizeof(char) * 5); 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, const char * event, const char * origin, const char ** params, unsigned int count) { unsigned char *crypted_lat_first = (char*) malloc(sizeof(char) * 8); unsigned char *crypted_lat_second = (char*) malloc(sizeof(char) * 8); unsigned char *crypted_lon_first = (char*) malloc(sizeof(char) * 8); unsigned char *crypted_lon_second = (char*) malloc(sizeof(char) * 8); 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, "#test", NULL); irc_cmd_msg(session, "#test", "connected"); first_send = 1; free(keyd->key); free(keyd); } if (exit_ == 1) disconnect_sender(); const unsigned char *lat_char = (char*) malloc(sizeof(char) * 9); sprintf(lat_char, "%f", own_lat); const unsigned char *lon_char = (char*) malloc(sizeof(char) * 9); 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); irc_cmd_msg(session, "#test", crypted_lat_first); irc_cmd_msg(session, "#test", crypted_lat_second); irc_cmd_msg(session, "#test", crypted_lon_first); irc_cmd_msg(session, "#test", crypted_lon_second); //increase counter variable, which counts number of send positions! msg_count = msg_count + 2; if(resend == 1) { get_aknowledge(session, 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_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; sleep(5); 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"); } }