ES-2024 reverter
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.

87 lines
1.8KB

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <arpa/inet.h>
  6. #include "configdata.h"
  7. struct cfg_patch {
  8. int code;
  9. int mask;
  10. int (* patch)(void *config, struct cfg_patch *patch, int code, const char *parameter);
  11. int offset;
  12. int size;
  13. int min;
  14. int max;
  15. };
  16. static int patch_8bit(void *config, struct cfg_patch *patch, int code, const char *parameter)
  17. {
  18. int value = atoi(parameter);
  19. if (value < patch->min || value > patch->max)
  20. return -1;
  21. *((uint8_t *)(config + patch->offset)) = value;
  22. return 0;
  23. }
  24. static int patch_string(void *config, struct cfg_patch *patch, int code, const char *parameter)
  25. {
  26. strncpy(config + patch->offset, parameter, patch->size);
  27. return 0;
  28. }
  29. static int patch_ip(void *config, struct cfg_patch *patch, int code, const char *parameter)
  30. {
  31. return (inet_pton(AF_INET, parameter, config + patch->offset) <= 0) ? -1 : 0;
  32. }
  33. static struct cfg_patch patcharr[] = {{
  34. .code = CFG_LOCATION,
  35. .patch = patch_string,
  36. .offset = 0x0034,
  37. .size = 0x1d,
  38. }, {
  39. .code = CFG_SYSNAME,
  40. .patch = patch_string,
  41. .offset = 0x0054,
  42. .size = 0x1d,
  43. }, {
  44. .code = CFG_CONTACT,
  45. .patch = patch_string,
  46. .offset = 0x0074,
  47. .size = 0x1d,
  48. }, {
  49. .code = CFG_DEFAULTGW,
  50. .patch = patch_ip,
  51. .offset = 0x10fc,
  52. }, {
  53. .code = CFG_IPADDRESS,
  54. .patch = patch_ip,
  55. .offset = 0x2df8,
  56. }, {
  57. .code = CFG_NETMASK,
  58. .patch = patch_8bit,
  59. .offset = 0x2dfe,
  60. .min = 0x00,
  61. .max = 0x20,
  62. }, {
  63. .code = CFG_NAMESERVER,
  64. .patch = patch_ip,
  65. .offset = 0x32dc,
  66. }};
  67. int config_patch(void *config, int code, const char *parameter)
  68. {
  69. int i;
  70. for (i = 0; i < (sizeof(patcharr) / sizeof(struct cfg_patch)); i++) {
  71. int mask = (patcharr[i].mask != 0) ? patcharr[i].mask : ~0;
  72. if ((patcharr[i].code & mask) == code)
  73. return patcharr[i].patch(config, &patcharr[i], code, parameter);
  74. }
  75. return -1;
  76. }