2
0

pqcomm.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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-2022, 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. socklen_t salen;
  58. } SockAddr;
  59. /* Configure the UNIX socket location for the well known port. */
  60. #define UNIXSOCK_PATH(path, port, sockdir) \
  61. (AssertMacro(sockdir), \
  62. AssertMacro(*(sockdir) != '\0'), \
  63. snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
  64. (sockdir), (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. * A host that looks either like an absolute path or starts with @ is
  78. * interpreted as a Unix-domain socket address.
  79. */
  80. static inline bool
  81. is_unixsock_path(const char *path)
  82. {
  83. return is_absolute_path(path) || path[0] == '@';
  84. }
  85. /*
  86. * These manipulate the frontend/backend protocol version number.
  87. *
  88. * The major number should be incremented for incompatible changes. The minor
  89. * number should be incremented for compatible changes (eg. additional
  90. * functionality).
  91. *
  92. * If a backend supports version m.n of the protocol it must actually support
  93. * versions m.[0..n]. Backend support for version m-1 can be dropped after a
  94. * `reasonable' length of time.
  95. *
  96. * A frontend isn't required to support anything other than the current
  97. * version.
  98. */
  99. #define PG_PROTOCOL_MAJOR(v) ((v) >> 16)
  100. #define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff)
  101. #define PG_PROTOCOL(m,n) (((m) << 16) | (n))
  102. /*
  103. * The earliest and latest frontend/backend protocol version supported.
  104. * (Only protocol version 3 is currently supported)
  105. */
  106. #define PG_PROTOCOL_EARLIEST PG_PROTOCOL(3,0)
  107. #define PG_PROTOCOL_LATEST PG_PROTOCOL(3,0)
  108. typedef uint32 ProtocolVersion; /* FE/BE protocol version number */
  109. typedef ProtocolVersion MsgType;
  110. /*
  111. * Packet lengths are 4 bytes in network byte order.
  112. *
  113. * The initial length is omitted from the packet layouts appearing below.
  114. */
  115. typedef uint32 PacketLen;
  116. extern PGDLLIMPORT bool Db_user_namespace;
  117. /*
  118. * In protocol 3.0 and later, the startup packet length is not fixed, but
  119. * we set an arbitrary limit on it anyway. This is just to prevent simple
  120. * denial-of-service attacks via sending enough data to run the server
  121. * out of memory.
  122. */
  123. #define MAX_STARTUP_PACKET_LENGTH 10000
  124. /* These are the authentication request codes sent by the backend. */
  125. #define AUTH_REQ_OK 0 /* User is authenticated */
  126. #define AUTH_REQ_KRB4 1 /* Kerberos V4. Not supported any more. */
  127. #define AUTH_REQ_KRB5 2 /* Kerberos V5. Not supported any more. */
  128. #define AUTH_REQ_PASSWORD 3 /* Password */
  129. #define AUTH_REQ_CRYPT 4 /* crypt password. Not supported any more. */
  130. #define AUTH_REQ_MD5 5 /* md5 password */
  131. #define AUTH_REQ_SCM_CREDS 6 /* transfer SCM credentials */
  132. #define AUTH_REQ_GSS 7 /* GSSAPI without wrap() */
  133. #define AUTH_REQ_GSS_CONT 8 /* Continue GSS exchanges */
  134. #define AUTH_REQ_SSPI 9 /* SSPI negotiate without wrap() */
  135. #define AUTH_REQ_SASL 10 /* Begin SASL authentication */
  136. #define AUTH_REQ_SASL_CONT 11 /* Continue SASL authentication */
  137. #define AUTH_REQ_SASL_FIN 12 /* Final SASL message */
  138. typedef uint32 AuthRequest;
  139. /*
  140. * A client can also send a cancel-current-operation request to the postmaster.
  141. * This is uglier than sending it directly to the client's backend, but it
  142. * avoids depending on out-of-band communication facilities.
  143. *
  144. * The cancel request code must not match any protocol version number
  145. * we're ever likely to use. This random choice should do.
  146. */
  147. #define CANCEL_REQUEST_CODE PG_PROTOCOL(1234,5678)
  148. typedef struct CancelRequestPacket
  149. {
  150. /* Note that each field is stored in network byte order! */
  151. MsgType cancelRequestCode; /* code to identify a cancel request */
  152. uint32 backendPID; /* PID of client's backend */
  153. uint32 cancelAuthCode; /* secret key to authorize cancel */
  154. } CancelRequestPacket;
  155. /*
  156. * A client can also start by sending a SSL or GSSAPI negotiation request to
  157. * get a secure channel.
  158. */
  159. #define NEGOTIATE_SSL_CODE PG_PROTOCOL(1234,5679)
  160. #define NEGOTIATE_GSS_CODE PG_PROTOCOL(1234,5680)
  161. #endif /* PQCOMM_H */