Browse Source

- cmsg.[ch]

master
Alex 'AdUser' Z 4 years ago
parent
commit
0c4958b2b2
  1. 2
      src/CMakeLists.txt
  2. 42
      src/cmsg.c
  3. 68
      src/cmsg.h
  4. 3
      src/csocket.c
  5. 4
      src/csocket.h
  6. 1
      src/daemon.c
  7. 2
      src/filter.h
  8. 2
      src/jail.h

2
src/CMakeLists.txt

@ -4,7 +4,7 @@ set(SOURCES "daemon.c" "strlcpy.c" "config.c" "log.c" "matches.c" "ipaddr.c"
"appconfig.c" "statefile.c" "source.c" "filter.c" "backend.c" "jail.c")
if (WITH_CSOCKET)
list(APPEND SOURCES "commands.c" "csocket.c" "cmsg.c")
list(APPEND SOURCES "commands.c" "csocket.c")
add_definitions("-DWITH_CSOCKET")
endif ()

42
src/cmsg.c

@ -1,42 +0,0 @@
#include "common.h"
#include "commands.h"
#include "cmsg.h"
#include <sys/uio.h>
void
f2b_cmsg_convert_args(f2b_cmsg_t *msg) {
assert(msg != NULL);
for (size_t i = 0; i < msg->size && i < sizeof(msg->data); i++) {
if (msg->data[i] == '\n')
msg->data[i] = '\0';
}
}
int
f2b_cmsg_extract_args(const f2b_cmsg_t *msg, const char **argv) {
char prev = '\0';
size_t argc = 0;
assert(msg != NULL);
assert(argv != NULL);
if (msg->size == 0)
return 0; /* no args */
if (msg->data[msg->size - 1] != '\0')
return -1; /* message data not null-terminated */
for (size_t i = 0; i < msg->size; i++) {
if (prev == '\0' && msg->data[i] != '\0')
argv[argc] = &msg->data[i], argc++;
if (argc >= DATA_ARGS_MAX) {
argc = -1;
break;
}
prev = msg->data[i];
}
return argc;
}

68
src/cmsg.h

@ -1,68 +0,0 @@
#ifndef F2B_CMSG_H_
#define F2B_CMSG_H_
/**
* @file
* This header contains definitions of control messages format and routines
*/
/**
* @def DATA_LEN_MAX
* Maximum length of data in packet
*/
#define DATA_LEN_MAX 1476 /* 1500 - (16 bytes of cmsg header + 8 bytes of udp) */
/**
* @def DATA_ARGS_MAX
* Maximum count of data pieces in packet
*/
#define DATA_ARGS_MAX 6 /* number of args in data */
/**
* @def F2B_PROTO_VER
* Protocol version of control message
*/
#define F2B_PROTO_VER 1
/**
* @def CMSG_FLAG_NEED_REPLY
* Server should reply to this control message
*/
#define CMSG_FLAG_NEED_REPLY 0x01
/**
* @def CMSG_FLAG_AUTH_PASS
* This control message contains password
*/
#define CMSG_FLAG_AUTH_PASS 0x02
/**
* f2b control message
* @note Use sendmsg/recvmsg and iovec structs to pack/unpack
*/
typedef struct f2b_cmsg_t {
char magic[3]; /**< magic string "F2B" */
uint8_t version; /**< protocol version */
/* 4 bytes */
uint8_t type; /**< command type, cast from enum f2b_cmd_type */
uint8_t flags; /**< CMSG_FLAG_* */
uint16_t size; /**< payload length */
/* 8 bytes */
char pass[8]; /**< auth data */
/* 16 bytes */
/* end of header */
char data[DATA_LEN_MAX]; /**< set of "\n"-terminated strings */
/* end of data */
} f2b_cmsg_t;
/**
* @brief Convert every '\n' in data to '\0'
* @param msg Pointer to control message
*/
void f2b_cmsg_convert_args(f2b_cmsg_t *msg);
/**
* @brief Fill @a argv array with pointers to found data pieces
* @param msg Pointer to control message
* @param argv Array of pointers
* @returns Number of found args
*/
int f2b_cmsg_extract_args(const f2b_cmsg_t *msg, const char **argv);
#endif /* F2B_CMSG_H_ */

3
src/csocket.c

@ -6,7 +6,6 @@
*/
#include "common.h"
#include "commands.h"
#include "cmsg.h"
#include "csocket.h"
#include "log.h"
@ -202,7 +201,7 @@ f2b_csocket_send(int csock, f2b_cmsg_t *cmsg, struct sockaddr_storage *addr, soc
}
int
f2b_csocket_poll(int csock, void (*cb)(const f2b_cmsg_t *cmsg, char *res, size_t ressize)) {
f2b_csocket_poll(int csock, void (*cb)(const f2b_cmd_t *cmd, f2b_buf_t *res)) {
char res[DATA_LEN_MAX + 1];
f2b_cmsg_t cmsg;
struct sockaddr_storage addr;

4
src/csocket.h

@ -64,7 +64,8 @@ f2b_csocket_error(int retcode);
* @param cb Callback for handling message
* @returns -1 on error, 0 on no messages, and > 0 on some messages processed
*/
int f2b_csocket_poll(int csock, void (*cb)(const f2b_cmsg_t *cmsg, char *res, size_t ressize));
int f2b_csocket_poll(int csock, void (*cb)(const f2b_cmd_t *cmd, f2b_buf_t *res));
/**
* @brief Pack and send control message
* @param csock Opened socket fd
@ -74,6 +75,7 @@ int f2b_csocket_poll(int csock, void (*cb)(const f2b_cmsg_t *cmsg, char *res, si
* @returns >0 on success
*/
int f2b_csocket_send(int csock, f2b_cmsg_t *cmsg, struct sockaddr_storage *addr, socklen_t *addrlen);
/**
* @brief Recieve and unpack control message
* @param csock Opened socket fd

1
src/daemon.c

@ -11,7 +11,6 @@
#include "backend.h"
#include "appconfig.h"
#include "commands.h"
#include "cmsg.h"
#include "csocket.h"
#include <getopt.h>

2
src/filter.h

@ -77,7 +77,7 @@ bool f2b_filter_append(f2b_filter_t *f, const char *pattern);
*/
bool f2b_filter_match (f2b_filter_t *f, const char *line, char *buf, size_t bufsize);
/* handlers for cmsg processing */
/* handlers for csocket commands processing */
/** handler of 'jail $JAIL filter reload' cmd */
void f2b_filter_cmd_reload(char *buf, size_t bufsize, f2b_filter_t *f);
/** handler of 'jail $JAIL filter stats' cmd */

2
src/jail.h

@ -110,7 +110,7 @@ size_t f2b_jail_process (f2b_jail_t *jail);
*/
bool f2b_jail_stop (f2b_jail_t *jail);
/* handlers for cmsg */
/* handlers for csocket commands processing */
/**
* @brief Get jail status

Loading…
Cancel
Save