basic message parsing

This commit is contained in:
2025-09-18 00:29:17 +02:00
parent 2a728034c2
commit c3d7bdfdf4

View File

@@ -1,5 +1,8 @@
#define _POSIX_C_SOURCE 200809L /* needed for strndup */
#include <strops.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
@@ -12,6 +15,68 @@ int server_sock;
struct sockaddr_un server;
int sockaddr_len;
/*
Message format: (Basically like a command line)
operation operand1 operand2 operand3
Supports up to 3 operands
*/
void parse_message(char *buffer, char *operation, char *operand1, char *operand2, char *operand3) {
int len_to_ws = 0;
int offset = 0;
char log_line[1000] = {0};
sprintf(log_line, "Beginning of parsing message: '%s'", buffer);
syslog(LOG_DEBUG, log_line);
len_to_ws = strcspn(buffer, " ");
strncpy(operation, buffer, len_to_ws);
offset = len_to_ws + 1;
sprintf(log_line, "operation = '%s'", operation);
syslog(LOG_DEBUG, log_line);
if (strlen(buffer) == offset - 1) {
syslog(LOG_DEBUG, "Finished parsing message.");
return;
}
len_to_ws = strcspn(buffer + offset, " ");
strncpy(operand1, buffer + offset, len_to_ws);
offset += len_to_ws + 1;
sprintf(log_line, "operand1 = '%s'", operand1);
syslog(LOG_DEBUG, log_line);
if (strlen(buffer) == offset -1) {
syslog(LOG_DEBUG, "Finished parsing message.");
return;
}
len_to_ws = strcspn(buffer + offset, " ");
strncpy(operand2, buffer + offset, len_to_ws);
offset += len_to_ws + 1;
sprintf(log_line, "operand2 = '%s'", operand2);
syslog(LOG_DEBUG, log_line);
if (strlen(buffer) == offset -1) {
syslog(LOG_DEBUG, "Finished parsing message.");
return;
}
len_to_ws = strcspn(buffer + offset, " ");
strncpy(operand3, buffer + offset, len_to_ws);
offset += len_to_ws + 1;
sprintf(log_line, "operand3 = '%s'", operand3);
syslog(LOG_DEBUG, log_line);
if (strlen(buffer) == offset -1) {
syslog(LOG_DEBUG, "Finished parsing message.");
return;
}
}
int main() {
/*
Setup daemon
@@ -40,7 +105,7 @@ int main() {
close(STDERR_FILENO);
openlog("hdbd", LOG_CONS|LOG_PID, LOG_USER);
syslog(LOG_INFO, "Hello from hdbd");
syslog(LOG_NOTICE, "Daemon setup done.");
/* Setup Unix Socket */
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -61,48 +126,52 @@ int main() {
return 1;
}
syslog(LOG_NOTICE, "Unix Socket up and running.");
/* Actual program */
int client_sock;
int data_length = 0;
char receive_buf[1000];
char send_buf[1000];
char log_line[10000];
memset(receive_buf, 0, sizeof receive_buf);
memset(send_buf, 0, sizeof send_buf);
memset(log_line, 0, sizeof log_line);
syslog(LOG_INFO, "Listening for connections...");
char receive_buf[1000] = {0};
char send_buf[1000] = {0};
char log_line[10000] = {0};
char operation[1000] = {0};
char operand1[1000] = {0};
char operand2[1000] = {0};
char operand3[1000] = {0};
while(1) {
client_sock = accept(server_sock, NULL, NULL);
if (client_sock == -1) {
return 1;
}
syslog(LOG_INFO, "Accepted a connection");
data_length = recv(client_sock, receive_buf, sizeof receive_buf, 0);
syslog(LOG_INFO, "Accepted a connection.");
data_length = read(client_sock, receive_buf, sizeof receive_buf);
if (data_length <= 0) {
syslog(LOG_WARNING, "Received no data from client");
syslog(LOG_WARNING, "Received no data from client!");
strcpy(send_buf, "Failed to receive any data.");
send(client_sock, send_buf, strlen(send_buf), 0);
close(client_sock);
continue;
}
sprintf(log_line, "Received from Client: %s", receive_buf);
sprintf(log_line, "Received from Client: '%s'", receive_buf);
syslog(LOG_DEBUG, log_line);
if (strstr(receive_buf, "shutdown") != NULL) {
syslog(LOG_NOTICE, "Shutting down");
strcpy(send_buf, "Shutting down.");
parse_message(receive_buf, operation, operand1, operand2, operand3);
if (strcmp(operation, "shutdown") == 0) {
syslog(LOG_NOTICE, "Starting shutdown.");
strcpy(send_buf, "Shutting down daemon.");
send(client_sock, send_buf, strlen(send_buf), 0);
close(client_sock);
break;
}
syslog(LOG_INFO, "Closing connection.");
close(client_sock);
}
close(server_sock);
syslog(LOG_NOTICE, "Bye Bye.");
return 0;
}