totemconfig.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.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 MontaVista Software, 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 <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <assert.h>
  39. #include <unistd.h>
  40. #include <sys/socket.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <fcntl.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <sys/param.h>
  47. #include "../include/list.h"
  48. #include "util.h"
  49. #include "totem.h"
  50. #include "totemconfig.h"
  51. #include "print.h"
  52. #define LOG_SERVICE LOG_SERVICE_GMI
  53. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  54. #define TOKEN_TIMEOUT 1000
  55. #define TOKEN_RETRANSMIT_TIMEOUT (int)(TOKEN_TIMEOUT / (TOKEN_RETRANSMITS_BEFORE_LOSS_CONST + 0.2))
  56. #define TOKEN_HOLD_TIMEOUT (int)(TOKEN_RETRANSMIT_TIMEOUT * 0.8 - (1000/(int)HZ))
  57. #define JOIN_TIMEOUT 100
  58. #define CONSENSUS_TIMEOUT 200
  59. #define MERGE_TIMEOUT 200
  60. #define DOWNCHECK_TIMEOUT 1000
  61. #define FAIL_TO_RECV_CONST 10
  62. #define SEQNO_UNCHANGED_CONST 30
  63. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  64. #define MAX_NETWORK_DELAY 50 /*In milliseconds*/
  65. static char error_string_response[512];
  66. typedef enum {
  67. MAIN_HEAD,
  68. MAIN_TOTEM
  69. } main_parse_t;
  70. static inline char *
  71. strstr_rs (const char *haystack, const char *needle)
  72. {
  73. char *end_address;
  74. char *new_needle;
  75. char *token = (char *)(needle + strlen (needle) - 1); /* last char is always the token */
  76. new_needle = (char *)strdup (needle);
  77. new_needle[strlen(new_needle) - 1] = '\0'; /* remove token */
  78. end_address = strstr (haystack, new_needle);
  79. if (end_address == 0) {
  80. goto not_found;
  81. }
  82. end_address += strlen (new_needle);
  83. /*
  84. * Parse all tabs and spaces until token is found
  85. * if other character found besides token, its not a match
  86. */
  87. do {
  88. if (*end_address == '\t' || *end_address == ' ') {
  89. end_address++;
  90. } else {
  91. break;
  92. }
  93. } while (*end_address != '\0' && *end_address != token[0]);
  94. if (*end_address != token[0]) {
  95. end_address = 0;
  96. goto not_found;
  97. }
  98. end_address++; /* skip past token */
  99. do {
  100. if (*end_address == '\t' || *end_address == ' ') {
  101. end_address++;
  102. } else {
  103. break;
  104. }
  105. } while (*end_address != '\0');
  106. not_found:
  107. free (new_needle);
  108. return (end_address);
  109. }
  110. extern int totem_config_read (
  111. struct totem_config *totem_config,
  112. char **error_string,
  113. int interface_max)
  114. {
  115. FILE *fp;
  116. int res = 0;
  117. int line_number = 0;
  118. main_parse_t parse = MAIN_HEAD;
  119. int totem_parsed = 0;
  120. char *loc;
  121. int i;
  122. int parse_done = 0;
  123. char line[512];
  124. char *error_reason = error_string_response;
  125. memset (totem_config, 0, sizeof (struct totem_config));
  126. totem_config->interfaces = malloc (sizeof (struct totem_interface) * interface_max);
  127. if (totem_config->interfaces == 0) {
  128. parse_done = 1;
  129. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  130. return -1;
  131. }
  132. memset (totem_config->interfaces, 0,
  133. sizeof (struct totem_interface) * interface_max);
  134. totem_config->mcast_addr.sin_family = AF_INET;
  135. totem_config->secauth = 1;
  136. fp = fopen ("/etc/ais/openais.conf", "r");
  137. if (fp == 0) {
  138. parse_done = 1;
  139. sprintf (error_reason, "Can't read file reason = (%s)\n", strerror (errno));
  140. *error_string = error_reason;
  141. return -1;
  142. }
  143. while (fgets (line, 255, fp)) {
  144. line_number += 1;
  145. line[strlen(line) - 1] = '\0';
  146. /*
  147. * Clear out white space and tabs
  148. */
  149. for (i = strlen (line) - 1; i > -1; i--) {
  150. if (line[i] == '\t' || line[i] == ' ') {
  151. line[i] = '\0';
  152. } else {
  153. break;
  154. }
  155. }
  156. /*
  157. * Clear out comments and empty lines
  158. */
  159. if (line[0] == '#' || line[0] == '\0') {
  160. continue;
  161. }
  162. line_number += 1;
  163. switch (parse) {
  164. case MAIN_HEAD:
  165. if (totem_parsed == 0 && strstr_rs (line, "network{")) {
  166. totem_parsed = 1;
  167. parse = MAIN_TOTEM;
  168. } else
  169. if (totem_parsed == 0 && strstr_rs (line, "totem{")) {
  170. totem_parsed = 1;
  171. parse = MAIN_TOTEM;
  172. } else {
  173. continue;
  174. }
  175. break;
  176. case MAIN_TOTEM:
  177. if ((loc = strstr_rs (line, "version:"))) {
  178. if (strcmp (loc, "1") == 0) {
  179. totem_config->version = 1;
  180. }
  181. } else
  182. if ((loc = strstr_rs (line, "secauth:"))) {
  183. if (strcmp (loc, "on") == 0) {
  184. totem_config->secauth = 1;
  185. } else
  186. if (strcmp (loc, "off") == 0) {
  187. totem_config->secauth = 0;
  188. }
  189. } else
  190. if ((loc = strstr_rs (line, "threads:"))) {
  191. totem_config->threads = atoi (loc);
  192. } else
  193. if ((loc = strstr_rs (line, "netmtu:"))) {
  194. totem_config->net_mtu = atoi (loc);
  195. } else
  196. if ((loc = strstr_rs (line, "mcastaddr:"))) {
  197. res = inet_aton (loc, &totem_config->mcast_addr.sin_addr);
  198. } else
  199. if ((loc = strstr_rs (line, "mcastport:"))) {
  200. res = totem_config->mcast_addr.sin_port = htons (atoi (loc));
  201. } else
  202. if ((loc = strstr_rs (line, "bindnetaddr:"))) {
  203. if (interface_max == totem_config->interface_count) {
  204. sprintf (error_reason,
  205. "%d is too many interfaces in /etc/ais/network.conf",
  206. totem_config->interface_count);
  207. goto parse_error;
  208. }
  209. res = inet_aton (loc,
  210. &totem_config->interfaces[totem_config->interface_count].bindnet.sin_addr);
  211. totem_config->interface_count += 1;
  212. } else
  213. if ((loc = strstr_rs (line, "token:"))) {
  214. totem_config->token_timeout = atoi(loc);
  215. } else if ((loc = strstr_rs (line, "token_retransmit:"))) {
  216. totem_config->token_retransmit_timeout = atoi(loc);
  217. } else if ((loc = strstr_rs (line, "hold:"))) {
  218. totem_config->token_hold_timeout = atoi(loc);
  219. } else if ((loc = strstr_rs (line, "token_retransmits_before_loss_const:"))) {
  220. totem_config->token_retransmits_before_loss_const = atoi(loc);
  221. } else if ((loc = strstr_rs (line, "join:"))) {
  222. totem_config->join_timeout = atoi(loc);
  223. } else if ((loc = strstr_rs (line, "consensus:"))) {
  224. totem_config->consensus_timeout = atoi(loc);
  225. } else if ((loc = strstr_rs (line, "merge:"))) {
  226. totem_config->merge_timeout = atoi(loc);
  227. } else if ((loc = strstr_rs (line, "downcheck:"))) {
  228. totem_config->downcheck_timeout = atoi(loc);
  229. } else if ((loc = strstr_rs (line, "fail_recv_const:"))) {
  230. totem_config->fail_to_recv_const = atoi(loc);
  231. } else if ((loc = strstr_rs (line, "seqno_unchanged_const:"))) {
  232. totem_config->seqno_unchanged_const = atoi(loc);
  233. } else if ((loc = strstr_rs (line, "heartbeat_failures_allowed:"))) {
  234. totem_config->heartbeat_failures_allowed = atoi(loc);
  235. } else if ((loc = strstr_rs (line, "max_network_delay:"))) {
  236. totem_config->max_network_delay = atoi(loc);
  237. } else if ((loc = strstr_rs (line, "}"))) {
  238. parse = MAIN_HEAD;
  239. } else {
  240. goto parse_error;
  241. }
  242. break;
  243. default:
  244. assert (0 == 1); /* SHOULDN'T HAPPEN */
  245. break;
  246. }
  247. }
  248. if (parse == MAIN_HEAD) {
  249. fclose (fp);
  250. return (0);
  251. } else {
  252. error_reason = "Missing closing brace";
  253. goto parse_error;
  254. }
  255. parse_error:
  256. if (parse_done) {
  257. sprintf (error_string_response,
  258. "parse error in /etc/ais/openais.conf: %s.\n", error_reason);
  259. } else {
  260. sprintf (error_string_response,
  261. "parse error in /etc/ais/openais.conf: %s (line %d).\n",
  262. error_reason, line_number);
  263. }
  264. *error_string = error_string_response;
  265. fclose (fp);
  266. return (-1);
  267. }
  268. int totem_config_validate (
  269. struct totem_config *totem_config,
  270. char **error_string)
  271. {
  272. static char local_error_reason[512];
  273. char *error_reason = local_error_reason;
  274. /*
  275. * Some error checking of parsed data to make sure its valid
  276. */
  277. if (totem_config->mcast_addr.sin_addr.s_addr == 0) {
  278. error_reason = "No multicast address specified";
  279. goto parse_error;
  280. }
  281. if (totem_config->mcast_addr.sin_port == 0) {
  282. error_reason = "No multicast port specified";
  283. goto parse_error;
  284. }
  285. if (totem_config->interface_count == 0) {
  286. error_reason = "No bindnet specified";
  287. goto parse_error;
  288. }
  289. if (totem_config->version != 1) {
  290. error_reason = "This totem parser can only parse version 1 configuration files.";
  291. goto parse_error;
  292. }
  293. if (totem_config->token_retransmits_before_loss_const == 0) {
  294. totem_config->token_retransmits_before_loss_const =
  295. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  296. }
  297. /*
  298. * Setup timeout values that are not setup by user
  299. */
  300. if (totem_config->token_timeout == 0) {
  301. totem_config->token_timeout = TOKEN_TIMEOUT;
  302. if (totem_config->token_retransmits_before_loss_const == 0) {
  303. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  304. }
  305. if (totem_config->token_retransmit_timeout == 0) {
  306. totem_config->token_retransmit_timeout =
  307. (int)(totem_config->token_timeout /
  308. (totem_config->token_retransmits_before_loss_const + 0.2));
  309. }
  310. if (totem_config->token_hold_timeout == 0) {
  311. totem_config->token_hold_timeout =
  312. (int)(totem_config->token_retransmit_timeout * 0.8 -
  313. (1000/HZ));
  314. }
  315. }
  316. if (totem_config->max_network_delay == 0) {
  317. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  318. }
  319. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  320. sprintf (local_error_reason, "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  321. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  322. goto parse_error;
  323. }
  324. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  325. sprintf (local_error_reason, "The token timeout parameter (%d ms) may not be less then (%d ms).",
  326. totem_config->token_timeout, MINIMUM_TIMEOUT);
  327. goto parse_error;
  328. }
  329. if (totem_config->token_retransmit_timeout == 0) {
  330. totem_config->token_retransmit_timeout =
  331. (int)(totem_config->token_timeout /
  332. (totem_config->token_retransmits_before_loss_const + 0.2));
  333. }
  334. if (totem_config->token_hold_timeout == 0) {
  335. totem_config->token_hold_timeout =
  336. (int)(totem_config->token_retransmit_timeout * 0.8 -
  337. (1000/HZ));
  338. }
  339. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  340. sprintf (local_error_reason, "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  341. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  342. goto parse_error;
  343. }
  344. if (totem_config->token_hold_timeout == 0) {
  345. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  346. }
  347. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  348. sprintf (local_error_reason, "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  349. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  350. goto parse_error;
  351. }
  352. if (totem_config->join_timeout == 0) {
  353. totem_config->join_timeout = JOIN_TIMEOUT;
  354. }
  355. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  356. sprintf (local_error_reason, "The join timeout parameter (%d ms) may not be less then (%d ms).",
  357. totem_config->join_timeout, MINIMUM_TIMEOUT);
  358. goto parse_error;
  359. }
  360. if (totem_config->consensus_timeout == 0) {
  361. totem_config->consensus_timeout = CONSENSUS_TIMEOUT;
  362. }
  363. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  364. sprintf (local_error_reason, "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  365. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  366. goto parse_error;
  367. }
  368. if (totem_config->merge_timeout == 0) {
  369. totem_config->merge_timeout = MERGE_TIMEOUT;
  370. }
  371. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  372. sprintf (local_error_reason, "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  373. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  374. goto parse_error;
  375. }
  376. if (totem_config->downcheck_timeout == 0) {
  377. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  378. }
  379. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  380. sprintf (local_error_reason, "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  381. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  382. goto parse_error;
  383. }
  384. if (totem_config->fail_to_recv_const == 0) {
  385. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  386. }
  387. if (totem_config->seqno_unchanged_const == 0) {
  388. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  389. }
  390. if (totem_config->net_mtu == 0) {
  391. totem_config->net_mtu = 1500;
  392. }
  393. if (totem_config->threads > SEND_THREADS_MAX) {
  394. totem_config->threads = SEND_THREADS_MAX;
  395. }
  396. if (totem_config->secauth == 0) {
  397. totem_config->threads = 0;
  398. }
  399. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  400. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  401. goto parse_error;
  402. }
  403. return (0);
  404. parse_error:
  405. sprintf (error_string_response,
  406. "parse error in /etc/ais/openais.conf: %s\n", error_reason);
  407. *error_string = error_string_response;
  408. return (-1);
  409. }
  410. int totem_config_keyread (
  411. char *key_location,
  412. struct totem_config *totem_config,
  413. char **error_string)
  414. {
  415. int fd;
  416. int res;
  417. if (totem_config->secauth == 0) {
  418. return (0);
  419. }
  420. fd = open (key_location, O_RDONLY);
  421. if (fd == -1) {
  422. sprintf (error_string_response, "Could not open %s: %s\n",
  423. key_location, strerror (errno));
  424. goto parse_error;
  425. }
  426. res = read (fd, totem_config->private_key, 128);
  427. if (res == -1) {
  428. close (fd);
  429. sprintf (error_string_response, "Could not read %s: %s\n",
  430. key_location, strerror (errno));
  431. goto parse_error;
  432. }
  433. totem_config->private_key_len = 128;
  434. if (res != 128) {
  435. close (fd);
  436. sprintf (error_string_response, "Could only read %d bits of 1024 bits from %s.\n",
  437. res * 8, key_location);
  438. goto parse_error;
  439. }
  440. return (0);
  441. parse_error:
  442. *error_string = error_string_response;
  443. return (-1);
  444. }