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 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 112 of file pqcomm.h.

◆ MAX_STARTUP_PACKET_LENGTH

#define MAX_STARTUP_PACKET_LENGTH   10000

Definition at line 138 of file pqcomm.h.

◆ NEGOTIATE_GSS_CODE

#define NEGOTIATE_GSS_CODE   PG_PROTOCOL(1234,5680)

Definition at line 119 of file pqcomm.h.

◆ NEGOTIATE_SSL_CODE

#define NEGOTIATE_SSL_CODE   PG_PROTOCOL(1234,5679)

Definition at line 118 of file pqcomm.h.

◆ PG_ALPN_PROTOCOL

#define PG_ALPN_PROTOCOL   "postgresql"

Definition at line 179 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 180 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_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 * A client can send a cancel-current-operation request to the postmaster.
108 * This is uglier than sending it directly to the client's backend, but it
109 * avoids depending on out-of-band communication facilities.
110 */
111#define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
112
113/*
114 * A client can also start by sending a SSL or GSSAPI negotiation request to
115 * get a secure channel.
116 */
117#define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
118#define NEGOTIATE_GSS_CODE PG_PROTOCOL(1234,5680)
119
120
121typedef uint32 ProtocolVersion; /* FE/BE protocol version number */
122
123
124/*
125 * Packet lengths are 4 bytes in network byte order.
126 *
127 * The initial length is omitted from the packet layouts appearing below.
128 */
129typedef uint32 PacketLen;
130
131/*
132 * In protocol 3.0 and later, the startup packet length is not fixed, but
133 * we set an arbitrary limit on it anyway. This is just to prevent simple
134 * denial-of-service attacks via sending enough data to run the server
135 * out of memory.
136 */
137#define MAX_STARTUP_PACKET_LENGTH 10000
138
139
140typedef uint32 AuthRequest; /* an AUTH_REQ_* code */
141
142
143/*
144 * The packet used with a CANCEL_REQUEST_CODE.
145 *
146 * Before PostgreSQL v18 and the protocol version bump from 3.0 to 3.2, the
147 * cancel key was always 4 bytes. With protocol version 3.2, it's variable
148 * length.
149 */
150typedef struct CancelRequestPacket
151{
152 /* Note that each field is stored in network byte order! */
153 ProtocolVersion cancelRequestCode; /* code to identify a cancel request */
154 uint32 backendPID; /* PID of client's backend */
156 * authorize cancel */
158
159
160/*
161 * Application-Layer Protocol Negotiation is required for direct connections
162 * to avoid protocol confusion attacks (e.g https://alpaca-attack.com/).
163 *
164 * ALPN is specified in RFC 7301
165 *
166 * This string should be registered at:
167 * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids
168 *
169 * OpenSSL uses this wire-format for the list of alpn protocols even in the
170 * API. Both server and client take the same format parameter but the client
171 * actually sends it to the server as-is and the server it specifies the
172 * preference order to use to choose the one selected to send back.
173 *
174 * c.f. https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_alpn_select_cb.html
175 *
176 * The #define can be used to initialize a char[] vector to use directly in the API
177 */
178#define PG_ALPN_PROTOCOL "postgresql"
179#define PG_ALPN_PROTOCOL_VECTOR { 10, 'p','o','s','t','g','r','e','s','q','l' }
180
181#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:130
uint32 ProtocolVersion
Definition pqcomm.h:122
uint32 AuthRequest
Definition pqcomm.h:141
uint8 cancelAuthCode[FLEXIBLE_ARRAY_MEMBER]
Definition pqcomm.h:156
ProtocolVersion cancelRequestCode
Definition pqcomm.h:154

◆ 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 141 of file pqcomm.h.

◆ CancelRequestPacket

◆ PacketLen

Definition at line 130 of file pqcomm.h.

◆ ProtocolVersion

Definition at line 122 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().