pqcomm.h 5.5 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * pqcomm.h
4
 *		Definitions common to frontends and backends.
5
 *
6 7
 * NOTE: for historical reasons, this does not correspond to pqcomm.c.
 * pqcomm.c's routines are declared in libpq.h.
8
 *
Bruce Momjian's avatar
Bruce Momjian committed
9
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
10
 * Portions Copyright (c) 1994, Regents of the University of California
11
 *
12
 * $Id: pqcomm.h,v 1.77 2003/04/19 00:02:29 tgl Exp $
13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
#ifndef PQCOMM_H
17
#define PQCOMM_H
18

Bruce Momjian's avatar
Bruce Momjian committed
19
#ifdef WIN32
20
#include <winsock.h>
21 22 23 24 25
/* workaround for clashing defines of "ERROR" */
#ifdef ELOG_H
#undef ERROR
#define ERROR	(-1)
#endif
26 27
#else							/* not WIN32 */
#include <sys/socket.h>
28
#include <netdb.h>
29 30 31 32
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#include <netinet/in.h>
33
#endif   /* not WIN32 */
34 35


36 37 38 39 40 41 42 43 44
#ifndef INET_ADDRSTRLEN
#define INET_ADDRSTRLEN 16
#endif

#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 46
#endif


45 46 47 48
#ifndef HAVE_STRUCT_SOCKADDR_UN
struct sockaddr_un
{
	short int	sun_family;		/* AF_UNIX */
49
	char		sun_path[108];	/* path name (gag) */
50
};
Bruce Momjian's avatar
Bruce Momjian committed
51
#endif
52 53 54

/* Define a generic socket address type. */

55 56 57 58
typedef union SockAddr
{
	struct sockaddr sa;
	struct sockaddr_in in;
59 60 61
#ifdef HAVE_IPV6
	struct sockaddr_in6 in6;
#endif
62
	struct sockaddr_un un;
63 64 65
} SockAddr;


66
/* Configure the UNIX socket location for the well known port. */
67

68
#define UNIXSOCK_PATH(sun,port,defpath) \
Tom Lane's avatar
Tom Lane committed
69
		snprintf((sun).sun_path, sizeof((sun).sun_path), "%s/.s.PGSQL.%d", \
70 71 72
				((defpath) && *(defpath) != '\0') ? (defpath) : \
				DEFAULT_PGSOCKET_DIR, \
				(port))
73

Bruce Momjian's avatar
Bruce Momjian committed
74
/*
75 76
 *		We do this because sun_len is in BSD's struct, while others don't.
 *		We never actually set BSD's sun_len, and I can't think of a
Bruce Momjian's avatar
Bruce Momjian committed
77 78
 *		platform-safe way of doing it, but the code still works. bjm
 */
79 80 81 82 83 84 85
#if defined(SUN_LEN)
#define UNIXSOCK_LEN(sun) \
		(SUN_LEN(&(sun)))
#else
#define UNIXSOCK_LEN(sun) \
		(strlen((sun).sun_path) + offsetof(struct sockaddr_un, sun_path))
#endif
86 87 88 89 90 91 92 93 94

/*
 * These manipulate the frontend/backend protocol version number.
 *
 * The major number should be incremented for incompatible changes.  The minor
 * number should be incremented for compatible changes (eg. additional
 * functionality).
 *
 * If a backend supports version m.n of the protocol it must actually support
95
 * versions m.[0..n].  Backend support for version m-1 can be dropped after a
96 97 98 99 100 101
 * `reasonable' length of time.
 *
 * A frontend isn't required to support anything other than the current
 * version.
 */

102 103 104
#define PG_PROTOCOL_MAJOR(v)	((v) >> 16)
#define PG_PROTOCOL_MINOR(v)	((v) & 0x0000ffff)
#define PG_PROTOCOL(m,n)	(((m) << 16) | (n))
105

106 107
/* The earliest and latest frontend/backend protocol version supported. */

108
#define PG_PROTOCOL_EARLIEST	PG_PROTOCOL(1,0)
109
#define PG_PROTOCOL_LATEST		PG_PROTOCOL(3,101) /* XXX temporary value */
110

111
typedef uint32 ProtocolVersion; /* FE/BE protocol version number */
112

113
typedef ProtocolVersion MsgType;
114

115

116
/*
117 118 119
 * Packet lengths are 4 bytes in network byte order.
 *
 * The initial length is omitted from the packet layouts appearing below.
120
 */
121

122 123 124
typedef uint32 PacketLen;


125
/*
126 127 128
 * Old-style startup packet layout with fixed-width fields.  This is used in
 * protocol 1.0 and 2.0, but not in later versions.  Note that the fields
 * in this layout are '\0' terminated only if there is room.
129
 */
Bruce Momjian's avatar
Bruce Momjian committed
130

131 132
#define SM_DATABASE		64
#define SM_USER			32
133
/* We append database name if db_user_namespace true. */
Bruce Momjian's avatar
Bruce Momjian committed
134
#define SM_DATABASE_USER (SM_DATABASE+SM_USER+1)		/* +1 for @ */
135 136 137
#define SM_OPTIONS		64
#define SM_UNUSED		64
#define SM_TTY			64
138

139 140 141
typedef struct StartupPacket
{
	ProtocolVersion protoVersion;		/* Protocol version */
142
	char		database[SM_DATABASE];	/* Database name */
Bruce Momjian's avatar
Bruce Momjian committed
143
	/* Db_user_namespace appends dbname */
144
	char		user[SM_USER];	/* User name */
145
	char		options[SM_OPTIONS];	/* Optional additional args */
146 147
	char		unused[SM_UNUSED];		/* Unused */
	char		tty[SM_TTY];	/* Tty for debug output */
148 149
} StartupPacket;

150
extern bool Db_user_namespace;
151

152 153 154 155 156 157 158 159 160 161
/*
 * In protocol 3.0 and later, the startup packet length is not fixed, but
 * we set an arbitrary limit on it anyway.  This is just to prevent simple
 * denial-of-service attacks via sending enough data to run the server
 * out of memory.
 */
#define MAX_STARTUP_PACKET_LENGTH 10000


/* These are the authentication request codes sent by the backend. */
162

163
#define AUTH_REQ_OK			0	/* User is authenticated  */
164 165 166
#define AUTH_REQ_KRB4		1	/* Kerberos V4 */
#define AUTH_REQ_KRB5		2	/* Kerberos V5 */
#define AUTH_REQ_PASSWORD	3	/* Password */
167 168
#define AUTH_REQ_CRYPT		4	/* crypt password */
#define AUTH_REQ_MD5		5	/* md5 password */
169
#define AUTH_REQ_SCM_CREDS	6	/* transfer SCM credentials */
170 171 172 173

typedef uint32 AuthRequest;


174 175
/*
 * A client can also send a cancel-current-operation request to the postmaster.
176 177
 * This is uglier than sending it directly to the client's backend, but it
 * avoids depending on out-of-band communication facilities.
178 179
 *
 * The cancel request code must not match any protocol version number
180 181
 * we're ever likely to use.  This random choice should do.
 */
182
#define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
183 184 185 186

typedef struct CancelRequestPacket
{
	/* Note that each field is stored in network byte order! */
187 188 189 190
	MsgType		cancelRequestCode;		/* code to identify a cancel
										 * request */
	uint32		backendPID;		/* PID of client's backend */
	uint32		cancelAuthCode; /* secret key to authorize cancel */
191
} CancelRequestPacket;
192

193 194 195 196 197 198 199

/*
 * A client can also start by sending a SSL negotiation request, to get a
 * secure channel.
 */
#define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)

200
#endif   /* PQCOMM_H */