mirror of
https://github.com/utoni/nDPId.git
synced 2026-05-05 19:15:06 +00:00
* aims to be re-usable for many different apps and use-cases Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
484 lines
14 KiB
C
484 lines
14 KiB
C
#ifndef NDPISRVD_H
|
|
#define NDPISRVD_H 1
|
|
|
|
#include <arpa/inet.h>
|
|
#include <errno.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/un.h>
|
|
#include <unistd.h>
|
|
|
|
#include "config.h"
|
|
#include "jsmn/jsmn.h"
|
|
#include "uthash.h"
|
|
|
|
// FIXME: Unify with event enums in nDPId.c
|
|
enum nDPIsrvd_event
|
|
{
|
|
EVENT_INVALID = 0,
|
|
EVENT_FLOW,
|
|
EVENT_PACKET,
|
|
EVENT_COUNT
|
|
};
|
|
|
|
struct nDPIsrvd_flow
|
|
{
|
|
char id[20];
|
|
void * user_data;
|
|
UT_hash_handle hh;
|
|
};
|
|
|
|
struct nDPIsrvd_socket
|
|
{
|
|
int fd;
|
|
int socket_family;
|
|
|
|
union {
|
|
struct
|
|
{
|
|
char const * dst_ip;
|
|
unsigned short dst_port;
|
|
} ip_socket;
|
|
struct
|
|
{
|
|
char * path;
|
|
} unix_socket;
|
|
} address;
|
|
|
|
struct
|
|
{
|
|
char raw[NETWORK_BUFFER_MAX_SIZE];
|
|
size_t used;
|
|
char * json_string;
|
|
size_t json_string_start;
|
|
unsigned long long int json_string_length;
|
|
} buffer;
|
|
|
|
struct
|
|
{
|
|
jsmn_parser parser;
|
|
jsmntok_t tokens[128];
|
|
int current_token;
|
|
int tokens_found;
|
|
struct
|
|
{
|
|
char const * key;
|
|
int key_length;
|
|
char const * value;
|
|
int value_length;
|
|
} key_value;
|
|
} jsmn;
|
|
|
|
struct
|
|
{
|
|
enum nDPIsrvd_event event;
|
|
char const * event_name;
|
|
int event_name_len;
|
|
char const * flow_id;
|
|
int flow_id_len;
|
|
} current;
|
|
|
|
struct nDPIsrvd_flow * flows;
|
|
|
|
void * user_data;
|
|
};
|
|
|
|
#define FIRST_ENUM_VALUE 1
|
|
#define LAST_ENUM_VALUE CALLBACK_LAST_ENUM_VALUE
|
|
|
|
enum nDPIsrvd_connect_return
|
|
{
|
|
CONNECT_OK = FIRST_ENUM_VALUE,
|
|
CONNECT_ERROR_SOCKET,
|
|
CONNECT_ERROR_PTON,
|
|
CONNECT_ERROR,
|
|
CONNECT_LAST_ENUM_VALUE
|
|
};
|
|
|
|
enum nDPIsrvd_read_return
|
|
{
|
|
READ_OK = CONNECT_LAST_ENUM_VALUE,
|
|
READ_PEER_DISCONNECT,
|
|
READ_ERROR,
|
|
READ_LAST_ENUM_VALUE
|
|
};
|
|
|
|
enum nDPIsrvd_parse_return
|
|
{
|
|
PARSE_OK = READ_LAST_ENUM_VALUE,
|
|
PARSE_INVALID_OPENING_CHAR,
|
|
PARSE_SIZE_EXCEEDS_CONVERSION_LIMIT,
|
|
PARSE_SIZE_MISSING,
|
|
PARSE_STRING_TOO_BIG,
|
|
PARSE_INVALID_CLOSING_CHAR,
|
|
PARSE_JSMN_ERROR,
|
|
PARSE_CALLBACK_ERROR,
|
|
PARSE_LAST_ENUM_VALUE
|
|
};
|
|
|
|
enum nDPIsrvd_callback_return
|
|
{
|
|
CALLBACK_OK = PARSE_LAST_ENUM_VALUE,
|
|
CALLBACK_ERROR,
|
|
CALLBACK_LAST_ENUM_VALUE
|
|
};
|
|
|
|
typedef enum nDPIsrvd_callback_return (*json_callback)(struct nDPIsrvd_socket * const sock, void * user_data);
|
|
|
|
static inline char const * nDPIsrvd_enum_to_string(int enum_value)
|
|
{
|
|
static char const * const enum_str[] = {"CONNECT_OK",
|
|
"CONNECT_ERROR_SOCKET",
|
|
"CONNECT_ERROR_PTON",
|
|
"CONNECT_ERROR",
|
|
"READ_OK",
|
|
"READ_PEER_DISCONNECT",
|
|
"READ_ERROR",
|
|
"PARSE_OK",
|
|
"PARSE_INVALID_OPENING_CHAR",
|
|
"PARSE_SIZE_EXCEEDS_CONVERSION_LIMIT",
|
|
"PARSE_SIZE_MISSING",
|
|
"PARSE_STRING_TOO_BIG",
|
|
"PARSE_INVALID_CLOSING_CHAR",
|
|
"PARSE_JSMN_ERROR",
|
|
"PARSE_CALLBACK_ERROR"};
|
|
|
|
if (enum_value < FIRST_ENUM_VALUE || enum_value >= LAST_ENUM_VALUE)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
return enum_str[enum_value - FIRST_ENUM_VALUE];
|
|
}
|
|
|
|
static inline struct nDPIsrvd_socket * nDPIsrvd_init(void)
|
|
{
|
|
struct nDPIsrvd_socket * sock = (struct nDPIsrvd_socket *)malloc(sizeof(*sock));
|
|
|
|
if (sock != NULL)
|
|
{
|
|
sock->fd = -1;
|
|
sock->socket_family = -1;
|
|
}
|
|
|
|
return sock;
|
|
}
|
|
|
|
static inline void nDPIsrvd_free(struct nDPIsrvd_socket ** const sock)
|
|
{
|
|
struct nDPIsrvd_flow * current_flow;
|
|
struct nDPIsrvd_flow * tmp;
|
|
|
|
HASH_ITER(hh, (*sock)->flows, current_flow, tmp)
|
|
{
|
|
HASH_DEL((*sock)->flows, current_flow);
|
|
free(current_flow);
|
|
}
|
|
|
|
free(*sock);
|
|
|
|
*sock = NULL;
|
|
}
|
|
|
|
static inline enum nDPIsrvd_connect_return nDPIsrvd_connect_ip(struct nDPIsrvd_socket * const sock,
|
|
char const * dst_ip,
|
|
unsigned short dst_port)
|
|
{
|
|
struct sockaddr_in remote_addr = {};
|
|
|
|
sock->socket_family = remote_addr.sin_family = AF_INET;
|
|
sock->fd = socket(sock->socket_family, SOCK_STREAM, 0);
|
|
|
|
if (sock->fd < 0)
|
|
{
|
|
return CONNECT_ERROR_SOCKET;
|
|
}
|
|
|
|
if (inet_pton(sock->socket_family, &dst_ip[0], &remote_addr.sin_addr) != 1)
|
|
{
|
|
return CONNECT_ERROR_PTON;
|
|
}
|
|
remote_addr.sin_port = htons(dst_port);
|
|
|
|
if (connect(sock->fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr)) != 0)
|
|
{
|
|
return CONNECT_ERROR;
|
|
}
|
|
|
|
return CONNECT_OK;
|
|
}
|
|
|
|
static inline enum nDPIsrvd_connect_return nDPIsrvd_connect_unix(struct nDPIsrvd_socket * const sock,
|
|
char const * const path)
|
|
{
|
|
struct sockaddr_un remote_addr = {};
|
|
|
|
sock->socket_family = remote_addr.sun_family = AF_UNIX;
|
|
sock->fd = socket(sock->socket_family, SOCK_STREAM, 0);
|
|
|
|
if (sock->fd < 0)
|
|
{
|
|
return CONNECT_ERROR_SOCKET;
|
|
}
|
|
|
|
if (snprintf(remote_addr.sun_path, sizeof(remote_addr.sun_path), "%s", path) <= 0)
|
|
{
|
|
return CONNECT_ERROR_SOCKET;
|
|
}
|
|
|
|
if (connect(sock->fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr)) != 0)
|
|
{
|
|
return CONNECT_ERROR;
|
|
}
|
|
|
|
return CONNECT_OK;
|
|
}
|
|
|
|
static inline enum nDPIsrvd_read_return nDPIsrvd_read(struct nDPIsrvd_socket * const sock)
|
|
{
|
|
ssize_t bytes_read =
|
|
read(sock->fd, sock->buffer.raw + sock->buffer.used, sizeof(sock->buffer.raw) - sock->buffer.used);
|
|
|
|
if (bytes_read == 0)
|
|
{
|
|
return READ_PEER_DISCONNECT;
|
|
}
|
|
if (bytes_read < 0)
|
|
{
|
|
return READ_ERROR;
|
|
}
|
|
|
|
sock->buffer.used += bytes_read;
|
|
|
|
return READ_OK;
|
|
}
|
|
|
|
static inline int token_is_key(struct nDPIsrvd_socket const * const sock)
|
|
{
|
|
return sock->jsmn.current_token % 2;
|
|
}
|
|
|
|
static inline char const * token_get(struct nDPIsrvd_socket const * const sock)
|
|
{
|
|
return sock->buffer.json_string + sock->jsmn.tokens[sock->jsmn.current_token].start;
|
|
}
|
|
|
|
static inline int token_size(struct nDPIsrvd_socket const * const sock)
|
|
{
|
|
return sock->jsmn.tokens[sock->jsmn.current_token].end - sock->jsmn.tokens[sock->jsmn.current_token].start;
|
|
}
|
|
|
|
static inline int token_is_start(struct nDPIsrvd_socket const * const sock)
|
|
{
|
|
return sock->jsmn.current_token == 0;
|
|
}
|
|
|
|
static inline int token_is_end(struct nDPIsrvd_socket const * const sock)
|
|
{
|
|
return sock->jsmn.current_token == sock->jsmn.tokens_found;
|
|
}
|
|
|
|
static inline int token_is_key_value_pair(struct nDPIsrvd_socket const * const sock)
|
|
{
|
|
return sock->jsmn.current_token > 0 && sock->jsmn.current_token < sock->jsmn.tokens_found;
|
|
}
|
|
|
|
static inline int token_is_jsmn_type(struct nDPIsrvd_socket const * const sock, jsmntype_t type_to_check)
|
|
{
|
|
if (token_is_key_value_pair(sock) == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return sock->jsmn.tokens[sock->jsmn.current_token].type == type_to_check;
|
|
}
|
|
|
|
static inline int key_equals(struct nDPIsrvd_socket const * const sock, char const * const name)
|
|
{
|
|
if (sock->jsmn.key_value.key == NULL || sock->jsmn.key_value.key_length == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return (int)strlen(name) == sock->jsmn.key_value.key_length &&
|
|
strncmp(name, sock->jsmn.key_value.key, sock->jsmn.key_value.key_length) == 0;
|
|
}
|
|
|
|
static inline int value_equals(struct nDPIsrvd_socket const * const sock, char const * const name)
|
|
{
|
|
if (sock->jsmn.key_value.value == NULL || sock->jsmn.key_value.value_length == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return (int)strlen(name) == sock->jsmn.key_value.value_length &&
|
|
strncmp(name, sock->jsmn.key_value.value, sock->jsmn.key_value.value_length) == 0;
|
|
}
|
|
|
|
static inline void nDPIsrvd_handle_flow(struct nDPIsrvd_socket * const sock)
|
|
{
|
|
if (token_is_start(sock) == 1)
|
|
{
|
|
memset(&sock->current, 0, sizeof(sock->current));
|
|
}
|
|
else if (token_is_end(sock) == 1)
|
|
{
|
|
if (sock->current.event_name != NULL && sock->current.flow_id != NULL)
|
|
{
|
|
if (strncmp(sock->current.event_name, "new", sock->current.event_name_len) == 0)
|
|
{
|
|
struct nDPIsrvd_flow * f = (struct nDPIsrvd_flow *)malloc(sizeof(*f));
|
|
snprintf(f->id, sizeof(f->id), "%.*s", sock->current.flow_id_len, sock->current.flow_id);
|
|
f->user_data = NULL;
|
|
HASH_ADD(hh, sock->flows, id, sizeof(f->id), f);
|
|
}
|
|
else if (strncmp(sock->current.event_name, "end", sock->current.event_name_len) == 0 ||
|
|
strncmp(sock->current.event_name, "idle", sock->current.event_name_len) == 0)
|
|
{
|
|
struct nDPIsrvd_flow * f = NULL;
|
|
HASH_FIND(hh, sock->flows, sock->current.flow_id, (size_t)sock->current.flow_id_len, f);
|
|
}
|
|
}
|
|
}
|
|
else if (token_is_key_value_pair(sock) == 1)
|
|
{
|
|
if (key_equals(sock, "packet_event_name") == 1)
|
|
{
|
|
sock->current.event = EVENT_PACKET;
|
|
sock->current.event_name = sock->jsmn.key_value.value;
|
|
sock->current.event_name_len = sock->jsmn.key_value.value_length;
|
|
}
|
|
else if (key_equals(sock, "flow_event_name") == 1)
|
|
{
|
|
sock->current.event = EVENT_FLOW;
|
|
sock->current.event_name = sock->jsmn.key_value.value;
|
|
sock->current.event_name_len = sock->jsmn.key_value.value_length;
|
|
}
|
|
else if (key_equals(sock, "flow_id") == 1)
|
|
{
|
|
sock->current.flow_id = sock->jsmn.key_value.value;
|
|
sock->current.flow_id_len = sock->jsmn.key_value.value_length;
|
|
}
|
|
}
|
|
}
|
|
|
|
static inline enum nDPIsrvd_parse_return nDPIsrvd_parse(struct nDPIsrvd_socket * const sock,
|
|
json_callback cb,
|
|
void * user_data)
|
|
{
|
|
while (sock->buffer.used >= nDPIsrvd_JSON_BYTES + 1)
|
|
{
|
|
if (sock->buffer.raw[nDPIsrvd_JSON_BYTES] != '{')
|
|
{
|
|
return PARSE_INVALID_OPENING_CHAR;
|
|
}
|
|
|
|
errno = 0;
|
|
sock->buffer.json_string_length = strtoull((const char *)sock->buffer.raw, &sock->buffer.json_string, 10);
|
|
sock->buffer.json_string_length += sock->buffer.json_string - sock->buffer.raw;
|
|
sock->buffer.json_string_start = sock->buffer.json_string - sock->buffer.raw;
|
|
|
|
if (errno == ERANGE)
|
|
{
|
|
return PARSE_SIZE_EXCEEDS_CONVERSION_LIMIT;
|
|
}
|
|
if (sock->buffer.json_string == sock->buffer.raw)
|
|
{
|
|
return PARSE_SIZE_MISSING;
|
|
}
|
|
if (sock->buffer.json_string_length > sizeof(sock->buffer.raw))
|
|
{
|
|
return PARSE_STRING_TOO_BIG;
|
|
}
|
|
if (sock->buffer.json_string_length > sock->buffer.used)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (sock->buffer.raw[sock->buffer.json_string_length - 2] != '}' ||
|
|
sock->buffer.raw[sock->buffer.json_string_length - 1] != '\n')
|
|
{
|
|
return PARSE_INVALID_CLOSING_CHAR;
|
|
}
|
|
|
|
jsmn_init(&sock->jsmn.parser);
|
|
sock->jsmn.tokens_found = jsmn_parse(&sock->jsmn.parser,
|
|
(char *)(sock->buffer.raw + sock->buffer.json_string_start),
|
|
sock->buffer.json_string_length - sock->buffer.json_string_start,
|
|
sock->jsmn.tokens,
|
|
sizeof(sock->jsmn.tokens) / sizeof(sock->jsmn.tokens[0]));
|
|
if (sock->jsmn.tokens_found < 0 || sock->jsmn.tokens[0].type != JSMN_OBJECT)
|
|
{
|
|
return PARSE_JSMN_ERROR;
|
|
}
|
|
|
|
sock->jsmn.key_value.key = NULL;
|
|
sock->jsmn.key_value.key_length = 0;
|
|
sock->jsmn.key_value.value = NULL;
|
|
sock->jsmn.key_value.value_length = 0;
|
|
sock->jsmn.current_token = 0;
|
|
nDPIsrvd_handle_flow(sock);
|
|
if (cb(sock, user_data) != CALLBACK_OK)
|
|
{
|
|
return PARSE_CALLBACK_ERROR;
|
|
}
|
|
|
|
for (sock->jsmn.current_token = 1; sock->jsmn.current_token < sock->jsmn.tokens_found;
|
|
sock->jsmn.current_token++)
|
|
{
|
|
if (token_is_key(sock) == 1)
|
|
{
|
|
sock->jsmn.key_value.key = token_get(sock);
|
|
sock->jsmn.key_value.key_length = token_size(sock);
|
|
|
|
if (sock->jsmn.key_value.key == NULL || sock->jsmn.key_value.value != NULL)
|
|
{
|
|
return PARSE_JSMN_ERROR;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sock->jsmn.key_value.value = token_get(sock);
|
|
sock->jsmn.key_value.value_length = token_size(sock);
|
|
|
|
if (sock->jsmn.key_value.key == NULL || sock->jsmn.key_value.value == NULL)
|
|
{
|
|
return PARSE_JSMN_ERROR;
|
|
}
|
|
nDPIsrvd_handle_flow(sock);
|
|
if (cb(sock, user_data) != CALLBACK_OK)
|
|
{
|
|
return PARSE_CALLBACK_ERROR;
|
|
}
|
|
|
|
sock->jsmn.key_value.key = NULL;
|
|
sock->jsmn.key_value.key_length = 0;
|
|
sock->jsmn.key_value.value = NULL;
|
|
sock->jsmn.key_value.value_length = 0;
|
|
}
|
|
}
|
|
|
|
if (cb(sock, user_data) != CALLBACK_OK)
|
|
{
|
|
return PARSE_CALLBACK_ERROR;
|
|
}
|
|
nDPIsrvd_handle_flow(sock);
|
|
|
|
sock->jsmn.current_token = -1;
|
|
sock->jsmn.tokens_found = 0;
|
|
|
|
memmove(sock->buffer.raw,
|
|
sock->buffer.raw + sock->buffer.json_string_length,
|
|
sock->buffer.used - sock->buffer.json_string_length);
|
|
sock->buffer.used -= sock->buffer.json_string_length;
|
|
sock->buffer.json_string_length = 0;
|
|
sock->buffer.json_string_start = 0;
|
|
}
|
|
|
|
return PARSE_OK;
|
|
}
|
|
|
|
#endif
|