-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathnetwork.c
More file actions
3333 lines (2779 loc) · 101 KB
/
Copy pathnetwork.c
File metadata and controls
3333 lines (2779 loc) · 101 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* collectd - src/network.c
* Copyright (C) 2005-2013 Florian octo Forster
* Copyright (C) 2009 Aman Gupta
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; only version 2.1 of the License is
* applicable.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Florian octo Forster <octo at collectd.org>
* Aman Gupta <aman at tmm1.net>
**/
#define _DEFAULT_SOURCE
#define _BSD_SOURCE /* For struct ip_mreq */
#include "collectd.h"
#include "plugin.h"
#include "utils/common/common.h"
#include "utils_cache.h"
#include "utils_complain.h"
#include "utils_fbhash.h"
#include "network.h"
#if HAVE_NETDB_H
#include <netdb.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#if HAVE_POLL_H
#include <poll.h>
#endif
#if HAVE_NET_IF_H
#include <net/if.h>
#endif
#if HAVE_GCRYPT_H
#if defined __APPLE__
/* default xcode compiler throws warnings even when deprecated functionality
* is not used. -Werror breaks the build because of erroneous warnings.
* http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
*/
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
/* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
* to properly hide all deprecated functionality.
* http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
*/
#define GCRYPT_NO_DEPRECATED
#include <gcrypt.h>
#if defined __APPLE__
/* Re enable deprecation warnings */
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
#endif
#if GCRYPT_VERSION_NUMBER < 0x010600
GCRY_THREAD_OPTION_PTHREAD_IMPL;
#endif
#endif
#ifndef IPV6_ADD_MEMBERSHIP
#ifdef IPV6_JOIN_GROUP
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
#else
#error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
#endif
#endif /* !IP_ADD_MEMBERSHIP */
/*
* Maximum size required for encryption / signing:
*
* 42 bytes for the encryption header
* + 64 bytes for the username
* -----------
* = 106 bytes
*/
#define BUFF_SIG_SIZE 106
/*
* Private data types
*/
#define SECURITY_LEVEL_NONE 0
#if HAVE_GCRYPT_H
#define SECURITY_LEVEL_SIGN 1
#define SECURITY_LEVEL_ENCRYPT 2
#endif
struct sockent_client {
int fd;
struct sockaddr_storage *addr;
socklen_t addrlen;
#if HAVE_GCRYPT_H
int security_level;
char *username;
char *password;
gcry_cipher_hd_t cypher;
unsigned char password_hash[32];
#endif
cdtime_t next_resolve_reconnect;
cdtime_t resolve_interval;
struct sockaddr_storage *bind_addr;
};
struct sockent_server {
int *fd;
size_t fd_num;
#if HAVE_GCRYPT_H
int security_level;
char *auth_file;
fbhash_t *userdb;
gcry_cipher_hd_t cypher;
#endif
};
typedef struct sockent {
#define SOCKENT_TYPE_CLIENT 1
#define SOCKENT_TYPE_SERVER 2
int type;
char *node;
char *service;
int interface;
union {
struct sockent_client client;
struct sockent_server server;
} data;
struct sockent *next;
pthread_mutex_t lock;
} sockent_t;
/* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-------+-----------------------+-------------------------------+
* ! Ver. ! ! Length !
* +-------+-----------------------+-------------------------------+
*/
struct part_header_s {
uint16_t type;
uint16_t length;
};
typedef struct part_header_s part_header_t;
/* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-------------------------------+-------------------------------+
* ! Type ! Length !
* +-------------------------------+-------------------------------+
* : (Length - 4) Bytes :
* +---------------------------------------------------------------+
*/
struct part_string_s {
part_header_t *head;
char *value;
};
typedef struct part_string_s part_string_t;
/* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-------------------------------+-------------------------------+
* ! Type ! Length !
* +-------------------------------+-------------------------------+
* : (Length - 4 == 2 || 4 || 8) Bytes :
* +---------------------------------------------------------------+
*/
struct part_number_s {
part_header_t *head;
uint64_t *value;
};
typedef struct part_number_s part_number_t;
/* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-------------------------------+-------------------------------+
* ! Type ! Length !
* +-------------------------------+---------------+---------------+
* ! Num of values ! Type0 ! Type1 !
* +-------------------------------+---------------+---------------+
* ! Value0 !
* ! !
* +---------------------------------------------------------------+
* ! Value1 !
* ! !
* +---------------------------------------------------------------+
*/
struct part_values_s {
part_header_t *head;
uint16_t *num_values;
uint8_t *values_types;
value_t *values;
};
typedef struct part_values_s part_values_t;
/* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-------------------------------+-------------------------------+
* ! Type ! Length !
* +-------------------------------+-------------------------------+
* ! Hash (Bits 0 - 31) !
* : : :
* ! Hash (Bits 224 - 255) !
* +---------------------------------------------------------------+
*/
/* Minimum size */
#define PART_SIGNATURE_SHA256_SIZE 36
struct part_signature_sha256_s {
part_header_t head;
unsigned char hash[32];
char *username;
};
typedef struct part_signature_sha256_s part_signature_sha256_t;
/* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-------------------------------+-------------------------------+
* ! Type ! Length !
* +-------------------------------+-------------------------------+
* ! Original length ! Padding (0 - 15 bytes) !
* +-------------------------------+-------------------------------+
* ! Hash (Bits 0 - 31) !
* : : :
* ! Hash (Bits 128 - 159) !
* +---------------------------------------------------------------+
*/
/* Minimum size */
#define PART_ENCRYPTION_AES256_SIZE 42
struct part_encryption_aes256_s {
part_header_t head;
uint16_t username_length;
char *username;
unsigned char iv[16];
/* <encrypted> */
unsigned char hash[20];
/* <payload /> */
/* </encrypted> */
};
typedef struct part_encryption_aes256_s part_encryption_aes256_t;
struct receive_list_entry_s {
char *data;
int data_len;
int fd;
struct sockaddr_storage sender;
struct receive_list_entry_s *next;
};
typedef struct receive_list_entry_s receive_list_entry_t;
/*
* Private variables
*/
static int network_config_ttl;
/* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
static size_t network_config_packet_size = 1452;
static bool network_config_forward;
static bool network_config_stats;
static sockent_t *sending_sockets;
static receive_list_entry_t *receive_list_head;
static receive_list_entry_t *receive_list_tail;
static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
static uint64_t receive_list_length;
static sockent_t *listen_sockets;
static struct pollfd *listen_sockets_pollfd;
static size_t listen_sockets_num;
/* The receive and dispatch threads will run as long as `listen_loop' is set to
* zero. */
static int listen_loop;
static int receive_thread_running;
static pthread_t receive_thread_id;
static int dispatch_thread_running;
static pthread_t dispatch_thread_id;
/* Buffer in which to-be-sent network packets are constructed. */
static char *send_buffer;
static char *send_buffer_ptr;
static int send_buffer_fill;
static cdtime_t send_buffer_first_write;
static value_list_t send_buffer_vl = VALUE_LIST_INIT;
static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
/* XXX: These counters are incremented from one place only. The spot in which
* the values are incremented is either only reachable by one thread (the
* dispatch thread, for example) or locked by some lock (send_buffer_lock for
* example). Only if neither is true, the stats_lock is acquired. The counters
* are always read without holding a lock in the hope that writing 8 bytes to
* memory is an atomic operation. */
static derive_t stats_octets_rx;
static derive_t stats_octets_tx;
static derive_t stats_packets_rx;
static derive_t stats_packets_tx;
static derive_t stats_values_dispatched;
static derive_t stats_values_not_dispatched;
static derive_t stats_values_sent;
static derive_t stats_values_not_sent;
static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
/*
* Private functions
*/
static bool check_receive_okay(const value_list_t *vl) /* {{{ */
{
uint64_t time_sent = 0;
int status;
status = uc_meta_data_get_unsigned_int(vl, "network:time_sent", &time_sent);
/* This is a value we already sent. Don't allow it to be received again in
* order to avoid looping. */
if ((status == 0) && (time_sent >= ((uint64_t)vl->time)))
return 0;
return 1;
} /* }}} bool check_receive_okay */
static bool check_send_okay(const value_list_t *vl) /* {{{ */
{
bool received = 0;
int status;
if (network_config_forward)
return 1;
if (vl->meta == NULL)
return 1;
status = meta_data_get_boolean(vl->meta, "network:received", &received);
if (status == -ENOENT)
return 1;
else if (status != 0) {
ERROR("network plugin: check_send_okay: meta_data_get_boolean failed "
"with status %i.",
status);
return 1;
}
/* By default, only *send* value lists that were not *received* by the
* network plugin. */
return !received;
} /* }}} bool check_send_okay */
static bool check_notify_received(const notification_t *n) /* {{{ */
{
for (notification_meta_t *ptr = n->meta; ptr != NULL; ptr = ptr->next)
if ((strcmp("network:received", ptr->name) == 0) &&
(ptr->type == NM_TYPE_BOOLEAN))
return (bool)ptr->nm_value.nm_boolean;
return 0;
} /* }}} bool check_notify_received */
static bool check_send_notify_okay(const notification_t *n) /* {{{ */
{
static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
bool received = 0;
if (n->meta == NULL)
return 1;
received = check_notify_received(n);
if (network_config_forward && received) {
c_complain_once(
LOG_ERR, &complain_forwarding,
"network plugin: A notification has been received via the network "
"and forwarding is enabled. Forwarding of notifications is currently "
"not supported, because there is not loop-detection available. "
"Please contact the collectd mailing list if you need this "
"feature.");
}
/* By default, only *send* value lists that were not *received* by the
* network plugin. */
return !received;
} /* }}} bool check_send_notify_okay */
static int network_dispatch_values(value_list_t *vl, /* {{{ */
const char *username,
struct sockaddr_storage *address) {
int status;
if ((vl->time == 0) || (strlen(vl->host) == 0) || (strlen(vl->plugin) == 0) ||
(strlen(vl->type) == 0))
return -EINVAL;
if (!check_receive_okay(vl)) {
#if COLLECT_DEBUG
char name[6 * DATA_MAX_NAME_LEN];
FORMAT_VL(name, sizeof(name), vl);
name[sizeof(name) - 1] = '\0';
DEBUG("network plugin: network_dispatch_values: "
"NOT dispatching %s.",
name);
#endif
stats_values_not_dispatched++;
return 0;
}
assert(vl->meta == NULL);
vl->meta = meta_data_create();
if (vl->meta == NULL) {
ERROR("network plugin: meta_data_create failed.");
return -ENOMEM;
}
status = meta_data_add_boolean(vl->meta, "network:received", 1);
if (status != 0) {
ERROR("network plugin: meta_data_add_boolean failed.");
meta_data_destroy(vl->meta);
vl->meta = NULL;
return status;
}
if (username != NULL) {
status = meta_data_add_string(vl->meta, "network:username", username);
if (status != 0) {
ERROR("network plugin: meta_data_add_string failed.");
meta_data_destroy(vl->meta);
vl->meta = NULL;
return status;
}
}
if (address != NULL) {
char host[48];
size_t len = sizeof(struct sockaddr_storage);
#ifdef __NetBSD__
if (address->ss_family == AF_INET) {
len = sizeof(struct sockaddr_in);
} else if (address->ss_family == AF_INET6) {
len = sizeof(struct sockaddr_in6);
}
#endif
status = getnameinfo((struct sockaddr *)address, len, host, sizeof(host),
NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV);
if (status != 0) {
ERROR("network plugin: getnameinfo failed: %s", gai_strerror(status));
meta_data_destroy(vl->meta);
vl->meta = NULL;
return status;
}
status = meta_data_add_string(vl->meta, "network:ip_address", host);
if (status != 0) {
ERROR("network plugin: meta_data_add_string failed.");
meta_data_destroy(vl->meta);
vl->meta = NULL;
return status;
}
}
plugin_dispatch_values(vl);
stats_values_dispatched++;
meta_data_destroy(vl->meta);
vl->meta = NULL;
return 0;
} /* }}} int network_dispatch_values */
static int network_dispatch_notification(notification_t *n) /* {{{ */
{
int status;
assert(n->meta == NULL);
status = plugin_notification_meta_add_boolean(n, "network:received", 1);
if (status != 0) {
ERROR("network plugin: plugin_notification_meta_add_boolean failed.");
plugin_notification_meta_free(n->meta);
n->meta = NULL;
return status;
}
status = plugin_dispatch_notification(n);
plugin_notification_meta_free(n->meta);
n->meta = NULL;
return status;
} /* }}} int network_dispatch_notification */
#if HAVE_GCRYPT_H
static int network_init_gcrypt(void) /* {{{ */
{
gcry_error_t err;
/* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html
* Because you can't know in a library whether another library has
* already initialized the library */
if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P))
return 0;
/* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
* To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS
* *before* initalizing Libgcrypt with gcry_check_version(), which itself must
* be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P
* above doesn't count, as it doesn't implicitly initalize Libgcrypt.
*
* tl;dr: keep all these gry_* statements in this exact order please. */
#if GCRYPT_VERSION_NUMBER < 0x010600
err = gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
if (err) {
ERROR("network plugin: gcry_control (GCRYCTL_SET_THREAD_CBS) failed: %s",
gcry_strerror(err));
return -1;
}
#endif
gcry_check_version(NULL);
err = gcry_control(GCRYCTL_INIT_SECMEM, 32768);
if (err) {
ERROR("network plugin: gcry_control (GCRYCTL_INIT_SECMEM) failed: %s",
gcry_strerror(err));
return -1;
}
gcry_control(GCRYCTL_INITIALIZATION_FINISHED);
return 0;
} /* }}} int network_init_gcrypt */
static gcry_cipher_hd_t network_get_aes256_cypher(sockent_t *se, /* {{{ */
const void *iv,
size_t iv_size,
const char *username) {
gcry_error_t err;
gcry_cipher_hd_t *cyper_ptr;
unsigned char password_hash[32];
if (se->type == SOCKENT_TYPE_CLIENT) {
cyper_ptr = &se->data.client.cypher;
memcpy(password_hash, se->data.client.password_hash, sizeof(password_hash));
} else {
char *secret;
cyper_ptr = &se->data.server.cypher;
if (username == NULL)
return NULL;
secret = fbh_get(se->data.server.userdb, username);
if (secret == NULL)
return NULL;
gcry_md_hash_buffer(GCRY_MD_SHA256, password_hash, secret, strlen(secret));
sfree(secret);
}
if (*cyper_ptr == NULL) {
err = gcry_cipher_open(cyper_ptr, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB,
/* flags = */ 0);
if (err != 0) {
ERROR("network plugin: gcry_cipher_open returned: %s",
gcry_strerror(err));
*cyper_ptr = NULL;
return NULL;
}
} else {
gcry_cipher_reset(*cyper_ptr);
}
assert(*cyper_ptr != NULL);
err = gcry_cipher_setkey(*cyper_ptr, password_hash, sizeof(password_hash));
if (err != 0) {
ERROR("network plugin: gcry_cipher_setkey returned: %s",
gcry_strerror(err));
gcry_cipher_close(*cyper_ptr);
*cyper_ptr = NULL;
return NULL;
}
err = gcry_cipher_setiv(*cyper_ptr, iv, iv_size);
if (err != 0) {
ERROR("network plugin: gcry_cipher_setkey returned: %s",
gcry_strerror(err));
gcry_cipher_close(*cyper_ptr);
*cyper_ptr = NULL;
return NULL;
}
return *cyper_ptr;
} /* }}} int network_get_aes256_cypher */
#endif /* HAVE_GCRYPT_H */
static int write_part_values(char **ret_buffer, size_t *ret_buffer_len,
const data_set_t *ds, const value_list_t *vl) {
char *packet_ptr;
size_t packet_len;
int num_values;
part_header_t pkg_ph;
uint16_t pkg_num_values;
uint8_t *pkg_values_types;
value_t *pkg_values;
size_t offset;
num_values = vl->values_len;
packet_len = sizeof(part_header_t) + sizeof(uint16_t) +
(num_values * sizeof(uint8_t)) + (num_values * sizeof(value_t));
if (*ret_buffer_len < packet_len)
return -1;
pkg_values_types = malloc(num_values * sizeof(*pkg_values_types));
if (pkg_values_types == NULL) {
ERROR("network plugin: write_part_values: malloc failed.");
return -1;
}
pkg_values = malloc(num_values * sizeof(*pkg_values));
if (pkg_values == NULL) {
free(pkg_values_types);
ERROR("network plugin: write_part_values: malloc failed.");
return -1;
}
pkg_ph.type = htons(TYPE_VALUES);
pkg_ph.length = htons(packet_len);
pkg_num_values = htons((uint16_t)vl->values_len);
for (int i = 0; i < num_values; i++) {
pkg_values_types[i] = (uint8_t)ds->ds[i].type;
switch (ds->ds[i].type) {
case DS_TYPE_COUNTER:
pkg_values[i].counter = htonll(vl->values[i].counter);
break;
case DS_TYPE_GAUGE:
pkg_values[i].gauge = htond(vl->values[i].gauge);
break;
case DS_TYPE_DERIVE:
pkg_values[i].derive = htonll(vl->values[i].derive);
break;
case DS_TYPE_ABSOLUTE:
pkg_values[i].absolute = htonll(vl->values[i].absolute);
break;
default:
free(pkg_values_types);
free(pkg_values);
ERROR("network plugin: write_part_values: "
"Unknown data source type: %i",
ds->ds[i].type);
return -1;
} /* switch (ds->ds[i].type) */
} /* for (num_values) */
/*
* Use `memcpy' to write everything to the buffer, because the pointer
* may be unaligned and some architectures, such as SPARC, can't handle
* that.
*/
packet_ptr = *ret_buffer;
offset = 0;
memcpy(packet_ptr + offset, &pkg_ph, sizeof(pkg_ph));
offset += sizeof(pkg_ph);
memcpy(packet_ptr + offset, &pkg_num_values, sizeof(pkg_num_values));
offset += sizeof(pkg_num_values);
memcpy(packet_ptr + offset, pkg_values_types, num_values * sizeof(uint8_t));
offset += num_values * sizeof(uint8_t);
memcpy(packet_ptr + offset, pkg_values, num_values * sizeof(value_t));
offset += num_values * sizeof(value_t);
assert(offset == packet_len);
*ret_buffer = packet_ptr + packet_len;
*ret_buffer_len -= packet_len;
free(pkg_values_types);
free(pkg_values);
return 0;
} /* int write_part_values */
static int write_part_number(char **ret_buffer, size_t *ret_buffer_len,
int type, uint64_t value) {
char *packet_ptr;
size_t packet_len;
part_header_t pkg_head;
uint64_t pkg_value;
size_t offset;
packet_len = sizeof(pkg_head) + sizeof(pkg_value);
if (*ret_buffer_len < packet_len)
return -1;
pkg_head.type = htons(type);
pkg_head.length = htons(packet_len);
pkg_value = htonll(value);
packet_ptr = *ret_buffer;
offset = 0;
memcpy(packet_ptr + offset, &pkg_head, sizeof(pkg_head));
offset += sizeof(pkg_head);
memcpy(packet_ptr + offset, &pkg_value, sizeof(pkg_value));
offset += sizeof(pkg_value);
assert(offset == packet_len);
*ret_buffer = packet_ptr + packet_len;
*ret_buffer_len -= packet_len;
return 0;
} /* int write_part_number */
static int write_part_string(char **ret_buffer, size_t *ret_buffer_len,
int type, const char *str, size_t str_len) {
char *buffer;
size_t buffer_len;
uint16_t pkg_type;
uint16_t pkg_length;
size_t offset;
buffer_len = 2 * sizeof(uint16_t) + str_len + 1;
if (*ret_buffer_len < buffer_len)
return -1;
pkg_type = htons(type);
pkg_length = htons(buffer_len);
buffer = *ret_buffer;
offset = 0;
memcpy(buffer + offset, (void *)&pkg_type, sizeof(pkg_type));
offset += sizeof(pkg_type);
memcpy(buffer + offset, (void *)&pkg_length, sizeof(pkg_length));
offset += sizeof(pkg_length);
memcpy(buffer + offset, str, str_len);
offset += str_len;
memset(buffer + offset, '\0', 1);
offset += 1;
assert(offset == buffer_len);
*ret_buffer = buffer + buffer_len;
*ret_buffer_len -= buffer_len;
return 0;
} /* int write_part_string */
static int parse_part_values(void **ret_buffer, size_t *ret_buffer_len,
value_t **ret_values, size_t *ret_num_values) {
char *buffer = *ret_buffer;
size_t buffer_len = *ret_buffer_len;
uint16_t tmp16;
size_t exp_size;
uint16_t pkg_length;
uint16_t pkg_type;
size_t pkg_numval;
uint8_t *pkg_types;
value_t *pkg_values;
if (buffer_len < 15) {
NOTICE("network plugin: packet is too short: "
"buffer_len = %" PRIsz,
buffer_len);
return -1;
}
memcpy((void *)&tmp16, buffer, sizeof(tmp16));
buffer += sizeof(tmp16);
pkg_type = ntohs(tmp16);
memcpy((void *)&tmp16, buffer, sizeof(tmp16));
buffer += sizeof(tmp16);
pkg_length = ntohs(tmp16);
memcpy((void *)&tmp16, buffer, sizeof(tmp16));
buffer += sizeof(tmp16);
pkg_numval = (size_t)ntohs(tmp16);
assert(pkg_type == TYPE_VALUES);
exp_size =
3 * sizeof(uint16_t) + pkg_numval * (sizeof(uint8_t) + sizeof(value_t));
if (buffer_len < exp_size) {
WARNING("network plugin: parse_part_values: "
"Packet too short: "
"Chunk of size %" PRIsz " expected, "
"but buffer has only %" PRIsz " bytes left.",
exp_size, buffer_len);
return -1;
}
assert(pkg_numval <= ((buffer_len - 6) / 9));
if (pkg_length != exp_size) {
WARNING("network plugin: parse_part_values: "
"Length and number of values "
"in the packet don't match.");
return -1;
}
pkg_types = calloc(pkg_numval, sizeof(*pkg_types));
pkg_values = calloc(pkg_numval, sizeof(*pkg_values));
if ((pkg_types == NULL) || (pkg_values == NULL)) {
sfree(pkg_types);
sfree(pkg_values);
ERROR("network plugin: parse_part_values: calloc failed.");
return -1;
}
memcpy(pkg_types, buffer, pkg_numval * sizeof(*pkg_types));
buffer += pkg_numval * sizeof(*pkg_types);
memcpy(pkg_values, buffer, pkg_numval * sizeof(*pkg_values));
buffer += pkg_numval * sizeof(*pkg_values);
for (size_t i = 0; i < pkg_numval; i++) {
switch (pkg_types[i]) {
case DS_TYPE_COUNTER:
pkg_values[i].counter = (counter_t)ntohll(pkg_values[i].counter);
break;
case DS_TYPE_GAUGE:
pkg_values[i].gauge = (gauge_t)ntohd(pkg_values[i].gauge);
break;
case DS_TYPE_DERIVE:
pkg_values[i].derive = (derive_t)ntohll(pkg_values[i].derive);
break;
case DS_TYPE_ABSOLUTE:
pkg_values[i].absolute = (absolute_t)ntohll(pkg_values[i].absolute);
break;
default:
NOTICE("network plugin: parse_part_values: "
"Don't know how to handle data source type %" PRIu8,
pkg_types[i]);
sfree(pkg_types);
sfree(pkg_values);
return -1;
} /* switch (pkg_types[i]) */
}
*ret_buffer = buffer;
*ret_buffer_len = buffer_len - pkg_length;
*ret_num_values = pkg_numval;
*ret_values = pkg_values;
sfree(pkg_types);
return 0;
} /* int parse_part_values */
static int parse_part_number(void **ret_buffer, size_t *ret_buffer_len,
uint64_t *value) {
char *buffer = *ret_buffer;
size_t buffer_len = *ret_buffer_len;
uint16_t tmp16;
uint64_t tmp64;
size_t exp_size = 2 * sizeof(uint16_t) + sizeof(uint64_t);
uint16_t pkg_length;
if (buffer_len < exp_size) {
WARNING("network plugin: parse_part_number: "
"Packet too short: "
"Chunk of size %" PRIsz " expected, "
"but buffer has only %" PRIsz " bytes left.",
exp_size, buffer_len);
return -1;
}
memcpy((void *)&tmp16, buffer, sizeof(tmp16));
buffer += sizeof(tmp16);
/* pkg_type = ntohs (tmp16); */
memcpy((void *)&tmp16, buffer, sizeof(tmp16));
buffer += sizeof(tmp16);
pkg_length = ntohs(tmp16);
memcpy((void *)&tmp64, buffer, sizeof(tmp64));
buffer += sizeof(tmp64);
*value = ntohll(tmp64);
*ret_buffer = buffer;
*ret_buffer_len = buffer_len - pkg_length;
return 0;
} /* int parse_part_number */
static int parse_part_string(void **ret_buffer, size_t *ret_buffer_len,
char *output, size_t const output_len) {
char *buffer = *ret_buffer;
size_t buffer_len = *ret_buffer_len;
uint16_t tmp16;
size_t const header_size = 2 * sizeof(uint16_t);
uint16_t pkg_length;
size_t payload_size;
if (output_len == 0)
return EINVAL;
if (buffer_len < header_size) {
WARNING("network plugin: parse_part_string: "
"Packet too short: "
"Chunk of at least size %" PRIsz " expected, "
"but buffer has only %" PRIsz " bytes left.",
header_size, buffer_len);
return -1;
}
memcpy((void *)&tmp16, buffer, sizeof(tmp16));
buffer += sizeof(tmp16);
/* pkg_type = ntohs (tmp16); */
memcpy((void *)&tmp16, buffer, sizeof(tmp16));
buffer += sizeof(tmp16);
pkg_length = ntohs(tmp16);
payload_size = ((size_t)pkg_length) - header_size;
/* Check that packet fits in the input buffer */
if (pkg_length > buffer_len) {
WARNING("network plugin: parse_part_string: "
"Packet too big: "
"Chunk of size %" PRIu16 " received, "
"but buffer has only %" PRIsz " bytes left.",
pkg_length, buffer_len);
return -1;
}
/* Check that pkg_length is in the valid range */
if (pkg_length <= header_size) {
WARNING("network plugin: parse_part_string: "
"Packet too short: "
"Header claims this packet is only %hu "
"bytes long.",
pkg_length);
return -1;
}
/* Check that the package data fits into the output buffer.
* The previous if-statement ensures that:
* `pkg_length > header_size' */
if (output_len < payload_size) {
WARNING("network plugin: parse_part_string: "
"Buffer too small: "
"Output buffer holds %" PRIsz " bytes, "
"which is too small to hold the received "
"%" PRIsz " byte string.",
output_len, payload_size);
return -1;
}
/* All sanity checks successfull, let's copy the data over */
memcpy((void *)output, (void *)buffer, payload_size);
buffer += payload_size;
/* For some very weird reason '\0' doesn't do the trick on SPARC in
* this statement. */
if (output[payload_size - 1] != 0) {
WARNING("network plugin: parse_part_string: "
"Received string does not end "
"with a NULL-byte.");
return -1;
}
*ret_buffer = buffer;
*ret_buffer_len = buffer_len - pkg_length;
return 0;
} /* int parse_part_string */