rrdpush.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "rrdpush.h"
  3. #include "parser/parser.h"
  4. /*
  5. * rrdpush
  6. *
  7. * 3 threads are involved for all stream operations
  8. *
  9. * 1. a random data collection thread, calling rrdset_done_push()
  10. * this is called for each chart.
  11. *
  12. * the output of this work is kept in a thread BUFFER
  13. * the sender thread is signalled via a pipe (in RRDHOST)
  14. *
  15. * 2. a sender thread running at the sending netdata
  16. * this is spawned automatically on the first chart to be pushed
  17. *
  18. * It tries to push the metrics to the remote netdata, as fast
  19. * as possible (i.e. immediately after they are collected).
  20. *
  21. * 3. a receiver thread, running at the receiving netdata
  22. * this is spawned automatically when the sender connects to
  23. * the receiver.
  24. *
  25. */
  26. struct config stream_config = {
  27. .first_section = NULL,
  28. .last_section = NULL,
  29. .mutex = NETDATA_MUTEX_INITIALIZER,
  30. .index = {
  31. .avl_tree = {
  32. .root = NULL,
  33. .compar = appconfig_section_compare
  34. },
  35. .rwlock = AVL_LOCK_INITIALIZER
  36. }
  37. };
  38. unsigned int default_rrdpush_enabled = 0;
  39. #ifdef ENABLE_COMPRESSION
  40. unsigned int default_compression_enabled = 1;
  41. #endif
  42. char *default_rrdpush_destination = NULL;
  43. char *default_rrdpush_api_key = NULL;
  44. char *default_rrdpush_send_charts_matching = NULL;
  45. bool default_rrdpush_enable_replication = true;
  46. time_t default_rrdpush_seconds_to_replicate = 86400;
  47. time_t default_rrdpush_replication_step = 600;
  48. #ifdef ENABLE_HTTPS
  49. int netdata_use_ssl_on_stream = NETDATA_SSL_OPTIONAL;
  50. char *netdata_ssl_ca_path = NULL;
  51. char *netdata_ssl_ca_file = NULL;
  52. #endif
  53. static void load_stream_conf() {
  54. errno = 0;
  55. char *filename = strdupz_path_subpath(netdata_configured_user_config_dir, "stream.conf");
  56. if(!appconfig_load(&stream_config, filename, 0, NULL)) {
  57. info("CONFIG: cannot load user config '%s'. Will try stock config.", filename);
  58. freez(filename);
  59. filename = strdupz_path_subpath(netdata_configured_stock_config_dir, "stream.conf");
  60. if(!appconfig_load(&stream_config, filename, 0, NULL))
  61. info("CONFIG: cannot load stock config '%s'. Running with internal defaults.", filename);
  62. }
  63. freez(filename);
  64. }
  65. bool rrdpush_receiver_needs_dbengine() {
  66. struct section *co;
  67. for(co = stream_config.first_section; co; co = co->next) {
  68. if(strcmp(co->name, "stream") == 0)
  69. continue; // the first section is not relevant
  70. char *s;
  71. s = appconfig_get_by_section(co, "enabled", NULL);
  72. if(!s || !appconfig_test_boolean_value(s))
  73. continue;
  74. s = appconfig_get_by_section(co, "default memory mode", NULL);
  75. if(s && strcmp(s, "dbengine") == 0)
  76. return true;
  77. s = appconfig_get_by_section(co, "memory mode", NULL);
  78. if(s && strcmp(s, "dbengine") == 0)
  79. return true;
  80. }
  81. return false;
  82. }
  83. int rrdpush_init() {
  84. // --------------------------------------------------------------------
  85. // load stream.conf
  86. load_stream_conf();
  87. default_rrdpush_enabled = (unsigned int)appconfig_get_boolean(&stream_config, CONFIG_SECTION_STREAM, "enabled", default_rrdpush_enabled);
  88. default_rrdpush_destination = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "destination", "");
  89. default_rrdpush_api_key = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "api key", "");
  90. default_rrdpush_send_charts_matching = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "send charts matching", "*");
  91. default_rrdpush_enable_replication = config_get_boolean(CONFIG_SECTION_DB, "enable replication", default_rrdpush_enable_replication);
  92. default_rrdpush_seconds_to_replicate = config_get_number(CONFIG_SECTION_DB, "seconds to replicate", default_rrdpush_seconds_to_replicate);
  93. default_rrdpush_replication_step = config_get_number(CONFIG_SECTION_DB, "seconds per replication step", default_rrdpush_replication_step);
  94. rrdhost_free_orphan_time = config_get_number(CONFIG_SECTION_DB, "cleanup orphan hosts after secs", rrdhost_free_orphan_time);
  95. #ifdef ENABLE_COMPRESSION
  96. default_compression_enabled = (unsigned int)appconfig_get_boolean(&stream_config, CONFIG_SECTION_STREAM,
  97. "enable compression", default_compression_enabled);
  98. #endif
  99. if(default_rrdpush_enabled && (!default_rrdpush_destination || !*default_rrdpush_destination || !default_rrdpush_api_key || !*default_rrdpush_api_key)) {
  100. error("STREAM [send]: cannot enable sending thread - information is missing.");
  101. default_rrdpush_enabled = 0;
  102. }
  103. #ifdef ENABLE_HTTPS
  104. if (netdata_use_ssl_on_stream == NETDATA_SSL_OPTIONAL) {
  105. if (default_rrdpush_destination){
  106. char *test = strstr(default_rrdpush_destination,":SSL");
  107. if(test){
  108. *test = 0X00;
  109. netdata_use_ssl_on_stream = NETDATA_SSL_FORCE;
  110. }
  111. }
  112. }
  113. bool invalid_certificate = appconfig_get_boolean(&stream_config, CONFIG_SECTION_STREAM, "ssl skip certificate verification", CONFIG_BOOLEAN_NO);
  114. if(invalid_certificate == CONFIG_BOOLEAN_YES){
  115. if(netdata_ssl_validate_server == NETDATA_SSL_VALID_CERTIFICATE){
  116. info("Netdata is configured to accept invalid SSL certificate.");
  117. netdata_ssl_validate_server = NETDATA_SSL_INVALID_CERTIFICATE;
  118. }
  119. }
  120. netdata_ssl_ca_path = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "CApath", NULL);
  121. netdata_ssl_ca_file = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "CAfile", NULL);
  122. #endif
  123. return default_rrdpush_enabled;
  124. }
  125. // data collection happens from multiple threads
  126. // each of these threads calls rrdset_done()
  127. // which in turn calls rrdset_done_push()
  128. // which uses this pipe to notify the streaming thread
  129. // that there are more data ready to be sent
  130. #define PIPE_READ 0
  131. #define PIPE_WRITE 1
  132. // to have the remote netdata re-sync the charts
  133. // to its current clock, we send for this many
  134. // iterations a BEGIN line without microseconds
  135. // this is for the first iterations of each chart
  136. unsigned int remote_clock_resync_iterations = 60;
  137. static inline bool should_send_chart_matching(RRDSET *st, RRDSET_FLAGS flags) {
  138. if(!(flags & RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED))
  139. return false;
  140. if(unlikely(!(flags & (RRDSET_FLAG_UPSTREAM_SEND | RRDSET_FLAG_UPSTREAM_IGNORE)))) {
  141. RRDHOST *host = st->rrdhost;
  142. if (flags & RRDSET_FLAG_ANOMALY_DETECTION) {
  143. if(ml_streaming_enabled())
  144. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_SEND);
  145. else
  146. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  147. }
  148. else if(simple_pattern_matches(host->rrdpush_send_charts_matching, rrdset_id(st)) ||
  149. simple_pattern_matches(host->rrdpush_send_charts_matching, rrdset_name(st)))
  150. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_SEND);
  151. else
  152. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  153. // get the flags again, to know how to respond
  154. flags = rrdset_flag_check(st, RRDSET_FLAG_UPSTREAM_SEND|RRDSET_FLAG_UPSTREAM_IGNORE);
  155. }
  156. return flags & RRDSET_FLAG_UPSTREAM_SEND;
  157. }
  158. int configured_as_parent() {
  159. struct section *section = NULL;
  160. int is_parent = 0;
  161. appconfig_wrlock(&stream_config);
  162. for (section = stream_config.first_section; section; section = section->next) {
  163. uuid_t uuid;
  164. if (uuid_parse(section->name, uuid) != -1 &&
  165. appconfig_get_boolean_by_section(section, "enabled", 0)) {
  166. is_parent = 1;
  167. break;
  168. }
  169. }
  170. appconfig_unlock(&stream_config);
  171. return is_parent;
  172. }
  173. // chart labels
  174. static int send_clabels_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
  175. BUFFER *wb = (BUFFER *)data;
  176. buffer_sprintf(wb, "CLABEL \"%s\" \"%s\" %d\n", name, value, ls);
  177. return 1;
  178. }
  179. static void rrdpush_send_clabels(BUFFER *wb, RRDSET *st) {
  180. if (st->rrdlabels) {
  181. if(rrdlabels_walkthrough_read(st->rrdlabels, send_clabels_callback, wb) > 0)
  182. buffer_sprintf(wb, "CLABEL_COMMIT\n");
  183. }
  184. }
  185. // Send the current chart definition.
  186. // Assumes that collector thread has already called sender_start for mutex / buffer state.
  187. static inline void rrdpush_send_chart_definition(BUFFER *wb, RRDSET *st) {
  188. RRDHOST *host = st->rrdhost;
  189. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  190. // properly set the name for the remote end to parse it
  191. char *name = "";
  192. if(likely(st->name)) {
  193. if(unlikely(st->id != st->name)) {
  194. // they differ
  195. name = strchr(rrdset_name(st), '.');
  196. if(name)
  197. name++;
  198. else
  199. name = "";
  200. }
  201. }
  202. // send the chart
  203. buffer_sprintf(
  204. wb
  205. , "CHART \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" %ld %d \"%s %s %s %s\" \"%s\" \"%s\"\n"
  206. , rrdset_id(st)
  207. , name
  208. , rrdset_title(st)
  209. , rrdset_units(st)
  210. , rrdset_family(st)
  211. , rrdset_context(st)
  212. , rrdset_type_name(st->chart_type)
  213. , st->priority
  214. , st->update_every
  215. , rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)?"obsolete":""
  216. , rrdset_flag_check(st, RRDSET_FLAG_DETAIL)?"detail":""
  217. , rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST)?"store_first":""
  218. , rrdset_flag_check(st, RRDSET_FLAG_HIDDEN)?"hidden":""
  219. , rrdset_plugin_name(st)
  220. , rrdset_module_name(st)
  221. );
  222. // send the chart labels
  223. if (stream_has_capability(host->sender, STREAM_CAP_CLABELS))
  224. rrdpush_send_clabels(wb, st);
  225. // send the dimensions
  226. RRDDIM *rd;
  227. rrddim_foreach_read(rd, st) {
  228. buffer_sprintf(
  229. wb
  230. , "DIMENSION \"%s\" \"%s\" \"%s\" " COLLECTED_NUMBER_FORMAT " " COLLECTED_NUMBER_FORMAT " \"%s %s %s\"\n"
  231. , rrddim_id(rd)
  232. , rrddim_name(rd)
  233. , rrd_algorithm_name(rd->algorithm)
  234. , rd->multiplier
  235. , rd->divisor
  236. , rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)?"obsolete":""
  237. , rrddim_option_check(rd, RRDDIM_OPTION_HIDDEN)?"hidden":""
  238. , rrddim_option_check(rd, RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS)?"noreset":""
  239. );
  240. rd->exposed = 1;
  241. }
  242. rrddim_foreach_done(rd);
  243. // send the chart functions
  244. if(stream_has_capability(host->sender, STREAM_CAP_FUNCTIONS))
  245. rrd_functions_expose_rrdpush(st, wb);
  246. // send the chart local custom variables
  247. rrdsetvar_print_to_streaming_custom_chart_variables(st, wb);
  248. if (stream_has_capability(host->sender, STREAM_CAP_REPLICATION)) {
  249. time_t first_entry_local = rrdset_first_entry_t_of_tier(st, 0);
  250. time_t last_entry_local = st->last_updated.tv_sec;
  251. if(!last_entry_local) {
  252. internal_error(true,
  253. "RRDSET: 'host:%s/chart:%s' db reports last updated time zero.",
  254. rrdhost_hostname(st->rrdhost), rrdset_id(st));
  255. last_entry_local = rrdset_last_entry_t(st);
  256. time_t now = now_realtime_sec();
  257. if(last_entry_local > now) {
  258. internal_error(true,
  259. "RRDSET: 'host:%s/chart:%s' last updated time %llu is in the future (now is %llu)",
  260. rrdhost_hostname(st->rrdhost), rrdset_id(st),
  261. (unsigned long long)last_entry_local, (unsigned long long)now);
  262. last_entry_local = now;
  263. }
  264. }
  265. buffer_sprintf(wb, PLUGINSD_KEYWORD_CHART_DEFINITION_END " %llu %llu\n",
  266. (unsigned long long)first_entry_local, (unsigned long long)last_entry_local);
  267. rrdset_flag_set(st, RRDSET_FLAG_SENDER_REPLICATION_IN_PROGRESS);
  268. rrdset_flag_clear(st, RRDSET_FLAG_SENDER_REPLICATION_FINISHED);
  269. rrdhost_sender_replicating_charts_plus_one(st->rrdhost);
  270. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  271. internal_error(true, "REPLAY: 'host:%s/chart:%s' replication starts",
  272. rrdhost_hostname(st->rrdhost), rrdset_id(st));
  273. #endif
  274. }
  275. st->upstream_resync_time = st->last_collected_time.tv_sec + (remote_clock_resync_iterations * st->update_every);
  276. }
  277. // sends the current chart dimensions
  278. static void rrdpush_send_chart_metrics(BUFFER *wb, RRDSET *st, struct sender_state *s, RRDSET_FLAGS flags) {
  279. buffer_fast_strcat(wb, "BEGIN \"", 7);
  280. buffer_fast_strcat(wb, rrdset_id(st), string_strlen(st->id));
  281. buffer_fast_strcat(wb, "\" ", 2);
  282. if(stream_has_capability(s, STREAM_CAP_REPLICATION) || st->last_collected_time.tv_sec > st->upstream_resync_time)
  283. buffer_print_llu(wb, st->usec_since_last_update);
  284. else
  285. buffer_fast_strcat(wb, "0", 1);
  286. buffer_fast_strcat(wb, "\n", 1);
  287. RRDDIM *rd;
  288. rrddim_foreach_read(rd, st) {
  289. if(unlikely(!rd->updated))
  290. continue;
  291. if(likely(rd->exposed)) {
  292. buffer_fast_strcat(wb, "SET \"", 5);
  293. buffer_fast_strcat(wb, rrddim_id(rd), string_strlen(rd->id));
  294. buffer_fast_strcat(wb, "\" = ", 4);
  295. buffer_print_ll(wb, rd->collected_value);
  296. buffer_fast_strcat(wb, "\n", 1);
  297. }
  298. else {
  299. internal_error(true, "STREAM: 'host:%s/chart:%s/dim:%s' flag 'exposed' is updated but not exposed",
  300. rrdhost_hostname(st->rrdhost), rrdset_id(st), rrddim_id(rd));
  301. // we will include it in the next iteration
  302. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  303. }
  304. }
  305. rrddim_foreach_done(rd);
  306. if(unlikely(flags & RRDSET_FLAG_UPSTREAM_SEND_VARIABLES))
  307. rrdsetvar_print_to_streaming_custom_chart_variables(st, wb);
  308. buffer_fast_strcat(wb, "END\n", 4);
  309. }
  310. static void rrdpush_sender_thread_spawn(RRDHOST *host);
  311. // Called from the internal collectors to mark a chart obsolete.
  312. bool rrdset_push_chart_definition_now(RRDSET *st) {
  313. RRDHOST *host = st->rrdhost;
  314. if(unlikely(!rrdhost_can_send_definitions_to_parent(host)
  315. || !should_send_chart_matching(st, __atomic_load_n(&st->flags, __ATOMIC_SEQ_CST))))
  316. return false;
  317. BUFFER *wb = sender_start(host->sender);
  318. rrdpush_send_chart_definition(wb, st);
  319. sender_commit(host->sender, wb);
  320. return true;
  321. }
  322. void rrdset_done_push(RRDSET *st) {
  323. RRDHOST *host = st->rrdhost;
  324. // fetch the flags we need to check with one atomic operation
  325. RRDHOST_FLAGS host_flags = __atomic_load_n(&host->flags, __ATOMIC_SEQ_CST);
  326. // check if we are not connected
  327. if(unlikely(!(host_flags & RRDHOST_FLAG_RRDPUSH_SENDER_READY_4_METRICS))) {
  328. if(unlikely(!(host_flags & (RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN | RRDHOST_FLAG_RRDPUSH_RECEIVER_DISCONNECTED))))
  329. rrdpush_sender_thread_spawn(host);
  330. if(unlikely(!(host_flags & RRDHOST_FLAG_RRDPUSH_SENDER_LOGGED_STATUS))) {
  331. rrdhost_flag_set(host, RRDHOST_FLAG_RRDPUSH_SENDER_LOGGED_STATUS);
  332. error("STREAM %s [send]: not ready - collected metrics are not sent to parent.", rrdhost_hostname(host));
  333. }
  334. return;
  335. }
  336. else if(unlikely(host_flags & RRDHOST_FLAG_RRDPUSH_SENDER_LOGGED_STATUS)) {
  337. info("STREAM %s [send]: sending metrics to parent...", rrdhost_hostname(host));
  338. rrdhost_flag_clear(host, RRDHOST_FLAG_RRDPUSH_SENDER_LOGGED_STATUS);
  339. }
  340. RRDSET_FLAGS rrdset_flags = __atomic_load_n(&st->flags, __ATOMIC_SEQ_CST);
  341. if(unlikely(!should_send_chart_matching(st, rrdset_flags)))
  342. return;
  343. BUFFER *wb = sender_start(host->sender);
  344. if(unlikely(!(rrdset_flags & RRDSET_FLAG_UPSTREAM_EXPOSED)))
  345. rrdpush_send_chart_definition(wb, st);
  346. if (likely(rrdset_flags & RRDSET_FLAG_SENDER_REPLICATION_FINISHED))
  347. rrdpush_send_chart_metrics(wb, st, host->sender, rrdset_flags);
  348. sender_commit(host->sender, wb);
  349. }
  350. // labels
  351. static int send_labels_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
  352. BUFFER *wb = (BUFFER *)data;
  353. buffer_sprintf(wb, "LABEL \"%s\" = %d \"%s\"\n", name, ls, value);
  354. return 1;
  355. }
  356. void rrdpush_send_host_labels(RRDHOST *host) {
  357. if(unlikely(!rrdhost_can_send_definitions_to_parent(host)
  358. || !stream_has_capability(host->sender, STREAM_CAP_HLABELS)))
  359. return;
  360. BUFFER *wb = sender_start(host->sender);
  361. rrdlabels_walkthrough_read(host->rrdlabels, send_labels_callback, wb);
  362. buffer_sprintf(wb, "OVERWRITE %s\n", "labels");
  363. sender_commit(host->sender, wb);
  364. }
  365. void rrdpush_claimed_id(RRDHOST *host)
  366. {
  367. if(!stream_has_capability(host->sender, STREAM_CAP_CLAIM))
  368. return;
  369. if(unlikely(!rrdhost_can_send_definitions_to_parent(host)))
  370. return;
  371. BUFFER *wb = sender_start(host->sender);
  372. rrdhost_aclk_state_lock(host);
  373. buffer_sprintf(wb, "CLAIMED_ID %s %s\n", host->machine_guid, (host->aclk_state.claimed_id ? host->aclk_state.claimed_id : "NULL") );
  374. rrdhost_aclk_state_unlock(host);
  375. sender_commit(host->sender, wb);
  376. }
  377. int connect_to_one_of_destinations(
  378. RRDHOST *host,
  379. int default_port,
  380. struct timeval *timeout,
  381. size_t *reconnects_counter,
  382. char *connected_to,
  383. size_t connected_to_size,
  384. struct rrdpush_destinations **destination)
  385. {
  386. int sock = -1;
  387. for (struct rrdpush_destinations *d = host->destinations; d; d = d->next) {
  388. time_t now = now_realtime_sec();
  389. if(d->postpone_reconnection_until > now) {
  390. info(
  391. "STREAM %s: skipping destination '%s' (default port: %d) due to last error (code: %d, %s), will retry it in %d seconds",
  392. rrdhost_hostname(host),
  393. string2str(d->destination),
  394. default_port,
  395. d->last_handshake, d->last_error?d->last_error:"unset reason description",
  396. (int)(d->postpone_reconnection_until - now));
  397. continue;
  398. }
  399. info(
  400. "STREAM %s: attempting to connect to '%s' (default port: %d)...",
  401. rrdhost_hostname(host),
  402. string2str(d->destination),
  403. default_port);
  404. if (reconnects_counter)
  405. *reconnects_counter += 1;
  406. sock = connect_to_this(string2str(d->destination), default_port, timeout);
  407. if (sock != -1) {
  408. if (connected_to && connected_to_size)
  409. strncpyz(connected_to, string2str(d->destination), connected_to_size);
  410. *destination = d;
  411. // move the current item to the end of the list
  412. // without this, this destination will break the loop again and again
  413. // not advancing the destinations to find one that may work
  414. DOUBLE_LINKED_LIST_REMOVE_UNSAFE(host->destinations, d, prev, next);
  415. DOUBLE_LINKED_LIST_APPEND_UNSAFE(host->destinations, d, prev, next);
  416. break;
  417. }
  418. }
  419. return sock;
  420. }
  421. struct destinations_init_tmp {
  422. RRDHOST *host;
  423. struct rrdpush_destinations *list;
  424. int count;
  425. };
  426. bool destinations_init_add_one(char *entry, void *data) {
  427. struct destinations_init_tmp *t = data;
  428. struct rrdpush_destinations *d = callocz(1, sizeof(struct rrdpush_destinations));
  429. d->destination = string_strdupz(entry);
  430. DOUBLE_LINKED_LIST_APPEND_UNSAFE(t->list, d, prev, next);
  431. t->count++;
  432. info("STREAM: added streaming destination No %d: '%s' to host '%s'", t->count, string2str(d->destination), rrdhost_hostname(t->host));
  433. return false; // we return false, so that we will get all defined destinations
  434. }
  435. void rrdpush_destinations_init(RRDHOST *host) {
  436. if(!host->rrdpush_send_destination) return;
  437. rrdpush_destinations_free(host);
  438. struct destinations_init_tmp t = {
  439. .host = host,
  440. .list = NULL,
  441. .count = 0,
  442. };
  443. foreach_entry_in_connection_string(host->rrdpush_send_destination, destinations_init_add_one, &t);
  444. host->destinations = t.list;
  445. }
  446. void rrdpush_destinations_free(RRDHOST *host) {
  447. while (host->destinations) {
  448. struct rrdpush_destinations *tmp = host->destinations;
  449. DOUBLE_LINKED_LIST_REMOVE_UNSAFE(host->destinations, tmp, prev, next);
  450. string_freez(tmp->destination);
  451. freez(tmp);
  452. }
  453. host->destinations = NULL;
  454. }
  455. // ----------------------------------------------------------------------------
  456. // rrdpush sender thread
  457. // Either the receiver lost the connection or the host is being destroyed.
  458. // The sender mutex guards thread creation, any spurious data is wiped on reconnection.
  459. void rrdpush_sender_thread_stop(RRDHOST *host) {
  460. if (!host->sender)
  461. return;
  462. netdata_mutex_lock(&host->sender->mutex);
  463. netdata_thread_t thr = 0;
  464. if(rrdhost_flag_check(host, RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN)) {
  465. rrdhost_flag_clear(host, RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN);
  466. info("STREAM %s [send]: signaling sending thread to stop...", rrdhost_hostname(host));
  467. // signal the thread that we want to join it
  468. rrdhost_flag_set(host, RRDHOST_FLAG_RRDPUSH_SENDER_JOIN);
  469. // copy the thread id, so that we will be waiting for the right one
  470. // even if a new one has been spawn
  471. thr = host->rrdpush_sender_thread;
  472. // signal it to cancel
  473. netdata_thread_cancel(host->rrdpush_sender_thread);
  474. }
  475. netdata_mutex_unlock(&host->sender->mutex);
  476. if(thr != 0) {
  477. info("STREAM %s [send]: waiting for the sending thread to stop...", rrdhost_hostname(host));
  478. void *result;
  479. netdata_thread_join(thr, &result);
  480. info("STREAM %s [send]: sending thread has exited.", rrdhost_hostname(host));
  481. }
  482. }
  483. // ----------------------------------------------------------------------------
  484. // rrdpush receiver thread
  485. void log_stream_connection(const char *client_ip, const char *client_port, const char *api_key, const char *machine_guid, const char *host, const char *msg) {
  486. log_access("STREAM: %d '[%s]:%s' '%s' host '%s' api key '%s' machine guid '%s'", gettid(), client_ip, client_port, msg, host, api_key, machine_guid);
  487. }
  488. static void rrdpush_sender_thread_spawn(RRDHOST *host) {
  489. netdata_mutex_lock(&host->sender->mutex);
  490. if(!rrdhost_flag_check(host, RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN)) {
  491. char tag[NETDATA_THREAD_TAG_MAX + 1];
  492. snprintfz(tag, NETDATA_THREAD_TAG_MAX, "STREAM_SENDER[%s]", rrdhost_hostname(host));
  493. if(netdata_thread_create(&host->rrdpush_sender_thread, tag, NETDATA_THREAD_OPTION_JOINABLE, rrdpush_sender_thread, (void *) host->sender))
  494. error("STREAM %s [send]: failed to create new thread for client.", rrdhost_hostname(host));
  495. else
  496. rrdhost_flag_set(host, RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN);
  497. }
  498. netdata_mutex_unlock(&host->sender->mutex);
  499. }
  500. int rrdpush_receiver_permission_denied(struct web_client *w) {
  501. // we always respond with the same message and error code
  502. // to prevent an attacker from gaining info about the error
  503. buffer_flush(w->response.data);
  504. buffer_sprintf(w->response.data, "You are not permitted to access this. Check the logs for more info.");
  505. return 401;
  506. }
  507. int rrdpush_receiver_too_busy_now(struct web_client *w) {
  508. // we always respond with the same message and error code
  509. // to prevent an attacker from gaining info about the error
  510. buffer_flush(w->response.data);
  511. buffer_sprintf(w->response.data, "The server is too busy now to accept this request. Try later.");
  512. return 503;
  513. }
  514. void *rrdpush_receiver_thread(void *ptr);
  515. int rrdpush_receiver_thread_spawn(struct web_client *w, char *url) {
  516. info("clients wants to STREAM metrics.");
  517. char *key = NULL, *hostname = NULL, *registry_hostname = NULL, *machine_guid = NULL, *os = "unknown", *timezone = "unknown", *abbrev_timezone = "UTC", *tags = NULL;
  518. int32_t utc_offset = 0;
  519. int update_every = default_rrd_update_every;
  520. uint32_t stream_version = UINT_MAX;
  521. char buf[GUID_LEN + 1];
  522. struct rrdhost_system_info *system_info = callocz(1, sizeof(struct rrdhost_system_info));
  523. system_info->hops = 1;
  524. while(url) {
  525. char *value = mystrsep(&url, "&");
  526. if(!value || !*value) continue;
  527. char *name = mystrsep(&value, "=");
  528. if(!name || !*name) continue;
  529. if(!value || !*value) continue;
  530. if(!strcmp(name, "key"))
  531. key = value;
  532. else if(!strcmp(name, "hostname"))
  533. hostname = value;
  534. else if(!strcmp(name, "registry_hostname"))
  535. registry_hostname = value;
  536. else if(!strcmp(name, "machine_guid"))
  537. machine_guid = value;
  538. else if(!strcmp(name, "update_every"))
  539. update_every = (int)strtoul(value, NULL, 0);
  540. else if(!strcmp(name, "os"))
  541. os = value;
  542. else if(!strcmp(name, "timezone"))
  543. timezone = value;
  544. else if(!strcmp(name, "abbrev_timezone"))
  545. abbrev_timezone = value;
  546. else if(!strcmp(name, "utc_offset"))
  547. utc_offset = (int32_t)strtol(value, NULL, 0);
  548. else if(!strcmp(name, "hops"))
  549. system_info->hops = (uint16_t) strtoul(value, NULL, 0);
  550. else if(!strcmp(name, "ml_capable"))
  551. system_info->ml_capable = strtoul(value, NULL, 0);
  552. else if(!strcmp(name, "ml_enabled"))
  553. system_info->ml_enabled = strtoul(value, NULL, 0);
  554. else if(!strcmp(name, "mc_version"))
  555. system_info->mc_version = strtoul(value, NULL, 0);
  556. else if(!strcmp(name, "tags"))
  557. tags = value;
  558. else if(!strcmp(name, "ver"))
  559. stream_version = convert_stream_version_to_capabilities(strtoul(value, NULL, 0));
  560. else {
  561. // An old Netdata child does not have a compatible streaming protocol, map to something sane.
  562. if (!strcmp(name, "NETDATA_SYSTEM_OS_NAME"))
  563. name = "NETDATA_HOST_OS_NAME";
  564. else if (!strcmp(name, "NETDATA_SYSTEM_OS_ID"))
  565. name = "NETDATA_HOST_OS_ID";
  566. else if (!strcmp(name, "NETDATA_SYSTEM_OS_ID_LIKE"))
  567. name = "NETDATA_HOST_OS_ID_LIKE";
  568. else if (!strcmp(name, "NETDATA_SYSTEM_OS_VERSION"))
  569. name = "NETDATA_HOST_OS_VERSION";
  570. else if (!strcmp(name, "NETDATA_SYSTEM_OS_VERSION_ID"))
  571. name = "NETDATA_HOST_OS_VERSION_ID";
  572. else if (!strcmp(name, "NETDATA_SYSTEM_OS_DETECTION"))
  573. name = "NETDATA_HOST_OS_DETECTION";
  574. else if(!strcmp(name, "NETDATA_PROTOCOL_VERSION") && stream_version == UINT_MAX) {
  575. stream_version = convert_stream_version_to_capabilities(1);
  576. }
  577. if (unlikely(rrdhost_set_system_info_variable(system_info, name, value))) {
  578. info("STREAM [receive from [%s]:%s]: request has parameter '%s' = '%s', which is not used.",
  579. w->client_ip, w->client_port, name, value);
  580. }
  581. }
  582. }
  583. if (stream_version == UINT_MAX)
  584. stream_version = convert_stream_version_to_capabilities(0);
  585. if(!key || !*key) {
  586. rrdhost_system_info_free(system_info);
  587. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO KEY");
  588. error("STREAM [receive from [%s]:%s]: request without an API key. Forbidding access.", w->client_ip, w->client_port);
  589. return rrdpush_receiver_permission_denied(w);
  590. }
  591. if(!hostname || !*hostname) {
  592. rrdhost_system_info_free(system_info);
  593. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO HOSTNAME");
  594. error("STREAM [receive from [%s]:%s]: request without a hostname. Forbidding access.", w->client_ip, w->client_port);
  595. return rrdpush_receiver_permission_denied(w);
  596. }
  597. if(!machine_guid || !*machine_guid) {
  598. rrdhost_system_info_free(system_info);
  599. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO MACHINE GUID");
  600. error("STREAM [receive from [%s]:%s]: request without a machine GUID. Forbidding access.", w->client_ip, w->client_port);
  601. return rrdpush_receiver_permission_denied(w);
  602. }
  603. if(regenerate_guid(key, buf) == -1) {
  604. rrdhost_system_info_free(system_info);
  605. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - INVALID KEY");
  606. error("STREAM [receive from [%s]:%s]: API key '%s' is not valid GUID (use the command uuidgen to generate one). Forbidding access.", w->client_ip, w->client_port, key);
  607. return rrdpush_receiver_permission_denied(w);
  608. }
  609. if(regenerate_guid(machine_guid, buf) == -1) {
  610. rrdhost_system_info_free(system_info);
  611. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - INVALID MACHINE GUID");
  612. error("STREAM [receive from [%s]:%s]: machine GUID '%s' is not GUID. Forbidding access.", w->client_ip, w->client_port, machine_guid);
  613. return rrdpush_receiver_permission_denied(w);
  614. }
  615. if(!appconfig_get_boolean(&stream_config, key, "enabled", 0)) {
  616. rrdhost_system_info_free(system_info);
  617. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - KEY NOT ENABLED");
  618. error("STREAM [receive from [%s]:%s]: API key '%s' is not allowed. Forbidding access.", w->client_ip, w->client_port, key);
  619. return rrdpush_receiver_permission_denied(w);
  620. }
  621. {
  622. SIMPLE_PATTERN *key_allow_from = simple_pattern_create(appconfig_get(&stream_config, key, "allow from", "*"), NULL, SIMPLE_PATTERN_EXACT);
  623. if(key_allow_from) {
  624. if(!simple_pattern_matches(key_allow_from, w->client_ip)) {
  625. simple_pattern_free(key_allow_from);
  626. rrdhost_system_info_free(system_info);
  627. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname) ? hostname : "-", "ACCESS DENIED - KEY NOT ALLOWED FROM THIS IP");
  628. error("STREAM [receive from [%s]:%s]: API key '%s' is not permitted from this IP. Forbidding access.", w->client_ip, w->client_port, key);
  629. return rrdpush_receiver_permission_denied(w);
  630. }
  631. simple_pattern_free(key_allow_from);
  632. }
  633. }
  634. if(!appconfig_get_boolean(&stream_config, machine_guid, "enabled", 1)) {
  635. rrdhost_system_info_free(system_info);
  636. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - MACHINE GUID NOT ENABLED");
  637. error("STREAM [receive from [%s]:%s]: machine GUID '%s' is not allowed. Forbidding access.", w->client_ip, w->client_port, machine_guid);
  638. return rrdpush_receiver_permission_denied(w);
  639. }
  640. {
  641. SIMPLE_PATTERN *machine_allow_from = simple_pattern_create(appconfig_get(&stream_config, machine_guid, "allow from", "*"), NULL, SIMPLE_PATTERN_EXACT);
  642. if(machine_allow_from) {
  643. if(!simple_pattern_matches(machine_allow_from, w->client_ip)) {
  644. simple_pattern_free(machine_allow_from);
  645. rrdhost_system_info_free(system_info);
  646. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname) ? hostname : "-", "ACCESS DENIED - MACHINE GUID NOT ALLOWED FROM THIS IP");
  647. error("STREAM [receive from [%s]:%s]: Machine GUID '%s' is not permitted from this IP. Forbidding access.", w->client_ip, w->client_port, machine_guid);
  648. return rrdpush_receiver_permission_denied(w);
  649. }
  650. simple_pattern_free(machine_allow_from);
  651. }
  652. }
  653. if(unlikely(web_client_streaming_rate_t > 0)) {
  654. static netdata_mutex_t stream_rate_mutex = NETDATA_MUTEX_INITIALIZER;
  655. static volatile time_t last_stream_accepted_t = 0;
  656. netdata_mutex_lock(&stream_rate_mutex);
  657. time_t now = now_realtime_sec();
  658. if(unlikely(last_stream_accepted_t == 0))
  659. last_stream_accepted_t = now;
  660. if(now - last_stream_accepted_t < web_client_streaming_rate_t) {
  661. netdata_mutex_unlock(&stream_rate_mutex);
  662. rrdhost_system_info_free(system_info);
  663. error("STREAM [receive from [%s]:%s]: too busy to accept new streaming request. Will be allowed in %ld secs.", w->client_ip, w->client_port, (long)(web_client_streaming_rate_t - (now - last_stream_accepted_t)));
  664. return rrdpush_receiver_too_busy_now(w);
  665. }
  666. last_stream_accepted_t = now;
  667. netdata_mutex_unlock(&stream_rate_mutex);
  668. }
  669. /*
  670. * Quick path for rejecting multiple connections. The lock taken is fine-grained - it only protects the receiver
  671. * pointer within the host (if a host exists). This protects against multiple concurrent web requests hitting
  672. * separate threads within the web-server and landing here. The lock guards the thread-shutdown sequence that
  673. * detaches the receiver from the host. If the host is being created (first time-access) then we also use the
  674. * lock to prevent race-hazard (two threads try to create the host concurrently, one wins and the other does a
  675. * lookup to the now-attached structure).
  676. */
  677. struct receiver_state *rpt = callocz(1, sizeof(*rpt));
  678. rrd_rdlock();
  679. RRDHOST *host = rrdhost_find_by_guid(machine_guid);
  680. if (unlikely(host && rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))) /* Ignore archived hosts. */
  681. host = NULL;
  682. if (host) {
  683. rrdhost_wrlock(host);
  684. netdata_mutex_lock(&host->receiver_lock);
  685. rrdhost_flag_clear(host, RRDHOST_FLAG_ORPHAN);
  686. host->senders_disconnected_time = 0;
  687. if (host->receiver != NULL) {
  688. time_t age = now_realtime_sec() - host->receiver->last_msg_t;
  689. if (age > 30) {
  690. host->receiver->shutdown = 1;
  691. shutdown(host->receiver->fd, SHUT_RDWR);
  692. host->receiver = NULL; // Thread holds reference to structure
  693. info(
  694. "STREAM %s [receive from [%s]:%s]: multiple connections for same host detected - "
  695. "existing connection is dead (%"PRId64" sec), accepting new connection.",
  696. rrdhost_hostname(host),
  697. w->client_ip,
  698. w->client_port,
  699. (int64_t)age);
  700. }
  701. else {
  702. netdata_mutex_unlock(&host->receiver_lock);
  703. rrdhost_unlock(host);
  704. rrd_unlock();
  705. log_stream_connection(w->client_ip, w->client_port, key, host->machine_guid, rrdhost_hostname(host),
  706. "REJECTED - ALREADY CONNECTED");
  707. info(
  708. "STREAM %s [receive from [%s]:%s]: multiple connections for same host detected - "
  709. "existing connection is active (within last %"PRId64" sec), rejecting new connection.",
  710. rrdhost_hostname(host),
  711. w->client_ip,
  712. w->client_port,
  713. (int64_t)age);
  714. // Have not set WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET - caller should clean up
  715. buffer_flush(w->response.data);
  716. buffer_strcat(w->response.data, "This GUID is already streaming to this server");
  717. freez(rpt);
  718. return 409;
  719. }
  720. }
  721. host->receiver = rpt;
  722. netdata_mutex_unlock(&host->receiver_lock);
  723. rrdhost_unlock(host);
  724. }
  725. rrd_unlock();
  726. rpt->last_msg_t = now_realtime_sec();
  727. rpt->host = host;
  728. rpt->fd = w->ifd;
  729. rpt->key = strdupz(key);
  730. rpt->hostname = strdupz(hostname);
  731. rpt->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
  732. rpt->machine_guid = strdupz(machine_guid);
  733. rpt->os = strdupz(os);
  734. rpt->timezone = strdupz(timezone);
  735. rpt->abbrev_timezone = strdupz(abbrev_timezone);
  736. rpt->utc_offset = utc_offset;
  737. rpt->tags = (tags)?strdupz(tags):NULL;
  738. rpt->client_ip = strdupz(w->client_ip);
  739. rpt->client_port = strdupz(w->client_port);
  740. rpt->update_every = update_every;
  741. rpt->system_info = system_info;
  742. rpt->capabilities = stream_version;
  743. #ifdef ENABLE_HTTPS
  744. rpt->ssl.conn = w->ssl.conn;
  745. rpt->ssl.flags = w->ssl.flags;
  746. w->ssl.conn = NULL;
  747. w->ssl.flags = NETDATA_SSL_START;
  748. #endif
  749. if(w->user_agent && w->user_agent[0]) {
  750. char *t = strchr(w->user_agent, '/');
  751. if(t && *t) {
  752. *t = '\0';
  753. t++;
  754. }
  755. rpt->program_name = strdupz(w->user_agent);
  756. if(t && *t) rpt->program_version = strdupz(t);
  757. }
  758. debug(D_SYSTEM, "starting STREAM receive thread.");
  759. char tag[FILENAME_MAX + 1];
  760. snprintfz(tag, FILENAME_MAX, "STREAM_RECEIVER[%s,[%s]:%s]", rpt->hostname, w->client_ip, w->client_port);
  761. if(netdata_thread_create(&rpt->thread, tag, NETDATA_THREAD_OPTION_DEFAULT, rrdpush_receiver_thread, (void *)rpt))
  762. error("Failed to create new STREAM receive thread for client.");
  763. // prevent the caller from closing the streaming socket
  764. if(web_server_mode == WEB_SERVER_MODE_STATIC_THREADED) {
  765. web_client_flag_set(w, WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET);
  766. }
  767. else {
  768. if(w->ifd == w->ofd)
  769. w->ifd = w->ofd = -1;
  770. else
  771. w->ifd = -1;
  772. }
  773. buffer_flush(w->response.data);
  774. return 200;
  775. }
  776. static void stream_capabilities_to_string(BUFFER *wb, STREAM_CAPABILITIES caps) {
  777. if(caps & STREAM_CAP_V1) buffer_strcat(wb, "V1 ");
  778. if(caps & STREAM_CAP_V2) buffer_strcat(wb, "V2 ");
  779. if(caps & STREAM_CAP_VN) buffer_strcat(wb, "VN ");
  780. if(caps & STREAM_CAP_VCAPS) buffer_strcat(wb, "VCAPS ");
  781. if(caps & STREAM_CAP_HLABELS) buffer_strcat(wb, "HLABELS ");
  782. if(caps & STREAM_CAP_CLAIM) buffer_strcat(wb, "CLAIM ");
  783. if(caps & STREAM_CAP_CLABELS) buffer_strcat(wb, "CLABELS ");
  784. if(caps & STREAM_CAP_COMPRESSION) buffer_strcat(wb, "COMPRESSION ");
  785. if(caps & STREAM_CAP_FUNCTIONS) buffer_strcat(wb, "FUNCTIONS ");
  786. if(caps & STREAM_CAP_REPLICATION) buffer_strcat(wb, "REPLICATION ");
  787. if(caps & STREAM_CAP_BINARY) buffer_strcat(wb, "BINARY ");
  788. }
  789. void log_receiver_capabilities(struct receiver_state *rpt) {
  790. BUFFER *wb = buffer_create(100);
  791. stream_capabilities_to_string(wb, rpt->capabilities);
  792. info("STREAM %s [receive from [%s]:%s]: established link with negotiated capabilities: %s",
  793. rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port, buffer_tostring(wb));
  794. buffer_free(wb);
  795. }
  796. void log_sender_capabilities(struct sender_state *s) {
  797. BUFFER *wb = buffer_create(100);
  798. stream_capabilities_to_string(wb, s->capabilities);
  799. info("STREAM %s [send to %s]: established link with negotiated capabilities: %s",
  800. rrdhost_hostname(s->host), s->connected_to, buffer_tostring(wb));
  801. buffer_free(wb);
  802. }
  803. STREAM_CAPABILITIES convert_stream_version_to_capabilities(int32_t version) {
  804. STREAM_CAPABILITIES caps = 0;
  805. if(version <= 1) caps = STREAM_CAP_V1;
  806. else if(version < STREAM_OLD_VERSION_CLAIM) caps = STREAM_CAP_V2 | STREAM_CAP_HLABELS;
  807. else if(version <= STREAM_OLD_VERSION_CLAIM) caps = STREAM_CAP_VN | STREAM_CAP_HLABELS | STREAM_CAP_CLAIM;
  808. else if(version <= STREAM_OLD_VERSION_CLABELS) caps = STREAM_CAP_VN | STREAM_CAP_HLABELS | STREAM_CAP_CLAIM | STREAM_CAP_CLABELS;
  809. else if(version <= STREAM_OLD_VERSION_COMPRESSION) caps = STREAM_CAP_VN | STREAM_CAP_HLABELS | STREAM_CAP_CLAIM | STREAM_CAP_CLABELS | STREAM_HAS_COMPRESSION;
  810. else caps = version;
  811. if(caps & STREAM_CAP_VCAPS)
  812. caps &= ~(STREAM_CAP_V1|STREAM_CAP_V2|STREAM_CAP_VN);
  813. if(caps & STREAM_CAP_VN)
  814. caps &= ~(STREAM_CAP_V1|STREAM_CAP_V2);
  815. if(caps & STREAM_CAP_V2)
  816. caps &= ~(STREAM_CAP_V1);
  817. return caps & STREAM_OUR_CAPABILITIES;
  818. }
  819. int32_t stream_capabilities_to_vn(uint32_t caps) {
  820. if(caps & STREAM_CAP_COMPRESSION) return STREAM_OLD_VERSION_COMPRESSION;
  821. if(caps & STREAM_CAP_CLABELS) return STREAM_OLD_VERSION_CLABELS;
  822. return STREAM_OLD_VERSION_CLAIM; // if(caps & STREAM_CAP_CLAIM)
  823. }