From 43c821e0bedc87ac9d55d5dbce0b2a15c1681c5f Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 29 Sep 2017 15:52:55 -0700
Subject: [PATCH 2/3] 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
---
 contrib/pgcrypto/crypt-des.c        | 17 +++++++---------
 contrib/uuid-ossp/uuid-ossp.c       | 17 +++++++---------
 src/backend/commands/copy.c         | 11 +++++-----
 src/backend/libpq/auth.c            | 18 ++++++++---------
 src/backend/libpq/ifaddr.c          |  6 +++---
 src/backend/libpq/pqcomm.c          |  6 +++---
 src/backend/libpq/pqformat.c        | 40 ++++++++++---------------------------
 src/backend/postmaster/postmaster.c | 13 ++++++------
 src/backend/tcop/fastpath.c         |  8 +++-----
 src/bin/pg_basebackup/streamutil.c  | 34 +++++++------------------------
 src/bin/pg_dump/parallel.c          |  6 ++++--
 src/bin/pg_rewind/libpq_fetch.c     | 29 ++-------------------------
 src/common/scram-common.c           |  7 ++-----
 src/interfaces/libpq/fe-connect.c   | 12 +++++------
 src/interfaces/libpq/fe-lobj.c      | 11 +++++-----
 src/interfaces/libpq/fe-misc.c      | 14 ++++++-------
 src/interfaces/libpq/fe-protocol2.c |  5 ++---
 src/interfaces/libpq/fe-protocol3.c |  5 ++---
 src/port/getaddrinfo.c              | 11 +++++-----
 src/port/inet_aton.c                |  4 +++-
 20 files changed, 99 insertions(+), 175 deletions(-)

diff --git a/contrib/pgcrypto/crypt-des.c b/contrib/pgcrypto/crypt-des.c
index ee3a0f2169..ed07fc4606 100644
--- a/contrib/pgcrypto/crypt-des.c
+++ b/contrib/pgcrypto/crypt-des.c
@@ -62,13 +62,10 @@
 
 #include "postgres.h"
 #include "miscadmin.h"
+#include "port/pg_bswap.h"
 
 #include "px-crypt.h"
 
-/* for ntohl/htonl */
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
 #define _PASSWORD_EFMT1 '_'
 
 static const char _crypt_a64[] =
@@ -408,8 +405,8 @@ des_setkey(const char *key)
 	if (!des_initialised)
 		des_init();
 
-	rawkey0 = ntohl(*(const uint32 *) key);
-	rawkey1 = ntohl(*(const uint32 *) (key + 4));
+	rawkey0 = pg_ntoh32(*(const uint32 *) key);
+	rawkey1 = pg_ntoh32(*(const uint32 *) (key + 4));
 
 	if ((rawkey0 | rawkey1)
 		&& rawkey0 == old_rawkey0
@@ -634,15 +631,15 @@ des_cipher(const char *in, char *out, long salt, int count)
 	/* copy data to avoid assuming input is word-aligned */
 	memcpy(buffer, in, sizeof(buffer));
 
-	rawl = ntohl(buffer[0]);
-	rawr = ntohl(buffer[1]);
+	rawl = pg_ntoh32(buffer[0]);
+	rawr = pg_ntoh32(buffer[1]);
 
 	retval = do_des(rawl, rawr, &l_out, &r_out, count);
 	if (retval)
 		return retval;
 
-	buffer[0] = htonl(l_out);
-	buffer[1] = htonl(r_out);
+	buffer[0] = pg_hton32(l_out);
+	buffer[1] = pg_hton32(r_out);
 
 	/* copy data to avoid assuming output is word-aligned */
 	memcpy(out, buffer, sizeof(buffer));
diff --git a/contrib/uuid-ossp/uuid-ossp.c b/contrib/uuid-ossp/uuid-ossp.c
index 55bc609415..fce4bc9140 100644
--- a/contrib/uuid-ossp/uuid-ossp.c
+++ b/contrib/uuid-ossp/uuid-ossp.c
@@ -14,13 +14,10 @@
 #include "postgres.h"
 
 #include "fmgr.h"
+#include "port/pg_bswap.h"
 #include "utils/builtins.h"
 #include "utils/uuid.h"
 
-/* for ntohl/htonl */
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
 /*
  * It's possible that there's more than one uuid.h header file present.
  * We expect configure to set the HAVE_ symbol for only the one we want.
@@ -90,16 +87,16 @@ typedef struct
 
 #define UUID_TO_NETWORK(uu) \
 do { \
-	uu.time_low = htonl(uu.time_low); \
-	uu.time_mid = htons(uu.time_mid); \
-	uu.time_hi_and_version = htons(uu.time_hi_and_version); \
+	uu.time_low = pg_hton32(uu.time_low); \
+	uu.time_mid = pg_hton16(uu.time_mid); \
+	uu.time_hi_and_version = pg_hton16(uu.time_hi_and_version); \
 } while (0)
 
 #define UUID_TO_LOCAL(uu) \
 do { \
-	uu.time_low = ntohl(uu.time_low); \
-	uu.time_mid = ntohs(uu.time_mid); \
-	uu.time_hi_and_version = ntohs(uu.time_hi_and_version); \
+	uu.time_low = pg_ntoh32(uu.time_low); \
+	uu.time_mid = pg_ntoh16(uu.time_mid); \
+	uu.time_hi_and_version = pg_ntoh16(uu.time_hi_and_version); \
 } while (0)
 
 #define UUID_V3_OR_V5(uu, v) \
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 7c004ffad8..e87588040f 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -17,8 +17,6 @@
 #include <ctype.h>
 #include <unistd.h>
 #include <sys/stat.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 
 #include "access/heapam.h"
 #include "access/htup_details.h"
@@ -38,6 +36,7 @@
 #include "optimizer/planner.h"
 #include "nodes/makefuncs.h"
 #include "parser/parse_relation.h"
+#include "port/pg_bswap.h"
 #include "rewrite/rewriteHandler.h"
 #include "storage/fd.h"
 #include "tcop/tcopprot.h"
@@ -671,7 +670,7 @@ CopySendInt32(CopyState cstate, int32 val)
 {
 	uint32		buf;
 
-	buf = htonl((uint32) val);
+	buf = pg_hton32((uint32) val);
 	CopySendData(cstate, &buf, sizeof(buf));
 }
 
@@ -690,7 +689,7 @@ CopyGetInt32(CopyState cstate, int32 *val)
 		*val = 0;				/* suppress compiler warning */
 		return false;
 	}
-	*val = (int32) ntohl(buf);
+	*val = (int32) pg_ntoh32(buf);
 	return true;
 }
 
@@ -702,7 +701,7 @@ CopySendInt16(CopyState cstate, int16 val)
 {
 	uint16		buf;
 
-	buf = htons((uint16) val);
+	buf = pg_hton16((uint16) val);
 	CopySendData(cstate, &buf, sizeof(buf));
 }
 
@@ -719,7 +718,7 @@ CopyGetInt16(CopyState cstate, int16 *val)
 		*val = 0;				/* suppress compiler warning */
 		return false;
 	}
-	*val = (int16) ntohs(buf);
+	*val = (int16) pg_ntoh16(buf);
 	return true;
 }
 
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
index 39a57d4835..480e344eb3 100644
--- a/src/backend/libpq/auth.c
+++ b/src/backend/libpq/auth.c
@@ -18,7 +18,6 @@
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
-#include <arpa/inet.h>
 #include <unistd.h>
 #ifdef HAVE_SYS_SELECT_H
 #include <sys/select.h>
@@ -33,6 +32,7 @@
 #include "libpq/pqformat.h"
 #include "libpq/scram.h"
 #include "miscadmin.h"
+#include "port/pg_bswap.h"
 #include "replication/walsender.h"
 #include "storage/ipc.h"
 #include "utils/backend_random.h"
@@ -2840,7 +2840,7 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
 	radius_packet *receivepacket = &radius_recv_pack;
 	char	   *radius_buffer = (char *) &radius_send_pack;
 	char	   *receive_buffer = (char *) &radius_recv_pack;
-	int32		service = htonl(RADIUS_AUTHENTICATE_ONLY);
+	int32		service = pg_hton32(RADIUS_AUTHENTICATE_ONLY);
 	uint8	   *cryptvector;
 	int			encryptedpasswordlen;
 	uint8		encryptedpassword[RADIUS_MAX_PASSWORD_LENGTH];
@@ -2948,7 +2948,7 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
 
 	/* Length needs to be in network order on the wire */
 	packetlength = packet->length;
-	packet->length = htons(packet->length);
+	packet->length = pg_hton16(packet->length);
 
 	sock = socket(serveraddrs[0].ai_family, SOCK_DGRAM, 0);
 	if (sock == PGINVALID_SOCKET)
@@ -3074,19 +3074,19 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
 		}
 
 #ifdef HAVE_IPV6
-		if (remoteaddr.sin6_port != htons(port))
+		if (remoteaddr.sin6_port != pg_hton16(port))
 #else
-		if (remoteaddr.sin_port != htons(port))
+		if (remoteaddr.sin_port != pg_hton16(port))
 #endif
 		{
 #ifdef HAVE_IPV6
 			ereport(LOG,
 					(errmsg("RADIUS response from %s was sent from incorrect port: %d",
-							server, ntohs(remoteaddr.sin6_port))));
+							server, pg_ntoh16(remoteaddr.sin6_port))));
 #else
 			ereport(LOG,
 					(errmsg("RADIUS response from %s was sent from incorrect port: %d",
-							server, ntohs(remoteaddr.sin_port))));
+							server, pg_ntoh16(remoteaddr.sin_port))));
 #endif
 			continue;
 		}
@@ -3098,11 +3098,11 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
 			continue;
 		}
 
-		if (packetlength != ntohs(receivepacket->length))
+		if (packetlength != pg_ntoh16(receivepacket->length))
 		{
 			ereport(LOG,
 					(errmsg("RADIUS response from %s has corrupt length: %d (actual length %d)",
-							server, ntohs(receivepacket->length), packetlength)));
+							server, pg_ntoh16(receivepacket->length), packetlength)));
 			continue;
 		}
 
diff --git a/src/backend/libpq/ifaddr.c b/src/backend/libpq/ifaddr.c
index 53bf6bcd80..b8c463b101 100644
--- a/src/backend/libpq/ifaddr.c
+++ b/src/backend/libpq/ifaddr.c
@@ -27,10 +27,10 @@
 #ifdef HAVE_NETINET_TCP_H
 #include <netinet/tcp.h>
 #endif
-#include <arpa/inet.h>
 #include <sys/file.h>
 
 #include "libpq/ifaddr.h"
+#include "port/pg_bswap.h"
 
 static int range_sockaddr_AF_INET(const struct sockaddr_in *addr,
 					   const struct sockaddr_in *netaddr,
@@ -144,7 +144,7 @@ pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family)
 						& 0xffffffffUL;
 				else
 					maskl = 0;
-				mask4.sin_addr.s_addr = htonl(maskl);
+				mask4.sin_addr.s_addr = pg_hton32(maskl);
 				memcpy(mask, &mask4, sizeof(mask4));
 				break;
 			}
@@ -568,7 +568,7 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
 	/* addr 127.0.0.1/8 */
 	memset(&addr, 0, sizeof(addr));
 	addr.sin_family = AF_INET;
-	addr.sin_addr.s_addr = ntohl(0x7f000001);
+	addr.sin_addr.s_addr = pg_ntoh32(0x7f000001);
 	memset(&mask, 0, sizeof(mask));
 	pg_sockaddr_cidr_mask(&mask, "8", AF_INET);
 	run_ifaddr_callback(callback, cb_data,
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index 4452ea4228..754154b83b 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -81,7 +81,6 @@
 #ifdef HAVE_NETINET_TCP_H
 #include <netinet/tcp.h>
 #endif
-#include <arpa/inet.h>
 #ifdef HAVE_UTIME_H
 #include <utime.h>
 #endif
@@ -92,6 +91,7 @@
 #include "common/ip.h"
 #include "libpq/libpq.h"
 #include "miscadmin.h"
+#include "port/pg_bswap.h"
 #include "storage/ipc.h"
 #include "utils/guc.h"
 #include "utils/memutils.h"
@@ -1286,7 +1286,7 @@ pq_getmessage(StringInfo s, int maxlen)
 		return EOF;
 	}
 
-	len = ntohl(len);
+	len = pg_ntoh32(len);
 
 	if (len < 4 ||
 		(maxlen > 0 && len > maxlen))
@@ -1569,7 +1569,7 @@ socket_putmessage(char msgtype, const char *s, size_t len)
 	{
 		uint32		n32;
 
-		n32 = htonl((uint32) (len + 4));
+		n32 = pg_hton32((uint32) (len + 4));
 		if (internal_putbytes((char *) &n32, 4))
 			goto fail;
 	}
diff --git a/src/backend/libpq/pqformat.c b/src/backend/libpq/pqformat.c
index c8cf67c041..f27a04f834 100644
--- a/src/backend/libpq/pqformat.c
+++ b/src/backend/libpq/pqformat.c
@@ -72,12 +72,11 @@
 #include "postgres.h"
 
 #include <sys/param.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 
 #include "libpq/libpq.h"
 #include "libpq/pqformat.h"
 #include "mb/pg_wchar.h"
+#include "port/pg_bswap.h"
 
 
 /* --------------------------------
@@ -246,11 +245,11 @@ pq_sendint(StringInfo buf, int i, int b)
 			appendBinaryStringInfo(buf, (char *) &n8, 1);
 			break;
 		case 2:
-			n16 = htons((uint16) i);
+			n16 = pg_hton16((uint16) i);
 			appendBinaryStringInfo(buf, (char *) &n16, 2);
 			break;
 		case 4:
-			n32 = htonl((uint32) i);
+			n32 = pg_hton32((uint32) i);
 			appendBinaryStringInfo(buf, (char *) &n32, 4);
 			break;
 		default:
@@ -270,17 +269,9 @@ pq_sendint(StringInfo buf, int i, int b)
 void
 pq_sendint64(StringInfo buf, int64 i)
 {
-	uint32		n32;
+	uint64		n64 = pg_hton64(i);
 
-	/* High order half first, since we're doing MSB-first */
-	n32 = (uint32) (i >> 32);
-	n32 = htonl(n32);
-	appendBinaryStringInfo(buf, (char *) &n32, 4);
-
-	/* Now the low order half */
-	n32 = (uint32) i;
-	n32 = htonl(n32);
-	appendBinaryStringInfo(buf, (char *) &n32, 4);
+	appendBinaryStringInfo(buf, (char *) &n64, sizeof(n64));
 }
 
 /* --------------------------------
@@ -304,7 +295,7 @@ pq_sendfloat4(StringInfo buf, float4 f)
 	}			swap;
 
 	swap.f = f;
-	swap.i = htonl(swap.i);
+	swap.i = pg_hton32(swap.i);
 
 	appendBinaryStringInfo(buf, (char *) &swap.i, 4);
 }
@@ -460,11 +451,11 @@ pq_getmsgint(StringInfo msg, int b)
 			break;
 		case 2:
 			pq_copymsgbytes(msg, (char *) &n16, 2);
-			result = ntohs(n16);
+			result = pg_ntoh16(n16);
 			break;
 		case 4:
 			pq_copymsgbytes(msg, (char *) &n32, 4);
-			result = ntohl(n32);
+			result = pg_ntoh32(n32);
 			break;
 		default:
 			elog(ERROR, "unsupported integer size %d", b);
@@ -485,20 +476,11 @@ pq_getmsgint(StringInfo msg, int b)
 int64
 pq_getmsgint64(StringInfo msg)
 {
-	int64		result;
-	uint32		h32;
-	uint32		l32;
+	uint64		n64;
 
-	pq_copymsgbytes(msg, (char *) &h32, 4);
-	pq_copymsgbytes(msg, (char *) &l32, 4);
-	h32 = ntohl(h32);
-	l32 = ntohl(l32);
+	pq_copymsgbytes(msg, (char *) &n64, sizeof(n64));
 
-	result = h32;
-	result <<= 32;
-	result |= l32;
-
-	return result;
+	return pg_ntoh64(n64);
 }
 
 /* --------------------------------
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 8a2cc2fc2b..2b2b993e2c 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -74,8 +74,6 @@
 #include <sys/socket.h>
 #include <fcntl.h>
 #include <sys/param.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 #include <netdb.h>
 #include <limits.h>
 
@@ -107,6 +105,7 @@
 #include "miscadmin.h"
 #include "pg_getopt.h"
 #include "pgstat.h"
+#include "port/pg_bswap.h"
 #include "postmaster/autovacuum.h"
 #include "postmaster/bgworker_internals.h"
 #include "postmaster/fork_process.h"
@@ -1072,7 +1071,7 @@ PostmasterMain(int argc, char *argv[])
 								 "_postgresql._tcp.",
 								 NULL,
 								 NULL,
-								 htons(PostPortNumber),
+								 pg_hton16(PostPortNumber),
 								 0,
 								 NULL,
 								 NULL,
@@ -1966,7 +1965,7 @@ ProcessStartupPacket(Port *port, bool SSLdone)
 		return STATUS_ERROR;
 	}
 
-	len = ntohl(len);
+	len = pg_ntoh32(len);
 	len -= 4;
 
 	if (len < (int32) sizeof(ProtocolVersion) ||
@@ -2002,7 +2001,7 @@ ProcessStartupPacket(Port *port, bool SSLdone)
 	 * The first field is either a protocol version number or a special
 	 * request code.
 	 */
-	port->proto = proto = ntohl(*((ProtocolVersion *) buf));
+	port->proto = proto = pg_ntoh32(*((ProtocolVersion *) buf));
 
 	if (proto == CANCEL_REQUEST_CODE)
 	{
@@ -2281,8 +2280,8 @@ processCancelRequest(Port *port, void *pkt)
 	int			i;
 #endif
 
-	backendPID = (int) ntohl(canc->backendPID);
-	cancelAuthCode = (int32) ntohl(canc->cancelAuthCode);
+	backendPID = (int) pg_ntoh32(canc->backendPID);
+	cancelAuthCode = (int32) pg_ntoh32(canc->cancelAuthCode);
 
 	/*
 	 * See if we have a matching backend.  In the EXEC_BACKEND case, we can no
diff --git a/src/backend/tcop/fastpath.c b/src/backend/tcop/fastpath.c
index 9207d76981..8101ae74e0 100644
--- a/src/backend/tcop/fastpath.c
+++ b/src/backend/tcop/fastpath.c
@@ -17,9 +17,6 @@
  */
 #include "postgres.h"
 
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
 #include "access/htup_details.h"
 #include "access/xact.h"
 #include "catalog/objectaccess.h"
@@ -28,6 +25,7 @@
 #include "libpq/pqformat.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
+#include "port/pg_bswap.h"
 #include "tcop/fastpath.h"
 #include "tcop/tcopprot.h"
 #include "utils/acl.h"
@@ -92,7 +90,7 @@ GetOldFunctionMessage(StringInfo buf)
 	if (pq_getbytes((char *) &ibuf, 4))
 		return EOF;
 	appendBinaryStringInfo(buf, (char *) &ibuf, 4);
-	nargs = ntohl(ibuf);
+	nargs = pg_ntoh32(ibuf);
 	/* For each argument ... */
 	while (nargs-- > 0)
 	{
@@ -102,7 +100,7 @@ GetOldFunctionMessage(StringInfo buf)
 		if (pq_getbytes((char *) &ibuf, 4))
 			return EOF;
 		appendBinaryStringInfo(buf, (char *) &ibuf, 4);
-		argsize = ntohl(ibuf);
+		argsize = pg_ntoh32(ibuf);
 		if (argsize < -1)
 		{
 			/* FATAL here since no hope of regaining message sync */
diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c
index 81fef8cd51..a57ff8f2c4 100644
--- a/src/bin/pg_basebackup/streamutil.c
+++ b/src/bin/pg_basebackup/streamutil.c
@@ -17,18 +17,15 @@
 #include <sys/time.h>
 #include <unistd.h>
 
-/* for ntohl/htonl */
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
 /* local includes */
 #include "receivelog.h"
 #include "streamutil.h"
 
 #include "access/xlog_internal.h"
-#include "pqexpbuffer.h"
 #include "common/fe_memutils.h"
 #include "datatype/timestamp.h"
+#include "port/pg_bswap.h"
+#include "pqexpbuffer.h"
 
 #define ERRCODE_DUPLICATE_OBJECT  "42710"
 
@@ -576,17 +573,9 @@ feTimestampDifferenceExceeds(TimestampTz start_time,
 void
 fe_sendint64(int64 i, char *buf)
 {
-	uint32		n32;
+	uint64		n64 = pg_hton64(i);
 
-	/* High order half first, since we're doing MSB-first */
-	n32 = (uint32) (i >> 32);
-	n32 = htonl(n32);
-	memcpy(&buf[0], &n32, 4);
-
-	/* Now the low order half */
-	n32 = (uint32) i;
-	n32 = htonl(n32);
-	memcpy(&buf[4], &n32, 4);
+	memcpy(buf, &n64, sizeof(n64));
 }
 
 /*
@@ -595,18 +584,9 @@ fe_sendint64(int64 i, char *buf)
 int64
 fe_recvint64(char *buf)
 {
-	int64		result;
-	uint32		h32;
-	uint32		l32;
+	uint64		n64;
 
-	memcpy(&h32, buf, 4);
-	memcpy(&l32, buf + 4, 4);
-	h32 = ntohl(h32);
-	l32 = ntohl(l32);
+	memcpy(&n64, buf, sizeof(n64));
 
-	result = h32;
-	result <<= 32;
-	result |= l32;
-
-	return result;
+	return pg_ntoh64(n64);
 }
diff --git a/src/bin/pg_dump/parallel.c b/src/bin/pg_dump/parallel.c
index 8ad51942ff..8b996f4699 100644
--- a/src/bin/pg_dump/parallel.c
+++ b/src/bin/pg_dump/parallel.c
@@ -63,7 +63,9 @@
 
 #include "parallel.h"
 #include "pg_backup_utils.h"
+
 #include "fe_utils/string_utils.h"
+#include "port/pg_bswap.h"
 
 /* Mnemonic macros for indexing the fd array returned by pipe(2) */
 #define PIPE_READ							0
@@ -1764,8 +1766,8 @@ pgpipe(int handles[2])
 
 	memset((void *) &serv_addr, 0, sizeof(serv_addr));
 	serv_addr.sin_family = AF_INET;
-	serv_addr.sin_port = htons(0);
-	serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+	serv_addr.sin_port = pg_hton16(0);
+	serv_addr.sin_addr.s_addr = pg_hton32(INADDR_LOOPBACK);
 	if (bind(s, (SOCKADDR *) &serv_addr, len) == SOCKET_ERROR)
 	{
 		write_msg(modulename, "pgpipe: could not bind: error code %d\n",
diff --git a/src/bin/pg_rewind/libpq_fetch.c b/src/bin/pg_rewind/libpq_fetch.c
index 0cdff55cab..79bec40b02 100644
--- a/src/bin/pg_rewind/libpq_fetch.c
+++ b/src/bin/pg_rewind/libpq_fetch.c
@@ -14,10 +14,6 @@
 #include <fcntl.h>
 #include <unistd.h>
 
-/* for ntohl/htonl */
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
 #include "pg_rewind.h"
 #include "datapagemap.h"
 #include "fetch.h"
@@ -28,6 +24,7 @@
 #include "libpq-fe.h"
 #include "catalog/catalog.h"
 #include "catalog/pg_type.h"
+#include "port/pg_bswap.h"
 
 static PGconn *conn = NULL;
 
@@ -220,28 +217,6 @@ libpqProcessFileList(void)
 	PQclear(res);
 }
 
-/*
- * Converts an int64 from network byte order to native format.
- */
-static int64
-pg_recvint64(int64 value)
-{
-	union
-	{
-		int64		i64;
-		uint32		i32[2];
-	}			swap;
-	int64		result;
-
-	swap.i64 = value;
-
-	result = (uint32) ntohl(swap.i32[0]);
-	result <<= 32;
-	result |= (uint32) ntohl(swap.i32[1]);
-
-	return result;
-}
-
 /*----
  * Runs a query, which returns pieces of files from the remote source data
  * directory, and overwrites the corresponding parts of target files with
@@ -318,7 +293,7 @@ receiveFileChunks(const char *sql)
 
 		/* Read result set to local variables */
 		memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int64));
-		chunkoff = pg_recvint64(chunkoff);
+		chunkoff = pg_ntoh64(chunkoff);
 		chunksize = PQgetlength(res, 0, 2);
 
 		filenamelen = PQgetlength(res, 0, 0);
diff --git a/src/common/scram-common.c b/src/common/scram-common.c
index e43d035d4d..e54fe1a7c9 100644
--- a/src/common/scram-common.c
+++ b/src/common/scram-common.c
@@ -19,12 +19,9 @@
 #include "postgres_fe.h"
 #endif
 
-/* for htonl */
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
 #include "common/base64.h"
 #include "common/scram-common.h"
+#include "port/pg_bswap.h"
 
 #define HMAC_IPAD 0x36
 #define HMAC_OPAD 0x5C
@@ -109,7 +106,7 @@ scram_SaltedPassword(const char *password,
 					 uint8 *result)
 {
 	int			password_len = strlen(password);
-	uint32		one = htonl(1);
+	uint32		one = pg_hton32(1);
 	int			i,
 				j;
 	uint8		Ui[SCRAM_KEY_LEN];
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index c580d91135..5f79803607 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 343e5303d9..2ff5559233 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 cac6359585..41b1749d07 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 83f74f3985..1320d18a99 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 7da5fb28fb..21fb8f2f21 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));
 	}
diff --git a/src/port/getaddrinfo.c b/src/port/getaddrinfo.c
index e5b5702c79..2e0e313c9f 100644
--- a/src/port/getaddrinfo.c
+++ b/src/port/getaddrinfo.c
@@ -31,6 +31,7 @@
 
 #include "getaddrinfo.h"
 #include "libpq/pqcomm.h"		/* needed for struct sockaddr_storage */
+#include "port/pg_bsawp.h"
 
 
 #ifdef WIN32
@@ -178,7 +179,7 @@ getaddrinfo(const char *node, const char *service,
 	if (node)
 	{
 		if (node[0] == '\0')
-			sin.sin_addr.s_addr = htonl(INADDR_ANY);
+			sin.sin_addr.s_addr = pg_hton32(INADDR_ANY);
 		else if (hints.ai_flags & AI_NUMERICHOST)
 		{
 			if (!inet_aton(node, &sin.sin_addr))
@@ -221,13 +222,13 @@ getaddrinfo(const char *node, const char *service,
 	else
 	{
 		if (hints.ai_flags & AI_PASSIVE)
-			sin.sin_addr.s_addr = htonl(INADDR_ANY);
+			sin.sin_addr.s_addr = pg_hton32(INADDR_ANY);
 		else
-			sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+			sin.sin_addr.s_addr = pg_hton32(INADDR_LOOPBACK);
 	}
 
 	if (service)
-		sin.sin_port = htons((unsigned short) atoi(service));
+		sin.sin_port = pg_hton16((unsigned short) atoi(service));
 
 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
 	sin.sin_len = sizeof(sin);
@@ -402,7 +403,7 @@ getnameinfo(const struct sockaddr *sa, int salen,
 		if (sa->sa_family == AF_INET)
 		{
 			ret = snprintf(service, servicelen, "%d",
-						   ntohs(((struct sockaddr_in *) sa)->sin_port));
+						   pg_ntoh16(((struct sockaddr_in *) sa)->sin_port));
 		}
 		if (ret == -1 || ret >= servicelen)
 			return EAI_MEMORY;
diff --git a/src/port/inet_aton.c b/src/port/inet_aton.c
index 68efd4723e..b31d1f025d 100644
--- a/src/port/inet_aton.c
+++ b/src/port/inet_aton.c
@@ -43,6 +43,8 @@
 #include <netinet/in.h>
 #include <ctype.h>
 
+#include "port/pg_swap.h"
+
 /*
  * Check whether "cp" is a valid ascii representation
  * of an Internet address and convert to a binary address.
@@ -142,6 +144,6 @@ inet_aton(const char *cp, struct in_addr *addr)
 			break;
 	}
 	if (addr)
-		addr->s_addr = htonl(val);
+		addr->s_addr = pg_hton32(val);
 	return 1;
 }
-- 
2.14.1.536.g6867272d5b.dirty

