totemconfig.c 12 KB

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