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
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>
26 #define PG_MAJOR_VER 3
27 #define PG_MINOR_VER 0
31 #define PG_PARAMSTATUS 'S'
32 #define PG_BACKENDKEYDATA 'K'
36 #define PG_SIMPLEQUERY 'Q'
37 #define PG_ROWDESC 'T'
38 #define PG_DATAROW 'D'
39 #define PG_CMDCOMPLETE 'C'
41 #define PG_PARSECOMPLETE '1'
43 #define PG_BINDCOMPLETE '2'
44 #define PG_DESCRIBE 'D'
45 #define PG_EXECUTE 'E'
48 #define PG_PARAMDESC 't'
49 #define PG_PORTALSUSPENDED 's'
50 #define PG_EMPTYQUERY 'I'
53 #define PG_COPYDATA 'd'
54 #define PG_COPYEND 'c'
60 #define PG_SEVERITY 'S'
62 #define PG_SQLSTATE 'C'
63 #define PG_MESSAGE 'M'
64 #define PG_POSITION 'P'
67 #define PG_ROUTINE 'R'
73 #define PG_PARAM_END -1
78 #define PG_PREPARED_OPERATOR 'S'
79 #define PG_PREPARED_PORTAL 'P'
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
;
102 } __attribute__ ((packed
)) pgmsg_body_t
;
104 typedef struct pgmsg
{
112 struct sockaddr_in in_addr
;
116 char srv_sign
[SCRAM_KEY_LEN
+1];
117 str_t
*srv_scram_msg
;
121 char salted_password
[SCRAM_KEY_LEN
];
137 uint8_t md5_auth
[4];
144 } pgmsg_sasl_initresp_t
;
150 } pgmsg_param_status_t
;
156 } pgmsg_backend_keydata_t
;
189 pgmsg_field_t fields
[0];
200 pgmsg_data_t fields
[0];
206 } pgmsg_cmd_complete_t
;
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
;
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
);