mirror of
https://github.com/optim-enterprises-bv/nDPId.git
synced 2025-11-02 03:07:49 +00:00
added basic "consumer" example
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
This commit is contained in:
13
Makefile
13
Makefile
@@ -1,5 +1,5 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
PROJECT_CFLAGS += -Wall -Wextra $(EXTRA_CFLAGS)
|
PROJECT_CFLAGS += -Wall -Wextra $(EXTRA_CFLAGS) -I.
|
||||||
LIBS += -pthread -lpcap -lm
|
LIBS += -pthread -lpcap -lm
|
||||||
|
|
||||||
ifneq ($(CUSTOM_LIBNDPI),)
|
ifneq ($(CUSTOM_LIBNDPI),)
|
||||||
@@ -37,14 +37,19 @@ RM = rm -f
|
|||||||
|
|
||||||
all: nDPId nDPIsrvd
|
all: nDPId nDPIsrvd
|
||||||
|
|
||||||
|
examples: examples/c-json-stdout/c-json-stdout
|
||||||
|
|
||||||
nDPId: help nDPId.c
|
nDPId: help nDPId.c
|
||||||
$(CC) $(PROJECT_CFLAGS) $(CFLAGS) nDPId.c -o $@ $(LDFLAGS) $(LIBS)
|
$(CC) $(PROJECT_CFLAGS) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LIBS)
|
||||||
|
|
||||||
nDPIsrvd: nDPIsrvd.c
|
nDPIsrvd: nDPIsrvd.c
|
||||||
$(CC) $(PROJECT_CFLAGS) $(CFLAGS) nDPIsrvd.c -o $@ $(LDFLAGS) $(LIBS)
|
$(CC) $(PROJECT_CFLAGS) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LIBS)
|
||||||
|
|
||||||
|
examples/c-json-stdout/c-json-stdout:
|
||||||
|
$(CC) $(PROJECT_CFLAGS) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LIBS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) -f nDPId nDPIsrvd
|
$(RM) -f nDPId nDPIsrvd examples/c-json-stdout/c-json-stdout
|
||||||
|
|
||||||
help:
|
help:
|
||||||
@echo 'CC = $(CC)'
|
@echo 'CC = $(CC)'
|
||||||
|
|||||||
52
examples/c-json-stdout/c-json-stdout.c
Normal file
52
examples/c-json-stdout/c-json-stdout.c
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "config.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)
|
||||||
|
{
|
||||||
|
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
struct sockaddr_in remote_addr = {};
|
||||||
|
socklen_t remote_addrlen = sizeof(remote_addr);
|
||||||
|
uint8_t buf[BUFSIZ];
|
||||||
|
//size_t buf_used = 0;
|
||||||
|
//unsigned long long int buf_wanted = 0;
|
||||||
|
|
||||||
|
if (sockfd < 0) {
|
||||||
|
perror("socket");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
remote_addr.sin_family = AF_INET;
|
||||||
|
if (inet_pton(AF_INET, &serv_listen_addr[0], &remote_addr.sin_addr) != 1) {
|
||||||
|
perror("inet_pton");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
remote_addr.sin_port = htons(serv_listen_port);
|
||||||
|
|
||||||
|
if (connect(sockfd, (struct sockaddr *) &remote_addr, remote_addrlen) != 0) {
|
||||||
|
perror("connect");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
errno = 0;
|
||||||
|
ssize_t bytes_read = read(sockfd, buf, sizeof(buf));
|
||||||
|
|
||||||
|
if (bytes_read <= 0 || errno != 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("RECV[%zd]: '%.*s'\n", bytes_read, (int) bytes_read, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user