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-2018, 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 PGresTuple 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 
213 /* PGAsyncStatusType defines the state of the query-execution state machine */
214 typedef enum
215 {
216  PGASYNC_IDLE, /* nothing's happening, dude */
217  PGASYNC_BUSY, /* query in progress */
218  PGASYNC_READY, /* result ready for PQgetResult */
219  PGASYNC_COPY_IN, /* Copy In data transfer in progress */
220  PGASYNC_COPY_OUT, /* Copy Out data transfer in progress */
221  PGASYNC_COPY_BOTH /* Copy In/Out data transfer in progress */
223 
224 /* PGQueryClass tracks which query protocol we are now executing */
225 typedef enum
226 {
227  PGQUERY_SIMPLE, /* simple Query protocol (PQexec) */
228  PGQUERY_EXTENDED, /* full Extended protocol (PQexecParams) */
229  PGQUERY_PREPARE, /* Parse only (PQprepare) */
230  PGQUERY_DESCRIBE /* Describe Statement or Portal */
231 } PGQueryClass;
232 
233 /* PGSetenvStatusType defines the state of the PQSetenv state machine */
234 /* (this is used only for 2.0-protocol connections) */
235 typedef enum
236 {
237  SETENV_STATE_CLIENT_ENCODING_SEND, /* About to send an Environment Option */
238  SETENV_STATE_CLIENT_ENCODING_WAIT, /* Waiting for above send to complete */
239  SETENV_STATE_OPTION_SEND, /* About to send an Environment Option */
240  SETENV_STATE_OPTION_WAIT, /* Waiting for above send to complete */
241  SETENV_STATE_QUERY1_SEND, /* About to send a status query */
242  SETENV_STATE_QUERY1_WAIT, /* Waiting for query to complete */
243  SETENV_STATE_QUERY2_SEND, /* About to send a status query */
244  SETENV_STATE_QUERY2_WAIT, /* Waiting for query to complete */
247 
248 /* Typedef for the EnvironmentOptions[] array */
249 typedef struct PQEnvironmentOption
250 {
251  const char *envName, /* name of an environment variable */
252  *pgName; /* name of corresponding SET variable */
254 
255 /* Typedef for parameter-status list entries */
256 typedef struct pgParameterStatus
257 {
258  struct pgParameterStatus *next; /* list link */
259  char *name; /* parameter name */
260  char *value; /* parameter value */
261  /* Note: name and value are stored in same malloc block as struct is */
263 
264 /* large-object-access data ... allocated only if large-object code is used. */
265 typedef struct pgLobjfuncs
266 {
267  Oid fn_lo_open; /* OID of backend function lo_open */
268  Oid fn_lo_close; /* OID of backend function lo_close */
269  Oid fn_lo_creat; /* OID of backend function lo_creat */
270  Oid fn_lo_create; /* OID of backend function lo_create */
271  Oid fn_lo_unlink; /* OID of backend function lo_unlink */
272  Oid fn_lo_lseek; /* OID of backend function lo_lseek */
273  Oid fn_lo_lseek64; /* OID of backend function lo_lseek64 */
274  Oid fn_lo_tell; /* OID of backend function lo_tell */
275  Oid fn_lo_tell64; /* OID of backend function lo_tell64 */
276  Oid fn_lo_truncate; /* OID of backend function lo_truncate */
277  Oid fn_lo_truncate64; /* OID of function lo_truncate64 */
278  Oid fn_lo_read; /* OID of backend function LOread */
279  Oid fn_lo_write; /* OID of backend function LOwrite */
280 } PGlobjfuncs;
281 
282 /* PGdataValue represents a data field value being passed to a row processor.
283  * It could be either text or binary data; text data is not zero-terminated.
284  * A SQL NULL is represented by len < 0; then value is still valid but there
285  * are no data bytes there.
286  */
287 typedef struct pgDataValue
288 {
289  int len; /* data length in bytes, or <0 if NULL */
290  const char *value; /* data value, without zero-termination */
291 } PGdataValue;
292 
293 typedef enum pg_conn_host_type
294 {
299 
300 /*
301  * pg_conn_host stores all information about one of possibly several hosts
302  * mentioned in the connection string. Derived by splitting the pghost
303  * on the comma character and then parsing each segment.
304  */
305 typedef struct pg_conn_host
306 {
307  pg_conn_host_type type; /* type of host */
308  char *host; /* host name or socket path */
309  char *hostaddr; /* host address */
310  char *port; /* port number for this host; if not NULL,
311  * overrides the PGConn's pgport */
312  char *password; /* password for this host, read from the
313  * password file. only set if the PGconn's
314  * pgpass field is NULL. */
315  struct addrinfo *addrlist; /* list of possible backend addresses */
316 } pg_conn_host;
317 
318 /*
319  * PGconn stores all the state data associated with a single connection
320  * to a backend.
321  */
322 struct pg_conn
323 {
324  /* Saved values of connection options */
325  char *pghost; /* the machine on which the server is running,
326  * or a path to a UNIX-domain socket, or a
327  * comma-separated list of machines and/or
328  * paths, optionally with port suffixes; if
329  * NULL, use DEFAULT_PGSOCKET_DIR */
330  char *pghostaddr; /* the numeric IP address of the machine on
331  * which the server is running. Takes
332  * precedence over above. */
333  char *pgport; /* the server's communication port number */
334  char *pgtty; /* tty on which the backend messages is
335  * displayed (OBSOLETE, NOT USED) */
336  char *connect_timeout; /* connection timeout (numeric string) */
337  char *client_encoding_initial; /* encoding to use */
338  char *pgoptions; /* options to start the backend with */
339  char *appname; /* application name */
340  char *fbappname; /* fallback application name */
341  char *dbName; /* database name */
342  char *replication; /* connect as the replication standby? */
343  char *pguser; /* Postgres username and password, if any */
344  char *pgpass;
345  char *pgpassfile; /* path to a file containing password(s) */
346  char *keepalives; /* use TCP keepalives? */
347  char *keepalives_idle; /* time between TCP keepalives */
348  char *keepalives_interval; /* time between TCP keepalive
349  * retransmits */
350  char *keepalives_count; /* maximum number of TCP keepalive
351  * retransmits */
352  char *scram_channel_binding; /* SCRAM channel binding type */
353  char *sslmode; /* SSL mode (require,prefer,allow,disable) */
354  char *sslcompression; /* SSL compression (0 or 1) */
355  char *sslkey; /* client key filename */
356  char *sslcert; /* client certificate filename */
357  char *sslrootcert; /* root certificate filename */
358  char *sslcrl; /* certificate revocation list filename */
359  char *requirepeer; /* required peer credentials for local sockets */
360 
361 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
362  char *krbsrvname; /* Kerberos service name */
363 #endif
364 
365  /* Type of connection to make. Possible values: any, read-write. */
367 
368  /* Optional file to write trace info to */
369  FILE *Pfdebug;
370 
371  /* Callback procedures for notice message processing */
373 
374  /* Event procs registered via PQregisterEventProc */
375  PGEvent *events; /* expandable array of event data */
376  int nEvents; /* number of active events */
377  int eventArraySize; /* allocated array size */
378 
379  /* Status indicators */
382  PGTransactionStatusType xactStatus; /* never changes to ACTIVE */
384  char *last_query; /* last SQL command, or NULL if unknown */
385  char last_sqlstate[6]; /* last reported SQLSTATE */
386  bool options_valid; /* true if OK to attempt connection */
387  bool nonblocking; /* whether this connection is using nonblock
388  * sending semantics */
389  bool singleRowMode; /* return current query result row-by-row? */
390  char copy_is_binary; /* 1 = copy binary, 0 = copy text */
391  int copy_already_done; /* # bytes already returned in COPY OUT */
392  PGnotify *notifyHead; /* oldest unreported Notify msg */
393  PGnotify *notifyTail; /* newest unreported Notify msg */
394 
395  /* Support for multiple hosts in connection string */
396  int nconnhost; /* # of possible hosts */
397  int whichhost; /* host we're currently considering */
398  pg_conn_host *connhost; /* details about each possible host */
399 
400  /* Connection data */
401  pgsocket sock; /* FD for socket, PGINVALID_SOCKET if
402  * unconnected */
403  SockAddr laddr; /* Local address */
404  SockAddr raddr; /* Remote address */
405  ProtocolVersion pversion; /* FE/BE protocol version in use */
406  int sversion; /* server version, e.g. 70401 for 7.4.1 */
407  bool auth_req_received; /* true if any type of auth req received */
408  bool password_needed; /* true if server demanded a password */
409  bool pgpassfile_used; /* true if password is from pgpassfile */
410  bool sigpipe_so; /* have we masked SIGPIPE via SO_NOSIGPIPE? */
411  bool sigpipe_flag; /* can we mask SIGPIPE via MSG_NOSIGNAL? */
412 
413  /* Transient state needed while establishing connection */
414  struct addrinfo *addr_cur; /* backend address currently being tried */
415  PGSetenvStatusType setenv_state; /* for 2.0 protocol only */
417  bool send_appname; /* okay to send application_name? */
418 
419  /* Miscellaneous stuff */
420  int be_pid; /* PID of backend --- needed for cancels */
421  int be_key; /* key of backend --- needed for cancels */
422  pgParameterStatus *pstatus; /* ParameterStatus data */
423  int client_encoding; /* encoding id */
424  bool std_strings; /* standard_conforming_strings */
425  PGVerbosity verbosity; /* error/notice message verbosity */
426  PGContextVisibility show_context; /* whether to show CONTEXT field */
427  PGlobjfuncs *lobjfuncs; /* private state for large-object access fns */
428 
429  /* Buffer for data received from backend and not yet processed */
430  char *inBuffer; /* currently allocated buffer */
431  int inBufSize; /* allocated size of buffer */
432  int inStart; /* offset to first unconsumed data in buffer */
433  int inCursor; /* next byte to tentatively consume */
434  int inEnd; /* offset to first position after avail data */
435 
436  /* Buffer for data not yet sent to backend */
437  char *outBuffer; /* currently allocated buffer */
438  int outBufSize; /* allocated size of buffer */
439  int outCount; /* number of chars waiting in buffer */
440 
441  /* State for constructing messages in outBuffer */
442  int outMsgStart; /* offset to msg start (length word); if -1,
443  * msg has no length word */
444  int outMsgEnd; /* offset to msg end (so far) */
445 
446  /* Row processor interface workspace */
447  PGdataValue *rowBuf; /* array for passing values to rowProcessor */
448  int rowBufLen; /* number of entries allocated in rowBuf */
449 
450  /* Status for asynchronous result construction */
451  PGresult *result; /* result being constructed */
452  PGresult *next_result; /* next result (used in single-row mode) */
453 
454  /* Assorted state for SASL, SSL, GSS, etc */
455  void *sasl_state;
456 
457  /* SSL structures */
459 
460 #ifdef USE_SSL
461  bool allow_ssl_try; /* Allowed to try SSL negotiation */
462  bool wait_ssl_try; /* Delay SSL negotiation until after
463  * attempting normal connection */
464 #ifdef USE_OPENSSL
465  SSL *ssl; /* SSL status, if have SSL connection */
466  X509 *peer; /* X509 cert of server */
467 #ifdef USE_SSL_ENGINE
468  ENGINE *engine; /* SSL engine, if any */
469 #else
470  void *engine; /* dummy field to keep struct the same if
471  * OpenSSL version changes */
472 #endif
473 #endif /* USE_OPENSSL */
474 #endif /* USE_SSL */
475 
476 #ifdef ENABLE_GSS
477  gss_ctx_id_t gctx; /* GSS context */
478  gss_name_t gtarg_nam; /* GSS target name */
479 #endif
480 
481 #ifdef ENABLE_SSPI
482 #ifdef ENABLE_GSS
483  char *gsslib; /* What GSS library to use ("gssapi" or
484  * "sspi") */
485 #endif
486  CredHandle *sspicred; /* SSPI credentials handle */
487  CtxtHandle *sspictx; /* SSPI context */
488  char *sspitarget; /* SSPI target name */
489  int usesspi; /* Indicate if SSPI is in use on the
490  * connection */
491 #endif
492 
493  /* Buffer for current error message */
494  PQExpBufferData errorMessage; /* expansible string */
495 
496  /* Buffer for receiving various parts of messages */
497  PQExpBufferData workBuffer; /* expansible string */
498 };
499 
500 /* PGcancel stores all data necessary to cancel a connection. A copy of this
501  * data is required to safely cancel a connection running on a different
502  * thread.
503  */
504 struct pg_cancel
505 {
506  SockAddr raddr; /* Remote address */
507  int be_pid; /* PID of backend --- needed for cancels */
508  int be_key; /* key of backend --- needed for cancels */
509 };
510 
511 
512 /* String descriptions of the ExecStatusTypes.
513  * direct use of this array is deprecated; call PQresStatus() instead.
514  */
515 extern char *const pgresStatus[];
516 
517 
518 #ifdef USE_SSL
519 
520 #ifndef WIN32
521 #define USER_CERT_FILE ".postgresql/postgresql.crt"
522 #define USER_KEY_FILE ".postgresql/postgresql.key"
523 #define ROOT_CERT_FILE ".postgresql/root.crt"
524 #define ROOT_CRL_FILE ".postgresql/root.crl"
525 #else
526 /* On Windows, the "home" directory is already PostgreSQL-specific */
527 #define USER_CERT_FILE "postgresql.crt"
528 #define USER_KEY_FILE "postgresql.key"
529 #define ROOT_CERT_FILE "root.crt"
530 #define ROOT_CRL_FILE "root.crl"
531 #endif
532 
533 #endif /* USE_SSL */
534 
535 /* ----------------
536  * Internal functions of libpq
537  * Functions declared here need to be visible across files of libpq,
538  * but are not intended to be called by applications. We use the
539  * convention "pqXXX" for internal functions, vs. the "PQxxx" names
540  * used for application-visible routines.
541  * ----------------
542  */
543 
544 /* === in fe-connect.c === */
545 
546 extern void pqDropConnection(PGconn *conn, bool flushInput);
547 extern int pqPacketSend(PGconn *conn, char pack_type,
548  const void *buf, size_t buf_len);
549 extern bool pqGetHomeDirectory(char *buf, int bufsize);
550 
551 #ifdef ENABLE_THREAD_SAFETY
553 
554 #define PGTHREAD_ERROR(msg) \
555  do { \
556  fprintf(stderr, "%s\n", msg); \
557  abort(); \
558  } while (0)
559 
560 
561 #define pglock_thread() pg_g_threadlock(true)
562 #define pgunlock_thread() pg_g_threadlock(false)
563 #else
564 #define pglock_thread() ((void) 0)
565 #define pgunlock_thread() ((void) 0)
566 #endif
567 
568 /* === in fe-exec.c === */
569 
570 extern void pqSetResultError(PGresult *res, const char *msg);
571 extern void pqCatenateResultError(PGresult *res, const char *msg);
572 extern void *pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary);
573 extern char *pqResultStrdup(PGresult *res, const char *str);
574 extern void pqClearAsyncResult(PGconn *conn);
575 extern void pqSaveErrorResult(PGconn *conn);
577 extern void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...) pg_attribute_printf(2, 3);
578 extern void pqSaveMessageField(PGresult *res, char code,
579  const char *value);
580 extern void pqSaveParameterStatus(PGconn *conn, const char *name,
581  const char *value);
582 extern int pqRowProcessor(PGconn *conn, const char **errmsgp);
583 extern void pqHandleSendFailure(PGconn *conn);
584 
585 /* === in fe-protocol2.c === */
586 
588 
589 extern char *pqBuildStartupPacket2(PGconn *conn, int *packetlen,
591 extern void pqParseInput2(PGconn *conn);
592 extern int pqGetCopyData2(PGconn *conn, char **buffer, int async);
593 extern int pqGetline2(PGconn *conn, char *s, int maxlen);
594 extern int pqGetlineAsync2(PGconn *conn, char *buffer, int bufsize);
595 extern int pqEndcopy2(PGconn *conn);
596 extern PGresult *pqFunctionCall2(PGconn *conn, Oid fnid,
597  int *result_buf, int *actual_result_len,
598  int result_is_int,
599  const PQArgBlock *args, int nargs);
600 
601 /* === in fe-protocol3.c === */
602 
603 extern char *pqBuildStartupPacket3(PGconn *conn, int *packetlen,
604  const PQEnvironmentOption *options);
605 extern void pqParseInput3(PGconn *conn);
606 extern int pqGetErrorNotice3(PGconn *conn, bool isError);
607 extern void pqBuildErrorMessage3(PQExpBuffer msg, const PGresult *res,
608  PGVerbosity verbosity, PGContextVisibility show_context);
609 extern int pqGetCopyData3(PGconn *conn, char **buffer, int async);
610 extern int pqGetline3(PGconn *conn, char *s, int maxlen);
611 extern int pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize);
612 extern int pqEndcopy3(PGconn *conn);
613 extern PGresult *pqFunctionCall3(PGconn *conn, Oid fnid,
614  int *result_buf, int *actual_result_len,
615  int result_is_int,
616  const PQArgBlock *args, int nargs);
617 
618 /* === in fe-misc.c === */
619 
620  /*
621  * "Get" and "Put" routines return 0 if successful, EOF if not. Note that for
622  * Get, EOF merely means the buffer is exhausted, not that there is
623  * necessarily any error.
624  */
625 extern int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn);
626 extern int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn);
627 extern int pqGetc(char *result, PGconn *conn);
628 extern int pqPutc(char c, PGconn *conn);
629 extern int pqGets(PQExpBuffer buf, PGconn *conn);
630 extern int pqGets_append(PQExpBuffer buf, PGconn *conn);
631 extern int pqPuts(const char *s, PGconn *conn);
632 extern int pqGetnchar(char *s, size_t len, PGconn *conn);
633 extern int pqSkipnchar(size_t len, PGconn *conn);
634 extern int pqPutnchar(const char *s, size_t len, PGconn *conn);
635 extern int pqGetInt(int *result, size_t bytes, PGconn *conn);
636 extern int pqPutInt(int value, size_t bytes, PGconn *conn);
637 extern int pqPutMsgStart(char msg_type, bool force_len, PGconn *conn);
638 extern int pqPutMsgEnd(PGconn *conn);
639 extern int pqReadData(PGconn *conn);
640 extern int pqFlush(PGconn *conn);
641 extern int pqWait(int forRead, int forWrite, PGconn *conn);
642 extern int pqWaitTimed(int forRead, int forWrite, PGconn *conn,
643  time_t finish_time);
644 extern int pqReadReady(PGconn *conn);
645 extern int pqWriteReady(PGconn *conn);
646 
647 /* === in fe-secure.c === */
648 
649 extern int pqsecure_initialize(PGconn *);
650 extern void pqsecure_destroy(void);
652 extern void pqsecure_close(PGconn *);
653 extern ssize_t pqsecure_read(PGconn *, void *ptr, size_t len);
654 extern ssize_t pqsecure_write(PGconn *, const void *ptr, size_t len);
655 extern ssize_t pqsecure_raw_read(PGconn *, void *ptr, size_t len);
656 extern ssize_t pqsecure_raw_write(PGconn *, const void *ptr, size_t len);
657 
658 #if defined(ENABLE_THREAD_SAFETY) && !defined(WIN32)
659 extern int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending);
660 extern void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending,
661  bool got_epipe);
662 #endif
663 
664 /* === SSL === */
665 
666 /*
667  * The SSL implementation provides these functions.
668  */
669 
670 /*
671  * Implementation of PQinitSSL().
672  */
673 extern void pgtls_init_library(bool do_ssl, int do_crypto);
674 
675 /*
676  * Initialize SSL library.
677  *
678  * The conn parameter is only used to be able to pass back an error
679  * message - no connection-local setup is made here.
680  *
681  * Returns 0 if OK, -1 on failure (with a message in conn->errorMessage).
682  */
683 extern int pgtls_init(PGconn *conn);
684 
685 /*
686  * Begin or continue negotiating a secure session.
687  */
688 extern PostgresPollingStatusType pgtls_open_client(PGconn *conn);
689 
690 /*
691  * Close SSL connection.
692  */
693 extern void pgtls_close(PGconn *conn);
694 
695 /*
696  * Read data from a secure connection.
697  *
698  * On failure, this function is responsible for putting a suitable message
699  * into conn->errorMessage. The caller must still inspect errno, but only
700  * to determine whether to continue/retry after error.
701  */
702 extern ssize_t pgtls_read(PGconn *conn, void *ptr, size_t len);
703 
704 /*
705  * Is there unread data waiting in the SSL read buffer?
706  */
707 extern bool pgtls_read_pending(PGconn *conn);
708 
709 /*
710  * Write data to a secure connection.
711  *
712  * On failure, this function is responsible for putting a suitable message
713  * into conn->errorMessage. The caller must still inspect errno, but only
714  * to determine whether to continue/retry after error.
715  */
716 extern ssize_t pgtls_write(PGconn *conn, const void *ptr, size_t len);
717 
718 /*
719  * Get the TLS finish message sent during last handshake.
720  *
721  * This information is useful for callers doing channel binding during
722  * authentication.
723  */
724 extern char *pgtls_get_finished(PGconn *conn, size_t *len);
725 
726 /*
727  * Get the hash of the server certificate, for SCRAM channel binding type
728  * tls-server-end-point.
729  *
730  * NULL is sent back to the caller in the event of an error, with an
731  * error message for the caller to consume.
732  */
733 extern char *pgtls_get_peer_certificate_hash(PGconn *conn, size_t *len);
734 
735 /*
736  * Verify that the server certificate matches the host name we connected to.
737  *
738  * The certificate's Common Name and Subject Alternative Names are considered.
739  *
740  * Returns 1 if the name matches, and 0 if it does not. On error, returns
741  * -1, and sets the libpq error message.
742  *
743  */
745  int *names_examined,
746  char **first_name);
747 
748 /* === miscellaneous macros === */
749 
750 /*
751  * this is so that we can check if a connection is non-blocking internally
752  * without the overhead of a function call
753  */
754 #define pqIsnonblocking(conn) ((conn)->nonblocking)
755 
756 #ifdef ENABLE_NLS
757 extern char *libpq_gettext(const char *msgid) pg_attribute_format_arg(1);
758 extern char *libpq_ngettext(const char *msgid, const char *msgid_plural, unsigned long n) pg_attribute_format_arg(1) pg_attribute_format_arg(2);
759 #else
760 #define libpq_gettext(x) (x)
761 #define libpq_ngettext(s, p, n) ((n) == 1 ? (s) : (p))
762 #endif
763 
764 /*
765  * These macros are needed to let error-handling code be portable between
766  * Unix and Windows. (ugh)
767  */
768 #ifdef WIN32
769 #define SOCK_ERRNO (WSAGetLastError())
770 #define SOCK_STRERROR winsock_strerror
771 #define SOCK_ERRNO_SET(e) WSASetLastError(e)
772 #else
773 #define SOCK_ERRNO errno
774 #define SOCK_STRERROR pqStrerror
775 #define SOCK_ERRNO_SET(e) (errno = (e))
776 #endif
777 
778 #endif /* LIBPQ_INT_H */
struct PGEvent PGEvent
struct pgDataValue PGdataValue
int pqPutInt(int value, size_t bytes, PGconn *conn)
Definition: fe-misc.c:307
PGContextVisibility show_context
Definition: libpq-int.h:426
bool sigpipe_flag
Definition: libpq-int.h:411
ConnStatusType
Definition: libpq-fe.h:47
int length(const List *list)
Definition: list.c:1333
void(* PQnoticeReceiver)(void *arg, const PGresult *res)
Definition: libpq-fe.h:171
void pgtls_close(PGconn *conn)
int inEnd
Definition: libpq-int.h:434
int pqWait(int forRead, int forWrite, PGconn *conn)
Definition: fe-misc.c:984
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:3983
int inStart
Definition: libpq-int.h:432
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:629
char * pqBuildStartupPacket3(PGconn *conn, int *packetlen, const PQEnvironmentOption *options)
int be_pid
Definition: libpq-int.h:507
struct addrinfo * addr_cur
Definition: libpq-int.h:414
PGEvent * events
Definition: libpq-int.h:375
ssize_t pqsecure_write(PGconn *, const void *ptr, size_t len)
Definition: fe-secure.c:290
void pqSaveErrorResult(PGconn *conn)
Definition: fe-exec.c:749
bool singleRowMode
Definition: libpq-int.h:389
bool resultInitialized
Definition: libpq-int.h:164
int rowBufLen
Definition: libpq-int.h:448
Oid fn_lo_write
Definition: libpq-int.h:279
int eventArraySize
Definition: libpq-int.h:377
int pqGetline2(PGconn *conn, char *s, int maxlen)
char * replication
Definition: libpq-int.h:342
char * pgpassfile
Definition: libpq-int.h:345
bool sigpipe_so
Definition: libpq-int.h:410
int pqPutMsgEnd(PGconn *conn)
Definition: fe-misc.c:587
int pqRowProcessor(PGconn *conn, const char **errmsgp)
Definition: fe-exec.c:1081
bool password_needed
Definition: libpq-int.h:408
char * pgtls_get_finished(PGconn *conn, size_t *len)
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:623
PGQueryClass
Definition: libpq-int.h:225
int outCount
Definition: libpq-int.h:439
PQExpBufferData workBuffer
Definition: libpq-int.h:497
void(* PQnoticeProcessor)(void *arg, const char *message)
Definition: libpq-fe.h:172
Oid fn_lo_create
Definition: libpq-int.h:270
int pqPutMsgStart(char msg_type, bool force_len, PGconn *conn)
Definition: fe-misc.c:519
PGAsyncStatusType
Definition: libpq-int.h:214
int tupArrSize
Definition: libpq-int.h:174
char * requirepeer
Definition: libpq-int.h:359
char * host
Definition: libpq-int.h:308
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:290
PGresult_data * next
Definition: libpq-int.h:105
PGQueryClass queryclass
Definition: libpq-int.h:383
char copy_is_binary
Definition: libpq-int.h:390
PGnotify * notifyHead
Definition: libpq-int.h:392
Oid fn_lo_lseek
Definition: libpq-int.h:272
char * dbName
Definition: libpq-int.h:341
struct pgMessageField * next
Definition: libpq-int.h:144
int outMsgEnd
Definition: libpq-int.h:444
ExecStatusType
Definition: libpq-fe.h:82
int spaceLeft
Definition: libpq-int.h:210
void pqClearAsyncResult(PGconn *conn)
Definition: fe-exec.c:726
void pqsecure_close(PGconn *)
Definition: fe-secure.c:197
char * keepalives
Definition: libpq-int.h:346
void pgtls_init_library(bool do_ssl, int do_crypto)
bool pgpassfile_used
Definition: libpq-int.h:409
void pqBuildErrorMessage3(PQExpBuffer msg, const PGresult *res, PGVerbosity verbosity, PGContextVisibility show_context)
Definition: fe-protocol3.c:989
int nEvents
Definition: libpq-int.h:188
unsigned int Oid
Definition: postgres_ext.h:31
PGresult * pqPrepareAsyncResult(PGconn *conn)
Definition: fe-exec.c:777
char * errMsg
Definition: libpq-int.h:196
#define CMDSTATUS_LEN
Definition: libpq-int.h:86
char * keepalives_idle
Definition: libpq-int.h:347
int pqGets_append(PQExpBuffer buf, PGconn *conn)
Definition: fe-misc.c:169
pg_conn_host_type type
Definition: libpq-int.h:307
PGresAttDesc * attDescs
Definition: libpq-int.h:171
FILE * Pfdebug
Definition: libpq-int.h:369
void pqParseInput3(PGconn *conn)
Definition: fe-protocol3.c:65
void * pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary)
Definition: fe-exec.c:527
PGresult * result
Definition: libpq-int.h:451
#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:337
ssize_t pqsecure_read(PGconn *, void *ptr, size_t len)
Definition: fe-secure.c:213
int sversion
Definition: libpq-int.h:406
char * sslkey
Definition: libpq-int.h:355
void pqsecure_destroy(void)
PGresult * next_result
Definition: libpq-int.h:452
char * sslcompression
Definition: libpq-int.h:354
PGAsyncStatusType asyncStatus
Definition: libpq-int.h:381
int pqPutnchar(const char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:246
int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:408
PGSetenvStatusType setenv_state
Definition: libpq-int.h:415
void pqSaveParameterStatus(PGconn *conn, const char *name, const char *value)
Definition: fe-exec.c:963
void(* pgthreadlock_t)(int acquire)
Definition: libpq-fe.h:374
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:55
Oid fn_lo_read
Definition: libpq-int.h:278
int inBufSize
Definition: libpq-int.h:431
Oid fn_lo_truncate
Definition: libpq-int.h:276
struct pgParameterStatus pgParameterStatus
PostgresPollingStatusType pgtls_open_client(PGconn *conn)
char * pgtls_get_peer_certificate_hash(PGconn *conn, size_t *len)
PGNoticeHooks noticeHooks
Definition: libpq-int.h:372
int curOffset
Definition: libpq-int.h:209
char * hostaddr
Definition: libpq-int.h:309
char * connect_timeout
Definition: libpq-int.h:336
pg_conn_host * connhost
Definition: libpq-int.h:398
void * noticeProcArg
Definition: libpq-int.h:155
bool ssl_in_use
Definition: libpq-int.h:458
void * sasl_state
Definition: libpq-int.h:455
char * keepalives_interval
Definition: libpq-int.h:348
#define libpq_ngettext(s, p, n)
Definition: libpq-int.h:761
char * c
PGVerbosity verbosity
Definition: libpq-int.h:425
const PQEnvironmentOption * next_eo
Definition: libpq-int.h:416
static char * buf
Definition: pg_test_fsync.c:67
Oid fn_lo_open
Definition: libpq-int.h:267
PQnoticeReceiver noticeRec
Definition: libpq-int.h:152
int pqGetInt(int *result, size_t bytes, PGconn *conn)
Definition: fe-misc.c:267
char * appname
Definition: libpq-int.h:339
PGresult_data * curBlock
Definition: libpq-int.h:208
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:366
char * last_query
Definition: libpq-int.h:384
Oid fn_lo_tell64
Definition: libpq-int.h:275
pgthreadlock_t pg_g_threadlock
Definition: fe-connect.c:416
PGdataValue * rowBuf
Definition: libpq-int.h:447
void pqHandleSendFailure(PGconn *conn)
Definition: fe-exec.c:1633
void void pqSaveMessageField(PGresult *res, char code, const char *value)
Definition: fe-exec.c:942
int binary
Definition: libpq-int.h:179
char * pguser
Definition: libpq-int.h:343
const char * pgName
Definition: libpq-int.h:251
#define pg_attribute_format_arg(a)
Definition: c.h:131
Oid fn_lo_truncate64
Definition: libpq-int.h:277
PGSetenvStatusType
Definition: libpq-int.h:235
int pqReadReady(PGconn *conn)
Definition: fe-misc.c:1021
pgsocket sock
Definition: libpq-int.h:401
int be_key
Definition: libpq-int.h:508
char * errQuery
Definition: libpq-int.h:198
void pqDropConnection(PGconn *conn, bool flushInput)
Definition: fe-connect.c:431
char * scram_channel_binding
Definition: libpq-int.h:352
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:163
pg_conn_host_type
Definition: libpq-int.h:293
PostgresPollingStatusType pqSetenvPoll(PGconn *conn)
Definition: fe-protocol2.c:49
SockAddr raddr
Definition: libpq-int.h:404
ssize_t pqsecure_raw_read(PGconn *, void *ptr, size_t len)
Definition: fe-secure.c:232
pgParameterStatus * pstatus
Definition: libpq-int.h:422
PGContextVisibility
Definition: libpq-fe.h:117
char * inBuffer
Definition: libpq-int.h:430
int pqGetCopyData3(PGconn *conn, char **buffer, int async)
int pqPuts(const char *s, PGconn *conn)
Definition: fe-misc.c:179
char *const pgresStatus[]
Definition: fe-exec.c:33
int pgtls_verify_peer_name_matches_certificate_guts(PGconn *conn, int *names_examined, char **first_name)
char * sslmode
Definition: libpq-int.h:353
struct pgresAttValue PGresAttValue
int pqWriteReady(PGconn *conn)
Definition: fe-misc.c:1031
static struct @131 value
PGVerbosity
Definition: libpq-fe.h:110
PQnoticeProcessor noticeProc
Definition: libpq-int.h:154
int outMsgStart
Definition: libpq-int.h:442
PQExpBufferData errorMessage
Definition: libpq-int.h:494
void pqParseInput2(PGconn *conn)
Definition: fe-protocol2.c:410
bool std_strings
Definition: libpq-int.h:424
Oid fn_lo_tell
Definition: libpq-int.h:274
int pqGetnchar(char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:195
char * sslcert
Definition: libpq-int.h:356
int pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
Definition: fe-misc.c:997
bool options_valid
Definition: libpq-int.h:386
PGTransactionStatusType
Definition: libpq-fe.h:101
int outBufSize
Definition: libpq-int.h:438
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:338
struct addrinfo * addrlist
Definition: libpq-int.h:315
int pqPutc(char c, PGconn *conn)
Definition: fe-misc.c:112
ssize_t pqsecure_raw_write(PGconn *, const void *ptr, size_t len)
Definition: fe-secure.c:309
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
struct pgParameterStatus * next
Definition: libpq-int.h:258
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:344
char contents[FLEXIBLE_ARRAY_MEMBER]
Definition: libpq-int.h:146
char * pgpass
Definition: libpq-int.h:344
ProtocolVersion pversion
Definition: libpq-int.h:405
char * sslrootcert
Definition: libpq-int.h:357
PGEventProc proc
Definition: libpq-int.h:160
ConnStatusType status
Definition: libpq-int.h:380
bool auth_req_received
Definition: libpq-int.h:407
int be_key
Definition: libpq-int.h:421
Oid fn_lo_creat
Definition: libpq-int.h:269
PGnotify * notifyTail
Definition: libpq-int.h:393
int copy_already_done
Definition: libpq-int.h:391
Oid fn_lo_close
Definition: libpq-int.h:268
const char * name
Definition: encode.c:521
int pqFlush(PGconn *conn)
Definition: fe-misc.c:961
char * pqBuildStartupPacket2(PGconn *conn, int *packetlen, const PQEnvironmentOption *options)
SockAddr laddr
Definition: libpq-int.h:403
char * outBuffer
Definition: libpq-int.h:437
bool nonblocking
Definition: libpq-int.h:387
PostgresPollingStatusType
Definition: libpq-fe.h:72
PostgresPollingStatusType pqsecure_open_client(PGconn *)
Definition: fe-secure.c:183
char * pghostaddr
Definition: libpq-int.h:330
Oid fn_lo_lseek64
Definition: libpq-int.h:273
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:376
int pqGetc(char *result, PGconn *conn)
Definition: fe-misc.c:94
void * passThrough
Definition: libpq-int.h:162
char * fbappname
Definition: libpq-int.h:340
int client_encoding
Definition: libpq-int.h:423
Oid fn_lo_unlink
Definition: libpq-int.h:271
char * value
Definition: libpq-int.h:138
void pqCatenateResultError(PGresult *res, const char *msg)
Definition: fe-exec.c:652
SockAddr raddr
Definition: libpq-int.h:506
bool pqGetHomeDirectory(char *buf, int bufsize)
Definition: fe-connect.c:6543
PGEvent * events
Definition: libpq-int.h:187
int inCursor
Definition: libpq-int.h:433
int pqSkipnchar(size_t len, PGconn *conn)
Definition: fe-misc.c:224
char * port
Definition: libpq-int.h:310
char * pgport
Definition: libpq-int.h:333
int pqEndcopy3(PGconn *conn)
int pqGetErrorNotice3(PGconn *conn, bool isError)
Definition: fe-protocol3.c:875
int be_pid
Definition: libpq-int.h:420
int nconnhost
Definition: libpq-int.h:396
char * name
Definition: libpq-int.h:161
ExecStatusType resultStatus
Definition: libpq-int.h:177
PGlobjfuncs * lobjfuncs
Definition: libpq-int.h:427
int pgtls_init(PGconn *conn)
char * sslcrl
Definition: libpq-int.h:358
void pqSetResultError(PGresult *res, const char *msg)
Definition: fe-exec.c:637
int whichhost
Definition: libpq-int.h:397
struct pgMessageField PGMessageField
#define libpq_gettext(x)
Definition: libpq-int.h:760
int pqGetline3(PGconn *conn, char *s, int maxlen)
bool send_appname
Definition: libpq-int.h:417
PGTransactionStatusType xactStatus
Definition: libpq-int.h:382
char * keepalives_count
Definition: libpq-int.h:350
char * pghost
Definition: libpq-int.h:325
char * pgtty
Definition: libpq-int.h:334
void * noticeRecArg
Definition: libpq-int.h:153
int client_encoding
Definition: libpq-int.h:189
char * password
Definition: libpq-int.h:312