fix
[libpgclient.git] / include / libpgcli / pgprov3.h
blob8fe9f598dfb36b7619cd84feb99b9594fb1a22e6
1 /*Copyright (c) Brian B.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later version.
7 See the file LICENSE included with this distribution for more
8 information.
9 */
10 #ifndef __PGPROV3_H__
11 #define __PGPROV3_H__
13 #include <stdint.h>
14 #include <errno.h>
15 #include <endian.h>
16 #include <stdarg.h>
17 #include <limits.h>
18 #include <termios.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <libex/str.h>
22 #include <libex/list.h>
23 #include <libex/array.h>
24 #include "pgfld.h"
26 #define PG_MAJOR_VER 3
27 #define PG_MINOR_VER 0
29 #define PG_AUTHOK 'R'
30 #define PG_PASS 'p'
31 #define PG_PARAMSTATUS 'S'
32 #define PG_BACKENDKEYDATA 'K'
33 #define PG_READY 'Z'
34 #define PG_TERM 'X'
35 #define PG_ERROR 'E'
36 #define PG_SIMPLEQUERY 'Q'
37 #define PG_ROWDESC 'T'
38 #define PG_DATAROW 'D'
39 #define PG_CMDCOMPLETE 'C'
40 #define PG_PARSE 'P'
41 #define PG_PARSECOMPLETE '1'
42 #define PG_BIND 'B'
43 #define PG_BINDCOMPLETE '2'
44 #define PG_DESCRIBE 'D'
45 #define PG_EXECUTE 'E'
46 #define PG_SYNC 'S'
47 #define PG_NODATA 'n'
48 #define PG_PARAMDESC 't'
49 #define PG_PORTALSUSPENDED 's'
50 #define PG_EMPTYQUERY 'I'
51 #define PG_CLOSE 'C'
52 #define PG_COPYIN 'G'
53 #define PG_COPYDATA 'd'
54 #define PG_COPYEND 'c'
56 #define PG_IDLE 'I'
57 #define PG_ACTIVE 'T'
58 #define PG_EXCEPT 'E'
60 #define PG_SEVERITY 'S'
61 #define PG_FATAL 'V'
62 #define PG_SQLSTATE 'C'
63 #define PG_MESSAGE 'M'
64 #define PG_POSITION 'P'
65 #define PG_FILE 'F'
66 #define PG_LINE 'L'
67 #define PG_ROUTINE 'R'
68 #define PG_DETAIL 'D'
70 #define PG_OPNAME 'S'
71 #define PG_PORTAL 'P'
73 #define PG_PARAM_END -1
75 #define PG_TEXT 0
76 #define PG_BIN 1
78 #define PG_PREPARED_OPERATOR 'S'
79 #define PG_PREPARED_PORTAL 'P'
81 #define PG_FULL 0
83 #define PG_OK 0
84 #define PG_REQMD5 5
85 #define PG_REQPASS 3
86 #define PG_REQSASL 10
87 #define PG_SASLCON 11
88 #define PG_SASLCOMP 12
90 #define PG_MD5PASS_LEN 35
92 #define SCRAM_RAW_NONCE_LEN 18
93 #define SCRAM_NONCE_LEN 24
94 #define SCRAM_KEY_LEN 32
96 typedef int32_t oid_t;
98 typedef struct {
99 char type;
100 int32_t len;
101 char ptr [0];
102 } __attribute__ ((packed)) pgmsg_body_t;
104 typedef struct pgmsg {
105 size_t bufsize;
106 size_t len;
107 char *pc;
108 pgmsg_body_t body;
109 } pgmsg_t;
111 typedef struct {
112 struct sockaddr_in in_addr;
113 char *user;
114 char *pass;
115 char salt [4];
116 char srv_sign [SCRAM_KEY_LEN+1];
117 str_t *srv_scram_msg;
118 char *r_attr;
119 char *s_attr;
120 char *i_attr;
121 char salted_password [SCRAM_KEY_LEN];
122 cstr_t *nonce;
123 cstr_t *fmsg_bare;
124 cstr_t *fmsg_srv;
125 cstr_t *fmsg_wproof;
126 } conninfo_t;
128 typedef struct {
129 int32_t len;
130 uint8_t data [0];
131 } sasl_t;
133 typedef struct {
134 char type;
135 int32_t success;
136 union {
137 uint8_t md5_auth [4];
138 sasl_t sasl_auth;
139 } kind;
140 } pgmsg_auth_t;
142 typedef struct {
143 int32_t kind;
144 } pgmsg_sasl_initresp_t;
146 typedef struct {
147 char type;
148 const char *name;
149 const char *value;
150 } pgmsg_param_status_t;
152 typedef struct {
153 char type;
154 int32_t pid;
155 int32_t sk;
156 } pgmsg_backend_keydata_t;
158 typedef struct {
159 char type;
160 char tr;
161 } pgmsg_ready_t;
163 typedef struct {
164 char type;
165 char *severity;
166 char *text;
167 char *code;
168 char *message;
169 char *position;
170 char *file;
171 char *line;
172 char *routine;
173 char *detail;
174 } pgmsg_error_t;
176 typedef struct {
177 const char *fname;
178 int32_t oid_table;
179 int16_t idx_field;
180 int32_t oid_field;
181 int16_t field_len;
182 int32_t type_mod;
183 int16_t field_fmt;
184 } pgmsg_field_t;
186 typedef struct {
187 char type;
188 int16_t nflds;
189 pgmsg_field_t fields [0];
190 } pgmsg_rowdesc_t;
192 typedef struct {
193 int32_t len;
194 uint8_t *data;
195 } pgmsg_data_t;
197 typedef struct {
198 char type;
199 int16_t nflds;
200 pgmsg_data_t fields [0];
201 } pgmsg_datarow_t;
203 typedef struct {
204 char type;
205 const char *tag;
206 } pgmsg_cmd_complete_t;
208 typedef struct {
209 int8_t fmt;
210 int16_t cols;
211 int16_t fmtcol;
212 } pgmsg_copyin_t;
214 typedef union {
215 char type;
216 pgmsg_auth_t msg_auth;
217 pgmsg_param_status_t msg_param_status;
218 pgmsg_backend_keydata_t msg_backend_keydata;
219 pgmsg_ready_t msg_ready;
220 pgmsg_error_t msg_error;
221 pgmsg_rowdesc_t msg_rowdesc;
222 pgmsg_datarow_t msg_datarow;
223 pgmsg_cmd_complete_t msg_complete;
224 pgmsg_copyin_t msg_copyin;
225 } pgmsg_resp_t;
227 extern char*(*on_pgauth) (const char *prompt, int is_echo);
229 typedef void* (*parse_param_h) (void*, const char*, const char*, uint32_t*);
230 void *parse_conninfo (void *data, const char *conn_info, parse_param_h fn, uint32_t *flags);
231 int pgmsg_set_param (pgmsg_t **msg, const char *name, size_t name_len, const char *value, size_t value_len);
233 pgmsg_t *pgmsg_create (char type);
234 pgmsg_t *pgmsg_create_startup (const char *user, size_t user_len, const char *database, size_t database_len);
235 pgmsg_t *pgmsg_create_startup_params (const char *conn_info);
236 pgmsg_t *pgmsg_create_pass (int req, const char *salt, size_t salt_len, const char *user, const char *pass);
237 pgmsg_t *pgmsg_create_sasl_init (conninfo_t *cinfo);
238 pgmsg_t *pgmsg_create_sasl_fin (pgmsg_resp_t *resp, conninfo_t *cinfo);
239 pgmsg_t *pgmsg_create_simple_query (const char *sql, size_t sql_len);
240 pgmsg_t *pgmsg_create_parse (const char *name, size_t name_len, const char *sql, size_t sql_len, int fld_len, pgfld_t **flds);
241 pgmsg_t *pgmsg_create_bind (const char *portal, size_t portal_len, const char *stmt, size_t stmt_len,
242 int fld_len, pgfld_t **flds, int res_fmt_len, int *res_fmt);
243 pgmsg_t *pgmsg_create_describe (uint8_t op, const char *name, size_t name_len);
244 pgmsg_t *pgmsg_create_execute (const char *portal, size_t portal_len, int32_t max_rows);
245 pgmsg_t *pgmsg_create_close(char what, const char *str, size_t slen);
246 static inline pgmsg_t *pgmsg_create_sync () { return pgmsg_create(PG_SYNC); };
247 static inline pgmsg_t *pgmsg_create_term () { return pgmsg_create(PG_TERM); };
248 pgmsg_t *pgmsg_copyin_flds (int len, pgfld_t **flds);
250 int pgmsg_send (int fd, pgmsg_t *msg);
251 int pgmsg_recv (int fd, pgmsg_t **msg);
253 pgmsg_resp_t *pgmsg_parse (pgmsg_t *msg);
255 #endif