-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathprotocol.c
More file actions
300 lines (243 loc) · 8.5 KB
/
Copy pathprotocol.c
File metadata and controls
300 lines (243 loc) · 8.5 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* This file is part of mod-host.
*
* mod-host is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mod-host is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mod-host. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
************************************************************************************************************************
* INCLUDE FILES
************************************************************************************************************************
*/
#include <stdio.h>
#include <string.h>
#ifndef SKIP_READLINE
#include <unistd.h>
#endif
#include "protocol.h"
#include "utils.h"
/*
************************************************************************************************************************
* LOCAL DEFINES
************************************************************************************************************************
*/
#define NOT_FOUND (-1)
#define MANY_ARGUMENTS (-2)
#define FEW_ARGUMENTS (-3)
#define INVALID_ARGUMENT (-4)
/*
************************************************************************************************************************
* LOCAL CONSTANTS
************************************************************************************************************************
*/
const char *g_error_messages[] = {
MESSAGE_COMMAND_NOT_FOUND,
MESSAGE_MANY_ARGUMENTS,
MESSAGE_FEW_ARGUMENTS,
MESSAGE_INVALID_ARGUMENT
};
/*
************************************************************************************************************************
* LOCAL DATA TYPES
************************************************************************************************************************
*/
typedef struct CMD_T {
char* command;
char** list;
uint32_t count;
void (*callback)(proto_t *proto);
} cmd_t;
/*
************************************************************************************************************************
* LOCAL MACROS
************************************************************************************************************************
*/
/*
************************************************************************************************************************
* LOCAL GLOBAL VARIABLES
************************************************************************************************************************
*/
static int g_verbose = 0;
static unsigned int g_command_count = 0;
static cmd_t g_commands[PROTOCOL_MAX_COMMANDS];
/*
************************************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
************************************************************************************************************************
*/
/*
************************************************************************************************************************
* LOCAL CONFIGURATION ERRORS
************************************************************************************************************************
*/
/*
************************************************************************************************************************
* LOCAL FUNCTIONS
************************************************************************************************************************
*/
static int is_wildcard(const char *str)
{
if (str && strchr(str, '%') != NULL) return 1;
return 0;
}
/*
************************************************************************************************************************
* GLOBAL FUNCTIONS
************************************************************************************************************************
*/
void protocol_parse(msg_t *msg)
{
uint32_t i, j;
int32_t index;
proto_t proto;
proto.list = strarr_split(msg->data);
proto.list_count = strarr_length(proto.list);
proto.response = NULL;
if (g_verbose)
{
printf("PROTOCOL: received '%s'", msg->data);
for (i = 1; i < proto.list_count; i++)
printf(" '%s'", proto.list[i]);
printf("\n");
}
// TODO: check invalid argumets (wildcards)
if (proto.list_count == 0) return;
unsigned int match, variable_arguments = 0;
index = NOT_FOUND;
// loop all registered commands
for (i = 0; i < g_command_count; i++)
{
match = 0;
// checks received protocol
for (j = 0; j < proto.list_count && j < g_commands[i].count; j++)
{
if (strcmp(g_commands[i].list[j], proto.list[j]) == 0)
{
match++;
}
else if (match > 0)
{
if (is_wildcard(g_commands[i].list[j]))
{
match++;
}
else if (strcmp(g_commands[i].list[j], "...") == 0)
{
match++;
variable_arguments = 1;
}
}
}
if (match > 0)
{
// checks if the last argument is ...
if (j < g_commands[i].count)
{
if (strcmp(g_commands[i].list[j], "...") == 0) variable_arguments = 1;
}
// few arguments
if (proto.list_count < (g_commands[i].count - variable_arguments))
{
index = FEW_ARGUMENTS;
}
// many arguments
else if (proto.list_count > g_commands[i].count && !variable_arguments)
{
index = MANY_ARGUMENTS;
}
// arguments match
else if (match == proto.list_count || variable_arguments)
{
index = i;
}
// not found
else
{
index = NOT_FOUND;
}
break;
}
}
// Protocol OK
if (index >= 0)
{
if (g_commands[index].callback)
{
g_commands[index].callback(&proto);
if (proto.response)
{
#ifndef SKIP_READLINE
if (msg->sender_id == STDOUT_FILENO)
write(msg->sender_id, proto.response, proto.response_size+1);
else
#endif
socket_send(msg->sender_id, proto.response, proto.response_size+1);
if (g_verbose) printf("PROTOCOL: response '%s'\n", proto.response);
FREE(proto.response);
}
}
}
// Protocol error
else
{
#ifndef SKIP_READLINE
if (msg->sender_id == STDOUT_FILENO)
write(msg->sender_id, g_error_messages[-index-1], strlen(g_error_messages[-index-1])+1);
else
#endif
socket_send(msg->sender_id, g_error_messages[-index-1], strlen(g_error_messages[-index-1])+1);
if (g_verbose) printf("PROTOCOL: error '%s'\n", g_error_messages[-index-1]);
}
FREE(proto.list);
}
void protocol_add_command(const char *command, void (*callback)(proto_t *proto))
{
if (g_command_count >= PROTOCOL_MAX_COMMANDS)
{
printf("error: PROTOCOL_MAX_COMMANDS reached (reconfigure it)\n");
return;
}
char *cmd = str_duplicate(command);
g_commands[g_command_count].command = cmd;
g_commands[g_command_count].list = strarr_split(cmd);
g_commands[g_command_count].count = strarr_length(g_commands[g_command_count].list);
g_commands[g_command_count].callback = callback;
g_command_count++;
}
void protocol_response(const char *response, proto_t *proto)
{
proto->response_size = strlen(response);
proto->response = MALLOC(proto->response_size + 1);
strcpy(proto->response, response);
}
void protocol_response_int(int resp, proto_t *proto)
{
char buffer[32];
snprintf(buffer, 32, "resp %i", resp);
buffer[31] = '\0';
protocol_response(buffer, proto);
}
void protocol_remove_commands(void)
{
unsigned int i;
for (i = 0; i < g_command_count; i++)
{
FREE(g_commands[i].command);
FREE(g_commands[i].list);
}
}
void protocol_verbose(int verbose)
{
g_verbose = verbose;
}