summaryrefslogtreecommitdiff
path: root/src/interfaces
diff options
context:
space:
mode:
authorAndres Freund2017-10-01 22:36:14 +0000
committerAndres Freund2017-10-01 22:36:14 +0000
commit0ba99c84e8c7138143059b281063d4cca5a2bfea (patch)
tree521367c1888ca186fa229efc3489fb4298c8dc08 /src/interfaces
parent1f2830f9df9f0196ba541c1e253463afe657cb67 (diff)
Replace most usages of ntoh[ls] and hton[sl] with pg_bswap.h.
All postgres internal usages are replaced, it's just libpq example usages that haven't been converted. External users of libpq can't generally rely on including postgres internal headers. Note that this includes replacing open-coded byte swapping of 64bit integers (using two 32 bit swaps) with a single 64bit swap. Where it looked applicable, I have removed netinet/in.h and arpa/inet.h usage, which previously provided the relevant functionality. It's perfectly possible that I missed other reasons for including those, the buildfarm will tell. Author: Andres Freund Discussion: https://postgr.es/m/20170927172019.gheidqy6xvlxb325@alap3.anarazel.de
Diffstat (limited to 'src/interfaces')
-rw-r--r--src/interfaces/libpq/fe-connect.c12
-rw-r--r--src/interfaces/libpq/fe-lobj.c11
-rw-r--r--src/interfaces/libpq/fe-misc.c14
-rw-r--r--src/interfaces/libpq/fe-protocol2.c5
-rw-r--r--src/interfaces/libpq/fe-protocol3.c5
5 files changed, 21 insertions, 26 deletions
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index c580d91135a..5f798036071 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -47,7 +47,6 @@
#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif
-#include <arpa/inet.h>
#endif
#ifdef ENABLE_THREAD_SAFETY
@@ -73,6 +72,7 @@ static int ldapServiceLookup(const char *purl, PQconninfoOption *options,
#include "common/ip.h"
#include "mb/pg_wchar.h"
+#include "port/pg_bswap.h"
#ifndef WIN32
@@ -2443,7 +2443,7 @@ keep_going: /* We will come back to here until there is
* shouldn't since we only got here if the socket is
* write-ready.
*/
- pv = htonl(NEGOTIATE_SSL_CODE);
+ pv = pg_hton32(NEGOTIATE_SSL_CODE);
if (pqPacketSend(conn, 0, &pv, sizeof(pv)) != STATUS_OK)
{
appendPQExpBuffer(&conn->errorMessage,
@@ -3838,10 +3838,10 @@ retry3:
/* Create and send the cancel request packet. */
- crp.packetlen = htonl((uint32) sizeof(crp));
- crp.cp.cancelRequestCode = (MsgType) htonl(CANCEL_REQUEST_CODE);
- crp.cp.backendPID = htonl(be_pid);
- crp.cp.cancelAuthCode = htonl(be_key);
+ crp.packetlen = pg_hton32((uint32) sizeof(crp));
+ crp.cp.cancelRequestCode = (MsgType) pg_hton32(CANCEL_REQUEST_CODE);
+ crp.cp.backendPID = pg_hton32(be_pid);
+ crp.cp.cancelAuthCode = pg_hton32(be_key);
retry4:
if (send(tmpsock, (char *) &crp, sizeof(crp), 0) != (int) sizeof(crp))
diff --git a/src/interfaces/libpq/fe-lobj.c b/src/interfaces/libpq/fe-lobj.c
index 343e5303d99..2ff5559233a 100644
--- a/src/interfaces/libpq/fe-lobj.c
+++ b/src/interfaces/libpq/fe-lobj.c
@@ -33,12 +33,11 @@
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
-#include <netinet/in.h> /* for ntohl/htonl */
-#include <arpa/inet.h>
#include "libpq-fe.h"
#include "libpq-int.h"
#include "libpq/libpq-fs.h" /* must come after sys/stat.h */
+#include "port/pg_bswap.h"
#define LO_BUFSIZE 8192
@@ -1070,11 +1069,11 @@ lo_hton64(pg_int64 host64)
/* High order half first, since we're doing MSB-first */
t = (uint32) (host64 >> 32);
- swap.i32[0] = htonl(t);
+ swap.i32[0] = pg_hton32(t);
/* Now the low order half */
t = (uint32) host64;
- swap.i32[1] = htonl(t);
+ swap.i32[1] = pg_hton32(t);
return swap.i64;
}
@@ -1095,9 +1094,9 @@ lo_ntoh64(pg_int64 net64)
swap.i64 = net64;
- result = (uint32) ntohl(swap.i32[0]);
+ result = (uint32) pg_ntoh32(swap.i32[0]);
result <<= 32;
- result |= (uint32) ntohl(swap.i32[1]);
+ result |= (uint32) pg_ntoh32(swap.i32[1]);
return result;
}
diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c
index cac6359585e..41b1749d074 100644
--- a/src/interfaces/libpq/fe-misc.c
+++ b/src/interfaces/libpq/fe-misc.c
@@ -33,9 +33,6 @@
#include <signal.h>
#include <time.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
#ifdef WIN32
#include "win32.h"
#else
@@ -53,6 +50,7 @@
#include "libpq-fe.h"
#include "libpq-int.h"
#include "mb/pg_wchar.h"
+#include "port/pg_bswap.h"
#include "pg_config_paths.h"
@@ -278,14 +276,14 @@ pqGetInt(int *result, size_t bytes, PGconn *conn)
return EOF;
memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
conn->inCursor += 2;
- *result = (int) ntohs(tmp2);
+ *result = (int) pg_ntoh16(tmp2);
break;
case 4:
if (conn->inCursor + 4 > conn->inEnd)
return EOF;
memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
conn->inCursor += 4;
- *result = (int) ntohl(tmp4);
+ *result = (int) pg_ntoh32(tmp4);
break;
default:
pqInternalNotice(&conn->noticeHooks,
@@ -314,12 +312,12 @@ pqPutInt(int value, size_t bytes, PGconn *conn)
switch (bytes)
{
case 2:
- tmp2 = htons((uint16) value);
+ tmp2 = pg_hton16((uint16) value);
if (pqPutMsgBytes((const char *) &tmp2, 2, conn))
return EOF;
break;
case 4:
- tmp4 = htonl((uint32) value);
+ tmp4 = pg_hton32((uint32) value);
if (pqPutMsgBytes((const char *) &tmp4, 4, conn))
return EOF;
break;
@@ -597,7 +595,7 @@ pqPutMsgEnd(PGconn *conn)
{
uint32 msgLen = conn->outMsgEnd - conn->outMsgStart;
- msgLen = htonl(msgLen);
+ msgLen = pg_hton32(msgLen);
memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4);
}
diff --git a/src/interfaces/libpq/fe-protocol2.c b/src/interfaces/libpq/fe-protocol2.c
index 83f74f39852..1320d18a996 100644
--- a/src/interfaces/libpq/fe-protocol2.c
+++ b/src/interfaces/libpq/fe-protocol2.c
@@ -19,17 +19,16 @@
#include "libpq-fe.h"
#include "libpq-int.h"
+#include "port/pg_bswap.h"
#ifdef WIN32
#include "win32.h"
#else
#include <unistd.h>
-#include <netinet/in.h>
#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif
-#include <arpa/inet.h>
#endif
@@ -1609,7 +1608,7 @@ pqBuildStartupPacket2(PGconn *conn, int *packetlen,
MemSet(startpacket, 0, sizeof(StartupPacket));
- startpacket->protoVersion = htonl(conn->pversion);
+ startpacket->protoVersion = pg_hton32(conn->pversion);
/* strncpy is safe here: postmaster will handle full fields correctly */
strncpy(startpacket->user, conn->pguser, SM_USER);
diff --git a/src/interfaces/libpq/fe-protocol3.c b/src/interfaces/libpq/fe-protocol3.c
index 7da5fb28fb2..21fb8f2f213 100644
--- a/src/interfaces/libpq/fe-protocol3.c
+++ b/src/interfaces/libpq/fe-protocol3.c
@@ -21,16 +21,15 @@
#include "libpq-int.h"
#include "mb/pg_wchar.h"
+#include "port/pg_bswap.h"
#ifdef WIN32
#include "win32.h"
#else
#include <unistd.h>
-#include <netinet/in.h>
#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif
-#include <arpa/inet.h>
#endif
@@ -2148,7 +2147,7 @@ build_startup_packet(const PGconn *conn, char *packet,
/* Protocol version comes first. */
if (packet)
{
- ProtocolVersion pv = htonl(conn->pversion);
+ ProtocolVersion pv = pg_hton32(conn->pversion);
memcpy(packet + packet_len, &pv, sizeof(ProtocolVersion));
}