-
Notifications
You must be signed in to change notification settings - Fork 0
/
param_cmds.c
155 lines (122 loc) · 3.81 KB
/
param_cmds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* \file param_cmds.c
* \brief Parameters management.
*
* Project: cybergod (Cyberspace administrator client program).
*
* Parameters management functions.
*
* \author Thomas Nemeth
*
* \version 1.0.0
* \date Sun, May 16 2010
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <cyberspace.h>
#include "0config.h"
#include "version.h"
#include "main.h"
enum {INT, CHR, STR};
typedef struct {
char * name;
unsigned char key;
int type;
char * desc;
} param_desc;
static param_desc parameter[] = {
{"max_clients", 'm', INT, "maximum clients allowed on the server"},
{"backup_delay", 'b', INT, "delay between backups/state-dumps"},
{"time_speed", 't', INT, "time acceleration"},
{"restrict_ip", 'r', STR, "IP address restriction for god/probes"},
{NULL, 0, 0, NULL}
};
static int show_parameters(void)
{
int i;
printf("Parameters list:\n");
for (i = 0 ; parameter[i].name ; i++)
{
printf("\t%s\t%s\n", parameter[i].name, parameter[i].desc);
}
return 1;
}
static int search_parameter(char * data, char ** value)
{
int i;
int found = -1;
char * param = strchr(data, ' ') + 1;
if (value)
{
*value = strchr(data, '=') + 1;
*value[-1] = 0;
}
for (i = 0 ; parameter[i].name ; i++)
{
if (strcmp(parameter[i].name, param) == 0)
{
found = i;
break;
}
}
return found;
}
int get_parameter(char * data)
{
int len;
int found;
unsigned char reply[MAX_PACKET_SIZE] = {0};
if (strchr(data, ' ') == NULL)
{
return show_parameters();
}
found = search_parameter(data, NULL);
exit_on(found < 0, 1, "Unknown parameter...\n");
cyberspace_transmit(cybersock, CMD_GET_PARAM, ¶meter[found].key, 1);
len = packet_read(cybersock, reply, MAX_PACKET_SIZE);
exit_on(len < 0, 0, "Reception error (%s)\n", get_error_info(len));
exit_on(len == 0, 0, "Reception error(read error)\n");
if (reply[2] != PACKET_MSG_ACK)
{
printf("Error receiving parameter information.\n");
return 1;
}
printf("%s=", parameter[found].name);
switch (parameter[found].type)
{
case INT: printf("%d\n", reply[3]); break;
case CHR: printf("%c\n", reply[3]); break;
case STR: printf("%s\n", &reply[3]); break;
}
return 1;
}
int set_parameter(char * data)
{
int len;
char * value;
int found;
unsigned char message[MAX_PACKET_SIZE] = {0};
if (strchr(data, ' ') == NULL)
{
return show_parameters();
}
exit_on(strchr(data, '=') == NULL, 1, "no new value given (set param=value)\n");
found = search_parameter(data, &value);
exit_on(found < 0, 1, "Unknown parameter...\n");
printf("Setting %s to %s\n", parameter[found].name, value);
len = 1;
message[0] = parameter[found].key;
switch (parameter[found].type)
{
case INT: message[1] = atoi(value); break;
case CHR: message[1] = value[0]; break;
case STR: len = snprintf((char *) &message[1], LEN_IPADDR, "%s", value);
break;
}
cyberspace_transmit(cybersock, CMD_SET_PARAM, message, len + 1);
check_reply("Error setting parameter...\n");
return 1;
}