PostgreSQL Source Code  git master
libpq-int.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * libpq-int.h
4  * This file contains internal definitions meant to be used only by
5  * the frontend libpq library, not by applications that call it.
6  *
7  * An application can include this file if it wants to bypass the
8  * official API defined by libpq-fe.h, but code that does so is much
9  * more likely to break across PostgreSQL releases than code that uses
10  * only the official API.
11  *
12  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * src/interfaces/libpq/libpq-int.h
16  *
17  *-------------------------------------------------------------------------
18  */
19 
20 #ifndef LIBPQ_INT_H
21 #define LIBPQ_INT_H
22 
23 /* We assume libpq-fe.h has already been included. */
24 #include "libpq-events.h"
25 
26 #include <time.h>
27 #ifndef WIN32
28 #include <sys/time.h>
29 #endif
30 
31 #ifdef ENABLE_THREAD_SAFETY
32 #ifdef WIN32
33 #include "pthread-win32.h"
34 #else
35 #include <pthread.h>
36 #endif
37 #include <signal.h>
38 #endif
39 
40 /* include stuff common to fe and be */
41 #include "getaddrinfo.h"
42 #include "libpq/pqcomm.h"
43 /* include stuff found in fe only */
44 #include "pqexpbuffer.h"
45 
46 #ifdef ENABLE_GSS
47 #if defined(HAVE_GSSAPI_H)
48 #include <gssapi.h>
49 #else
50 #include <gssapi/gssapi.h>
51 #endif
52 #endif
53 
54 #ifdef ENABLE_SSPI
55 #define SECURITY_WIN32
56 #if defined(WIN32) && !defined(_MSC_VER)
57 #include <ntsecapi.h>
58 #endif
59 #include <security.h>
60 #undef SECURITY_WIN32
61 
62 #ifndef ENABLE_GSS
63 /*
64  * Define a fake structure compatible with GSSAPI on Unix.
65  */
66 typedef struct
67 {
68  void *value;
69  int length;
70 } gss_buffer_desc;
71 #endif
72 #endif /* ENABLE_SSPI */
73 
74 #ifdef USE_OPENSSL
75 #include <openssl/ssl.h>
76 #include <openssl/err.h>
77 
78 #ifndef OPENSSL_NO_ENGINE
79 #define USE_SSL_ENGINE
80 #endif
81 #endif /* USE_OPENSSL */
82 
83 /*
84  * POSTGRES backend dependent Constants.
85  */
86 #define CMDSTATUS_LEN 64 /* should match COMPLETION_TAG_BUFSIZE */
87 
88 /*
89  * PGresult and the subsidiary types PGresAttDesc, PGresAttValue
90  * represent the result of a query (or more precisely, of a single SQL
91  * command --- a query string given to PQexec can contain multiple commands).
92  * Note we assume that a single command can return at most one tuple group,
93  * hence there is no need for multiple descriptor sets.
94  */
95 
96 /* Subsidiary-storage management structure for PGresult.
97  * See space management routines in fe-exec.c for details.
98  * Note that space[k] refers to the k'th byte starting from the physical
99  * head of the block --- it's a union, not a struct!
100  */
102 
104 {
105  PGresult_data *next; /* link to next block, or NULL */
106  char space[1]; /* dummy for accessing block as bytes */
107 };
108 
109 /* Data about a single parameter of a prepared statement */
110 typedef struct pgresParamDesc
111 {
112  Oid typid; /* type id */
114 
115 /*
116  * Data for a single attribute of a single tuple
117  *
118  * We use char* for Attribute values.
119  *
120  * The value pointer always points to a null-terminated area; we add a
121  * null (zero) byte after whatever the backend sends us. This is only
122  * particularly useful for text values ... with a binary value, the
123  * value might have embedded nulls, so the application can't use C string
124  * operators on it. But we add a null anyway for consistency.
125  * Note that the value itself does not contain a length word.
126  *
127  * A NULL attribute is a special case in two ways: its len field is NULL_LEN
128  * and its value field points to null_field in the owning PGresult. All the
129  * NULL attributes in a query result point to the same place (there's no need
130  * to store a null string separately for each one).
131  */
132 
133 #define NULL_LEN (-1) /* pg_result len for NULL value */
134 
135 typedef struct pgresAttValue
136 {
137  int len; /* length in bytes of the value */
138  char *value; /* actual value, plus terminating zero byte */
139 } PGresAttValue;
140 
141 /* Typedef for message-field list entries */
142 typedef struct pgMessageField
143 {
144  struct pgMessageField *next; /* list link */
145  char code; /* field code */
146  char contents[FLEXIBLE_ARRAY_MEMBER]; /* value, nul-terminated */
148 
149 /* Fields needed for notice handling */
150 typedef struct
151 {
152  PQnoticeReceiver noticeRec; /* notice message receiver */
154  PQnoticeProcessor noticeProc; /* notice message processor */
156 } PGNoticeHooks;
157 
158 typedef struct PGEvent
159 {
160  PGEventProc proc; /* the function to call on events */
161  char *name; /* used only for error messages */
162  void *passThrough; /* pointer supplied at registration time */
163  void *data; /* optional state (instance) data */
164  bool resultInitialized; /* T if RESULTCREATE/COPY succeeded */
165 } PGEvent;
166 
167 struct pg_result
168 {
169  int ntups;
172  PGresAttValue **tuples; /* each PGresult tuple is an array of
173  * PGresAttValue's */
174  int tupArrSize; /* allocated size of tuples array */
178  char cmdStatus[CMDSTATUS_LEN]; /* cmd status from the query */
179  int binary; /* binary tuple values if binary == 1,
180  * otherwise text */
181 
182  /*
183  * These fields are copied from the originating PGconn, so that operations
184  * on the PGresult don't have to reference the PGconn.
185  */
188  int nEvents;
189  int client_encoding; /* encoding id */
190 
191  /*
192  * Error information (all NULL if not an error result). errMsg is the
193  * "overall" error message returned by PQresultErrorMessage. If we have
194  * per-field info then it is stored in a linked list.
195  */
196  char *errMsg; /* error message, or NULL if no error */
197  PGMessageField *errFields; /* message broken into fields */
198  char *errQuery; /* text of triggering query, if available */
199 
200  /* All NULL attributes in the query result point to this null string */
201  char null_field[1];
202 
203  /*
204  * Space management information. Note that attDescs and error stuff, if
205  * not null, point into allocated blocks. But tuples points to a
206  * separately malloc'd block, so that we can realloc it.
207  */
208  PGresult_data *curBlock; /* most recently allocated block */
209  int curOffset; /* start offset of free space in block */
210  int spaceLeft; /* number of free bytes remaining in block */
211 
212  size_t memorySize; /* total space allocated for this PGresult */
213 };
214 
215 /* PGAsyncStatusType defines the state of the query-execution state machine */
216 typedef enum
217 {
218  PGASYNC_IDLE, /* nothing's happening, dude */
219  PGASYNC_BUSY, /* query in progress */
220  PGASYNC_READY, /* result ready for PQgetResult */
221  PGASYNC_COPY_IN, /* Copy In data transfer in progress */
222  PGASYNC_COPY_OUT, /* Copy Out data transfer in progress */
223  PGASYNC_COPY_BOTH /* Copy In/Out data transfer in progress */
225 
226 /* PGQueryClass tracks which query protocol we are now executing */
227 typedef enum
228 {
229  PGQUERY_SIMPLE, /* simple Query protocol (PQexec) */
230  PGQUERY_EXTENDED, /* full Extended protocol (PQexecParams) */
231  PGQUERY_PREPARE, /* Parse only (PQprepare) */
232  PGQUERY_DESCRIBE /* Describe Statement or Portal */
233 } PGQueryClass;
234 
235 /* PGSetenvStatusType defines the state of the pqSetenv state machine */
236 
237 /* (this is used only for 2.0-protocol connections) */
238 typedef enum
239 {
240  SETENV_STATE_CLIENT_ENCODING_SEND, /* About to send an Environment Option */
241  SETENV_STATE_CLIENT_ENCODING_WAIT, /* Waiting for above send to complete */
242  SETENV_STATE_OPTION_SEND, /* About to send an Environment Option */
243  SETENV_STATE_OPTION_WAIT, /* Waiting for above send to complete */
244  SETENV_STATE_QUERY1_SEND, /* About to send a status query */
245  SETENV_STATE_QUERY1_WAIT, /* Waiting for query to complete */
246  SETENV_STATE_QUERY2_SEND, /* About to send a status query */
247  SETENV_STATE_QUERY2_WAIT, /* Waiting for query to complete */
250 
251 /* Typedef for the EnvironmentOptions[] array */
252 typedef struct PQEnvironmentOption
253 {
254  const char *envName, /* name of an environment variable */
255  *pgName; /* name of corresponding SET variable */
257 
258 /* Typedef for parameter-status list entries */
259 typedef struct pgParameterStatus
260 {
261  struct pgParameterStatus *next; /* list link */
262  char *name; /* parameter name */
263  char *value; /* parameter value */
264  /* Note: name and value are stored in same malloc block as struct is */
266 
267 /* large-object-access data ... allocated only if large-object code is used. */
268 typedef struct pgLobjfuncs
269 {
270  Oid fn_lo_open; /* OID of backend function lo_open */
271  Oid fn_lo_close; /* OID of backend function lo_close */
272  Oid fn_lo_creat; /* OID of backend function lo_creat */
273  Oid fn_lo_create; /* OID of backend function lo_create */
274  Oid fn_lo_unlink; /* OID of backend function lo_unlink */
275  Oid fn_lo_lseek; /* OID of backend function lo_lseek */
276  Oid fn_lo_lseek64; /* OID of backend function lo_lseek64 */
277  Oid fn_lo_tell; /* OID of backend function lo_tell */
278  Oid fn_lo_tell64; /* OID of backend function lo_tell64 */
279  Oid fn_lo_truncate; /* OID of backend function lo_truncate */
280  Oid fn_lo_truncate64; /* OID of function lo_truncate64 */
281  Oid fn_lo_read; /* OID of backend function LOread */
282  Oid fn_lo_write; /* OID of backend function LOwrite */
283 } PGlobjfuncs;
284 
285 /* PGdataValue represents a data field value being passed to a row processor.
286  * It could be either text or binary data; text data is not zero-terminated.
287  * A SQL NULL is represented by len < 0; then value is still valid but there
288  * are no data bytes there.
289  */
290 typedef struct pgDataValue
291 {
292  int len; /* data length in bytes, or <0 if NULL */
293  const char *value; /* data value, without zero-termination */
294 } PGdataValue;
295 
296 /* Host address type enum for struct pg_conn_host */
297 typedef enum pg_conn_host_type
298 {
303 
304 /*
305  * pg_conn_host stores all information about each of possibly several hosts
306  * mentioned in the connection string. Most fields are derived by splitting
307  * the relevant connection parameter (e.g., pghost) at commas.
308  */
309 typedef struct pg_conn_host
310 {
311  pg_conn_host_type type; /* type of host address */
312  char *host; /* host name or socket path */
313  char *hostaddr; /* host numeric IP address */
314  char *port; /* port number (always provided) */
315  char *password; /* password for this host, read from the
316  * password file; NULL if not sought or not
317  * found in password file. */
318 } pg_conn_host;
319 
320 /*
321  * PGconn stores all the state data associated with a single connection
322  * to a backend.
323  */
324 struct pg_conn
325 {
326  /* Saved values of connection options */
327  char *pghost; /* the machine on which the server is running,
328  * or a path to a UNIX-domain socket, or a
329  * comma-separated list of machines and/or
330  * paths; if NULL, use DEFAULT_PGSOCKET_DIR */
331  char *pghostaddr; /* the numeric IP address of the machine on
332  * which the server is running, or a
333  * comma-separated list of same. Takes
334  * precedence over pghost. */
335  char *pgport; /* the server's communication port number, or
336  * a comma-separated list of ports */
337  char *pgtty; /* tty on which the backend messages is
338  * displayed (OBSOLETE, NOT USED) */
339  char *connect_timeout; /* connection timeout (numeric string) */
340  char *pgtcp_user_timeout; /* tcp user timeout (numeric string) */
341  char *client_encoding_initial; /* encoding to use */
342  char *pgoptions; /* options to start the backend with */
343  char *appname; /* application name */
344  char *fbappname; /* fallback application name */
345  char *dbName; /* database name */
346  char *replication; /* connect as the replication standby? */
347  char *pguser; /* Postgres username and password, if any */
348  char *pgpass;
349  char *pgpassfile; /* path to a file containing password(s) */
350  char *channel_binding; /* channel binding mode
351  * (require,prefer,disable) */
352  char *keepalives; /* use TCP keepalives? */
353  char *keepalives_idle; /* time between TCP keepalives */
354  char *keepalives_interval; /* time between TCP keepalive
355  * retransmits */
356  char *keepalives_count; /* maximum number of TCP keepalive
357  * retransmits */
358  char *sslmode; /* SSL mode (require,prefer,allow,disable) */
359  char *sslcompression; /* SSL compression (0 or 1) */
360  char *sslkey; /* client key filename */
361  char *sslcert; /* client certificate filename */
362  char *sslrootcert; /* root certificate filename */
363  char *sslcrl; /* certificate revocation list filename */
364  char *requirepeer; /* required peer credentials for local sockets */
365 
366 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
367  char *krbsrvname; /* Kerberos service name */
368 #endif
369 
370  /* Type of connection to make. Possible values: any, read-write. */
372 
373  /* Optional file to write trace info to */
374  FILE *Pfdebug;
375 
376  /* Callback procedures for notice message processing */
378 
379  /* Event procs registered via PQregisterEventProc */
380  PGEvent *events; /* expandable array of event data */
381  int nEvents; /* number of active events */
382  int eventArraySize; /* allocated array size */
383 
384  /* Status indicators */
387  PGTransactionStatusType xactStatus; /* never changes to ACTIVE */
389  char *last_query; /* last SQL command, or NULL if unknown */
390  char last_sqlstate[6]; /* last reported SQLSTATE */
391  bool options_valid; /* true if OK to attempt connection */
392  bool nonblocking; /* whether this connection is using nonblock
393  * sending semantics */
394  bool singleRowMode; /* return current query result row-by-row? */
395  char copy_is_binary; /* 1 = copy binary, 0 = copy text */
396  int copy_already_done; /* # bytes already returned in COPY OUT */
397  PGnotify *notifyHead; /* oldest unreported Notify msg */
398  PGnotify *notifyTail; /* newest unreported Notify msg */
399 
400  /* Support for multiple hosts in connection string */
401  int nconnhost; /* # of hosts named in conn string */
402  int whichhost; /* host we're currently trying/connected to */
403  pg_conn_host *connhost; /* details about each named host */
404  char *connip; /* IP address for current network connection */
405 
406  /* Connection data */
407  pgsocket sock; /* FD for socket, PGINVALID_SOCKET if
408  * unconnected */
409  SockAddr laddr; /* Local address */
410  SockAddr raddr; /* Remote address */
411  ProtocolVersion pversion; /* FE/BE protocol version in use */
412  int sversion; /* server version, e.g. 70401 for 7.4.1 */
413  bool auth_req_received; /* true if any type of auth req received */
414  bool password_needed; /* true if server demanded a password */
415  bool sigpipe_so; /* have we masked SIGPIPE via SO_NOSIGPIPE? */
416  bool sigpipe_flag; /* can we mask SIGPIPE via MSG_NOSIGNAL? */
417  bool write_failed; /* have we had a write failure on sock? */
418  char *write_err_msg; /* write error message, or NULL if OOM */
419 
420  /* Transient state needed while establishing connection */
421  bool try_next_addr; /* time to advance to next address/host? */
422  bool try_next_host; /* time to advance to next connhost[]? */
423  struct addrinfo *addrlist; /* list of addresses for current connhost */
424  struct addrinfo *addr_cur; /* the one currently being tried */
425  int addrlist_family; /* needed to know how to free addrlist */
426  PGSetenvStatusType setenv_state; /* for 2.0 protocol only */
428  bool send_appname; /* okay to send application_name? */
429 
430  /* Miscellaneous stuff */
431  int be_pid; /* PID of backend --- needed for cancels */
432  int be_key; /* key of backend --- needed for cancels */
433  pgParameterStatus *pstatus; /* ParameterStatus data */
434  int client_encoding; /* encoding id */
435  bool std_strings; /* standard_conforming_strings */
436  PGVerbosity verbosity; /* error/notice message verbosity */
437  PGContextVisibility show_context; /* whether to show CONTEXT field */
438  PGlobjfuncs *lobjfuncs; /* private state for large-object access fns */
439 
440  /* Buffer for data received from backend and not yet processed */
441  char *inBuffer; /* currently allocated buffer */
442  int inBufSize; /* allocated size of buffer */
443  int inStart; /* offset to first unconsumed data in buffer */
444  int inCursor; /* next byte to tentatively consume */
445  int inEnd; /* offset to first position after avail data */
446 
447  /* Buffer for data not yet sent to backend */
448  char *outBuffer; /* currently allocated buffer */
449  int outBufSize; /* allocated size of buffer */
450  int outCount; /* number of chars waiting in buffer */
451 
452  /* State for constructing messages in outBuffer */
453  int outMsgStart; /* offset to msg start (length word); if -1,
454  * msg has no length word */
455  int outMsgEnd; /* offset to msg end (so far) */
456 
457  /* Row processor interface workspace */
458  PGdataValue *rowBuf; /* array for passing values to rowProcessor */
459  int rowBufLen; /* number of entries allocated in rowBuf */
460 
461  /* Status for asynchronous result construction */
462  PGresult *result; /* result being constructed */
463  PGresult *next_result; /* next result (used in single-row mode) */
464 
465  /* Assorted state for SASL, SSL, GSS, etc */
466  void *sasl_state;
467 
468  /* SSL structures */
470 
471 #ifdef USE_SSL
472  bool allow_ssl_try; /* Allowed to try SSL negotiation */
473  bool wait_ssl_try; /* Delay SSL negotiation until after
474  * attempting normal connection */
475 #ifdef USE_OPENSSL
476  SSL *ssl; /* SSL status, if have SSL connection */
477  X509 *peer; /* X509 cert of server */
478 #ifdef USE_SSL_ENGINE
479  ENGINE *engine; /* SSL engine, if any */
480 #else
481  void *engine; /* dummy field to keep struct the same if
482  * OpenSSL version changes */
483 #endif
484 #endif /* USE_OPENSSL */
485 #endif /* USE_SSL */
486 
487  char *gssencmode; /* GSS mode (require,prefer,disable) */
488 #ifdef ENABLE_GSS
489  gss_ctx_id_t gctx; /* GSS context */
490  gss_name_t gtarg_nam; /* GSS target name */
491 
492  /* The following are encryption-only */
493  bool try_gss; /* GSS attempting permitted */
494  bool gssenc; /* GSS encryption is usable */
495  gss_cred_id_t gcred; /* GSS credential temp storage. */
496 #endif
497 
498 #ifdef ENABLE_SSPI
499 #ifdef ENABLE_GSS
500  char *gsslib; /* What GSS library to use ("gssapi" or
501  * "sspi") */
502 #endif
503  CredHandle *sspicred; /* SSPI credentials handle */
504  CtxtHandle *sspictx; /* SSPI context */
505  char *sspitarget; /* SSPI target name */
506  int usesspi; /* Indicate if SSPI is in use on the
507  * connection */
508 #endif
509 
510  /* Buffer for current error message */
511  PQExpBufferData errorMessage; /* expansible string */
512 
513  /* Buffer for receiving various parts of messages */
514  PQExpBufferData workBuffer; /* expansible string */
515 };
516 
517 /* PGcancel stores all data necessary to cancel a connection. A copy of this
518  * data is required to safely cancel a connection running on a different
519  * thread.
520  */
521 struct pg_cancel
522 {
523  SockAddr raddr; /* Remote address */
524  int be_pid; /* PID of backend --- needed for cancels */
525  int be_key; /* key of backend --- needed for cancels */
526 };
527 
528 
529 /* String descriptions of the ExecStatusTypes.
530  * direct use of this array is deprecated; call PQresStatus() instead.
531  */
532 extern char *const pgresStatus[];
533 
534 
535 #ifdef USE_SSL
536 
537 #ifndef WIN32
538 #define USER_CERT_FILE ".postgresql/postgresql.crt"
539 #define USER_KEY_FILE ".postgresql/postgresql.key"
540 #define ROOT_CERT_FILE ".postgresql/root.crt"
541 #define ROOT_CRL_FILE ".postgresql/root.crl"
542 #else
543 /* On Windows, the "home" directory is already PostgreSQL-specific */
544 #define USER_CERT_FILE "postgresql.crt"
545 #define USER_KEY_FILE "postgresql.key"
546 #define ROOT_CERT_FILE "root.crt"
547 #define ROOT_CRL_FILE "root.crl"
548 #endif
549 
550 #endif /* USE_SSL */
551 
552 /* ----------------
553  * Internal functions of libpq
554  * Functions declared here need to be visible across files of libpq,
555  * but are not intended to be called by applications. We use the
556  * convention "pqXXX" for internal functions, vs. the "PQxxx" names
557  * used for application-visible routines.
558  * ----------------
559  */
560 
561 /* === in fe-connect.c === */
562 
563 extern void pqDropConnection(PGconn *conn, bool flushInput);
564 extern int pqPacketSend(PGconn *conn, char pack_type,
565  const void *buf, size_t buf_len);
566 extern bool pqGetHomeDirectory(char *buf, int bufsize);
567 
568 #ifdef ENABLE_THREAD_SAFETY
570 
571 #define PGTHREAD_ERROR(msg) \
572  do { \
573  fprintf(stderr, "%s\n", msg); \
574  abort(); \
575  } while (0)
576 
577 
578 #define pglock_thread() pg_g_threadlock(true)
579 #define pgunlock_thread() pg_g_threadlock(false)
580 #else
581 #define pglock_thread() ((void) 0)
582 #define pgunlock_thread() ((void) 0)
583 #endif
584 
585 /* === in fe-exec.c === */
586 
587 extern void pqSetResultError(PGresult *res, const char *msg);
588 extern void pqCatenateResultError(PGresult *res, const char *msg);
589 extern void *pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary);
590 extern char *pqResultStrdup(PGresult *res, const char *str);
591 extern void pqClearAsyncResult(PGconn *conn);
592 extern void pqSaveErrorResult(PGconn *conn);
594 extern void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...) pg_attribute_printf(2, 3);
595 extern void pqSaveMessageField(PGresult *res, char code,
596  const char *value);
597 extern void pqSaveParameterStatus(PGconn *conn, const char *name,
598  const char *value);
599 extern int pqRowProcessor(PGconn *conn, const char **errmsgp);
600 
601 /* === in fe-protocol2.c === */
602 
604 
605 extern char *pqBuildStartupPacket2(PGconn *conn, int *packetlen,
607 extern void pqParseInput2(PGconn *conn);
608 extern int pqGetCopyData2(PGconn *conn, char **buffer, int async);
609 extern int pqGetline2(PGconn *conn, char *s, int maxlen);
610 extern int pqGetlineAsync2(PGconn *conn, char *buffer, int bufsize);
611 extern int pqEndcopy2(PGconn *conn);
612 extern PGresult *pqFunctionCall2(PGconn *conn, Oid fnid,
613  int *result_buf, int *actual_result_len,
614  int result_is_int,
615  const PQArgBlock *args, int nargs);
616 
617 /* === in fe-protocol3.c === */
618 
619 extern char *pqBuildStartupPacket3(PGconn *conn, int *packetlen,
620  const PQEnvironmentOption *options);
621 extern void pqParseInput3(PGconn *conn);
622 extern int pqGetErrorNotice3(PGconn *conn, bool isError);
623 extern void pqBuildErrorMessage3(PQExpBuffer msg, const PGresult *res,
624  PGVerbosity verbosity, PGContextVisibility show_context);
625 extern int pqGetCopyData3(PGconn *conn, char **buffer, int async);
626 extern int pqGetline3(PGconn *conn, char *s, int maxlen);
627 extern int pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize);
628 extern int pqEndcopy3(PGconn *conn);
629 extern PGresult *pqFunctionCall3(PGconn *conn, Oid fnid,
630  int *result_buf, int *actual_result_len,
631  int result_is_int,
632  const PQArgBlock *args, int nargs);
633 
634 /* === in fe-misc.c === */
635 
636  /*
637  * "Get" and "Put" routines return 0 if successful, EOF if not. Note that for
638  * Get, EOF merely means the buffer is exhausted, not that there is
639  * necessarily any error.
640  */
641 extern int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn);
642 extern int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn);
643 extern int pqGetc(char *result, PGconn *conn);
644 extern int pqPutc(char c, PGconn *conn);
645 extern int pqGets(PQExpBuffer buf, PGconn *conn);
646 extern int pqGets_append(PQExpBuffer buf, PGconn *conn);
647 extern int pqPuts(const char *s, PGconn *conn);
648 extern int pqGetnchar(char *s, size_t len, PGconn *conn);
649 extern int pqSkipnchar(size_t len, PGconn *conn);
650 extern int pqPutnchar(const char *s, size_t len, PGconn *conn);
651 extern int pqGetInt(int *result, size_t bytes, PGconn *conn);
652 extern int pqPutInt(int value, size_t bytes, PGconn *conn);
653 extern int pqPutMsgStart(char msg_type, bool force_len, PGconn *conn);
654 extern int pqPutMsgEnd(PGconn *conn);
655 extern int pqReadData(PGconn *conn);
656 extern int pqFlush(PGconn *conn);
657 extern int pqWait(int forRead, int forWrite, PGconn *conn);
658 extern int pqWaitTimed(int forRead, int forWrite, PGconn *conn,
659  time_t finish_time);
660 extern int pqReadReady(PGconn *conn);
661 extern int pqWriteReady(PGconn *conn);
662 
663 /* === in fe-secure.c === */
664 
665 extern int pqsecure_initialize(PGconn *);
667 extern void pqsecure_close(PGconn *);
668 extern ssize_t pqsecure_read(PGconn *, void *ptr, size_t len);
669 extern ssize_t pqsecure_write(PGconn *, const void *ptr, size_t len);
670 extern ssize_t pqsecure_raw_read(PGconn *, void *ptr, size_t len);
671 extern ssize_t pqsecure_raw_write(PGconn *, const void *ptr, size_t len);
672 
673 #if defined(ENABLE_THREAD_SAFETY) && !defined(WIN32)
674 extern int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending);
675 extern void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending,
676  bool got_epipe);
677 #endif
678 
679 /* === SSL === */
680 
681 /*
682  * The SSL implementation provides these functions.
683  */
684 
685 /*
686  * Implementation of PQinitSSL().
687  */
688 extern void pgtls_init_library(bool do_ssl, int do_crypto);
689 
690 /*
691  * Initialize SSL library.
692  *
693  * The conn parameter is only used to be able to pass back an error
694  * message - no connection-local setup is made here.
695  *
696  * Returns 0 if OK, -1 on failure (with a message in conn->errorMessage).
697  */
698 extern int pgtls_init(PGconn *conn);
699 
700 /*
701  * Begin or continue negotiating a secure session.
702  */
703 extern PostgresPollingStatusType pgtls_open_client(PGconn *conn);
704 
705 /*
706  * Close SSL connection.
707  */
708 extern void pgtls_close(PGconn *conn);
709 
710 /*
711  * Read data from a secure connection.
712  *
713  * On failure, this function is responsible for putting a suitable message
714  * into conn->errorMessage. The caller must still inspect errno, but only
715  * to determine whether to continue/retry after error.
716  */
717 extern ssize_t pgtls_read(PGconn *conn, void *ptr, size_t len);
718 
719 /*
720  * Is there unread data waiting in the SSL read buffer?
721  */
722 extern bool pgtls_read_pending(PGconn *conn);
723 
724 /*
725  * Write data to a secure connection.
726  *
727  * On failure, this function is responsible for putting a suitable message
728  * into conn->errorMessage. The caller must still inspect errno, but only
729  * to determine whether to continue/retry after error.
730  */
731 extern ssize_t pgtls_write(PGconn *conn, const void *ptr, size_t len);
732 
733 /*
734  * Get the hash of the server certificate, for SCRAM channel binding type
735  * tls-server-end-point.
736  *
737  * NULL is sent back to the caller in the event of an error, with an
738  * error message for the caller to consume.
739  *
740  * This is not supported with old versions of OpenSSL that don't have
741  * the X509_get_signature_nid() function.
742  */
743 #if defined(USE_OPENSSL) && defined(HAVE_X509_GET_SIGNATURE_NID)
744 #define HAVE_PGTLS_GET_PEER_CERTIFICATE_HASH
745 extern char *pgtls_get_peer_certificate_hash(PGconn *conn, size_t *len);
746 #endif
747 
748 /*
749  * Verify that the server certificate matches the host name we connected to.
750  *
751  * The certificate's Common Name and Subject Alternative Names are considered.
752  *
753  * Returns 1 if the name matches, and 0 if it does not. On error, returns
754  * -1, and sets the libpq error message.
755  *
756  */
758  int *names_examined,
759  char **first_name);
760 
761 /* === GSSAPI === */
762 
763 #ifdef ENABLE_GSS
764 
765 /*
766  * Establish a GSSAPI-encrypted connection.
767  */
768 extern PostgresPollingStatusType pqsecure_open_gss(PGconn *conn);
769 
770 /*
771  * Read and write functions for GSSAPI-encrypted connections, with internal
772  * buffering to handle nonblocking sockets.
773  */
774 extern ssize_t pg_GSS_write(PGconn *conn, const void *ptr, size_t len);
775 extern ssize_t pg_GSS_read(PGconn *conn, void *ptr, size_t len);
776 #endif
777 
778 /* === miscellaneous macros === */
779 
780 /*
781  * this is so that we can check if a connection is non-blocking internally
782  * without the overhead of a function call
783  */
784 #define pqIsnonblocking(conn) ((conn)->nonblocking)
785 
786 #ifdef ENABLE_NLS
787 extern char *libpq_gettext(const char *msgid) pg_attribute_format_arg(1);
788 extern char *libpq_ngettext(const char *msgid, const char *msgid_plural, unsigned long n) pg_attribute_format_arg(1) pg_attribute_format_arg(2);
789 #else
790 #define libpq_gettext(x) (x)
791 #define libpq_ngettext(s, p, n) ((n) == 1 ? (s) : (p))
792 #endif
793 
794 /*
795  * These macros are needed to let error-handling code be portable between
796  * Unix and Windows. (ugh)
797  */
798 #ifdef WIN32
799 #define SOCK_ERRNO (WSAGetLastError())
800 #define SOCK_STRERROR winsock_strerror
801 #define SOCK_ERRNO_SET(e) WSASetLastError(e)
802 #else
803 #define SOCK_ERRNO errno
804 #define SOCK_STRERROR strerror_r
805 #define SOCK_ERRNO_SET(e) (errno = (e))
806 #endif
807 
808 #endif /* LIBPQ_INT_H */
struct PGEvent PGEvent
struct pgDataValue PGdataValue
int pqPutInt(int value, size_t bytes, PGconn *conn)
Definition: fe-misc.c:306
PGContextVisibility show_context
Definition: libpq-int.h:437
bool sigpipe_flag
Definition: libpq-int.h:416
ConnStatusType
Definition: libpq-fe.h:47
size_t memorySize
Definition: libpq-int.h:212
char * gssencmode
Definition: libpq-int.h:487
void(* PQnoticeReceiver)(void *arg, const PGresult *res)
Definition: libpq-fe.h:174
void pgtls_close(PGconn *conn)
int inEnd
Definition: libpq-int.h:445
int pqWait(int forRead, int forWrite, PGconn *conn)
Definition: fe-misc.c:1020
PGMessageField * errFields
Definition: libpq-int.h:197
bool pgtls_read_pending(PGconn *conn)
int pqPacketSend(PGconn *conn, char pack_type, const void *buf, size_t buf_len)
Definition: fe-connect.c:4464
int inStart
Definition: libpq-int.h:443
ssize_t pgtls_read(PGconn *conn, void *ptr, size_t len)
char space[1]
Definition: libpq-int.h:106
int pqReadData(PGconn *conn)
Definition: fe-misc.c:628
char * pqBuildStartupPacket3(PGconn *conn, int *packetlen, const PQEnvironmentOption *options)
int be_pid
Definition: libpq-int.h:524
struct addrinfo * addr_cur
Definition: libpq-int.h:424
PGEvent * events
Definition: libpq-int.h:380
ssize_t pqsecure_write(PGconn *, const void *ptr, size_t len)
Definition: fe-secure.c:297
void pqSaveErrorResult(PGconn *conn)
Definition: fe-exec.c:772
bool singleRowMode
Definition: libpq-int.h:394
bool resultInitialized
Definition: libpq-int.h:164
int rowBufLen
Definition: libpq-int.h:459
Oid fn_lo_write
Definition: libpq-int.h:282
int eventArraySize
Definition: libpq-int.h:382
int pqGetline2(PGconn *conn, char *s, int maxlen)
char * replication
Definition: libpq-int.h:346
char * pgpassfile
Definition: libpq-int.h:349
bool sigpipe_so
Definition: libpq-int.h:415
int pqPutMsgEnd(PGconn *conn)
Definition: fe-misc.c:586
int pqRowProcessor(PGconn *conn, const char **errmsgp)
Definition: fe-exec.c:1132
bool password_needed
Definition: libpq-int.h:414
int(* PGEventProc)(PGEventId evtId, void *evtInfo, void *passThrough)
Definition: libpq-events.h:69
ssize_t pgtls_write(PGconn *conn, const void *ptr, size_t len)
char * pqResultStrdup(PGresult *res, const char *str)
Definition: fe-exec.c:646
PGQueryClass
Definition: libpq-int.h:227
def bytes(source, encoding='ascii', errors='strict')
int outCount
Definition: libpq-int.h:450
PQExpBufferData workBuffer
Definition: libpq-int.h:514
void(* PQnoticeProcessor)(void *arg, const char *message)
Definition: libpq-fe.h:175
Oid fn_lo_create
Definition: libpq-int.h:273
int pqPutMsgStart(char msg_type, bool force_len, PGconn *conn)
Definition: fe-misc.c:518
PGAsyncStatusType
Definition: libpq-int.h:216
int tupArrSize
Definition: libpq-int.h:174
char * requirepeer
Definition: libpq-int.h:364
char * host
Definition: libpq-int.h:312
PGresult * pqFunctionCall2(PGconn *conn, Oid fnid, int *result_buf, int *actual_result_len, int result_is_int, const PQArgBlock *args, int nargs)
const char * value
Definition: libpq-int.h:293
PGresult_data * next
Definition: libpq-int.h:105
static struct @145 value
PGQueryClass queryclass
Definition: libpq-int.h:388
char copy_is_binary
Definition: libpq-int.h:395
PGnotify * notifyHead
Definition: libpq-int.h:397
Oid fn_lo_lseek
Definition: libpq-int.h:275
char * dbName
Definition: libpq-int.h:345
struct pgMessageField * next
Definition: libpq-int.h:144
int outMsgEnd
Definition: libpq-int.h:455
ExecStatusType
Definition: libpq-fe.h:84
int spaceLeft
Definition: libpq-int.h:210
void pqClearAsyncResult(PGconn *conn)
Definition: fe-exec.c:749
void pqsecure_close(PGconn *)
Definition: fe-secure.c:197
char * keepalives
Definition: libpq-int.h:352
void pgtls_init_library(bool do_ssl, int do_crypto)
void pqBuildErrorMessage3(PQExpBuffer msg, const PGresult *res, PGVerbosity verbosity, PGContextVisibility show_context)
Definition: fe-protocol3.c:987
int nEvents
Definition: libpq-int.h:188
unsigned int Oid
Definition: postgres_ext.h:31
PGresult * pqPrepareAsyncResult(PGconn *conn)
Definition: fe-exec.c:826
char * errMsg
Definition: libpq-int.h:196
struct addrinfo * addrlist
Definition: libpq-int.h:423
#define CMDSTATUS_LEN
Definition: libpq-int.h:86
char * write_err_msg
Definition: libpq-int.h:418
char * keepalives_idle
Definition: libpq-int.h:353
int pqGets_append(PQExpBuffer buf, PGconn *conn)
Definition: fe-misc.c:168
pg_conn_host_type type
Definition: libpq-int.h:311
PGresAttDesc * attDescs
Definition: libpq-int.h:171
FILE * Pfdebug
Definition: libpq-int.h:374
void pqParseInput3(PGconn *conn)
Definition: fe-protocol3.c:63
void * pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary)
Definition: fe-exec.c:534
PGresult * result
Definition: libpq-int.h:462
#define pg_attribute_printf(f, a)
Definition: c.h:132
void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...) pg_attribute_printf(2
PGresAttValue ** tuples
Definition: libpq-int.h:172
PGresult * pqFunctionCall3(PGconn *conn, Oid fnid, int *result_buf, int *actual_result_len, int result_is_int, const PQArgBlock *args, int nargs)
char * client_encoding_initial
Definition: libpq-int.h:341
ssize_t pqsecure_read(PGconn *, void *ptr, size_t len)
Definition: fe-secure.c:213
int sversion
Definition: libpq-int.h:412
char * sslkey
Definition: libpq-int.h:360
PGresult * next_result
Definition: libpq-int.h:463
char * sslcompression
Definition: libpq-int.h:359
PGAsyncStatusType asyncStatus
Definition: libpq-int.h:386
int pqPutnchar(const char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:245
int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:407
PGSetenvStatusType setenv_state
Definition: libpq-int.h:426
void pqSaveParameterStatus(PGconn *conn, const char *name, const char *value)
Definition: fe-exec.c:1014
bool write_failed
Definition: libpq-int.h:417
void(* pgthreadlock_t)(int acquire)
Definition: libpq-fe.h:384
PGNoticeHooks noticeHooks
Definition: libpq-int.h:186
void * data
Definition: libpq-int.h:163
int pqsecure_initialize(PGconn *)
Definition: fe-secure.c:168
PGconn * conn
Definition: streamutil.c:54
Oid fn_lo_read
Definition: libpq-int.h:281
int inBufSize
Definition: libpq-int.h:442
ssize_t pg_GSS_write(PGconn *conn, const void *ptr, size_t len)
Oid fn_lo_truncate
Definition: libpq-int.h:279
struct pgParameterStatus pgParameterStatus
PostgresPollingStatusType pgtls_open_client(PGconn *conn)
PGNoticeHooks noticeHooks
Definition: libpq-int.h:377
bool try_next_host
Definition: libpq-int.h:422
int curOffset
Definition: libpq-int.h:209
char * hostaddr
Definition: libpq-int.h:313
char * connect_timeout
Definition: libpq-int.h:339
pg_conn_host * connhost
Definition: libpq-int.h:403
void * noticeProcArg
Definition: libpq-int.h:155
bool ssl_in_use
Definition: libpq-int.h:469
void * sasl_state
Definition: libpq-int.h:466
char * keepalives_interval
Definition: libpq-int.h:354
#define libpq_ngettext(s, p, n)
Definition: libpq-int.h:791
char * c
PGVerbosity verbosity
Definition: libpq-int.h:436
const PQEnvironmentOption * next_eo
Definition: libpq-int.h:427
static char * buf
Definition: pg_test_fsync.c:67
Oid fn_lo_open
Definition: libpq-int.h:270
PQnoticeReceiver noticeRec
Definition: libpq-int.h:152
int pqGetInt(int *result, size_t bytes, PGconn *conn)
Definition: fe-misc.c:266
char * appname
Definition: libpq-int.h:343
PGresult_data * curBlock
Definition: libpq-int.h:208
char * channel_binding
Definition: libpq-int.h:350
int numParameters
Definition: libpq-int.h:175
int pgsocket
Definition: port.h:31
int pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize)
char * target_session_attrs
Definition: libpq-int.h:371
char * last_query
Definition: libpq-int.h:389
Oid fn_lo_tell64
Definition: libpq-int.h:278
pgthreadlock_t pg_g_threadlock
Definition: fe-connect.c:437
PGdataValue * rowBuf
Definition: libpq-int.h:458
void void pqSaveMessageField(PGresult *res, char code, const char *value)
Definition: fe-exec.c:993
int binary
Definition: libpq-int.h:179
char * pguser
Definition: libpq-int.h:347
const char * pgName
Definition: libpq-int.h:254
#define pg_attribute_format_arg(a)
Definition: c.h:131
Oid fn_lo_truncate64
Definition: libpq-int.h:280
PGSetenvStatusType
Definition: libpq-int.h:238
int pqReadReady(PGconn *conn)
Definition: fe-misc.c:1057
pgsocket sock
Definition: libpq-int.h:407
int be_key
Definition: libpq-int.h:525
char * errQuery
Definition: libpq-int.h:198
void pqDropConnection(PGconn *conn, bool flushInput)
Definition: fe-connect.c:453
int numAttributes
Definition: libpq-int.h:170
uint32 ProtocolVersion
Definition: pqcomm.h:113
struct PQEnvironmentOption PQEnvironmentOption
struct pgresParamDesc PGresParamDesc
int pqGets(PQExpBuffer buf, PGconn *conn)
Definition: fe-misc.c:162
pg_conn_host_type
Definition: libpq-int.h:297
PostgresPollingStatusType pqSetenvPoll(PGconn *conn)
Definition: fe-protocol2.c:47
SockAddr raddr
Definition: libpq-int.h:410
ssize_t pqsecure_raw_read(PGconn *, void *ptr, size_t len)
Definition: fe-secure.c:239
pgParameterStatus * pstatus
Definition: libpq-int.h:433
PGContextVisibility
Definition: libpq-fe.h:120
char * inBuffer
Definition: libpq-int.h:441
int addrlist_family
Definition: libpq-int.h:425
int pqGetCopyData3(PGconn *conn, char **buffer, int async)
int pqPuts(const char *s, PGconn *conn)
Definition: fe-misc.c:178
char *const pgresStatus[]
Definition: fe-exec.c:32
int pgtls_verify_peer_name_matches_certificate_guts(PGconn *conn, int *names_examined, char **first_name)
char * sslmode
Definition: libpq-int.h:358
struct pgresAttValue PGresAttValue
int pqWriteReady(PGconn *conn)
Definition: fe-misc.c:1067
PGVerbosity
Definition: libpq-fe.h:112
PQnoticeProcessor noticeProc
Definition: libpq-int.h:154
int outMsgStart
Definition: libpq-int.h:453
PQExpBufferData errorMessage
Definition: libpq-int.h:511
void pqParseInput2(PGconn *conn)
Definition: fe-protocol2.c:408
bool std_strings
Definition: libpq-int.h:435
bool try_next_addr
Definition: libpq-int.h:421
Oid fn_lo_tell
Definition: libpq-int.h:277
int pqGetnchar(char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:194
char * sslcert
Definition: libpq-int.h:361
int pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
Definition: fe-misc.c:1033
bool options_valid
Definition: libpq-int.h:391
PGTransactionStatusType
Definition: libpq-fe.h:103
int outBufSize
Definition: libpq-int.h:449
PGresParamDesc * paramDescs
Definition: libpq-int.h:176
struct pg_conn_host pg_conn_host
int ntups
Definition: libpq-int.h:169
char * pgoptions
Definition: libpq-int.h:342
char * connip
Definition: libpq-int.h:404
int pqPutc(char c, PGconn *conn)
Definition: fe-misc.c:111
ssize_t pqsecure_raw_write(PGconn *, const void *ptr, size_t len)
Definition: fe-secure.c:323
struct pgParameterStatus * next
Definition: libpq-int.h:261
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:343
char contents[FLEXIBLE_ARRAY_MEMBER]
Definition: libpq-int.h:146
char * pgpass
Definition: libpq-int.h:348
ProtocolVersion pversion
Definition: libpq-int.h:411
char * sslrootcert
Definition: libpq-int.h:362
PGEventProc proc
Definition: libpq-int.h:160
ConnStatusType status
Definition: libpq-int.h:385
bool auth_req_received
Definition: libpq-int.h:413
int be_key
Definition: libpq-int.h:432
Oid fn_lo_creat
Definition: libpq-int.h:272
PGnotify * notifyTail
Definition: libpq-int.h:398
int copy_already_done
Definition: libpq-int.h:396
Oid fn_lo_close
Definition: libpq-int.h:271
const char * name
Definition: encode.c:521
int pqFlush(PGconn *conn)
Definition: fe-misc.c:997
char * pqBuildStartupPacket2(PGconn *conn, int *packetlen, const PQEnvironmentOption *options)
SockAddr laddr
Definition: libpq-int.h:409
char * outBuffer
Definition: libpq-int.h:448
bool nonblocking
Definition: libpq-int.h:392
PostgresPollingStatusType
Definition: libpq-fe.h:74
PostgresPollingStatusType pqsecure_open_client(PGconn *)
Definition: fe-secure.c:183
char * pghostaddr
Definition: libpq-int.h:331
ssize_t pg_GSS_read(PGconn *conn, void *ptr, size_t len)
Oid fn_lo_lseek64
Definition: libpq-int.h:276
struct pgLobjfuncs PGlobjfuncs
int pqGetCopyData2(PGconn *conn, char **buffer, int async)
int pqEndcopy2(PGconn *conn)
int pqGetlineAsync2(PGconn *conn, char *buffer, int bufsize)
int nEvents
Definition: libpq-int.h:381
int pqGetc(char *result, PGconn *conn)
Definition: fe-misc.c:93
void * passThrough
Definition: libpq-int.h:162
char * fbappname
Definition: libpq-int.h:344
int client_encoding
Definition: libpq-int.h:434
Oid fn_lo_unlink
Definition: libpq-int.h:274
char * value
Definition: libpq-int.h:138
void pqCatenateResultError(PGresult *res, const char *msg)
Definition: fe-exec.c:675
PostgresPollingStatusType pqsecure_open_gss(PGconn *conn)
SockAddr raddr
Definition: libpq-int.h:523
bool pqGetHomeDirectory(char *buf, int bufsize)
Definition: fe-connect.c:7040
PGEvent * events
Definition: libpq-int.h:187
int inCursor
Definition: libpq-int.h:444
int pqSkipnchar(size_t len, PGconn *conn)
Definition: fe-misc.c:223
char * port
Definition: libpq-int.h:314
char * pgport
Definition: libpq-int.h:335
int pqEndcopy3(PGconn *conn)
int pqGetErrorNotice3(PGconn *conn, bool isError)
Definition: fe-protocol3.c:873
int be_pid
Definition: libpq-int.h:431
int nconnhost
Definition: libpq-int.h:401
char * name
Definition: libpq-int.h:161
ExecStatusType resultStatus
Definition: libpq-int.h:177
PGlobjfuncs * lobjfuncs
Definition: libpq-int.h:438
char * pgtcp_user_timeout
Definition: libpq-int.h:340
int pgtls_init(PGconn *conn)
char * sslcrl
Definition: libpq-int.h:363
void pqSetResultError(PGresult *res, const char *msg)
Definition: fe-exec.c:660
int whichhost
Definition: libpq-int.h:402
struct pgMessageField PGMessageField
#define libpq_gettext(x)
Definition: libpq-int.h:790
int pqGetline3(PGconn *conn, char *s, int maxlen)
bool send_appname
Definition: libpq-int.h:428
PGTransactionStatusType xactStatus
Definition: libpq-int.h:387
char * keepalives_count
Definition: libpq-int.h:356
char * pghost
Definition: libpq-int.h:327
char * pgtty
Definition: libpq-int.h:337
void * noticeRecArg
Definition: libpq-int.h:153
int client_encoding
Definition: libpq-int.h:189
char * password
Definition: libpq-int.h:315