From 30ae391e85128810ee3d7e9e60416d64da89d29c Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Mon, 8 Jan 2018 18:15:41 -0500 Subject: [PATCH] Let socket-test take in an optional command Example: ./socket-test travis ">stats" Signed-off-by: Mcat12 --- socket_client.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/socket_client.c b/socket_client.c index 6df0fc27..37abe99b 100644 --- a/socket_client.c +++ b/socket_client.c @@ -37,11 +37,22 @@ int main (int argc, char **argv) { // Set socket family to local socket (not an Internet socket) address.sun_family = AF_LOCAL; - // Set socket file location (respect special location on the CI system Travis) - if(argc == 2 && strcmp(argv[1], "travis") == 0) - strcpy(address.sun_path,"pihole-FTL.sock"); - else - strcpy(address.sun_path,"/var/run/pihole/FTL.sock"); + char *command = ">stats"; + + int i; + for(i = 1; i < argc; i++) { + // Get command + if(strstr(argv[i], ">") == argv[i]) { + command = argv[i]; + continue; + } + + // Set socket file location (respect special location on the CI system Travis) + if(strcmp(argv[i], "travis") == 0) + strcpy(address.sun_path,"pihole-FTL.sock"); + else + strcpy(address.sun_path,"/var/run/pihole/FTL.sock"); + } // Connect to the socket provided by pihole-FTL ret = connect(socketfd, (struct sockaddr *) &address, sizeof (address)); @@ -53,14 +64,13 @@ int main (int argc, char **argv) { printf("Connection established\n"); // As an example, we query the current statistics from FTL through the socket here - sprintf(buffer, ">stats"); + sprintf(buffer, command); send(socketfd, buffer, strlen (buffer), 0); // Try to receive data until either recv() fails or we see "--EOM--" while((size = recv(socketfd, buffer, BUF-1, 0)) > -1) { // Print received data to stdout - int i; for(i = 0; i < size; ++i) { printf("%02x ", (unsigned char) buffer[i]); }