summaryrefslogtreecommitdiff
path: root/api/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/main.go')
-rw-r--r--api/main.go37
1 files changed, 34 insertions, 3 deletions
diff --git a/api/main.go b/api/main.go
index 69cf981..7081d3c 100644
--- a/api/main.go
+++ b/api/main.go
@@ -1,13 +1,23 @@
package api
import (
+ "encoding/json"
"log"
"net"
"sinanmohd.com/redq/usage"
)
-const sockPath = "/tmp/redq_ebpf.sock"
+const (
+ sockPath = "/tmp/redq_ebpf.sock"
+ bufSize = 4096
+)
+
+type ApiReq struct {
+ Type string `json:"type"`
+ Action string `json:"action"`
+ Arg string `json:"arg"`
+}
type Api struct {
sock net.Listener
@@ -38,10 +48,31 @@ func (a *Api) Run(u *usage.Usage) {
continue
}
- go handleConn(conn)
+ go handleConn(conn, u)
}
}
-func handleConn(conn net.Conn) {
+func handleConn(conn net.Conn, u *usage.Usage) {
defer conn.Close()
+ var req ApiReq
+ buf := make([]byte, bufSize)
+
+ count, err := conn.Read(buf)
+ if err != nil {
+ log.Printf("reading to buffer: %s", err)
+ return
+ }
+
+ err = json.Unmarshal(buf[:count], &req)
+ if err != nil {
+ log.Printf("unmarshaling json: %s", err)
+ return
+ }
+
+ switch req.Type {
+ case "bandwidth":
+ handleBandwidth(conn, u)
+ default:
+ log.Printf("invalid request type: %s", req.Type)
+ }
}