Use AC_PROG_INSTALL & ./install-sh
[pgbouncer.git] / include / pktbuf.h
blob638aaf724b66af8a39085a35816faf3499788475
1 /*
2 * PgBouncer - Lightweight connection pooler for PostgreSQL.
3 *
4 * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Safe & easy creation of PostgreSQL packets.
23 typedef struct PktBuf PktBuf;
24 struct PktBuf {
25 uint8_t *buf;
26 int buf_len;
27 int write_pos;
28 int pktlen_pos;
30 int send_pos;
31 struct event *ev;
33 unsigned failed:1;
34 unsigned sending:1;
35 unsigned fixed_buf:1;
39 * pktbuf creation
41 PktBuf *pktbuf_dynamic(int start_len) _MUSTCHECK;
42 void pktbuf_static(PktBuf *buf, uint8_t *data, int len);
45 * sending
47 bool pktbuf_send_immidiate(PktBuf *buf, PgSocket *sk) _MUSTCHECK;
48 bool pktbuf_send_queued(PktBuf *buf, PgSocket *sk) _MUSTCHECK;
51 * low-level ops
53 void pktbuf_start_packet(PktBuf *buf, int type);
54 void pktbuf_put_char(PktBuf *buf, char val);
55 void pktbuf_put_uint16(PktBuf *buf, uint16_t val);
56 void pktbuf_put_uint32(PktBuf *buf, uint32_t val);
57 void pktbuf_put_uint64(PktBuf *buf, uint64_t val);
58 void pktbuf_put_string(PktBuf *buf, const char *str);
59 void pktbuf_put_bytes(PktBuf *buf, const void *data, int len);
60 void pktbuf_finish_packet(PktBuf *buf);
61 #define pktbuf_written(buf) ((buf)->write_pos)
65 * Packet writing
67 void pktbuf_write_generic(PktBuf *buf, int type, const char *fmt, ...);
68 void pktbuf_write_RowDescription(PktBuf *buf, const char *tupdesc, ...);
69 void pktbuf_write_DataRow(PktBuf *buf, const char *tupdesc, ...);
72 * Shortcuts for actual packets.
74 #define pktbuf_write_ParameterStatus(buf, key, val) \
75 pktbuf_write_generic(buf, 'S', "ss", key, val)
77 #define pktbuf_write_AuthenticationOk(buf) \
78 pktbuf_write_generic(buf, 'R', "i", 0)
80 #define pktbuf_write_ReadyForQuery(buf) \
81 pktbuf_write_generic(buf, 'Z', "c", 'I')
83 #define pktbuf_write_CommandComplete(buf, desc) \
84 pktbuf_write_generic(buf, 'C', "s", desc)
86 #define pktbuf_write_BackendKeyData(buf, key) \
87 pktbuf_write_generic(buf, 'K', "b", key, 8)
89 #define pktbuf_write_CancelRequest(buf, key) \
90 pktbuf_write_generic(buf, PKT_CANCEL, "b", key, 8)
92 #define pktbuf_write_StartupMessage(buf, user, parms, parms_len) \
93 pktbuf_write_generic(buf, PKT_STARTUP, "bsss", parms, parms_len, "user", user, "")
95 #define pktbuf_write_PasswordMessage(buf, psw) \
96 pktbuf_write_generic(buf, 'p', "s", psw)
98 #define pktbuf_write_Notice(buf, msg) \
99 pktbuf_write_generic(buf, 'N', "sscss", "SNOTICE", "C00000", 'M', msg, "");
102 * Shortcut for creating DataRow in memory.
105 #define BUILD_DataRow(reslen, dst, dstlen, args...) do { \
106 PktBuf _buf; \
107 pktbuf_static(&_buf, dst, dstlen); \
108 pktbuf_write_DataRow(&_buf, ## args); \
109 reslen = _buf.failed ? -1 : _buf.write_pos; \
110 } while (0)
113 * Shortcuts for immidiate send of one packet.
116 #define SEND_wrap(buflen, pktfn, res, sk, args...) do { \
117 uint8_t _data[buflen]; PktBuf _buf; \
118 pktbuf_static(&_buf, _data, sizeof(_data)); \
119 pktfn(&_buf, ## args); \
120 res = pktbuf_send_immidiate(&_buf, sk); \
121 } while (0)
123 #define SEND_RowDescription(res, sk, args...) \
124 SEND_wrap(512, pktbuf_write_RowDescription, res, sk, ## args)
126 #define SEND_generic(res, sk, args...) \
127 SEND_wrap(512, pktbuf_write_generic, res, sk, ## args)
129 #define SEND_ReadyForQuery(res, sk) \
130 SEND_wrap(8, pktbuf_write_ReadyForQuery, res, sk)
132 #define SEND_CancelRequest(res, sk, key) \
133 SEND_wrap(16, pktbuf_write_CancelRequest, res, sk, key)
135 #define SEND_PasswordMessage(res, sk, psw) \
136 SEND_wrap(512, pktbuf_write_PasswordMessage, res, sk, psw)