totemconfig.c 14 KB

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