Skip to content

Instantly share code, notes, and snippets.

@incebellipipo
Created September 13, 2018 13:14
Show Gist options
  • Save incebellipipo/ca0c42f9d175d36673154291cb006657 to your computer and use it in GitHub Desktop.
Save incebellipipo/ca0c42f9d175d36673154291cb006657 to your computer and use it in GitHub Desktop.
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <cerrno>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <curl/curl.h>
#include <ifaddrs.h>
#include <net/if.h>
bool check_connection(char* ifname, char* test_server){
CURL *curl = curl_easy_init();
CURLcode res;
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, test_server);
curl_easy_setopt(curl, CURLOPT_INTERFACE, ifname);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
return (res == CURLE_OK);
}
int main(int argc, char** argv){
if(argc < 2){
printf("Need a argument to be test server\n");
exit(EXIT_FAILURE);
}
struct ifaddrs *ifaddr, *ifa;
if(getifaddrs(&ifaddr) == -1){
perror("getifaddrs()");
exit(EXIT_FAILURE);
}
for(ifa = ifaddr; ifa != nullptr ; ifa = ifa->ifa_next) {
bool has_it = false;
if(ifa->ifa_addr->sa_family == AF_PACKET){
has_it = check_connection(ifa->ifa_name, argv[1]);
} else {
continue;
}
printf("%*s: %s\n",
IFNAMSIZ,
ifa->ifa_name,
has_it ? "[Route to internet]" : "[-----------------]"
);
}
freeifaddrs(ifaddr);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment