]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/libpq-int.h
Back out kerberos changes. Causes compile problems.
[postgresql] / src / interfaces / libpq / libpq-int.h
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-2000, PostgreSQL, Inc
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * $Id: libpq-int.h,v 1.25 2000/05/27 03:58:20 momjian Exp $
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
25 /* include stuff common to fe and be */
26 #include "libpq/pqcomm.h"
27 #include "lib/dllist.h"
28 /* include stuff found in fe only */
29 #include "pqexpbuffer.h"
30
31 #ifdef USE_SSL
32 #include <openssl/ssl.h>
33 #include <openssl/err.h>
34 #endif
35
36 /* libpq supports this version of the frontend/backend protocol.
37  *
38  * NB: we used to use PG_PROTOCOL_LATEST from the backend pqcomm.h file,
39  * but that's not really the right thing: just recompiling libpq
40  * against a more recent backend isn't going to magically update it
41  * for most sorts of protocol changes.  So, when you change libpq
42  * to support a different protocol revision, you have to change this
43  * constant too.  PG_PROTOCOL_EARLIEST and PG_PROTOCOL_LATEST in
44  * pqcomm.h describe what the backend knows, not what libpq knows.
45  */
46
47 #define PG_PROTOCOL_LIBPQ       PG_PROTOCOL(2,0)
48
49 /*
50  * POSTGRES backend dependent Constants.
51  */
52
53 #define CMDSTATUS_LEN 40
54
55 /*
56  * PGresult and the subsidiary types PGresAttDesc, PGresAttValue
57  * represent the result of a query (or more precisely, of a single SQL
58  * command --- a query string given to PQexec can contain multiple commands).
59  * Note we assume that a single command can return at most one tuple group,
60  * hence there is no need for multiple descriptor sets.
61  */
62
63 /* Subsidiary-storage management structure for PGresult.
64  * See space management routines in fe-exec.c for details.
65  * Note that space[k] refers to the k'th byte starting from the physical
66  * head of the block --- it's a union, not a struct!
67  */
68 typedef union pgresult_data PGresult_data;
69
70 union pgresult_data
71 {
72         PGresult_data *next;            /* link to next block, or NULL */
73         char            space[1];               /* dummy for accessing block as bytes */
74 };
75
76 /* Data about a single attribute (column) of a query result */
77
78 typedef struct pgresAttDesc
79 {
80         char       *name;                       /* type name */
81         Oid                     typid;                  /* type id */
82         int                     typlen;                 /* type size */
83         int                     atttypmod;              /* type-specific modifier info */
84 }                       PGresAttDesc;
85
86 /* Data for a single attribute of a single tuple */
87
88 /* We use char* for Attribute values.
89    The value pointer always points to a null-terminated area; we add a
90    null (zero) byte after whatever the backend sends us.  This is only
91    particularly useful for ASCII tuples ... with a binary value, the
92    value might have embedded nulls, so the application can't use C string
93    operators on it.  But we add a null anyway for consistency.
94    Note that the value itself does not contain a length word.
95
96    A NULL attribute is a special case in two ways: its len field is NULL_LEN
97    and its value field points to null_field in the owning PGresult.  All the
98    NULL attributes in a query result point to the same place (there's no need
99    to store a null string separately for each one).
100  */
101
102 #define NULL_LEN                (-1)    /* pg_result len for NULL value */
103
104 typedef struct pgresAttValue
105 {
106         int                     len;                    /* length in bytes of the value */
107         char       *value;                      /* actual value, plus terminating zero
108                                                                  * byte */
109 }                       PGresAttValue;
110
111 struct pg_result
112 {
113         int                     ntups;
114         int                     numAttributes;
115         PGresAttDesc *attDescs;
116         PGresAttValue **tuples;         /* each PGresTuple is an array of
117                                                                  * PGresAttValue's */
118         int                     tupArrSize;             /* size of tuples array allocated */
119         ExecStatusType resultStatus;
120         char            cmdStatus[CMDSTATUS_LEN];               /* cmd status from the
121                                                                                                  * last query */
122         int                     binary;                 /* binary tuple values if binary == 1,
123                                                                  * otherwise ASCII */
124
125         /*
126          * The conn link in PGresult is no longer used by any libpq code. It
127          * should be removed entirely, because it could be a dangling link
128          * (the application could keep the PGresult around longer than it
129          * keeps the PGconn!)  But there may be apps out there that depend on
130          * it, so we will leave it here at least for a release or so.
131          */
132         PGconn     *xconn;                      /* connection we did the query on, if any */
133
134         /*
135          * These fields are copied from the originating PGconn, so that
136          * operations on the PGresult don't have to reference the PGconn.
137          */
138         PQnoticeProcessor noticeHook;           /* notice/error message processor */
139         void       *noticeArg;
140         int                     client_encoding;/* encoding id */
141
142
143         char       *errMsg;                     /* error message, or NULL if no error */
144
145         /* All NULL attributes in the query result point to this null string */
146         char            null_field[1];
147
148         /*
149          * Space management information.  Note that attDescs and errMsg, if
150          * not null, point into allocated blocks.  But tuples points to a
151          * separately malloc'd block, so that we can realloc it.
152          */
153         PGresult_data *curBlock;        /* most recently allocated block */
154         int                     curOffset;              /* start offset of free space in block */
155         int                     spaceLeft;              /* number of free bytes remaining in block */
156 };
157
158 /* PGAsyncStatusType defines the state of the query-execution state machine */
159 typedef enum
160 {
161         PGASYNC_IDLE,                           /* nothing's happening, dude */
162         PGASYNC_BUSY,                           /* query in progress */
163         PGASYNC_READY,                          /* result ready for PQgetResult */
164         PGASYNC_COPY_IN,                        /* Copy In data transfer in progress */
165         PGASYNC_COPY_OUT                        /* Copy Out data transfer in progress */
166 }                       PGAsyncStatusType;
167
168 /* PGSetenvStatusType defines the state of the PQSetenv state machine */
169 typedef enum
170 {
171         SETENV_STATE_OPTION_SEND,       /* About to send an Environment Option */
172         SETENV_STATE_OPTION_WAIT,       /* Waiting for above send to complete  */
173         /* these next two are only used in MULTIBYTE mode */
174         SETENV_STATE_ENCODINGS_SEND,/* About to send an "encodings" query */
175         SETENV_STATE_ENCODINGS_WAIT,/* Waiting for query to complete      */
176         SETENV_STATE_IDLE
177 }                       PGSetenvStatusType;
178
179 /* large-object-access data ... allocated only if large-object code is used. */
180 typedef struct pgLobjfuncs
181 {
182         Oid                     fn_lo_open;             /* OID of backend function lo_open              */
183         Oid                     fn_lo_close;    /* OID of backend function lo_close             */
184         Oid                     fn_lo_creat;    /* OID of backend function lo_creat             */
185         Oid                     fn_lo_unlink;   /* OID of backend function lo_unlink    */
186         Oid                     fn_lo_lseek;    /* OID of backend function lo_lseek             */
187         Oid                     fn_lo_tell;             /* OID of backend function lo_tell              */
188         Oid                     fn_lo_read;             /* OID of backend function LOread               */
189         Oid                     fn_lo_write;    /* OID of backend function LOwrite              */
190 }                       PGlobjfuncs;
191
192 /* PGconn stores all the state data associated with a single connection
193  * to a backend.
194  */
195 struct pg_conn
196 {
197         /* Saved values of connection options */
198         char       *pghost;                     /* the machine on which the server is
199                                                                  * running */
200         char       *pghostaddr;         /* the IPv4 address of the machine on
201                                                                  * which the server is running, in IPv4
202                                                                  * numbers-and-dots notation. Takes
203                                                                  * precedence over above. */
204         char       *pgport;                     /* the server's communication port */
205         char       *pgtty;                      /* tty on which the backend messages is
206                                                                  * displayed (NOT ACTUALLY USED???) */
207         char       *pgoptions;          /* options to start the backend with */
208         char       *dbName;                     /* database name */
209         char       *pguser;                     /* Postgres username and password, if any */
210         char       *pgpass;
211
212         /* Optional file to write trace info to */
213         FILE       *Pfdebug;
214
215         /* Callback procedure for notice/error message processing */
216         PQnoticeProcessor noticeHook;
217         void       *noticeArg;
218
219         /* Status indicators */
220         ConnStatusType status;
221         PGAsyncStatusType asyncStatus;
222         Dllist     *notifyList;         /* Notify msgs not yet handed to
223                                                                  * application */
224
225         /* Connection data */
226         int                     sock;                   /* Unix FD for socket, -1 if not connected */
227         SockAddr        laddr;                  /* Local address */
228         SockAddr        raddr;                  /* Remote address */
229         int                     raddr_len;              /* Length of remote address */
230
231         /* Miscellaneous stuff */
232         int                     be_pid;                 /* PID of backend --- needed for cancels */
233         int                     be_key;                 /* key of backend --- needed for cancels */
234         char            salt[2];                /* password salt received from backend */
235         PGlobjfuncs *lobjfuncs;         /* private state for large-object access
236                                                                  * fns */
237
238         /* Buffer for data received from backend and not yet processed */
239         char       *inBuffer;           /* currently allocated buffer */
240         int                     inBufSize;              /* allocated size of buffer */
241         int                     inStart;                /* offset to first unconsumed data in
242                                                                  * buffer */
243         int                     inCursor;               /* next byte to tentatively consume */
244         int                     inEnd;                  /* offset to first position after avail
245                                                                  * data */
246
247         int                     nonblocking;    /* whether this connection is using a
248                                                                  * blocking socket to the backend or not */
249
250         /* Buffer for data not yet sent to backend */
251         char       *outBuffer;          /* currently allocated buffer */
252         int                     outBufSize;             /* allocated size of buffer */
253         int                     outCount;               /* number of chars waiting in buffer */
254
255         /* Status for asynchronous result construction */
256         PGresult   *result;                     /* result being constructed */
257         PGresAttValue *curTuple;        /* tuple currently being read */
258
259         /* Status for sending environment info.  Used during PQSetenv only. */
260         PGSetenvStatusType setenv_state;
261         const struct EnvironmentOptions *next_eo;
262
263 #ifdef USE_SSL
264         bool            allow_ssl_try;  /* Allowed to try SSL negotiation */
265         SSL                *ssl;                        /* SSL status, if have SSL connection */
266 #endif
267
268         /* Buffer for current error message */
269         PQExpBufferData errorMessage;           /* expansible string */
270
271         /* Buffer for receiving various parts of messages */
272         PQExpBufferData workBuffer; /* expansible string */
273
274         int                     client_encoding;/* encoding id */
275 };
276
277 /* String descriptions of the ExecStatusTypes.
278  * direct use of this array is deprecated; call PQresStatus() instead.
279  */
280 extern char *const pgresStatus[];
281
282 /* ----------------
283  * Internal functions of libpq
284  * Functions declared here need to be visible across files of libpq,
285  * but are not intended to be called by applications.  We use the
286  * convention "pqXXX" for internal functions, vs. the "PQxxx" names
287  * used for application-visible routines.
288  * ----------------
289  */
290
291 /* === in fe-connect.c === */
292
293 extern int      pqPacketSend(PGconn *conn, const char *buf, size_t len);
294
295 /* === in fe-exec.c === */
296
297 extern void pqSetResultError(PGresult *res, const char *msg);
298 extern void *pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary);
299 extern char *pqResultStrdup(PGresult *res, const char *str);
300 extern void pqClearAsyncResult(PGconn *conn);
301
302 /* === in fe-misc.c === */
303
304  /*
305   * "Get" and "Put" routines return 0 if successful, EOF if not. Note that
306   * for Get, EOF merely means the buffer is exhausted, not that there is
307   * necessarily any error.
308   */
309 extern int      pqGetc(char *result, PGconn *conn);
310 extern int      pqGets(PQExpBuffer buf, PGconn *conn);
311 extern int      pqPuts(const char *s, PGconn *conn);
312 extern int      pqGetnchar(char *s, size_t len, PGconn *conn);
313 extern int      pqPutnchar(const char *s, size_t len, PGconn *conn);
314 extern int      pqGetInt(int *result, size_t bytes, PGconn *conn);
315 extern int      pqPutInt(int value, size_t bytes, PGconn *conn);
316 extern int      pqReadData(PGconn *conn);
317 extern int      pqFlush(PGconn *conn);
318 extern int      pqWait(int forRead, int forWrite, PGconn *conn);
319 extern int      pqReadReady(PGconn *conn);
320 extern int      pqWriteReady(PGconn *conn);
321
322 /* bits in a byte */
323 #define BYTELEN 8
324
325 /* fall back options if they are not specified by arguments or defined
326    by environment variables */
327 #define DefaultHost             "localhost"
328 #define DefaultTty              ""
329 #define DefaultOption   ""
330 #define DefaultAuthtype           ""
331 #define DefaultPassword           ""
332
333 /* supply an implementation of strerror() macro if system doesn't have it */
334 #ifndef strerror
335 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
336 extern char *sys_errlist[];
337
338 #define strerror(A) (sys_errlist[(A)])
339 #endif   /* sunos4 */
340 #endif   /* !strerror */
341
342 /*
343  * this is so that we can check is a connection is non-blocking internally
344  * without the overhead of a function call
345  */
346 #define pqIsnonblocking(conn)   (conn->nonblocking)
347
348 #endif   /* LIBPQ_INT_H */