PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pqcomm.h File Reference
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <netinet/in.h>
#include "libpq/protocol.h"
Include dependency graph for pqcomm.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SockAddr
 
struct  AddrInfo
 
struct  CancelRequestPacket
 

Macros

#define UNIXSOCK_PATH(path, port, sockdir)
 
#define UNIXSOCK_PATH_BUFLEN   sizeof(((struct sockaddr_un *) NULL)->sun_path)
 
#define PG_PROTOCOL_MAJOR(v)   ((v) >> 16)
 
#define PG_PROTOCOL_MINOR(v)   ((v) & 0x0000ffff)
 
#define PG_PROTOCOL_FULL(v)   (PG_PROTOCOL_MAJOR(v) * 10000 + PG_PROTOCOL_MINOR(v))
 
#define PG_PROTOCOL(m, n)   (((m) << 16) | (n))
 
#define PG_PROTOCOL_EARLIEST   PG_PROTOCOL(3,0)
 
#define PG_PROTOCOL_LATEST   PG_PROTOCOL(3,2)
 
#define PG_PROTOCOL_RESERVED_31   PG_PROTOCOL(3,1)
 
#define PG_PROTOCOL_GREASE   PG_PROTOCOL(3,9999)
 
#define CANCEL_REQUEST_CODE   PG_PROTOCOL(1234,5678)
 
#define NEGOTIATE_SSL_CODE   PG_PROTOCOL(1234,5679)
 
#define NEGOTIATE_GSS_CODE   PG_PROTOCOL(1234,5680)
 
#define MAX_STARTUP_PACKET_LENGTH   10000
 
#define PG_ALPN_PROTOCOL   "postgresql"
 
#define PG_ALPN_PROTOCOL_VECTOR   { 10, 'p','o','s','t','g','r','e','s','q','l' }
 

Typedefs

typedef uint32 ProtocolVersion
 
typedef uint32 PacketLen
 
typedef uint32 AuthRequest
 
typedef struct CancelRequestPacket CancelRequestPacket
 

Functions

static bool is_unixsock_path (const char *path)
 

Macro Definition Documentation

◆ CANCEL_REQUEST_CODE

#define CANCEL_REQUEST_CODE   PG_PROTOCOL(1234,5678)

Definition at line 122 of file pqcomm.h.

◆ MAX_STARTUP_PACKET_LENGTH

#define MAX_STARTUP_PACKET_LENGTH   10000

Definition at line 148 of file pqcomm.h.

◆ NEGOTIATE_GSS_CODE

#define NEGOTIATE_GSS_CODE   PG_PROTOCOL(1234,5680)

Definition at line 129 of file pqcomm.h.

◆ NEGOTIATE_SSL_CODE

#define NEGOTIATE_SSL_CODE   PG_PROTOCOL(1234,5679)

Definition at line 128 of file pqcomm.h.

◆ PG_ALPN_PROTOCOL

#define PG_ALPN_PROTOCOL   "postgresql"

Definition at line 189 of file pqcomm.h.

◆ PG_ALPN_PROTOCOL_VECTOR

#define PG_ALPN_PROTOCOL_VECTOR   { 10, 'p','o','s','t','g','r','e','s','q','l' }

Definition at line 190 of file pqcomm.h.

◆ PG_PROTOCOL

#define PG_PROTOCOL (   m,
 
)    (((m) << 16) | (n))

Definition at line 89 of file pqcomm.h.

◆ PG_PROTOCOL_EARLIEST

#define PG_PROTOCOL_EARLIEST   PG_PROTOCOL(3,0)

Definition at line 94 of file pqcomm.h.

◆ PG_PROTOCOL_FULL

#define PG_PROTOCOL_FULL (   v)    (PG_PROTOCOL_MAJOR(v) * 10000 + PG_PROTOCOL_MINOR(v))

Definition at line 88 of file pqcomm.h.

◆ PG_PROTOCOL_GREASE

#define PG_PROTOCOL_GREASE   PG_PROTOCOL(3,9999)

Definition at line 115 of file pqcomm.h.

◆ PG_PROTOCOL_LATEST

#define PG_PROTOCOL_LATEST   PG_PROTOCOL(3,2)

Definition at line 95 of file pqcomm.h.

◆ PG_PROTOCOL_MAJOR

#define PG_PROTOCOL_MAJOR (   v)    ((v) >> 16)

Definition at line 86 of file pqcomm.h.

◆ PG_PROTOCOL_MINOR

#define PG_PROTOCOL_MINOR (   v)    ((v) & 0x0000ffff)

Definition at line 87 of file pqcomm.h.

◆ PG_PROTOCOL_RESERVED_31

#define PG_PROTOCOL_RESERVED_31   PG_PROTOCOL(3,1)

Definition at line 105 of file pqcomm.h.

◆ UNIXSOCK_PATH

#define UNIXSOCK_PATH (   path,
  port,
  sockdir 
)
Value:
AssertMacro(*(sockdir) != '\0'), \
snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
(sockdir), (port)))
#define AssertMacro(condition)
Definition c.h:874
static int port
Definition pg_regress.c:115
static const char * sockdir
Definition pg_regress.c:128
static int fb(int x)

Definition at line 43 of file pqcomm.h.

66{
67 return is_absolute_path(path) || path[0] == '@';
68}
69
70
71/*
72 * These manipulate the frontend/backend protocol version number.
73 *
74 * The major number should be incremented for incompatible changes. The minor
75 * number should be incremented for compatible changes (eg. additional
76 * functionality).
77 *
78 * If a backend supports version m.n of the protocol it must actually support
79 * versions m.[0..n]. Backend support for version m-1 can be dropped after a
80 * `reasonable' length of time.
81 *
82 * A frontend isn't required to support anything other than the current
83 * version.
84 */
85#define PG_PROTOCOL_MAJOR(v) ((v) >> 16)
86#define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff)
87#define PG_PROTOCOL_FULL(v) (PG_PROTOCOL_MAJOR(v) * 10000 + PG_PROTOCOL_MINOR(v))
88#define PG_PROTOCOL(m,n) (((m) << 16) | (n))
89
90/*
91 * The earliest and latest frontend/backend protocol version supported.
92 */
93#define PG_PROTOCOL_EARLIEST PG_PROTOCOL(3,0)
94#define PG_PROTOCOL_LATEST PG_PROTOCOL(3,2)
95
96/*
97 * Reserved protocol numbers, which have special semantics:
98 */
99
100/*
101 * 3.1 would have collided with old pgbouncer deployments, and was skipped. We
102 * neither emit it nor accept it on the wire.
103 */
104#define PG_PROTOCOL_RESERVED_31 PG_PROTOCOL(3,1)
105
106/*
107 * PG_PROTOCOL_GREASE is an intentionally unsupported protocol version used
108 * for "greasing" (the practice of sending valid, but extraneous or otherwise
109 * unusual, messages to keep peer implementations honest). This helps ensure
110 * that servers properly implement protocol version negotiation. Version 3.9999
111 * was chosen since it is safely within the valid range, it is representable
112 * via PQfullProtocolVersion, and it is unlikely to ever be needed in practice.
113 */
114#define PG_PROTOCOL_GREASE PG_PROTOCOL(3,9999)
115
116/*
117 * A client can send a cancel-current-operation request to the postmaster.
118 * This is uglier than sending it directly to the client's backend, but it
119 * avoids depending on out-of-band communication facilities.
120 */
121#define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
122
123/*
124 * A client can also start by sending a SSL or GSSAPI negotiation request to
125 * get a secure channel.
126 */
127#define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
128#define NEGOTIATE_GSS_CODE PG_PROTOCOL(1234,5680)
129
130
131typedef uint32 ProtocolVersion; /* FE/BE protocol version number */
132
133
134/*
135 * Packet lengths are 4 bytes in network byte order.
136 *
137 * The initial length is omitted from the packet layouts appearing below.
138 */
139typedef uint32 PacketLen;
140
141/*
142 * In protocol 3.0 and later, the startup packet length is not fixed, but
143 * we set an arbitrary limit on it anyway. This is just to prevent simple
144 * denial-of-service attacks via sending enough data to run the server
145 * out of memory.
146 */
147#define MAX_STARTUP_PACKET_LENGTH 10000
148
149
150typedef uint32 AuthRequest; /* an AUTH_REQ_* code */
151
152
153/*
154 * The packet used with a CANCEL_REQUEST_CODE.
155 *
156 * Before PostgreSQL v18 and the protocol version bump from 3.0 to 3.2, the
157 * cancel key was always 4 bytes. With protocol version 3.2, it's variable
158 * length.
159 */
160typedef struct CancelRequestPacket
161{
162 /* Note that each field is stored in network byte order! */
163 ProtocolVersion cancelRequestCode; /* code to identify a cancel request */
164 uint32 backendPID; /* PID of client's backend */
166 * authorize cancel */
168
169
170/*
171 * Application-Layer Protocol Negotiation is required for direct connections
172 * to avoid protocol confusion attacks (e.g https://alpaca-attack.com/).
173 *
174 * ALPN is specified in RFC 7301
175 *
176 * This string should be registered at:
177 * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids
178 *
179 * OpenSSL uses this wire-format for the list of alpn protocols even in the
180 * API. Both server and client take the same format parameter but the client
181 * actually sends it to the server as-is and the server it specifies the
182 * preference order to use to choose the one selected to send back.
183 *
184 * c.f. https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_alpn_select_cb.html
185 *
186 * The #define can be used to initialize a char[] vector to use directly in the API
187 */
188#define PG_ALPN_PROTOCOL "postgresql"
189#define PG_ALPN_PROTOCOL_VECTOR { 10, 'p','o','s','t','g','r','e','s','q','l' }
190
191#endif /* PQCOMM_H */
uint8_t uint8
Definition c.h:544
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:480
uint32_t uint32
Definition c.h:546
#define is_absolute_path(filename)
Definition port.h:104
uint32 PacketLen
Definition pqcomm.h:140
uint32 ProtocolVersion
Definition pqcomm.h:132
uint32 AuthRequest
Definition pqcomm.h:151
uint8 cancelAuthCode[FLEXIBLE_ARRAY_MEMBER]
Definition pqcomm.h:166
ProtocolVersion cancelRequestCode
Definition pqcomm.h:164

◆ UNIXSOCK_PATH_BUFLEN

#define UNIXSOCK_PATH_BUFLEN   sizeof(((struct sockaddr_un *) NULL)->sun_path)

Definition at line 59 of file pqcomm.h.

Typedef Documentation

◆ AuthRequest

Definition at line 151 of file pqcomm.h.

◆ CancelRequestPacket

◆ PacketLen

Definition at line 140 of file pqcomm.h.

◆ ProtocolVersion

Definition at line 132 of file pqcomm.h.

Function Documentation

◆ is_unixsock_path()

static bool is_unixsock_path ( const char path)
inlinestatic

Definition at line 66 of file pqcomm.h.

67{
68 return is_absolute_path(path) || path[0] == '@';
69}

References is_absolute_path.

Referenced by check_pghost_envvar(), do_connect(), exec_command_conninfo(), get_prompt(), passwordFromFile(), and pqConnectOptions2().