#include #include #include #include #include #include #include bool has_internet() { int ret; // https://www.man7.org/linux/man-pages/man3/getaddrinfo.3.html struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = 0; hints.ai_protocol = 0; struct addrinfo *res, *rp; ret = getaddrinfo("hopeless-cloud.xyz", "80", 0, &res); if (ret != 0) { // fprintf(stderr, "Couldn't getaddrinfo()\n"); return false; } int sockfd; for (rp = res; rp != NULL; rp = rp->ai_next) { // https://www.man7.org/linux/man-pages/man2/socket.2.html sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sockfd == -1) { continue; } // https://www.man7.org/linux/man-pages/man2/connect.2.html if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0) { break; } close(sockfd); } freeaddrinfo(res); if (rp == NULL) { // fprintf(stderr, "Couldn't connect socket\n"); return false; } // https://www.man7.org/linux/man-pages/man2/write.2.html char *req_msg = "GET / HTTP/1.1\r\n\r\n"; if (write(sockfd, req_msg, strlen(req_msg)) != strlen(req_msg)) { // fprintf(stderr, "Couldn't fully write to socket\n"); return false; } // https://www.man7.org/linux/man-pages/man2/recv.2.html size_t count = 1000000; char buf[count]; if (recv(sockfd, buf, count-1, MSG_WAITALL) == -1 ) { // fprintf(stderr, "Couldn't read from socket\n"); return false; } // printf("%s\n", buf); close(sockfd); return true; } int main() { if (has_internet()) { // printf("has internet\n"); return 0; } else { // printf("no internet\n"); return 1; } }