Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 148 lines (127 sloc) 4.182 kb
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
1 /*
33269e0 @kostja End the copyright madness.
kostja authored
2 * Redistribution and use in source and binary forms, with or
3 * without modification, are permitted provided that the following
4 * conditions are met:
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
5 *
33269e0 @kostja End the copyright madness.
kostja authored
6 * 1. Redistributions of source code must retain the above
7 * copyright notice, this list of conditions and the
8 * following disclaimer.
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
9 *
33269e0 @kostja End the copyright madness.
kostja authored
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19 * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
27 * SUCH DAMAGE.
28 */
65df38a @kostja Implement blueprint 'consistent-header-guards'.
kostja authored
29 #include "iproto.h"
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
30 #include "exception.h"
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
31
32 #include <stdio.h>
33 #include <string.h>
34
72bfb7f @rtokarev Isolate error codes definition from iproto.[ch] to errcode.[ch].
rtokarev authored
35 #include <errcode.h>
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
36 #include <palloc.h>
37 #include <fiber.h>
38 #include <tbuf.h>
39 #include <say.h>
40
41 const uint32_t msg_ping = 0xff00;
42
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
43 static void iproto_reply(iproto_callback callback, struct tbuf *request);
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
44
0d3e60b @kostya-shulgin bug899343:
kostya-shulgin authored
45 static void
46 iproto_validate_header(struct iproto_header *header);
47
b924563 @pmwkaa Bug972345: memory leak while processing insert requests
pmwkaa authored
48 inline static int
49 iproto_flush(struct tbuf **in, ssize_t to_read)
50 {
51 /*
52 * Flush output and garbage collect before reading
53 * next header.
54 */
55 if (to_read > 0) {
56 if (iov_flush() < 0) {
57 say_warn("io_error: %s", strerror(errno));
58 return -1;
59 }
60 fiber_gc();
61 /* Must be reset after fiber_gc() */
62 *in = fiber->rbuf;
63 }
64 return 0;
65 }
66
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
67 void
d8025e5 @kostja Refactoring: remove the hack with storing txn in fiber.
kostja authored
68 iproto_interact(iproto_callback callback)
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
69 {
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
70 struct tbuf *in = fiber->rbuf;
71 ssize_t to_read = sizeof(struct iproto_header);
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
72
73 for (;;) {
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
74 if (to_read > 0 && fiber_bread(in, to_read) <= 0)
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
75 break;
76
0d3e60b @kostya-shulgin bug899343:
kostya-shulgin authored
77 /* validating iproto package header */
78 iproto_validate_header(iproto(in));
79
80 ssize_t request_len = sizeof(struct iproto_header)
81 + iproto(in)->len;
472a615 @kostja Rename tbuf->size0 -> tbuf->size.
kostja authored
82 to_read = request_len - in->size;
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
83
b924563 @pmwkaa Bug972345: memory leak while processing insert requests
pmwkaa authored
84 if (iproto_flush(&in, to_read) == -1)
85 break;
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
86 if (to_read > 0 && fiber_bread(in, to_read) <= 0)
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
87 break;
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
88
89 struct tbuf *request = tbuf_split(in, request_len);
d8025e5 @kostja Refactoring: remove the hack with storing txn in fiber.
kostja authored
90 iproto_reply(callback, request);
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
91
472a615 @kostja Rename tbuf->size0 -> tbuf->size.
kostja authored
92 to_read = sizeof(struct iproto_header) - in->size;
b924563 @pmwkaa Bug972345: memory leak while processing insert requests
pmwkaa authored
93 if (iproto_flush(&in, to_read) == -1)
94 break;
9b8dd70 @delamonpansie Initial public import
delamonpansie authored
95 }
96 }
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
97
98 /** Stack a reply to a single request to the fiber's io vector. */
99
100 static void iproto_reply(iproto_callback callback, struct tbuf *request)
101 {
102 struct iproto_header_retcode *reply;
103
d6965dc @kostja A pre-requisite patch for Lua stored procedures: fiber->pool.
kostja authored
104 reply = palloc(fiber->gc_pool, sizeof(*reply));
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
105 reply->msg_code = iproto(request)->msg_code;
106 reply->sync = iproto(request)->sync;
107
108 if (unlikely(reply->msg_code == msg_ping)) {
109 reply->len = 0;
828eed5 @kostja Move all iov functions to a common prefix (iov_).
kostja authored
110 iov_add(reply, sizeof(struct iproto_header));
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
111 return;
112 }
113
114 reply->len = sizeof(uint32_t); /* ret_code */
828eed5 @kostja Move all iov functions to a common prefix (iov_).
kostja authored
115 iov_add(reply, sizeof(struct iproto_header_retcode));
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
116 size_t saved_iov_cnt = fiber->iov_cnt;
117 /* make request point to iproto data */
472a615 @kostja Rename tbuf->size0 -> tbuf->size.
kostja authored
118 request->size = iproto(request)->len;
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
119 request->data = iproto(request)->data;
120
121 @try {
122 callback(reply->msg_code, request);
123 reply->ret_code = 0;
124 }
125 @catch (ClientError *e) {
472a615 @kostja Rename tbuf->size0 -> tbuf->size.
kostja authored
126 fiber->iov->size -= (fiber->iov_cnt - saved_iov_cnt) * sizeof(struct iovec);
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
127 fiber->iov_cnt = saved_iov_cnt;
128 reply->ret_code = tnt_errcode_val(e->errcode);
828eed5 @kostja Move all iov functions to a common prefix (iov_).
kostja authored
129 iov_dup(e->errmsg, strlen(e->errmsg)+1);
9b3d0a7 @kostja https://blueprints.launchpad.net/tarantool/+spec/client-error
kostja authored
130 }
131 for (; saved_iov_cnt < fiber->iov_cnt; saved_iov_cnt++)
132 reply->len += iovec(fiber->iov)[saved_iov_cnt].iov_len;
133 }
0d3e60b @kostya-shulgin bug899343:
kostya-shulgin authored
134
135 static void
136 iproto_validate_header(struct iproto_header *header)
137 {
7563b6e @kostya-shulgin bug899343: review fixes.
kostya-shulgin authored
138 if (header->len > IPROTO_BODY_LEN_MAX) {
139 /*
140 * The package is too big, just close connection for now to
141 * avoid DoS.
142 */
143 say_error("received package is too big: %llu",
0d3e60b @kostya-shulgin bug899343:
kostya-shulgin authored
144 (unsigned long long)header->len);
145 tnt_raise(FiberCancelException);
146 }
147 }
Something went wrong with that request. Please try again.