1 /*-------------------------------------------------------------------------
4 * Definitions common to frontends and backends.
6 * NOTE: for historical reasons, this does not correspond to pqcomm.c.
7 * pqcomm.c's routines are declared in libpq.h.
9 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Id: pqcomm.h,v 1.87 2003/06/23 23:51:59 momjian Exp $
14 *-------------------------------------------------------------------------
21 /* workaround for clashing defines of "ERROR" */
27 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #endif /* not WIN32 */
35 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
36 /* Define a struct sockaddr_storage if we don't have one. */
38 * Desired design of maximum size and alignment
40 #define _SS_MAXSIZE 128 /* Implementation specific max size */
41 #define _SS_ALIGNSIZE (sizeof (int64_t))
42 /* Implementation specific desired alignment */
44 * Definitions used for sockaddr_storage structure paddings design.
46 #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof (sa_family_t))
47 #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (sa_family_t) + \
48 _SS_PAD1SIZE + _SS_ALIGNSIZE))
50 struct sockaddr_storage {
52 uint8_t __ss_len; /* address length */
54 sa_family_t ss_family; /* address family */
56 char __ss_pad1[_SS_PAD1SIZE];
57 /* 6 byte pad, this is to make implementation
58 * specific pad up to alignment field that
59 * follows explicit in the data structure */
61 /* field to force desired structure
62 * storage alignment */
63 char __ss_pad2[_SS_PAD2SIZE];
64 /* 112 byte pad to achieve desired size,
65 * _SS_MAXSIZE value minus size of ss_family
66 * __ss_pad1, __ss_align fields is 112 */
68 #elif !defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY)
69 # ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
70 # define ss_family __ss_family
72 # error struct sockaddr_storage does not provide an ss_family member
77 struct sockaddr_storage addr;
78 ACCEPT_TYPE_ARG3 salen;
81 /* Some systems don't have it, so default it to 0 so it doesn't
82 * have any effect on those systems. */
84 #define AI_ADDRCONFIG 0
87 /* Configure the UNIX socket location for the well known port. */
89 #define UNIXSOCK_PATH(path,port,defpath) \
90 snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
91 ((defpath) && *(defpath) != '\0') ? (defpath) : \
92 DEFAULT_PGSOCKET_DIR, \
96 * These manipulate the frontend/backend protocol version number.
98 * The major number should be incremented for incompatible changes. The minor
99 * number should be incremented for compatible changes (eg. additional
102 * If a backend supports version m.n of the protocol it must actually support
103 * versions m.[0..n]. Backend support for version m-1 can be dropped after a
104 * `reasonable' length of time.
106 * A frontend isn't required to support anything other than the current
110 #define PG_PROTOCOL_MAJOR(v) ((v) >> 16)
111 #define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff)
112 #define PG_PROTOCOL(m,n) (((m) << 16) | (n))
114 /* The earliest and latest frontend/backend protocol version supported. */
116 #define PG_PROTOCOL_EARLIEST PG_PROTOCOL(1,0)
117 #define PG_PROTOCOL_LATEST PG_PROTOCOL(3,0)
119 typedef uint32 ProtocolVersion; /* FE/BE protocol version number */
121 typedef ProtocolVersion MsgType;
125 * Packet lengths are 4 bytes in network byte order.
127 * The initial length is omitted from the packet layouts appearing below.
130 typedef uint32 PacketLen;
134 * Old-style startup packet layout with fixed-width fields. This is used in
135 * protocol 1.0 and 2.0, but not in later versions. Note that the fields
136 * in this layout are '\0' terminated only if there is room.
139 #define SM_DATABASE 64
141 /* We append database name if db_user_namespace true. */
142 #define SM_DATABASE_USER (SM_DATABASE+SM_USER+1) /* +1 for @ */
143 #define SM_OPTIONS 64
147 typedef struct StartupPacket
149 ProtocolVersion protoVersion; /* Protocol version */
150 char database[SM_DATABASE]; /* Database name */
151 /* Db_user_namespace appends dbname */
152 char user[SM_USER]; /* User name */
153 char options[SM_OPTIONS]; /* Optional additional args */
154 char unused[SM_UNUSED]; /* Unused */
155 char tty[SM_TTY]; /* Tty for debug output */
158 extern bool Db_user_namespace;
161 * In protocol 3.0 and later, the startup packet length is not fixed, but
162 * we set an arbitrary limit on it anyway. This is just to prevent simple
163 * denial-of-service attacks via sending enough data to run the server
166 #define MAX_STARTUP_PACKET_LENGTH 10000
169 /* These are the authentication request codes sent by the backend. */
171 #define AUTH_REQ_OK 0 /* User is authenticated */
172 #define AUTH_REQ_KRB4 1 /* Kerberos V4 */
173 #define AUTH_REQ_KRB5 2 /* Kerberos V5 */
174 #define AUTH_REQ_PASSWORD 3 /* Password */
175 #define AUTH_REQ_CRYPT 4 /* crypt password */
176 #define AUTH_REQ_MD5 5 /* md5 password */
177 #define AUTH_REQ_SCM_CREDS 6 /* transfer SCM credentials */
179 typedef uint32 AuthRequest;
183 * A client can also send a cancel-current-operation request to the postmaster.
184 * This is uglier than sending it directly to the client's backend, but it
185 * avoids depending on out-of-band communication facilities.
187 * The cancel request code must not match any protocol version number
188 * we're ever likely to use. This random choice should do.
190 #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
192 typedef struct CancelRequestPacket
194 /* Note that each field is stored in network byte order! */
195 MsgType cancelRequestCode; /* code to identify a cancel
197 uint32 backendPID; /* PID of client's backend */
198 uint32 cancelAuthCode; /* secret key to authorize cancel */
199 } CancelRequestPacket;
203 * A client can also start by sending a SSL negotiation request, to get a
206 #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
208 #endif /* PQCOMM_H */