rbuv 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +0 -1
- data/README.md +6 -1
- data/Rakefile +42 -0
- data/deps/libuv/.gitignore +34 -0
- data/deps/libuv/.mailmap +16 -0
- data/deps/libuv/AUTHORS +81 -0
- data/deps/libuv/ChangeLog +45 -0
- data/deps/libuv/LICENSE +41 -0
- data/deps/libuv/Makefile +53 -0
- data/deps/libuv/README.md +118 -0
- data/deps/libuv/build.mk +164 -0
- data/deps/libuv/checksparse.sh +230 -0
- data/deps/libuv/common.gypi +197 -0
- data/deps/libuv/config-mingw.mk +48 -0
- data/deps/libuv/config-unix.mk +167 -0
- data/deps/libuv/gyp_uv +98 -0
- data/deps/libuv/include/uv-private/ngx-queue.h +129 -0
- data/deps/libuv/include/uv-private/stdint-msvc2008.h +247 -0
- data/deps/libuv/include/uv-private/tree.h +768 -0
- data/deps/libuv/include/uv-private/uv-bsd.h +34 -0
- data/deps/libuv/include/uv-private/uv-darwin.h +61 -0
- data/deps/libuv/include/uv-private/uv-linux.h +34 -0
- data/deps/libuv/include/uv-private/uv-sunos.h +44 -0
- data/deps/libuv/include/uv-private/uv-unix.h +332 -0
- data/deps/libuv/include/uv-private/uv-win.h +585 -0
- data/deps/libuv/include/uv.h +1987 -0
- data/deps/libuv/src/fs-poll.c +248 -0
- data/deps/libuv/src/inet.c +298 -0
- data/deps/libuv/src/unix/aix.c +393 -0
- data/deps/libuv/src/unix/async.c +281 -0
- data/deps/libuv/src/unix/core.c +714 -0
- data/deps/libuv/src/unix/cygwin.c +93 -0
- data/deps/libuv/src/unix/darwin-proctitle.m +78 -0
- data/deps/libuv/src/unix/darwin.c +431 -0
- data/deps/libuv/src/unix/dl.c +83 -0
- data/deps/libuv/src/unix/error.c +109 -0
- data/deps/libuv/src/unix/freebsd.c +343 -0
- data/deps/libuv/src/unix/fs.c +869 -0
- data/deps/libuv/src/unix/fsevents.c +299 -0
- data/deps/libuv/src/unix/getaddrinfo.c +159 -0
- data/deps/libuv/src/unix/internal.h +259 -0
- data/deps/libuv/src/unix/kqueue.c +347 -0
- data/deps/libuv/src/unix/linux-core.c +724 -0
- data/deps/libuv/src/unix/linux-inotify.c +236 -0
- data/deps/libuv/src/unix/linux-syscalls.c +388 -0
- data/deps/libuv/src/unix/linux-syscalls.h +150 -0
- data/deps/libuv/src/unix/loop-watcher.c +64 -0
- data/deps/libuv/src/unix/loop.c +114 -0
- data/deps/libuv/src/unix/netbsd.c +353 -0
- data/deps/libuv/src/unix/openbsd.c +304 -0
- data/deps/libuv/src/unix/pipe.c +261 -0
- data/deps/libuv/src/unix/poll.c +108 -0
- data/deps/libuv/src/unix/process.c +501 -0
- data/deps/libuv/src/unix/proctitle.c +103 -0
- data/deps/libuv/src/unix/signal.c +455 -0
- data/deps/libuv/src/unix/stream.c +1380 -0
- data/deps/libuv/src/unix/sunos.c +647 -0
- data/deps/libuv/src/unix/tcp.c +357 -0
- data/deps/libuv/src/unix/thread.c +431 -0
- data/deps/libuv/src/unix/threadpool.c +286 -0
- data/deps/libuv/src/unix/timer.c +153 -0
- data/deps/libuv/src/unix/tty.c +179 -0
- data/deps/libuv/src/unix/udp.c +715 -0
- data/deps/libuv/src/uv-common.c +431 -0
- data/deps/libuv/src/uv-common.h +204 -0
- data/deps/libuv/src/version.c +60 -0
- data/deps/libuv/src/win/async.c +99 -0
- data/deps/libuv/src/win/atomicops-inl.h +56 -0
- data/deps/libuv/src/win/core.c +310 -0
- data/deps/libuv/src/win/dl.c +86 -0
- data/deps/libuv/src/win/error.c +164 -0
- data/deps/libuv/src/win/fs-event.c +506 -0
- data/deps/libuv/src/win/fs.c +1951 -0
- data/deps/libuv/src/win/getaddrinfo.c +365 -0
- data/deps/libuv/src/win/handle-inl.h +164 -0
- data/deps/libuv/src/win/handle.c +153 -0
- data/deps/libuv/src/win/internal.h +346 -0
- data/deps/libuv/src/win/loop-watcher.c +124 -0
- data/deps/libuv/src/win/pipe.c +1656 -0
- data/deps/libuv/src/win/poll.c +615 -0
- data/deps/libuv/src/win/process-stdio.c +503 -0
- data/deps/libuv/src/win/process.c +1048 -0
- data/deps/libuv/src/win/req-inl.h +224 -0
- data/deps/libuv/src/win/req.c +25 -0
- data/deps/libuv/src/win/signal.c +354 -0
- data/deps/libuv/src/win/stream-inl.h +67 -0
- data/deps/libuv/src/win/stream.c +198 -0
- data/deps/libuv/src/win/tcp.c +1422 -0
- data/deps/libuv/src/win/thread.c +666 -0
- data/deps/libuv/src/win/threadpool.c +82 -0
- data/deps/libuv/src/win/timer.c +230 -0
- data/deps/libuv/src/win/tty.c +1857 -0
- data/deps/libuv/src/win/udp.c +744 -0
- data/deps/libuv/src/win/util.c +946 -0
- data/deps/libuv/src/win/winapi.c +152 -0
- data/deps/libuv/src/win/winapi.h +4476 -0
- data/deps/libuv/src/win/winsock.c +560 -0
- data/deps/libuv/src/win/winsock.h +171 -0
- data/deps/libuv/test/benchmark-async-pummel.c +119 -0
- data/deps/libuv/test/benchmark-async.c +139 -0
- data/deps/libuv/test/benchmark-fs-stat.c +136 -0
- data/deps/libuv/test/benchmark-getaddrinfo.c +91 -0
- data/deps/libuv/test/benchmark-list.h +163 -0
- data/deps/libuv/test/benchmark-loop-count.c +90 -0
- data/deps/libuv/test/benchmark-million-async.c +112 -0
- data/deps/libuv/test/benchmark-million-timers.c +77 -0
- data/deps/libuv/test/benchmark-multi-accept.c +432 -0
- data/deps/libuv/test/benchmark-ping-pongs.c +212 -0
- data/deps/libuv/test/benchmark-pound.c +325 -0
- data/deps/libuv/test/benchmark-pump.c +459 -0
- data/deps/libuv/test/benchmark-sizes.c +45 -0
- data/deps/libuv/test/benchmark-spawn.c +163 -0
- data/deps/libuv/test/benchmark-tcp-write-batch.c +141 -0
- data/deps/libuv/test/benchmark-thread.c +64 -0
- data/deps/libuv/test/benchmark-udp-pummel.c +238 -0
- data/deps/libuv/test/blackhole-server.c +118 -0
- data/deps/libuv/test/dns-server.c +329 -0
- data/deps/libuv/test/echo-server.c +384 -0
- data/deps/libuv/test/fixtures/empty_file +0 -0
- data/deps/libuv/test/fixtures/load_error.node +1 -0
- data/deps/libuv/test/run-benchmarks.c +64 -0
- data/deps/libuv/test/run-tests.c +159 -0
- data/deps/libuv/test/runner-unix.c +328 -0
- data/deps/libuv/test/runner-unix.h +36 -0
- data/deps/libuv/test/runner-win.c +318 -0
- data/deps/libuv/test/runner-win.h +43 -0
- data/deps/libuv/test/runner.c +394 -0
- data/deps/libuv/test/runner.h +165 -0
- data/deps/libuv/test/task.h +122 -0
- data/deps/libuv/test/test-active.c +83 -0
- data/deps/libuv/test/test-async.c +136 -0
- data/deps/libuv/test/test-barrier.c +98 -0
- data/deps/libuv/test/test-callback-order.c +77 -0
- data/deps/libuv/test/test-callback-stack.c +204 -0
- data/deps/libuv/test/test-condvar.c +173 -0
- data/deps/libuv/test/test-connection-fail.c +150 -0
- data/deps/libuv/test/test-cwd-and-chdir.c +64 -0
- data/deps/libuv/test/test-delayed-accept.c +189 -0
- data/deps/libuv/test/test-dlerror.c +58 -0
- data/deps/libuv/test/test-embed.c +136 -0
- data/deps/libuv/test/test-error.c +59 -0
- data/deps/libuv/test/test-fail-always.c +29 -0
- data/deps/libuv/test/test-fs-event.c +504 -0
- data/deps/libuv/test/test-fs-poll.c +148 -0
- data/deps/libuv/test/test-fs.c +1899 -0
- data/deps/libuv/test/test-get-currentexe.c +63 -0
- data/deps/libuv/test/test-get-loadavg.c +36 -0
- data/deps/libuv/test/test-get-memory.c +38 -0
- data/deps/libuv/test/test-getaddrinfo.c +120 -0
- data/deps/libuv/test/test-getsockname.c +344 -0
- data/deps/libuv/test/test-hrtime.c +54 -0
- data/deps/libuv/test/test-idle.c +82 -0
- data/deps/libuv/test/test-ipc-send-recv.c +218 -0
- data/deps/libuv/test/test-ipc.c +625 -0
- data/deps/libuv/test/test-list.h +492 -0
- data/deps/libuv/test/test-loop-handles.c +337 -0
- data/deps/libuv/test/test-loop-stop.c +73 -0
- data/deps/libuv/test/test-multiple-listen.c +103 -0
- data/deps/libuv/test/test-mutexes.c +63 -0
- data/deps/libuv/test/test-pass-always.c +28 -0
- data/deps/libuv/test/test-ping-pong.c +250 -0
- data/deps/libuv/test/test-pipe-bind-error.c +144 -0
- data/deps/libuv/test/test-pipe-connect-error.c +98 -0
- data/deps/libuv/test/test-platform-output.c +87 -0
- data/deps/libuv/test/test-poll-close.c +73 -0
- data/deps/libuv/test/test-poll.c +575 -0
- data/deps/libuv/test/test-process-title.c +49 -0
- data/deps/libuv/test/test-ref.c +415 -0
- data/deps/libuv/test/test-run-nowait.c +46 -0
- data/deps/libuv/test/test-run-once.c +49 -0
- data/deps/libuv/test/test-semaphore.c +111 -0
- data/deps/libuv/test/test-shutdown-close.c +105 -0
- data/deps/libuv/test/test-shutdown-eof.c +184 -0
- data/deps/libuv/test/test-signal-multiple-loops.c +270 -0
- data/deps/libuv/test/test-signal.c +152 -0
- data/deps/libuv/test/test-spawn.c +938 -0
- data/deps/libuv/test/test-stdio-over-pipes.c +250 -0
- data/deps/libuv/test/test-tcp-bind-error.c +198 -0
- data/deps/libuv/test/test-tcp-bind6-error.c +159 -0
- data/deps/libuv/test/test-tcp-close-while-connecting.c +81 -0
- data/deps/libuv/test/test-tcp-close.c +130 -0
- data/deps/libuv/test/test-tcp-connect-error-after-write.c +96 -0
- data/deps/libuv/test/test-tcp-connect-error.c +71 -0
- data/deps/libuv/test/test-tcp-connect-timeout.c +86 -0
- data/deps/libuv/test/test-tcp-connect6-error.c +69 -0
- data/deps/libuv/test/test-tcp-flags.c +52 -0
- data/deps/libuv/test/test-tcp-open.c +175 -0
- data/deps/libuv/test/test-tcp-read-stop.c +73 -0
- data/deps/libuv/test/test-tcp-shutdown-after-write.c +132 -0
- data/deps/libuv/test/test-tcp-unexpected-read.c +114 -0
- data/deps/libuv/test/test-tcp-write-to-half-open-connection.c +136 -0
- data/deps/libuv/test/test-tcp-writealot.c +171 -0
- data/deps/libuv/test/test-thread.c +183 -0
- data/deps/libuv/test/test-threadpool-cancel.c +311 -0
- data/deps/libuv/test/test-threadpool.c +77 -0
- data/deps/libuv/test/test-timer-again.c +142 -0
- data/deps/libuv/test/test-timer.c +266 -0
- data/deps/libuv/test/test-tty.c +111 -0
- data/deps/libuv/test/test-udp-dgram-too-big.c +87 -0
- data/deps/libuv/test/test-udp-ipv6.c +158 -0
- data/deps/libuv/test/test-udp-multicast-join.c +140 -0
- data/deps/libuv/test/test-udp-multicast-ttl.c +87 -0
- data/deps/libuv/test/test-udp-open.c +154 -0
- data/deps/libuv/test/test-udp-options.c +87 -0
- data/deps/libuv/test/test-udp-send-and-recv.c +210 -0
- data/deps/libuv/test/test-util.c +97 -0
- data/deps/libuv/test/test-walk-handles.c +78 -0
- data/deps/libuv/uv.gyp +431 -0
- data/deps/libuv/vcbuild.bat +128 -0
- data/ext/rbuv/debug.h +27 -0
- data/ext/rbuv/error.c +7 -0
- data/ext/rbuv/error.h +10 -0
- data/ext/rbuv/extconf.rb +35 -0
- data/ext/rbuv/handle.c +40 -0
- data/ext/rbuv/handle.h +14 -0
- data/ext/rbuv/libuv.mk +12 -0
- data/ext/rbuv/loop.c +50 -0
- data/ext/rbuv/loop.h +13 -0
- data/ext/rbuv/rbuv.c +15 -0
- data/ext/rbuv/rbuv.h +27 -0
- data/ext/rbuv/timer.c +133 -0
- data/ext/rbuv/timer.h +13 -0
- data/lib/rbuv/timer.rb +7 -0
- data/lib/rbuv/version.rb +1 -1
- data/lib/rbuv.rb +24 -2
- data/rbuv.gemspec +5 -1
- data/spec/spec_helper.rb +22 -0
- data/spec/timer_spec.rb +144 -0
- metadata +278 -9
@@ -0,0 +1,329 @@
|
|
1
|
+
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
2
|
+
*
|
3
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
* of this software and associated documentation files (the "Software"), to
|
5
|
+
* deal in the Software without restriction, including without limitation the
|
6
|
+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
* sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
* furnished to do so, subject to the following conditions:
|
9
|
+
*
|
10
|
+
* The above copyright notice and this permission notice shall be included in
|
11
|
+
* all copies or substantial portions of the Software.
|
12
|
+
*
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
18
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
19
|
+
* IN THE SOFTWARE.
|
20
|
+
*/
|
21
|
+
|
22
|
+
#include "uv.h"
|
23
|
+
#include "task.h"
|
24
|
+
#include <stdio.h>
|
25
|
+
#include <stdlib.h>
|
26
|
+
#include <string.h>
|
27
|
+
|
28
|
+
|
29
|
+
typedef struct {
|
30
|
+
uv_write_t req;
|
31
|
+
uv_buf_t buf;
|
32
|
+
} write_req_t;
|
33
|
+
|
34
|
+
|
35
|
+
/* used to track multiple DNS requests received */
|
36
|
+
typedef struct {
|
37
|
+
char* prevbuf_ptr;
|
38
|
+
int prevbuf_pos;
|
39
|
+
int prevbuf_rem;
|
40
|
+
} dnsstate;
|
41
|
+
|
42
|
+
|
43
|
+
/* modify handle to append dnsstate */
|
44
|
+
typedef struct {
|
45
|
+
uv_tcp_t handle;
|
46
|
+
dnsstate state;
|
47
|
+
} dnshandle;
|
48
|
+
|
49
|
+
|
50
|
+
static uv_loop_t* loop;
|
51
|
+
|
52
|
+
|
53
|
+
static uv_tcp_t server;
|
54
|
+
|
55
|
+
|
56
|
+
static void after_write(uv_write_t* req, int status);
|
57
|
+
static void after_read(uv_stream_t*, ssize_t nread, uv_buf_t buf);
|
58
|
+
static void on_close(uv_handle_t* peer);
|
59
|
+
static void on_connection(uv_stream_t*, int status);
|
60
|
+
|
61
|
+
#define WRITE_BUF_LEN (64*1024)
|
62
|
+
#define DNSREC_LEN (4)
|
63
|
+
|
64
|
+
#define LEN_OFFSET 0
|
65
|
+
#define QUERYID_OFFSET 2
|
66
|
+
|
67
|
+
static unsigned char DNSRsp[] = {
|
68
|
+
0, 43, 0, 0, 0x81, 0x80, 0, 1, 0, 1, 0, 0, 0, 0
|
69
|
+
};
|
70
|
+
|
71
|
+
static unsigned char qrecord[] = {
|
72
|
+
5, 'e', 'c', 'h', 'o', 's', 3, 's', 'r', 'v', 0, 0, 1, 0, 1
|
73
|
+
};
|
74
|
+
|
75
|
+
static unsigned char arecord[] = {
|
76
|
+
0xc0, 0x0c, 0, 1, 0, 1, 0, 0, 5, 0xbd, 0, 4, 10, 0, 1, 1
|
77
|
+
};
|
78
|
+
|
79
|
+
|
80
|
+
static void after_write(uv_write_t* req, int status) {
|
81
|
+
write_req_t* wr;
|
82
|
+
|
83
|
+
if (status) {
|
84
|
+
uv_err_t err = uv_last_error(loop);
|
85
|
+
fprintf(stderr, "uv_write error: %s\n", uv_strerror(err));
|
86
|
+
ASSERT(0);
|
87
|
+
}
|
88
|
+
|
89
|
+
wr = (write_req_t*) req;
|
90
|
+
|
91
|
+
/* Free the read/write buffer and the request */
|
92
|
+
free(wr->buf.base);
|
93
|
+
free(wr);
|
94
|
+
}
|
95
|
+
|
96
|
+
|
97
|
+
static void after_shutdown(uv_shutdown_t* req, int status) {
|
98
|
+
uv_close((uv_handle_t*) req->handle, on_close);
|
99
|
+
free(req);
|
100
|
+
}
|
101
|
+
|
102
|
+
|
103
|
+
static void addrsp(write_req_t* wr, char* hdr) {
|
104
|
+
char * dnsrsp;
|
105
|
+
short int rsplen;
|
106
|
+
short int* reclen;
|
107
|
+
|
108
|
+
rsplen = sizeof(DNSRsp) + sizeof(qrecord) + sizeof(arecord);
|
109
|
+
|
110
|
+
ASSERT (rsplen + wr->buf.len < WRITE_BUF_LEN);
|
111
|
+
|
112
|
+
dnsrsp = wr->buf.base + wr->buf.len;
|
113
|
+
|
114
|
+
/* copy stock response */
|
115
|
+
memcpy(dnsrsp, DNSRsp, sizeof(DNSRsp));
|
116
|
+
memcpy(dnsrsp + sizeof(DNSRsp), qrecord, sizeof(qrecord));
|
117
|
+
memcpy(dnsrsp + sizeof(DNSRsp) + sizeof(qrecord), arecord, sizeof(arecord));
|
118
|
+
|
119
|
+
/* overwrite with network order length and id from request header */
|
120
|
+
reclen = (short int*)dnsrsp;
|
121
|
+
*reclen = htons(rsplen-2);
|
122
|
+
dnsrsp[QUERYID_OFFSET] = hdr[QUERYID_OFFSET];
|
123
|
+
dnsrsp[QUERYID_OFFSET+1] = hdr[QUERYID_OFFSET+1];
|
124
|
+
|
125
|
+
wr->buf.len += rsplen;
|
126
|
+
}
|
127
|
+
|
128
|
+
static void process_req(uv_stream_t* handle, ssize_t nread, uv_buf_t buf) {
|
129
|
+
write_req_t* wr;
|
130
|
+
dnshandle* dns = (dnshandle*)handle;
|
131
|
+
char hdrbuf[DNSREC_LEN];
|
132
|
+
int hdrbuf_remaining = DNSREC_LEN;
|
133
|
+
int rec_remaining = 0;
|
134
|
+
int readbuf_remaining;
|
135
|
+
char* dnsreq;
|
136
|
+
char* hdrstart;
|
137
|
+
int usingprev = 0;
|
138
|
+
|
139
|
+
wr = (write_req_t*) malloc(sizeof *wr);
|
140
|
+
wr->buf.base = (char*)malloc(WRITE_BUF_LEN);
|
141
|
+
wr->buf.len = 0;
|
142
|
+
|
143
|
+
if (dns->state.prevbuf_ptr != NULL) {
|
144
|
+
dnsreq = dns->state.prevbuf_ptr + dns->state.prevbuf_pos;
|
145
|
+
readbuf_remaining = dns->state.prevbuf_rem;
|
146
|
+
usingprev = 1;
|
147
|
+
} else {
|
148
|
+
dnsreq = buf.base;
|
149
|
+
readbuf_remaining = nread;
|
150
|
+
}
|
151
|
+
hdrstart = dnsreq;
|
152
|
+
|
153
|
+
while (dnsreq != NULL) {
|
154
|
+
/* something to process */
|
155
|
+
while (readbuf_remaining > 0) {
|
156
|
+
/* something to process in current buffer */
|
157
|
+
if (hdrbuf_remaining > 0) {
|
158
|
+
/* process len and id */
|
159
|
+
if (readbuf_remaining < hdrbuf_remaining) {
|
160
|
+
/* too little to get request header. save for next buffer */
|
161
|
+
memcpy(&hdrbuf[DNSREC_LEN - hdrbuf_remaining], dnsreq, readbuf_remaining);
|
162
|
+
hdrbuf_remaining = DNSREC_LEN - readbuf_remaining;
|
163
|
+
break;
|
164
|
+
} else {
|
165
|
+
/* save header */
|
166
|
+
memcpy(&hdrbuf[DNSREC_LEN - hdrbuf_remaining], dnsreq, hdrbuf_remaining);
|
167
|
+
dnsreq += hdrbuf_remaining;
|
168
|
+
readbuf_remaining -= hdrbuf_remaining;
|
169
|
+
hdrbuf_remaining = 0;
|
170
|
+
|
171
|
+
/* get record length */
|
172
|
+
rec_remaining = (unsigned) hdrbuf[0] * 256 + (unsigned) hdrbuf[1];
|
173
|
+
rec_remaining -= (DNSREC_LEN - 2);
|
174
|
+
}
|
175
|
+
}
|
176
|
+
|
177
|
+
if (rec_remaining <= readbuf_remaining) {
|
178
|
+
/* prepare reply */
|
179
|
+
addrsp(wr, hdrbuf);
|
180
|
+
|
181
|
+
/* move to next record */
|
182
|
+
dnsreq += rec_remaining;
|
183
|
+
hdrstart = dnsreq;
|
184
|
+
readbuf_remaining -= rec_remaining;
|
185
|
+
rec_remaining = 0;
|
186
|
+
hdrbuf_remaining = DNSREC_LEN;
|
187
|
+
} else {
|
188
|
+
/* otherwise this buffer is done. */
|
189
|
+
rec_remaining -= readbuf_remaining;
|
190
|
+
break;
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
/* if we had to use bytes from prev buffer, start processing the current one */
|
195
|
+
if (usingprev == 1) {
|
196
|
+
/* free previous buffer */
|
197
|
+
free(dns->state.prevbuf_ptr);
|
198
|
+
dnsreq = buf.base;
|
199
|
+
readbuf_remaining = nread;
|
200
|
+
usingprev = 0;
|
201
|
+
} else {
|
202
|
+
dnsreq = NULL;
|
203
|
+
}
|
204
|
+
}
|
205
|
+
|
206
|
+
/* send write buffer */
|
207
|
+
if (wr->buf.len > 0) {
|
208
|
+
if (uv_write((uv_write_t*) &wr->req, handle, &wr->buf, 1, after_write)) {
|
209
|
+
FATAL("uv_write failed");
|
210
|
+
}
|
211
|
+
}
|
212
|
+
|
213
|
+
if (readbuf_remaining > 0) {
|
214
|
+
/* save start of record position, so we can continue on next read */
|
215
|
+
dns->state.prevbuf_ptr = buf.base;
|
216
|
+
dns->state.prevbuf_pos = hdrstart - buf.base;
|
217
|
+
dns->state.prevbuf_rem = nread - dns->state.prevbuf_pos;
|
218
|
+
} else {
|
219
|
+
/* nothing left in this buffer */
|
220
|
+
dns->state.prevbuf_ptr = NULL;
|
221
|
+
dns->state.prevbuf_pos = 0;
|
222
|
+
dns->state.prevbuf_rem = 0;
|
223
|
+
free(buf.base);
|
224
|
+
}
|
225
|
+
}
|
226
|
+
|
227
|
+
static void after_read(uv_stream_t* handle, ssize_t nread, uv_buf_t buf) {
|
228
|
+
uv_shutdown_t* req;
|
229
|
+
|
230
|
+
if (nread < 0) {
|
231
|
+
/* Error or EOF */
|
232
|
+
ASSERT (uv_last_error(loop).code == UV_EOF);
|
233
|
+
|
234
|
+
if (buf.base) {
|
235
|
+
free(buf.base);
|
236
|
+
}
|
237
|
+
|
238
|
+
req = malloc(sizeof *req);
|
239
|
+
uv_shutdown(req, handle, after_shutdown);
|
240
|
+
|
241
|
+
return;
|
242
|
+
}
|
243
|
+
|
244
|
+
if (nread == 0) {
|
245
|
+
/* Everything OK, but nothing read. */
|
246
|
+
free(buf.base);
|
247
|
+
return;
|
248
|
+
}
|
249
|
+
/* process requests and send responses */
|
250
|
+
process_req(handle, nread, buf);
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
static void on_close(uv_handle_t* peer) {
|
255
|
+
free(peer);
|
256
|
+
}
|
257
|
+
|
258
|
+
|
259
|
+
static uv_buf_t buf_alloc(uv_handle_t* handle, size_t suggested_size) {
|
260
|
+
uv_buf_t buf;
|
261
|
+
buf.base = (char*) malloc(suggested_size);
|
262
|
+
buf.len = suggested_size;
|
263
|
+
return buf;
|
264
|
+
}
|
265
|
+
|
266
|
+
|
267
|
+
static void on_connection(uv_stream_t* server, int status) {
|
268
|
+
dnshandle* handle;
|
269
|
+
int r;
|
270
|
+
|
271
|
+
ASSERT(status == 0);
|
272
|
+
|
273
|
+
handle = (dnshandle*) malloc(sizeof *handle);
|
274
|
+
ASSERT(handle != NULL);
|
275
|
+
|
276
|
+
/* initialize read buffer state */
|
277
|
+
handle->state.prevbuf_ptr = 0;
|
278
|
+
handle->state.prevbuf_pos = 0;
|
279
|
+
handle->state.prevbuf_rem = 0;
|
280
|
+
|
281
|
+
r = uv_tcp_init(loop, (uv_tcp_t*)handle);
|
282
|
+
ASSERT(r == 0);
|
283
|
+
|
284
|
+
r = uv_accept(server, (uv_stream_t*)handle);
|
285
|
+
ASSERT(r == 0);
|
286
|
+
|
287
|
+
r = uv_read_start((uv_stream_t*)handle, buf_alloc, after_read);
|
288
|
+
ASSERT(r == 0);
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
static int dns_start(int port) {
|
293
|
+
struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port);
|
294
|
+
int r;
|
295
|
+
|
296
|
+
r = uv_tcp_init(loop, &server);
|
297
|
+
if (r) {
|
298
|
+
/* TODO: Error codes */
|
299
|
+
fprintf(stderr, "Socket creation error\n");
|
300
|
+
return 1;
|
301
|
+
}
|
302
|
+
|
303
|
+
r = uv_tcp_bind(&server, addr);
|
304
|
+
if (r) {
|
305
|
+
/* TODO: Error codes */
|
306
|
+
fprintf(stderr, "Bind error\n");
|
307
|
+
return 1;
|
308
|
+
}
|
309
|
+
|
310
|
+
r = uv_listen((uv_stream_t*)&server, 128, on_connection);
|
311
|
+
if (r) {
|
312
|
+
/* TODO: Error codes */
|
313
|
+
fprintf(stderr, "Listen error\n");
|
314
|
+
return 1;
|
315
|
+
}
|
316
|
+
|
317
|
+
return 0;
|
318
|
+
}
|
319
|
+
|
320
|
+
|
321
|
+
HELPER_IMPL(dns_server) {
|
322
|
+
loop = uv_default_loop();
|
323
|
+
|
324
|
+
if (dns_start(TEST_PORT_2))
|
325
|
+
return 1;
|
326
|
+
|
327
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
328
|
+
return 0;
|
329
|
+
}
|
@@ -0,0 +1,384 @@
|
|
1
|
+
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
2
|
+
*
|
3
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
* of this software and associated documentation files (the "Software"), to
|
5
|
+
* deal in the Software without restriction, including without limitation the
|
6
|
+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
* sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
* furnished to do so, subject to the following conditions:
|
9
|
+
*
|
10
|
+
* The above copyright notice and this permission notice shall be included in
|
11
|
+
* all copies or substantial portions of the Software.
|
12
|
+
*
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
18
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
19
|
+
* IN THE SOFTWARE.
|
20
|
+
*/
|
21
|
+
|
22
|
+
#include "uv.h"
|
23
|
+
#include "task.h"
|
24
|
+
#include <stdio.h>
|
25
|
+
#include <stdlib.h>
|
26
|
+
|
27
|
+
typedef struct {
|
28
|
+
uv_write_t req;
|
29
|
+
uv_buf_t buf;
|
30
|
+
} write_req_t;
|
31
|
+
|
32
|
+
static uv_loop_t* loop;
|
33
|
+
|
34
|
+
static int server_closed;
|
35
|
+
static stream_type serverType;
|
36
|
+
static uv_tcp_t tcpServer;
|
37
|
+
static uv_udp_t udpServer;
|
38
|
+
static uv_pipe_t pipeServer;
|
39
|
+
static uv_handle_t* server;
|
40
|
+
|
41
|
+
static void after_write(uv_write_t* req, int status);
|
42
|
+
static void after_read(uv_stream_t*, ssize_t nread, uv_buf_t buf);
|
43
|
+
static void on_close(uv_handle_t* peer);
|
44
|
+
static void on_server_close(uv_handle_t* handle);
|
45
|
+
static void on_connection(uv_stream_t*, int status);
|
46
|
+
|
47
|
+
|
48
|
+
static void after_write(uv_write_t* req, int status) {
|
49
|
+
write_req_t* wr;
|
50
|
+
uv_err_t err;
|
51
|
+
|
52
|
+
/* Free the read/write buffer and the request */
|
53
|
+
wr = (write_req_t*) req;
|
54
|
+
free(wr->buf.base);
|
55
|
+
free(wr);
|
56
|
+
|
57
|
+
if (status == 0)
|
58
|
+
return;
|
59
|
+
|
60
|
+
err = uv_last_error(loop);
|
61
|
+
fprintf(stderr, "uv_write error: %s\n", uv_strerror(err));
|
62
|
+
|
63
|
+
if (err.code == UV_ECANCELED)
|
64
|
+
return;
|
65
|
+
|
66
|
+
ASSERT(err.code == UV_EPIPE);
|
67
|
+
uv_close((uv_handle_t*)req->handle, on_close);
|
68
|
+
}
|
69
|
+
|
70
|
+
|
71
|
+
static void after_shutdown(uv_shutdown_t* req, int status) {
|
72
|
+
uv_close((uv_handle_t*)req->handle, on_close);
|
73
|
+
free(req);
|
74
|
+
}
|
75
|
+
|
76
|
+
|
77
|
+
static void after_read(uv_stream_t* handle, ssize_t nread, uv_buf_t buf) {
|
78
|
+
int i;
|
79
|
+
write_req_t *wr;
|
80
|
+
uv_shutdown_t* req;
|
81
|
+
|
82
|
+
if (nread < 0) {
|
83
|
+
/* Error or EOF */
|
84
|
+
ASSERT (uv_last_error(loop).code == UV_EOF);
|
85
|
+
|
86
|
+
if (buf.base) {
|
87
|
+
free(buf.base);
|
88
|
+
}
|
89
|
+
|
90
|
+
req = (uv_shutdown_t*) malloc(sizeof *req);
|
91
|
+
uv_shutdown(req, handle, after_shutdown);
|
92
|
+
|
93
|
+
return;
|
94
|
+
}
|
95
|
+
|
96
|
+
if (nread == 0) {
|
97
|
+
/* Everything OK, but nothing read. */
|
98
|
+
free(buf.base);
|
99
|
+
return;
|
100
|
+
}
|
101
|
+
|
102
|
+
/*
|
103
|
+
* Scan for the letter Q which signals that we should quit the server.
|
104
|
+
* If we get QS it means close the stream.
|
105
|
+
*/
|
106
|
+
if (!server_closed) {
|
107
|
+
for (i = 0; i < nread; i++) {
|
108
|
+
if (buf.base[i] == 'Q') {
|
109
|
+
if (i + 1 < nread && buf.base[i + 1] == 'S') {
|
110
|
+
free(buf.base);
|
111
|
+
uv_close((uv_handle_t*)handle, on_close);
|
112
|
+
return;
|
113
|
+
} else {
|
114
|
+
uv_close(server, on_server_close);
|
115
|
+
server_closed = 1;
|
116
|
+
}
|
117
|
+
}
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
wr = (write_req_t*) malloc(sizeof *wr);
|
122
|
+
|
123
|
+
wr->buf = uv_buf_init(buf.base, nread);
|
124
|
+
if (uv_write(&wr->req, handle, &wr->buf, 1, after_write)) {
|
125
|
+
FATAL("uv_write failed");
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
static void on_close(uv_handle_t* peer) {
|
131
|
+
free(peer);
|
132
|
+
}
|
133
|
+
|
134
|
+
|
135
|
+
static uv_buf_t echo_alloc(uv_handle_t* handle, size_t suggested_size) {
|
136
|
+
return uv_buf_init(malloc(suggested_size), suggested_size);
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
static void on_connection(uv_stream_t* server, int status) {
|
141
|
+
uv_stream_t* stream;
|
142
|
+
int r;
|
143
|
+
|
144
|
+
if (status != 0) {
|
145
|
+
fprintf(stderr, "Connect error %d\n",
|
146
|
+
uv_last_error(loop).code);
|
147
|
+
}
|
148
|
+
ASSERT(status == 0);
|
149
|
+
|
150
|
+
switch (serverType) {
|
151
|
+
case TCP:
|
152
|
+
stream = malloc(sizeof(uv_tcp_t));
|
153
|
+
ASSERT(stream != NULL);
|
154
|
+
r = uv_tcp_init(loop, (uv_tcp_t*)stream);
|
155
|
+
ASSERT(r == 0);
|
156
|
+
break;
|
157
|
+
|
158
|
+
case PIPE:
|
159
|
+
stream = malloc(sizeof(uv_pipe_t));
|
160
|
+
ASSERT(stream != NULL);
|
161
|
+
r = uv_pipe_init(loop, (uv_pipe_t*)stream, 0);
|
162
|
+
ASSERT(r == 0);
|
163
|
+
break;
|
164
|
+
|
165
|
+
default:
|
166
|
+
ASSERT(0 && "Bad serverType");
|
167
|
+
abort();
|
168
|
+
}
|
169
|
+
|
170
|
+
/* associate server with stream */
|
171
|
+
stream->data = server;
|
172
|
+
|
173
|
+
r = uv_accept(server, stream);
|
174
|
+
ASSERT(r == 0);
|
175
|
+
|
176
|
+
r = uv_read_start(stream, echo_alloc, after_read);
|
177
|
+
ASSERT(r == 0);
|
178
|
+
}
|
179
|
+
|
180
|
+
|
181
|
+
static void on_server_close(uv_handle_t* handle) {
|
182
|
+
ASSERT(handle == server);
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
static void on_send(uv_udp_send_t* req, int status);
|
187
|
+
|
188
|
+
|
189
|
+
static void on_recv(uv_udp_t* handle,
|
190
|
+
ssize_t nread,
|
191
|
+
uv_buf_t buf,
|
192
|
+
struct sockaddr* addr,
|
193
|
+
unsigned flags) {
|
194
|
+
uv_udp_send_t* req;
|
195
|
+
int r;
|
196
|
+
|
197
|
+
ASSERT(nread > 0);
|
198
|
+
ASSERT(addr->sa_family == AF_INET);
|
199
|
+
|
200
|
+
req = malloc(sizeof(*req));
|
201
|
+
ASSERT(req != NULL);
|
202
|
+
|
203
|
+
r = uv_udp_send(req, handle, &buf, 1, *(struct sockaddr_in*)addr, on_send);
|
204
|
+
ASSERT(r == 0);
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
static void on_send(uv_udp_send_t* req, int status) {
|
209
|
+
ASSERT(status == 0);
|
210
|
+
free(req);
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
static int tcp4_echo_start(int port) {
|
215
|
+
struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port);
|
216
|
+
int r;
|
217
|
+
|
218
|
+
server = (uv_handle_t*)&tcpServer;
|
219
|
+
serverType = TCP;
|
220
|
+
|
221
|
+
r = uv_tcp_init(loop, &tcpServer);
|
222
|
+
if (r) {
|
223
|
+
/* TODO: Error codes */
|
224
|
+
fprintf(stderr, "Socket creation error\n");
|
225
|
+
return 1;
|
226
|
+
}
|
227
|
+
|
228
|
+
r = uv_tcp_bind(&tcpServer, addr);
|
229
|
+
if (r) {
|
230
|
+
/* TODO: Error codes */
|
231
|
+
fprintf(stderr, "Bind error\n");
|
232
|
+
return 1;
|
233
|
+
}
|
234
|
+
|
235
|
+
r = uv_listen((uv_stream_t*)&tcpServer, SOMAXCONN, on_connection);
|
236
|
+
if (r) {
|
237
|
+
/* TODO: Error codes */
|
238
|
+
fprintf(stderr, "Listen error %s\n",
|
239
|
+
uv_err_name(uv_last_error(loop)));
|
240
|
+
return 1;
|
241
|
+
}
|
242
|
+
|
243
|
+
return 0;
|
244
|
+
}
|
245
|
+
|
246
|
+
|
247
|
+
static int tcp6_echo_start(int port) {
|
248
|
+
struct sockaddr_in6 addr6 = uv_ip6_addr("::1", port);
|
249
|
+
int r;
|
250
|
+
|
251
|
+
server = (uv_handle_t*)&tcpServer;
|
252
|
+
serverType = TCP;
|
253
|
+
|
254
|
+
r = uv_tcp_init(loop, &tcpServer);
|
255
|
+
if (r) {
|
256
|
+
/* TODO: Error codes */
|
257
|
+
fprintf(stderr, "Socket creation error\n");
|
258
|
+
return 1;
|
259
|
+
}
|
260
|
+
|
261
|
+
/* IPv6 is optional as not all platforms support it */
|
262
|
+
r = uv_tcp_bind6(&tcpServer, addr6);
|
263
|
+
if (r) {
|
264
|
+
/* show message but return OK */
|
265
|
+
fprintf(stderr, "IPv6 not supported\n");
|
266
|
+
return 0;
|
267
|
+
}
|
268
|
+
|
269
|
+
r = uv_listen((uv_stream_t*)&tcpServer, SOMAXCONN, on_connection);
|
270
|
+
if (r) {
|
271
|
+
/* TODO: Error codes */
|
272
|
+
fprintf(stderr, "Listen error\n");
|
273
|
+
return 1;
|
274
|
+
}
|
275
|
+
|
276
|
+
return 0;
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
static int udp4_echo_start(int port) {
|
281
|
+
int r;
|
282
|
+
|
283
|
+
server = (uv_handle_t*)&udpServer;
|
284
|
+
serverType = UDP;
|
285
|
+
|
286
|
+
r = uv_udp_init(loop, &udpServer);
|
287
|
+
if (r) {
|
288
|
+
fprintf(stderr, "uv_udp_init: %s\n",
|
289
|
+
uv_strerror(uv_last_error(loop)));
|
290
|
+
return 1;
|
291
|
+
}
|
292
|
+
|
293
|
+
r = uv_udp_recv_start(&udpServer, echo_alloc, on_recv);
|
294
|
+
if (r) {
|
295
|
+
fprintf(stderr, "uv_udp_recv_start: %s\n",
|
296
|
+
uv_strerror(uv_last_error(loop)));
|
297
|
+
return 1;
|
298
|
+
}
|
299
|
+
|
300
|
+
return 0;
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
static int pipe_echo_start(char* pipeName) {
|
305
|
+
int r;
|
306
|
+
|
307
|
+
#ifndef _WIN32
|
308
|
+
{
|
309
|
+
uv_fs_t req;
|
310
|
+
uv_fs_unlink(uv_default_loop(), &req, pipeName, NULL);
|
311
|
+
uv_fs_req_cleanup(&req);
|
312
|
+
}
|
313
|
+
#endif
|
314
|
+
|
315
|
+
server = (uv_handle_t*)&pipeServer;
|
316
|
+
serverType = PIPE;
|
317
|
+
|
318
|
+
r = uv_pipe_init(loop, &pipeServer, 0);
|
319
|
+
if (r) {
|
320
|
+
fprintf(stderr, "uv_pipe_init: %s\n",
|
321
|
+
uv_strerror(uv_last_error(loop)));
|
322
|
+
return 1;
|
323
|
+
}
|
324
|
+
|
325
|
+
r = uv_pipe_bind(&pipeServer, pipeName);
|
326
|
+
if (r) {
|
327
|
+
fprintf(stderr, "uv_pipe_bind: %s\n",
|
328
|
+
uv_strerror(uv_last_error(loop)));
|
329
|
+
return 1;
|
330
|
+
}
|
331
|
+
|
332
|
+
r = uv_listen((uv_stream_t*)&pipeServer, SOMAXCONN, on_connection);
|
333
|
+
if (r) {
|
334
|
+
fprintf(stderr, "uv_pipe_listen: %s\n",
|
335
|
+
uv_strerror(uv_last_error(loop)));
|
336
|
+
return 1;
|
337
|
+
}
|
338
|
+
|
339
|
+
return 0;
|
340
|
+
}
|
341
|
+
|
342
|
+
|
343
|
+
HELPER_IMPL(tcp4_echo_server) {
|
344
|
+
loop = uv_default_loop();
|
345
|
+
|
346
|
+
if (tcp4_echo_start(TEST_PORT))
|
347
|
+
return 1;
|
348
|
+
|
349
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
350
|
+
return 0;
|
351
|
+
}
|
352
|
+
|
353
|
+
|
354
|
+
HELPER_IMPL(tcp6_echo_server) {
|
355
|
+
loop = uv_default_loop();
|
356
|
+
|
357
|
+
if (tcp6_echo_start(TEST_PORT))
|
358
|
+
return 1;
|
359
|
+
|
360
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
361
|
+
return 0;
|
362
|
+
}
|
363
|
+
|
364
|
+
|
365
|
+
HELPER_IMPL(pipe_echo_server) {
|
366
|
+
loop = uv_default_loop();
|
367
|
+
|
368
|
+
if (pipe_echo_start(TEST_PIPENAME))
|
369
|
+
return 1;
|
370
|
+
|
371
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
372
|
+
return 0;
|
373
|
+
}
|
374
|
+
|
375
|
+
|
376
|
+
HELPER_IMPL(udp4_echo_server) {
|
377
|
+
loop = uv_default_loop();
|
378
|
+
|
379
|
+
if (udp4_echo_start(TEST_PORT))
|
380
|
+
return 1;
|
381
|
+
|
382
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
383
|
+
return 0;
|
384
|
+
}
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
foobar
|