summaryrefslogtreecommitdiff
path: root/ip.c
diff options
context:
space:
mode:
authorAleksei <email@email.email>2022-07-14 17:31:47 +0300
committerAleksei <email@email.email>2022-07-14 17:31:47 +0300
commit62d382f913bfdb5f9bf724b91d013e51202f09ef (patch)
treef5c9061d2810d8bcf12645591fe89670708a90ff /ip.c
Diffstat (limited to 'ip.c')
-rw-r--r--ip.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/ip.c b/ip.c
new file mode 100644
index 0000000..da99afd
--- /dev/null
+++ b/ip.c
@@ -0,0 +1,39 @@
+#include <sys/types.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include "ip.h"
+#define HNAME "rskrf.ru"
+static struct addrinfo hints;
+static struct addrinfo *result, *rp;
+static char ip[INET_ADDRSTRLEN];
+static int s;
+char *get_ip(size_t len)
+{
+ if (len != INET_ADDRSTRLEN) {
+ fprintf(stderr, "get_ip: len must be %d\n", INET_ADDRSTRLEN);
+ exit(EXIT_FAILURE);
+ }
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET;
+ hints.ai_flags = AI_ADDRCONFIG; /* ret IPv4 only if system has IPv4 addr */
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+ s = getaddrinfo(HNAME, "https", &hints, &result);
+ if (s != 0) {
+ fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
+ exit(EXIT_FAILURE);
+ }
+ for (rp = result; rp != NULL; rp = rp->ai_next) {
+ inet_ntop(AF_INET,
+ &((struct sockaddr_in *)(rp->ai_addr))->sin_addr,
+ ip, INET_ADDRSTRLEN);
+ }
+ freeaddrinfo(result);
+ return ip;
+}