-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPFixLibESP.ino
220 lines (148 loc) · 6.37 KB
/
IPFixLibESP.ino
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdint.h>
#include <string.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include "IPFixLib.h"
#define QUEUE_SIZE 20
typedef struct IPFIX_Fields {
uint16_t type;
IPFix_Data_Field_4 field;
int is_enterprise;
uint32_t enterprise_number;
};
//queue
IPFIX_Fields fields[QUEUE_SIZE];
//uint16_t fields_types[QUEUE_SIZE];
//IPFix_Data_Field_4 fields[QUEUE_SIZE];
int queue_count = 0;
int enterprise_count = 0;
struct IPFix_Data_Field_4 Create_IPFIX_Data_Field_4_number(uint32_t _datafield)
{
IPFix_Data_Field_4 ipfixdata_data_f;
memset(&ipfixdata_data_f, 0, sizeof (IPFix_Data_Field_4));
ipfixdata_data_f.DataField.number = _datafield;
swap_bytes_4(&ipfixdata_data_f.DataField);
return ipfixdata_data_f;
}
struct IPFix_Data_Field_4 Create_IPFIX_Data_Field_4_bytes(unsigned char byte0, unsigned char byte1, unsigned char byte2, unsigned char byte3)
{
IPFix_Data_Field_4 ipfixdata_data_f;
memset(&ipfixdata_data_f, 0, sizeof (IPFix_Data_Field_4));
ipfixdata_data_f.DataField.b[3] = byte3;
ipfixdata_data_f.DataField.b[2] = byte2;
ipfixdata_data_f.DataField.b[1] = byte1;
ipfixdata_data_f.DataField.b[0] = byte0;
swap_bytes_4(&ipfixdata_data_f.DataField);
return ipfixdata_data_f;
}
void enqueue_IPFix_data(uint16_t type, IPFix_Data_Field_4 _value)
{
if (queue_count < (QUEUE_SIZE - 1))
{
fields[queue_count].type = type;
fields[queue_count].field = _value;
fields[queue_count].is_enterprise = 0;
fields[queue_count].enterprise_number = 0;
queue_count++;
}
}
void enqueue_IPFix_data_enterprise(uint16_t type, IPFix_Data_Field_4 _value, uint32_t enterprise_number)
{
if (queue_count < (QUEUE_SIZE - 1))
{
fields[queue_count].type = type;
fields[queue_count].field = _value;
fields[queue_count].is_enterprise = 1;
fields[queue_count].enterprise_number = enterprise_number;
queue_count++;
enterprise_count++;
}
}
void clear_queue()
{
queue_count = 0;
enterprise_count = 0;
}
//WiFiUDP udp, char *udpAddress, const int udpPort, uint32_t epochtime, uint32_t observ_dom_id, uint16_t set_id, uint16_t template_id
void send_IPFix_packet(WiFiUDP *udp, const char *udpAddress, const int udpPort, uint32_t epochtime, uint32_t observ_dom_id, uint16_t template_id)
{
int countlen = queue_count;
//uint16_t _length, uint32_t _export_time, uint32_t _seq_number, uint32_t _observ_dom_id
IPFix_Data_Header ipfixdata_header = Get_IPFIX_Header(header_length_calc(countlen, enterprise_count, 1), epochtime, observ_dom_id);
//uint16_t _set_id, uint16_t _length, uint16_t _template_id, uint16_t _fieldcount
IPFix_Data_Template ipfixdata_temp = Get_IPFIX_Template(2, template_length_calc(countlen, enterprise_count), template_id, countlen);
//uint16_t _set_id, uint16_t _length
IPFix_Data ipfixdata = Get_IPFIX_Data(template_id, data_length_calc(countlen));
IPFix_Data_Template_Description ipfixdata_temp_desc;
IPFix_Data_Template_Description_Enterprise ipfixdata_temp_desc_ent;
udp->beginPacket(udpAddress, udpPort);
udp->write((byte*)&ipfixdata_header, sizeof (IPFix_Data_Header));
udp->write((byte*)&ipfixdata_temp, sizeof (IPFix_Data_Template));
for (int i = 0; i < countlen; i++)
{
//uint16_t _data_type, uint16_t _data_length
if (fields[i].is_enterprise == 1)
{
ipfixdata_temp_desc_ent = Get_IPFIX_Template_Description_Enterprise(fields[i].type, 4, fields[i].enterprise_number, 1);
udp->write((byte*)&ipfixdata_temp_desc_ent, sizeof (IPFix_Data_Template_Description_Enterprise));
} else {
ipfixdata_temp_desc = Get_IPFIX_Template_Description(fields[i].type, 4);
udp->write((byte*)&ipfixdata_temp_desc, sizeof (IPFix_Data_Template_Description));
}
}
udp->write((byte*)&ipfixdata, sizeof (IPFix_Data));
for (int i = 0; i < countlen; i++)
{
udp->write((byte*)&fields[i].field, sizeof (IPFix_Data_Field_4));
}
udp->endPacket();
increment_seq_num();
clear_queue();
}
void send_IPFix_packet_only_template(WiFiUDP *udp, const char *udpAddress, const int udpPort, uint32_t epochtime, uint32_t observ_dom_id, uint16_t template_id)
{
int countlen = queue_count;
//uint16_t _length, uint32_t _export_time, uint32_t _seq_number, uint32_t _observ_dom_id
IPFix_Data_Header ipfixdata_header = Get_IPFIX_Header(header_length_calc(countlen, enterprise_count, 2), epochtime, observ_dom_id);
//uint16_t _set_id, uint16_t _length, uint16_t _template_id, uint16_t _fieldcount
IPFix_Data_Template ipfixdata_temp = Get_IPFIX_Template(2, template_length_calc(countlen, enterprise_count), template_id, countlen);
//uint16_t _set_id, uint16_t _length
IPFix_Data ipfixdata = Get_IPFIX_Data(template_id, data_length_calc(countlen));
IPFix_Data_Template_Description ipfixdata_temp_desc;
IPFix_Data_Template_Description_Enterprise ipfixdata_temp_desc_ent;
udp->beginPacket(udpAddress, udpPort);
udp->write((byte*)&ipfixdata_header, sizeof (IPFix_Data_Header));
udp->write((byte*)&ipfixdata_temp, sizeof (IPFix_Data_Template));
for (int i = 0; i < countlen; i++)
{
//uint16_t _data_type, uint16_t _data_length
if (fields[i].is_enterprise == 1)
{
ipfixdata_temp_desc_ent = Get_IPFIX_Template_Description_Enterprise(fields[i].type, 4, fields[i].enterprise_number, 1);
udp->write((byte*)&ipfixdata_temp_desc_ent, sizeof (IPFix_Data_Template_Description_Enterprise));
} else {
ipfixdata_temp_desc = Get_IPFIX_Template_Description(fields[i].type, 4);
udp->write((byte*)&ipfixdata_temp_desc, sizeof (IPFix_Data_Template_Description));
}
}
udp->endPacket();
clear_queue();
}
void send_IPFix_packet_only_data(WiFiUDP *udp, const char *udpAddress, const int udpPort, uint32_t epochtime, uint32_t observ_dom_id, uint16_t template_id)
{
int countlen = queue_count;
//uint16_t _length, uint32_t _export_time, uint32_t _seq_number, uint32_t _observ_dom_id
IPFix_Data_Header ipfixdata_header = Get_IPFIX_Header(header_length_calc(countlen, enterprise_count, 0), epochtime, observ_dom_id);
//uint16_t _set_id, uint16_t _length
IPFix_Data ipfixdata = Get_IPFIX_Data(template_id, data_length_calc(countlen));
udp->beginPacket(udpAddress, udpPort);
udp->write((byte*)&ipfixdata_header, sizeof (IPFix_Data_Header));
udp->write((byte*)&ipfixdata, sizeof (IPFix_Data));
for (int i = 0; i < countlen; i++)
{
udp->write((byte*)&fields[i].field, sizeof (IPFix_Data_Field_4));
}
udp->endPacket();
increment_seq_num();
clear_queue();
}