Skip to content

Commit

Permalink
Merge pull request #1396 from pi-hole/fix/socket_simplification
Browse files Browse the repository at this point in the history
Major rewrite of the socket processing routines
  • Loading branch information
DL6ER authored Aug 3, 2022
2 parents 0b9a6f3 + 3ecb146 commit 7ec4b58
Show file tree
Hide file tree
Showing 26 changed files with 536 additions and 853 deletions.
371 changes: 182 additions & 189 deletions src/api/api.c

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions src/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@
#define API_H

// Statistic methods
void getStats(const int *sock);
void getOverTime(const int *sock);
void getTopDomains(const char *client_message, const int *sock);
void getTopClients(const char *client_message, const int *sock);
void getUpstreamDestinations(const char *client_message, const int *sock);
void getQueryTypes(const int *sock);
void getAllQueries(const char *client_message, const int *sock);
void getRecentBlocked(const char *client_message, const int *sock);
void getClientsOverTime(const int *sock);
void getClientNames(const int *sock);
void getStats(const int sock, const bool istelnet);
void getOverTime(const int sock, const bool istelnet);
void getTopDomains(const char *client_message, const int sock, const bool istelnet);
void getTopClients(const char *client_message, const int sock, const bool istelnet);
void getUpstreamDestinations(const char *client_message, const int sock, const bool istelnet);
void getQueryTypes(const int sock, const bool istelnet);
void getAllQueries(const char *client_message, const int sock, const bool istelnet);
void getRecentBlocked(const char *client_message, const int sock, const bool istelnet);
void getClientsOverTime(const int sock, const bool istelnet);
void getClientNames(const int sock, const bool istelnet);

// FTL methods
void getClientID(const int *sock);
void getVersion(const int *sock);
void getDBstats(const int *sock);
void getUnknownQueries(const int *sock);
void getMAXLOGAGE(const int *sock);
void getGateway(const int *sock);
void getInterfaces(const int *sock);
void getClientID(const int sock, const bool istelnet);
void getVersion(const int sock, const bool istelnet);
void getDBstats(const int sock, const bool istelnet);
void getUnknownQueries(const int sock, const bool istelnet);
void getMAXLOGAGE(const int sock);
void getGateway(const int sock);
void getInterfaces(const int sock);

// DNS resolver methods (dnsmasq_interface.c)
void getCacheInformation(const int *sock);
void getDNSport(const int *sock);
void getCacheInformation(const int sock);
void getDNSport(const int sock);

// MessagePack serialization helpers
void pack_eom(const int sock);
Expand All @@ -48,6 +48,6 @@ bool pack_str32(const int sock, const char *string);
void pack_map16_start(const int sock, const uint16_t length);

// DHCP lease management
void delete_lease(const char *client_message, const int *sock);
void delete_lease(const char *client_message, const int sock);

#endif // API_H
52 changes: 23 additions & 29 deletions src/api/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool __attribute__((pure)) command(const char *client_message, const char* cmd)
return strstr(client_message, cmd) != NULL;
}

void process_request(const char *client_message, int *sock)
bool process_request(const char *client_message, const int sock, const bool istelnet)
{
char EOT[2];
EOT[0] = 0x04;
Expand All @@ -37,104 +37,104 @@ void process_request(const char *client_message, int *sock)
{
processed = true;
lock_shm();
getStats(sock);
getStats(sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">overTime"))
{
processed = true;
lock_shm();
getOverTime(sock);
getOverTime(sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">top-domains") || command(client_message, ">top-ads"))
{
processed = true;
lock_shm();
getTopDomains(client_message, sock);
getTopDomains(client_message, sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">top-clients"))
{
processed = true;
lock_shm();
getTopClients(client_message, sock);
getTopClients(client_message, sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">forward-dest"))
{
processed = true;
lock_shm();
getUpstreamDestinations(client_message, sock);
getUpstreamDestinations(client_message, sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">forward-names"))
{
processed = true;
lock_shm();
getUpstreamDestinations(">forward-dest unsorted", sock);
getUpstreamDestinations(">forward-dest unsorted", sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">querytypes"))
{
processed = true;
lock_shm();
getQueryTypes(sock);
getQueryTypes(sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">getallqueries"))
{
processed = true;
lock_shm();
getAllQueries(client_message, sock);
getAllQueries(client_message, sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">recentBlocked"))
{
processed = true;
lock_shm();
getRecentBlocked(client_message, sock);
getRecentBlocked(client_message, sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">clientID"))
{
processed = true;
lock_shm();
getClientID(sock);
getClientID(sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">version"))
{
processed = true;
// No lock required
getVersion(sock);
getVersion(sock, istelnet);
}
else if(command(client_message, ">dbstats"))
{
processed = true;
// No lock required. Access to the database
// is guaranteed to be atomic
getDBstats(sock);
getDBstats(sock, istelnet);
}
else if(command(client_message, ">ClientsoverTime"))
{
processed = true;
lock_shm();
getClientsOverTime(sock);
getClientsOverTime(sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">client-names"))
{
processed = true;
lock_shm();
getClientNames(sock);
getClientNames(sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">unknown"))
{
processed = true;
lock_shm();
getUnknownQueries(sock);
getUnknownQueries(sock, istelnet);
unlock_shm();
}
else if(command(client_message, ">cacheinfo"))
Expand Down Expand Up @@ -191,21 +191,15 @@ void process_request(const char *client_message, int *sock)
if(command(client_message, ">quit") || command(client_message, EOT))
{
if(config.debug & DEBUG_API)
logg("Received >quit or EOT on socket %d", *sock);
processed = true;
close(*sock);
*sock = 0;
logg("Received >quit or EOT on socket %d", sock);
return true;
}

if(!processed)
{
ssend(*sock, "unknown command: %s\n", client_message);
}
ssend(sock, "unknown command: %s\n", client_message);

// End of queryable commands
if(*sock != 0)
{
// Send EOM
seom(*sock);
}
// End of queryable commands: Send EOM
seom(sock, istelnet);

return false;
}
2 changes: 1 addition & 1 deletion src/api/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef REQUEST_H
#define REQUEST_H

void process_request(const char *client_message, int *sock);
bool process_request(const char *client_message, const int sock, const bool istelnet);
bool command(const char *client_message, const char* cmd) __attribute__((pure));

#endif //REQUEST_H
Loading

0 comments on commit 7ec4b58

Please sign in to comment.