From 52df1e0db79d426bef26b1f6d13054b3fbe29c46 Mon Sep 17 00:00:00 2001 From: VictorH28 Date: Sat, 17 Feb 2024 18:15:11 -0500 Subject: [PATCH] Se creo una funcion para ver si tenemos internet --- src/NetworHaklab.cpp | 73 ++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/src/NetworHaklab.cpp b/src/NetworHaklab.cpp index b11036e7..d31b5147 100644 --- a/src/NetworHaklab.cpp +++ b/src/NetworHaklab.cpp @@ -1,6 +1,5 @@ #include "../include/network/NetworHaklab.h" -#include <__iterator/access.h> #include #include #include @@ -9,9 +8,8 @@ #include #include // #include -#include -#include // - +#include +#include // sing std::cout; using std::cerr; using std::endl; @@ -19,9 +17,40 @@ using std::endl; namespace beast = boost::beast; // from namespace http = beast::http; // from namespace net = boost::asio; // from +namespace fs = boost::filesystem; // from using tcp = net::ip::tcp; // from using nethak = network::NetworHakaklab; + + +// Descargar arcivos +bool network::NetworHakaklab::DownloadFile(string URL) { + CURL *curl; + FILE *fp; + CURLcode result; + curl = curl_easy_init(); + if (curl) { + fs::path outputFilename; + // Optener el nombre del archivo + fp = fopen(outputFilename.filename().c_str(), "wb"); + curl_easy_setopt(curl, CURLOPT_URL, URL.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + result = curl_easy_perform(curl); + // Verifica si la descarga se realizó correctamente + if (result != CURLE_OK) { + std::cerr << "Error a descargar archivo " << endl; + curl_easy_cleanup(curl); + fclose(fp); + return EXIT_FAILURE; + } + curl_easy_cleanup(curl); + fclose(fp); + } + return EXIT_SUCCESS; +}; + + std::set // type nethak::ListAllInterfaces() { std::set interfaces; // Conjunto para almacenar nombres únicos @@ -136,20 +165,26 @@ int nethak::GetStatusCode(const string &host, const string &port) { } // Comprobar si tenemos internet bool nethak::CheckInternet(){ - try { - // Conector E.S - net::io_context io; - tcp::resolver resolver(io); - tcp::resolver::results_type endpoints = - resolver.resolve("http://www.google.com","80"); - - // Para trabajar co E/S - tcp::socket socket(io); - net::connect(socket,endpoints); - } - catch (const std::exception& ex) { - cerr << "Error: "<< ex.what() << endl; + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, "https://google.com"); + // Solicitar descsrga sin cuerpo + curl_easy_setopt(curl, CURLOPT_NOBODY, 1); + res = curl_easy_perform(curl); + if(res == CURLE_OK) { + long response_code; + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); + if(response_code == 200) { + // Si la respuesta es 200, hay conexión a internet + std::cout << "¡Hay conexión a internet!\n"; + curl_easy_cleanup(curl); + return true; + } + } + curl_easy_cleanup(curl); + } + std::cout << "No hay conexión a internet.\n"; return false; - } - return true; }