1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
/* Various helper functions not intended to be part of a public API */
23
#include "libdrizzle_priv.h"
27
const char _dig_vec_upper[] =
28
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
29
const char _dig_vec_lower[] =
30
"0123456789abcdefghijklmnopqrstuvwxyz";
32
const char *unknown_sqlstate= "HY000";
33
const char *not_error_sqlstate= "00000";
34
const char *cant_connect_sqlstate= "08001";
36
const char * sqlstate_get_unknown(void)
38
return unknown_sqlstate;
41
const char * sqlstate_get_not_error(void)
43
return not_error_sqlstate;
46
const char * sqlstate_get_cant_connect(void)
48
return cant_connect_sqlstate;
51
/****************************************************************************
52
A modified version of connect(). connect_with_timeout() allows you to specify
53
a timeout value, in seconds, that we should wait until we
54
derermine we can't connect to a particular host. If timeout is 0,
55
connect_with_timeout() will behave exactly like connect().
57
Base version coded by Steve Bernacki, Jr. <steve@navinet.net>
58
*****************************************************************************/
60
int connect_with_timeout(int fd, const struct sockaddr *name, uint32_t namelen, int32_t timeout)
62
int flags, res, s_err;
65
If they passed us a timeout of zero, we should behave
66
exactly like the normal connect() call does.
70
return connect(fd, (struct sockaddr*) name, namelen);
72
flags = fcntl(fd, F_GETFL, 0); /* Set socket to not block */
74
fcntl(fd, F_SETFL, flags | O_NONBLOCK); /* and save the flags.. */
77
res= connect(fd, (struct sockaddr*) name, namelen);
78
s_err= errno; /* Save the error... */
79
fcntl(fd, F_SETFL, flags);
80
if ((res != 0) && (s_err != EINPROGRESS))
82
errno= s_err; /* Restore it */
85
if (res == 0) /* Connected quickly! */
88
return wait_for_data(fd, timeout);
92
Wait up to timeout seconds for a connection to be established.
94
We prefer to do this with poll() as there is no limitations with this.
95
If not, we will use select()
98
int wait_for_data(int fd, int32_t timeout)
104
ufds.events= POLLIN | POLLPRI;
105
if (!(res= poll(&ufds, 1, (int) timeout*1000)))
110
if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI)) || (ufds.revents & POLLHUP))