Skip to content

Commit 919e985

Browse files
committedMar 25, 2024·
feat: display client IP
1 parent 6446ff4 commit 919e985

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed
 

‎include/unix_socket.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define UNIX_SOCKET_H_
44

55
#include <iostream>
6+
#include <cstring>
67
#include <sys/types.h>
78
#include <sys/socket.h>
89
#include <netinet/in.h>
@@ -22,16 +23,18 @@ namespace tpt
2223
unsigned int port;
2324
unsigned int max_connections;
2425
std::string ip_address;
26+
std::string client_ip;
2527

2628
public:
2729
UnixSocket();
2830
UnixSocket(unsigned int port);
2931
UnixSocket(std::string ip_address, unsigned int port);
3032
UnixSocket(std::string ip_address, unsigned int port, unsigned int max_connections);
3133
~UnixSocket();
34+
std::string getClientIp();
3235
virtual void bindSocket() override;
3336
virtual void listenToConnections() override;
34-
virtual void acceptConnection(SOCKET&client_socket, void *client_address) override;
37+
virtual void acceptConnection(SOCKET &client_socket, void *client_address) override;
3538
virtual ssize_t receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size) override;
3639
virtual void sendData(SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags) override;
3740
virtual void closeSocket() override;

‎src/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ int main(int argc, char *argv[])
88
std::cout << "Fatal error - please provide a port number!" << std::endl;
99
return EXIT_FAILURE;
1010
}
11+
1112
tpt::Teapot server = tpt::Teapot(atoi(argv[1]));
1213
tpt::CORSMiddleware cors_middleware = tpt::CORSMiddleware("*", "*", "*", 86400, true);
1314
tpt::SecurityMiddleware security_middleware;

‎src/teapot.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void Teapot::mainEventLoop(SOCKET client_socket)
8383
std::string method = request->getMethod();
8484
content_type = determineContentType(uri); // Determine content type early based on URI
8585

86-
std::cout << "[" << request->getDate() << "] " << method << " " << uri << " HTTP/1.1 ";
86+
std::cout << "[" << request->getDate() << "] " << this->socket.getClientIp() + " " << method << " " << uri << " HTTP/1.1 ";
8787

8888
if (method == "GET")
8989
{

‎src/unix_socket.cpp

+32-3
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,38 @@ void UnixSocket::listenToConnections()
109109

110110
void UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
111111
{
112-
socklen_t client_addr_size = sizeof(client_address);
113-
client_socket = accept(this->server_socket, (struct sockaddr *)&client_address, &client_addr_size);
112+
struct sockaddr_storage client_addr_storage;
113+
socklen_t client_addr_size = sizeof(client_addr_storage);
114+
115+
client_socket = accept(this->server_socket, (struct sockaddr *)&client_addr_storage, &client_addr_size);
114116
if (client_socket < 0)
115117
{
116118
perror("Accept failed");
117-
std::cout << "Error code: " + std::to_string(errno) << std::endl;
119+
std::cout << "Error code: " << std::to_string(errno) << std::endl;
118120
exit(EXIT_FAILURE);
119121
}
122+
123+
// Assuming client_address is meant to store the result
124+
if (client_address != nullptr)
125+
{
126+
std::memcpy(client_address, &client_addr_storage, client_addr_size);
127+
}
128+
129+
char ip_str[INET6_ADDRSTRLEN] = {0}; // Large enough for both IPv4 and IPv6
130+
if (client_addr_storage.ss_family == AF_INET)
131+
{
132+
// IPv4
133+
struct sockaddr_in *addr_in = (struct sockaddr_in *)&client_addr_storage;
134+
inet_ntop(AF_INET, &addr_in->sin_addr, ip_str, INET_ADDRSTRLEN);
135+
}
136+
else if (client_addr_storage.ss_family == AF_INET6)
137+
{
138+
// IPv6
139+
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&client_addr_storage;
140+
inet_ntop(AF_INET6, &addr_in6->sin6_addr, ip_str, INET6_ADDRSTRLEN);
141+
}
142+
143+
this->client_ip = std::string(ip_str);
120144
}
121145

122146
ssize_t UnixSocket::receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size)
@@ -163,6 +187,11 @@ void UnixSocket::closeSocket(SOCKET client_socket)
163187
close(client_socket);
164188
}
165189

190+
std::string UnixSocket::getClientIp()
191+
{
192+
return this->client_ip;
193+
}
194+
166195
UnixSocket::~UnixSocket() {}
167196

168197
#endif

‎src/utils.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using namespace tpt;
55
std::string Utils::readFileToBuffer(std::string filename)
66
{
77
// Use std::filesystem to handle paths correctly across different OSes
8-
std::filesystem::path filePath{ filename };
8+
std::filesystem::path filePath{filename};
99

1010
std::ifstream file(filePath); // Open the input file using the filesystem path
1111

@@ -125,4 +125,4 @@ std::string Utils::sanitizePath(const std::string &path)
125125
sanitized.erase(pos, 3);
126126
}
127127
return sanitized;
128-
}
128+
}

0 commit comments

Comments
 (0)
Please sign in to comment.