Skip to content

Commit

Permalink
[agroal#392] Add application name and version in management protocol
Browse files Browse the repository at this point in the history
In order to improve debugging, [agroal#392] suggested to write
the sender application name and its version into the header
over which pgagroal-cli and pgagroal communicate.

Introduces the 'write_header_info' function in
management.c.
  • Loading branch information
EuGig committed Mar 18, 2024
1 parent 411dd98 commit 490eb32
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/libpgagroal/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@
#include <openssl/err.h>
#include <openssl/ssl.h>

#define MANAGEMENT_HEADER_SIZE 5
#define MANAGEMENT_HEADER_SIZE 60

static int read_complete(SSL* ssl, int socket, void* buf, size_t size);
static int write_complete(SSL* ssl, int socket, void* buf, size_t size);
static int write_socket(int socket, void* buf, size_t size);
static int write_ssl(SSL* ssl, void* buf, size_t size);
static int write_header(SSL* ssl, int fd, signed char type, int slot);
static int write_header_info(char* header, bool start, char* command);

static int pgagroal_management_write_conf_ls_detail(int socket, char* what);
static int pgagroal_management_read_conf_ls_detail(SSL* ssl, int socket, char* buffer);
Expand Down Expand Up @@ -1113,10 +1114,12 @@ pgagroal_management_read_isalive(SSL* ssl, int socket, int* status, char output_
int
pgagroal_management_write_isalive(int socket, bool gracefully)
{
char buf[4];
char buf[MANAGEMENT_HEADER_SIZE];

memset(&buf, 0, sizeof(buf));

write_header_info(buf, 1, "pgagroal ");

if (!gracefully)
{
pgagroal_write_int32(buf, PING_STATUS_RUNNING);
Expand Down Expand Up @@ -1610,6 +1613,27 @@ write_ssl(SSL* ssl, void* buf, size_t size)
return 1;
}

static int
write_header_info(char* header, bool start, char* command) {
char header_info[42] = " {sender: ";

strcat(header_info, command);
strcat(header_info, "version: ");
strcat(header_info, PGAGROAL_VERSION);
strcat(header_info, "}");

if (start) {
pgagroal_write_string(header, header_info);
}
else {
pgagroal_write_string(header + 4, header_info);
}

pgagroal_log_debug("Header: %s\n", header);

return 0;
}

static int
write_header(SSL* ssl, int fd, signed char type, int slot)
{
Expand All @@ -1618,6 +1642,8 @@ write_header(SSL* ssl, int fd, signed char type, int slot)
pgagroal_write_byte(&(header), type);
pgagroal_write_int32(&(header[1]), slot);

write_header_info(header, 0 ,"pgagroal-cli ");

return write_complete(ssl, fd, &(header), MANAGEMENT_HEADER_SIZE);
}

Expand Down

0 comments on commit 490eb32

Please sign in to comment.