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