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-2003, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Id: pqcomm.h,v 1.93 2003/08/04 02:40:13 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 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE
37 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY
38 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
39 #define ss_family __ss_family
41 #error struct sockaddr_storage does not provide an ss_family member
45 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN
46 #define ss_len __ss_len
47 #define HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN 1
50 #else /* !HAVE_STRUCT_SOCKADDR_STORAGE */
52 /* Define a struct sockaddr_storage if we don't have one. */
54 struct sockaddr_storage
58 struct sockaddr sa; /* get the system-dependent fields */
59 int64 ss_align; /* ensures struct is properly aligned */
60 char ss_pad[128]; /* ensures struct has desired size */
64 #define ss_family ss_stuff.sa.sa_family
65 /* It should have an ss_len field if sockaddr has sa_len. */
66 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
67 #define ss_len ss_stuff.sa.sa_len
68 #define HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN 1
70 #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */
74 struct sockaddr_storage addr;
75 ACCEPT_TYPE_ARG3 salen;
78 /* Configure the UNIX socket location for the well known port. */
80 #define UNIXSOCK_PATH(path,port,defpath) \
81 snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
82 ((defpath) && *(defpath) != '\0') ? (defpath) : \
83 DEFAULT_PGSOCKET_DIR, \
87 * These manipulate the frontend/backend protocol version number.
89 * The major number should be incremented for incompatible changes. The minor
90 * number should be incremented for compatible changes (eg. additional
93 * If a backend supports version m.n of the protocol it must actually support
94 * versions m.[0..n]. Backend support for version m-1 can be dropped after a
95 * `reasonable' length of time.
97 * A frontend isn't required to support anything other than the current
101 #define PG_PROTOCOL_MAJOR(v) ((v) >> 16)
102 #define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff)
103 #define PG_PROTOCOL(m,n) (((m) << 16) | (n))
105 /* The earliest and latest frontend/backend protocol version supported. */
107 #define PG_PROTOCOL_EARLIEST PG_PROTOCOL(1,0)
108 #define PG_PROTOCOL_LATEST PG_PROTOCOL(3,0)
110 typedef uint32 ProtocolVersion; /* FE/BE protocol version number */
112 typedef ProtocolVersion MsgType;
116 * Packet lengths are 4 bytes in network byte order.
118 * The initial length is omitted from the packet layouts appearing below.
121 typedef uint32 PacketLen;
125 * Old-style startup packet layout with fixed-width fields. This is used in
126 * protocol 1.0 and 2.0, but not in later versions. Note that the fields
127 * in this layout are '\0' terminated only if there is room.
130 #define SM_DATABASE 64
132 /* We append database name if db_user_namespace true. */
133 #define SM_DATABASE_USER (SM_DATABASE+SM_USER+1) /* +1 for @ */
134 #define SM_OPTIONS 64
138 typedef struct StartupPacket
140 ProtocolVersion protoVersion; /* Protocol version */
141 char database[SM_DATABASE]; /* Database name */
142 /* Db_user_namespace appends dbname */
143 char user[SM_USER]; /* User name */
144 char options[SM_OPTIONS]; /* Optional additional args */
145 char unused[SM_UNUSED]; /* Unused */
146 char tty[SM_TTY]; /* Tty for debug output */
149 extern bool Db_user_namespace;
152 * In protocol 3.0 and later, the startup packet length is not fixed, but
153 * we set an arbitrary limit on it anyway. This is just to prevent simple
154 * denial-of-service attacks via sending enough data to run the server
157 #define MAX_STARTUP_PACKET_LENGTH 10000
160 /* These are the authentication request codes sent by the backend. */
162 #define AUTH_REQ_OK 0 /* User is authenticated */
163 #define AUTH_REQ_KRB4 1 /* Kerberos V4 */
164 #define AUTH_REQ_KRB5 2 /* Kerberos V5 */
165 #define AUTH_REQ_PASSWORD 3 /* Password */
166 #define AUTH_REQ_CRYPT 4 /* crypt password */
167 #define AUTH_REQ_MD5 5 /* md5 password */
168 #define AUTH_REQ_SCM_CREDS 6 /* transfer SCM credentials */
170 typedef uint32 AuthRequest;
174 * A client can also send a cancel-current-operation request to the postmaster.
175 * This is uglier than sending it directly to the client's backend, but it
176 * avoids depending on out-of-band communication facilities.
178 * The cancel request code must not match any protocol version number
179 * we're ever likely to use. This random choice should do.
181 #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
183 typedef struct CancelRequestPacket
185 /* Note that each field is stored in network byte order! */
186 MsgType cancelRequestCode; /* code to identify a cancel
188 uint32 backendPID; /* PID of client's backend */
189 uint32 cancelAuthCode; /* secret key to authorize cancel */
190 } CancelRequestPacket;
194 * A client can also start by sending a SSL negotiation request, to get a
197 #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
199 #endif /* PQCOMM_H */