Skip to content

Commit dafcc65

Browse files
author
Sebastiano Merlino
committed
avoid usage of ntop and pton functions.
1 parent b6dc904 commit dafcc65

File tree

3 files changed

+29
-48
lines changed

3 files changed

+29
-48
lines changed

src/http_utils.cpp

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
#include <winsock2.h>
2929
#include <ws2tcpip.h>
3030
#else
31-
#include <arpa/inet.h>
31+
#include <sys/socket.h>
32+
#include <netdb.h>
3233
#endif
3334
#include <sstream>
3435
#include <iomanip>
@@ -244,36 +245,8 @@ void get_ip_str(
244245
{
245246
if(sa)
246247
{
247-
char to_ret[INET6_ADDRSTRLEN] = { '\0' };
248-
switch(sa->sa_family)
249-
{
250-
case AF_INET:
251-
if(maxlen == 0)
252-
maxlen = INET_ADDRSTRLEN;
253-
254-
inet_ntop(AF_INET,
255-
&(((struct sockaddr_in *)sa)->sin_addr),
256-
to_ret,
257-
maxlen
258-
);
259-
260-
break;
261-
262-
case AF_INET6:
263-
if(maxlen == 0)
264-
maxlen = INET6_ADDRSTRLEN;
265-
266-
inet_ntop(AF_INET6,
267-
&(((struct sockaddr_in6 *)sa)->sin6_addr),
268-
to_ret,
269-
maxlen
270-
);
271-
272-
break;
273-
default:
274-
strncpy(to_ret, "Unknown AF", 11);
275-
return;
276-
}
248+
char to_ret[NI_MAXHOST];
249+
getnameinfo(sa, sizeof (struct sockaddr), to_ret, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
277250
result = to_ret;
278251
}
279252
}
@@ -288,20 +261,6 @@ std::string get_ip_str_new(
288261
return to_ret;
289262
}
290263

291-
const struct sockaddr str_to_ip(const std::string& src)
292-
{
293-
struct sockaddr s;
294-
if(src.find(":") != std::string::npos)
295-
{
296-
inet_pton(AF_INET6, src.c_str(), (void*) &s);
297-
}
298-
else
299-
{
300-
inet_pton(AF_INET, src.c_str(), (void*) &s);
301-
}
302-
return s;
303-
}
304-
305264
short get_port(const struct sockaddr* sa)
306265
{
307266
if(sa)

src/httpserver/http_utils.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ void dump_header_map(std::ostream &os, const std::string &prefix,
347347
**/
348348
void dump_arg_map(std::ostream &os, const std::string &prefix,
349349
const std::map<std::string,std::string,arg_comparator> &map);
350-
350+
351351
/**
352352
* Process escape sequences ('+'=space, %HH) Updates val in place; the
353353
* result should be UTF-8 encoded and cannot be larger than the input.
@@ -359,8 +359,6 @@ void dump_arg_map(std::ostream &os, const std::string &prefix,
359359
*/
360360
size_t http_unescape (char *val);
361361

362-
const struct sockaddr str_to_ip(const std::string& src);
363-
364362
char* load_file (const char *filename);
365363

366364
size_t load_file (const char* filename, char** content);

test/unit/http_utils_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@
1818
USA
1919
*/
2020

21+
#if defined(__MINGW32__) || defined(__CYGWIN32__)
22+
#define _WINDOWS
23+
#undef _WIN32_WINNT
24+
#define _WIN32_WINNT 0x600
25+
#include <winsock2.h>
26+
#include <ws2tcpip.h>
27+
#else
28+
#include <arpa/inet.h>
29+
#endif
30+
2131
#include "littletest.hpp"
2232
#include "http_utils.hpp"
33+
2334
#include <cstdio>
2435

2536
using namespace httpserver;
@@ -69,6 +80,19 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, standardize_url)
6980
LT_CHECK_EQ(result, "/abc/pqr");
7081
LT_END_AUTO_TEST(standardize_url)
7182

83+
LT_BEGIN_AUTO_TEST(http_utils_suite, ip_to_str)
84+
struct sockaddr_in ip4addr;
85+
86+
ip4addr.sin_family = AF_INET;
87+
ip4addr.sin_port = htons(3490);
88+
ip4addr.sin_addr.s_addr = inet_addr("127.0.0.1");
89+
90+
string result = "";
91+
http::get_ip_str((struct sockaddr *) &ip4addr, result);
92+
93+
LT_CHECK_EQ(result, "127.0.0.1");
94+
LT_END_AUTO_TEST(ip_to_str)
95+
7296
LT_BEGIN_AUTO_TEST_ENV()
7397
AUTORUN_TESTS()
7498
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)