4
0

parse.c 13 KB

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