00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef LIBPQ_INT_H
00021 #define LIBPQ_INT_H
00022
00023
00024 #include "postgres_fe.h"
00025 #include "libpq-events.h"
00026
00027 #include <time.h>
00028 #include <sys/types.h>
00029 #ifndef WIN32
00030 #include <sys/time.h>
00031 #endif
00032
00033 #ifdef ENABLE_THREAD_SAFETY
00034 #ifdef WIN32
00035 #include "pthread-win32.h"
00036 #else
00037 #include <pthread.h>
00038 #endif
00039 #include <signal.h>
00040 #endif
00041
00042
00043 #include "getaddrinfo.h"
00044 #include "libpq/pqcomm.h"
00045
00046 #include "pqexpbuffer.h"
00047
00048 #ifdef ENABLE_GSS
00049 #if defined(HAVE_GSSAPI_H)
00050 #include <gssapi.h>
00051 #else
00052 #include <gssapi/gssapi.h>
00053 #endif
00054 #endif
00055
00056 #ifdef ENABLE_SSPI
00057 #define SECURITY_WIN32
00058 #if defined(WIN32) && !defined(WIN32_ONLY_COMPILER)
00059 #include <ntsecapi.h>
00060 #endif
00061 #include <security.h>
00062 #undef SECURITY_WIN32
00063
00064 #ifndef ENABLE_GSS
00065
00066
00067
00068 typedef struct
00069 {
00070 void *value;
00071 int length;
00072 } gss_buffer_desc;
00073 #endif
00074 #endif
00075
00076 #ifdef USE_SSL
00077 #include <openssl/ssl.h>
00078 #include <openssl/err.h>
00079
00080 #if (SSLEAY_VERSION_NUMBER >= 0x00907000L) && !defined(OPENSSL_NO_ENGINE)
00081 #define USE_SSL_ENGINE
00082 #endif
00083 #endif
00084
00085
00086
00087
00088 #define CMDSTATUS_LEN 64
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 typedef union pgresult_data PGresult_data;
00104
00105 union pgresult_data
00106 {
00107 PGresult_data *next;
00108 char space[1];
00109 };
00110
00111
00112 typedef struct pgresParamDesc
00113 {
00114 Oid typid;
00115 } PGresParamDesc;
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 #define NULL_LEN (-1)
00136
00137 typedef struct pgresAttValue
00138 {
00139 int len;
00140 char *value;
00141 } PGresAttValue;
00142
00143
00144 typedef struct pgMessageField
00145 {
00146 struct pgMessageField *next;
00147 char code;
00148 char contents[1];
00149 } PGMessageField;
00150
00151
00152 typedef struct
00153 {
00154 PQnoticeReceiver noticeRec;
00155 void *noticeRecArg;
00156 PQnoticeProcessor noticeProc;
00157 void *noticeProcArg;
00158 } PGNoticeHooks;
00159
00160 typedef struct PGEvent
00161 {
00162 PGEventProc proc;
00163 char *name;
00164 void *passThrough;
00165 void *data;
00166 bool resultInitialized;
00167 } PGEvent;
00168
00169 struct pg_result
00170 {
00171 int ntups;
00172 int numAttributes;
00173 PGresAttDesc *attDescs;
00174 PGresAttValue **tuples;
00175
00176 int tupArrSize;
00177 int numParameters;
00178 PGresParamDesc *paramDescs;
00179 ExecStatusType resultStatus;
00180 char cmdStatus[CMDSTATUS_LEN];
00181 int binary;
00182
00183
00184
00185
00186
00187
00188 PGNoticeHooks noticeHooks;
00189 PGEvent *events;
00190 int nEvents;
00191 int client_encoding;
00192
00193
00194
00195
00196
00197
00198 char *errMsg;
00199 PGMessageField *errFields;
00200
00201
00202 char null_field[1];
00203
00204
00205
00206
00207
00208
00209 PGresult_data *curBlock;
00210 int curOffset;
00211 int spaceLeft;
00212 };
00213
00214
00215 typedef enum
00216 {
00217 PGASYNC_IDLE,
00218 PGASYNC_BUSY,
00219 PGASYNC_READY,
00220 PGASYNC_COPY_IN,
00221 PGASYNC_COPY_OUT,
00222 PGASYNC_COPY_BOTH
00223 } PGAsyncStatusType;
00224
00225
00226 typedef enum
00227 {
00228 PGQUERY_SIMPLE,
00229 PGQUERY_EXTENDED,
00230 PGQUERY_PREPARE,
00231 PGQUERY_DESCRIBE
00232 } PGQueryClass;
00233
00234
00235
00236 typedef enum
00237 {
00238 SETENV_STATE_CLIENT_ENCODING_SEND,
00239 SETENV_STATE_CLIENT_ENCODING_WAIT,
00240 SETENV_STATE_OPTION_SEND,
00241 SETENV_STATE_OPTION_WAIT,
00242 SETENV_STATE_QUERY1_SEND,
00243 SETENV_STATE_QUERY1_WAIT,
00244 SETENV_STATE_QUERY2_SEND,
00245 SETENV_STATE_QUERY2_WAIT,
00246 SETENV_STATE_IDLE
00247 } PGSetenvStatusType;
00248
00249
00250 typedef struct PQEnvironmentOption
00251 {
00252 const char *envName,
00253 *pgName;
00254 } PQEnvironmentOption;
00255
00256
00257 typedef struct pgParameterStatus
00258 {
00259 struct pgParameterStatus *next;
00260 char *name;
00261 char *value;
00262
00263 } pgParameterStatus;
00264
00265
00266 typedef struct pgLobjfuncs
00267 {
00268 Oid fn_lo_open;
00269 Oid fn_lo_close;
00270 Oid fn_lo_creat;
00271 Oid fn_lo_create;
00272 Oid fn_lo_unlink;
00273 Oid fn_lo_lseek;
00274 Oid fn_lo_lseek64;
00275 Oid fn_lo_tell;
00276 Oid fn_lo_tell64;
00277 Oid fn_lo_truncate;
00278 Oid fn_lo_truncate64;
00279 Oid fn_lo_read;
00280 Oid fn_lo_write;
00281 } PGlobjfuncs;
00282
00283
00284
00285
00286
00287
00288 typedef struct pgDataValue
00289 {
00290 int len;
00291 const char *value;
00292 } PGdataValue;
00293
00294
00295
00296
00297
00298 struct pg_conn
00299 {
00300
00301 char *pghost;
00302 char *pghostaddr;
00303
00304
00305 char *pgport;
00306 char *pgunixsocket;
00307
00308
00309 char *pgtty;
00310
00311 char *connect_timeout;
00312 char *client_encoding_initial;
00313 char *pgoptions;
00314 char *appname;
00315 char *fbappname;
00316 char *dbName;
00317 char *replication;
00318 char *pguser;
00319 char *pgpass;
00320 char *keepalives;
00321 char *keepalives_idle;
00322 char *keepalives_interval;
00323
00324 char *keepalives_count;
00325
00326 char *sslmode;
00327 char *sslcompression;
00328 char *sslkey;
00329 char *sslcert;
00330 char *sslrootcert;
00331 char *sslcrl;
00332 char *requirepeer;
00333
00334 #if defined(KRB5) || defined(ENABLE_GSS) || defined(ENABLE_SSPI)
00335 char *krbsrvname;
00336 #endif
00337
00338
00339 FILE *Pfdebug;
00340
00341
00342 PGNoticeHooks noticeHooks;
00343
00344
00345 PGEvent *events;
00346 int nEvents;
00347 int eventArraySize;
00348
00349
00350 ConnStatusType status;
00351 PGAsyncStatusType asyncStatus;
00352 PGTransactionStatusType xactStatus;
00353 PGQueryClass queryclass;
00354 char *last_query;
00355 char last_sqlstate[6];
00356 bool options_valid;
00357 bool nonblocking;
00358
00359 bool singleRowMode;
00360 char copy_is_binary;
00361 int copy_already_done;
00362
00363 PGnotify *notifyHead;
00364 PGnotify *notifyTail;
00365
00366
00367 int sock;
00368 SockAddr laddr;
00369 SockAddr raddr;
00370 ProtocolVersion pversion;
00371 int sversion;
00372 bool auth_req_received;
00373
00374 bool password_needed;
00375 bool dot_pgpass_used;
00376 bool sigpipe_so;
00377 bool sigpipe_flag;
00378
00379
00380 struct addrinfo *addrlist;
00381 struct addrinfo *addr_cur;
00382 int addrlist_family;
00383 PGSetenvStatusType setenv_state;
00384 const PQEnvironmentOption *next_eo;
00385 bool send_appname;
00386
00387
00388 int be_pid;
00389 int be_key;
00390 char md5Salt[4];
00391 pgParameterStatus *pstatus;
00392 int client_encoding;
00393 bool std_strings;
00394 PGVerbosity verbosity;
00395 PGlobjfuncs *lobjfuncs;
00396
00397
00398 char *inBuffer;
00399 int inBufSize;
00400 int inStart;
00401 int inCursor;
00402 int inEnd;
00403
00404
00405 char *outBuffer;
00406 int outBufSize;
00407 int outCount;
00408
00409
00410 int outMsgStart;
00411
00412 int outMsgEnd;
00413
00414
00415 PGdataValue *rowBuf;
00416 int rowBufLen;
00417
00418
00419 PGresult *result;
00420 PGresult *next_result;
00421
00422
00423
00424 #ifdef USE_SSL
00425 bool allow_ssl_try;
00426 bool wait_ssl_try;
00427
00428 SSL *ssl;
00429 X509 *peer;
00430 #ifdef USE_SSL_ENGINE
00431 ENGINE *engine;
00432 #else
00433 void *engine;
00434
00435 #endif
00436 #endif
00437
00438 #ifdef ENABLE_GSS
00439 gss_ctx_id_t gctx;
00440 gss_name_t gtarg_nam;
00441 gss_buffer_desc ginbuf;
00442 gss_buffer_desc goutbuf;
00443 #endif
00444
00445 #ifdef ENABLE_SSPI
00446 #ifndef ENABLE_GSS
00447 gss_buffer_desc ginbuf;
00448 #else
00449 char *gsslib;
00450
00451 #endif
00452 CredHandle *sspicred;
00453 CtxtHandle *sspictx;
00454 char *sspitarget;
00455 int usesspi;
00456
00457 #endif
00458
00459
00460 PQExpBufferData errorMessage;
00461
00462
00463 PQExpBufferData workBuffer;
00464 };
00465
00466
00467
00468
00469
00470 struct pg_cancel
00471 {
00472 SockAddr raddr;
00473 int be_pid;
00474 int be_key;
00475 };
00476
00477
00478
00479
00480
00481 extern char *const pgresStatus[];
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494 extern void pqDropConnection(PGconn *conn);
00495 extern int pqPacketSend(PGconn *conn, char pack_type,
00496 const void *buf, size_t buf_len);
00497 extern bool pqGetHomeDirectory(char *buf, int bufsize);
00498
00499 #ifdef ENABLE_THREAD_SAFETY
00500 extern pgthreadlock_t pg_g_threadlock;
00501
00502 #define PGTHREAD_ERROR(msg) \
00503 do { \
00504 fprintf(stderr, "%s\n", msg); \
00505 abort(); \
00506 } while (0)
00507
00508
00509 #define pglock_thread() pg_g_threadlock(true)
00510 #define pgunlock_thread() pg_g_threadlock(false)
00511 #else
00512 #define pglock_thread() ((void) 0)
00513 #define pgunlock_thread() ((void) 0)
00514 #endif
00515
00516
00517
00518 extern void pqSetResultError(PGresult *res, const char *msg);
00519 extern void pqCatenateResultError(PGresult *res, const char *msg);
00520 extern void *pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary);
00521 extern char *pqResultStrdup(PGresult *res, const char *str);
00522 extern void pqClearAsyncResult(PGconn *conn);
00523 extern void pqSaveErrorResult(PGconn *conn);
00524 extern PGresult *pqPrepareAsyncResult(PGconn *conn);
00525 extern void
00526 pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...)
00527
00528 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
00529 extern void pqSaveMessageField(PGresult *res, char code,
00530 const char *value);
00531 extern void pqSaveParameterStatus(PGconn *conn, const char *name,
00532 const char *value);
00533 extern int pqRowProcessor(PGconn *conn, const char **errmsgp);
00534 extern void pqHandleSendFailure(PGconn *conn);
00535
00536
00537
00538 extern PostgresPollingStatusType pqSetenvPoll(PGconn *conn);
00539
00540 extern char *pqBuildStartupPacket2(PGconn *conn, int *packetlen,
00541 const PQEnvironmentOption *options);
00542 extern void pqParseInput2(PGconn *conn);
00543 extern int pqGetCopyData2(PGconn *conn, char **buffer, int async);
00544 extern int pqGetline2(PGconn *conn, char *s, int maxlen);
00545 extern int pqGetlineAsync2(PGconn *conn, char *buffer, int bufsize);
00546 extern int pqEndcopy2(PGconn *conn);
00547 extern PGresult *pqFunctionCall2(PGconn *conn, Oid fnid,
00548 int *result_buf, int *actual_result_len,
00549 int result_is_int,
00550 const PQArgBlock *args, int nargs);
00551
00552
00553
00554 extern char *pqBuildStartupPacket3(PGconn *conn, int *packetlen,
00555 const PQEnvironmentOption *options);
00556 extern void pqParseInput3(PGconn *conn);
00557 extern int pqGetErrorNotice3(PGconn *conn, bool isError);
00558 extern int pqGetCopyData3(PGconn *conn, char **buffer, int async);
00559 extern int pqGetline3(PGconn *conn, char *s, int maxlen);
00560 extern int pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize);
00561 extern int pqEndcopy3(PGconn *conn);
00562 extern PGresult *pqFunctionCall3(PGconn *conn, Oid fnid,
00563 int *result_buf, int *actual_result_len,
00564 int result_is_int,
00565 const PQArgBlock *args, int nargs);
00566
00567
00568
00569
00570
00571
00572
00573
00574 extern int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn);
00575 extern int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn);
00576 extern int pqGetc(char *result, PGconn *conn);
00577 extern int pqPutc(char c, PGconn *conn);
00578 extern int pqGets(PQExpBuffer buf, PGconn *conn);
00579 extern int pqGets_append(PQExpBuffer buf, PGconn *conn);
00580 extern int pqPuts(const char *s, PGconn *conn);
00581 extern int pqGetnchar(char *s, size_t len, PGconn *conn);
00582 extern int pqSkipnchar(size_t len, PGconn *conn);
00583 extern int pqPutnchar(const char *s, size_t len, PGconn *conn);
00584 extern int pqGetInt(int *result, size_t bytes, PGconn *conn);
00585 extern int pqPutInt(int value, size_t bytes, PGconn *conn);
00586 extern int pqPutMsgStart(char msg_type, bool force_len, PGconn *conn);
00587 extern int pqPutMsgEnd(PGconn *conn);
00588 extern int pqReadData(PGconn *conn);
00589 extern int pqFlush(PGconn *conn);
00590 extern int pqWait(int forRead, int forWrite, PGconn *conn);
00591 extern int pqWaitTimed(int forRead, int forWrite, PGconn *conn,
00592 time_t finish_time);
00593 extern int pqReadReady(PGconn *conn);
00594 extern int pqWriteReady(PGconn *conn);
00595
00596
00597
00598 extern int pqsecure_initialize(PGconn *);
00599 extern void pqsecure_destroy(void);
00600 extern PostgresPollingStatusType pqsecure_open_client(PGconn *);
00601 extern void pqsecure_close(PGconn *);
00602 extern ssize_t pqsecure_read(PGconn *, void *ptr, size_t len);
00603 extern ssize_t pqsecure_write(PGconn *, const void *ptr, size_t len);
00604
00605 #if defined(ENABLE_THREAD_SAFETY) && !defined(WIN32)
00606 extern int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending);
00607 extern void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending,
00608 bool got_epipe);
00609 #endif
00610
00611
00612
00613
00614
00615 #define pqIsnonblocking(conn) ((conn)->nonblocking)
00616
00617 #ifdef ENABLE_NLS
00618 extern char *
00619 libpq_gettext(const char *msgid)
00620 __attribute__((format_arg(1)));
00621 #else
00622 #define libpq_gettext(x) (x)
00623 #endif
00624
00625
00626
00627
00628
00629 #ifdef WIN32
00630 #define SOCK_ERRNO (WSAGetLastError())
00631 #define SOCK_STRERROR winsock_strerror
00632 #define SOCK_ERRNO_SET(e) WSASetLastError(e)
00633 #else
00634 #define SOCK_ERRNO errno
00635 #define SOCK_STRERROR pqStrerror
00636 #define SOCK_ERRNO_SET(e) (errno = (e))
00637 #endif
00638
00639 #endif