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