]> granicus.if.org Git - postgresql/blob - src/include/libpq/pqcomm.h
Remove references to sa_family_t, except when SOCKADDR_STORAGE requires
[postgresql] / 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.87 2003/06/23 23:51:59 momjian Exp $
13  *
14  *-------------------------------------------------------------------------
15  */
16 #ifndef PQCOMM_H
17 #define PQCOMM_H
18
19 #ifdef WIN32
20 #include <winsock.h>
21 /* workaround for clashing defines of "ERROR" */
22 #ifdef ELOG_H
23 #undef ERROR
24 #define ERROR   (-1)
25 #endif
26 #else                                                   /* not WIN32 */
27 #include <sys/socket.h>
28 #include <netdb.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 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
36 /* Define a struct sockaddr_storage if we don't have one. */
37 /*
38  * Desired design of maximum size and alignment
39  */
40 #define _SS_MAXSIZE    128  /* Implementation specific max size */
41 #define _SS_ALIGNSIZE  (sizeof (int64_t))
42                          /* Implementation specific desired alignment */
43 /*
44  * Definitions used for sockaddr_storage structure paddings design.
45  */
46 #define _SS_PAD1SIZE    (_SS_ALIGNSIZE - sizeof (sa_family_t))
47 #define _SS_PAD2SIZE    (_SS_MAXSIZE - (sizeof (sa_family_t) + \
48                                 _SS_PAD1SIZE + _SS_ALIGNSIZE))
49
50 struct sockaddr_storage {
51 #ifdef SALEN
52     uint8_t     __ss_len;        /* address length */
53 #endif
54     sa_family_t ss_family;      /* address family */
55
56     char        __ss_pad1[_SS_PAD1SIZE];
57                 /* 6 byte pad, this is to make implementation
58                  * specific pad up to alignment field that
59                  * follows explicit in the data structure */
60     int64_t     __ss_align;
61                 /* field to force desired structure
62                  * storage alignment */
63     char        __ss_pad2[_SS_PAD2SIZE];
64                 /* 112 byte pad to achieve desired size,
65                  * _SS_MAXSIZE value minus size of ss_family
66                  * __ss_pad1, __ss_align fields is 112 */
67 };
68 #elif !defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY)
69 # ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
70 #  define ss_family __ss_family
71 # else
72 #  error struct sockaddr_storage does not provide an ss_family member
73 # endif
74 #endif
75
76 typedef struct {
77         struct sockaddr_storage addr;
78         ACCEPT_TYPE_ARG3        salen;
79 } SockAddr;
80
81 /* Some systems don't have it, so default it to 0 so it doesn't
82  * have any effect on those systems. */
83 #ifndef AI_ADDRCONFIG
84 #define AI_ADDRCONFIG 0
85 #endif
86
87 /* Configure the UNIX socket location for the well known port. */
88
89 #define UNIXSOCK_PATH(path,port,defpath) \
90                 snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
91                                 ((defpath) && *(defpath) != '\0') ? (defpath) : \
92                                 DEFAULT_PGSOCKET_DIR, \
93                                 (port))
94
95 /*
96  * These manipulate the frontend/backend protocol version number.
97  *
98  * The major number should be incremented for incompatible changes.  The minor
99  * number should be incremented for compatible changes (eg. additional
100  * functionality).
101  *
102  * If a backend supports version m.n of the protocol it must actually support
103  * versions m.[0..n].  Backend support for version m-1 can be dropped after a
104  * `reasonable' length of time.
105  *
106  * A frontend isn't required to support anything other than the current
107  * version.
108  */
109
110 #define PG_PROTOCOL_MAJOR(v)    ((v) >> 16)
111 #define PG_PROTOCOL_MINOR(v)    ((v) & 0x0000ffff)
112 #define PG_PROTOCOL(m,n)        (((m) << 16) | (n))
113
114 /* The earliest and latest frontend/backend protocol version supported. */
115
116 #define PG_PROTOCOL_EARLIEST    PG_PROTOCOL(1,0)
117 #define PG_PROTOCOL_LATEST              PG_PROTOCOL(3,0)
118
119 typedef uint32 ProtocolVersion; /* FE/BE protocol version number */
120
121 typedef ProtocolVersion MsgType;
122
123
124 /*
125  * Packet lengths are 4 bytes in network byte order.
126  *
127  * The initial length is omitted from the packet layouts appearing below.
128  */
129
130 typedef uint32 PacketLen;
131
132
133 /*
134  * Old-style startup packet layout with fixed-width fields.  This is used in
135  * protocol 1.0 and 2.0, but not in later versions.  Note that the fields
136  * in this layout are '\0' terminated only if there is room.
137  */
138
139 #define SM_DATABASE             64
140 #define SM_USER                 32
141 /* We append database name if db_user_namespace true. */
142 #define SM_DATABASE_USER (SM_DATABASE+SM_USER+1)                /* +1 for @ */
143 #define SM_OPTIONS              64
144 #define SM_UNUSED               64
145 #define SM_TTY                  64
146
147 typedef struct StartupPacket
148 {
149         ProtocolVersion protoVersion;           /* Protocol version */
150         char            database[SM_DATABASE];  /* Database name */
151         /* Db_user_namespace appends dbname */
152         char            user[SM_USER];  /* User name */
153         char            options[SM_OPTIONS];    /* Optional additional args */
154         char            unused[SM_UNUSED];              /* Unused */
155         char            tty[SM_TTY];    /* Tty for debug output */
156 } StartupPacket;
157
158 extern bool Db_user_namespace;
159
160 /*
161  * In protocol 3.0 and later, the startup packet length is not fixed, but
162  * we set an arbitrary limit on it anyway.  This is just to prevent simple
163  * denial-of-service attacks via sending enough data to run the server
164  * out of memory.
165  */
166 #define MAX_STARTUP_PACKET_LENGTH 10000
167
168
169 /* These are the authentication request codes sent by the backend. */
170
171 #define AUTH_REQ_OK                     0       /* User is authenticated  */
172 #define AUTH_REQ_KRB4           1       /* Kerberos V4 */
173 #define AUTH_REQ_KRB5           2       /* Kerberos V5 */
174 #define AUTH_REQ_PASSWORD       3       /* Password */
175 #define AUTH_REQ_CRYPT          4       /* crypt password */
176 #define AUTH_REQ_MD5            5       /* md5 password */
177 #define AUTH_REQ_SCM_CREDS      6       /* transfer SCM credentials */
178
179 typedef uint32 AuthRequest;
180
181
182 /*
183  * A client can also send a cancel-current-operation request to the postmaster.
184  * This is uglier than sending it directly to the client's backend, but it
185  * avoids depending on out-of-band communication facilities.
186  *
187  * The cancel request code must not match any protocol version number
188  * we're ever likely to use.  This random choice should do.
189  */
190 #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
191
192 typedef struct CancelRequestPacket
193 {
194         /* Note that each field is stored in network byte order! */
195         MsgType         cancelRequestCode;              /* code to identify a cancel
196                                                                                  * request */
197         uint32          backendPID;             /* PID of client's backend */
198         uint32          cancelAuthCode; /* secret key to authorize cancel */
199 } CancelRequestPacket;
200
201
202 /*
203  * A client can also start by sending a SSL negotiation request, to get a
204  * secure channel.
205  */
206 #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
207
208 #endif   /* PQCOMM_H */