PostgreSQL Source Code  git master
pqcomm.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pqcomm.h
4  * Definitions common to frontends and backends.
5  *
6  * NOTE: for historical reasons, this does not correspond to pqcomm.c.
7  * pqcomm.c's routines are declared in libpq.h.
8  *
9  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * src/include/libpq/pqcomm.h
13  *
14  *-------------------------------------------------------------------------
15  */
16 #ifndef PQCOMM_H
17 #define PQCOMM_H
18 
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <netdb.h>
22 #include <netinet/in.h>
23 
24 /*
25  * The definitions for the request/response codes are kept in a separate file
26  * for ease of use in third party programs.
27  */
28 #include "libpq/protocol.h"
29 
30 typedef struct
31 {
32  struct sockaddr_storage addr;
34 } SockAddr;
35 
36 typedef struct
37 {
38  int family;
40 } AddrInfo;
41 
42 /* Configure the UNIX socket location for the well known port. */
43 
44 #define UNIXSOCK_PATH(path, port, sockdir) \
45  (AssertMacro(sockdir), \
46  AssertMacro(*(sockdir) != '\0'), \
47  snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
48  (sockdir), (port)))
49 
50 /*
51  * The maximum workable length of a socket path is what will fit into
52  * struct sockaddr_un. This is usually only 100 or so bytes :-(.
53  *
54  * For consistency, always pass a MAXPGPATH-sized buffer to UNIXSOCK_PATH(),
55  * then complain if the resulting string is >= UNIXSOCK_PATH_BUFLEN bytes.
56  * (Because the standard API for getaddrinfo doesn't allow it to complain in
57  * a useful way when the socket pathname is too long, we have to test for
58  * this explicitly, instead of just letting the subroutine return an error.)
59  */
60 #define UNIXSOCK_PATH_BUFLEN sizeof(((struct sockaddr_un *) NULL)->sun_path)
61 
62 /*
63  * A host that looks either like an absolute path or starts with @ is
64  * interpreted as a Unix-domain socket address.
65  */
66 static inline bool
67 is_unixsock_path(const char *path)
68 {
69  return is_absolute_path(path) || path[0] == '@';
70 }
71 
72 /*
73  * These manipulate the frontend/backend protocol version number.
74  *
75  * The major number should be incremented for incompatible changes. The minor
76  * number should be incremented for compatible changes (eg. additional
77  * functionality).
78  *
79  * If a backend supports version m.n of the protocol it must actually support
80  * versions m.[0..n]. Backend support for version m-1 can be dropped after a
81  * `reasonable' length of time.
82  *
83  * A frontend isn't required to support anything other than the current
84  * version.
85  */
86 
87 #define PG_PROTOCOL_MAJOR(v) ((v) >> 16)
88 #define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff)
89 #define PG_PROTOCOL(m,n) (((m) << 16) | (n))
90 
91 /*
92  * The earliest and latest frontend/backend protocol version supported.
93  * (Only protocol version 3 is currently supported)
94  */
95 
96 #define PG_PROTOCOL_EARLIEST PG_PROTOCOL(3,0)
97 #define PG_PROTOCOL_LATEST PG_PROTOCOL(3,0)
98 
99 typedef uint32 ProtocolVersion; /* FE/BE protocol version number */
100 
102 
103 
104 /*
105  * Packet lengths are 4 bytes in network byte order.
106  *
107  * The initial length is omitted from the packet layouts appearing below.
108  */
109 
111 
112 /*
113  * In protocol 3.0 and later, the startup packet length is not fixed, but
114  * we set an arbitrary limit on it anyway. This is just to prevent simple
115  * denial-of-service attacks via sending enough data to run the server
116  * out of memory.
117  */
118 #define MAX_STARTUP_PACKET_LENGTH 10000
119 
120 
122 
123 
124 /*
125  * A client can also send a cancel-current-operation request to the postmaster.
126  * This is uglier than sending it directly to the client's backend, but it
127  * avoids depending on out-of-band communication facilities.
128  *
129  * The cancel request code must not match any protocol version number
130  * we're ever likely to use. This random choice should do.
131  */
132 #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
133 
134 typedef struct CancelRequestPacket
135 {
136  /* Note that each field is stored in network byte order! */
137  MsgType cancelRequestCode; /* code to identify a cancel request */
138  uint32 backendPID; /* PID of client's backend */
139  uint32 cancelAuthCode; /* secret key to authorize cancel */
141 
142 
143 /*
144  * A client can also start by sending a SSL or GSSAPI negotiation request to
145  * get a secure channel.
146  */
147 #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
148 #define NEGOTIATE_GSS_CODE PG_PROTOCOL(1234,5680)
149 
150 #endif /* PQCOMM_H */
unsigned int uint32
Definition: c.h:493
#define is_absolute_path(filename)
Definition: port.h:103
unsigned int socklen_t
Definition: port.h:40
static bool is_unixsock_path(const char *path)
Definition: pqcomm.h:67
uint32 PacketLen
Definition: pqcomm.h:110
uint32 ProtocolVersion
Definition: pqcomm.h:99
struct CancelRequestPacket CancelRequestPacket
uint32 AuthRequest
Definition: pqcomm.h:121
ProtocolVersion MsgType
Definition: pqcomm.h:101
int family
Definition: pqcomm.h:38
SockAddr addr
Definition: pqcomm.h:39
MsgType cancelRequestCode
Definition: pqcomm.h:137
uint32 backendPID
Definition: pqcomm.h:138
uint32 cancelAuthCode
Definition: pqcomm.h:139
socklen_t salen
Definition: pqcomm.h:33