create manual internet connectivity test
This commit is contained in:
4
Makefile
4
Makefile
@ -1,11 +1,15 @@
|
||||
install: build
|
||||
install -D -m 0644 --target-directory="${HOME}/.local/bin" has_internet
|
||||
install -D -m 0644 --target-directory="${HOME}/.local/bin" target/release/Hentai add_and_download_url.sh download_website.sh
|
||||
install -D -m 0644 --target-directory="${HOME}/.config" Hentai.conf
|
||||
|
||||
build:
|
||||
gcc --std gnu23 -o has_internet has_internet.c
|
||||
strip has_internet
|
||||
cargo build --release
|
||||
strip target/release/Hentai
|
||||
|
||||
uninstall:
|
||||
rm -f "${HOME}"/.local/bin/has_internet
|
||||
rm -f "${HOME}"/.local/bin/Hentai "${HOME}"/.local/bin/add_and_download_url.sh "${HOME}"/.local/bin/download_website.sh
|
||||
rm -f "${HOME}"/.config/Hentai.conf
|
||||
|
69
has_internet.c
Normal file
69
has_internet.c
Normal file
@ -0,0 +1,69 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user