From a952a107593d115be047fc53c9b043ea45657131 Mon Sep 17 00:00:00 2001 From: Wizzard Date: Mon, 26 Feb 2024 01:39:53 -0500 Subject: [PATCH] Update Makefile, change build --- Makefile | 8 +++-- build.sh | 1 - main.c | 86 ----------------------------------------------------- src/main.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 92 insertions(+), 90 deletions(-) delete mode 100755 build.sh delete mode 100644 main.c mode change 120000 => 100644 src/main.c diff --git a/Makefile b/Makefile index 2dd43b4..74d206d 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,9 @@ CC=gcc CFLAGS=-lcurl -lcjson +SRCDIR=src +OBJDIR=bin + +# Use variables for source and object files +ip-lookup: $(SRCDIR)/main.c + $(CC) -o $(OBJDIR)/ip-lookup $(SRCDIR)/main.c $(CFLAGS) -ip-lookup: main.c - $(CC) -o ip-lookup main.c $(CFLAGS) diff --git a/build.sh b/build.sh deleted file mode 100755 index cc1f6ec..0000000 --- a/build.sh +++ /dev/null @@ -1 +0,0 @@ -gcc -o ip-lookup main.c -lcurl -lcjson diff --git a/main.c b/main.c deleted file mode 100644 index bc5b41a..0000000 --- a/main.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include -#include -#include - -static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp) { - size_t realsize = size * nmemb; - ((char *)userp)[realsize] = 0; - strcat(userp, contents); - return realsize; -} - -int main(int argc, char *argv[]) { - if (argc != 2) { - printf("Usage: %s \n", argv[0]); - return 1; - } - - char url[256] = "http://ip-api.com/json/"; - strcat(url, argv[1]); - - CURL *curl; - CURLcode res; - char buffer[2048] = {0}; - - curl_global_init(CURL_GLOBAL_DEFAULT); - curl = curl_easy_init(); - - if (!curl) { - fprintf(stderr, "Error: Failed to initialize libcurl.\n"); - return 1; - } - - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer); - - res = curl_easy_perform(curl); - - if (res != CURLE_OK) { - fprintf(stderr, "Error: Failed to fetch data from ip-api.com: %s\n", curl_easy_strerror(res)); - curl_easy_cleanup(curl); - return 1; - } - - curl_easy_cleanup(curl); - curl_global_cleanup(); - - cJSON *json = cJSON_Parse(buffer); - if (!json) { - fprintf(stderr, "Error: Failed to parse JSON data.\n"); - return 1; - } - - cJSON *status = cJSON_GetObjectItem(json, "status"); - if (status && cJSON_IsString(status) && strcmp(status->valuestring, "fail") == 0) { - fprintf(stderr, "Error: Failed to fetch data for IP address %s\n", argv[1]); - cJSON_Delete(json); - return 1; - } - - cJSON *city = cJSON_GetObjectItem(json, "city"); - cJSON *regionName = cJSON_GetObjectItem(json, "regionName"); - cJSON *country = cJSON_GetObjectItem(json, "country"); - cJSON *timezone = cJSON_GetObjectItem(json, "timezone"); - cJSON *zip = cJSON_GetObjectItem(json, "zip"); - cJSON *isp = cJSON_GetObjectItem(json, "isp"); - cJSON *org = cJSON_GetObjectItem(json, "org"); - cJSON *as = cJSON_GetObjectItem(json, "as"); - cJSON *hostname = cJSON_GetObjectItem(json, "hostName"); - - - printf("Hostname: %s\n", hostname ? hostname->valuestring : "Unknown"); - printf("City: %s\n", city ? city->valuestring : "Unknown"); - printf("Region: %s\n", regionName ? regionName->valuestring : "Unknown"); - printf("Country: %s\n", country ? country->valuestring : "Unknown"); - printf("Time Zone: %s\n", timezone ? timezone->valuestring : "Unknown"); - printf("ZIP: %s\n", zip ? zip->valuestring : "Unknown"); - printf("ISP: %s\n", isp ? isp->valuestring : "Unknown"); - printf("Organization: %s\n", org ? org->valuestring : "Unknown"); - printf("AS: %s\n", as ? as->valuestring : "Unknown"); - - cJSON_Delete(json); - return 0; -} diff --git a/src/main.c b/src/main.c deleted file mode 120000 index cc2f2f3..0000000 --- a/src/main.c +++ /dev/null @@ -1 +0,0 @@ -/home/sweat/gitprojects/ip-lookup/main.c \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..bc5b41a --- /dev/null +++ b/src/main.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include + +static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp) { + size_t realsize = size * nmemb; + ((char *)userp)[realsize] = 0; + strcat(userp, contents); + return realsize; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + return 1; + } + + char url[256] = "http://ip-api.com/json/"; + strcat(url, argv[1]); + + CURL *curl; + CURLcode res; + char buffer[2048] = {0}; + + curl_global_init(CURL_GLOBAL_DEFAULT); + curl = curl_easy_init(); + + if (!curl) { + fprintf(stderr, "Error: Failed to initialize libcurl.\n"); + return 1; + } + + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer); + + res = curl_easy_perform(curl); + + if (res != CURLE_OK) { + fprintf(stderr, "Error: Failed to fetch data from ip-api.com: %s\n", curl_easy_strerror(res)); + curl_easy_cleanup(curl); + return 1; + } + + curl_easy_cleanup(curl); + curl_global_cleanup(); + + cJSON *json = cJSON_Parse(buffer); + if (!json) { + fprintf(stderr, "Error: Failed to parse JSON data.\n"); + return 1; + } + + cJSON *status = cJSON_GetObjectItem(json, "status"); + if (status && cJSON_IsString(status) && strcmp(status->valuestring, "fail") == 0) { + fprintf(stderr, "Error: Failed to fetch data for IP address %s\n", argv[1]); + cJSON_Delete(json); + return 1; + } + + cJSON *city = cJSON_GetObjectItem(json, "city"); + cJSON *regionName = cJSON_GetObjectItem(json, "regionName"); + cJSON *country = cJSON_GetObjectItem(json, "country"); + cJSON *timezone = cJSON_GetObjectItem(json, "timezone"); + cJSON *zip = cJSON_GetObjectItem(json, "zip"); + cJSON *isp = cJSON_GetObjectItem(json, "isp"); + cJSON *org = cJSON_GetObjectItem(json, "org"); + cJSON *as = cJSON_GetObjectItem(json, "as"); + cJSON *hostname = cJSON_GetObjectItem(json, "hostName"); + + + printf("Hostname: %s\n", hostname ? hostname->valuestring : "Unknown"); + printf("City: %s\n", city ? city->valuestring : "Unknown"); + printf("Region: %s\n", regionName ? regionName->valuestring : "Unknown"); + printf("Country: %s\n", country ? country->valuestring : "Unknown"); + printf("Time Zone: %s\n", timezone ? timezone->valuestring : "Unknown"); + printf("ZIP: %s\n", zip ? zip->valuestring : "Unknown"); + printf("ISP: %s\n", isp ? isp->valuestring : "Unknown"); + printf("Organization: %s\n", org ? org->valuestring : "Unknown"); + printf("AS: %s\n", as ? as->valuestring : "Unknown"); + + cJSON_Delete(json); + return 0; +}