0% found this document useful (0 votes)
13 views3 pages

TCP Server with IP Geolocation Logging

The document contains a C program that implements a TCP server which retrieves geolocation information based on the client's IP address using an external API. It logs the client's IP address, geolocation data, and received data into a file. The server listens on port 2222 and sends a large response back to the client after processing the connection.

Uploaded by

Bram Truyens
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

TCP Server with IP Geolocation Logging

The document contains a C program that implements a TCP server which retrieves geolocation information based on the client's IP address using an external API. It logs the client's IP address, geolocation data, and received data into a file. The server listens on port 2222 and sends a large response back to the client after processing the connection.

Uploaded by

Bram Truyens
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <curl/curl.h>
#include <json-c/json.h>

#define BUFFER_SIZE 1024

// Struct to store geolocation information


struct GeoLocation {
char country[100];
char region[100];
char city[100];
};

// Function to retrieve geolocation using IP Geolocation API


struct GeoLocation get_geolocation(const char* ip) {
struct GeoLocation location;
char url[200];
sprintf(url, "[Link] ip);

CURL* curl;
CURLcode res;

curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();

if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);

res = curl_easy_perform(curl);

if (res != CURLE_OK) {
fprintf(stderr, "Failed to fetch geolocation data: %s\n",
curl_easy_strerror(res));
strcpy([Link], "");
strcpy([Link], "");
strcpy([Link], "");
} else {
struct json_object* json;
struct json_object* country_obj;
struct json_object* region_obj;
struct json_object* city_obj;

json = json_tokener_parse(curl_easy_strerror(res));
json_object_object_get_ex(json, "country_name", &country_obj);
json_object_object_get_ex(json, "region_name", &region_obj);
json_object_object_get_ex(json, "city", &city_obj);

strcpy([Link], json_object_get_string(country_obj));
strcpy([Link], json_object_get_string(region_obj));
strcpy([Link], json_object_get_string(city_obj));

json_object_put(json);
}
curl_easy_cleanup(curl);
}

curl_global_cleanup();
return location;
}

// TCP server to accept connections and log data


void tcp_server() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};

// Create a TCP socket


if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(2222);

// Bind the socket to the specified IP address and port


if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
perror("Failed to bind socket");
exit(EXIT_FAILURE);
}

// Start listening for incoming connections


if (listen(server_fd, 1) < 0) {
perror("Failed to listen for connections");
exit(EXIT_FAILURE);
}

printf("TCP server is listening on port 2222...\n");

while (1) {
// Accept an incoming connection
if ((new_socket = accept(server_fd, (struct sockaddr*)&address,
(socklen_t*)&addrlen)) < 0) {
perror("Failed to accept connection");
exit(EXIT_FAILURE);
}

printf("Connected with: %s:%d\n", inet_ntoa(address.sin_addr),


ntohs(address.sin_port));

// Receive data from the client


valread = recv(new_socket, buffer, BUFFER_SIZE, 0);

// Get the geolocation


struct GeoLocation location = get_geolocation(inet_ntoa(address.sin_addr));

// Log the data to a file


FILE* log_file = fopen("[Link]", "a");
if (log_file) {
fprintf(log_file, "IP address: %s\n", inet_ntoa(address.sin_addr));
fprintf(log_file, "Geolocation: %s, %s, %s\n", [Link],
[Link], [Link]);
fprintf(log_file, "Received data:\n%s\n", buffer);
fprintf(log_file, "-----------\n");
fclose(log_file);
}

// Send a large amount of data back to the client


char* response = (char*)malloc(1000000);
memset(response, 'X', 1000000);
send(new_socket, response, 1000000, 0);
free(response);

// Close the connection with the client


close(new_socket);
}
}

int main() {
tcp_server();
return 0;
}

You might also like