From 6a962df079d8bb2d714a5d005311fa30503f5b8d Mon Sep 17 00:00:00 2001 From: Lev Dragunov Date: Wed, 21 Oct 2015 12:56:37 +0300 Subject: [PATCH] ConnectionStatus platform method for Linux. --- platform/platform_linux.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/platform/platform_linux.cpp b/platform/platform_linux.cpp index f8b5b5622d..fd220b2ccc 100644 --- a/platform/platform_linux.cpp +++ b/platform/platform_linux.cpp @@ -6,9 +6,18 @@ #include #include +#include #include #include +#include +#include + +namespace +{ + // Web service ip to check internet connection. Now it's a mail.ru ip. + char constexpr kSomeWorkingWebServer[] = "217.69.139.202"; +} // namespace /// @return directory where binary resides, including slash at the end static bool GetBinaryFolder(string & outPath) @@ -121,6 +130,22 @@ void Platform::RunAsync(TFunctor const & fn, Priority p) Platform::EConnectionType Platform::ConnectionStatus() { - // @TODO Add implementation - return EConnectionType::CONNECTION_NONE; + int socketFd = socket(AF_INET, SOCK_STREAM, 0); + struct sockaddr_in addr; + if(socketFd < 0) + return EConnectionType::CONNECTION_NONE; + + memset(&addr, '0', sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(80); + inet_pton(AF_INET, kSomeWorkingWebServer, &addr.sin_addr); + + if(connect(socketFd, (struct sockaddr *)&addr, sizeof(addr)) < 0) + { + close(socketFd); + return EConnectionType::CONNECTION_NONE; + } + + close(socketFd); + return EConnectionType::CONNECTION_WIFI; }