pqcomm.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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-2016, PostgreSQL Global Development Group
  10. * Portions Copyright (c) 1994, Regents of the University of California
  11. *
  12. * src/include/libpq/pqcomm.h
  13. *
  14. *-------------------------------------------------------------------------
  15. */
  16. #ifndef PQCOMM_H
  17. #define PQCOMM_H
  18. #include <sys/socket.h>
  19. #include <netdb.h>
  20. #ifdef HAVE_SYS_UN_H
  21. #include <sys/un.h>
  22. #endif
  23. #include <netinet/in.h>
  24. #ifdef HAVE_STRUCT_SOCKADDR_STORAGE
  25. #ifndef HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY
  26. #ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
  27. #define ss_family __ss_family
  28. #else
  29. #error struct sockaddr_storage does not provide an ss_family member
  30. #endif
  31. #endif
  32. #ifdef HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN
  33. #define ss_len __ss_len
  34. #define HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN 1
  35. #endif
  36. #else /* !HAVE_STRUCT_SOCKADDR_STORAGE */
  37. /* Define a struct sockaddr_storage if we don't have one. */
  38. struct sockaddr_storage
  39. {
  40. union
  41. {
  42. struct sockaddr sa; /* get the system-dependent fields */
  43. int64 ss_align; /* ensures struct is properly aligned */
  44. char ss_pad[128]; /* ensures struct has desired size */
  45. } ss_stuff;
  46. };
  47. #define ss_family ss_stuff.sa.sa_family
  48. /* It should have an ss_len field if sockaddr has sa_len. */
  49. #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
  50. #define ss_len ss_stuff.sa.sa_len
  51. #define HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN 1
  52. #endif
  53. #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */
  54. typedef struct
  55. {
  56. struct sockaddr_storage addr;
  57. ACCEPT_TYPE_ARG3 salen;
  58. } SockAddr;
  59. /* Configure the UNIX socket location for the well known port. */
  60. #define UNIXSOCK_PATH(path, port, sockdir) \
  61. snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
  62. ((sockdir) && *(sockdir) != '\0') ? (sockdir) : \
  63. DEFAULT_PGSOCKET_DIR, \
  64. (port))
  65. /*
  66. * The maximum workable length of a socket path is what will fit into
  67. * struct sockaddr_un. This is usually only 100 or so bytes :-(.
  68. *
  69. * For consistency, always pass a MAXPGPATH-sized buffer to UNIXSOCK_PATH(),
  70. * then complain if the resulting string is >= UNIXSOCK_PATH_BUFLEN bytes.
  71. * (Because the standard API for getaddrinfo doesn't allow it to complain in
  72. * a useful way when the socket pathname is too long, we have to test for
  73. * this explicitly, instead of just letting the subroutine return an error.)
  74. */
  75. #define UNIXSOCK_PATH_BUFLEN sizeof(((struct sockaddr_un *) NULL)->sun_path)
  76. /*
  77. * These manipulate the frontend/backend protocol version number.
  78. *
  79. * The major number should be incremented for incompatible changes. The minor
  80. * number should be incremented for compatible changes (eg. additional
  81. * functionality).
  82. *
  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.
  86. *
  87. * A frontend isn't required to support anything other than the current
  88. * version.
  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. /* The earliest and latest frontend/backend protocol version supported. */
  94. #define PG_PROTOCOL_EARLIEST PG_PROTOCOL(1,0)
  95. #define PG_PROTOCOL_LATEST PG_PROTOCOL(3,0)
  96. typedef uint32 ProtocolVersion; /* FE/BE protocol version number */
  97. typedef ProtocolVersion MsgType;
  98. /*
  99. * Packet lengths are 4 bytes in network byte order.
  100. *
  101. * The initial length is omitted from the packet layouts appearing below.
  102. */
  103. typedef uint32 PacketLen;
  104. /*
  105. * Old-style startup packet layout with fixed-width fields. This is used in
  106. * protocol 1.0 and 2.0, but not in later versions. Note that the fields
  107. * in this layout are '\0' terminated only if there is room.
  108. */
  109. #define SM_DATABASE 64
  110. #define SM_USER 32
  111. /* We append database name if db_user_namespace true. */
  112. #define SM_DATABASE_USER (SM_DATABASE+SM_USER+1) /* +1 for @ */
  113. #define SM_OPTIONS 64
  114. #define SM_UNUSED 64
  115. #define SM_TTY 64
  116. typedef struct StartupPacket
  117. {
  118. ProtocolVersion protoVersion; /* Protocol version */
  119. char database[SM_DATABASE]; /* Database name */
  120. /* Db_user_namespace appends dbname */
  121. char user[SM_USER]; /* User name */
  122. char options[SM_OPTIONS]; /* Optional additional args */
  123. char unused[SM_UNUSED]; /* Unused */
  124. char tty[SM_TTY]; /* Tty for debug output */
  125. } StartupPacket;
  126. extern bool Db_user_namespace;
  127. /*
  128. * In protocol 3.0 and later, the startup packet length is not fixed, but
  129. * we set an arbitrary limit on it anyway. This is just to prevent simple
  130. * denial-of-service attacks via sending enough data to run the server
  131. * out of memory.
  132. */
  133. #define MAX_STARTUP_PACKET_LENGTH 10000
  134. /* These are the authentication request codes sent by the backend. */
  135. #define AUTH_REQ_OK 0 /* User is authenticated */
  136. #define AUTH_REQ_KRB4 1 /* Kerberos V4. Not supported any more. */
  137. #define AUTH_REQ_KRB5 2 /* Kerberos V5. Not supported any more. */
  138. #define AUTH_REQ_PASSWORD 3 /* Password */
  139. #define AUTH_REQ_CRYPT 4 /* crypt password. Not supported any more. */
  140. #define AUTH_REQ_MD5 5 /* md5 password */
  141. #define AUTH_REQ_SCM_CREDS 6 /* transfer SCM credentials */
  142. #define AUTH_REQ_GSS 7 /* GSSAPI without wrap() */
  143. #define AUTH_REQ_GSS_CONT 8 /* Continue GSS exchanges */
  144. #define AUTH_REQ_SSPI 9 /* SSPI negotiate without wrap() */
  145. #define AUTH_REQ_SASL 10 /* SASL authentication. Not supported before
  146. * libpq version 10. */
  147. typedef uint32 AuthRequest;
  148. /*
  149. * A client can also send a cancel-current-operation request to the postmaster.
  150. * This is uglier than sending it directly to the client's backend, but it
  151. * avoids depending on out-of-band communication facilities.
  152. *
  153. * The cancel request code must not match any protocol version number
  154. * we're ever likely to use. This random choice should do.
  155. */
  156. #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
  157. typedef struct CancelRequestPacket
  158. {
  159. /* Note that each field is stored in network byte order! */
  160. MsgType cancelRequestCode; /* code to identify a cancel request */
  161. uint32 backendPID; /* PID of client's backend */
  162. uint32 cancelAuthCode; /* secret key to authorize cancel */
  163. } CancelRequestPacket;
  164. /*
  165. * A client can also start by sending a SSL negotiation request, to get a
  166. * secure channel.
  167. */
  168. #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
  169. #endif /* PQCOMM_H */