Skip to content

Commit e289db1

Browse files
committed
PublicIp: improve performance
1 parent e7c9b80 commit e289db1

File tree

5 files changed

+52
-20
lines changed

5 files changed

+52
-20
lines changed

src/common/init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ static void exitSignalHandler(int signal)
359359
void ffStart(FFinstance* instance)
360360
{
361361
ffPrepareCPUUsage();
362+
ffPreparePublicIp(instance);
362363

363364
if(instance->config.multithreading)
364365
startDetectionThreads(instance);

src/common/networking.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,38 @@
66
#include <sys/socket.h>
77
#include <netdb.h>
88

9-
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer)
9+
int ffNetworkingSendHttpRequest(const char* host, const char* path, uint32_t timeout)
1010
{
11-
struct addrinfo hints = {0};
12-
hints.ai_family = AF_INET;
13-
hints.ai_socktype = SOCK_STREAM;
11+
struct addrinfo hints = {
12+
.ai_family = AF_INET,
13+
.ai_socktype = SOCK_STREAM,
14+
};
1415

1516
struct addrinfo* addr;
1617

1718
if(getaddrinfo(host, "80", &hints, &addr) != 0)
18-
return;
19+
return -1;
1920

20-
int sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
21-
if(sock == -1)
21+
int sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
22+
if(sockfd == -1)
2223
{
2324
freeaddrinfo(addr);
24-
return;
25+
return -1;
2526
}
2627

2728
if(timeout > 0)
2829
{
2930
struct timeval timev;
3031
timev.tv_sec = 0;
3132
timev.tv_usec = (__typeof__(timev.tv_usec)) (timeout * 1000); //milliseconds to microseconds
32-
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timev, sizeof(timev));
33+
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timev, sizeof(timev));
3334
}
3435

35-
if(connect(sock, addr->ai_addr, addr->ai_addrlen) == -1)
36+
if(connect(sockfd, addr->ai_addr, addr->ai_addrlen) == -1)
3637
{
37-
close(sock);
38+
close(sockfd);
3839
freeaddrinfo(addr);
39-
return;
40+
return -1;
4041
}
4142

4243
freeaddrinfo(addr);
@@ -49,21 +50,32 @@ void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, F
4950
ffStrbufAppendS(&command, host);
5051
ffStrbufAppendS(&command, "\r\n\r\n");
5152

52-
if(send(sock, command.chars, command.length, 0) == -1)
53+
if(send(sockfd, command.chars, command.length, 0) == -1)
5354
{
5455
ffStrbufDestroy(&command);
55-
close(sock);
56-
return;
56+
close(sockfd);
57+
return -1;
5758
}
59+
ffStrbufDestroy(&command);
60+
return sockfd;
61+
}
5862

59-
ssize_t received = recv(sock, buffer->chars + buffer->length, ffStrbufGetFree(buffer), 0);
63+
void ffNetworkingRecvHttpResponse(int sockfd, FFstrbuf* buffer)
64+
{
65+
ssize_t received = recv(sockfd, buffer->chars + buffer->length, ffStrbufGetFree(buffer), 0);
6066

6167
if(received > 0)
6268
{
6369
buffer->length += (uint32_t) received;
6470
buffer->chars[buffer->length] = '\0';
6571
}
6672

67-
ffStrbufDestroy(&command);
68-
close(sock);
73+
close(sockfd);
74+
}
75+
76+
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer)
77+
{
78+
int sockfd = ffNetworkingSendHttpRequest(host, path, timeout);
79+
if(sockfd > 0)
80+
ffNetworkingRecvHttpResponse(sockfd, buffer);
6981
}

src/common/networking.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "util/FFstrbuf.h"
77

8+
int ffNetworkingSendHttpRequest(const char* host, const char* path, uint32_t timeout);
9+
void ffNetworkingRecvHttpResponse(int sock, FFstrbuf* buffer);
810
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer);
911

1012
#endif

src/fastfetch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ void ffLogoBuiltinListAutocompletion();
236236

237237
void ffPrintDateTimeFormat(FFinstance* instance, const char* moduleName, const FFModuleArgs* moduleArgs);
238238
void ffPrepareCPUUsage();
239+
void ffPreparePublicIp(FFinstance* instance);
239240

240241
//Printing
241242

src/modules/publicip.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,32 @@
55
#define FF_PUBLICIP_MODULE_NAME "Public IP"
66
#define FF_PUBLICIP_NUM_FORMAT_ARGS 1
77

8+
static int sockfd;
9+
10+
void ffPreparePublicIp(FFinstance* instance)
11+
{
12+
sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", instance->config.publicIpTimeout);
13+
}
14+
815
void ffPrintPublicIp(FFinstance* instance)
916
{
17+
if(sockfd == 0)
18+
ffPreparePublicIp(instance);
19+
20+
if(sockfd < 0)
21+
{
22+
ffPrintError(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP, "Failed to connect to an IP detection server");
23+
return;
24+
}
25+
1026
FFstrbuf result;
1127
ffStrbufInitA(&result, 4096);
12-
ffNetworkingGetHttp("ipinfo.io", "/ip", instance->config.publicIpTimeout, &result);
28+
ffNetworkingRecvHttpResponse(sockfd, &result);
1329
ffStrbufSubstrAfterFirstS(&result, "\r\n\r\n");
1430

1531
if(result.length == 0)
1632
{
17-
ffPrintError(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP, "Failed to connect to an IP detection server");
33+
ffPrintError(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP, "Failed to receive the server response");
1834
ffStrbufDestroy(&result);
1935
return;
2036
}

0 commit comments

Comments
 (0)