aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2020-08-31 08:45:36 +0200
committerToni Uhlig <matzeton@googlemail.com>2020-08-31 08:52:17 +0200
commit9c3b5170fc0429a579b556cc864bc8597a37500c (patch)
tree12d8bc17b4d205ac75b4e9fd3e74146bba6f64b7
parent905d84506ece0ce6612301146f11fe31cea18923 (diff)
Added golang JSON deserializer example.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r--Makefile16
-rw-r--r--examples/go-dashboard/main.go100
2 files changed, 113 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index bbea5e99d..b3d942d1f 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,9 @@ CC = gcc
PROJECT_CFLAGS += -Wall -Wextra $(EXTRA_CFLAGS) -I.
LIBS += -pthread -lpcap -lm
+GOCC = go
+GOFLAGS = -ldflags='-s -w'
+
ifneq ($(PKG_CONFIG_BIN),)
ifneq ($(PKG_CONFIG_PREFIX),)
PC_CFLAGS=$(shell PKG_CONFIG_PATH=$(shell realpath $(PKG_CONFIG_PREFIX)) $(PKG_CONFIG_BIN) --define-prefix=$(shell realpath $(PKG_CONFIG_PREFIX)) --cflags libndpi)
@@ -67,7 +70,7 @@ RM = rm -f
all: help nDPId nDPIsrvd
-examples: examples/c-json-stdout/c-json-stdout
+examples: examples/c-json-stdout/c-json-stdout examples/go-dashboard/go-dashboard
nDPId: nDPId.c utils.c
$(CC) $(PROJECT_CFLAGS) $(CFLAGS) $(PC_CFLAGS) $^ -o $@ $(LDFLAGS) $(PC_LDFLAGS) $(LIBS)
@@ -75,15 +78,20 @@ nDPId: nDPId.c utils.c
nDPIsrvd: nDPIsrvd.c utils.c
$(CC) $(PROJECT_CFLAGS) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(LIBS)
-examples/c-json-stdout/c-json-stdout:
+examples/c-json-stdout/c-json-stdout: examples/c-json-stdout/c-json-stdout.c
ifneq ($(DISABLE_JSMN),yes)
$(CC) $(PROJECT_CFLAGS) $(CFLAGS) -DJSMN_STATIC=1 -DJSMN_STRICT=1 -DUSE_JSON=1 $@.c -o $@ $(LDFLAGS) $(LIBS)
else
$(CC) $(PROJECT_CFLAGS) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LIBS)
endif
+examples/go-dashboard/go-dashboard: examples/go-dashboard/main.go
+ifneq ($(GOCC),)
+ $(GOCC) build -o examples/go-dashboard/go-dashboard $(GOFLAGS) examples/go-dashboard/main.go
+endif
+
clean:
- $(RM) -f nDPId nDPIsrvd examples/c-json-stdout/c-json-stdout
+ $(RM) -f nDPId nDPIsrvd examples/c-json-stdout/c-json-stdout examples/go-dashboard/go-dashboard
help:
@echo '------------------------------------'
@@ -96,6 +104,8 @@ help:
@echo 'LDFLAGS = $(LDFLAGS)'
@echo 'PROJECT_CFLAGS = $(PROJECT_CFLAGS)'
@echo 'LIBS = $(LIBS)'
+ @echo 'GOCC = $(GOCC)'
+ @echo 'GOFLAGS = $(GOFLAGS)'
@echo 'CUSTOM_LIBNDPI = $(CUSTOM_LIBNDPI)'
@echo 'NDPI_WITH_GCRYPT = $(NDPI_WITH_GCRYPT)'
@echo 'NDPI_WITH_PCRE = $(NDPI_WITH_PCRE)'
diff --git a/examples/go-dashboard/main.go b/examples/go-dashboard/main.go
new file mode 100644
index 000000000..b9dcd314d
--- /dev/null
+++ b/examples/go-dashboard/main.go
@@ -0,0 +1,100 @@
+package main
+
+import (
+ "bufio"
+ "encoding/json"
+ "io"
+ "log"
+ "net"
+ "os"
+ "strconv"
+ "strings"
+)
+
+var (
+ WarningLogger *log.Logger
+ InfoLogger *log.Logger
+ ErrorLogger *log.Logger
+
+ NETWORK_BUFFER_MAX_SIZE uint16 = 9216
+ nDPIsrvd_JSON_BYTES uint16 = 4
+)
+
+func main() {
+ InfoLogger = log.New(os.Stderr, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
+ WarningLogger = log.New(os.Stderr, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile)
+ ErrorLogger = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
+
+ conn, _ := net.Dial("tcp", "127.0.0.1:7000")
+
+ buf := make([]byte, NETWORK_BUFFER_MAX_SIZE)
+ var jsonStr string
+ var jsonStrLen uint16 = 0
+ var jsonLen uint16 = 0
+
+ rd := bufio.NewReaderSize(conn, int(NETWORK_BUFFER_MAX_SIZE))
+ for {
+ nread, err := rd.Read(buf)
+
+ if err != nil {
+ if err != io.EOF {
+ ErrorLogger.Printf("Read Error: %v\n", err)
+ break
+ }
+ }
+
+ if nread == 0 || err == io.EOF {
+ WarningLogger.Printf("Disconnect from Server\n")
+ break
+ }
+
+ jsonStr += string(buf[:nread])
+ jsonStrLen += uint16(nread)
+
+ for {
+ if jsonStrLen < nDPIsrvd_JSON_BYTES+1 {
+ break
+ }
+
+ if jsonStr[nDPIsrvd_JSON_BYTES] != '{' {
+ ErrorLogger.Printf("BUG: JSON invalid opening character at position %d: '%s' (%x)\n",
+ nDPIsrvd_JSON_BYTES,
+ string(jsonStr[:nDPIsrvd_JSON_BYTES]), jsonStr[nDPIsrvd_JSON_BYTES])
+ os.Exit(1)
+ }
+
+ if jsonLen == 0 {
+ var tmp uint64
+ if tmp, err = strconv.ParseUint(strings.TrimLeft(jsonStr[:4], "0"), 10, 16); err != nil {
+ ErrorLogger.Printf("BUG: Could not parse length of a JSON string: %v\n", err)
+ os.Exit(1)
+ } else {
+ jsonLen = uint16(tmp)
+ }
+ }
+
+ if jsonStrLen < jsonLen+nDPIsrvd_JSON_BYTES {
+ break
+ }
+
+ if jsonStr[jsonLen+nDPIsrvd_JSON_BYTES-1] != '}' {
+ ErrorLogger.Printf("BUG: JSON invalid closing character at position %d: '%s'\n",
+ jsonLen+nDPIsrvd_JSON_BYTES,
+ string(jsonStr[jsonLen+nDPIsrvd_JSON_BYTES-1]))
+ os.Exit(1)
+ }
+
+ jsonMap := make(map[string]interface{})
+ err := json.Unmarshal([]byte(jsonStr[nDPIsrvd_JSON_BYTES:nDPIsrvd_JSON_BYTES+jsonLen]), &jsonMap)
+ if err != nil {
+ ErrorLogger.Printf("BUG: JSON error: %v\n", err)
+ os.Exit(1)
+ }
+ InfoLogger.Printf("JSON map: %v\n-------------------------------------------------------\n", jsonMap)
+
+ jsonStr = jsonStr[jsonLen+nDPIsrvd_JSON_BYTES:]
+ jsonStrLen -= (jsonLen + nDPIsrvd_JSON_BYTES)
+ jsonLen = 0
+ }
+ }
+}