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.

settings.c 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <vsprintf.h>
  24. #include <gpxe/in.h>
  25. #include <gpxe/settings.h>
  26. /** @file
  27. *
  28. * Configuration settings
  29. *
  30. */
  31. /** Registered configuration setting types */
  32. static struct config_setting_type
  33. config_setting_types[0] __table_start ( config_setting_types );
  34. static struct config_setting_type
  35. config_setting_types_end[0] __table_end ( config_setting_types );
  36. /** Registered configuration settings */
  37. static struct config_setting
  38. config_settings[0] __table_start ( config_settings );
  39. static struct config_setting
  40. config_settings_end[0] __table_end ( config_settings );
  41. /**
  42. * Find configuration setting type
  43. *
  44. * @v name Name
  45. * @ret type Configuration setting type, or NULL
  46. */
  47. static struct config_setting_type *
  48. find_config_setting_type ( const char *name ) {
  49. struct config_setting_type *type;
  50. for ( type = config_setting_types ; type < config_setting_types_end ;
  51. type++ ) {
  52. if ( strcmp ( name, type->name ) == 0 )
  53. return type;
  54. }
  55. return NULL;
  56. }
  57. /**
  58. * Find configuration setting
  59. *
  60. * @v name Name
  61. * @ret setting Configuration setting, or NULL
  62. */
  63. static struct config_setting * find_config_setting ( const char *name ) {
  64. struct config_setting *setting;
  65. for ( setting = config_settings ; setting < config_settings_end ;
  66. setting++ ) {
  67. if ( strcmp ( name, setting->name ) == 0 )
  68. return setting;
  69. }
  70. return NULL;
  71. }
  72. /**
  73. * Find or build configuration setting
  74. *
  75. * @v name Name
  76. * @v tmp_setting Temporary buffer for constructing a setting
  77. * @ret setting Configuration setting, or NULL
  78. *
  79. * Find setting if it exists. If it doesn't exist, but the name is of
  80. * the form "<num>.<type>" (e.g. "12.string"), then construct a
  81. * setting for that tag and data type, and return it. The constructed
  82. * setting will be placed in the temporary buffer.
  83. */
  84. static struct config_setting *
  85. find_or_build_config_setting ( const char *name,
  86. struct config_setting *tmp_setting ) {
  87. struct config_setting *setting;
  88. char *separator;
  89. /* Look in the list of registered settings first */
  90. setting = find_config_setting ( name );
  91. if ( setting )
  92. return setting;
  93. /* If name is of the form "<num>.<type>", try to construct a setting */
  94. setting = tmp_setting;
  95. memset ( setting, 0, sizeof ( *setting ) );
  96. setting->name = name;
  97. setting->tag = strtoul ( name, &separator, 10 );
  98. if ( *separator != '.' )
  99. return NULL;
  100. setting->type = find_config_setting_type ( separator + 1 );
  101. if ( ! setting->type )
  102. return NULL;
  103. return setting;
  104. }
  105. /** Show value of setting
  106. *
  107. * @v context Configuration context
  108. * @v name Configuration setting name
  109. * @v buf Buffer to contain value
  110. * @v len Length of buffer
  111. * @ret rc Return status code
  112. */
  113. int ( show_setting ) ( struct config_context *context, const char *name,
  114. char *buf, size_t len ) {
  115. struct config_setting *setting;
  116. struct config_setting tmp_setting;
  117. setting = find_or_build_config_setting ( name, &tmp_setting );
  118. if ( ! setting )
  119. return -ENOENT;
  120. return setting->type->show ( context, setting, buf, len );
  121. }
  122. /** Set value of setting
  123. *
  124. * @v context Configuration context
  125. * @v name Configuration setting name
  126. * @v value Setting value (as a string)
  127. * @ret rc Return status code
  128. */
  129. int ( set_setting ) ( struct config_context *context, const char *name,
  130. const char *value ) {
  131. struct config_setting *setting;
  132. struct config_setting tmp_setting;
  133. setting = find_or_build_config_setting ( name, &tmp_setting );
  134. if ( ! setting )
  135. return -ENOENT;
  136. return setting->type->set ( context, setting, value );
  137. }
  138. /**
  139. * Show value of string setting
  140. *
  141. * @v context Configuration context
  142. * @v setting Configuration setting
  143. * @v buf Buffer to contain value
  144. * @v len Length of buffer
  145. * @ret rc Return status code
  146. */
  147. static int show_string ( struct config_context *context,
  148. struct config_setting *setting,
  149. char *buf, size_t len ) {
  150. struct dhcp_option *option;
  151. option = find_dhcp_option ( context->options, setting->tag );
  152. if ( ! option )
  153. return -ENOENT;
  154. dhcp_snprintf ( buf, len, option );
  155. return 0;
  156. }
  157. /** Set value of string setting
  158. *
  159. * @v context Configuration context
  160. * @v setting Configuration setting
  161. * @v value Setting value (as a string)
  162. * @ret rc Return status code
  163. */
  164. static int set_string ( struct config_context *context,
  165. struct config_setting *setting,
  166. const char *value ) {
  167. struct dhcp_option *option;
  168. option = set_dhcp_option ( context->options, setting->tag,
  169. value, strlen ( value ) );
  170. if ( ! option )
  171. return -ENOMEM;
  172. return 0;
  173. }
  174. /** A string configuration setting */
  175. struct config_setting_type config_setting_type_string __config_setting_type = {
  176. .name = "string",
  177. .show = show_string,
  178. .set = set_string,
  179. };
  180. /**
  181. * Show value of IPv4 setting
  182. *
  183. * @v context Configuration context
  184. * @v setting Configuration setting
  185. * @v buf Buffer to contain value
  186. * @v len Length of buffer
  187. * @ret rc Return status code
  188. */
  189. static int show_ipv4 ( struct config_context *context,
  190. struct config_setting *setting,
  191. char *buf, size_t len ) {
  192. struct dhcp_option *option;
  193. struct in_addr ipv4;
  194. option = find_dhcp_option ( context->options, setting->tag );
  195. if ( ! option )
  196. return -ENOENT;
  197. dhcp_ipv4_option ( option, &ipv4 );
  198. snprintf ( buf, len, inet_ntoa ( ipv4 ) );
  199. return 0;
  200. }
  201. /** Set value of IPV4 setting
  202. *
  203. * @v context Configuration context
  204. * @v setting Configuration setting
  205. * @v value Setting value (as a string)
  206. * @ret rc Return status code
  207. */
  208. static int set_ipv4 ( struct config_context *context,
  209. struct config_setting *setting,
  210. const char *value ) {
  211. struct dhcp_option *option;
  212. struct in_addr ipv4;
  213. int rc;
  214. if ( ( rc = inet_aton ( value, &ipv4 ) ) != 0 )
  215. return rc;
  216. option = set_dhcp_option ( context->options, setting->tag,
  217. &ipv4, sizeof ( ipv4 ) );
  218. if ( ! option )
  219. return -ENOMEM;
  220. return 0;
  221. }
  222. /** An IPv4 configuration setting */
  223. struct config_setting_type config_setting_type_ipv4 __config_setting_type = {
  224. .name = "ipv4",
  225. .show = show_ipv4,
  226. .set = set_ipv4,
  227. };
  228. /** Some basic setting definitions */
  229. struct config_setting basic_config_settings[] __config_setting = {
  230. {
  231. .name = "hostname",
  232. .tag = DHCP_HOST_NAME,
  233. .type = &config_setting_type_string,
  234. },
  235. {
  236. .name = "ip",
  237. .tag = DHCP_EB_YIADDR,
  238. .type = &config_setting_type_ipv4,
  239. },
  240. };
  241. /* Quick and dirty proof of concept */
  242. int cmdl_show ( int argc, char **argv ) {
  243. char buf[256];
  244. struct config_context dummy_context = { NULL };
  245. int rc;
  246. if ( argc < 2 )
  247. return -EINVAL;
  248. if ( ( rc = show_setting ( &dummy_context, argv[1],
  249. buf, sizeof ( buf ) ) ) != 0 )
  250. return rc;
  251. printf ( "%s = %s\n", argv[1], buf );
  252. return 0;
  253. }