├── .gitignore ├── Makefile ├── README.md └── ipify.c /.gitignore: -------------------------------------------------------------------------------- 1 | ipify 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ipify: ipify.c 2 | gcc -lcurl -o ipify ipify.c 3 | 4 | test: ipify 5 | @./ipify && printf "\n\033[0;32msuccess\033[0m\n" || printf "\n \033[0;31mfail\033[0m\n" 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # API call to ipify in C using libcurl 2 | 3 | Just a quick C program making a call to [ipify](https://www.ipify.org/) and 4 | print the IP as JSON format. 5 | 6 | # Build and Run 7 | 8 | ```shell 9 | $ make && ipify 10 | ``` 11 | 12 | # Test 13 | 14 | ```shell 15 | $ make test 16 | {"ip":"167.220.196.18"} 17 | success 18 | ``` 19 | 20 | # License 21 | MIT 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /ipify.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void herror(CURL*, const char*, const char*); 6 | 7 | int main(int argc, char *argv[]) { 8 | 9 | CURL *curl; 10 | CURLcode res; 11 | curl_global_init(CURL_GLOBAL_DEFAULT); 12 | 13 | if(!(curl = curl_easy_init())) 14 | herror(curl, "error initializing curl", ""); 15 | 16 | curl_easy_setopt(curl, CURLOPT_URL, "https://api.ipify.org/?format=json"); 17 | if ((res = curl_easy_perform(curl)) != CURLE_OK) 18 | herror(curl, "error performing request: %s", curl_easy_strerror(res)); 19 | 20 | curl_global_cleanup(); 21 | exit(0); 22 | } 23 | 24 | void herror(CURL *curl, const char *tmpl, const char* err) { 25 | fprintf(stderr, tmpl, err); 26 | if (curl) curl_easy_cleanup(curl); 27 | curl_global_cleanup(); 28 | exit(1); 29 | } 30 | 31 | --------------------------------------------------------------------------------