Skip to content

Commit 8cffaf6

Browse files
committed
initial import
0 parents  commit 8cffaf6

File tree

100 files changed

+35587
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+35587
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin/
2+
pkg/
3+
libkflow.*

demo.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include "kflow.h"
4+
5+
int main(int argc, char **argv) {
6+
int r;
7+
kflowConfig cfg = {
8+
.URL = "http://chdev:20012/chf",
9+
.API = {
10+
.email = "[email protected]",
11+
.token = "81b7262feceecc94eef3ddafbc2c152f",
12+
.URL = "http://chdev:8080/api/v5",
13+
},
14+
.device_id = 1001,
15+
.verbose = 1,
16+
};
17+
18+
if ((r = kflowInit(&cfg)) != 0) {
19+
printf("error initializing libkflow: %d", r);
20+
exit(1);
21+
};
22+
23+
kflow flow = {
24+
.deviceId = cfg.device_id,
25+
.ipv4SrcAddr = 167772161,
26+
.ipv4DstAddr = 167772162,
27+
.srcAs = 1234,
28+
.inPkts = 20,
29+
.inBytes = 40,
30+
};
31+
32+
if ((r = kflowSend(&flow)) != 0) {
33+
printf("error sending flow: %d", r);
34+
exit(1);
35+
}
36+
37+
if ((r = kflowStop(10*1000)) != 0) {
38+
printf("error stopping libkflow: %d", r);
39+
exit(1);
40+
}
41+
42+
return 0;
43+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package api
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
"time"
10+
)
11+
12+
type Client struct {
13+
Header http.Header
14+
*http.Client
15+
}
16+
17+
func NewClient(email, token string, timeout time.Duration) *Client {
18+
client := &http.Client{
19+
Timeout: timeout,
20+
}
21+
22+
header := http.Header{
23+
"X-CH-Auth-Email": {email},
24+
"X-CH-Auth-API-Token": {token},
25+
}
26+
27+
return &Client{
28+
Header: header,
29+
Client: client,
30+
}
31+
}
32+
33+
func (c *Client) GetDevice(url string, did int) (*Device, error) {
34+
r, err := c.do("GET", fmt.Sprintf("%s/device/%d", url, did), nil)
35+
if err != nil {
36+
return nil, err
37+
}
38+
defer r.Body.Close()
39+
40+
if r.StatusCode != 200 {
41+
return nil, fmt.Errorf("api: HTTP status code %d", r.StatusCode)
42+
}
43+
44+
dr := &DeviceResponse{}
45+
if err := json.NewDecoder(r.Body).Decode(dr); err != nil {
46+
return nil, err
47+
}
48+
49+
return &dr.Device, nil
50+
}
51+
52+
func (c *Client) SendFlow(url string, buf *bytes.Buffer) error {
53+
r, err := c.do("POST", url, buf)
54+
if err != nil {
55+
return err
56+
}
57+
defer r.Body.Close()
58+
59+
if r.StatusCode != 200 {
60+
return fmt.Errorf("api: HTTP status code %d", r.StatusCode)
61+
}
62+
63+
return nil
64+
}
65+
66+
func (c *Client) do(method, url string, body io.Reader) (*http.Response, error) {
67+
r, err := http.NewRequest(method, url, body)
68+
if err != nil {
69+
return nil, err
70+
}
71+
r.Header = c.Header
72+
return c.Client.Do(r)
73+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strconv"
7+
)
8+
9+
type DeviceResponse struct {
10+
Device Device `json:"device"`
11+
}
12+
13+
type Device struct {
14+
ID string `json:"id"`
15+
Name string `json:"device_name"`
16+
CompanyID string `json:"company_id"`
17+
Custom CustomColumns `json:"custom_columns"`
18+
}
19+
20+
type CustomColumns map[string]uint64
21+
22+
func (d *Device) ClientID() string {
23+
return fmt.Sprintf("%s:%s:%s", d.CompanyID, d.Name, d.ID)
24+
}
25+
26+
func (c *CustomColumns) UnmarshalJSON(data []byte) error {
27+
m := map[string]uint64{}
28+
for _, match := range split.FindAllSubmatchIndex(data, -1) {
29+
key := string(data[match[2]:match[3]])
30+
val := string(data[match[4]:match[5]])
31+
m[key], _ = strconv.ParseUint(val, 10, 64)
32+
}
33+
*c = m
34+
return nil
35+
}
36+
37+
var split = regexp.MustCompile(`(\w+)=(\d+),?`)

0 commit comments

Comments
 (0)