OSDN Git Service

pgindent run.
[pg-rex/syncrep.git] / src / include / libpq / pqcomm.h
1 /*-------------------------------------------------------------------------
2  *
3  * pqcomm.h
4  *              Definitions common to frontends and backends.
5  *
6  * NOTE: for historical reasons, this does not correspond to pqcomm.c.
7  * pqcomm.c's routines are declared in libpq.h.
8  *
9  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * $Id: pqcomm.h,v 1.70 2002/09/04 20:31:42 momjian Exp $
13  *
14  *-------------------------------------------------------------------------
15  */
16 #ifndef PQCOMM_H
17 #define PQCOMM_H
18
19 #include <sys/types.h>
20 #ifdef WIN32
21 #include <winsock.h>
22 /* workaround for clashing defines of "ERROR" */
23 #ifdef ELOG_H
24 #undef ERROR
25 #define ERROR   (-1)
26 #endif
27 #else                                                   /* not WIN32 */
28 #include <sys/socket.h>
29 #ifdef HAVE_SYS_UN_H
30 #include <sys/un.h>
31 #endif
32 #include <netinet/in.h>
33 #endif   /* not WIN32 */
34
35
36 #ifndef HAVE_STRUCT_SOCKADDR_UN
37 struct sockaddr_un
38 {
39         short int       sun_family;             /* AF_UNIX */
40         char            sun_path[108];  /* path name (gag) */
41 };
42 #endif
43
44 /* Define a generic socket address type. */
45
46 typedef union SockAddr
47 {
48         struct sockaddr sa;
49         struct sockaddr_in in;
50         struct sockaddr_un un;
51 } SockAddr;
52
53
54 /* Configure the UNIX socket location for the well known port. */
55
56 #define UNIXSOCK_PATH(sun,port,defpath) \
57                 snprintf((sun).sun_path, sizeof((sun).sun_path), "%s/.s.PGSQL.%d", \
58                                 ((defpath) && *(defpath) != '\0') ? (defpath) : \
59                                 DEFAULT_PGSOCKET_DIR, \
60                                 (port))
61
62 /*
63  *              We do this because sun_len is in BSD's struct, while others don't.
64  *              We never actually set BSD's sun_len, and I can't think of a
65  *              platform-safe way of doing it, but the code still works. bjm
66  */
67 #if defined(SUN_LEN)
68 #define UNIXSOCK_LEN(sun) \
69                 (SUN_LEN(&(sun)))
70 #else
71 #define UNIXSOCK_LEN(sun) \
72                 (strlen((sun).sun_path) + offsetof(struct sockaddr_un, sun_path))
73 #endif
74
75 /*
76  * These manipulate the frontend/backend protocol version number.
77  *
78  * The major number should be incremented for incompatible changes.  The minor
79  * number should be incremented for compatible changes (eg. additional
80  * functionality).
81  *
82  * If a backend supports version m.n of the protocol it must actually support
83  * versions m.0..n].  Backend support for version m-1 can be dropped after a
84  * `reasonable' length of time.
85  *
86  * A frontend isn't required to support anything other than the current
87  * version.
88  */
89
90 #define PG_PROTOCOL_MAJOR(v)    ((v) >> 16)
91 #define PG_PROTOCOL_MINOR(v)    ((v) & 0x0000ffff)
92 #define PG_PROTOCOL(m,n)        (((m) << 16) | (n))
93
94 /* The earliest and latest frontend/backend protocol version supported. */
95
96 #define PG_PROTOCOL_EARLIEST    PG_PROTOCOL(1,0)
97 #define PG_PROTOCOL_LATEST      PG_PROTOCOL(2,0)
98
99 /*
100  * All packets sent to the postmaster start with the length.  This is omitted
101  * from the different packet definitions specified below.
102  */
103
104 typedef uint32 PacketLen;
105
106
107 /*
108  * Startup message parameters sizes.  These must not be changed without changing
109  * the protocol version.  These are all strings that are '\0' terminated only if
110  * there is room.
111  */
112
113 /*
114  * FIXME: remove the fixed size limitations on the database name, user
115  * name, and options fields and use a variable length field instead. The
116  * actual limits on database & user name will then be NAMEDATALEN, which
117  * can be changed without changing the FE/BE protocol. -neilc,2002/08/27
118  */
119
120 #define SM_DATABASE             64
121 #define SM_USER                 32
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
125 #define SM_UNUSED               64
126 #define SM_TTY                  64
127
128 typedef uint32 ProtocolVersion; /* Fe/Be protocol version number */
129
130 typedef ProtocolVersion MsgType;
131
132
133 typedef struct StartupPacket
134 {
135         ProtocolVersion protoVersion;           /* Protocol version */
136         char            database[SM_DATABASE];  /* Database name */
137         /* Db_user_namespace appends dbname */
138         char            user[SM_USER];  /* User name */
139         char            options[SM_OPTIONS];    /* Optional additional args */
140         char            unused[SM_UNUSED];              /* Unused */
141         char            tty[SM_TTY];    /* Tty for debug output */
142 } StartupPacket;
143
144 extern bool Db_user_namespace;
145
146 /* These are the authentication requests sent by the backend. */
147
148 #define AUTH_REQ_OK                     0       /* User is authenticated  */
149 #define AUTH_REQ_KRB4           1       /* Kerberos V4 */
150 #define AUTH_REQ_KRB5           2       /* Kerberos V5 */
151 #define AUTH_REQ_PASSWORD       3       /* Password */
152 #define AUTH_REQ_CRYPT          4       /* crypt password */
153 #define AUTH_REQ_MD5            5       /* md5 password */
154 #define AUTH_REQ_SCM_CREDS      6       /* transfer SCM credentials */
155
156 typedef uint32 AuthRequest;
157
158
159 /* A client can also send a cancel-current-operation request to the postmaster.
160  * This is uglier than sending it directly to the client's backend, but it
161  * avoids depending on out-of-band communication facilities.
162  */
163
164 /* The cancel request code must not match any protocol version number
165  * we're ever likely to use.  This random choice should do.
166  */
167 #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
168
169 typedef struct CancelRequestPacket
170 {
171         /* Note that each field is stored in network byte order! */
172         MsgType         cancelRequestCode;              /* code to identify a cancel
173                                                                                  * request */
174         uint32          backendPID;             /* PID of client's backend */
175         uint32          cancelAuthCode; /* secret key to authorize cancel */
176 } CancelRequestPacket;
177
178
179 /*
180  * A client can also start by sending a SSL negotiation request, to get a
181  * secure channel.
182  */
183 #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
184
185 #endif   /* PQCOMM_H */