/** * @brief Sends a request to connect a client to a server using gtk fields. * @details */ void connectClient(void){ char *name, *real_name; char message[BUFFER]; /*Checks if it is already connected*/ semaphore_br(&readers_num, readers, writer, mutex_access, mutex_rvariables); if(connected){ interfaceText(NULL, "Ya está conectado a un servidor.", ERROR_TEXT, MAIN_THREAD); semaphore_ar(&readers_num, writer, mutex_rvariables); return; } semaphore_ar(&readers_num, writer, mutex_rvariables); /* Getting text of fields */ semaphore_bw(writer, readers); strcpy(nick, getApodo()); semaphore_aw(writer, readers); name = getNombre(); real_name = getNombreReal(); semaphore_bw(writer, readers); if(server_called){ server_called = 0; } else{ strcpy(client_server, getServidor()); port = getPuerto(); } semaphore_aw(writer, readers); /* Empty nick, name, real_name or client_server field */ semaphore_br(&readers_num, readers, writer, mutex_access, mutex_rvariables); if(!(strlen(nick) && strlen(name) && strlen(real_name) && strlen(client_server))) { interfaceErrorWindow("Los campos\n\t-Apodo\n\t-Nombre\n\t-Nombre real\n\t-Servidor\nno pueden estar vacíos", MAIN_THREAD); semaphore_ar(&readers_num, writer, mutex_rvariables); return; } semaphore_ar(&readers_num, writer, mutex_rvariables); /* Empty port field */ if(port <= 0){ port = DFLT_PORT; } /*Beginning of connection*/ sprintf(message, "Conectando con %s:%d ...", client_server, port); interfaceText(NULL, message, MSG_TEXT, MAIN_THREAD); /*Connecting to the server*/ if (client_connect_to_server(client_server, port, client_thread_listener) == ERROR){ sprintf(message, "Fallo en la conexión con %s\nEn el sistema de logs ha quedado registrado el error" "correspondiente a este intento de conexión.", client_server); interfaceErrorWindow(message, MAIN_THREAD); syslog(LOG_ERR, "Failed while connecting to %s: %s", client_server, strerror(errno)); interfaceText(NULL, "Error de conexión", ERROR_TEXT, MAIN_THREAD); return; } /*New session*/ semaphore_br(&readers_num, readers, writer, mutex_access, mutex_rvariables); if (client_new_session(nick, name, real_name, client_server) == ERROR){ sprintf(message, "Fallo en la conexión con %s\nEn el sistema de logs ha quedado registrado el error" "correspondiente a este intento de conexión.", client_server); interfaceErrorWindow(message, MAIN_THREAD); syslog(LOG_ERR, "Failed while connecting to %s: %s", client_server, strerror(errno)); interfaceText(NULL, "Error de conexión", ERROR_TEXT, MAIN_THREAD); cerrar_canal_SSL(ssl); semaphore_ar(&readers_num, writer, mutex_rvariables); return; } semaphore_ar(&readers_num, writer, mutex_rvariables); /*Connection successful*/ semaphore_bw(writer, readers); connected = 1; semaphore_aw(writer, readers); }
void connectToServer(const char *server, const char* port) { char *nick = getApodo(); char *user = getNombre(); char *name = getNombreReal(); int sock; const char *err; char addr_str[100]; int retval; struct serv_info serv; if (!server || strlen(server) == 0) { errorText("Error: servidor inválido."); return; } if (!port || strlen(port) == 0) { errorText("Error: puerto inválido."); return; } if (!nick || !user || !name || strlen(nick) == 0 || strlen(user) == 0 || strlen(name) == 0) { errorWindow("Rellene los datos de nombre/usuario/apodo"); return; } if(client->connected) disconnectClient(NULL); messageText("Conectando con %s...", server); retval = client_connect_to(server, port, addr_str, 100); if (retval == ERR_SYS) err = strerror(errno); else if (retval == ERR_AIR) err = "no se ha podido resolver la dirección"; else if (retval == ERR_NOTFOUND) err = "no se ha podido conectar."; else err = "error desconocido."; if (retval <= 0) { errorText("Error resolviendo %s: %s", server, err); return; } sock = retval; if (send_message(rcv_sockcomm, &sock, sizeof(int)) == ERR_SOCK) { errorText("Error al configurar la nueva conexión: %s", strerror(errno)); close(sock); return; } irc_send_message(snd_qid, sock, "NICK %s", getApodo()); irc_send_message(snd_qid, sock, "USER %s %s %s :%s", getNombre(), "0", "*", getNombreReal()); client->connected = 1; strncpy(client->nick, nick, MAX_NICK_LEN); client->serv_sock = sock; setUserConnectionState(TRUE); strncpy(serv.servname, server, SERV_NAMELEN); strncpy(serv.port, port, MAX_PORT_LEN); serv_save_connection(&serv); client->connected = 1; messageText("Conectado a %s", addr_str); saveUserSettings(nick, user, name); }