4
0

qnetd-client-msg-received.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /*
  2. * Copyright (c) 2015-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/types.h>
  35. #include "log.h"
  36. #include "log-common.h"
  37. #include "qnetd-algorithm.h"
  38. #include "qnetd-instance.h"
  39. #include "qnetd-log-debug.h"
  40. #include "qnetd-client-send.h"
  41. #include "qnetd-client-dpd-timer.h"
  42. #include "msg.h"
  43. #include "nss-sock.h"
  44. #include "qnetd-client-msg-received.h"
  45. /*
  46. * 0 - Success
  47. * -1 - Disconnect client
  48. * -2 - Error reply sent, but no need to disconnect client
  49. */
  50. static int
  51. qnetd_client_msg_received_check_tls(struct qnetd_instance *instance, struct qnetd_client *client,
  52. const struct msg_decoded *msg)
  53. {
  54. int check_certificate;
  55. int tls_required;
  56. CERTCertificate *peer_cert;
  57. int case_processed;
  58. check_certificate = 0;
  59. tls_required = 0;
  60. case_processed = 0;
  61. switch (instance->tls_supported) {
  62. case TLV_TLS_UNSUPPORTED:
  63. case_processed = 1;
  64. tls_required = 0;
  65. check_certificate = 0;
  66. break;
  67. case TLV_TLS_SUPPORTED:
  68. case_processed = 1;
  69. tls_required = 0;
  70. if (client->tls_started && instance->tls_client_cert_required &&
  71. !client->tls_peer_certificate_verified) {
  72. check_certificate = 1;
  73. }
  74. break;
  75. case TLV_TLS_REQUIRED:
  76. case_processed = 1;
  77. tls_required = 1;
  78. if (instance->tls_client_cert_required && !client->tls_peer_certificate_verified) {
  79. check_certificate = 1;
  80. }
  81. break;
  82. /*
  83. * Default is not defined intentionally. Compiler shows warning when new
  84. * tls supported is added
  85. */
  86. }
  87. if (!case_processed) {
  88. log(LOG_ERR, "Unhandled instance tls supported %u", instance->tls_supported);
  89. exit(EXIT_FAILURE);
  90. }
  91. if (tls_required && !client->tls_started) {
  92. log(LOG_ERR, "TLS is required but doesn't started yet. "
  93. "Sending back error message");
  94. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  95. TLV_REPLY_ERROR_CODE_TLS_REQUIRED) != 0) {
  96. return (-1);
  97. }
  98. return (-2);
  99. }
  100. if (check_certificate) {
  101. peer_cert = SSL_PeerCertificate(client->socket);
  102. if (peer_cert == NULL) {
  103. log(LOG_ERR, "Client doesn't sent valid certificate. "
  104. "Disconnecting client");
  105. return (-1);
  106. }
  107. if (CERT_VerifyCertName(peer_cert, client->cluster_name) != SECSuccess) {
  108. log(LOG_ERR, "Client doesn't sent certificate with valid CN. "
  109. "Disconnecting client");
  110. CERT_DestroyCertificate(peer_cert);
  111. return (-1);
  112. }
  113. CERT_DestroyCertificate(peer_cert);
  114. client->tls_peer_certificate_verified = 1;
  115. }
  116. return (0);
  117. }
  118. static int
  119. qnetd_client_msg_received_preinit(struct qnetd_instance *instance, struct qnetd_client *client,
  120. const struct msg_decoded *msg)
  121. {
  122. struct send_buffer_list_entry *send_buffer;
  123. if (msg->cluster_name == NULL) {
  124. log(LOG_ERR, "Received preinit message without cluster name. "
  125. "Sending error reply.");
  126. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  127. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  128. return (-1);
  129. }
  130. return (0);
  131. }
  132. client->cluster_name = malloc(msg->cluster_name_len + 1);
  133. if (client->cluster_name == NULL) {
  134. log(LOG_ERR, "Can't allocate cluster name. Sending error reply.");
  135. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  136. TLV_REPLY_ERROR_CODE_INTERNAL_ERROR) != 0) {
  137. return (-1);
  138. }
  139. return (0);
  140. }
  141. memset(client->cluster_name, 0, msg->cluster_name_len + 1);
  142. memcpy(client->cluster_name, msg->cluster_name, msg->cluster_name_len);
  143. client->cluster_name_len = msg->cluster_name_len;
  144. client->preinit_received = 1;
  145. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  146. if (send_buffer == NULL) {
  147. log(LOG_ERR, "Can't alloc preinit reply msg from list. "
  148. "Disconnecting client connection.");
  149. return (-1);
  150. }
  151. if (msg_create_preinit_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number,
  152. instance->tls_supported, instance->tls_client_cert_required) == 0) {
  153. log(LOG_ERR, "Can't alloc preinit reply msg. "
  154. "Disconnecting client connection.");
  155. send_buffer_list_discard_new(&client->send_buffer_list, send_buffer);
  156. return (-1);
  157. };
  158. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  159. return (0);
  160. }
  161. static int
  162. qnetd_client_msg_received_unexpected_msg(struct qnetd_client *client,
  163. const struct msg_decoded *msg, const char *msg_str)
  164. {
  165. log(LOG_ERR, "Received %s message. Sending back error message", msg_str);
  166. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  167. TLV_REPLY_ERROR_CODE_UNEXPECTED_MESSAGE) != 0) {
  168. return (-1);
  169. }
  170. return (0);
  171. }
  172. static int
  173. qnetd_client_msg_received_preinit_reply(struct qnetd_instance *instance,
  174. struct qnetd_client *client, const struct msg_decoded *msg)
  175. {
  176. return (qnetd_client_msg_received_unexpected_msg(client, msg, "preinit reply"));
  177. }
  178. static int
  179. qnetd_client_msg_received_starttls(struct qnetd_instance *instance, struct qnetd_client *client,
  180. const struct msg_decoded *msg)
  181. {
  182. PRFileDesc *new_pr_fd;
  183. if (!client->preinit_received) {
  184. log(LOG_ERR, "Received starttls before preinit message. "
  185. "Sending error reply.");
  186. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  187. TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED) != 0) {
  188. return (-1);
  189. }
  190. return (0);
  191. }
  192. if ((new_pr_fd = nss_sock_start_ssl_as_server(client->socket, instance->server.cert,
  193. instance->server.private_key, instance->tls_client_cert_required, 0, NULL)) == NULL) {
  194. log_nss(LOG_ERR, "Can't start TLS. Disconnecting client.");
  195. return (-1);
  196. }
  197. client->tls_started = 1;
  198. client->tls_peer_certificate_verified = 0;
  199. client->socket = new_pr_fd;
  200. return (0);
  201. }
  202. static int
  203. qnetd_client_msg_received_server_error(struct qnetd_instance *instance, struct qnetd_client *client,
  204. const struct msg_decoded *msg)
  205. {
  206. return (qnetd_client_msg_received_unexpected_msg(client, msg, "server error"));
  207. }
  208. /*
  209. * Checks if new client send information are valid. It means:
  210. * - in cluster is no duplicate node with same nodeid
  211. * - it has same tie_breaker as other nodes in cluster
  212. * - it has same algorithm as other nodes in cluster
  213. */
  214. static enum tlv_reply_error_code
  215. qnetd_client_msg_received_init_check_new_client(struct qnetd_instance *instance,
  216. struct qnetd_client *new_client)
  217. {
  218. struct qnetd_cluster *cluster;
  219. struct qnetd_client *client;
  220. cluster = qnetd_cluster_list_find_by_name(&instance->clusters, new_client->cluster_name,
  221. new_client->cluster_name_len);
  222. if (cluster == NULL) {
  223. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  224. }
  225. TAILQ_FOREACH(client, &cluster->client_list, cluster_entries) {
  226. if (!tlv_tie_breaker_eq(&new_client->tie_breaker, &client->tie_breaker)) {
  227. log(LOG_ERR, "Received init message contains tie-breaker which "
  228. "differs from rest of cluster. Sending error reply");
  229. return (TLV_REPLY_ERROR_CODE_TIE_BREAKER_DIFFERS_FROM_OTHER_NODES);
  230. }
  231. if (new_client->decision_algorithm != client->decision_algorithm) {
  232. log(LOG_ERR, "Received init message contains algorithm which "
  233. "differs from rest of cluster. Sending error reply");
  234. return (TLV_REPLY_ERROR_CODE_ALGORITHM_DIFFERS_FROM_OTHER_NODES);
  235. }
  236. if (new_client->node_id == client->node_id) {
  237. log(LOG_ERR, "Received init message contains node id which is "
  238. "duplicate of other node in cluster. Sending error reply");
  239. return (TLV_REPLY_ERROR_CODE_DUPLICATE_NODE_ID);
  240. }
  241. }
  242. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  243. }
  244. static int
  245. qnetd_client_msg_received_init(struct qnetd_instance *instance, struct qnetd_client *client,
  246. const struct msg_decoded *msg)
  247. {
  248. int res;
  249. size_t zi;
  250. enum msg_type *supported_msgs;
  251. size_t no_supported_msgs;
  252. enum tlv_opt_type *supported_opts;
  253. size_t no_supported_opts;
  254. struct send_buffer_list_entry *send_buffer;
  255. enum tlv_reply_error_code reply_error_code;
  256. struct qnetd_cluster *cluster;
  257. supported_msgs = NULL;
  258. supported_opts = NULL;
  259. no_supported_msgs = 0;
  260. no_supported_opts = 0;
  261. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  262. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  263. return (res == -1 ? -1 : 0);
  264. }
  265. if (!client->preinit_received) {
  266. log(LOG_ERR, "Received init before preinit message. Sending error reply.");
  267. reply_error_code = TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED;
  268. }
  269. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->node_id_set) {
  270. log(LOG_ERR, "Received init message without node id set. "
  271. "Sending error reply.");
  272. reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION;
  273. } else {
  274. client->node_id_set = 1;
  275. client->node_id = msg->node_id;
  276. }
  277. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->ring_id_set) {
  278. log(LOG_ERR, "Received init message without ring id set. "
  279. "Sending error reply.");
  280. reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION;
  281. } else {
  282. memcpy(&client->last_ring_id, &msg->ring_id, sizeof(struct tlv_ring_id));
  283. }
  284. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->heartbeat_interval_set) {
  285. log(LOG_ERR, "Received init message without heartbeat interval set. "
  286. "Sending error reply.");
  287. reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION;
  288. } else {
  289. if (msg->heartbeat_interval < instance->advanced_settings->heartbeat_interval_min ||
  290. msg->heartbeat_interval > instance->advanced_settings->heartbeat_interval_max) {
  291. log(LOG_ERR, "Client requested invalid heartbeat interval %u. "
  292. "Sending error reply.", msg->heartbeat_interval);
  293. reply_error_code = TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL;
  294. } else {
  295. client->heartbeat_interval = msg->heartbeat_interval;
  296. if (qnetd_client_dpd_timer_update_interval(instance, client) != 0) {
  297. reply_error_code = TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL;
  298. }
  299. }
  300. }
  301. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->tie_breaker_set) {
  302. log(LOG_ERR, "Received init message without tie-breaker set. "
  303. "Sending error reply.");
  304. reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION;
  305. } else {
  306. memcpy(&client->tie_breaker, &msg->tie_breaker, sizeof(msg->tie_breaker));
  307. }
  308. if (msg->supported_messages != NULL) {
  309. /*
  310. * Client sent supported messages. For now this is ignored but in the future
  311. * this may be used to ensure backward compatibility.
  312. */
  313. /*
  314. for (i = 0; i < msg->no_supported_messages; i++) {
  315. log(LOG_DEBUG, "Client supports %u message",
  316. (int)msg->supported_messages[i]);
  317. }
  318. */
  319. /*
  320. * Sent back supported messages
  321. */
  322. msg_get_supported_messages(&supported_msgs, &no_supported_msgs);
  323. }
  324. if (msg->supported_options != NULL) {
  325. /*
  326. * Client sent supported options. For now this is ignored but in the future
  327. * this may be used to ensure backward compatibility.
  328. */
  329. /*
  330. for (i = 0; i < msg->no_supported_options; i++) {
  331. log(LOG_DEBUG, "Client supports %u option",
  332. (int)msg->supported_messages[i]);
  333. }
  334. */
  335. /*
  336. * Send back supported options
  337. */
  338. tlv_get_supported_options(&supported_opts, &no_supported_opts);
  339. }
  340. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->decision_algorithm_set) {
  341. log(LOG_ERR, "Received init message without decision algorithm. "
  342. "Sending error reply.");
  343. reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION;
  344. } else {
  345. /*
  346. * Check if decision algorithm requested by client is supported
  347. */
  348. res = 0;
  349. for (zi = 0; zi < QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE && !res; zi++) {
  350. if (qnetd_static_supported_decision_algorithms[zi] ==
  351. msg->decision_algorithm) {
  352. res = 1;
  353. }
  354. }
  355. if (!res) {
  356. log(LOG_ERR, "Client requested unsupported decision algorithm %u. "
  357. "Sending error reply.", msg->decision_algorithm);
  358. reply_error_code = TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM;
  359. }
  360. client->decision_algorithm = msg->decision_algorithm;
  361. }
  362. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  363. /*
  364. * Preset keep_active_partition_tie_breaker from default config
  365. */
  366. client->keep_active_partition_tie_breaker =
  367. instance->advanced_settings->keep_active_partition_tie_breaker;
  368. }
  369. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  370. reply_error_code = qnetd_client_msg_received_init_check_new_client(instance,
  371. client);
  372. }
  373. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  374. cluster = qnetd_cluster_list_add_client(&instance->clusters, client);
  375. if (cluster == NULL) {
  376. log(LOG_ERR, "Can't add client to cluster list. "
  377. "Sending error reply.");
  378. reply_error_code = TLV_REPLY_ERROR_CODE_INTERNAL_ERROR;
  379. } else {
  380. client->cluster = cluster;
  381. client->cluster_list = &instance->clusters;
  382. }
  383. }
  384. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  385. qnetd_log_debug_new_client_connected(client);
  386. reply_error_code = qnetd_algorithm_client_init(client);
  387. }
  388. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  389. /*
  390. * Correct init received
  391. */
  392. client->init_received = 1;
  393. } else {
  394. log(LOG_ERR, "Algorithm returned error code. Sending error reply.");
  395. }
  396. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  397. if (send_buffer == NULL) {
  398. log(LOG_ERR, "Can't alloc init reply msg from list. "
  399. "Disconnecting client connection.");
  400. return (-1);
  401. }
  402. if (msg_create_init_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number,
  403. reply_error_code,
  404. supported_msgs, no_supported_msgs, supported_opts, no_supported_opts,
  405. instance->advanced_settings->max_client_receive_size,
  406. instance->advanced_settings->max_client_send_size,
  407. qnetd_static_supported_decision_algorithms,
  408. QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE) == 0) {
  409. log(LOG_ERR, "Can't alloc init reply msg. Disconnecting client connection.");
  410. send_buffer_list_discard_new(&client->send_buffer_list, send_buffer);
  411. return (-1);
  412. }
  413. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  414. return (0);
  415. }
  416. static int
  417. qnetd_client_msg_received_init_reply(struct qnetd_instance *instance, struct qnetd_client *client,
  418. const struct msg_decoded *msg)
  419. {
  420. return (qnetd_client_msg_received_unexpected_msg(client, msg, "init reply"));
  421. }
  422. static int
  423. qnetd_client_msg_received_set_option_reply(struct qnetd_instance *instance,
  424. struct qnetd_client *client, const struct msg_decoded *msg)
  425. {
  426. return (qnetd_client_msg_received_unexpected_msg(client, msg, "set option reply"));
  427. }
  428. static int
  429. qnetd_client_msg_received_set_option(struct qnetd_instance *instance, struct qnetd_client *client,
  430. const struct msg_decoded *msg)
  431. {
  432. int res;
  433. struct send_buffer_list_entry *send_buffer;
  434. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  435. return (res == -1 ? -1 : 0);
  436. }
  437. if (!client->init_received) {
  438. log(LOG_ERR, "Received set option message before init message. "
  439. "Sending error reply.");
  440. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  441. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  442. return (-1);
  443. }
  444. return (0);
  445. }
  446. if (msg->heartbeat_interval_set) {
  447. /*
  448. * Check if heartbeat interval is valid
  449. */
  450. if (msg->heartbeat_interval < instance->advanced_settings->heartbeat_interval_min ||
  451. msg->heartbeat_interval > instance->advanced_settings->heartbeat_interval_max) {
  452. log(LOG_ERR, "Client requested invalid heartbeat interval %u. "
  453. "Sending error reply.", msg->heartbeat_interval);
  454. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  455. TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL) != 0) {
  456. return (-1);
  457. }
  458. return (0);
  459. }
  460. client->heartbeat_interval = msg->heartbeat_interval;
  461. if (qnetd_client_dpd_timer_update_interval(instance, client) != 0) {
  462. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  463. TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL) != 0) {
  464. return (-1);
  465. }
  466. return (0);
  467. }
  468. }
  469. if (msg->keep_active_partition_tie_breaker_set) {
  470. client->keep_active_partition_tie_breaker =
  471. msg->keep_active_partition_tie_breaker;
  472. }
  473. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  474. if (send_buffer == NULL) {
  475. log(LOG_ERR, "Can't alloc set option reply msg from list. "
  476. "Disconnecting client connection.");
  477. return (-1);
  478. }
  479. if (msg_create_set_option_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number,
  480. msg->heartbeat_interval_set, client->heartbeat_interval,
  481. msg->keep_active_partition_tie_breaker_set, client->keep_active_partition_tie_breaker) == 0) {
  482. log(LOG_ERR, "Can't alloc set option reply msg. "
  483. "Disconnecting client connection.");
  484. send_buffer_list_discard_new(&client->send_buffer_list, send_buffer);
  485. return (-1);
  486. }
  487. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  488. return (0);
  489. }
  490. static int
  491. qnetd_client_msg_received_echo_reply(struct qnetd_instance *instance, struct qnetd_client *client,
  492. const struct msg_decoded *msg)
  493. {
  494. return (qnetd_client_msg_received_unexpected_msg(client, msg, "echo reply"));
  495. }
  496. static int
  497. qnetd_client_msg_received_echo_request(struct qnetd_instance *instance, struct qnetd_client *client,
  498. const struct msg_decoded *msg, const struct dynar *msg_orig)
  499. {
  500. int res;
  501. struct send_buffer_list_entry *send_buffer;
  502. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  503. return (res == -1 ? -1 : 0);
  504. }
  505. if (!client->init_received) {
  506. log(LOG_ERR, "Received echo request before init message. "
  507. "Sending error reply.");
  508. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  509. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  510. return (-1);
  511. }
  512. return (0);
  513. }
  514. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  515. if (send_buffer == NULL) {
  516. log(LOG_ERR, "Can't alloc echo reply msg from list. "
  517. "Disconnecting client connection.");
  518. return (-1);
  519. }
  520. if (msg_create_echo_reply(&send_buffer->buffer, msg_orig) == 0) {
  521. log(LOG_ERR, "Can't alloc echo reply msg. Disconnecting client connection.");
  522. send_buffer_list_discard_new(&client->send_buffer_list, send_buffer);
  523. return (-1);
  524. }
  525. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  526. return (0);
  527. }
  528. static int
  529. qnetd_client_msg_received_node_list(struct qnetd_instance *instance, struct qnetd_client *client,
  530. const struct msg_decoded *msg)
  531. {
  532. int res;
  533. struct send_buffer_list_entry *send_buffer;
  534. enum tlv_reply_error_code reply_error_code;
  535. enum tlv_vote result_vote;
  536. int case_processed;
  537. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  538. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  539. return (res == -1 ? -1 : 0);
  540. }
  541. if (!client->init_received) {
  542. log(LOG_ERR, "Received node list message before init message. "
  543. "Sending error reply.");
  544. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  545. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  546. return (-1);
  547. }
  548. return (0);
  549. }
  550. if (!msg->node_list_type_set) {
  551. log(LOG_ERR, "Received node list message without node list type set. "
  552. "Sending error reply.");
  553. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  554. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  555. return (-1);
  556. }
  557. return (0);
  558. }
  559. if (!msg->seq_number_set) {
  560. log(LOG_ERR, "Received node list message without seq number set. "
  561. "Sending error reply.");
  562. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  563. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  564. return (-1);
  565. }
  566. return (0);
  567. }
  568. result_vote = TLV_VOTE_NO_CHANGE;
  569. case_processed = 0;
  570. switch (msg->node_list_type) {
  571. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  572. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  573. case_processed = 1;
  574. qnetd_log_debug_config_node_list_received(client, msg->seq_number,
  575. msg->config_version_set, msg->config_version, &msg->nodes,
  576. (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG));
  577. reply_error_code = qnetd_algorithm_config_node_list_received(client,
  578. msg->seq_number, msg->config_version_set, msg->config_version,
  579. &msg->nodes,
  580. (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG),
  581. &result_vote);
  582. break;
  583. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  584. case_processed = 1;
  585. if (!msg->ring_id_set) {
  586. log(LOG_ERR, "Received node list message without ring id number set. "
  587. "Sending error reply.");
  588. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  589. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  590. return (-1);
  591. }
  592. return (0);
  593. }
  594. qnetd_log_debug_membership_node_list_received(client, msg->seq_number, &msg->ring_id,
  595. msg->heuristics, &msg->nodes);
  596. reply_error_code = qnetd_algorithm_membership_node_list_received(client,
  597. msg->seq_number, &msg->ring_id, &msg->nodes, msg->heuristics, &result_vote);
  598. break;
  599. case TLV_NODE_LIST_TYPE_QUORUM:
  600. case_processed = 1;
  601. if (!msg->quorate_set) {
  602. log(LOG_ERR, "Received quorum list message without quorate set. "
  603. "Sending error reply.");
  604. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  605. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  606. return (-1);
  607. }
  608. return (0);
  609. }
  610. qnetd_log_debug_quorum_node_list_received(client, msg->seq_number,msg->quorate,
  611. &msg->nodes);
  612. reply_error_code = qnetd_algorithm_quorum_node_list_received(client,
  613. msg->seq_number,msg->quorate, &msg->nodes, &result_vote);
  614. break;
  615. /*
  616. * Default is not defined intentionally. Compiler shows warning when new
  617. * node list type is added
  618. */
  619. }
  620. if (!case_processed) {
  621. log(LOG_ERR, "qnetd_client_msg_received_node_list fatal error. "
  622. "Unhandled node_list_type");
  623. exit(EXIT_FAILURE);
  624. }
  625. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  626. log(LOG_ERR, "Algorithm returned error code. "
  627. "Sending error reply.");
  628. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  629. reply_error_code) != 0) {
  630. return (-1);
  631. }
  632. return (0);
  633. } else {
  634. log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  635. }
  636. /*
  637. * Store node list for future use
  638. */
  639. case_processed = 0;
  640. switch (msg->node_list_type) {
  641. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  642. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  643. case_processed = 1;
  644. node_list_free(&client->configuration_node_list);
  645. if (node_list_clone(&client->configuration_node_list, &msg->nodes) == -1) {
  646. log(LOG_ERR, "Can't alloc config node list clone. "
  647. "Disconnecting client connection.");
  648. return (-1);
  649. }
  650. client->config_version_set = msg->config_version_set;
  651. client->config_version = msg->config_version;
  652. break;
  653. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  654. case_processed = 1;
  655. node_list_free(&client->last_membership_node_list);
  656. if (node_list_clone(&client->last_membership_node_list, &msg->nodes) == -1) {
  657. log(LOG_ERR, "Can't alloc membership node list clone. "
  658. "Disconnecting client connection.");
  659. return (-1);
  660. }
  661. memcpy(&client->last_ring_id, &msg->ring_id, sizeof(struct tlv_ring_id));
  662. client->last_membership_heuristics = msg->heuristics;
  663. client->last_heuristics = msg->heuristics;
  664. break;
  665. case TLV_NODE_LIST_TYPE_QUORUM:
  666. case_processed = 1;
  667. node_list_free(&client->last_quorum_node_list);
  668. if (node_list_clone(&client->last_quorum_node_list, &msg->nodes) == -1) {
  669. log(LOG_ERR, "Can't alloc quorum node list clone. "
  670. "Disconnecting client connection.");
  671. return (-1);
  672. }
  673. break;
  674. /*
  675. * Default is not defined intentionally. Compiler shows warning when new
  676. * node list type is added
  677. */
  678. }
  679. if (!case_processed) {
  680. log(LOG_ERR, "qnetd_client_msg_received_node_list fatal error. "
  681. "Unhandled node_list_type");
  682. exit(EXIT_FAILURE);
  683. }
  684. /*
  685. * Store result vote
  686. */
  687. client->last_sent_vote = result_vote;
  688. if (result_vote == TLV_VOTE_ACK || result_vote == TLV_VOTE_NACK) {
  689. client->last_sent_ack_nack_vote = result_vote;
  690. }
  691. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  692. if (send_buffer == NULL) {
  693. log(LOG_ERR, "Can't alloc node list reply msg from list. "
  694. "Disconnecting client connection.");
  695. return (-1);
  696. }
  697. if (msg_create_node_list_reply(&send_buffer->buffer, msg->seq_number, msg->node_list_type,
  698. &client->last_ring_id, result_vote) == 0) {
  699. log(LOG_ERR, "Can't alloc node list reply msg. "
  700. "Disconnecting client connection.");
  701. send_buffer_list_discard_new(&client->send_buffer_list, send_buffer);
  702. return (-1);
  703. }
  704. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  705. return (0);
  706. }
  707. static int
  708. qnetd_client_msg_received_node_list_reply(struct qnetd_instance *instance,
  709. struct qnetd_client *client, const struct msg_decoded *msg)
  710. {
  711. return (qnetd_client_msg_received_unexpected_msg(client, msg, "node list reply"));
  712. }
  713. static int
  714. qnetd_client_msg_received_ask_for_vote(struct qnetd_instance *instance, struct qnetd_client *client,
  715. const struct msg_decoded *msg)
  716. {
  717. int res;
  718. struct send_buffer_list_entry *send_buffer;
  719. enum tlv_reply_error_code reply_error_code;
  720. enum tlv_vote result_vote;
  721. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  722. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  723. return (res == -1 ? -1 : 0);
  724. }
  725. if (!client->init_received) {
  726. log(LOG_ERR, "Received ask for vote message before init message. "
  727. "Sending error reply.");
  728. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  729. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  730. return (-1);
  731. }
  732. return (0);
  733. }
  734. if (!msg->seq_number_set) {
  735. log(LOG_ERR, "Received ask for vote message without seq number set. "
  736. "Sending error reply.");
  737. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  738. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  739. return (-1);
  740. }
  741. return (0);
  742. }
  743. qnetd_log_debug_ask_for_vote_received(client, msg->seq_number);
  744. reply_error_code = qnetd_algorithm_ask_for_vote_received(client, msg->seq_number,
  745. &result_vote);
  746. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  747. log(LOG_ERR, "Algorithm returned error code. "
  748. "Sending error reply.");
  749. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  750. reply_error_code) != 0) {
  751. return (-1);
  752. }
  753. return (0);
  754. } else {
  755. log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  756. }
  757. /*
  758. * Store result vote
  759. */
  760. client->last_sent_vote = result_vote;
  761. if (result_vote == TLV_VOTE_ACK || result_vote == TLV_VOTE_NACK) {
  762. client->last_sent_ack_nack_vote = result_vote;
  763. }
  764. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  765. if (send_buffer == NULL) {
  766. log(LOG_ERR, "Can't alloc ask for vote reply msg from list. "
  767. "Disconnecting client connection.");
  768. return (-1);
  769. }
  770. if (msg_create_ask_for_vote_reply(&send_buffer->buffer, msg->seq_number,
  771. &client->last_ring_id, result_vote) == 0) {
  772. log(LOG_ERR, "Can't alloc ask for vote reply msg. "
  773. "Disconnecting client connection.");
  774. send_buffer_list_discard_new(&client->send_buffer_list, send_buffer);
  775. return (-1);
  776. }
  777. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  778. return (0);
  779. }
  780. static int
  781. qnetd_client_msg_received_ask_for_vote_reply(struct qnetd_instance *instance,
  782. struct qnetd_client *client, const struct msg_decoded *msg)
  783. {
  784. return (qnetd_client_msg_received_unexpected_msg(client, msg, "ask for vote reply"));
  785. }
  786. static int
  787. qnetd_client_msg_received_vote_info(struct qnetd_instance *instance, struct qnetd_client *client,
  788. const struct msg_decoded *msg)
  789. {
  790. return (qnetd_client_msg_received_unexpected_msg(client, msg, "vote info"));
  791. }
  792. static int
  793. qnetd_client_msg_received_vote_info_reply(struct qnetd_instance *instance,
  794. struct qnetd_client *client, const struct msg_decoded *msg)
  795. {
  796. int res;
  797. enum tlv_reply_error_code reply_error_code;
  798. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  799. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  800. return (res == -1 ? -1 : 0);
  801. }
  802. if (!client->init_received) {
  803. log(LOG_ERR, "Received vote info reply before init message. "
  804. "Sending error reply.");
  805. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  806. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  807. return (-1);
  808. }
  809. return (0);
  810. }
  811. if (!msg->seq_number_set) {
  812. log(LOG_ERR, "Received vote info reply message without seq number set. "
  813. "Sending error reply.");
  814. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  815. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  816. return (-1);
  817. }
  818. return (0);
  819. }
  820. qnetd_log_debug_vote_info_reply_received(client, msg->seq_number);
  821. reply_error_code = qnetd_algorithm_vote_info_reply_received(client, msg->seq_number);
  822. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  823. log(LOG_ERR, "Algorithm returned error code. "
  824. "Sending error reply.");
  825. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  826. reply_error_code) != 0) {
  827. return (-1);
  828. }
  829. return (0);
  830. }
  831. return (0);
  832. }
  833. static int
  834. qnetd_client_msg_received_heuristics_change(struct qnetd_instance *instance, struct qnetd_client *client,
  835. const struct msg_decoded *msg)
  836. {
  837. int res;
  838. struct send_buffer_list_entry *send_buffer;
  839. enum tlv_reply_error_code reply_error_code;
  840. enum tlv_vote result_vote;
  841. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  842. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  843. return (res == -1 ? -1 : 0);
  844. }
  845. if (!client->init_received) {
  846. log(LOG_ERR, "Received heuristics change message before init message. "
  847. "Sending error reply.");
  848. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  849. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  850. return (-1);
  851. }
  852. return (0);
  853. }
  854. if (!msg->seq_number_set || msg->heuristics == TLV_HEURISTICS_UNDEFINED) {
  855. log(LOG_ERR, "Received heuristics change message without seq number set or "
  856. "with undefined heuristics. Sending error reply.");
  857. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  858. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  859. return (-1);
  860. }
  861. return (0);
  862. }
  863. qnetd_log_debug_heuristics_change_received(client, msg->seq_number, msg->heuristics);
  864. reply_error_code = qnetd_algorithm_heuristics_change_received(client, msg->seq_number,
  865. msg->heuristics, &result_vote);
  866. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  867. log(LOG_ERR, "Algorithm returned error code. "
  868. "Sending error reply.");
  869. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  870. reply_error_code) != 0) {
  871. return (-1);
  872. }
  873. return (0);
  874. } else {
  875. log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  876. }
  877. /*
  878. * Store result vote and heuristics result
  879. */
  880. client->last_sent_vote = result_vote;
  881. if (result_vote == TLV_VOTE_ACK || result_vote == TLV_VOTE_NACK) {
  882. client->last_sent_ack_nack_vote = result_vote;
  883. }
  884. client->last_regular_heuristics = msg->heuristics;
  885. client->last_heuristics = msg->heuristics;
  886. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  887. if (send_buffer == NULL) {
  888. log(LOG_ERR, "Can't alloc heuristics change reply msg from list. "
  889. "Disconnecting client connection.");
  890. return (-1);
  891. }
  892. if (msg_create_heuristics_change_reply(&send_buffer->buffer, msg->seq_number,
  893. &client->last_ring_id, msg->heuristics, result_vote) == 0) {
  894. log(LOG_ERR, "Can't alloc heuristics change reply msg. "
  895. "Disconnecting client connection.");
  896. send_buffer_list_discard_new(&client->send_buffer_list, send_buffer);
  897. return (-1);
  898. }
  899. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  900. return (0);
  901. }
  902. static int
  903. qnetd_client_msg_received_heuristics_change_reply(struct qnetd_instance *instance, struct qnetd_client *client,
  904. const struct msg_decoded *msg)
  905. {
  906. return (qnetd_client_msg_received_unexpected_msg(client, msg, "heuristics change reply"));
  907. }
  908. int
  909. qnetd_client_msg_received(struct qnetd_instance *instance, struct qnetd_client *client)
  910. {
  911. struct msg_decoded msg;
  912. int res;
  913. int ret_val;
  914. int msg_processed;
  915. qnetd_client_dpd_timer_reschedule(instance, client);
  916. msg_decoded_init(&msg);
  917. res = msg_decode(&client->receive_buffer, &msg);
  918. if (res != 0) {
  919. /*
  920. * Error occurred. Send server error.
  921. */
  922. log_common_msg_decode_error(res);
  923. log(LOG_INFO, "Sending back error message");
  924. if (qnetd_client_send_err(client, msg.seq_number_set, msg.seq_number,
  925. TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG) != 0) {
  926. return (-1);
  927. }
  928. return (0);
  929. }
  930. ret_val = 0;
  931. msg_processed = 0;
  932. switch (msg.type) {
  933. case MSG_TYPE_PREINIT:
  934. msg_processed = 1;
  935. ret_val = qnetd_client_msg_received_preinit(instance, client, &msg);
  936. break;
  937. case MSG_TYPE_PREINIT_REPLY:
  938. msg_processed = 1;
  939. ret_val = qnetd_client_msg_received_preinit_reply(instance, client, &msg);
  940. break;
  941. case MSG_TYPE_STARTTLS:
  942. msg_processed = 1;
  943. ret_val = qnetd_client_msg_received_starttls(instance, client, &msg);
  944. break;
  945. case MSG_TYPE_INIT:
  946. msg_processed = 1;
  947. ret_val = qnetd_client_msg_received_init(instance, client, &msg);
  948. break;
  949. case MSG_TYPE_INIT_REPLY:
  950. msg_processed = 1;
  951. ret_val = qnetd_client_msg_received_init_reply(instance, client, &msg);
  952. break;
  953. case MSG_TYPE_SERVER_ERROR:
  954. msg_processed = 1;
  955. ret_val = qnetd_client_msg_received_server_error(instance, client, &msg);
  956. break;
  957. case MSG_TYPE_SET_OPTION:
  958. msg_processed = 1;
  959. ret_val = qnetd_client_msg_received_set_option(instance, client, &msg);
  960. break;
  961. case MSG_TYPE_SET_OPTION_REPLY:
  962. msg_processed = 1;
  963. ret_val = qnetd_client_msg_received_set_option_reply(instance, client, &msg);
  964. break;
  965. case MSG_TYPE_ECHO_REQUEST:
  966. msg_processed = 1;
  967. ret_val = qnetd_client_msg_received_echo_request(instance, client, &msg,
  968. &client->receive_buffer);
  969. break;
  970. case MSG_TYPE_ECHO_REPLY:
  971. msg_processed = 1;
  972. ret_val = qnetd_client_msg_received_echo_reply(instance, client, &msg);
  973. break;
  974. case MSG_TYPE_NODE_LIST:
  975. msg_processed = 1;
  976. ret_val = qnetd_client_msg_received_node_list(instance, client, &msg);
  977. break;
  978. case MSG_TYPE_NODE_LIST_REPLY:
  979. msg_processed = 1;
  980. ret_val = qnetd_client_msg_received_node_list_reply(instance, client, &msg);
  981. break;
  982. case MSG_TYPE_ASK_FOR_VOTE:
  983. msg_processed = 1;
  984. ret_val = qnetd_client_msg_received_ask_for_vote(instance, client, &msg);
  985. break;
  986. case MSG_TYPE_ASK_FOR_VOTE_REPLY:
  987. msg_processed = 1;
  988. ret_val = qnetd_client_msg_received_ask_for_vote_reply(instance, client, &msg);
  989. break;
  990. case MSG_TYPE_VOTE_INFO:
  991. msg_processed = 1;
  992. ret_val = qnetd_client_msg_received_vote_info(instance, client, &msg);
  993. break;
  994. case MSG_TYPE_VOTE_INFO_REPLY:
  995. msg_processed = 1;
  996. ret_val = qnetd_client_msg_received_vote_info_reply(instance, client, &msg);
  997. break;
  998. case MSG_TYPE_HEURISTICS_CHANGE:
  999. msg_processed = 1;
  1000. ret_val = qnetd_client_msg_received_heuristics_change(instance, client, &msg);
  1001. break;
  1002. case MSG_TYPE_HEURISTICS_CHANGE_REPLY:
  1003. msg_processed = 1;
  1004. ret_val = qnetd_client_msg_received_heuristics_change_reply(instance, client,
  1005. &msg);
  1006. break;
  1007. /*
  1008. * Default is not defined intentionally. Compiler shows warning when new
  1009. * msg type is added.
  1010. */
  1011. }
  1012. if (!msg_processed) {
  1013. log(LOG_ERR, "Unsupported message %u received from client. "
  1014. "Sending back error message", msg.type);
  1015. if (qnetd_client_send_err(client, msg.seq_number_set, msg.seq_number,
  1016. TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE) != 0) {
  1017. ret_val = -1;
  1018. }
  1019. }
  1020. msg_decoded_destroy(&msg);
  1021. return (ret_val);
  1022. }