parse.c 13 KB

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