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-2023, 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 typedef struct
25 {
26  struct sockaddr_storage addr;
28 } SockAddr;
29 
30 typedef struct
31 {
32  int family;
34 } AddrInfo;
35 
36 /* Configure the UNIX socket location for the well known port. */
37 
38 #define UNIXSOCK_PATH(path, port, sockdir) \
39  (AssertMacro(sockdir), \
40  AssertMacro(*(sockdir) != '\0'), \
41  snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
42  (sockdir), (port)))
43 
44 /*
45  * The maximum workable length of a socket path is what will fit into
46  * struct sockaddr_un. This is usually only 100 or so bytes :-(.
47  *
48  * For consistency, always pass a MAXPGPATH-sized buffer to UNIXSOCK_PATH(),
49  * then complain if the resulting string is >= UNIXSOCK_PATH_BUFLEN bytes.
50  * (Because the standard API for getaddrinfo doesn't allow it to complain in
51  * a useful way when the socket pathname is too long, we have to test for
52  * this explicitly, instead of just letting the subroutine return an error.)
53  */
54 #define UNIXSOCK_PATH_BUFLEN sizeof(((struct sockaddr_un *) NULL)->sun_path)
55 
56 /*
57  * A host that looks either like an absolute path or starts with @ is
58  * interpreted as a Unix-domain socket address.
59  */
60 static inline bool
61 is_unixsock_path(const char *path)
62 {
63  return is_absolute_path(path) || path[0] == '@';
64 }
65 
66 /*
67  * These manipulate the frontend/backend protocol version number.
68  *
69  * The major number should be incremented for incompatible changes. The minor
70  * number should be incremented for compatible changes (eg. additional
71  * functionality).
72  *
73  * If a backend supports version m.n of the protocol it must actually support
74  * versions m.[0..n]. Backend support for version m-1 can be dropped after a
75  * `reasonable' length of time.
76  *
77  * A frontend isn't required to support anything other than the current
78  * version.
79  */
80 
81 #define PG_PROTOCOL_MAJOR(v) ((v) >> 16)
82 #define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff)
83 #define PG_PROTOCOL(m,n) (((m) << 16) | (n))
84 
85 /*
86  * The earliest and latest frontend/backend protocol version supported.
87  * (Only protocol version 3 is currently supported)
88  */
89 
90 #define PG_PROTOCOL_EARLIEST PG_PROTOCOL(3,0)
91 #define PG_PROTOCOL_LATEST PG_PROTOCOL(3,0)
92 
93 typedef uint32 ProtocolVersion; /* FE/BE protocol version number */
94 
96 
97 
98 /*
99  * Packet lengths are 4 bytes in network byte order.
100  *
101  * The initial length is omitted from the packet layouts appearing below.
102  */
103 
105 
106 extern PGDLLIMPORT bool Db_user_namespace;
107 
108 /*
109  * In protocol 3.0 and later, the startup packet length is not fixed, but
110  * we set an arbitrary limit on it anyway. This is just to prevent simple
111  * denial-of-service attacks via sending enough data to run the server
112  * out of memory.
113  */
114 #define MAX_STARTUP_PACKET_LENGTH 10000
115 
116 
117 /* These are the authentication request codes sent by the backend. */
118 
119 #define AUTH_REQ_OK 0 /* User is authenticated */
120 #define AUTH_REQ_KRB4 1 /* Kerberos V4. Not supported any more. */
121 #define AUTH_REQ_KRB5 2 /* Kerberos V5. Not supported any more. */
122 #define AUTH_REQ_PASSWORD 3 /* Password */
123 #define AUTH_REQ_CRYPT 4 /* crypt password. Not supported any more. */
124 #define AUTH_REQ_MD5 5 /* md5 password */
125 /* 6 is available. It was used for SCM creds, not supported any more. */
126 #define AUTH_REQ_GSS 7 /* GSSAPI without wrap() */
127 #define AUTH_REQ_GSS_CONT 8 /* Continue GSS exchanges */
128 #define AUTH_REQ_SSPI 9 /* SSPI negotiate without wrap() */
129 #define AUTH_REQ_SASL 10 /* Begin SASL authentication */
130 #define AUTH_REQ_SASL_CONT 11 /* Continue SASL authentication */
131 #define AUTH_REQ_SASL_FIN 12 /* Final SASL message */
132 #define AUTH_REQ_MAX AUTH_REQ_SASL_FIN /* maximum AUTH_REQ_* value */
133 
135 
136 
137 /*
138  * A client can also send a cancel-current-operation request to the postmaster.
139  * This is uglier than sending it directly to the client's backend, but it
140  * avoids depending on out-of-band communication facilities.
141  *
142  * The cancel request code must not match any protocol version number
143  * we're ever likely to use. This random choice should do.
144  */
145 #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
146 
147 typedef struct CancelRequestPacket
148 {
149  /* Note that each field is stored in network byte order! */
150  MsgType cancelRequestCode; /* code to identify a cancel request */
151  uint32 backendPID; /* PID of client's backend */
152  uint32 cancelAuthCode; /* secret key to authorize cancel */
154 
155 
156 /*
157  * A client can also start by sending a SSL or GSSAPI negotiation request to
158  * get a secure channel.
159  */
160 #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
161 #define NEGOTIATE_GSS_CODE PG_PROTOCOL(1234,5680)
162 
163 #endif /* PQCOMM_H */
unsigned int uint32
Definition: c.h:490
#define PGDLLIMPORT
Definition: c.h:1303
#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:61
uint32 PacketLen
Definition: pqcomm.h:104
uint32 ProtocolVersion
Definition: pqcomm.h:93
PGDLLIMPORT bool Db_user_namespace
Definition: postmaster.c:239
struct CancelRequestPacket CancelRequestPacket
uint32 AuthRequest
Definition: pqcomm.h:134
ProtocolVersion MsgType
Definition: pqcomm.h:95
int family
Definition: pqcomm.h:32
SockAddr addr
Definition: pqcomm.h:33
MsgType cancelRequestCode
Definition: pqcomm.h:150
uint32 backendPID
Definition: pqcomm.h:151
uint32 cancelAuthCode
Definition: pqcomm.h:152
socklen_t salen
Definition: pqcomm.h:27