-
Notifications
You must be signed in to change notification settings - Fork 514
/
duk_trans_socket_unix.c
364 lines (309 loc) · 8.68 KB
/
duk_trans_socket_unix.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
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
/*
* Example debug transport using a Linux/Unix TCP socket
*
* Provides a TCP server socket which a debug client can connect to.
* After that data is just passed through.
*
* On some UNIX systems poll() may not be available but select() is.
* The default is to use poll(), but you can switch to select() by
* defining USE_SELECT. See https://daniel.haxx.se/docs/poll-vs-select.html.
*/
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#if !defined(USE_SELECT)
#include <poll.h>
#endif /* !USE_SELECT */
#include <errno.h>
#include "duktape.h"
#include "duk_trans_socket.h"
#if !defined(DUK_DEBUG_PORT)
#define DUK_DEBUG_PORT 9091
#endif
#if 0
#define DEBUG_PRINTS
#endif
static int server_sock = -1;
static int client_sock = -1;
/*
* Transport init and finish
*/
void duk_trans_socket_init(void) {
struct sockaddr_in addr;
int on;
server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0) {
fprintf(stderr, "%s: failed to create server socket: %s\n",
__FILE__, strerror(errno));
fflush(stderr);
goto fail;
}
on = 1;
if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(on)) < 0) {
fprintf(stderr, "%s: failed to set SO_REUSEADDR for server socket: %s\n",
__FILE__, strerror(errno));
fflush(stderr);
goto fail;
}
memset((void *) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(DUK_DEBUG_PORT);
if (bind(server_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
fprintf(stderr, "%s: failed to bind server socket: %s\n",
__FILE__, strerror(errno));
fflush(stderr);
goto fail;
}
listen(server_sock, 1 /*backlog*/);
return;
fail:
if (server_sock >= 0) {
(void) close(server_sock);
server_sock = -1;
}
}
void duk_trans_socket_finish(void) {
if (client_sock >= 0) {
(void) close(client_sock);
client_sock = -1;
}
if (server_sock >= 0) {
(void) close(server_sock);
server_sock = -1;
}
}
void duk_trans_socket_waitconn(void) {
struct sockaddr_in addr;
socklen_t sz;
if (server_sock < 0) {
fprintf(stderr, "%s: no server socket, skip waiting for connection\n",
__FILE__);
fflush(stderr);
return;
}
if (client_sock >= 0) {
(void) close(client_sock);
client_sock = -1;
}
fprintf(stderr, "Waiting for debug connection on port %d\n", (int) DUK_DEBUG_PORT);
fflush(stderr);
sz = (socklen_t) sizeof(addr);
client_sock = accept(server_sock, (struct sockaddr *) &addr, &sz);
if (client_sock < 0) {
fprintf(stderr, "%s: accept() failed, skip waiting for connection: %s\n",
__FILE__, strerror(errno));
fflush(stderr);
goto fail;
}
fprintf(stderr, "Debug connection established\n");
fflush(stderr);
/* XXX: For now, close the listen socket because we won't accept new
* connections anyway. A better implementation would allow multiple
* debug attaches.
*/
if (server_sock >= 0) {
(void) close(server_sock);
server_sock = -1;
}
return;
fail:
if (client_sock >= 0) {
(void) close(client_sock);
client_sock = -1;
}
}
/*
* Duktape callbacks
*/
/* Duktape debug transport callback: (possibly partial) read. */
duk_size_t duk_trans_socket_read_cb(void *udata, char *buffer, duk_size_t length) {
ssize_t ret;
(void) udata; /* not needed by the example */
#if defined(DEBUG_PRINTS)
fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n",
__func__, (void *) udata, (void *) buffer, (long) length);
fflush(stderr);
#endif
if (client_sock < 0) {
return 0;
}
if (length == 0) {
/* This shouldn't happen. */
fprintf(stderr, "%s: read request length == 0, closing connection\n",
__FILE__);
fflush(stderr);
goto fail;
}
if (buffer == NULL) {
/* This shouldn't happen. */
fprintf(stderr, "%s: read request buffer == NULL, closing connection\n",
__FILE__);
fflush(stderr);
goto fail;
}
/* In a production quality implementation there would be a sanity
* timeout here to recover from "black hole" disconnects.
*/
ret = read(client_sock, (void *) buffer, (size_t) length);
if (ret < 0) {
fprintf(stderr, "%s: debug read failed, closing connection: %s\n",
__FILE__, strerror(errno));
fflush(stderr);
goto fail;
} else if (ret == 0) {
fprintf(stderr, "%s: debug read failed, ret == 0 (EOF), closing connection\n",
__FILE__);
fflush(stderr);
goto fail;
} else if (ret > (ssize_t) length) {
fprintf(stderr, "%s: debug read failed, ret too large (%ld > %ld), closing connection\n",
__FILE__, (long) ret, (long) length);
fflush(stderr);
goto fail;
}
return (duk_size_t) ret;
fail:
if (client_sock >= 0) {
(void) close(client_sock);
client_sock = -1;
}
return 0;
}
/* Duktape debug transport callback: (possibly partial) write. */
duk_size_t duk_trans_socket_write_cb(void *udata, const char *buffer, duk_size_t length) {
ssize_t ret;
(void) udata; /* not needed by the example */
#if defined(DEBUG_PRINTS)
fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n",
__func__, (void *) udata, (const void *) buffer, (long) length);
fflush(stderr);
#endif
if (client_sock < 0) {
return 0;
}
if (length == 0) {
/* This shouldn't happen. */
fprintf(stderr, "%s: write request length == 0, closing connection\n",
__FILE__);
fflush(stderr);
goto fail;
}
if (buffer == NULL) {
/* This shouldn't happen. */
fprintf(stderr, "%s: write request buffer == NULL, closing connection\n",
__FILE__);
fflush(stderr);
goto fail;
}
/* In a production quality implementation there would be a sanity
* timeout here to recover from "black hole" disconnects.
*/
ret = write(client_sock, (const void *) buffer, (size_t) length);
if (ret <= 0 || ret > (ssize_t) length) {
fprintf(stderr, "%s: debug write failed, closing connection: %s\n",
__FILE__, strerror(errno));
fflush(stderr);
goto fail;
}
return (duk_size_t) ret;
fail:
if (client_sock >= 0) {
(void) close(client_sock);
client_sock = -1;
}
return 0;
}
duk_size_t duk_trans_socket_peek_cb(void *udata) {
#if defined(USE_SELECT)
struct timeval tm;
fd_set rfds;
int select_rc;
#else
struct pollfd fds[1];
int poll_rc;
#endif
(void) udata; /* not needed by the example */
#if defined(DEBUG_PRINTS)
fprintf(stderr, "%s: udata=%p\n", __func__, (void *) udata);
fflush(stderr);
#endif
if (client_sock < 0) {
return 0;
}
#if defined(USE_SELECT)
FD_ZERO(&rfds);
FD_SET(client_sock, &rfds);
tm.tv_sec = tm.tv_usec = 0;
select_rc = select(client_sock + 1, &rfds, NULL, NULL, &tm);
if (select_rc == 0) {
return 0;
} else if (select_rc == 1) {
return 1;
}
goto fail;
#else /* USE_SELECT */
fds[0].fd = client_sock;
fds[0].events = POLLIN;
fds[0].revents = 0;
poll_rc = poll(fds, 1, 0);
if (poll_rc < 0) {
fprintf(stderr, "%s: poll returned < 0, closing connection: %s\n",
__FILE__, strerror(errno));
fflush(stderr);
goto fail; /* also returns 0, which is correct */
} else if (poll_rc > 1) {
fprintf(stderr, "%s: poll returned > 1, treating like 1\n",
__FILE__);
fflush(stderr);
return 1; /* should never happen */
} else if (poll_rc == 0) {
return 0; /* nothing to read */
} else {
return 1; /* something to read */
}
#endif /* USE_SELECT */
fail:
if (client_sock >= 0) {
(void) close(client_sock);
client_sock = -1;
}
return 0;
}
void duk_trans_socket_read_flush_cb(void *udata) {
(void) udata; /* not needed by the example */
#if defined(DEBUG_PRINTS)
fprintf(stderr, "%s: udata=%p\n", __func__, (void *) udata);
fflush(stderr);
#endif
/* Read flush: Duktape may not be making any more read calls at this
* time. If the transport maintains a receive window, it can use a
* read flush as a signal to update the window status to the remote
* peer. A read flush is guaranteed to occur before Duktape stops
* reading for a while; it may occur in other situations as well so
* it's not a 100% reliable indication.
*/
/* This TCP transport requires no read flush handling so ignore.
* You can also pass a NULL to duk_debugger_attach() and not
* implement this callback at all.
*/
}
void duk_trans_socket_write_flush_cb(void *udata) {
(void) udata; /* not needed by the example */
#if defined(DEBUG_PRINTS)
fprintf(stderr, "%s: udata=%p\n", __func__, (void *) udata);
fflush(stderr);
#endif
/* Write flush. If the transport combines multiple writes
* before actually sending, a write flush is an indication
* to write out any pending bytes: Duktape may not be doing
* any more writes on this occasion.
*/
/* This TCP transport requires no write flush handling so ignore.
* You can also pass a NULL to duk_debugger_attach() and not
* implement this callback at all.
*/
return;
}