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.

125 lines
2.8 KiB

  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_16bit(void *config, struct cfg_patch *patch, int code, const char *parameter)
  25. {
  26. int value = atoi(parameter);
  27. if (value < patch->min || value > patch->max)
  28. return -1;
  29. *((uint16_t *)(config + patch->offset)) = htons(value);
  30. return 0;
  31. }
  32. static int patch_string(void *config, struct cfg_patch *patch, int code, const char *parameter)
  33. {
  34. strncpy(config + patch->offset, parameter, patch->size);
  35. return 0;
  36. }
  37. static int patch_ip(void *config, struct cfg_patch *patch, int code, const char *parameter)
  38. {
  39. return (inet_pton(AF_INET, parameter, config + patch->offset) <= 0) ? -1 : 0;
  40. }
  41. static int patch_defaultgw(void *config, struct cfg_patch *patch, int code, const char *parameter)
  42. {
  43. /* metric? */
  44. *((uint8_t *)(config + 0x110d)) = 0x02;
  45. return patch_ip(config, patch, code, parameter);
  46. }
  47. static int patch_portname(void *config, struct cfg_patch *patch, int code, const char *parameter)
  48. {
  49. int num = code - patch->code;
  50. if (num < patch->min || num > patch->max)
  51. return -1;
  52. strncpy(config + patch->offset + (num -1) * 0x70, parameter, patch->size);
  53. return 0;
  54. }
  55. static struct cfg_patch patcharr[] = {{
  56. .code = CFG_LOCATION,
  57. .patch = patch_string,
  58. .offset = 0x0034,
  59. .size = 0x1d,
  60. }, {
  61. .code = CFG_SYSNAME,
  62. .patch = patch_string,
  63. .offset = 0x0054,
  64. .size = 0x1d,
  65. }, {
  66. .code = CFG_CONTACT,
  67. .patch = patch_string,
  68. .offset = 0x0074,
  69. .size = 0x1d,
  70. }, {
  71. .code = CFG_DEFAULTGW,
  72. .patch = patch_defaultgw,
  73. .offset = 0x10fc,
  74. }, {
  75. .code = CFG_IPADDRESS,
  76. .patch = patch_ip,
  77. .offset = 0x2df8,
  78. }, {
  79. .code = CFG_NETMASK,
  80. .patch = patch_8bit,
  81. .offset = 0x2dfe,
  82. .min = 0, .max = 32,
  83. }, {
  84. .code = CFG_NAMESERVER,
  85. .patch = patch_ip,
  86. .offset = 0x32dc,
  87. }, {
  88. .code = CFG_MACAGEING,
  89. .patch = patch_16bit,
  90. .offset = 0x53e8,
  91. .min = 0, .max = 65536,
  92. }, {
  93. .code = CFG_PORTNAME_MASK,
  94. .mask = 0xFFFFFFE0,
  95. .patch = patch_portname,
  96. .offset = 0x336c,
  97. .size = 9,
  98. .min = 1, .max = 26,
  99. }};
  100. int config_patch(void *config, int code, const char *parameter)
  101. {
  102. int i;
  103. for (i = 0; i < (sizeof(patcharr) / sizeof(struct cfg_patch)); i++) {
  104. int mask = (patcharr[i].mask != 0) ? patcharr[i].mask : ~0;
  105. if ((code & mask) == patcharr[i].code)
  106. return patcharr[i].patch(config, &patcharr[i], code, parameter);
  107. }
  108. return -1;
  109. }