libpq-int.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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-2016, 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. #ifndef LIBPQ_INT_H
  20. #define LIBPQ_INT_H
  21. /* We assume libpq-fe.h has already been included. */
  22. #include "postgres_fe.h"
  23. #include "libpq-events.h"
  24. #include <time.h>
  25. #include <sys/types.h>
  26. #ifndef WIN32
  27. #include <sys/time.h>
  28. #endif
  29. #ifdef ENABLE_THREAD_SAFETY
  30. #ifdef WIN32
  31. #include "pthread-win32.h"
  32. #else
  33. #include <pthread.h>
  34. #endif
  35. #include <signal.h>
  36. #endif
  37. /* include stuff common to fe and be */
  38. #include "getaddrinfo.h"
  39. #include "libpq/pqcomm.h"
  40. /* include stuff found in fe only */
  41. #include "pqexpbuffer.h"
  42. #ifdef ENABLE_GSS
  43. #if defined(HAVE_GSSAPI_H)
  44. #include <gssapi.h>
  45. #else
  46. #include <gssapi/gssapi.h>
  47. #endif
  48. #endif
  49. #ifdef ENABLE_SSPI
  50. #define SECURITY_WIN32
  51. #if defined(WIN32) && !defined(WIN32_ONLY_COMPILER)
  52. #include <ntsecapi.h>
  53. #endif
  54. #include <security.h>
  55. #undef SECURITY_WIN32
  56. #ifndef ENABLE_GSS
  57. /*
  58. * Define a fake structure compatible with GSSAPI on Unix.
  59. */
  60. typedef struct
  61. {
  62. void *value;
  63. int length;
  64. } gss_buffer_desc;
  65. #endif
  66. #endif /* ENABLE_SSPI */
  67. #ifdef USE_OPENSSL
  68. #include <openssl/ssl.h>
  69. #include <openssl/err.h>
  70. #if (OPENSSL_VERSION_NUMBER >= 0x00907000L) && !defined(OPENSSL_NO_ENGINE)
  71. #define USE_SSL_ENGINE
  72. #endif
  73. #endif /* USE_OPENSSL */
  74. /*
  75. * POSTGRES backend dependent Constants.
  76. */
  77. #define CMDSTATUS_LEN 64 /* should match COMPLETION_TAG_BUFSIZE */
  78. /*
  79. * PGresult and the subsidiary types PGresAttDesc, PGresAttValue
  80. * represent the result of a query (or more precisely, of a single SQL
  81. * command --- a query string given to PQexec can contain multiple commands).
  82. * Note we assume that a single command can return at most one tuple group,
  83. * hence there is no need for multiple descriptor sets.
  84. */
  85. /* Subsidiary-storage management structure for PGresult.
  86. * See space management routines in fe-exec.c for details.
  87. * Note that space[k] refers to the k'th byte starting from the physical
  88. * head of the block --- it's a union, not a struct!
  89. */
  90. typedef union pgresult_data PGresult_data;
  91. union pgresult_data
  92. {
  93. PGresult_data *next; /* link to next block, or NULL */
  94. char space[1]; /* dummy for accessing block as bytes */
  95. };
  96. /* Data about a single parameter of a prepared statement */
  97. typedef struct pgresParamDesc
  98. {
  99. Oid typid; /* type id */
  100. } PGresParamDesc;
  101. /*
  102. * Data for a single attribute of a single tuple
  103. *
  104. * We use char* for Attribute values.
  105. *
  106. * The value pointer always points to a null-terminated area; we add a
  107. * null (zero) byte after whatever the backend sends us. This is only
  108. * particularly useful for text values ... with a binary value, the
  109. * value might have embedded nulls, so the application can't use C string
  110. * operators on it. But we add a null anyway for consistency.
  111. * Note that the value itself does not contain a length word.
  112. *
  113. * A NULL attribute is a special case in two ways: its len field is NULL_LEN
  114. * and its value field points to null_field in the owning PGresult. All the
  115. * NULL attributes in a query result point to the same place (there's no need
  116. * to store a null string separately for each one).
  117. */
  118. #define NULL_LEN (-1) /* pg_result len for NULL value */
  119. typedef struct pgresAttValue
  120. {
  121. int len; /* length in bytes of the value */
  122. char *value; /* actual value, plus terminating zero byte */
  123. } PGresAttValue;
  124. /* Typedef for message-field list entries */
  125. typedef struct pgMessageField
  126. {
  127. struct pgMessageField *next; /* list link */
  128. char code; /* field code */
  129. char contents[FLEXIBLE_ARRAY_MEMBER]; /* value, nul-terminated */
  130. } PGMessageField;
  131. /* Fields needed for notice handling */
  132. typedef struct
  133. {
  134. PQnoticeReceiver noticeRec; /* notice message receiver */
  135. void *noticeRecArg;
  136. PQnoticeProcessor noticeProc; /* notice message processor */
  137. void *noticeProcArg;
  138. } PGNoticeHooks;
  139. typedef struct PGEvent
  140. {
  141. PGEventProc proc; /* the function to call on events */
  142. char *name; /* used only for error messages */
  143. void *passThrough; /* pointer supplied at registration time */
  144. void *data; /* optional state (instance) data */
  145. bool resultInitialized; /* T if RESULTCREATE/COPY succeeded */
  146. } PGEvent;
  147. struct pg_result
  148. {
  149. int ntups;
  150. int numAttributes;
  151. PGresAttDesc *attDescs;
  152. PGresAttValue **tuples; /* each PGresTuple is an array of
  153. * PGresAttValue's */
  154. int tupArrSize; /* allocated size of tuples array */
  155. int numParameters;
  156. PGresParamDesc *paramDescs;
  157. ExecStatusType resultStatus;
  158. char cmdStatus[CMDSTATUS_LEN]; /* cmd status from the query */
  159. int binary; /* binary tuple values if binary == 1,
  160. * otherwise text */
  161. /*
  162. * These fields are copied from the originating PGconn, so that operations
  163. * on the PGresult don't have to reference the PGconn.
  164. */
  165. PGNoticeHooks noticeHooks;
  166. PGEvent *events;
  167. int nEvents;
  168. int client_encoding; /* encoding id */
  169. /*
  170. * Error information (all NULL if not an error result). errMsg is the
  171. * "overall" error message returned by PQresultErrorMessage. If we have
  172. * per-field info then it is stored in a linked list.
  173. */
  174. char *errMsg; /* error message, or NULL if no error */
  175. PGMessageField *errFields; /* message broken into fields */
  176. char *errQuery; /* text of triggering query, if available */
  177. /* All NULL attributes in the query result point to this null string */
  178. char null_field[1];
  179. /*
  180. * Space management information. Note that attDescs and error stuff, if
  181. * not null, point into allocated blocks. But tuples points to a
  182. * separately malloc'd block, so that we can realloc it.
  183. */
  184. PGresult_data *curBlock; /* most recently allocated block */
  185. int curOffset; /* start offset of free space in block */
  186. int spaceLeft; /* number of free bytes remaining in block */
  187. };
  188. /* PGAsyncStatusType defines the state of the query-execution state machine */
  189. typedef enum
  190. {
  191. PGASYNC_IDLE, /* nothing's happening, dude */
  192. PGASYNC_BUSY, /* query in progress */
  193. PGASYNC_READY, /* result ready for PQgetResult */
  194. PGASYNC_COPY_IN, /* Copy In data transfer in progress */
  195. PGASYNC_COPY_OUT, /* Copy Out data transfer in progress */
  196. PGASYNC_COPY_BOTH /* Copy In/Out data transfer in progress */
  197. } PGAsyncStatusType;
  198. /* PGQueryClass tracks which query protocol we are now executing */
  199. typedef enum
  200. {
  201. PGQUERY_SIMPLE, /* simple Query protocol (PQexec) */
  202. PGQUERY_EXTENDED, /* full Extended protocol (PQexecParams) */
  203. PGQUERY_PREPARE, /* Parse only (PQprepare) */
  204. PGQUERY_DESCRIBE /* Describe Statement or Portal */
  205. } PGQueryClass;
  206. /* PGSetenvStatusType defines the state of the PQSetenv state machine */
  207. /* (this is used only for 2.0-protocol connections) */
  208. typedef enum
  209. {
  210. SETENV_STATE_CLIENT_ENCODING_SEND, /* About to send an Environment Option */
  211. SETENV_STATE_CLIENT_ENCODING_WAIT, /* Waiting for above send to complete */
  212. SETENV_STATE_OPTION_SEND, /* About to send an Environment Option */
  213. SETENV_STATE_OPTION_WAIT, /* Waiting for above send to complete */
  214. SETENV_STATE_QUERY1_SEND, /* About to send a status query */
  215. SETENV_STATE_QUERY1_WAIT, /* Waiting for query to complete */
  216. SETENV_STATE_QUERY2_SEND, /* About to send a status query */
  217. SETENV_STATE_QUERY2_WAIT, /* Waiting for query to complete */
  218. SETENV_STATE_IDLE
  219. } PGSetenvStatusType;
  220. /* Typedef for the EnvironmentOptions[] array */
  221. typedef struct PQEnvironmentOption
  222. {
  223. const char *envName, /* name of an environment variable */
  224. *pgName; /* name of corresponding SET variable */
  225. } PQEnvironmentOption;
  226. /* Typedef for parameter-status list entries */
  227. typedef struct pgParameterStatus
  228. {
  229. struct pgParameterStatus *next; /* list link */
  230. char *name; /* parameter name */
  231. char *value; /* parameter value */
  232. /* Note: name and value are stored in same malloc block as struct is */
  233. } pgParameterStatus;
  234. /* large-object-access data ... allocated only if large-object code is used. */
  235. typedef struct pgLobjfuncs
  236. {
  237. Oid fn_lo_open; /* OID of backend function lo_open */
  238. Oid fn_lo_close; /* OID of backend function lo_close */
  239. Oid fn_lo_creat; /* OID of backend function lo_creat */
  240. Oid fn_lo_create; /* OID of backend function lo_create */
  241. Oid fn_lo_unlink; /* OID of backend function lo_unlink */
  242. Oid fn_lo_lseek; /* OID of backend function lo_lseek */
  243. Oid fn_lo_lseek64; /* OID of backend function lo_lseek64 */
  244. Oid fn_lo_tell; /* OID of backend function lo_tell */
  245. Oid fn_lo_tell64; /* OID of backend function lo_tell64 */
  246. Oid fn_lo_truncate; /* OID of backend function lo_truncate */
  247. Oid fn_lo_truncate64; /* OID of function lo_truncate64 */
  248. Oid fn_lo_read; /* OID of backend function LOread */
  249. Oid fn_lo_write; /* OID of backend function LOwrite */
  250. } PGlobjfuncs;
  251. /* PGdataValue represents a data field value being passed to a row processor.
  252. * It could be either text or binary data; text data is not zero-terminated.
  253. * A SQL NULL is represented by len < 0; then value is still valid but there
  254. * are no data bytes there.
  255. */
  256. typedef struct pgDataValue
  257. {
  258. int len; /* data length in bytes, or <0 if NULL */
  259. const char *value; /* data value, without zero-termination */
  260. } PGdataValue;
  261. /*
  262. * PGconn stores all the state data associated with a single connection
  263. * to a backend.
  264. */
  265. struct pg_conn
  266. {
  267. /* Saved values of connection options */
  268. char *pghost; /* the machine on which the server is running */
  269. char *pghostaddr; /* the numeric IP address of the machine on
  270. * which the server is running. Takes
  271. * precedence over above. */
  272. char *pgport; /* the server's communication port number */
  273. char *pgunixsocket; /* the directory of the server's Unix-domain
  274. * socket; if NULL, use DEFAULT_PGSOCKET_DIR */
  275. char *pgtty; /* tty on which the backend messages is
  276. * displayed (OBSOLETE, NOT USED) */
  277. char *connect_timeout; /* connection timeout (numeric string) */
  278. char *client_encoding_initial; /* encoding to use */
  279. char *pgoptions; /* options to start the backend with */
  280. char *appname; /* application name */
  281. char *fbappname; /* fallback application name */
  282. char *dbName; /* database name */
  283. char *replication; /* connect as the replication standby? */
  284. char *pguser; /* Postgres username and password, if any */
  285. char *pgpass;
  286. char *keepalives; /* use TCP keepalives? */
  287. char *keepalives_idle; /* time between TCP keepalives */
  288. char *keepalives_interval; /* time between TCP keepalive
  289. * retransmits */
  290. char *keepalives_count; /* maximum number of TCP keepalive
  291. * retransmits */
  292. char *sslmode; /* SSL mode (require,prefer,allow,disable) */
  293. char *sslcompression; /* SSL compression (0 or 1) */
  294. char *sslkey; /* client key filename */
  295. char *sslcert; /* client certificate filename */
  296. char *sslrootcert; /* root certificate filename */
  297. char *sslcrl; /* certificate revocation list filename */
  298. char *requirepeer; /* required peer credentials for local sockets */
  299. #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
  300. char *krbsrvname; /* Kerberos service name */
  301. #endif
  302. /* Optional file to write trace info to */
  303. FILE *Pfdebug;
  304. /* Callback procedures for notice message processing */
  305. PGNoticeHooks noticeHooks;
  306. /* Event procs registered via PQregisterEventProc */
  307. PGEvent *events; /* expandable array of event data */
  308. int nEvents; /* number of active events */
  309. int eventArraySize; /* allocated array size */
  310. /* Status indicators */
  311. ConnStatusType status;
  312. PGAsyncStatusType asyncStatus;
  313. PGTransactionStatusType xactStatus; /* never changes to ACTIVE */
  314. PGQueryClass queryclass;
  315. char *last_query; /* last SQL command, or NULL if unknown */
  316. char last_sqlstate[6]; /* last reported SQLSTATE */
  317. bool options_valid; /* true if OK to attempt connection */
  318. bool nonblocking; /* whether this connection is using nonblock
  319. * sending semantics */
  320. bool singleRowMode; /* return current query result row-by-row? */
  321. char copy_is_binary; /* 1 = copy binary, 0 = copy text */
  322. int copy_already_done; /* # bytes already returned in COPY
  323. * OUT */
  324. PGnotify *notifyHead; /* oldest unreported Notify msg */
  325. PGnotify *notifyTail; /* newest unreported Notify msg */
  326. /* Connection data */
  327. /* See PQconnectPoll() for how we use 'int' and not 'pgsocket'. */
  328. pgsocket sock; /* FD for socket, PGINVALID_SOCKET if
  329. * unconnected */
  330. SockAddr laddr; /* Local address */
  331. SockAddr raddr; /* Remote address */
  332. ProtocolVersion pversion; /* FE/BE protocol version in use */
  333. int sversion; /* server version, e.g. 70401 for 7.4.1 */
  334. bool auth_req_received; /* true if any type of auth req
  335. * received */
  336. bool password_needed; /* true if server demanded a password */
  337. bool dot_pgpass_used; /* true if used .pgpass */
  338. bool sigpipe_so; /* have we masked SIGPIPE via SO_NOSIGPIPE? */
  339. bool sigpipe_flag; /* can we mask SIGPIPE via MSG_NOSIGNAL? */
  340. /* Transient state needed while establishing connection */
  341. struct addrinfo *addrlist; /* list of possible backend addresses */
  342. struct addrinfo *addr_cur; /* the one currently being tried */
  343. int addrlist_family; /* needed to know how to free addrlist */
  344. PGSetenvStatusType setenv_state; /* for 2.0 protocol only */
  345. const PQEnvironmentOption *next_eo;
  346. bool send_appname; /* okay to send application_name? */
  347. /* Miscellaneous stuff */
  348. int be_pid; /* PID of backend --- needed for cancels */
  349. int be_key; /* key of backend --- needed for cancels */
  350. char md5Salt[4]; /* password salt received from backend */
  351. pgParameterStatus *pstatus; /* ParameterStatus data */
  352. int client_encoding; /* encoding id */
  353. bool std_strings; /* standard_conforming_strings */
  354. PGVerbosity verbosity; /* error/notice message verbosity */
  355. PGContextVisibility show_context; /* whether to show CONTEXT field */
  356. PGlobjfuncs *lobjfuncs; /* private state for large-object access fns */
  357. /* Buffer for data received from backend and not yet processed */
  358. char *inBuffer; /* currently allocated buffer */
  359. int inBufSize; /* allocated size of buffer */
  360. int inStart; /* offset to first unconsumed data in buffer */
  361. int inCursor; /* next byte to tentatively consume */
  362. int inEnd; /* offset to first position after avail data */
  363. /* Buffer for data not yet sent to backend */
  364. char *outBuffer; /* currently allocated buffer */
  365. int outBufSize; /* allocated size of buffer */
  366. int outCount; /* number of chars waiting in buffer */
  367. /* State for constructing messages in outBuffer */
  368. int outMsgStart; /* offset to msg start (length word); if -1,
  369. * msg has no length word */
  370. int outMsgEnd; /* offset to msg end (so far) */
  371. /* Row processor interface workspace */
  372. PGdataValue *rowBuf; /* array for passing values to rowProcessor */
  373. int rowBufLen; /* number of entries allocated in rowBuf */
  374. /* Status for asynchronous result construction */
  375. PGresult *result; /* result being constructed */
  376. PGresult *next_result; /* next result (used in single-row mode) */
  377. /* Assorted state for SSL, GSS, etc */
  378. #ifdef USE_SSL
  379. bool allow_ssl_try; /* Allowed to try SSL negotiation */
  380. bool wait_ssl_try; /* Delay SSL negotiation until after
  381. * attempting normal connection */
  382. bool ssl_in_use;
  383. #ifdef USE_OPENSSL
  384. SSL *ssl; /* SSL status, if have SSL connection */
  385. X509 *peer; /* X509 cert of server */
  386. #ifdef USE_SSL_ENGINE
  387. ENGINE *engine; /* SSL engine, if any */
  388. #else
  389. void *engine; /* dummy field to keep struct the same if
  390. * OpenSSL version changes */
  391. #endif
  392. #endif /* USE_OPENSSL */
  393. #endif /* USE_SSL */
  394. #ifdef ENABLE_GSS
  395. gss_ctx_id_t gctx; /* GSS context */
  396. gss_name_t gtarg_nam; /* GSS target name */
  397. gss_buffer_desc ginbuf; /* GSS input token */
  398. gss_buffer_desc goutbuf; /* GSS output token */
  399. #endif
  400. #ifdef ENABLE_SSPI
  401. #ifndef ENABLE_GSS
  402. gss_buffer_desc ginbuf; /* GSS input token */
  403. #else
  404. char *gsslib; /* What GSS librart to use ("gssapi" or
  405. * "sspi") */
  406. #endif
  407. CredHandle *sspicred; /* SSPI credentials handle */
  408. CtxtHandle *sspictx; /* SSPI context */
  409. char *sspitarget; /* SSPI target name */
  410. int usesspi; /* Indicate if SSPI is in use on the
  411. * connection */
  412. #endif
  413. /* Buffer for current error message */
  414. PQExpBufferData errorMessage; /* expansible string */
  415. /* Buffer for receiving various parts of messages */
  416. PQExpBufferData workBuffer; /* expansible string */
  417. };
  418. /* PGcancel stores all data necessary to cancel a connection. A copy of this
  419. * data is required to safely cancel a connection running on a different
  420. * thread.
  421. */
  422. struct pg_cancel
  423. {
  424. SockAddr raddr; /* Remote address */
  425. int be_pid; /* PID of backend --- needed for cancels */
  426. int be_key; /* key of backend --- needed for cancels */
  427. };
  428. /* String descriptions of the ExecStatusTypes.
  429. * direct use of this array is deprecated; call PQresStatus() instead.
  430. */
  431. extern char *const pgresStatus[];
  432. #ifdef USE_SSL
  433. #ifndef WIN32
  434. #define USER_CERT_FILE ".postgresql/postgresql.crt"
  435. #define USER_KEY_FILE ".postgresql/postgresql.key"
  436. #define ROOT_CERT_FILE ".postgresql/root.crt"
  437. #define ROOT_CRL_FILE ".postgresql/root.crl"
  438. #else
  439. /* On Windows, the "home" directory is already PostgreSQL-specific */
  440. #define USER_CERT_FILE "postgresql.crt"
  441. #define USER_KEY_FILE "postgresql.key"
  442. #define ROOT_CERT_FILE "root.crt"
  443. #define ROOT_CRL_FILE "root.crl"
  444. #endif
  445. #endif /* USE_SSL */
  446. /* ----------------
  447. * Internal functions of libpq
  448. * Functions declared here need to be visible across files of libpq,
  449. * but are not intended to be called by applications. We use the
  450. * convention "pqXXX" for internal functions, vs. the "PQxxx" names
  451. * used for application-visible routines.
  452. * ----------------
  453. */
  454. /* === in fe-connect.c === */
  455. extern void pqDropConnection(PGconn *conn, bool flushInput);
  456. extern int pqPacketSend(PGconn *conn, char pack_type,
  457. const void *buf, size_t buf_len);
  458. extern bool pqGetHomeDirectory(char *buf, int bufsize);
  459. #ifdef ENABLE_THREAD_SAFETY
  460. extern pgthreadlock_t pg_g_threadlock;
  461. #define PGTHREAD_ERROR(msg) \
  462. do { \
  463. fprintf(stderr, "%s\n", msg); \
  464. abort(); \
  465. } while (0)
  466. #define pglock_thread() pg_g_threadlock(true)
  467. #define pgunlock_thread() pg_g_threadlock(false)
  468. #else
  469. #define pglock_thread() ((void) 0)
  470. #define pgunlock_thread() ((void) 0)
  471. #endif
  472. /* === in fe-exec.c === */
  473. extern void pqSetResultError(PGresult *res, const char *msg);
  474. extern void pqCatenateResultError(PGresult *res, const char *msg);
  475. extern void *pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary);
  476. extern char *pqResultStrdup(PGresult *res, const char *str);
  477. extern void pqClearAsyncResult(PGconn *conn);
  478. extern void pqSaveErrorResult(PGconn *conn);
  479. extern PGresult *pqPrepareAsyncResult(PGconn *conn);
  480. extern void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...) pg_attribute_printf(2, 3);
  481. extern void pqSaveMessageField(PGresult *res, char code,
  482. const char *value);
  483. extern void pqSaveParameterStatus(PGconn *conn, const char *name,
  484. const char *value);
  485. extern int pqRowProcessor(PGconn *conn, const char **errmsgp);
  486. extern void pqHandleSendFailure(PGconn *conn);
  487. /* === in fe-protocol2.c === */
  488. extern PostgresPollingStatusType pqSetenvPoll(PGconn *conn);
  489. extern char *pqBuildStartupPacket2(PGconn *conn, int *packetlen,
  490. const PQEnvironmentOption *options);
  491. extern void pqParseInput2(PGconn *conn);
  492. extern int pqGetCopyData2(PGconn *conn, char **buffer, int async);
  493. extern int pqGetline2(PGconn *conn, char *s, int maxlen);
  494. extern int pqGetlineAsync2(PGconn *conn, char *buffer, int bufsize);
  495. extern int pqEndcopy2(PGconn *conn);
  496. extern PGresult *pqFunctionCall2(PGconn *conn, Oid fnid,
  497. int *result_buf, int *actual_result_len,
  498. int result_is_int,
  499. const PQArgBlock *args, int nargs);
  500. /* === in fe-protocol3.c === */
  501. extern char *pqBuildStartupPacket3(PGconn *conn, int *packetlen,
  502. const PQEnvironmentOption *options);
  503. extern void pqParseInput3(PGconn *conn);
  504. extern int pqGetErrorNotice3(PGconn *conn, bool isError);
  505. extern void pqBuildErrorMessage3(PQExpBuffer msg, const PGresult *res,
  506. PGVerbosity verbosity, PGContextVisibility show_context);
  507. extern int pqGetCopyData3(PGconn *conn, char **buffer, int async);
  508. extern int pqGetline3(PGconn *conn, char *s, int maxlen);
  509. extern int pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize);
  510. extern int pqEndcopy3(PGconn *conn);
  511. extern PGresult *pqFunctionCall3(PGconn *conn, Oid fnid,
  512. int *result_buf, int *actual_result_len,
  513. int result_is_int,
  514. const PQArgBlock *args, int nargs);
  515. /* === in fe-misc.c === */
  516. /*
  517. * "Get" and "Put" routines return 0 if successful, EOF if not. Note that for
  518. * Get, EOF merely means the buffer is exhausted, not that there is
  519. * necessarily any error.
  520. */
  521. extern int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn);
  522. extern int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn);
  523. extern int pqGetc(char *result, PGconn *conn);
  524. extern int pqPutc(char c, PGconn *conn);
  525. extern int pqGets(PQExpBuffer buf, PGconn *conn);
  526. extern int pqGets_append(PQExpBuffer buf, PGconn *conn);
  527. extern int pqPuts(const char *s, PGconn *conn);
  528. extern int pqGetnchar(char *s, size_t len, PGconn *conn);
  529. extern int pqSkipnchar(size_t len, PGconn *conn);
  530. extern int pqPutnchar(const char *s, size_t len, PGconn *conn);
  531. extern int pqGetInt(int *result, size_t bytes, PGconn *conn);
  532. extern int pqPutInt(int value, size_t bytes, PGconn *conn);
  533. extern int pqPutMsgStart(char msg_type, bool force_len, PGconn *conn);
  534. extern int pqPutMsgEnd(PGconn *conn);
  535. extern int pqReadData(PGconn *conn);
  536. extern int pqFlush(PGconn *conn);
  537. extern int pqWait(int forRead, int forWrite, PGconn *conn);
  538. extern int pqWaitTimed(int forRead, int forWrite, PGconn *conn,
  539. time_t finish_time);
  540. extern int pqReadReady(PGconn *conn);
  541. extern int pqWriteReady(PGconn *conn);
  542. /* === in fe-secure.c === */
  543. extern int pqsecure_initialize(PGconn *);
  544. extern void pqsecure_destroy(void);
  545. extern PostgresPollingStatusType pqsecure_open_client(PGconn *);
  546. extern void pqsecure_close(PGconn *);
  547. extern ssize_t pqsecure_read(PGconn *, void *ptr, size_t len);
  548. extern ssize_t pqsecure_write(PGconn *, const void *ptr, size_t len);
  549. extern ssize_t pqsecure_raw_read(PGconn *, void *ptr, size_t len);
  550. extern ssize_t pqsecure_raw_write(PGconn *, const void *ptr, size_t len);
  551. #if defined(ENABLE_THREAD_SAFETY) && !defined(WIN32)
  552. extern int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending);
  553. extern void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending,
  554. bool got_epipe);
  555. #endif
  556. /*
  557. * The SSL implementation provides these functions (fe-secure-openssl.c)
  558. */
  559. extern void pgtls_init_library(bool do_ssl, int do_crypto);
  560. extern int pgtls_init(PGconn *conn);
  561. extern PostgresPollingStatusType pgtls_open_client(PGconn *conn);
  562. extern void pgtls_close(PGconn *conn);
  563. extern ssize_t pgtls_read(PGconn *conn, void *ptr, size_t len);
  564. extern bool pgtls_read_pending(PGconn *conn);
  565. extern ssize_t pgtls_write(PGconn *conn, const void *ptr, size_t len);
  566. /*
  567. * this is so that we can check if a connection is non-blocking internally
  568. * without the overhead of a function call
  569. */
  570. #define pqIsnonblocking(conn) ((conn)->nonblocking)
  571. #ifdef ENABLE_NLS
  572. extern char *libpq_gettext(const char *msgid) pg_attribute_format_arg(1);
  573. extern char *libpq_ngettext(const char *msgid, const char *msgid_plural, unsigned long n) pg_attribute_format_arg(1) pg_attribute_format_arg(2);
  574. #else
  575. #define libpq_gettext(x) (x)
  576. #define libpq_ngettext(s, p, n) ((n) == 1 ? (s) : (p))
  577. #endif
  578. /*
  579. * These macros are needed to let error-handling code be portable between
  580. * Unix and Windows. (ugh)
  581. */
  582. #ifdef WIN32
  583. #define SOCK_ERRNO (WSAGetLastError())
  584. #define SOCK_STRERROR winsock_strerror
  585. #define SOCK_ERRNO_SET(e) WSASetLastError(e)
  586. #else
  587. #define SOCK_ERRNO errno
  588. #define SOCK_STRERROR pqStrerror
  589. #define SOCK_ERRNO_SET(e) (errno = (e))
  590. #endif
  591. #endif /* LIBPQ_INT_H */