Linux HLSW LAN Master
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
5.9 KiB

  1. /***************************************************************************
  2. * Copyright (C) 06/2006 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #include <netinet/ip.h>
  27. #include <arpa/inet.h>
  28. #include "configfile.h"
  29. #include "list.h"
  30. #include "logging.h"
  31. #define BUFSIZE 1024
  32. struct conf_section {
  33. struct list_head list;
  34. struct list_head tupel_list;
  35. char *name;
  36. };
  37. struct conf_tupel {
  38. struct list_head list;
  39. char *option;
  40. char *parameter;
  41. };
  42. static LIST_HEAD(config_list);
  43. static struct conf_section * config_add_section(const char *name)
  44. {
  45. struct conf_section *section;
  46. section = malloc(sizeof(struct conf_section) + strlen(name));
  47. if (section == NULL)
  48. return NULL;
  49. INIT_LIST_HEAD(&section->list);
  50. INIT_LIST_HEAD(&section->tupel_list);
  51. section->name = strdup(name);
  52. if (section->name == NULL) {
  53. free(section);
  54. return NULL;
  55. }
  56. list_add_tail(&section->list, &config_list);
  57. return section;
  58. }
  59. static int config_add_tupel(struct conf_section *section, const char *option, const char *parameter)
  60. {
  61. struct conf_tupel *tupel = malloc(sizeof(struct conf_tupel));
  62. if (tupel == NULL)
  63. return -1;
  64. INIT_LIST_HEAD(&tupel->list);
  65. tupel->option = strdup(option);
  66. tupel->parameter = strdup(parameter);
  67. if (tupel->option == NULL || tupel->parameter == NULL) {
  68. free(tupel);
  69. return -1;
  70. }
  71. list_add_tail(&tupel->list, &section->tupel_list);
  72. return 0;
  73. }
  74. int config_parse(const char *config)
  75. {
  76. FILE *fz = fopen(config, "r");
  77. if (fz == NULL) {
  78. log_print(LOG_ERROR, "config_parse(): %s", config);
  79. return -1;
  80. }
  81. char *line = malloc(BUFSIZE);
  82. if (line == NULL) {
  83. log_print(LOG_ERROR, "config_parse(): out of memory");
  84. fclose(fz);
  85. return -1;
  86. }
  87. int linenum = 0;
  88. struct conf_section *section = NULL;
  89. while (fgets(line, BUFSIZE, fz) != NULL) {
  90. linenum++;
  91. if (line[0] == '#' || line[0] <= ' ') {
  92. continue;
  93. } else if (line[0] == '[') {
  94. char *tok = strtok(line +1, " ]\n");
  95. if (tok == NULL || (section = config_add_section(tok)) == NULL) {
  96. log_print(LOG_WARN, "config_parse(): invalid section in row %d", linenum);
  97. free(line);
  98. fclose(fz);
  99. return -1;
  100. }
  101. continue;
  102. } else if (section == NULL) {
  103. log_print(LOG_WARN, "config_parse(): missing section in row %d", linenum);
  104. free(line);
  105. fclose(fz);
  106. return -1;
  107. }
  108. char *tok = strtok(line, " \n");
  109. if (tok != NULL) {
  110. char *tok2;
  111. while ((tok2 = strtok(NULL, " \n"))) {
  112. if (config_add_tupel(section, tok, tok2) != 0)
  113. log_print(LOG_WARN, "config_parse(): invalid row %d", linenum);
  114. }
  115. }
  116. }
  117. fclose(fz);
  118. free(line);
  119. return 0;
  120. }
  121. static struct conf_section * config_get_section(const char *name)
  122. {
  123. struct conf_section *section;
  124. list_for_each_entry(section, &config_list, list) {
  125. if (!strcmp(section->name, name))
  126. return section;
  127. }
  128. return NULL;
  129. }
  130. char * config_get_string(const char *section_str, const char *option, char *def)
  131. {
  132. struct conf_section *section = config_get_section(section_str);
  133. if (section != NULL) {
  134. struct conf_tupel *tupel;
  135. list_for_each_entry(tupel, &section->tupel_list, list) {
  136. if (!strcmp(tupel->option, option))
  137. return tupel->parameter;
  138. }
  139. }
  140. if (def != NULL)
  141. log_print(LOG_WARN, "config [%s:%s] not found, using default: '%s'",
  142. section_str, option, def);
  143. return def;
  144. }
  145. int config_get_int(const char *section, const char *option, int def)
  146. {
  147. char *ret = config_get_string(section, option, NULL);
  148. if (ret == NULL) {
  149. log_print(LOG_WARN, "config [%s:%s] not found, using default: '%d'",
  150. section, option, def);
  151. return def;
  152. }
  153. return atoi(ret);
  154. }
  155. int config_get_strings(const char *section_str, const char *option,
  156. int (*callback)(const char *value, void *privdata),
  157. void *privdata)
  158. {
  159. struct conf_section *section = config_get_section(section_str);
  160. if (section == NULL)
  161. return -1;
  162. int cnt = 0;
  163. struct conf_tupel *tupel;
  164. list_for_each_entry(tupel, &section->tupel_list, list) {
  165. if (!strcmp(tupel->option, option))
  166. if (callback(tupel->parameter, privdata) == 0)
  167. cnt++;
  168. }
  169. return cnt;
  170. }
  171. // TODO: wrong place!
  172. int parse_saddr(const char *addr, struct sockaddr_in *sa)
  173. {
  174. char *addr_cpy = strdup(addr);
  175. if (addr_cpy == NULL) {
  176. log_print(LOG_WARN, "parse_saddr(): out of memory");
  177. return -1;
  178. }
  179. char *tmp;
  180. char *ip = strtok_r(addr_cpy, ":", &tmp);
  181. if (ip == NULL) {
  182. free(addr_cpy);
  183. return -1;
  184. }
  185. char *port = strtok_r(NULL, ":", &tmp);
  186. if (port == NULL) {
  187. free(addr_cpy);
  188. return -1;
  189. }
  190. sa->sin_family = AF_INET;
  191. sa->sin_port = htons(atoi(port));
  192. int ret = inet_aton(ip, &sa->sin_addr);
  193. free(addr_cpy);
  194. return (ret != 0) ? 0 : -1;
  195. }