3 @@ -37,6 +37,9 @@ $$LIC$$
5 #include <netinet/sctp.h>
13 @@ -123,6 +126,18 @@ static uint16_t g_lasttid;
14 static ipfix_datarecord_t g_data = { NULL, NULL, 0 }; /* ipfix_export */
16 static ipfix_field_t *g_ipfix_fields;
18 +static pthread_mutex_t g_mutex;
19 +#define mod_lock() { \
20 + if ( pthread_mutex_lock( &g_mutex ) !=0 ) \
21 + mlogf( 0, "[ipfix] mutex_lock() failed: %s\n", \
22 + strerror( errno ) ); \
24 +#define mod_unlock() { pthread_mutex_unlock( &g_mutex ); }
30 /*----- prototypes -------------------------------------------------------*/
32 @@ -133,6 +148,7 @@ int _ipfix_send_message( ipfix_t *ifh,
33 ipfix_message_t *message );
34 int _ipfix_write_msghdr( ipfix_t *ifh, ipfix_message_t *msg, iobuf_t *buf );
35 void _ipfix_disconnect( ipfix_collector_t *col );
36 +int _ipfix_export_flush( ipfix_t *ifh );
39 /* name : do_writeselect
40 @@ -576,16 +592,18 @@ int ipfix_decode_float( void *in, void *
42 int ipfix_snprint_float( char *str, size_t size, void *data, size_t len )
51 - ipfix_decode_float( data, &tmp32, 4);
52 - return snprintf( str, size, "%f", tmp32 );
53 + memcpy( &tmp32, data, len );
54 + tmp32 = htonl( tmp32 );
55 + return snprintf( str, size, "%f", (float)tmp32 );
57 - ipfix_decode_float( data, &tmp64, 8);
58 - return snprintf( str, size, "%lf", tmp64);
59 + memcpy( &tmp64, data, len );
60 + tmp64 = HTONLL( tmp64 );
61 + return snprintf( str, size, "%lf", (double)tmp64 );
65 @@ -682,12 +700,19 @@ int ipfix_get_eno_ieid( char *field, int
67 * remarks: init module, read field type info.
69 -int ipfix_init ( void )
70 +int ipfix_init( void )
77 + if ( pthread_mutex_init( &g_mutex, NULL ) !=0 ) {
78 + mlogf( 0, "[ipfix] pthread_mutex_init() failed: %s\n",
83 g_tstart = time(NULL);
84 signal( SIGPIPE, SIG_IGN );
86 @@ -806,6 +831,9 @@ void ipfix_cleanup ( void )
91 + (void)pthread_mutex_destroy( &g_mutex );
95 int _ipfix_connect ( ipfix_collector_t *col )
96 @@ -1465,7 +1493,7 @@ int _ipfix_write_template( ipfix_t
99 if ( tsize+ifh->offset > IPFIX_DEFAULT_BUFLEN ) {
100 - if ( ipfix_export_flush( ifh ) < 0 )
101 + if ( _ipfix_export_flush( ifh ) < 0 )
103 if ( tsize+ifh->offset > IPFIX_DEFAULT_BUFLEN )
105 @@ -1474,6 +1502,8 @@ int _ipfix_write_template( ipfix_t
106 /* write template prior to data */
107 if ( ifh->offset > 0 ) {
108 memmove( ifh->buffer + tsize, ifh->buffer, ifh->offset );
110 + ifh->cs_header += tsize;
114 @@ -1615,8 +1645,11 @@ int ipfix_open( ipfix_t **ipfixh, int so
120 node->next = g_ipfixlist;
126 @@ -1633,7 +1666,8 @@ void ipfix_close( ipfix_t *h )
130 - ipfix_export_flush( h );
132 + _ipfix_export_flush( h );
134 while( h->collectors )
135 _ipfix_drop_collector( (ipfix_collector_t**)&h->collectors );
136 @@ -1659,6 +1693,7 @@ void ipfix_close( ipfix_t *h )
144 @@ -2156,6 +2191,22 @@ void ipfix_release_template( ipfix_t *if
145 ipfix_delete_template( ifh, templ );
148 +static void _finish_cs( ipfix_t *ifh )
153 + /* finish current dataset */
154 + if ( (buf=ifh->cs_header) ==NULL )
157 + INSERTU16( buf+buflen, buflen, ifh->cs_tid );
158 + INSERTU16( buf+buflen, buflen, ifh->cs_bytes );
160 + ifh->cs_header = NULL;
164 int ipfix_export( ipfix_t *ifh, ipfix_template_t *templ, ... )
167 @@ -2199,13 +2250,14 @@ int ipfix_export( ipfix_t *ifh, ipfix_te
168 g_data.addrs, g_data.lens );
171 -int ipfix_export_array( ipfix_t *ifh,
172 - ipfix_template_t *templ,
175 - uint16_t *lengths )
177 +_ipfix_export_array( ipfix_t *ifh,
178 + ipfix_template_t *templ,
181 + uint16_t *lengths )
185 size_t buflen, datasetlen;
188 @@ -2249,7 +2301,19 @@ int ipfix_export_array( ipfix_t
190 /** get size of data set, check space
192 - for ( i=0, datasetlen=4; i<nfields; i++ ) {
193 + if ( templ->tid == ifh->cs_tid ) {
198 + if ( ifh->cs_tid > 0 ) {
205 + for ( i=0; i<nfields; i++ ) {
206 if ( templ->fields[i].flength == IPFIX_FT_VARLEN ) {
207 if ( lengths[i]>254 )
209 @@ -2263,21 +2327,29 @@ int ipfix_export_array( ipfix_t
211 datasetlen += lengths[i];
213 - if ( ((ifh->offset + datasetlen) > IPFIX_DEFAULT_BUFLEN )
214 - && (ipfix_export_flush( ifh ) <0) ) {
217 + if ( (ifh->offset + datasetlen) > IPFIX_DEFAULT_BUFLEN ) {
222 + if ( _ipfix_export_flush( ifh ) <0 )
229 buf = (uint8_t*)(ifh->buffer) + ifh->offset;
235 - INSERTU16( buf+buflen, buflen, templ->tid );
236 - INSERTU16( buf+buflen, buflen, datasetlen );
241 + ifh->cs_header = buf;
242 + ifh->cs_tid = templ->tid;
243 + INSERTU16( buf+buflen, buflen, templ->tid );
244 + INSERTU16( buf+buflen, buflen, 4 );
247 /* insert data record
249 @@ -2303,7 +2375,9 @@ int ipfix_export_array( ipfix_t
250 buflen += lengths[i];
254 ifh->offset += buflen;
255 + ifh->cs_bytes += buflen;
256 if ( ifh->version == IPFIX_VERSION )
259 @@ -2313,7 +2387,7 @@ int ipfix_export_array( ipfix_t
261 * remarks: rewrite this func!
263 -int ipfix_export_flush( ipfix_t *ifh )
264 +int _ipfix_export_flush( ipfix_t *ifh )
267 ipfix_collector_t *col;
268 @@ -2322,8 +2396,14 @@ int ipfix_export_flush( ipfix_t *ifh )
269 if ( (ifh==NULL) || (ifh->offset==0) )
272 - if ( (buf=_ipfix_getbuf()) ==NULL )
273 + if ( ifh->cs_tid > 0 ) {
274 + /* finish current dataset */
278 + if ( (buf=_ipfix_getbuf()) ==NULL ) {
283 mlogf( 0, "[ipfix_export_flush] msg has %d records, %d bytes\n",
284 @@ -2350,3 +2430,30 @@ int ipfix_export_flush( ipfix_t *ifh )
285 _ipfix_freebuf( buf );
289 +int ipfix_export_array( ipfix_t *ifh,
290 + ipfix_template_t *templ,
293 + uint16_t *lengths )
298 + ret = _ipfix_export_array( ifh, templ, nfields, fields, lengths );
304 +int ipfix_export_flush( ipfix_t *ifh )
309 + ret = _ipfix_export_flush( ifh );
317 @@ -142,6 +142,12 @@ typedef struct
318 int nrecords; /* no. of records in buffer */
319 size_t offset; /* output buffer fill level */
320 uint32_t seqno; /* sequence no. of next message */
323 + int cs_tid; /* template id of current dataset */
324 + int cs_bytes; /* size of current set */
325 + uint8_t *cs_header; /* start of current set */
330 --- a/lib/ipfix_col.c
331 +++ b/lib/ipfix_col.c
332 @@ -907,7 +907,7 @@ int ipfix_decode_datarecord( ipfixt_node
336 -static void do_free_datarecord( ipfix_datarecord_t *data )
337 +void ipfix_free_datarecord( ipfix_datarecord_t *data )
341 @@ -925,6 +925,7 @@ int ipfix_parse_msg( ipfix_input_t *inpu
342 ipfix_hdr_t hdr; /* ipfix packet header */
344 ipfix_datarecord_t data = { NULL, NULL, 0 };
346 uint8_t *buf; /* ipfix payload */
347 uint16_t setid, setlen; /* set id, set lenght */
348 int i, nread, offset; /* counter */
349 @@ -1042,6 +1043,12 @@ int ipfix_parse_msg( ipfix_input_t *inpu
353 + for ( e=g_exporter; e!=NULL; e=e->next ) {
354 + if ( e->elem->export_dset )
355 + (void) e->elem->export_dset( t, buf+nread, setlen,
359 /** read data records
361 for ( offset=nread, bytesleft=setlen; bytesleft>4; ) {
362 @@ -1076,11 +1083,11 @@ int ipfix_parse_msg( ipfix_input_t *inpu
366 - do_free_datarecord( &data );
367 + ipfix_free_datarecord( &data );
371 - do_free_datarecord( &data );
372 + ipfix_free_datarecord( &data );
376 @@ -1093,7 +1100,7 @@ void process_client_tcp( int fd, int mas
377 tcp_conn_t *tcon = (tcp_conn_t*)data;
378 char *func = "process_client_tcp";
380 - mlogf( 3, "[%s] fd %d mask %d called.\n", func, fd, mask );
381 + mlogf( 4, "[%s] fd %d mask %d called.\n", func, fd, mask );
383 /** read ipfix header
385 --- a/lib/ipfix_col.h
386 +++ b/lib/ipfix_col.h
387 @@ -88,6 +88,7 @@ typedef struct ipfix_col_info
388 int (*export_newsource)(ipfixs_node_t*,void*);
389 int (*export_newmsg)(ipfixs_node_t*,ipfix_hdr_t*,void*);
390 int (*export_trecord)(ipfixs_node_t*,ipfixt_node_t*,void*);
391 + int (*export_dset)(ipfixt_node_t*,uint8_t*,size_t,void*);
392 int (*export_drecord)(ipfixs_node_t*,ipfixt_node_t*,
393 ipfix_datarecord_t*,void*);
394 void (*export_cleanup)(void*);
395 --- a/lib/ipfix_col_files.c
396 +++ b/lib/ipfix_col_files.c
397 @@ -68,7 +68,7 @@ static int export_newsource_file( ipfixs
400 snprintf( s->fname+strlen(s->fname), PATH_MAX-strlen(s->fname),
402 + "/%u", (unsigned int)s->odid );
403 if ( (access( s->fname, R_OK ) <0 )
404 && (mkdir( s->fname, S_IRWXU ) <0) ) {
405 mlogf( 0, "[%s] cannot access dir '%s': %s\n",