pool.h
/* TDSPool - Connection pooling for TDS based databases
* Copyright (C) 2001 Brian Bruns
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef _pool_h_
#define _pool_h_
#include <assert.h>
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
/*
* POSIX says fd_set type may be defined in either sys/select.h or sys/time.h.
*/
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <freetds/tds.h>
#include <freetds/utils/dlist.h>
/* defines */
#define PGSIZ 2048
#define BLOCKSIZ 512
#define MAX_POOL_USERS 1024
/* enums and typedefs */
typedef enum
{
TDS_SRV_WAIT, /* if no members are free wait */
TDS_SRV_QUERY,
} TDS_USER_STATE;
/* forward declaration */
typedef struct tds_pool_event TDS_POOL_EVENT;
typedef struct tds_pool_socket TDS_POOL_SOCKET;
typedef struct tds_pool_member TDS_POOL_MEMBER;
typedef struct tds_pool_user TDS_POOL_USER;
typedef struct tds_pool TDS_POOL;
typedef void (*TDS_POOL_EXECUTE)(TDS_POOL_EVENT *event);
struct tds_pool_event
{
TDS_POOL_EVENT *next;
TDS_POOL_EXECUTE execute;
};
struct tds_pool_socket
{
TDSSOCKET *tds;
bool poll_recv;
bool poll_send;
};
struct tds_pool_user
{
TDS_POOL_SOCKET sock;
DLIST_FIELDS(dlist_user_item);
TDSLOGIN *login;
TDS_USER_STATE user_state;
TDS_POOL_MEMBER *assigned_member;
};
struct tds_pool_member
{
TDS_POOL_SOCKET sock;
DLIST_FIELDS(dlist_member_item);
bool doing_async;
time_t last_used_tm;
TDS_POOL_USER *current_user;
};
#define DLIST_PREFIX dlist_member
#define DLIST_LIST_TYPE dlist_members
#define DLIST_ITEM_TYPE TDS_POOL_MEMBER
#include <freetds/utils/dlist.tmpl.h>
#define DLIST_PREFIX dlist_user
#define DLIST_LIST_TYPE dlist_users
#define DLIST_ITEM_TYPE TDS_POOL_USER
#include <freetds/utils/dlist.tmpl.h>
struct tds_pool
{
char *name;
char *user;
char *password;
char *server;
char *database;
char *server_user;
char *server_password;
int port;
int max_member_age; /* in seconds */
int min_open_conn;
int max_open_conn;
tds_mutex events_mtx;
TDS_SYS_SOCKET listen_fd;
TDS_SYS_SOCKET wakeup_fd;
TDS_SYS_SOCKET event_fd;
TDS_POOL_EVENT *events;
int num_active_members;
dlist_members active_members;
dlist_members idle_members;
dlist_users waiters;
int num_users;
dlist_users users;
TDSCONTEXT *ctx;
unsigned long user_logins;
unsigned long member_logins;
};
/* prototypes */
/* member.c */
int pool_process_members(TDS_POOL * pool, fd_set * rfds, fd_set * wfds);
TDS_POOL_MEMBER *pool_assign_idle_member(TDS_POOL * pool, TDS_POOL_USER *user);
void pool_mbr_init(TDS_POOL * pool);
void pool_mbr_destroy(TDS_POOL * pool);
void pool_free_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
void pool_assign_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr, TDS_POOL_USER *puser);
void pool_deassign_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
void pool_reset_member(TDS_POOL *pool, TDS_POOL_MEMBER * pmbr);
bool pool_packet_read(TDSSOCKET * tds);
/* user.c */
void pool_process_users(TDS_POOL * pool, fd_set * rfds, fd_set * wfds);
void pool_user_init(TDS_POOL * pool);
void pool_user_destroy(TDS_POOL * pool);
TDS_POOL_USER *pool_user_create(TDS_POOL * pool, TDS_SYS_SOCKET s);
void pool_free_user(TDS_POOL * pool, TDS_POOL_USER * puser);
void pool_user_query(TDS_POOL * pool, TDS_POOL_USER * puser);
bool pool_user_send_login_ack(TDS_POOL * pool, TDS_POOL_USER * puser);
void pool_user_finish_login(TDS_POOL * pool, TDS_POOL_USER * puser);
/* util.c */
void dump_login(TDSLOGIN * login);
void pool_event_add(TDS_POOL *pool, TDS_POOL_EVENT *ev, TDS_POOL_EXECUTE execute);
int pool_write(TDS_SYS_SOCKET sock, const void *buf, size_t len);
bool pool_write_data(TDS_POOL_SOCKET *from, TDS_POOL_SOCKET *to);
/* config.c */
bool pool_read_conf_files(const char *path, const char *poolname, TDS_POOL * pool, char **err);
#endif
tds_start_query
void tds_start_query(TDSSOCKET *tds, unsigned char packet_type)
Start query packet of a given type.
Definition: query.c:332
tds_dstr_init
static void tds_dstr_init(DSTR *s)
init a string with empty
Definition: string.h:53
tds_dstr_empty
#define tds_dstr_empty(s)
Make a string empty.
Definition: string.h:91
tds_pool
Definition: pool.h:105
tds_set_state
TDS_STATE tds_set_state(TDSSOCKET *tds, TDS_STATE state)
Set state of TDS connection, with logging and checking.
Definition: util.c:58
tds_dstr_cstr
static const char * tds_dstr_cstr(const DSTR *s)
Returns a C version (NUL terminated string) of dstr.
Definition: string.h:78
tds_login::password
DSTR password
password of account login
Definition: tds.h:535
tds_locale
Definition: tds.h:582
tds_socket::in_pos
unsigned in_pos
current position in in_buf
Definition: tds.h:1192
tds.h
Main include file for libtds.
tds_connection::env
TDSENV env
environment is shared between all sessions
Definition: tds.h:1101
tds_read_conf_section
bool tds_read_conf_section(FILE *in, const char *section, TDSCONFPARSE tds_conf_parse, void *param)
Read a section of configuration file (INI style file)
Definition: config.c:503
tds_dstr_isempty
static int tds_dstr_isempty(const DSTR *s)
test if string is empty
Definition: string.h:60
CONNECT_EVENT
Definition: member.c:413
tds_lookup_host
struct addrinfo * tds_lookup_host(const char *servername)
Get the IP address for a hostname.
Definition: config.c:987
tds_lookup_port
static int tds_lookup_port(const char *portname)
Given a portname lookup the port.
Definition: config.c:1026
tds_socket_set_nonblocking
int tds_socket_set_nonblocking(TDS_SYS_SOCKET sock)
Set socket to non-blocking.
Definition: net.c:168
tds_init_login
TDSLOGIN * tds_init_login(TDSLOGIN *login, TDSLOCALE *locale)
Initialize login structure with locale information and other stuff for connection.
Definition: mem.c:800
parse_server_name_for_port
static bool parse_server_name_for_port(TDSLOGIN *connection, TDSLOGIN *login, bool update_server)
Check the server name to find port info first Warning: connection-> & login-> are all modified when n...
Definition: config.c:1293
tds_pool_member
Definition: pool.h:86
tds_fix_login
void tds_fix_login(TDSLOGIN *login)
Fix configuration after reading it.
Definition: config.c:285
tds_socket::in_buf
unsigned char * in_buf
Input buffer.
Definition: tds.h:1177
tds_close_socket
void tds_close_socket(TDSSOCKET *tds)
Close current socket.
Definition: net.c:548
DSNINFO::login
TDSLOGIN * login
everything else
Definition: winsetup.c:66
tds_config_verstr
TDS_USMALLINT * tds_config_verstr(const char *tdsver, TDSLOGIN *login)
Set TDS version from given string.
Definition: config.c:923
tds_read_config_info
TDSLOGIN * tds_read_config_info(TDSSOCKET *tds, TDSLOGIN *login, TDSLOCALE *locale)
tds_read_config_info() will fill the tds connection structure based on configuration information gath...
Definition: config.c:138
tds_pool_socket
Definition: pool.h:70
tds_set_interfaces_file_loc
TDSRET tds_set_interfaces_file_loc(const char *interf)
Set the full name of interface file.
Definition: config.c:961
tds_login::server_realm_name
DSTR server_realm_name
server realm name (in freetds.conf)
Definition: tds.h:527
select_info
Definition: main.c:209
poll.h
Provide poll call where missing.
search_interface_file
static int search_interface_file(TDSLOGIN *login, const char *dir, const char *file, const char *host)
Open and read the file 'file' searching for a logical server by the name of 'host'.
Definition: config.c:1064
tds_pool::waiters
dlist_users waiters
users in wait state
Definition: pool.h:128
DSNINFO::dsn
DSTR dsn
edited name of the data source
Definition: winsetup.c:65
tds_process_cancel
TDSRET tds_process_cancel(TDSSOCKET *tds)
Definition: token.c:2554
tds_set_server
bool tds_set_server(TDSLOGIN *tds_login, const char *server) TDS_WUR
Set the servername in a TDSLOGIN structure.
Definition: login.c:129
tds_process_simple_query
TDSRET tds_process_simple_query(TDSSOCKET *tds)
Process results for simple query as "SET TEXTSIZE" or "USE dbname" If the statement returns results,...
Definition: token.c:881
tds_dstr_copyn
DSTR * tds_dstr_copyn(DSTR *s, const char *src, size_t length)
Set string to a given buffer of characters.
Definition: tdsstring.c:77
tds_dstr_dup
DSTR * tds_dstr_dup(DSTR *s, const DSTR *src)
Duplicate a string from another dynamic string.
Definition: tdsstring.c:134
DSNINFO
Definition: winsetup.c:63
tds_login::db_filename
DSTR db_filename
database filename to attach (MSSQL)
Definition: tds.h:529
tds_dstr_set
DSTR * tds_dstr_set(DSTR *s, char *src)
set a string from another buffer.
Definition: tdsstring.c:107
tds_pool_event
Definition: pool.h:64
tds_socket::out_flag
unsigned char out_flag
output buffer type
Definition: tds.h:1196
tds_socket::in_cancel
volatile unsigned char in_cancel
indicate we are waiting a cancel reply; discard tokens till acknowledge; 1 mean we have to send cance...
Definition: tds.h:1248
tds_read_conf_file
bool tds_read_conf_file(TDSLOGIN *login, const char *server)
Read configuration info for given server return 0 on error.
Definition: config.c:346
tds_login::bulk_copy
unsigned int bulk_copy
if bulk copy should be enabled
Definition: tds.h:556
tds_login::new_password
DSTR new_password
new password to set (TDS 7.2+)
Definition: tds.h:536
DSNINFO::origdsn
DSTR origdsn
original name of the data source
Definition: winsetup.c:64
tds_dstr
Structure to hold a string.
Definition: string.h:36
tds_login::tds_version
TDS_USMALLINT tds_version
TDS version.
Definition: tds.h:520
tds_login::ip_addrs
struct addrinfo * ip_addrs
ip(s) of server
Definition: tds.h:546
TDS_WRITING
@ TDS_WRITING
client is writing data
Definition: tds.h:791
tdsvername_t
Definition: config.c:905
tds_read_interfaces
static int tds_read_interfaces(const char *server, TDSLOGIN *login)
Try to find the IP number and port for a (possibly) logical server name.
Definition: config.c:1187
tds_login::server_name
DSTR server_name
server name (in freetds.conf)
Definition: tds.h:518
tds_socket::in_len
unsigned in_len
input buffer length
Definition: tds.h:1194
tdsdump_open
int tdsdump_open(const char *filename)
Create and truncate a human readable dump file for the TDS traffic.
Definition: log.c:131
tds_dstr_free
void tds_dstr_free(DSTR *s)
free string
Definition: tdsstring.c:62
tds_dstr_copy
DSTR * tds_dstr_copy(DSTR *s, const char *src)
copy a string from another
Definition: tdsstring.c:122
tds_put_string
int tds_put_string(TDSSOCKET *tds, const char *s, int len)
Output a string to wire automatic translate string to unicode if needed.
Definition: write.c:97
tdserror
int tdserror(const TDSCONTEXT *tds_ctx, TDSSOCKET *tds, int msgno, int errnum)
Call the client library's error handler (for library-generated errors only)
Definition: util.c:321
tds_login::port
int port
port of database service
Definition: tds.h:519
tdsdump_dump_buf
void tdsdump_dump_buf(const char *file, unsigned int level_line, const char *msg, const void *buf, size_t length)
Dump the contents of data into the log file in a human readable format.
Definition: log.c:293
tds_login::user_name
DSTR user_name
account for login
Definition: tds.h:534
tds_env::database
char * database
database name
Definition: tds.h:970
tds_login::crlfile
DSTR crlfile
certificate revocation file
Definition: tds.h:531
tds_pool_user
Definition: pool.h:77
tds_socket
Information for a server connection.
Definition: tds.h:1163
DSTR_INITIALIZER
#define DSTR_INITIALIZER
Initializer, used to initialize string like in the following example.
Definition: string.h:49
tds_login::server_spn
DSTR server_spn
server SPN (in freetds.conf)
Definition: tds.h:528
conf_params
Definition: config.c:55
tds_flush_packet
TDSRET tds_flush_packet(TDSSOCKET *tds)
Flush packet to server.
Definition: write.c:224
tds_login::server_charset
DSTR server_charset
charset of server e.g.
Definition: tds.h:523
TDS_PENDING
@ TDS_PENDING
cilent is waiting for data
Definition: tds.h:793
tds_context
Definition: tds.h:1029
tds_compiletime_settings
A structure to hold all the compile-time settings.
Definition: tds.h:82
tds_login
Definition: tds.h:517
tds_get_compiletime_settings
const TDS_COMPILETIME_SETTINGS * tds_get_compiletime_settings(void)
Return a structure capturing the compile-time settings provided to the configure script.
Definition: config.c:1344
tds_login::cafile
DSTR cafile
certificate authorities file
Definition: tds.h:530
tdsdump_close
void tdsdump_close(void)
Close the TDS dump log file.
Definition: log.c:212
tds_get_home_file
char * tds_get_home_file(const char *file)
Return filename from HOME directory.
Definition: config.c:325
tds_dstr_zero
void tds_dstr_zero(DSTR *s)
clear all string filling with zeroes (mainly for security reason)
Definition: tdsstring.c:55
tdsdump_log
void tdsdump_log(const char *file, unsigned int level_line, const char *fmt,...)
Write a message to the debug log.
Definition: log.c:396