main.c (1672B)
1 #include <stdio.h> 2 #include <curl/curl.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 typedef struct s_result { 7 char* data; 8 unsigned int size; 9 } s_result; 10 11 static unsigned int save_to_char(void *contents, unsigned int size, unsigned int nmemb, void* usr_dta){ 12 unsigned int full_size; 13 struct s_result* tmp_result = (struct s_result*)usr_dta; 14 char* ralc_ptr; 15 16 full_size = size*nmemb; 17 18 ralc_ptr = realloc(tmp_result->data, tmp_result->size + full_size + 1); 19 20 if(!ralc_ptr){ 21 printf("frenchman >> out of memory\n"); 22 return 0; 23 } 24 25 tmp_result->data = ralc_ptr; 26 memcpy(&(tmp_result->data[tmp_result->size]), contents, full_size); 27 tmp_result->size += full_size; 28 tmp_result->data[tmp_result->size] = 0; 29 30 return full_size; 31 } 32 33 CURL* g_curl; 34 struct s_result result; 35 CURLcode res; 36 37 /*The number of bytes after which we see the actual IP*/ 38 #define PREFIX_OFFSET 5 39 40 int main(int argc, char** argv){ 41 char ip_addr[24]; 42 int i = 0; 43 44 memset(ip_addr,'\0',24); 45 46 g_curl = curl_easy_init(); 47 48 if(!g_curl){ 49 printf("Failed to init curl\n"); 50 return -1; 51 } 52 53 curl_easy_setopt(g_curl, CURLOPT_URL, "http://ip4only.me/api/"); 54 curl_easy_setopt(g_curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); 55 curl_easy_setopt(g_curl, CURLOPT_WRITEFUNCTION, save_to_char); 56 curl_easy_setopt(g_curl, CURLOPT_WRITEDATA, (void *)&result); 57 58 res = curl_easy_perform(g_curl); 59 60 for(i = 0;i<16;++i){ 61 62 if(result.data[i+PREFIX_OFFSET] == ',') 63 break; 64 65 ip_addr[i] = result.data[i+PREFIX_OFFSET]; 66 67 } 68 69 if(argc >= 2){ 70 if(strcmp(argv[1],"-q") == 0){ 71 printf("%s",ip_addr); 72 return 0; 73 } 74 } 75 76 printf("\n\tYour public IPV4 Address: %s\n\n",ip_addr); 77 return 0; 78 }