This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.c
242 lines (199 loc) · 5.55 KB
/
protocol.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
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
/* Monkey HTTP Daemon
* ------------------
* Copyright (C) 2012, Sonny Karlsson
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <string.h> /* memcpy */
#include "dbg.h"
#include "protocol.h"
const char *fcgi_msg_type_str[] = {
[0] = "NULL MSG TYPE",
[FCGI_BEGIN_REQUEST] = "FCGI_BEGIN_REQUEST",
[FCGI_ABORT_REQUEST] = "FCGI_ABORT_REQUEST",
[FCGI_END_REQUEST] = "FCGI_END_REQUEST",
[FCGI_PARAMS] = "FCGI_PARAMS",
[FCGI_STDIN] = "FCGI_STDIN",
[FCGI_STDOUT] = "FCGI_STDOUT",
[FCGI_STDERR] = "FCGI_STDERR",
[FCGI_DATA] = "FCGI_DATA",
[FCGI_GET_VALUES] = "FCGI_GET_VALUES",
[FCGI_GET_VALUES_RESULT] = "FCGI_GET_VALUES_RESULT",
[FCGI_UNKNOWN_TYPE] = "FCGI_UNKNOWN_TYPE",
};
const char *fcgi_role_str[] = {
[0] = "NULL ROLE",
[FCGI_RESPONDER] = "FCGI_RESPONDER",
[FCGI_AUTHORIZER] = "FCGI_AUTHORIZER",
[FCGI_FILTER] = "FCGI_FILTER",
};
const char *fcgi_protocol_status_str[] = {
[FCGI_REQUEST_COMPLETE] = "FCGI_REQUEST_COMPLETE",
[FCGI_CANT_MPX_CONN] = "FCGI_CANT_MPX_CONN",
[FCGI_OVERLOADED] = "FCGI_OVERLOADED",
[FCGI_UNKNOWN_ROLE] = "FCGI_UNKNOWN_ROLE",
};
int fcgi_validate_struct_sizes(void)
{
struct fcgi_header header;
struct fcgi_begin_req_body begin_body;
struct fcgi_end_req_body end_body;
check(FCGI_HEADER_LEN == sizeof(header),
"sizeof(header) does not match FCGI_HEADER_LEN.");
check(FCGI_BEGIN_BODY_LEN == sizeof(begin_body),
"sizeof(begin_body) does not match FCGI_BEGIN_BODY_LEN.");
check(FCGI_END_BODY_LEN == sizeof(end_body),
"sizeof(end_body) does not match FCGI_END_BODY_LEN.");
return 0;
error:
return -1;
}
size_t fcgi_read_header(uint8_t *p, struct fcgi_header *h)
{
h->version = p[0];
h->type = p[1];
h->req_id = (p[2] << 8) + p[3];
h->body_len = (p[4] << 8) + p[5];
h->body_pad = p[6];
return sizeof(*h);
}
size_t fcgi_write_header(uint8_t *p, const struct fcgi_header *h)
{
p[0] = h->version;
p[1] = h->type;
p[2] = (h->req_id >> 8) & 0xff;
p[3] = (h->req_id) & 0xff;
p[4] = (h->body_len >> 8) & 0xff;
p[5] = (h->body_len) & 0xff;
p[6] = h->body_pad;
return sizeof(*h);
}
size_t fcgi_read_end_req_body(uint8_t *p, struct fcgi_end_req_body *b)
{
b->app_status = (p[0] << 24) + (p[1] << 16);
b->app_status += (p[2] << 8) + p[3];
b->protocol_status = p[4];
return sizeof(*b);
}
size_t fcgi_write_begin_req_body(uint8_t *p, const struct fcgi_begin_req_body *b)
{
p[0] = (b->role >> 8) & 0xff;
p[1] = (b->role) & 0xff;
p[2] = b->flags;
return sizeof(*b);
}
static uint32_t fcgi_param_read_length(uint8_t *p)
{
size_t len;
if (p[0] >> 7 == 1) {
len = (p[0] & 0x7f) << 24;
len += (p[1]) << 16;
len += (p[2]) << 8;
len += (p[3]);
} else {
len = p[0];
}
return len;
}
static size_t write_length(uint8_t *p, size_t len)
{
if (len > 127) {
p[0] = 1 << 7;
p[0] += (len >> 24) & 0x7f;
p[1] = (len >> 16) & 0xff;
p[2] = (len >> 8) & 0xff;
p[3] = (len) & 0xff;
return 4;
} else {
p[0] = len & 0x7f;
return 1;
}
}
size_t fcgi_param_write(uint8_t *p,
mk_pointer key,
mk_pointer value)
{
size_t ret, cnt;
if (!p) {
cnt = (key.len > 127 ? 4 : 1) + (value.len > 127 ? 4 : 1);
cnt += key.len + value.len;
return cnt;
}
cnt = 0;
ret = write_length(p + cnt, key.len);
cnt += ret;
ret = write_length(p + cnt, value.len);
cnt += ret;
memcpy(p + cnt, key.data, key.len);
cnt += key.len;
memcpy(p + cnt, value.data, value.len);
cnt += value.len;
return cnt;
}
int fcgi_param_entry_next(struct fcgi_param_entry *e)
{
e->position += e->key_len + e->value_len;
check_debug(e->position < e->base_len, "At end of buffer.");
e->key_len = fcgi_param_read_length(e->base + e->position);
e->position += e->key_len > 127 ? 4 : 1;
e->value_len = fcgi_param_read_length(e->base + e->position);
e->position += e->value_len > 127 ? 4 : 1;
return 0;
error:
return -1;
}
void fcgi_param_entry_init(struct fcgi_param_entry *e,
uint8_t *p,
size_t p_len)
{
e->key_len = 0;
e->value_len = 0;
e->position = 0;
e->base_len = p_len;
e->base = p;
fcgi_param_entry_next(e);
}
void fcgi_param_entry_reset(struct fcgi_param_entry *e)
{
e->key_len = 0;
e->value_len = 0;
e->position = 0;
fcgi_param_entry_next(e);
}
int fcgi_param_entry_search(struct fcgi_param_entry *e, mk_pointer key)
{
mk_pointer e_key;
do {
e_key = fcgi_param_entry_key(e);
if (e_key.len == key.len && !bcmp(e_key.data, key.data, key.len)) {
return 0;
}
} while (fcgi_param_entry_next(e) != -1);
return -1;
}
mk_pointer fcgi_param_entry_key(struct fcgi_param_entry *e)
{
return (mk_pointer){
.data = (char *)e->base + e->position,
.len = e->key_len,
};
}
mk_pointer fcgi_param_entry_value(struct fcgi_param_entry *e)
{
return (mk_pointer){
.data = (char *)e->base + e->position + e->key_len,
.len = e->value_len,
};
}