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