1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
1 |
/*
|
2 |
* Drizzle Client & Protocol Library
|
|
3 |
*
|
|
4 |
* Copyright (C) 2008 Eric Day (eday@oddments.org)
|
|
5 |
* All rights reserved.
|
|
6 |
*
|
|
1971.2.1
by kalebral at gmail
update files that did not have license or had incorrect license structure |
7 |
* Redistribution and use in source and binary forms, with or without
|
8 |
* modification, are permitted provided that the following conditions are
|
|
9 |
* met:
|
|
10 |
*
|
|
11 |
* * Redistributions of source code must retain the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer.
|
|
13 |
*
|
|
14 |
* * Redistributions in binary form must reproduce the above
|
|
15 |
* copyright notice, this list of conditions and the following disclaimer
|
|
16 |
* in the documentation and/or other materials provided with the
|
|
17 |
* distribution.
|
|
18 |
*
|
|
19 |
* * The names of its contributors may not be used to endorse or
|
|
20 |
* promote products derived from this software without specific prior
|
|
21 |
* written permission.
|
|
22 |
*
|
|
23 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
24 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
25 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
26 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
27 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
28 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
29 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
30 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
31 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
32 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
33 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
34 |
*
|
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
35 |
*/
|
36 |
||
1971.2.1
by kalebral at gmail
update files that did not have license or had incorrect license structure |
37 |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
38 |
/**
|
39 |
* @file
|
|
40 |
* @brief Connection Definitions
|
|
41 |
*/
|
|
42 |
||
43 |
#include "common.h" |
|
44 |
||
45 |
/**
|
|
46 |
* @addtogroup drizzle_con_static Static Connection Declarations
|
|
47 |
* @ingroup drizzle_con
|
|
48 |
* @{
|
|
49 |
*/
|
|
50 |
||
51 |
/**
|
|
52 |
* Set socket options for a connection.
|
|
53 |
*
|
|
54 |
* @param[in] con Connection structure previously initialized with
|
|
55 |
* drizzle_con_create(), drizzle_con_clone(), or related functions.
|
|
56 |
* @return Standard drizzle return value.
|
|
57 |
*/
|
|
58 |
static drizzle_return_t _con_setsockopt(drizzle_con_st *con); |
|
59 |
||
60 |
/** @} */
|
|
61 |
||
62 |
/*
|
|
63 |
* Common Definitions
|
|
64 |
*/
|
|
65 |
||
66 |
int drizzle_con_fd(const drizzle_con_st *con) |
|
67 |
{
|
|
68 |
return con->fd; |
|
69 |
}
|
|
70 |
||
71 |
drizzle_return_t drizzle_con_set_fd(drizzle_con_st *con, int fd) |
|
72 |
{
|
|
73 |
drizzle_return_t ret; |
|
74 |
||
75 |
con->fd= fd; |
|
76 |
||
77 |
ret= _con_setsockopt(con); |
|
78 |
if (ret != DRIZZLE_RETURN_OK) |
|
79 |
con->drizzle->last_errno= errno; |
|
80 |
||
81 |
return ret; |
|
82 |
}
|
|
83 |
||
84 |
void drizzle_con_close(drizzle_con_st *con) |
|
85 |
{
|
|
86 |
if (con->fd == -1) |
|
87 |
return; |
|
88 |
||
89 |
(void)close(con->fd); |
|
90 |
con->fd= -1; |
|
91 |
||
92 |
con->options&= (drizzle_con_options_t)~DRIZZLE_CON_READY; |
|
93 |
con->packet_number= 0; |
|
94 |
con->buffer_ptr= con->buffer; |
|
95 |
con->buffer_size= 0; |
|
96 |
con->events= 0; |
|
97 |
con->revents= 0; |
|
98 |
||
99 |
drizzle_state_reset(con); |
|
100 |
}
|
|
101 |
||
102 |
drizzle_return_t drizzle_con_set_events(drizzle_con_st *con, short events) |
|
103 |
{
|
|
104 |
drizzle_return_t ret; |
|
105 |
||
106 |
if ((con->events | events) == con->events) |
|
107 |
return DRIZZLE_RETURN_OK; |
|
108 |
||
109 |
con->events|= events; |
|
110 |
||
111 |
if (con->drizzle->event_watch_fn != NULL) |
|
112 |
{
|
|
113 |
ret= con->drizzle->event_watch_fn(con, con->events, |
|
114 |
con->drizzle->event_watch_context); |
|
115 |
if (ret != DRIZZLE_RETURN_OK) |
|
116 |
{
|
|
117 |
drizzle_con_close(con); |
|
118 |
return ret; |
|
119 |
}
|
|
120 |
}
|
|
121 |
||
122 |
return DRIZZLE_RETURN_OK; |
|
123 |
}
|
|
124 |
||
125 |
drizzle_return_t drizzle_con_set_revents(drizzle_con_st *con, short revents) |
|
126 |
{
|
|
127 |
drizzle_return_t ret; |
|
128 |
||
129 |
if (revents != 0) |
|
130 |
con->options|= DRIZZLE_CON_IO_READY; |
|
131 |
||
132 |
con->revents= revents; |
|
133 |
||
134 |
/* Remove external POLLOUT watch if we didn't ask for it. Otherwise we spin
|
|
135 |
forever until another POLLIN state change. This is much more efficient
|
|
136 |
than removing POLLOUT on every state change since some external polling
|
|
137 |
mechanisms need to use a system call to change flags (like Linux epoll). */
|
|
138 |
if (revents & POLLOUT && !(con->events & POLLOUT) && |
|
139 |
con->drizzle->event_watch_fn != NULL) |
|
140 |
{
|
|
141 |
ret= con->drizzle->event_watch_fn(con, con->events, |
|
142 |
con->drizzle->event_watch_context); |
|
143 |
if (ret != DRIZZLE_RETURN_OK) |
|
144 |
{
|
|
145 |
drizzle_con_close(con); |
|
146 |
return ret; |
|
147 |
}
|
|
148 |
}
|
|
149 |
||
150 |
con->events&= (short)~revents; |
|
151 |
||
152 |
return DRIZZLE_RETURN_OK; |
|
153 |
}
|
|
154 |
||
155 |
drizzle_st *drizzle_con_drizzle(const drizzle_con_st *con) |
|
156 |
{
|
|
157 |
return con->drizzle; |
|
158 |
}
|
|
159 |
||
160 |
const char *drizzle_con_error(const drizzle_con_st *con) |
|
161 |
{
|
|
162 |
return drizzle_error(con->drizzle); |
|
163 |
}
|
|
164 |
||
165 |
int drizzle_con_errno(const drizzle_con_st *con) |
|
166 |
{
|
|
167 |
return drizzle_errno(con->drizzle); |
|
168 |
}
|
|
169 |
||
170 |
uint16_t drizzle_con_error_code(const drizzle_con_st *con) |
|
171 |
{
|
|
172 |
return drizzle_error_code(con->drizzle); |
|
173 |
}
|
|
174 |
||
175 |
const char *drizzle_con_sqlstate(const drizzle_con_st *con) |
|
176 |
{
|
|
177 |
return drizzle_sqlstate(con->drizzle); |
|
178 |
}
|
|
179 |
||
180 |
drizzle_con_options_t drizzle_con_options(const drizzle_con_st *con) |
|
181 |
{
|
|
182 |
return con->options; |
|
183 |
}
|
|
184 |
||
185 |
void drizzle_con_set_options(drizzle_con_st *con, |
|
186 |
drizzle_con_options_t options) |
|
187 |
{
|
|
188 |
con->options= options; |
|
189 |
}
|
|
190 |
||
191 |
void drizzle_con_add_options(drizzle_con_st *con, |
|
192 |
drizzle_con_options_t options) |
|
193 |
{
|
|
194 |
con->options|= options; |
|
195 |
||
196 |
/* If asking for the experimental Drizzle protocol, clean the MySQL flag. */
|
|
197 |
if (con->options & DRIZZLE_CON_EXPERIMENTAL) |
|
198 |
con->options&= (drizzle_con_options_t)~DRIZZLE_CON_MYSQL; |
|
199 |
}
|
|
200 |
||
201 |
void drizzle_con_remove_options(drizzle_con_st *con, |
|
202 |
drizzle_con_options_t options) |
|
203 |
{
|
|
204 |
con->options&= ~options; |
|
205 |
}
|
|
206 |
||
207 |
const char *drizzle_con_host(const drizzle_con_st *con) |
|
208 |
{
|
|
209 |
if (con->socket_type == DRIZZLE_CON_SOCKET_TCP) |
|
210 |
{
|
|
211 |
if (con->socket.tcp.host == NULL && !(con->options & DRIZZLE_CON_LISTEN)) |
|
212 |
return DRIZZLE_DEFAULT_TCP_HOST; |
|
213 |
||
214 |
return con->socket.tcp.host; |
|
215 |
}
|
|
216 |
||
217 |
return NULL; |
|
218 |
}
|
|
219 |
||
220 |
in_port_t drizzle_con_port(const drizzle_con_st *con) |
|
221 |
{
|
|
222 |
if (con->socket_type == DRIZZLE_CON_SOCKET_TCP) |
|
223 |
{
|
|
224 |
if (con->socket.tcp.port != 0) |
|
225 |
return con->socket.tcp.port; |
|
226 |
||
227 |
if (con->options & DRIZZLE_CON_MYSQL) |
|
228 |
return DRIZZLE_DEFAULT_TCP_PORT_MYSQL; |
|
229 |
||
230 |
return DRIZZLE_DEFAULT_TCP_PORT; |
|
231 |
}
|
|
232 |
||
233 |
return 0; |
|
234 |
}
|
|
235 |
||
236 |
void drizzle_con_set_tcp(drizzle_con_st *con, const char *host, in_port_t port) |
|
237 |
{
|
|
238 |
drizzle_con_reset_addrinfo(con); |
|
239 |
||
240 |
con->socket_type= DRIZZLE_CON_SOCKET_TCP; |
|
241 |
||
242 |
if (host == NULL) |
|
243 |
con->socket.tcp.host= NULL; |
|
244 |
else
|
|
245 |
{
|
|
246 |
con->socket.tcp.host= con->socket.tcp.host_buffer; |
|
247 |
strncpy(con->socket.tcp.host, host, NI_MAXHOST); |
|
248 |
con->socket.tcp.host[NI_MAXHOST - 1]= 0; |
|
249 |
}
|
|
250 |
||
251 |
con->socket.tcp.port= port; |
|
252 |
}
|
|
253 |
||
254 |
const char *drizzle_con_user(const drizzle_con_st *con) |
|
255 |
{
|
|
256 |
return con->user; |
|
257 |
}
|
|
258 |
||
259 |
const char *drizzle_con_password(const drizzle_con_st *con) |
|
260 |
{
|
|
261 |
return con->password; |
|
262 |
}
|
|
263 |
||
264 |
void drizzle_con_set_auth(drizzle_con_st *con, const char *user, |
|
265 |
const char *password) |
|
266 |
{
|
|
267 |
if (user == NULL) |
|
268 |
con->user[0]= 0; |
|
269 |
else
|
|
270 |
{
|
|
271 |
strncpy(con->user, user, DRIZZLE_MAX_USER_SIZE); |
|
272 |
con->user[DRIZZLE_MAX_USER_SIZE - 1]= 0; |
|
273 |
}
|
|
274 |
||
275 |
if (password == NULL) |
|
276 |
con->password[0]= 0; |
|
277 |
else
|
|
278 |
{
|
|
279 |
strncpy(con->password, password, DRIZZLE_MAX_PASSWORD_SIZE); |
|
280 |
con->password[DRIZZLE_MAX_PASSWORD_SIZE - 1]= 0; |
|
281 |
}
|
|
282 |
}
|
|
283 |
||
284 |
const char *drizzle_con_db(const drizzle_con_st *con) |
|
285 |
{
|
|
286 |
return con->db; |
|
287 |
}
|
|
288 |
||
289 |
void drizzle_con_set_db(drizzle_con_st *con, const char *db) |
|
290 |
{
|
|
291 |
if (db == NULL) |
|
292 |
con->db[0]= 0; |
|
293 |
else
|
|
294 |
{
|
|
295 |
strncpy(con->db, db, DRIZZLE_MAX_DB_SIZE); |
|
296 |
con->db[DRIZZLE_MAX_DB_SIZE - 1]= 0; |
|
297 |
}
|
|
298 |
}
|
|
299 |
||
300 |
void *drizzle_con_context(const drizzle_con_st *con) |
|
301 |
{
|
|
302 |
return con->context; |
|
303 |
}
|
|
304 |
||
305 |
void drizzle_con_set_context(drizzle_con_st *con, void *context) |
|
306 |
{
|
|
307 |
con->context= context; |
|
308 |
}
|
|
309 |
||
310 |
void drizzle_con_set_context_free_fn(drizzle_con_st *con, |
|
311 |
drizzle_con_context_free_fn *function) |
|
312 |
{
|
|
313 |
con->context_free_fn= function; |
|
314 |
}
|
|
315 |
||
316 |
uint8_t drizzle_con_protocol_version(const drizzle_con_st *con) |
|
317 |
{
|
|
318 |
return con->protocol_version; |
|
319 |
}
|
|
320 |
||
321 |
const char *drizzle_con_server_version(const drizzle_con_st *con) |
|
322 |
{
|
|
323 |
return con->server_version; |
|
324 |
}
|
|
325 |
||
326 |
uint32_t drizzle_con_server_version_number(const drizzle_con_st *con) |
|
327 |
{
|
|
328 |
uint32_t major; |
|
329 |
uint32_t minor; |
|
330 |
uint32_t version; |
|
331 |
const char *current; |
|
332 |
char *end; |
|
333 |
||
334 |
current= con->server_version; |
|
335 |
||
336 |
major= (uint32_t)strtoul(current, &end, 10); |
|
337 |
current= end + 1; |
|
338 |
minor= (uint32_t)strtoul(current, &end, 10); |
|
339 |
current= end + 1; |
|
340 |
version= (uint32_t)strtoul(current, &end, 10); |
|
341 |
||
342 |
return (major * 10000) + (minor * 100) + version; |
|
343 |
}
|
|
344 |
||
345 |
uint32_t drizzle_con_thread_id(const drizzle_con_st *con) |
|
346 |
{
|
|
347 |
return con->thread_id; |
|
348 |
}
|
|
349 |
||
350 |
const uint8_t *drizzle_con_scramble(const drizzle_con_st *con) |
|
351 |
{
|
|
352 |
return con->scramble; |
|
353 |
}
|
|
354 |
||
355 |
drizzle_capabilities_t drizzle_con_capabilities(const drizzle_con_st *con) |
|
356 |
{
|
|
357 |
return con->capabilities; |
|
358 |
}
|
|
359 |
||
360 |
drizzle_charset_t drizzle_con_charset(const drizzle_con_st *con) |
|
361 |
{
|
|
362 |
return con->charset; |
|
363 |
}
|
|
364 |
||
365 |
drizzle_con_status_t drizzle_con_status(const drizzle_con_st *con) |
|
366 |
{
|
|
367 |
return con->status; |
|
368 |
}
|
|
369 |
||
370 |
uint32_t drizzle_con_max_packet_size(const drizzle_con_st *con) |
|
371 |
{
|
|
372 |
return con->max_packet_size; |
|
373 |
}
|
|
374 |
||
375 |
/*
|
|
376 |
* Client Definitions
|
|
377 |
*/
|
|
378 |
||
379 |
drizzle_return_t drizzle_con_connect(drizzle_con_st *con) |
|
380 |
{
|
|
381 |
if (con->options & DRIZZLE_CON_READY) |
|
382 |
return DRIZZLE_RETURN_OK; |
|
383 |
||
384 |
if (drizzle_state_none(con)) |
|
385 |
{
|
|
386 |
if (!(con->options & DRIZZLE_CON_RAW_PACKET)) |
|
387 |
{
|
|
388 |
drizzle_state_push(con, drizzle_state_handshake_server_read); |
|
389 |
drizzle_state_push(con, drizzle_state_packet_read); |
|
390 |
}
|
|
391 |
||
392 |
drizzle_state_push(con, drizzle_state_connect); |
|
393 |
drizzle_state_push(con, drizzle_state_addrinfo); |
|
394 |
}
|
|
395 |
||
396 |
return drizzle_state_loop(con); |
|
397 |
}
|
|
398 |
||
399 |
drizzle_result_st *drizzle_con_quit(drizzle_con_st *con, |
|
400 |
drizzle_result_st *result, |
|
401 |
drizzle_return_t *ret_ptr) |
|
402 |
{
|
|
403 |
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUIT, NULL, 0, |
|
404 |
0, ret_ptr); |
|
405 |
}
|
|
406 |
||
407 |
drizzle_result_st *drizzle_quit(drizzle_con_st *con, |
|
408 |
drizzle_result_st *result, |
|
409 |
drizzle_return_t *ret_ptr) |
|
410 |
{
|
|
411 |
return drizzle_con_quit(con, result, ret_ptr); |
|
412 |
}
|
|
413 |
||
414 |
drizzle_result_st *drizzle_con_select_db(drizzle_con_st *con, |
|
415 |
drizzle_result_st *result, |
|
416 |
const char *db, |
|
417 |
drizzle_return_t *ret_ptr) |
|
418 |
{
|
|
419 |
drizzle_con_set_db(con, db); |
|
420 |
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_INIT_DB, |
|
421 |
db, strlen(db), strlen(db), ret_ptr); |
|
422 |
}
|
|
423 |
||
424 |
drizzle_result_st *drizzle_select_db(drizzle_con_st *con, |
|
425 |
drizzle_result_st *result, |
|
426 |
const char *db, |
|
427 |
drizzle_return_t *ret_ptr) |
|
428 |
{
|
|
429 |
return drizzle_con_select_db(con, result, db, ret_ptr); |
|
430 |
}
|
|
431 |
||
432 |
drizzle_result_st *drizzle_con_shutdown(drizzle_con_st *con, |
|
433 |
drizzle_result_st *result, |
|
434 |
drizzle_return_t *ret_ptr) |
|
435 |
{
|
|
436 |
if (con->options & DRIZZLE_CON_MYSQL) |
|
437 |
{
|
|
438 |
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_SHUTDOWN, |
|
439 |
"0", 1, 1, ret_ptr); |
|
440 |
}
|
|
441 |
||
442 |
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_SHUTDOWN, NULL, |
|
443 |
0, 0, ret_ptr); |
|
444 |
}
|
|
445 |
||
446 |
drizzle_result_st *drizzle_shutdown(drizzle_con_st *con, |
|
447 |
drizzle_result_st *result, uint32_t level, |
|
448 |
drizzle_return_t *ret_ptr) |
|
449 |
{
|
|
450 |
(void) level; |
|
451 |
return drizzle_con_shutdown(con, result, ret_ptr); |
|
452 |
}
|
|
453 |
||
454 |
drizzle_result_st *drizzle_con_ping(drizzle_con_st *con, |
|
455 |
drizzle_result_st *result, |
|
456 |
drizzle_return_t *ret_ptr) |
|
457 |
{
|
|
458 |
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_PING, NULL, 0, |
|
459 |
0, ret_ptr); |
|
460 |
}
|
|
461 |
||
462 |
drizzle_result_st *drizzle_ping(drizzle_con_st *con, |
|
463 |
drizzle_result_st *result, |
|
464 |
drizzle_return_t *ret_ptr) |
|
465 |
{
|
|
466 |
return drizzle_con_ping(con, result, ret_ptr); |
|
467 |
}
|
|
468 |
||
469 |
drizzle_result_st *drizzle_con_command_write(drizzle_con_st *con, |
|
470 |
drizzle_result_st *result, |
|
471 |
drizzle_command_t command, |
|
472 |
const void *data, size_t size, |
|
473 |
size_t total, |
|
474 |
drizzle_return_t *ret_ptr) |
|
475 |
{
|
|
476 |
if (!(con->options & DRIZZLE_CON_READY)) |
|
477 |
{
|
|
478 |
if (con->options & DRIZZLE_CON_RAW_PACKET) |
|
479 |
{
|
|
480 |
drizzle_set_error(con->drizzle, "drizzle_command_write", |
|
481 |
"connection not ready"); |
|
482 |
*ret_ptr= DRIZZLE_RETURN_NOT_READY; |
|
483 |
return result; |
|
484 |
}
|
|
485 |
||
486 |
*ret_ptr= drizzle_con_connect(con); |
|
487 |
if (*ret_ptr != DRIZZLE_RETURN_OK) |
|
488 |
return result; |
|
489 |
}
|
|
490 |
||
491 |
if (drizzle_state_none(con)) |
|
492 |
{
|
|
493 |
if (con->options & (DRIZZLE_CON_RAW_PACKET | DRIZZLE_CON_NO_RESULT_READ)) |
|
494 |
con->result= NULL; |
|
495 |
else
|
|
496 |
{
|
|
497 |
con->result= drizzle_result_create(con, result); |
|
498 |
if (con->result == NULL) |
|
499 |
{
|
|
500 |
*ret_ptr= DRIZZLE_RETURN_MEMORY; |
|
501 |
return NULL; |
|
502 |
}
|
|
503 |
}
|
|
504 |
||
505 |
con->command= command; |
|
506 |
con->command_data= (uint8_t *)data; |
|
507 |
con->command_size= size; |
|
508 |
con->command_offset= 0; |
|
509 |
con->command_total= total; |
|
510 |
||
511 |
drizzle_state_push(con, drizzle_state_command_write); |
|
512 |
}
|
|
513 |
else if (con->command_data == NULL) |
|
514 |
{
|
|
515 |
con->command_data= (uint8_t *)data; |
|
516 |
con->command_size= size; |
|
517 |
}
|
|
518 |
||
519 |
*ret_ptr= drizzle_state_loop(con); |
|
520 |
if (*ret_ptr == DRIZZLE_RETURN_PAUSE) |
|
521 |
*ret_ptr= DRIZZLE_RETURN_OK; |
|
522 |
else if (*ret_ptr != DRIZZLE_RETURN_OK && |
|
523 |
*ret_ptr != DRIZZLE_RETURN_IO_WAIT && |
|
524 |
*ret_ptr != DRIZZLE_RETURN_ERROR_CODE) |
|
525 |
{
|
|
526 |
drizzle_result_free(con->result); |
|
527 |
con->result= result; |
|
528 |
}
|
|
529 |
||
530 |
return con->result; |
|
531 |
}
|
|
532 |
||
533 |
/*
|
|
534 |
* Server Definitions
|
|
535 |
*/
|
|
536 |
||
537 |
drizzle_return_t drizzle_con_listen(drizzle_con_st *con) |
|
538 |
{
|
|
539 |
if (con->options & DRIZZLE_CON_READY) |
|
540 |
return DRIZZLE_RETURN_OK; |
|
541 |
||
542 |
if (drizzle_state_none(con)) |
|
543 |
{
|
|
544 |
drizzle_state_push(con, drizzle_state_listen); |
|
545 |
drizzle_state_push(con, drizzle_state_addrinfo); |
|
546 |
}
|
|
547 |
||
548 |
return drizzle_state_loop(con); |
|
549 |
}
|
|
550 |
||
551 |
int drizzle_con_backlog(const drizzle_con_st *con) |
|
552 |
{
|
|
553 |
return con->backlog; |
|
554 |
}
|
|
555 |
||
556 |
void drizzle_con_set_backlog(drizzle_con_st *con, int backlog) |
|
557 |
{
|
|
558 |
con->backlog= backlog; |
|
559 |
}
|
|
560 |
||
561 |
void drizzle_con_set_protocol_version(drizzle_con_st *con, |
|
562 |
uint8_t protocol_version) |
|
563 |
{
|
|
564 |
con->protocol_version= protocol_version; |
|
565 |
}
|
|
566 |
||
567 |
void drizzle_con_set_server_version(drizzle_con_st *con, |
|
568 |
const char *server_version) |
|
569 |
{
|
|
570 |
if (server_version == NULL) |
|
571 |
con->server_version[0]= 0; |
|
572 |
else
|
|
573 |
{
|
|
574 |
strncpy(con->server_version, server_version, |
|
575 |
DRIZZLE_MAX_SERVER_VERSION_SIZE); |
|
576 |
con->server_version[DRIZZLE_MAX_SERVER_VERSION_SIZE - 1]= 0; |
|
577 |
}
|
|
578 |
}
|
|
579 |
||
580 |
void drizzle_con_set_thread_id(drizzle_con_st *con, uint32_t thread_id) |
|
581 |
{
|
|
582 |
con->thread_id= thread_id; |
|
583 |
}
|
|
584 |
||
585 |
void drizzle_con_set_scramble(drizzle_con_st *con, const uint8_t *scramble) |
|
586 |
{
|
|
587 |
if (scramble == NULL) |
|
588 |
con->scramble= NULL; |
|
589 |
else
|
|
590 |
{
|
|
591 |
con->scramble= con->scramble_buffer; |
|
592 |
memcpy(con->scramble, scramble, DRIZZLE_MAX_SCRAMBLE_SIZE); |
|
593 |
}
|
|
594 |
}
|
|
595 |
||
596 |
void drizzle_con_set_capabilities(drizzle_con_st *con, |
|
597 |
drizzle_capabilities_t capabilities) |
|
598 |
{
|
|
599 |
con->capabilities= capabilities; |
|
600 |
}
|
|
601 |
||
602 |
void drizzle_con_set_charset(drizzle_con_st *con, drizzle_charset_t charset) |
|
603 |
{
|
|
604 |
con->charset= charset; |
|
605 |
}
|
|
606 |
||
607 |
void drizzle_con_set_status(drizzle_con_st *con, drizzle_con_status_t status) |
|
608 |
{
|
|
609 |
con->status= status; |
|
610 |
}
|
|
611 |
||
612 |
void drizzle_con_set_max_packet_size(drizzle_con_st *con, |
|
613 |
uint32_t max_packet_size) |
|
614 |
{
|
|
615 |
con->max_packet_size= max_packet_size; |
|
616 |
}
|
|
617 |
||
618 |
void drizzle_con_copy_handshake(drizzle_con_st *con, drizzle_con_st *from) |
|
619 |
{
|
|
620 |
drizzle_con_set_auth(con, from->user, NULL); |
|
621 |
drizzle_con_set_scramble(con, from->scramble); |
|
622 |
drizzle_con_set_db(con, from->db); |
|
623 |
drizzle_con_set_protocol_version(con, from->protocol_version); |
|
624 |
drizzle_con_set_server_version(con, from->server_version); |
|
625 |
drizzle_con_set_thread_id(con, from->thread_id); |
|
626 |
drizzle_con_set_scramble(con, from->scramble); |
|
627 |
drizzle_con_set_capabilities(con, from->capabilities); |
|
628 |
drizzle_con_set_charset(con, from->charset); |
|
629 |
drizzle_con_set_status(con, from->status); |
|
630 |
drizzle_con_set_max_packet_size(con, from->max_packet_size); |
|
631 |
}
|
|
632 |
||
633 |
void *drizzle_con_command_read(drizzle_con_st *con, |
|
634 |
drizzle_command_t *command, size_t *offset, |
|
635 |
size_t *size, size_t *total, |
|
636 |
drizzle_return_t *ret_ptr) |
|
637 |
{
|
|
638 |
if (drizzle_state_none(con)) |
|
639 |
{
|
|
640 |
con->packet_number= 0; |
|
641 |
con->command_offset= 0; |
|
642 |
con->command_total= 0; |
|
643 |
||
644 |
drizzle_state_push(con, drizzle_state_command_read); |
|
645 |
drizzle_state_push(con, drizzle_state_packet_read); |
|
646 |
}
|
|
647 |
||
648 |
*offset= con->command_offset; |
|
649 |
||
650 |
*ret_ptr= drizzle_state_loop(con); |
|
651 |
if (*ret_ptr == DRIZZLE_RETURN_PAUSE) |
|
652 |
*ret_ptr= DRIZZLE_RETURN_OK; |
|
653 |
||
654 |
*command= con->command; |
|
655 |
*size= con->command_size; |
|
656 |
*total= con->command_total; |
|
657 |
||
658 |
return con->command_data; |
|
659 |
}
|
|
660 |
||
661 |
void *drizzle_con_command_buffer(drizzle_con_st *con, |
|
662 |
drizzle_command_t *command, size_t *total, |
|
663 |
drizzle_return_t *ret_ptr) |
|
664 |
{
|
|
665 |
uint8_t *command_data; |
|
666 |
size_t offset= 0; |
|
667 |
size_t size= 0; |
|
668 |
||
669 |
command_data= drizzle_con_command_read(con, command, &offset, &size, total, |
|
670 |
ret_ptr); |
|
671 |
if (*ret_ptr != DRIZZLE_RETURN_OK) |
|
672 |
return NULL; |
|
673 |
||
674 |
if (command_data == NULL) |
|
675 |
{
|
|
676 |
*total= 0; |
|
677 |
return NULL; |
|
678 |
}
|
|
679 |
||
680 |
if (con->command_buffer == NULL) |
|
681 |
{
|
|
682 |
con->command_buffer= malloc((*total) + 1); |
|
683 |
if (con->command_buffer == NULL) |
|
684 |
{
|
|
685 |
drizzle_set_error(con->drizzle, "drizzle_command_buffer", "malloc"); |
|
686 |
*ret_ptr= DRIZZLE_RETURN_MEMORY; |
|
687 |
return NULL; |
|
688 |
}
|
|
689 |
}
|
|
690 |
||
691 |
memcpy(con->command_buffer + offset, command_data, size); |
|
692 |
||
693 |
while ((offset + size) != (*total)) |
|
694 |
{
|
|
695 |
command_data= drizzle_con_command_read(con, command, &offset, &size, total, |
|
696 |
ret_ptr); |
|
697 |
if (*ret_ptr != DRIZZLE_RETURN_OK) |
|
698 |
return NULL; |
|
699 |
||
700 |
memcpy(con->command_buffer + offset, command_data, size); |
|
701 |
}
|
|
702 |
||
703 |
command_data= con->command_buffer; |
|
704 |
con->command_buffer= NULL; |
|
705 |
command_data[*total]= 0; |
|
706 |
||
707 |
return command_data; |
|
708 |
}
|
|
709 |
||
710 |
/*
|
|
711 |
* Local Definitions
|
|
712 |
*/
|
|
713 |
||
714 |
void drizzle_con_reset_addrinfo(drizzle_con_st *con) |
|
715 |
{
|
|
716 |
switch (con->socket_type) |
|
717 |
{
|
|
718 |
case DRIZZLE_CON_SOCKET_TCP: |
|
719 |
if (con->socket.tcp.addrinfo != NULL) |
|
720 |
{
|
|
721 |
freeaddrinfo(con->socket.tcp.addrinfo); |
|
722 |
con->socket.tcp.addrinfo= NULL; |
|
723 |
}
|
|
724 |
break; |
|
725 |
||
726 |
case DRIZZLE_CON_SOCKET_UDS: |
|
727 |
con->socket.uds.addrinfo.ai_addr= NULL; |
|
728 |
break; |
|
729 |
||
730 |
default: |
|
731 |
break; |
|
732 |
}
|
|
733 |
||
734 |
con->addrinfo_next= NULL; |
|
735 |
}
|
|
736 |
||
737 |
/*
|
|
738 |
* State Definitions
|
|
739 |
*/
|
|
740 |
||
741 |
drizzle_return_t drizzle_state_addrinfo(drizzle_con_st *con) |
|
742 |
{
|
|
743 |
drizzle_con_tcp_st *tcp; |
|
744 |
const char *host; |
|
745 |
char port[NI_MAXSERV]; |
|
746 |
struct addrinfo ai; |
|
747 |
int ret; |
|
748 |
||
749 |
drizzle_log_debug(con->drizzle, "drizzle_state_addrinfo"); |
|
750 |
||
751 |
switch (con->socket_type) |
|
752 |
{
|
|
753 |
case DRIZZLE_CON_SOCKET_TCP: |
|
754 |
tcp= &(con->socket.tcp); |
|
755 |
||
756 |
if (tcp->addrinfo != NULL) |
|
757 |
{
|
|
758 |
freeaddrinfo(tcp->addrinfo); |
|
759 |
tcp->addrinfo= NULL; |
|
760 |
}
|
|
761 |
||
762 |
if (tcp->port != 0) |
|
763 |
snprintf(port, NI_MAXSERV, "%u", tcp->port); |
|
764 |
else if (con->options & DRIZZLE_CON_MYSQL) |
|
765 |
snprintf(port, NI_MAXSERV, "%u", DRIZZLE_DEFAULT_TCP_PORT_MYSQL); |
|
766 |
else
|
|
767 |
snprintf(port, NI_MAXSERV, "%u", DRIZZLE_DEFAULT_TCP_PORT); |
|
768 |
||
769 |
memset(&ai, 0, sizeof(struct addrinfo)); |
|
770 |
ai.ai_socktype= SOCK_STREAM; |
|
771 |
ai.ai_protocol= IPPROTO_TCP; |
|
772 |
||
773 |
if (con->options & DRIZZLE_CON_LISTEN) |
|
774 |
{
|
|
775 |
ai.ai_flags = AI_PASSIVE; |
|
776 |
ai.ai_family = AF_UNSPEC; |
|
777 |
host= tcp->host; |
|
778 |
}
|
|
779 |
else
|
|
780 |
{
|
|
781 |
if (tcp->host == NULL) |
|
782 |
host= DRIZZLE_DEFAULT_TCP_HOST; |
|
783 |
else
|
|
784 |
host= tcp->host; |
|
785 |
}
|
|
786 |
||
787 |
ret= getaddrinfo(host, port, &ai, &(tcp->addrinfo)); |
|
788 |
if (ret != 0) |
|
789 |
{
|
|
790 |
drizzle_set_error(con->drizzle, "drizzle_state_addrinfo", |
|
791 |
"getaddrinfo:%s", gai_strerror(ret)); |
|
792 |
return DRIZZLE_RETURN_GETADDRINFO; |
|
793 |
}
|
|
794 |
||
795 |
con->addrinfo_next= tcp->addrinfo; |
|
796 |
||
797 |
break; |
|
798 |
||
799 |
case DRIZZLE_CON_SOCKET_UDS: |
|
800 |
con->addrinfo_next= &(con->socket.uds.addrinfo); |
|
801 |
break; |
|
802 |
||
803 |
default: |
|
804 |
break; |
|
805 |
}
|
|
806 |
||
807 |
drizzle_state_pop(con); |
|
808 |
return DRIZZLE_RETURN_OK; |
|
809 |
}
|
|
810 |
||
811 |
drizzle_return_t drizzle_state_connect(drizzle_con_st *con) |
|
812 |
{
|
|
813 |
int ret; |
|
814 |
drizzle_return_t dret; |
|
815 |
||
816 |
drizzle_log_debug(con->drizzle, "drizzle_state_connect"); |
|
817 |
||
818 |
if (con->fd != -1) |
|
819 |
{
|
|
820 |
(void)close(con->fd); |
|
821 |
con->fd= -1; |
|
822 |
}
|
|
823 |
||
824 |
if (con->addrinfo_next == NULL) |
|
825 |
{
|
|
826 |
drizzle_set_error(con->drizzle, "drizzle_state_connect", |
|
827 |
"could not connect"); |
|
828 |
drizzle_state_reset(con); |
|
829 |
return DRIZZLE_RETURN_COULD_NOT_CONNECT; |
|
830 |
}
|
|
831 |
||
832 |
con->fd= socket(con->addrinfo_next->ai_family, |
|
833 |
con->addrinfo_next->ai_socktype, |
|
834 |
con->addrinfo_next->ai_protocol); |
|
835 |
if (con->fd == -1) |
|
836 |
{
|
|
837 |
drizzle_set_error(con->drizzle, "drizzle_state_connect", "socket:%d", |
|
838 |
errno); |
|
839 |
con->drizzle->last_errno= errno; |
|
840 |
return DRIZZLE_RETURN_ERRNO; |
|
841 |
}
|
|
842 |
||
843 |
dret= _con_setsockopt(con); |
|
844 |
if (dret != DRIZZLE_RETURN_OK) |
|
845 |
{
|
|
846 |
con->drizzle->last_errno= errno; |
|
847 |
return dret; |
|
848 |
}
|
|
849 |
||
850 |
while (1) |
|
851 |
{
|
|
852 |
ret= connect(con->fd, con->addrinfo_next->ai_addr, |
|
853 |
con->addrinfo_next->ai_addrlen); |
|
854 |
||
855 |
#ifdef _WIN32
|
|
856 |
/*Mapping windows specific error codes to Posix*/
|
|
857 |
errno = WSAGetLastError(); |
|
858 |
switch(errno) { |
|
859 |
case WSAEINVAL: |
|
860 |
case WSAEALREADY: |
|
861 |
case WSAEWOULDBLOCK: |
|
862 |
errno = EINPROGRESS; |
|
863 |
break; |
|
864 |
default: |
|
865 |
break; |
|
866 |
}
|
|
867 |
#endif /* _WIN32 */ |
|
868 |
||
869 |
drizzle_log_crazy(con->drizzle, "connect return=%d errno=%d", ret, errno); |
|
870 |
||
871 |
if (ret == 0) |
|
872 |
{
|
|
873 |
con->addrinfo_next= NULL; |
|
874 |
break; |
|
875 |
}
|
|
876 |
||
877 |
if (errno == EAGAIN || errno == EINTR) |
|
878 |
continue; |
|
879 |
||
880 |
if (errno == EINPROGRESS) |
|
881 |
{
|
|
882 |
drizzle_state_pop(con); |
|
883 |
drizzle_state_push(con, drizzle_state_connecting); |
|
884 |
return DRIZZLE_RETURN_OK; |
|
885 |
}
|
|
886 |
||
887 |
if (errno == ECONNREFUSED || errno == ENETUNREACH || errno == ETIMEDOUT) |
|
888 |
{
|
|
889 |
con->addrinfo_next= con->addrinfo_next->ai_next; |
|
890 |
return DRIZZLE_RETURN_OK; |
|
891 |
}
|
|
892 |
||
893 |
drizzle_set_error(con->drizzle, "drizzle_state_connect", "connect:%d", |
|
894 |
errno); |
|
895 |
con->drizzle->last_errno= errno; |
|
896 |
return DRIZZLE_RETURN_ERRNO; |
|
897 |
}
|
|
898 |
||
899 |
drizzle_state_pop(con); |
|
900 |
return DRIZZLE_RETURN_OK; |
|
901 |
}
|
|
902 |
||
903 |
drizzle_return_t drizzle_state_connecting(drizzle_con_st *con) |
|
904 |
{
|
|
905 |
drizzle_return_t ret; |
|
906 |
||
907 |
drizzle_log_debug(con->drizzle, "drizzle_state_connecting"); |
|
908 |
||
909 |
while (1) |
|
910 |
{
|
|
911 |
if (con->revents & POLLOUT) |
|
912 |
{
|
|
913 |
drizzle_state_pop(con); |
|
914 |
return DRIZZLE_RETURN_OK; |
|
915 |
}
|
|
916 |
else if (con->revents & (POLLERR | POLLHUP | POLLNVAL)) |
|
917 |
{
|
|
918 |
con->revents= 0; |
|
919 |
drizzle_state_pop(con); |
|
920 |
drizzle_state_push(con, drizzle_state_connect); |
|
921 |
con->addrinfo_next= con->addrinfo_next->ai_next; |
|
922 |
return DRIZZLE_RETURN_OK; |
|
923 |
}
|
|
924 |
||
925 |
ret= drizzle_con_set_events(con, POLLOUT); |
|
926 |
if (ret != DRIZZLE_RETURN_OK) |
|
927 |
return ret; |
|
928 |
||
929 |
if (con->drizzle->options & DRIZZLE_NON_BLOCKING) |
|
930 |
return DRIZZLE_RETURN_IO_WAIT; |
|
931 |
||
932 |
ret= drizzle_con_wait(con->drizzle); |
|
933 |
if (ret != DRIZZLE_RETURN_OK) |
|
934 |
return ret; |
|
935 |
}
|
|
936 |
}
|
|
937 |
||
938 |
drizzle_return_t drizzle_state_read(drizzle_con_st *con) |
|
939 |
{
|
|
940 |
drizzle_return_t ret; |
|
941 |
ssize_t read_size; |
|
942 |
||
943 |
drizzle_log_debug(con->drizzle, "drizzle_state_read"); |
|
944 |
||
945 |
if (con->buffer_size == 0) |
|
946 |
con->buffer_ptr= con->buffer; |
|
947 |
else if ((con->buffer_ptr - con->buffer) > (DRIZZLE_MAX_BUFFER_SIZE / 2)) |
|
948 |
{
|
|
949 |
memmove(con->buffer, con->buffer_ptr, con->buffer_size); |
|
950 |
con->buffer_ptr= con->buffer; |
|
951 |
}
|
|
952 |
||
953 |
while (1) |
|
954 |
{
|
|
955 |
read_size = recv(con->fd, (char *)con->buffer_ptr + con->buffer_size, |
|
956 |
(size_t)DRIZZLE_MAX_BUFFER_SIZE - |
|
957 |
((size_t)(con->buffer_ptr - con->buffer) + |
|
958 |
con->buffer_size),0); |
|
959 |
#ifdef _WIN32
|
|
960 |
/*Get windows error codes and map it to Posix*/
|
|
961 |
errno = WSAGetLastError(); |
|
962 |
switch(errno) { |
|
963 |
case WSAEWOULDBLOCK: |
|
964 |
case 10057: |
|
965 |
errno = EAGAIN; |
|
966 |
break; |
|
967 |
default: |
|
968 |
break; |
|
969 |
}
|
|
970 |
#endif /* _WIN32 */ |
|
971 |
drizzle_log_crazy(con->drizzle, "read fd=%d return=%zd errno=%d", con->fd, |
|
972 |
read_size, errno); |
|
973 |
||
974 |
if (read_size == 0) |
|
975 |
{
|
|
976 |
drizzle_set_error(con->drizzle, "drizzle_state_read", |
|
977 |
"lost connection to server (EOF)"); |
|
978 |
return DRIZZLE_RETURN_LOST_CONNECTION; |
|
979 |
}
|
|
980 |
else if (read_size == -1) |
|
981 |
{
|
|
982 |
if (errno == EAGAIN) |
|
983 |
{
|
|
984 |
ret= drizzle_con_set_events(con, POLLIN); |
|
985 |
if (ret != DRIZZLE_RETURN_OK) |
|
986 |
return 0; |
|
987 |
||
988 |
if (con->drizzle->options & DRIZZLE_NON_BLOCKING) |
|
989 |
return DRIZZLE_RETURN_IO_WAIT; |
|
990 |
||
991 |
ret= drizzle_con_wait(con->drizzle); |
|
992 |
if (ret != DRIZZLE_RETURN_OK) |
|
993 |
return ret; |
|
994 |
||
995 |
continue; |
|
996 |
}
|
|
997 |
else if (errno == ECONNREFUSED) |
|
998 |
{
|
|
999 |
con->revents= 0; |
|
1000 |
drizzle_state_pop(con); |
|
1001 |
drizzle_state_push(con, drizzle_state_connect); |
|
1002 |
con->addrinfo_next= con->addrinfo_next->ai_next; |
|
1003 |
return DRIZZLE_RETURN_OK; |
|
1004 |
}
|
|
1005 |
else if (errno == EINTR) |
|
1006 |
continue; |
|
1007 |
else if (errno == EPIPE || errno == ECONNRESET) |
|
1008 |
{
|
|
1009 |
drizzle_set_error(con->drizzle, "drizzle_state_read", |
|
1010 |
"lost connection to server (%d)", errno); |
|
1011 |
return DRIZZLE_RETURN_LOST_CONNECTION; |
|
1012 |
}
|
|
1013 |
||
1014 |
drizzle_set_error(con->drizzle, "drizzle_state_read", "read:%d", errno); |
|
1015 |
con->drizzle->last_errno= errno; |
|
1016 |
return DRIZZLE_RETURN_ERRNO; |
|
1017 |
}
|
|
1018 |
||
1019 |
con->buffer_size+= (size_t)read_size; |
|
1020 |
break; |
|
1021 |
}
|
|
1022 |
||
1023 |
drizzle_state_pop(con);; |
|
1024 |
return DRIZZLE_RETURN_OK; |
|
1025 |
}
|
|
1026 |
||
1027 |
drizzle_return_t drizzle_state_write(drizzle_con_st *con) |
|
1028 |
{
|
|
1029 |
drizzle_return_t ret; |
|
1030 |
ssize_t write_size; |
|
1031 |
||
1032 |
drizzle_log_debug(con->drizzle, "drizzle_state_write"); |
|
1033 |
||
1034 |
while (con->buffer_size != 0) |
|
1035 |
{
|
|
1036 |
||
1877.1.5
by Andrew Hutchings
Fix min() usage for 32bit |
1037 |
write_size = send(con->fd,(char *) con->buffer_ptr, con->buffer_size, 0); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
1038 |
|
1039 |
drizzle_log_crazy(con->drizzle, "write fd=%d return=%zd errno=%d", con->fd, |
|
1040 |
write_size, errno); |
|
1041 |
||
1042 |
if (write_size == 0) |
|
1043 |
{
|
|
1044 |
drizzle_set_error(con->drizzle, "drizzle_state_write", |
|
1045 |
"lost connection to server (EOF)"); |
|
1046 |
return DRIZZLE_RETURN_LOST_CONNECTION; |
|
1047 |
}
|
|
1048 |
else if (write_size == -1) |
|
1049 |
{
|
|
1050 |
if (errno == EAGAIN) |
|
1051 |
{
|
|
1052 |
ret= drizzle_con_set_events(con, POLLOUT); |
|
1053 |
if (ret != DRIZZLE_RETURN_OK) |
|
1054 |
return ret; |
|
1055 |
||
1056 |
if (con->drizzle->options & DRIZZLE_NON_BLOCKING) |
|
1057 |
return DRIZZLE_RETURN_IO_WAIT; |
|
1058 |
||
1059 |
ret= drizzle_con_wait(con->drizzle); |
|
1060 |
if (ret != DRIZZLE_RETURN_OK) |
|
1061 |
return ret; |
|
1062 |
||
1063 |
continue; |
|
1064 |
}
|
|
1065 |
else if (errno == EINTR) |
|
1066 |
continue; |
|
1067 |
else if (errno == EPIPE || errno == ECONNRESET) |
|
1068 |
{
|
|
1069 |
drizzle_set_error(con->drizzle, "drizzle_state_write", |
|
1070 |
"lost connection to server (%d)", errno); |
|
1071 |
return DRIZZLE_RETURN_LOST_CONNECTION; |
|
1072 |
}
|
|
1073 |
||
1074 |
drizzle_set_error(con->drizzle, "drizzle_state_write", "write:%d", errno); |
|
1075 |
con->drizzle->last_errno= errno; |
|
1076 |
return DRIZZLE_RETURN_ERRNO; |
|
1077 |
}
|
|
1078 |
||
1079 |
con->buffer_ptr+= write_size; |
|
1080 |
con->buffer_size-= (size_t)write_size; |
|
1081 |
if (con->buffer_size == 0) |
|
1082 |
break; |
|
1083 |
}
|
|
1084 |
||
1085 |
con->buffer_ptr= con->buffer; |
|
1086 |
||
1087 |
drizzle_state_pop(con); |
|
1088 |
return DRIZZLE_RETURN_OK; |
|
1089 |
}
|
|
1090 |
||
1091 |
drizzle_return_t drizzle_state_listen(drizzle_con_st *con) |
|
1092 |
{
|
|
1093 |
char host[NI_MAXHOST]; |
|
1094 |
char port[NI_MAXSERV]; |
|
1095 |
int ret; |
|
1096 |
int fd; |
|
1097 |
int opt; |
|
1098 |
drizzle_con_st *new_con; |
|
1099 |
||
1100 |
for (; con->addrinfo_next != NULL; |
|
1101 |
con->addrinfo_next= con->addrinfo_next->ai_next) |
|
1102 |
{
|
|
1103 |
ret= getnameinfo(con->addrinfo_next->ai_addr, |
|
1104 |
con->addrinfo_next->ai_addrlen, host, NI_MAXHOST, port, |
|
1105 |
NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV); |
|
1106 |
if (ret != 0) |
|
1107 |
{
|
|
1108 |
drizzle_set_error(con->drizzle, "drizzle_state_listen", "getnameinfo:%s", |
|
1109 |
gai_strerror(ret)); |
|
1110 |
return DRIZZLE_RETURN_GETADDRINFO; |
|
1111 |
}
|
|
1112 |
||
1113 |
/* Call to socket() can fail for some getaddrinfo results, try another. */
|
|
1114 |
fd= socket(con->addrinfo_next->ai_family, con->addrinfo_next->ai_socktype, |
|
1115 |
con->addrinfo_next->ai_protocol); |
|
1116 |
if (fd == -1) |
|
1117 |
{
|
|
1118 |
drizzle_log_info(con->drizzle, "could not listen on %s:%s", host, port); |
|
1119 |
drizzle_set_error(con->drizzle, "drizzle_state_listen", "socket:%d", |
|
1120 |
errno); |
|
1121 |
continue; |
|
1122 |
}
|
|
1123 |
||
1124 |
opt= 1; |
|
1125 |
#ifdef _WIN32
|
|
1126 |
ret= setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,(const char*) &opt, sizeof(opt)); |
|
1127 |
#else
|
|
1128 |
ret= setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
|
1129 |
#endif /* _WIN32 */ |
|
1130 |
if (ret == -1) |
|
1131 |
{
|
|
1132 |
close(fd); |
|
1133 |
drizzle_set_error(con->drizzle, "drizzle_state_listen", "setsockopt:%d", |
|
1134 |
errno); |
|
1135 |
return DRIZZLE_RETURN_ERRNO; |
|
1136 |
}
|
|
1137 |
||
1138 |
ret= bind(fd, con->addrinfo_next->ai_addr, con->addrinfo_next->ai_addrlen); |
|
1139 |
if (ret == -1) |
|
1140 |
{
|
|
1141 |
close(fd); |
|
1142 |
drizzle_set_error(con->drizzle, "drizzle_state_listen", "bind:%d", errno); |
|
1143 |
if (errno == EADDRINUSE) |
|
1144 |
{
|
|
1145 |
if (con->fd == -1) |
|
1146 |
{
|
|
1147 |
drizzle_log_info(con->drizzle, "could not listen on %s:%s", host, |
|
1148 |
port); |
|
1149 |
}
|
|
1150 |
||
1151 |
continue; |
|
1152 |
}
|
|
1153 |
||
1154 |
return DRIZZLE_RETURN_ERRNO; |
|
1155 |
}
|
|
1156 |
||
1157 |
if (listen(fd, con->backlog) == -1) |
|
1158 |
{
|
|
1159 |
close(fd); |
|
1160 |
drizzle_set_error(con->drizzle, "drizzle_state_listen", "listen:%d", |
|
1161 |
errno); |
|
1162 |
return DRIZZLE_RETURN_ERRNO; |
|
1163 |
}
|
|
1164 |
||
1165 |
if (con->fd == -1) |
|
1166 |
{
|
|
1167 |
con->fd= fd; |
|
1168 |
new_con= con; |
|
1169 |
}
|
|
1170 |
else
|
|
1171 |
{
|
|
1172 |
new_con= drizzle_con_clone(con->drizzle, NULL, con); |
|
1173 |
if (new_con == NULL) |
|
1174 |
{
|
|
1175 |
close(fd); |
|
1176 |
return DRIZZLE_RETURN_MEMORY; |
|
1177 |
}
|
|
1178 |
||
1179 |
new_con->fd= fd; |
|
1180 |
}
|
|
1181 |
||
1182 |
/* Wait for read events on the listening socket. */
|
|
1183 |
ret= drizzle_con_set_events(new_con, POLLIN); |
|
1184 |
if (ret != DRIZZLE_RETURN_OK) |
|
1185 |
{
|
|
1186 |
drizzle_con_free(new_con); |
|
1187 |
return ret; |
|
1188 |
}
|
|
1189 |
||
1190 |
drizzle_log_info(con->drizzle, "listening on %s:%s", host, port); |
|
1191 |
}
|
|
1192 |
||
1193 |
/* Report last socket() error if we couldn't find an address to bind. */
|
|
1194 |
if (con->fd == -1) |
|
1195 |
return DRIZZLE_RETURN_ERRNO; |
|
1196 |
||
1197 |
drizzle_state_pop(con); |
|
1198 |
return DRIZZLE_RETURN_OK; |
|
1199 |
}
|
|
1200 |
||
1201 |
/*
|
|
1202 |
* Static Definitions
|
|
1203 |
*/
|
|
1204 |
||
1205 |
static drizzle_return_t _con_setsockopt(drizzle_con_st *con) |
|
1206 |
{
|
|
1207 |
int ret; |
|
1208 |
struct linger linger; |
|
1209 |
struct timeval waittime; |
|
1210 |
||
1211 |
ret= 1; |
|
1212 |
||
1213 |
#ifdef _WIN32
|
|
1214 |
ret= setsockopt(con->fd, IPPROTO_TCP, TCP_NODELAY, (const char*)&ret, |
|
1215 |
(socklen_t)sizeof(int)); |
|
1216 |
#else
|
|
1217 |
ret= setsockopt(con->fd, IPPROTO_TCP, TCP_NODELAY, &ret, |
|
1218 |
(socklen_t)sizeof(int)); |
|
1219 |
#endif /* _WIN32 */ |
|
1220 |
||
1221 |
if (ret == -1 && errno != EOPNOTSUPP) |
|
1222 |
{
|
|
1223 |
drizzle_set_error(con->drizzle, "_con_setsockopt", |
|
1224 |
"setsockopt:TCP_NODELAY:%d", errno); |
|
1225 |
return DRIZZLE_RETURN_ERRNO; |
|
1226 |
}
|
|
1227 |
||
1228 |
linger.l_onoff= 1; |
|
1229 |
linger.l_linger= DRIZZLE_DEFAULT_SOCKET_TIMEOUT; |
|
1230 |
||
1231 |
#ifdef _WIN32
|
|
1232 |
ret= setsockopt(con->fd, SOL_SOCKET, SO_LINGER, (const char*)&linger, |
|
1233 |
(socklen_t)sizeof(struct linger)); |
|
1234 |
#else
|
|
1235 |
ret= setsockopt(con->fd, SOL_SOCKET, SO_LINGER, &linger, |
|
1236 |
(socklen_t)sizeof(struct linger)); |
|
1237 |
#endif /* _WIN32 */ |
|
1238 |
||
1239 |
if (ret == -1) |
|
1240 |
{
|
|
1241 |
drizzle_set_error(con->drizzle, "_con_setsockopt", |
|
1242 |
"setsockopt:SO_LINGER:%d", errno); |
|
1243 |
return DRIZZLE_RETURN_ERRNO; |
|
1244 |
}
|
|
1245 |
||
1246 |
waittime.tv_sec= DRIZZLE_DEFAULT_SOCKET_TIMEOUT; |
|
1247 |
waittime.tv_usec= 0; |
|
1248 |
||
1249 |
#ifdef _WIN32
|
|
1250 |
ret= setsockopt(con->fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&waittime, |
|
1251 |
(socklen_t)sizeof(struct timeval)); |
|
1252 |
#else
|
|
1253 |
ret= setsockopt(con->fd, SOL_SOCKET, SO_SNDTIMEO, &waittime, |
|
1254 |
(socklen_t)sizeof(struct timeval)); |
|
1255 |
#endif /* _WIN32 */ |
|
1256 |
||
1257 |
if (ret == -1 && errno != ENOPROTOOPT) |
|
1258 |
{
|
|
1259 |
drizzle_set_error(con->drizzle, "_con_setsockopt", |
|
1260 |
"setsockopt:SO_SNDTIMEO:%d", errno); |
|
1261 |
return DRIZZLE_RETURN_ERRNO; |
|
1262 |
}
|
|
1263 |
||
1264 |
#ifdef _WIN32
|
|
1265 |
ret= setsockopt(con->fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&waittime, |
|
1266 |
(socklen_t)sizeof(struct timeval)); |
|
1267 |
#else
|
|
1268 |
ret= setsockopt(con->fd, SOL_SOCKET, SO_RCVTIMEO, &waittime, |
|
1269 |
(socklen_t)sizeof(struct timeval)); |
|
1270 |
#endif /* _WIN32 */ |
|
1271 |
||
1272 |
if (ret == -1 && errno != ENOPROTOOPT) |
|
1273 |
{
|
|
1274 |
drizzle_set_error(con->drizzle, "_con_setsockopt", |
|
1275 |
"setsockopt:SO_RCVTIMEO:%d", errno); |
|
1276 |
return DRIZZLE_RETURN_ERRNO; |
|
1277 |
}
|
|
1278 |
||
1279 |
ret= DRIZZLE_DEFAULT_SOCKET_SEND_SIZE; |
|
1280 |
#ifdef _WIN32
|
|
1281 |
ret= setsockopt(con->fd, SOL_SOCKET, SO_SNDBUF, (const char*)&ret, (socklen_t)sizeof(int)); |
|
1282 |
#else
|
|
1283 |
ret= setsockopt(con->fd, SOL_SOCKET, SO_SNDBUF, &ret, (socklen_t)sizeof(int)); |
|
1284 |
#endif /* _WIN32 */ |
|
1285 |
if (ret == -1) |
|
1286 |
{
|
|
1287 |
drizzle_set_error(con->drizzle, "_con_setsockopt", |
|
1288 |
"setsockopt:SO_SNDBUF:%d", errno); |
|
1289 |
return DRIZZLE_RETURN_ERRNO; |
|
1290 |
}
|
|
1291 |
||
1292 |
ret= DRIZZLE_DEFAULT_SOCKET_RECV_SIZE; |
|
1293 |
#ifdef _WIN32
|
|
1294 |
ret= setsockopt(con->fd, SOL_SOCKET, SO_RCVBUF, (const char*)&ret, (socklen_t)sizeof(int)); |
|
1295 |
#else
|
|
1296 |
ret= setsockopt(con->fd, SOL_SOCKET, SO_RCVBUF, &ret, (socklen_t)sizeof(int)); |
|
1297 |
#endif /* _WIN32 */ |
|
1298 |
if (ret == -1) |
|
1299 |
{
|
|
1300 |
drizzle_set_error(con->drizzle, "_con_setsockopt", |
|
1301 |
"setsockopt:SO_RCVBUF:%d", errno); |
|
1302 |
return DRIZZLE_RETURN_ERRNO; |
|
1303 |
}
|
|
1304 |
||
1305 |
#if defined (_WIN32)
|
|
1306 |
unsigned long asyncmode = 1; |
|
1307 |
ioctlsocket(con->fd, FIONBIO, &asyncmode); |
|
1308 |
#else
|
|
1309 |
ret= fcntl(con->fd, F_GETFL, 0); |
|
1310 |
if (ret == -1) |
|
1311 |
{
|
|
1312 |
drizzle_set_error(con->drizzle, "_con_setsockopt", "fcntl:F_GETFL:%d", |
|
1313 |
errno); |
|
1314 |
return DRIZZLE_RETURN_ERRNO; |
|
1315 |
}
|
|
1316 |
||
1317 |
ret= fcntl(con->fd, F_SETFL, ret | O_NONBLOCK); |
|
1318 |
if (ret == -1) |
|
1319 |
{
|
|
1320 |
drizzle_set_error(con->drizzle, "_con_setsockopt", "fcntl:F_SETFL:%d", |
|
1321 |
errno); |
|
1322 |
return DRIZZLE_RETURN_ERRNO; |
|
1323 |
}
|
|
1324 |
#endif
|
|
1325 |
||
1326 |
return DRIZZLE_RETURN_OK; |
|
1327 |
}
|