-
Notifications
You must be signed in to change notification settings - Fork 6
/
sflow.h
121 lines (101 loc) · 2.97 KB
/
sflow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef APERMON_SFLOW_H
#define APERMON_SFLOW_H
#include <stdint.h>
#include <unistd.h>
#include "config.h"
/* infomation types */
typedef uint32_t net_uint32_t;
typedef uint16_t net_uint16_t;
/* sflow protocol structs */
enum sflow_sample_proto {
SFLOW_PROTO_ETHER = 1,
};
enum sflow_sample_format {
SFLOW_SAMPLE_FORMAT_RAW = 1,
};
typedef struct _sflow_sample_element_common {
net_uint32_t format; /* enum sflow_sample_format */
net_uint32_t len;
} sflow_sample_element_common;
typedef struct _sflow_sample_element_hdr {
net_uint32_t format; /* enum sflow_sample_format */
net_uint32_t len;
net_uint32_t proto; /* enum sflow_sample_proto */
net_uint32_t orig_frame_len;
net_uint32_t stripped;
net_uint32_t hdr_len;
uint8_t hdr_bytes[];
} sflow_sample_element_hdr;
enum sflow_sampletype {
SFLOW_SAMPLETYPE_SAMPLE = 1,
};
typedef struct _sflow_sample_common {
net_uint32_t tag; /* enum sflow_sampletype */
net_uint32_t len;
} sflow_sample_common;
typedef struct _sflow_sample {
net_uint32_t tag; /* enum sflow_sampletype */
net_uint32_t len;
net_uint32_t seq;
net_uint32_t source_id;
net_uint32_t rate;
net_uint32_t pool;
net_uint32_t drops;
net_uint32_t in_ifindex;
net_uint32_t out_ifindex;
net_uint32_t n_elements;
} sflow_sample;
enum sflow_af {
SFLOW_AF_UNDEFINED = 0,
SFLOW_AF_INET = 1,
SFLOW_AF_INET6 = 2,
};
typedef struct _sflow_common_hdr {
net_uint32_t ver;
net_uint32_t agent_af; /* enum sflow_af */
} sflow_common_hdr;
typedef struct _sflow_inet_hdr {
net_uint32_t ver;
net_uint32_t agent_af; /* enum sflow_af */
uint32_t agent_inet;
net_uint32_t sub_agent_id;
net_uint32_t seq;
net_uint32_t uptime;
net_uint32_t n_samples;
} sflow_inet_hdr;
typedef struct _sflow_inet6_hdr {
net_uint32_t ver;
net_uint32_t agent_af; /* enum sflow_af */
uint8_t agent_inet6[16];
net_uint32_t sub_agent_id;
net_uint32_t seq;
net_uint32_t uptime;
net_uint32_t n_samples;
} sflow_inet6_hdr;
/* sflow protocol structs, parsed */
typedef struct _sflow_parsed_elements {
union {
const sflow_sample_element_common *common_element_hdr;
const sflow_sample_element_hdr *hdr_element;
};
struct _sflow_parsed_elements *next;
} sflow_parsed_elements;
typedef struct _sflow_parsed_samples {
const sflow_sample *sample;
sflow_parsed_elements *elements;
struct _sflow_parsed_samples *next;
} sflow_parsed_samples;
typedef struct _sflow_parsed {
union {
const sflow_common_hdr *common_hdr;
const sflow_inet_hdr *inet_hdr;
const sflow_inet6_hdr *inet6_hdr;
};
sflow_parsed_samples *samples;
} sflow_parsed;
/* functions */
ssize_t parse_sflow(const uint8_t *packet, size_t packet_len, sflow_parsed **output);
void free_sflow(sflow_parsed *parsed_pkt);
void init_sflow(const apermon_config *config);
ssize_t handle_sflow_packet(const uint8_t *packet, size_t packet_len);
#endif // APERMON_SFLOW_H