00001 /*------------------------------------------------------------------------- 00002 * 00003 * pqcomm.h 00004 * Definitions common to frontends and backends. 00005 * 00006 * NOTE: for historical reasons, this does not correspond to pqcomm.c. 00007 * pqcomm.c's routines are declared in libpq.h. 00008 * 00009 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group 00010 * Portions Copyright (c) 1994, Regents of the University of California 00011 * 00012 * src/include/libpq/pqcomm.h 00013 * 00014 *------------------------------------------------------------------------- 00015 */ 00016 #ifndef PQCOMM_H 00017 #define PQCOMM_H 00018 00019 #include <sys/socket.h> 00020 #include <netdb.h> 00021 #ifdef HAVE_SYS_UN_H 00022 #include <sys/un.h> 00023 #endif 00024 #include <netinet/in.h> 00025 00026 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE 00027 00028 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY 00029 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY 00030 #define ss_family __ss_family 00031 #else 00032 #error struct sockaddr_storage does not provide an ss_family member 00033 #endif 00034 #endif 00035 00036 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN 00037 #define ss_len __ss_len 00038 #define HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN 1 00039 #endif 00040 #else /* !HAVE_STRUCT_SOCKADDR_STORAGE */ 00041 00042 /* Define a struct sockaddr_storage if we don't have one. */ 00043 00044 struct sockaddr_storage 00045 { 00046 union 00047 { 00048 struct sockaddr sa; /* get the system-dependent fields */ 00049 int64 ss_align; /* ensures struct is properly aligned */ 00050 char ss_pad[128]; /* ensures struct has desired size */ 00051 } ss_stuff; 00052 }; 00053 00054 #define ss_family ss_stuff.sa.sa_family 00055 /* It should have an ss_len field if sockaddr has sa_len. */ 00056 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 00057 #define ss_len ss_stuff.sa.sa_len 00058 #define HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN 1 00059 #endif 00060 #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */ 00061 00062 typedef struct 00063 { 00064 struct sockaddr_storage addr; 00065 ACCEPT_TYPE_ARG3 salen; 00066 } SockAddr; 00067 00068 /* Configure the UNIX socket location for the well known port. */ 00069 00070 #define UNIXSOCK_PATH(path, port, sockdir) \ 00071 snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \ 00072 ((sockdir) && *(sockdir) != '\0') ? (sockdir) : \ 00073 DEFAULT_PGSOCKET_DIR, \ 00074 (port)) 00075 00076 /* 00077 * The maximum workable length of a socket path is what will fit into 00078 * struct sockaddr_un. This is usually only 100 or so bytes :-(. 00079 * 00080 * For consistency, always pass a MAXPGPATH-sized buffer to UNIXSOCK_PATH(), 00081 * then complain if the resulting string is >= UNIXSOCK_PATH_BUFLEN bytes. 00082 * (Because the standard API for getaddrinfo doesn't allow it to complain in 00083 * a useful way when the socket pathname is too long, we have to test for 00084 * this explicitly, instead of just letting the subroutine return an error.) 00085 */ 00086 #define UNIXSOCK_PATH_BUFLEN sizeof(((struct sockaddr_un *) NULL)->sun_path) 00087 00088 00089 /* 00090 * These manipulate the frontend/backend protocol version number. 00091 * 00092 * The major number should be incremented for incompatible changes. The minor 00093 * number should be incremented for compatible changes (eg. additional 00094 * functionality). 00095 * 00096 * If a backend supports version m.n of the protocol it must actually support 00097 * versions m.[0..n]. Backend support for version m-1 can be dropped after a 00098 * `reasonable' length of time. 00099 * 00100 * A frontend isn't required to support anything other than the current 00101 * version. 00102 */ 00103 00104 #define PG_PROTOCOL_MAJOR(v) ((v) >> 16) 00105 #define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff) 00106 #define PG_PROTOCOL(m,n) (((m) << 16) | (n)) 00107 00108 /* The earliest and latest frontend/backend protocol version supported. */ 00109 00110 #define PG_PROTOCOL_EARLIEST PG_PROTOCOL(1,0) 00111 #define PG_PROTOCOL_LATEST PG_PROTOCOL(3,0) 00112 00113 typedef uint32 ProtocolVersion; /* FE/BE protocol version number */ 00114 00115 typedef ProtocolVersion MsgType; 00116 00117 00118 /* 00119 * Packet lengths are 4 bytes in network byte order. 00120 * 00121 * The initial length is omitted from the packet layouts appearing below. 00122 */ 00123 00124 typedef uint32 PacketLen; 00125 00126 00127 /* 00128 * Old-style startup packet layout with fixed-width fields. This is used in 00129 * protocol 1.0 and 2.0, but not in later versions. Note that the fields 00130 * in this layout are '\0' terminated only if there is room. 00131 */ 00132 00133 #define SM_DATABASE 64 00134 #define SM_USER 32 00135 /* We append database name if db_user_namespace true. */ 00136 #define SM_DATABASE_USER (SM_DATABASE+SM_USER+1) /* +1 for @ */ 00137 #define SM_OPTIONS 64 00138 #define SM_UNUSED 64 00139 #define SM_TTY 64 00140 00141 typedef struct StartupPacket 00142 { 00143 ProtocolVersion protoVersion; /* Protocol version */ 00144 char database[SM_DATABASE]; /* Database name */ 00145 /* Db_user_namespace appends dbname */ 00146 char user[SM_USER]; /* User name */ 00147 char options[SM_OPTIONS]; /* Optional additional args */ 00148 char unused[SM_UNUSED]; /* Unused */ 00149 char tty[SM_TTY]; /* Tty for debug output */ 00150 } StartupPacket; 00151 00152 extern bool Db_user_namespace; 00153 00154 /* 00155 * In protocol 3.0 and later, the startup packet length is not fixed, but 00156 * we set an arbitrary limit on it anyway. This is just to prevent simple 00157 * denial-of-service attacks via sending enough data to run the server 00158 * out of memory. 00159 */ 00160 #define MAX_STARTUP_PACKET_LENGTH 10000 00161 00162 00163 /* These are the authentication request codes sent by the backend. */ 00164 00165 #define AUTH_REQ_OK 0 /* User is authenticated */ 00166 #define AUTH_REQ_KRB4 1 /* Kerberos V4. Not supported any more. */ 00167 #define AUTH_REQ_KRB5 2 /* Kerberos V5 */ 00168 #define AUTH_REQ_PASSWORD 3 /* Password */ 00169 #define AUTH_REQ_CRYPT 4 /* crypt password. Not supported any more. */ 00170 #define AUTH_REQ_MD5 5 /* md5 password */ 00171 #define AUTH_REQ_SCM_CREDS 6 /* transfer SCM credentials */ 00172 #define AUTH_REQ_GSS 7 /* GSSAPI without wrap() */ 00173 #define AUTH_REQ_GSS_CONT 8 /* Continue GSS exchanges */ 00174 #define AUTH_REQ_SSPI 9 /* SSPI negotiate without wrap() */ 00175 00176 typedef uint32 AuthRequest; 00177 00178 00179 /* 00180 * A client can also send a cancel-current-operation request to the postmaster. 00181 * This is uglier than sending it directly to the client's backend, but it 00182 * avoids depending on out-of-band communication facilities. 00183 * 00184 * The cancel request code must not match any protocol version number 00185 * we're ever likely to use. This random choice should do. 00186 */ 00187 #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678) 00188 00189 typedef struct CancelRequestPacket 00190 { 00191 /* Note that each field is stored in network byte order! */ 00192 MsgType cancelRequestCode; /* code to identify a cancel request */ 00193 uint32 backendPID; /* PID of client's backend */ 00194 uint32 cancelAuthCode; /* secret key to authorize cancel */ 00195 } CancelRequestPacket; 00196 00197 00198 /* 00199 * A client can also start by sending a SSL negotiation request, to get a 00200 * secure channel. 00201 */ 00202 #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679) 00203 00204 #endif /* PQCOMM_H */