basic message parsing
This commit is contained in:
103
src/hdbd.c
103
src/hdbd.c
@@ -1,5 +1,8 @@
|
|||||||
|
#define _POSIX_C_SOURCE 200809L /* needed for strndup */
|
||||||
|
|
||||||
#include <strops.h>
|
#include <strops.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -12,6 +15,68 @@ int server_sock;
|
|||||||
struct sockaddr_un server;
|
struct sockaddr_un server;
|
||||||
int sockaddr_len;
|
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() {
|
int main() {
|
||||||
/*
|
/*
|
||||||
Setup daemon
|
Setup daemon
|
||||||
@@ -40,7 +105,7 @@ int main() {
|
|||||||
close(STDERR_FILENO);
|
close(STDERR_FILENO);
|
||||||
|
|
||||||
openlog("hdbd", LOG_CONS|LOG_PID, LOG_USER);
|
openlog("hdbd", LOG_CONS|LOG_PID, LOG_USER);
|
||||||
syslog(LOG_INFO, "Hello from hdbd");
|
syslog(LOG_NOTICE, "Daemon setup done.");
|
||||||
|
|
||||||
/* Setup Unix Socket */
|
/* Setup Unix Socket */
|
||||||
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
@@ -61,48 +126,52 @@ int main() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syslog(LOG_NOTICE, "Unix Socket up and running.");
|
||||||
|
|
||||||
/* Actual program */
|
/* Actual program */
|
||||||
int client_sock;
|
int client_sock;
|
||||||
int data_length = 0;
|
int data_length = 0;
|
||||||
char receive_buf[1000];
|
char receive_buf[1000] = {0};
|
||||||
char send_buf[1000];
|
char send_buf[1000] = {0};
|
||||||
char log_line[10000];
|
char log_line[10000] = {0};
|
||||||
memset(receive_buf, 0, sizeof receive_buf);
|
char operation[1000] = {0};
|
||||||
memset(send_buf, 0, sizeof send_buf);
|
char operand1[1000] = {0};
|
||||||
memset(log_line, 0, sizeof log_line);
|
char operand2[1000] = {0};
|
||||||
|
char operand3[1000] = {0};
|
||||||
|
|
||||||
syslog(LOG_INFO, "Listening for connections...");
|
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
client_sock = accept(server_sock, NULL, NULL);
|
client_sock = accept(server_sock, NULL, NULL);
|
||||||
if (client_sock == -1) {
|
if (client_sock == -1) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
syslog(LOG_INFO, "Accepted a connection");
|
syslog(LOG_INFO, "Accepted a connection.");
|
||||||
data_length = recv(client_sock, receive_buf, sizeof receive_buf, 0);
|
data_length = read(client_sock, receive_buf, sizeof receive_buf);
|
||||||
if (data_length <= 0) {
|
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.");
|
strcpy(send_buf, "Failed to receive any data.");
|
||||||
send(client_sock, send_buf, strlen(send_buf), 0);
|
send(client_sock, send_buf, strlen(send_buf), 0);
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(log_line, "Received from Client: %s", receive_buf);
|
sprintf(log_line, "Received from Client: '%s'", receive_buf);
|
||||||
syslog(LOG_DEBUG, log_line);
|
syslog(LOG_DEBUG, log_line);
|
||||||
|
|
||||||
if (strstr(receive_buf, "shutdown") != NULL) {
|
parse_message(receive_buf, operation, operand1, operand2, operand3);
|
||||||
syslog(LOG_NOTICE, "Shutting down");
|
|
||||||
strcpy(send_buf, "Shutting down.");
|
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);
|
send(client_sock, send_buf, strlen(send_buf), 0);
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syslog(LOG_INFO, "Closing connection.");
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(server_sock);
|
close(server_sock);
|
||||||
|
syslog(LOG_NOTICE, "Bye Bye.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user