summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2020-11-13 14:02:39 +0100
committerToni Uhlig <matzeton@googlemail.com>2020-11-13 14:09:19 +0100
commit7362da8c5fac01b97ee2060eb3e640ed8565ea24 (patch)
treea21ca20992cc069d20ca39a23a58a668808e0c78 /examples
parent8c81c7c8db9886555b326145674ad97199549bdd (diff)
Finished unfinished nDPIsrvd.h helper functionality.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/c-captured/c-captured.c129
1 files changed, 35 insertions, 94 deletions
diff --git a/examples/c-captured/c-captured.c b/examples/c-captured/c-captured.c
index eb56470e7..8b6785aea 100644
--- a/examples/c-captured/c-captured.c
+++ b/examples/c-captured/c-captured.c
@@ -7,127 +7,68 @@
#include <sys/types.h>
#include <unistd.h>
-#include "config.h"
-#include "jsmn.h"
+#include "nDPIsrvd.h"
+#include "jsmn/jsmn.h"
static char serv_listen_addr[INET_ADDRSTRLEN] = DISTRIBUTOR_HOST;
static uint16_t serv_listen_port = DISTRIBUTOR_PORT;
-int main(void)
+static enum nDPIsrvd_callback_return nDPIsrvd_json_callback(struct nDPIsrvd_socket * const sock, void * user_data)
{
- int sockfd = socket(AF_INET, SOCK_STREAM, 0);
- struct sockaddr_in remote_addr = {};
- socklen_t remote_addrlen = sizeof(remote_addr);
- uint8_t buf[NETWORK_BUFFER_MAX_SIZE];
- size_t buf_used = 0;
- size_t json_start = 0;
- unsigned long long int json_bytes = 0;
- jsmn_parser parser;
- jsmntok_t tokens[128];
+ (void)user_data;
- if (sockfd < 0)
+ if (sock->jsmn.current_token % 2 == 1)
{
- perror("socket");
- return 1;
+ printf("[%.*s : ",
+ sock->jsmn.tokens[sock->jsmn.current_token].end - sock->jsmn.tokens[sock->jsmn.current_token].start,
+ sock->buffer.json_string + sock->jsmn.tokens[sock->jsmn.current_token].start);
+ }
+ else
+ {
+ printf("%.*s] ",
+ sock->jsmn.tokens[sock->jsmn.current_token].end - sock->jsmn.tokens[sock->jsmn.current_token].start,
+ sock->buffer.json_string + sock->jsmn.tokens[sock->jsmn.current_token].start);
}
- remote_addr.sin_family = AF_INET;
- if (inet_pton(AF_INET, &serv_listen_addr[0], &remote_addr.sin_addr) != 1)
+ return CALLBACK_OK;
+}
+
+int main(int argc, char ** argv)
+{
+ struct nDPIsrvd_socket * sock = nDPIsrvd_init();
+
+ (void)argc;
+
+ if (sock == NULL)
{
- perror("inet_pton");
+ fprintf(stderr, "%s: nDPIsrvd socket memory allocation failed!\n", argv[0]);
return 1;
}
- remote_addr.sin_port = htons(serv_listen_port);
- if (connect(sockfd, (struct sockaddr *)&remote_addr, remote_addrlen) != 0)
+ printf("Connecting to %s:%u\n", serv_listen_addr, serv_listen_port);
+ enum nDPIsrvd_connect_return connect_ret = nDPIsrvd_connect_ip(sock, serv_listen_addr, serv_listen_port);
+ if (connect_ret != CONNECT_OK)
{
- perror("connect");
+ fprintf(stderr, "%s: nDPIsrvd socket connect failed!\n", argv[0]);
return 1;
}
while (1)
{
errno = 0;
- ssize_t bytes_read = read(sockfd, buf + buf_used, sizeof(buf) - buf_used);
-
- if (bytes_read <= 0 || errno != 0)
+ enum nDPIsrvd_read_return read_ret = nDPIsrvd_read(sock);
+ if (read_ret != READ_OK)
{
- fprintf(stderr, "Remote end disconnected.\n");
break;
}
- buf_used += bytes_read;
- while (buf_used >= nDPIsrvd_JSON_BYTES + 1)
+ enum nDPIsrvd_parse_return parse_ret = nDPIsrvd_parse(sock, nDPIsrvd_json_callback, NULL);
+ switch (parse_ret)
{
- if (buf[nDPIsrvd_JSON_BYTES] != '{')
- {
- fprintf(stderr, "BUG: JSON invalid opening character: '%c'\n", buf[nDPIsrvd_JSON_BYTES]);
- exit(1);
- }
-
- char * json_str_start = NULL;
- json_bytes = strtoull((char *)buf, &json_str_start, 10);
- json_bytes += (uint8_t *)json_str_start - buf;
- json_start = (uint8_t *)json_str_start - buf;
-
- if (errno == ERANGE)
- {
- fprintf(stderr, "BUG: Size of JSON exceeds limit\n");
- exit(1);
- }
- if ((uint8_t *)json_str_start == buf)
- {
- fprintf(stderr, "BUG: Missing size before JSON string: \"%.*s\"\n", nDPIsrvd_JSON_BYTES, buf);
- exit(1);
- }
- if (json_bytes > sizeof(buf))
- {
- fprintf(stderr, "BUG: JSON string too big: %llu > %zu\n", json_bytes, sizeof(buf));
- exit(1);
- }
- if (json_bytes > buf_used)
- {
+ default:
break;
- }
-
- if (buf[json_bytes - 1] != '}')
- {
- fprintf(stderr, "BUG: Invalid JSON string: %.*s\n", (int)json_bytes, buf);
- exit(1);
- }
-
- int r;
- jsmn_init(&parser);
- r = jsmn_parse(&parser,
- (char *)(buf + json_start),
- json_bytes - json_start,
- tokens,
- sizeof(tokens) / sizeof(tokens[0]));
- if (r < 0 || tokens[0].type != JSMN_OBJECT)
- {
- fprintf(stderr, "JSON parsing failed with return value %d at position %u\n", r, parser.pos);
- fprintf(stderr, "JSON string: '%.*s'\n", (int)(json_bytes - json_start), (char *)(buf + json_start));
- exit(1);
- }
-
- for (int i = 1; i < r; i++)
- {
- if (i % 2 == 1)
- {
- printf("[%.*s : ", tokens[i].end - tokens[i].start, (char *)(buf + json_start) + tokens[i].start);
- }
- else
- {
- printf("%.*s] ", tokens[i].end - tokens[i].start, (char *)(buf + json_start) + tokens[i].start);
- }
- }
- printf("EoF\n");
-
- memmove(buf, buf + json_bytes, buf_used - json_bytes);
- buf_used -= json_bytes;
- json_bytes = 0;
- json_start = 0;
}
+ printf("EoF\n");
}
return 0;