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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* File: dpip.c
*
* Copyright 2005-2007 Jorge Arellano Cid <jcid@dillo.org>
*
* 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 3 of the License, or
* (at your option) any later version.
*
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h> /* for close */
#include <fcntl.h> /* for fcntl */
#include "dpip.h"
#include "d_size.h"
#define DPIP_TAG_END " '>"
#define DPIP_MODE_SWITCH_TAG "cmd='start_send_page' "
#define MSG_ERR(...) fprintf(stderr, "[dpip]: " __VA_ARGS__)
/*
* Local variables
*/
static const char Quote = '\'';
static const int DpipTag = 1;
/*
* Basically the syntax of a dpip tag is:
*
* "<"[*alpha] *(<name>"="Quote<escaped_value>Quote) " "Quote">"
*
* 1.- No space is allowed around the "=" sign between a name and its value.
* 2.- The Quote character is not allowed in <name>.
* 3.- Attribute values stuff Quote as QuoteQuote.
*
* e.g. (with ' as Quote):
*
* <a='b' b='c' '> OK
* <dpi a='b i' b='12' '> OK
* <a='>' '> OK
* <a='ain''t no doubt' '> OK
* <a='ain''t b=''no'' b='' doubt' '> OK
* <a = '>' '> Wrong
*
* Notes:
*
* Restriction #1 is for easy finding of end of tag (EOT=Space+Quote+>).
* Restriction #2 can be removed, but what for? ;)
* The functions here provide for this functionality.
*/
typedef enum {
SEEK_NAME,
MATCH_NAME,
SKIP_VALUE,
SKIP_QUOTE,
FOUND
} DpipTagParsingState;
/* ------------------------------------------------------------------------- */
/*
* Printf like function for building dpip commands.
* It takes care of dpip escaping of its arguments.
* NOTE : It ONLY accepts string parameters, and
* only one %s per parameter.
*/
char *a_Dpip_build_cmd(const char *format, ...)
{
va_list argp;
char *p, *q, *s;
Dstr *cmd;
/* Don't allow Quote characters in attribute names */
if (strchr(format, Quote))
return NULL;
cmd = dStr_sized_new(64);
dStr_append_c(cmd, '<');
va_start(argp, format);
for (p = q = (char*)format; *q; ) {
p = strstr(q, "%s");
if (!p) {
dStr_append(cmd, q);
break;
} else {
/* Copy format's part */
while (q != p)
dStr_append_c(cmd, *q++);
q += 2;
dStr_append_c(cmd, Quote);
/* Stuff-copy of argument */
s = va_arg (argp, char *);
for ( ; *s; ++s) {
dStr_append_c(cmd, *s);
if (*s == Quote)
dStr_append_c(cmd, *s);
}
dStr_append_c(cmd, Quote);
}
}
va_end(argp);
dStr_append_c(cmd, ' ');
dStr_append_c(cmd, Quote);
dStr_append_c(cmd, '>');
p = cmd->str;
dStr_free(cmd, FALSE);
return p;
}
/*
* Task: given a tag, its size and an attribute name, return the
* attribute value (stuffing of ' is removed here).
*
* Return value: the attribute value, or NULL if not present or malformed.
*/
char *a_Dpip_get_attr_l(char *tag, size_t tagsize, const char *attrname)
{
uint_t i, n = 0, found = 0;
char *p, *q, *start, *val = NULL;
DpipTagParsingState state = SEEK_NAME;
if (!tag || !tagsize || !attrname || !*attrname)
return NULL;
for (i = 1; i < tagsize && !found; ++i) {
switch (state) {
case SEEK_NAME:
if (tag[i] == attrname[0] && (tag[i-1] == ' ' || tag[i-1] == '<')) {
n = 1;
state = MATCH_NAME;
} else if (tag[i] == Quote && tag[i-1] == '=')
state = SKIP_VALUE;
break;
case MATCH_NAME:
if (tag[i] == attrname[n])
++n;
else if (tag[i] == '=' && !attrname[n])
state = FOUND;
else
state = SEEK_NAME;
break;
case SKIP_VALUE:
if (tag[i] == Quote)
state = (tag[i+1] == Quote) ? SKIP_QUOTE : SEEK_NAME;
break;
case SKIP_QUOTE:
state = SKIP_VALUE;
break;
case FOUND:
found = 1;
break;
}
}
if (found) {
p = start = tag + i;
while ((q = strchr(p, Quote)) && q[1] == Quote)
p = q + 2;
if (q && q[1] == ' ') {
val = dStrndup(start, (uint_t)(q - start));
for (p = q = val; (*q = *p); ++p, ++q)
if (*p == Quote && p[1] == p[0])
++p;
}
}
return val;
}
/*
* Task: given a tag and an attribute name, return its value.
* Return value: the attribute value, or NULL if not present or malformed.
*/
char *a_Dpip_get_attr(char *tag, const char *attrname)
{
return (tag ? a_Dpip_get_attr_l(tag, strlen(tag), attrname) : NULL);
}
/*
* Check whether the given 'auth' string equals what dpid saved.
* Return value: 1 if equal, -1 otherwise
*/
int a_Dpip_check_auth(const char *auth)
{
char SharedSecret[32];
FILE *In;
char *fname, *rcline = NULL, *tail;
int i, port, ret = -1;
dReturn_val_if (auth == NULL, -1);
fname = dStrconcat(dGethomedir(), "/.dillo/dpid_comm_keys", NULL);
if ((In = fopen(fname, "r")) == NULL) {
MSG_ERR("[a_Dpip_check_auth] %s\n", dStrerror(errno));
} else if ((rcline = dGetline(In)) == NULL) {
MSG_ERR("[a_Dpip_check_auth] empty file: %s\n", fname);
} else {
port = strtol(rcline, &tail, 10);
for (i = 0; *tail && isxdigit(tail[i+1]); ++i)
SharedSecret[i] = tail[i+1];
SharedSecret[i] = 0;
if (strcmp(auth, SharedSecret) == 0)
ret = 1;
}
dFree(rcline);
dFree(fname);
return ret;
}
/* --------------------------------------------------------------------------
* Dpip socket API ----------------------------------------------------------
*/
/*
* Create and initialize a dpip socket handler
*/
Dsh *a_Dpip_dsh_new(int fd_in, int fd_out, int flush_sz)
{
Dsh *dsh = dNew(Dsh, 1);
/* init descriptors and streams */
dsh->fd_in = fd_in;
dsh->fd_out = fd_out;
dsh->out = fdopen(fd_out, "w");
/* init buffer */
dsh->dbuf = dStr_sized_new(8 *1024);
dsh->rd_dbuf = dStr_sized_new(8 *1024);
dsh->flush_sz = flush_sz;
dsh->mode = DPIP_TAG;
if (fcntl(dsh->fd_in, F_GETFL) & O_NONBLOCK)
dsh->mode |= DPIP_NONBLOCK;
dsh->status = 0;
return dsh;
}
/*
* Streamed write to socket
* Return: 0 on success, 1 on error.
*/
int a_Dpip_dsh_write(Dsh *dsh, int flush, const char *Data, int DataSize)
{
int ret = 1;
/* append to buf */
dStr_append_l(dsh->dbuf, Data, DataSize);
/* flush data if necessary */
if (flush || dsh->dbuf->len >= dsh->flush_sz) {
if (dsh->dbuf->len &&
fwrite (dsh->dbuf->str, dsh->dbuf->len, 1, dsh->out) != 1) {
MSG_ERR("[a_Dpip_dsh_write] %s\n", dStrerror(errno));
} else {
fflush(dsh->out);
dStr_truncate(dsh->dbuf, 0);
ret = 0;
}
} else {
ret = 0;
}
return ret;
}
/*
* Convenience function.
*/
int a_Dpip_dsh_write_str(Dsh *dsh, int flush, const char *str)
{
return a_Dpip_dsh_write(dsh, flush, str, (int)strlen(str));
}
/*
* Read new data from the socket into our buffer.
* Used by both blocking and non-blocking IO.
*/
static void Dpip_dsh_read(Dsh *dsh)
{
//#define LBUF_SZ 16384
#define LBUF_SZ 1
ssize_t st;
int old_flags, blocking;
char buf[LBUF_SZ];
blocking = !(dsh->mode & DPIP_NONBLOCK);
if (blocking) {
old_flags = fcntl(dsh->fd_in, F_GETFL);
}
while (1) {
/* can't use fread() */
do
st = read(dsh->fd_in, buf, LBUF_SZ);
while (st < 0 && errno == EINTR);
if (st < 0) {
if (errno == EAGAIN) {
/* no problem, return what we've got so far... */
dsh->status = DPIP_EAGAIN;
} else {
MSG_ERR("[Dpip_dsh_read] %s\n", dStrerror(errno));
dsh->status = DPIP_ERROR;
}
break;
} else if (st == 0) {
dsh->status = DPIP_EOF;
break;
} else {
/* append to buf */
dStr_append_l(dsh->rd_dbuf, buf, st);
if (blocking) {
/* set NONBLOCKING temporarily... */
fcntl(dsh->fd_in, F_SETFL, O_NONBLOCK | old_flags);
}
}
}
if (blocking) {
/* restore blocking mode */
fcntl(dsh->fd_in, F_SETFL, old_flags);
}
}
/*
* Return a newlly allocated string with the next dpip token in the socket.
* Return value: token string on success, NULL otherwise
*/
char *a_Dpip_dsh_read_token(Dsh *dsh)
{
char *p, *ret = NULL;
/* switch mode upon request */
if (dsh->mode & DPIP_LAST_TAG)
dsh->mode = DPIP_RAW;
/* Only read from socket when there's no data in buffer or
* the tag is incomplete */
if (dsh->rd_dbuf->len == 0 ||
(dsh->mode & DPIP_TAG &&
!(p = strstr(dsh->rd_dbuf->str, DPIP_TAG_END)))) {
Dpip_dsh_read(dsh);
}
if (dsh->mode & DPIP_TAG) {
/* return a full tag */
if ((p = strstr(dsh->rd_dbuf->str, DPIP_TAG_END))) {
ret = dStrndup(dsh->rd_dbuf->str, p - dsh->rd_dbuf->str + 3);
dStr_erase(dsh->rd_dbuf, 0, p - dsh->rd_dbuf->str + 3);
if (strstr(ret, DPIP_MODE_SWITCH_TAG))
dsh->mode |= DPIP_LAST_TAG;
}
} else {
/* raw mode, return what we have "as is" */
if (dsh->rd_dbuf->len > 0) {
ret = dStrndup(dsh->rd_dbuf->str, dsh->rd_dbuf->len);
dStr_truncate(dsh->rd_dbuf, 0);
}
}
return ret;
}
/*
* Close this socket for reading and writing.
*/
void a_Dpip_dsh_close(Dsh *dsh)
{
fclose(dsh->out);
close(dsh->fd_out);
}
/*
* Free the SockHandler structure
*/
void a_Dpip_dsh_free(Dsh *dsh)
{
dStr_free(dsh->dbuf, 1);
dStr_free(dsh->rd_dbuf, 1);
dFree(dsh);
}
|