parse.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 <sys/socket.h>
  38. #include <netinet/in.h>
  39. #include <arpa/inet.h>
  40. #include "../include/ais_types.h"
  41. #include "../include/list.h"
  42. #include "parse.h"
  43. #include "mempool.h"
  44. DECLARE_LIST_INIT (saAmfGroupHead);
  45. typedef enum {
  46. HEAD,
  47. GROUP,
  48. UNIT,
  49. PROTECTION,
  50. COMPONENT
  51. } SaParsingT;
  52. void setSaNameT (SaNameT *name, char *str) {
  53. strncpy (name->value, str, SA_MAX_NAME_LENGTH);
  54. if (strlen (name->value) > SA_MAX_NAME_LENGTH) {
  55. name->length = SA_MAX_NAME_LENGTH;
  56. } else {
  57. name->length = strlen (str);
  58. }
  59. }
  60. int SaNameTisEqual (SaNameT *str1, char *str2) {
  61. if (str1->length == strlen (str2)) {
  62. return ((strncmp (str1->value, str2, str1->length)) == 0);
  63. } else {
  64. return 0;
  65. }
  66. }
  67. int SaNameTisNameT (SaNameT *name1, SaNameT *name2) {
  68. if (name1->length == name2->length) {
  69. return ((strncmp (name1->value, name2->value, name1->length)) == 0);
  70. } else {
  71. return 0;
  72. }
  73. }
  74. struct saAmfComponent *findComponent (SaNameT *name)
  75. {
  76. struct list_head *AmfGroupList = 0;
  77. struct list_head *AmfUnitList = 0;
  78. struct list_head *AmfComponentList = 0;
  79. struct saAmfGroup *saAmfGroup = 0;
  80. struct saAmfUnit *AmfUnit = 0;
  81. struct saAmfComponent *AmfComponent = 0;
  82. int found = 0;
  83. /*
  84. * Search all groups
  85. */
  86. for (AmfGroupList = saAmfGroupHead.next;
  87. AmfGroupList != &saAmfGroupHead && found == 0;
  88. AmfGroupList = AmfGroupList->next) {
  89. saAmfGroup = list_entry (AmfGroupList,
  90. struct saAmfGroup, saAmfGroupList);
  91. /*
  92. * Search all units
  93. */
  94. for (AmfUnitList = saAmfGroup->saAmfUnitHead.next;
  95. AmfUnitList != &saAmfGroup->saAmfUnitHead && found == 0;
  96. AmfUnitList = AmfUnitList->next) {
  97. AmfUnit = list_entry (AmfUnitList,
  98. struct saAmfUnit, saAmfUnitList);
  99. /*
  100. * Search all components
  101. */
  102. for (AmfComponentList = AmfUnit->saAmfComponentHead.next;
  103. AmfComponentList != &AmfUnit->saAmfComponentHead && found == 0;
  104. AmfComponentList = AmfComponentList->next) {
  105. AmfComponent = list_entry (AmfComponentList,
  106. struct saAmfComponent, saAmfComponentList);
  107. if (SaNameTisNameT (name, &AmfComponent->name)) {
  108. found = 1;
  109. }
  110. }
  111. }
  112. }
  113. if (found) {
  114. return (AmfComponent);
  115. } else {
  116. return (0);
  117. }
  118. }
  119. char *
  120. strstr_rs (const char *haystack, const char *needle)
  121. {
  122. char *end_address;
  123. char *new_needle;
  124. new_needle = (char *)mempool_strdup (needle);
  125. new_needle[strlen(new_needle) - 1] = '\0';
  126. end_address = strstr (haystack, new_needle);
  127. if (end_address) {
  128. end_address += strlen (new_needle);
  129. end_address = strstr (end_address, needle + strlen (new_needle));
  130. }
  131. if (end_address) {
  132. end_address += 1; /* skip past { or = */
  133. do {
  134. if (*end_address == '\t' || *end_address == ' ') {
  135. end_address++;
  136. } else {
  137. break;
  138. }
  139. } while (*end_address != '\0');
  140. }
  141. mempool_free (new_needle);
  142. return (end_address);
  143. }
  144. static char error_string_response[256];
  145. int amfReadGroups (char **error_string)
  146. {
  147. char line[255];
  148. FILE *fp;
  149. SaParsingT current_parse = HEAD;
  150. int line_number = 0;
  151. char *loc;
  152. int i;
  153. struct saAmfGroup *saAmfGroup = 0;
  154. struct saAmfUnit *saAmfUnit = 0;
  155. struct saAmfProtectionGroup *saAmfProtectionGroup = 0;
  156. struct saAmfComponent *saAmfComponent = 0;
  157. struct list_head *findAmfUnitList = 0;
  158. struct list_head *findAmfComponentList = 0;
  159. struct saAmfUnit *findAmfUnit = 0;
  160. struct saAmfComponent *findAmfComponent = 0;
  161. SaNameT componentName;
  162. fp = fopen ("/etc/ais/groups.conf", "r");
  163. if (fp == 0) {
  164. *error_string = "ERROR: Could not open /etc/groups.conf file.\n";
  165. return (-1);
  166. }
  167. while (fgets (line, 255, fp)) {
  168. line_number += 1;
  169. line[strlen(line) - 1] = '\0';
  170. /*
  171. * Clear out white space and tabs
  172. */
  173. for (i = strlen (line) - 1; i > -1; i--) {
  174. if (line[i] == '\t' || line[i] == ' ') {
  175. line[i] = '\0';
  176. } else {
  177. break;
  178. }
  179. }
  180. /*
  181. * Clear out comments and empty lines
  182. */
  183. if (line[0] == '#' || line[0] == '\0') {
  184. continue;
  185. }
  186. switch (current_parse) {
  187. case HEAD:
  188. if (strstr_rs (line, "group{")) {
  189. saAmfGroup = (struct saAmfGroup *)mempool_malloc (sizeof (struct saAmfGroup));
  190. memset (saAmfGroup, 0, sizeof (struct saAmfGroup));
  191. list_init (&saAmfGroup->saAmfGroupList);
  192. list_init (&saAmfGroup->saAmfUnitHead);
  193. list_init (&saAmfGroup->saAmfProtectionGroupHead);
  194. list_add (&saAmfGroup->saAmfGroupList, &saAmfGroupHead);
  195. current_parse = GROUP;
  196. } else
  197. if (strcmp (line, "") == 0) {
  198. } else {
  199. goto parse_error;
  200. }
  201. break;
  202. case GROUP:
  203. if ((loc = strstr_rs (line, "name=")) != 0) {
  204. setSaNameT (&saAmfGroup->name, loc);
  205. } else
  206. if ((loc = strstr_rs (line, "model=")) != 0) {
  207. if (strcmp (loc, "2n") == 0) {
  208. saAmfGroup->model = GROUPCAPABILITYMODEL_2N;
  209. } else
  210. if (strcmp (loc, "nplusm") == 0) {
  211. saAmfGroup->model = GROUPCAPABILITYMODEL_NPLUSM;
  212. } else
  213. if (strcmp (loc, "nway") == 0) {
  214. printf ("nway redundancy model not supported.\n");
  215. goto parse_error;
  216. } else
  217. if (strcmp (loc, "nwayactive") == 0) {
  218. printf ("nway active redundancy model not supported.\n");
  219. goto parse_error;
  220. } else
  221. if (strcmp (loc, "noredundancy") == 0) {
  222. saAmfGroup->model = GROUPCAPABILITYMODEL_NOREDUNDANCY;
  223. } else {
  224. goto parse_error;
  225. }
  226. } else
  227. if ((loc = strstr_rs (line, "active-units=")) != 0) {
  228. saAmfGroup->saAmfActiveUnitsDesired = atoi (loc);
  229. } else
  230. if ((loc = strstr_rs (line, "backup-units=")) != 0) {
  231. saAmfGroup->saAmfStandbyUnitsDesired = atoi (loc);
  232. } else
  233. if (strstr_rs (line, "unit{")) {
  234. saAmfUnit = (struct saAmfUnit *)mempool_malloc (sizeof (struct saAmfUnit));
  235. memset (saAmfUnit, 0, sizeof (struct saAmfUnit));
  236. saAmfUnit->saAmfGroup = saAmfGroup;
  237. list_init (&saAmfUnit->saAmfComponentHead);
  238. list_add (&saAmfUnit->saAmfUnitList, &saAmfGroup->saAmfUnitHead);
  239. current_parse = UNIT;
  240. } else
  241. if (strstr_rs (line, "protection{")) {
  242. saAmfProtectionGroup = (struct saAmfProtectionGroup *)mempool_malloc (sizeof (struct saAmfProtectionGroup));
  243. memset (saAmfProtectionGroup, 0, sizeof (struct saAmfProtectionGroup));
  244. list_init (&saAmfProtectionGroup->saAmfMembersHead);
  245. list_init (&saAmfProtectionGroup->saAmfProtectionGroupList);
  246. list_add (&saAmfProtectionGroup->saAmfProtectionGroupList, &saAmfGroup->saAmfProtectionGroupHead);
  247. current_parse = PROTECTION;
  248. } else
  249. if (strstr_rs (line, "}")) {
  250. current_parse = HEAD;
  251. } else {
  252. goto parse_error;
  253. }
  254. break;
  255. case UNIT:
  256. if ((loc = strstr_rs (line, "name=")) != 0) {
  257. setSaNameT (&saAmfUnit->name, loc);
  258. } else
  259. if ((loc = strstr_rs (line, "component{")) != 0) {
  260. saAmfComponent = (struct saAmfComponent *)mempool_malloc (sizeof (struct saAmfComponent));
  261. memset (saAmfComponent, 0, sizeof (struct saAmfComponent));
  262. saAmfComponent->saAmfUnit = saAmfUnit;
  263. saAmfComponent->currentReadinessState = SA_AMF_OUT_OF_SERVICE;
  264. saAmfComponent->newReadinessState = SA_AMF_OUT_OF_SERVICE;
  265. saAmfComponent->currentHAState = SA_AMF_QUIESCED;
  266. saAmfComponent->newHAState = SA_AMF_QUIESCED;
  267. saAmfComponent->healthcheckInterval = 100;
  268. list_init (&saAmfComponent->saAmfComponentList);
  269. list_init (&saAmfComponent->saAmfProtectionGroupList);
  270. list_add (&saAmfComponent->saAmfComponentList, &saAmfUnit->saAmfComponentHead);
  271. current_parse = COMPONENT;
  272. } else
  273. if (strstr_rs (line, "}")) {
  274. current_parse = GROUP;
  275. } else {
  276. goto parse_error;
  277. }
  278. break;
  279. case COMPONENT:
  280. if ((loc = strstr_rs (line, "name=")) != 0) {
  281. setSaNameT (&saAmfComponent->name, loc);
  282. } else
  283. if ((loc = strstr_rs (line, "model=")) != 0) {
  284. if (strcmp (loc, "x_active_and_y_standby") == 0) {
  285. saAmfComponent->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_X_ACTIVE_AND_Y_STANDBY;
  286. } else
  287. if (strcmp (loc, "x_active_or_y_standby") == 0) {
  288. saAmfComponent->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_X_ACTIVE_OR_Y_STANDBY;
  289. } else
  290. if (strcmp (loc, "1_active_or_y_standby") == 0) {
  291. saAmfComponent->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_1_ACTIVE_OR_Y_STANDBY;
  292. } else
  293. if (strcmp (loc, "1_active_or_1_standby") == 0) {
  294. saAmfComponent->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_1_ACTIVE_OR_1_STANDBY;
  295. } else
  296. if (strcmp (loc, "x_active") == 0) {
  297. saAmfComponent->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_X_ACTIVE;
  298. } else
  299. if (strcmp (loc, "1_active") == 0) {
  300. saAmfComponent->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_1_ACTIVE;
  301. } else
  302. if (strcmp (loc, "no_active") == 0) {
  303. saAmfComponent->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_NO_ACTIVE;
  304. } else {
  305. goto parse_error;
  306. }
  307. } else
  308. if (strstr_rs (line, "}")) {
  309. current_parse = UNIT;
  310. } else {
  311. goto parse_error;
  312. }
  313. break;
  314. case PROTECTION:
  315. if ((loc = strstr_rs (line, "name=")) != 0) {
  316. setSaNameT (&saAmfProtectionGroup->name, loc);
  317. } else
  318. if ((loc = strstr_rs (line, "member=")) != 0) {
  319. for (findAmfUnitList = saAmfGroup->saAmfUnitHead.next;
  320. findAmfUnitList != &saAmfGroup->saAmfUnitHead;
  321. findAmfUnitList = findAmfUnitList->next) {
  322. findAmfUnit = list_entry (findAmfUnitList,
  323. struct saAmfUnit, saAmfUnitList);
  324. for (findAmfComponentList = findAmfUnit->saAmfComponentHead.next;
  325. findAmfComponentList != &findAmfUnit->saAmfComponentHead;
  326. findAmfComponentList = findAmfComponentList->next) {
  327. findAmfComponent = list_entry (findAmfComponentList,
  328. struct saAmfComponent, saAmfComponentList);
  329. if (SaNameTisEqual (&findAmfComponent->name, loc)) {
  330. list_add (&findAmfComponent->saAmfProtectionGroupList,
  331. &saAmfProtectionGroup->saAmfMembersHead);
  332. }
  333. }
  334. /*
  335. * Connect component to protection group
  336. */
  337. setSaNameT (&componentName, loc);
  338. saAmfComponent = findComponent (&componentName);
  339. saAmfComponent->saAmfProtectionGroup = saAmfProtectionGroup;
  340. }
  341. } else
  342. if (strstr_rs (line, "}")) {
  343. current_parse = GROUP;
  344. } else {
  345. goto parse_error;
  346. }
  347. break;
  348. default:
  349. printf ("Invalid state\n");
  350. goto parse_error;
  351. break;
  352. }
  353. }
  354. fclose (fp);
  355. return (0);
  356. parse_error:
  357. sprintf (error_string_response,
  358. "ERROR: parse error at /etc/groups.conf:%d\n", line_number);
  359. *error_string = error_string_response;
  360. fclose (fp);
  361. return (-1);
  362. }
  363. int amfReadNetwork (char **error_string,
  364. struct sockaddr_in *mcast_addr,
  365. struct sockaddr_in *bindnet_addr)
  366. {
  367. char line[255];
  368. FILE *fp;
  369. int res = 0;
  370. int line_number = 0;
  371. mcast_addr->sin_family = AF_INET;
  372. fp = fopen ("/etc/ais/network.conf", "r");
  373. while (fgets (line, 255, fp)) {
  374. line_number += 1;
  375. if (strncmp ("mcastaddr:", line, strlen ("mcastaddr:")) == 0) {
  376. res = inet_aton (&line[strlen("mcastaddr:")], &mcast_addr->sin_addr);
  377. } else
  378. if (strncmp ("bindnetaddr:", line, strlen ("bindnetaddr:")) == 0) {
  379. res = inet_aton (&line[strlen("bindnetaddr:")], &bindnet_addr->sin_addr);
  380. } else
  381. if (strncmp ("mcastport:", line, strlen ("mcastport:")) == 0) {
  382. res = mcast_addr->sin_port = atoi (&line[strlen("mcastport:")]);
  383. } else {
  384. res = 0;
  385. break;
  386. }
  387. if (res == 0) {
  388. sprintf (error_string_response,
  389. "ERROR: parse error at /etc/ais/network.conf:%d\n", line_number);
  390. *error_string = error_string_response;
  391. res = -1;
  392. break;
  393. }
  394. res = 0;
  395. }
  396. fclose (fp);
  397. return (res);
  398. }