13 FILE* file =
popen(
"hostname -I",
"r");
20 if (fgets(buffer,
sizeof(buffer), file) == NULL)
27 char* newline = strchr(buffer,
'\n');
30 strcpy(ip_addr, buffer);
35 FILE* file =
popen(
"curl -s ifconfig.me",
"r");
42 if (fgets(buffer,
sizeof(buffer), file) == NULL)
49 char* newline = strchr(buffer,
'\n');
52 strcpy(ip_addr, buffer);
59 char ipstr[INET6_ADDRSTRLEN];
62 memset(&hints, 0,
sizeof(hints));
66 if ((status =
getaddrinfo(lookup_addr, NULL, &hints, &res)) != 0)
68 fprintf(stderr,
"getaddrinfo: %s\n",
gai_strerror(status));
73 for (p = res; p != NULL; p = p->
ai_next)
78 struct sockaddr_in* ipv4 = (
struct sockaddr_in*)p->
ai_addr;
79 addr = &(ipv4->sin_addr);
82 if (strcmp(inet_ntoa(ipv4->sin_addr),
"0.0.0.0") == 0)
84 inet_ntop(p->
ai_family, addr, ipstr,
sizeof(ipstr));
85 strcat(ip_addr, ipstr);
91 if (strlen(ip_addr) > 0)
92 ip_addr[strlen(ip_addr) - 1] =
'\0';
107 unsigned int b1, b2, b3, b4;
110 if (sscanf(ip_addr,
"%u.%u.%u.%u", &b1, &b2, &b3, &b4) != 4)
112 if ((b1 == 192 && b2 == 168) ||
114 (b1 == 172 && b2 >= 16 && b2 <= 31))
124 for (
size_t i = 0; i < strlen(ip_addr); ++i)
126 if (ip_addr[i] ==
'.')
128 else if (ip_addr[i] <
'0' || ip_addr[i] >
'9')
138 if (domain_addr == NULL)
142 if (strstr(domain_addr,
".") == NULL)
void get_local_ip_address(char *ip_addr)
Get the local IP address of the machine (e.g.
int lookup_address(const char *lookup_addr, char *ip_addr)
Perform lookup of given IP address by the domain address (e.g.
int is_numeric_address(const char *ip_addr)
Check if the IP address is a numeric address (e.g.
int is_in_private_network(const char *ip_addr)
Check if the IP address is in the private prefix (e.g.
int is_valid_domain_address(const char *domain_addr)
Check if the IP address is a valid domain address (e.g.
void get_remote_ip_address(char *ip_addr)
Get the remote IP address of the machine (e.g.
void freeaddrinfo(struct addrinfo *res)
const char * gai_strerror(int errcode)
int getaddrinfo(const char *restrict node, const char *restrict service, const struct addrinfo *restrict hints, struct addrinfo **restrict res)
void log_message(log_level level, const char *filename, const char *source_file, const char *format,...)
Log a message used to log a message to the console or a file.
struct sockaddr * ai_addr
struct addrinfo * ai_next
FILE * popen(const char *command, const char *type)