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-2008, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
14 *-------------------------------------------------------------------------
19 #include <sys/socket.h>
24 #include <netinet/in.h>
26 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE
28 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY
29 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
30 #define ss_family __ss_family
32 #error struct sockaddr_storage does not provide an ss_family member
36 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN
37 #define ss_len __ss_len
38 #define HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN 1
40 #else /* !HAVE_STRUCT_SOCKADDR_STORAGE */
42 /* Define a struct sockaddr_storage if we don't have one. */
44 struct sockaddr_storage
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 */
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
60 #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */
64 struct sockaddr_storage addr
;
65 ACCEPT_TYPE_ARG3 salen
;
68 /* Configure the UNIX socket location for the well known port. */
70 #define UNIXSOCK_PATH(path, port, sockdir) \
71 snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
72 ((sockdir) && *(sockdir) != '\0') ? (sockdir) : \
73 DEFAULT_PGSOCKET_DIR, \
77 * These manipulate the frontend/backend protocol version number.
79 * The major number should be incremented for incompatible changes. The minor
80 * number should be incremented for compatible changes (eg. additional
83 * If a backend supports version m.n of the protocol it must actually support
84 * versions m.[0..n]. Backend support for version m-1 can be dropped after a
85 * `reasonable' length of time.
87 * A frontend isn't required to support anything other than the current
91 #define PG_PROTOCOL_MAJOR(v) ((v) >> 16)
92 #define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff)
93 #define PG_PROTOCOL(m,n) (((m) << 16) | (n))
95 /* The earliest and latest frontend/backend protocol version supported. */
97 #define PG_PROTOCOL_EARLIEST PG_PROTOCOL(1,0)
98 #define PG_PROTOCOL_LATEST PG_PROTOCOL(3,0)
100 typedef uint32 ProtocolVersion
; /* FE/BE protocol version number */
102 typedef ProtocolVersion MsgType
;
106 * Packet lengths are 4 bytes in network byte order.
108 * The initial length is omitted from the packet layouts appearing below.
111 typedef uint32 PacketLen
;
115 * Old-style startup packet layout with fixed-width fields. This is used in
116 * protocol 1.0 and 2.0, but not in later versions. Note that the fields
117 * in this layout are '\0' terminated only if there is room.
120 #define SM_DATABASE 64
122 /* We append database name if db_user_namespace true. */
123 #define SM_DATABASE_USER (SM_DATABASE+SM_USER+1) /* +1 for @ */
124 #define SM_OPTIONS 64
128 typedef struct StartupPacket
130 ProtocolVersion protoVersion
; /* Protocol version */
131 char database
[SM_DATABASE
]; /* Database name */
132 /* Db_user_namespace appends dbname */
133 char user
[SM_USER
]; /* User name */
134 char options
[SM_OPTIONS
]; /* Optional additional args */
135 char unused
[SM_UNUSED
]; /* Unused */
136 char tty
[SM_TTY
]; /* Tty for debug output */
139 extern bool Db_user_namespace
;
142 * In protocol 3.0 and later, the startup packet length is not fixed, but
143 * we set an arbitrary limit on it anyway. This is just to prevent simple
144 * denial-of-service attacks via sending enough data to run the server
147 #define MAX_STARTUP_PACKET_LENGTH 10000
150 /* These are the authentication request codes sent by the backend. */
152 #define AUTH_REQ_OK 0 /* User is authenticated */
153 #define AUTH_REQ_KRB4 1 /* Kerberos V4. Not supported any more. */
154 #define AUTH_REQ_KRB5 2 /* Kerberos V5 */
155 #define AUTH_REQ_PASSWORD 3 /* Password */
156 #define AUTH_REQ_CRYPT 4 /* crypt password. Not supported any more. */
157 #define AUTH_REQ_MD5 5 /* md5 password */
158 #define AUTH_REQ_SCM_CREDS 6 /* transfer SCM credentials */
159 #define AUTH_REQ_GSS 7 /* GSSAPI without wrap() */
160 #define AUTH_REQ_GSS_CONT 8 /* Continue GSS exchanges */
161 #define AUTH_REQ_SSPI 9 /* SSPI negotiate without wrap() */
163 typedef uint32 AuthRequest
;
167 * A client can also send a cancel-current-operation request to the postmaster.
168 * This is uglier than sending it directly to the client's backend, but it
169 * avoids depending on out-of-band communication facilities.
171 * The cancel request code must not match any protocol version number
172 * we're ever likely to use. This random choice should do.
174 #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
176 typedef struct CancelRequestPacket
178 /* Note that each field is stored in network byte order! */
179 MsgType cancelRequestCode
; /* code to identify a cancel request */
180 uint32 backendPID
; /* PID of client's backend */
181 uint32 cancelAuthCode
; /* secret key to authorize cancel */
182 } CancelRequestPacket
;
186 * A client can also start by sending a SSL negotiation request, to get a
189 #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
191 #endif /* PQCOMM_H */