Commit 0ba99c84 authored by Andres Freund's avatar Andres Freund

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
parent 1f2830f9
...@@ -62,13 +62,10 @@ ...@@ -62,13 +62,10 @@
#include "postgres.h" #include "postgres.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "port/pg_bswap.h"
#include "px-crypt.h" #include "px-crypt.h"
/* for ntohl/htonl */
#include <netinet/in.h>
#include <arpa/inet.h>
#define _PASSWORD_EFMT1 '_' #define _PASSWORD_EFMT1 '_'
static const char _crypt_a64[] = static const char _crypt_a64[] =
...@@ -408,8 +405,8 @@ des_setkey(const char *key) ...@@ -408,8 +405,8 @@ des_setkey(const char *key)
if (!des_initialised) if (!des_initialised)
des_init(); des_init();
rawkey0 = ntohl(*(const uint32 *) key); rawkey0 = pg_ntoh32(*(const uint32 *) key);
rawkey1 = ntohl(*(const uint32 *) (key + 4)); rawkey1 = pg_ntoh32(*(const uint32 *) (key + 4));
if ((rawkey0 | rawkey1) if ((rawkey0 | rawkey1)
&& rawkey0 == old_rawkey0 && rawkey0 == old_rawkey0
...@@ -634,15 +631,15 @@ des_cipher(const char *in, char *out, long salt, int count) ...@@ -634,15 +631,15 @@ des_cipher(const char *in, char *out, long salt, int count)
/* copy data to avoid assuming input is word-aligned */ /* copy data to avoid assuming input is word-aligned */
memcpy(buffer, in, sizeof(buffer)); memcpy(buffer, in, sizeof(buffer));
rawl = ntohl(buffer[0]); rawl = pg_ntoh32(buffer[0]);
rawr = ntohl(buffer[1]); rawr = pg_ntoh32(buffer[1]);
retval = do_des(rawl, rawr, &l_out, &r_out, count); retval = do_des(rawl, rawr, &l_out, &r_out, count);
if (retval) if (retval)
return retval; return retval;
buffer[0] = htonl(l_out); buffer[0] = pg_hton32(l_out);
buffer[1] = htonl(r_out); buffer[1] = pg_hton32(r_out);
/* copy data to avoid assuming output is word-aligned */ /* copy data to avoid assuming output is word-aligned */
memcpy(out, buffer, sizeof(buffer)); memcpy(out, buffer, sizeof(buffer));
......
...@@ -14,13 +14,10 @@ ...@@ -14,13 +14,10 @@
#include "postgres.h" #include "postgres.h"
#include "fmgr.h" #include "fmgr.h"
#include "port/pg_bswap.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/uuid.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. * 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. * We expect configure to set the HAVE_ symbol for only the one we want.
...@@ -90,16 +87,16 @@ typedef struct ...@@ -90,16 +87,16 @@ typedef struct
#define UUID_TO_NETWORK(uu) \ #define UUID_TO_NETWORK(uu) \
do { \ do { \
uu.time_low = htonl(uu.time_low); \ uu.time_low = pg_hton32(uu.time_low); \
uu.time_mid = htons(uu.time_mid); \ uu.time_mid = pg_hton16(uu.time_mid); \
uu.time_hi_and_version = htons(uu.time_hi_and_version); \ uu.time_hi_and_version = pg_hton16(uu.time_hi_and_version); \
} while (0) } while (0)
#define UUID_TO_LOCAL(uu) \ #define UUID_TO_LOCAL(uu) \
do { \ do { \
uu.time_low = ntohl(uu.time_low); \ uu.time_low = pg_ntoh32(uu.time_low); \
uu.time_mid = ntohs(uu.time_mid); \ uu.time_mid = pg_ntoh16(uu.time_mid); \
uu.time_hi_and_version = ntohs(uu.time_hi_and_version); \ uu.time_hi_and_version = pg_ntoh16(uu.time_hi_and_version); \
} while (0) } while (0)
#define UUID_V3_OR_V5(uu, v) \ #define UUID_V3_OR_V5(uu, v) \
......
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,6 @@
#include <ctype.h> #include <ctype.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "access/heapam.h" #include "access/heapam.h"
#include "access/htup_details.h" #include "access/htup_details.h"
...@@ -38,6 +36,7 @@ ...@@ -38,6 +36,7 @@
#include "optimizer/planner.h" #include "optimizer/planner.h"
#include "nodes/makefuncs.h" #include "nodes/makefuncs.h"
#include "parser/parse_relation.h" #include "parser/parse_relation.h"
#include "port/pg_bswap.h"
#include "rewrite/rewriteHandler.h" #include "rewrite/rewriteHandler.h"
#include "storage/fd.h" #include "storage/fd.h"
#include "tcop/tcopprot.h" #include "tcop/tcopprot.h"
...@@ -671,7 +670,7 @@ CopySendInt32(CopyState cstate, int32 val) ...@@ -671,7 +670,7 @@ CopySendInt32(CopyState cstate, int32 val)
{ {
uint32 buf; uint32 buf;
buf = htonl((uint32) val); buf = pg_hton32((uint32) val);
CopySendData(cstate, &buf, sizeof(buf)); CopySendData(cstate, &buf, sizeof(buf));
} }
...@@ -690,7 +689,7 @@ CopyGetInt32(CopyState cstate, int32 *val) ...@@ -690,7 +689,7 @@ CopyGetInt32(CopyState cstate, int32 *val)
*val = 0; /* suppress compiler warning */ *val = 0; /* suppress compiler warning */
return false; return false;
} }
*val = (int32) ntohl(buf); *val = (int32) pg_ntoh32(buf);
return true; return true;
} }
...@@ -702,7 +701,7 @@ CopySendInt16(CopyState cstate, int16 val) ...@@ -702,7 +701,7 @@ CopySendInt16(CopyState cstate, int16 val)
{ {
uint16 buf; uint16 buf;
buf = htons((uint16) val); buf = pg_hton16((uint16) val);
CopySendData(cstate, &buf, sizeof(buf)); CopySendData(cstate, &buf, sizeof(buf));
} }
...@@ -719,7 +718,7 @@ CopyGetInt16(CopyState cstate, int16 *val) ...@@ -719,7 +718,7 @@ CopyGetInt16(CopyState cstate, int16 *val)
*val = 0; /* suppress compiler warning */ *val = 0; /* suppress compiler warning */
return false; return false;
} }
*val = (int16) ntohs(buf); *val = (int16) pg_ntoh16(buf);
return true; return true;
} }
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
#include <sys/param.h> #include <sys/param.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
...@@ -33,6 +32,7 @@ ...@@ -33,6 +32,7 @@
#include "libpq/pqformat.h" #include "libpq/pqformat.h"
#include "libpq/scram.h" #include "libpq/scram.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "port/pg_bswap.h"
#include "replication/walsender.h" #include "replication/walsender.h"
#include "storage/ipc.h" #include "storage/ipc.h"
#include "utils/backend_random.h" #include "utils/backend_random.h"
...@@ -2840,7 +2840,7 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi ...@@ -2840,7 +2840,7 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
radius_packet *receivepacket = &radius_recv_pack; radius_packet *receivepacket = &radius_recv_pack;
char *radius_buffer = (char *) &radius_send_pack; char *radius_buffer = (char *) &radius_send_pack;
char *receive_buffer = (char *) &radius_recv_pack; char *receive_buffer = (char *) &radius_recv_pack;
int32 service = htonl(RADIUS_AUTHENTICATE_ONLY); int32 service = pg_hton32(RADIUS_AUTHENTICATE_ONLY);
uint8 *cryptvector; uint8 *cryptvector;
int encryptedpasswordlen; int encryptedpasswordlen;
uint8 encryptedpassword[RADIUS_MAX_PASSWORD_LENGTH]; uint8 encryptedpassword[RADIUS_MAX_PASSWORD_LENGTH];
...@@ -2948,7 +2948,7 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi ...@@ -2948,7 +2948,7 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
/* Length needs to be in network order on the wire */ /* Length needs to be in network order on the wire */
packetlength = packet->length; packetlength = packet->length;
packet->length = htons(packet->length); packet->length = pg_hton16(packet->length);
sock = socket(serveraddrs[0].ai_family, SOCK_DGRAM, 0); sock = socket(serveraddrs[0].ai_family, SOCK_DGRAM, 0);
if (sock == PGINVALID_SOCKET) if (sock == PGINVALID_SOCKET)
...@@ -3074,19 +3074,19 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi ...@@ -3074,19 +3074,19 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
} }
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
if (remoteaddr.sin6_port != htons(port)) if (remoteaddr.sin6_port != pg_hton16(port))
#else #else
if (remoteaddr.sin_port != htons(port)) if (remoteaddr.sin_port != pg_hton16(port))
#endif #endif
{ {
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
ereport(LOG, ereport(LOG,
(errmsg("RADIUS response from %s was sent from incorrect port: %d", (errmsg("RADIUS response from %s was sent from incorrect port: %d",
server, ntohs(remoteaddr.sin6_port)))); server, pg_ntoh16(remoteaddr.sin6_port))));
#else #else
ereport(LOG, ereport(LOG,
(errmsg("RADIUS response from %s was sent from incorrect port: %d", (errmsg("RADIUS response from %s was sent from incorrect port: %d",
server, ntohs(remoteaddr.sin_port)))); server, pg_ntoh16(remoteaddr.sin_port))));
#endif #endif
continue; continue;
} }
...@@ -3098,11 +3098,11 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi ...@@ -3098,11 +3098,11 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
continue; continue;
} }
if (packetlength != ntohs(receivepacket->length)) if (packetlength != pg_ntoh16(receivepacket->length))
{ {
ereport(LOG, ereport(LOG,
(errmsg("RADIUS response from %s has corrupt length: %d (actual length %d)", (errmsg("RADIUS response from %s has corrupt length: %d (actual length %d)",
server, ntohs(receivepacket->length), packetlength))); server, pg_ntoh16(receivepacket->length), packetlength)));
continue; continue;
} }
......
...@@ -27,10 +27,10 @@ ...@@ -27,10 +27,10 @@
#ifdef HAVE_NETINET_TCP_H #ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h> #include <netinet/tcp.h>
#endif #endif
#include <arpa/inet.h>
#include <sys/file.h> #include <sys/file.h>
#include "libpq/ifaddr.h" #include "libpq/ifaddr.h"
#include "port/pg_bswap.h"
static int range_sockaddr_AF_INET(const struct sockaddr_in *addr, static int range_sockaddr_AF_INET(const struct sockaddr_in *addr,
const struct sockaddr_in *netaddr, const struct sockaddr_in *netaddr,
...@@ -144,7 +144,7 @@ pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family) ...@@ -144,7 +144,7 @@ pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family)
& 0xffffffffUL; & 0xffffffffUL;
else else
maskl = 0; maskl = 0;
mask4.sin_addr.s_addr = htonl(maskl); mask4.sin_addr.s_addr = pg_hton32(maskl);
memcpy(mask, &mask4, sizeof(mask4)); memcpy(mask, &mask4, sizeof(mask4));
break; break;
} }
...@@ -568,7 +568,7 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) ...@@ -568,7 +568,7 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
/* addr 127.0.0.1/8 */ /* addr 127.0.0.1/8 */
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET; 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)); memset(&mask, 0, sizeof(mask));
pg_sockaddr_cidr_mask(&mask, "8", AF_INET); pg_sockaddr_cidr_mask(&mask, "8", AF_INET);
run_ifaddr_callback(callback, cb_data, run_ifaddr_callback(callback, cb_data,
......
...@@ -81,7 +81,6 @@ ...@@ -81,7 +81,6 @@
#ifdef HAVE_NETINET_TCP_H #ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h> #include <netinet/tcp.h>
#endif #endif
#include <arpa/inet.h>
#ifdef HAVE_UTIME_H #ifdef HAVE_UTIME_H
#include <utime.h> #include <utime.h>
#endif #endif
...@@ -92,6 +91,7 @@ ...@@ -92,6 +91,7 @@
#include "common/ip.h" #include "common/ip.h"
#include "libpq/libpq.h" #include "libpq/libpq.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "port/pg_bswap.h"
#include "storage/ipc.h" #include "storage/ipc.h"
#include "utils/guc.h" #include "utils/guc.h"
#include "utils/memutils.h" #include "utils/memutils.h"
...@@ -1286,7 +1286,7 @@ pq_getmessage(StringInfo s, int maxlen) ...@@ -1286,7 +1286,7 @@ pq_getmessage(StringInfo s, int maxlen)
return EOF; return EOF;
} }
len = ntohl(len); len = pg_ntoh32(len);
if (len < 4 || if (len < 4 ||
(maxlen > 0 && len > maxlen)) (maxlen > 0 && len > maxlen))
...@@ -1569,7 +1569,7 @@ socket_putmessage(char msgtype, const char *s, size_t len) ...@@ -1569,7 +1569,7 @@ socket_putmessage(char msgtype, const char *s, size_t len)
{ {
uint32 n32; uint32 n32;
n32 = htonl((uint32) (len + 4)); n32 = pg_hton32((uint32) (len + 4));
if (internal_putbytes((char *) &n32, 4)) if (internal_putbytes((char *) &n32, 4))
goto fail; goto fail;
} }
......
...@@ -72,12 +72,11 @@ ...@@ -72,12 +72,11 @@
#include "postgres.h" #include "postgres.h"
#include <sys/param.h> #include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "libpq/libpq.h" #include "libpq/libpq.h"
#include "libpq/pqformat.h" #include "libpq/pqformat.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#include "port/pg_bswap.h"
/* -------------------------------- /* --------------------------------
...@@ -246,11 +245,11 @@ pq_sendint(StringInfo buf, int i, int b) ...@@ -246,11 +245,11 @@ pq_sendint(StringInfo buf, int i, int b)
appendBinaryStringInfo(buf, (char *) &n8, 1); appendBinaryStringInfo(buf, (char *) &n8, 1);
break; break;
case 2: case 2:
n16 = htons((uint16) i); n16 = pg_hton16((uint16) i);
appendBinaryStringInfo(buf, (char *) &n16, 2); appendBinaryStringInfo(buf, (char *) &n16, 2);
break; break;
case 4: case 4:
n32 = htonl((uint32) i); n32 = pg_hton32((uint32) i);
appendBinaryStringInfo(buf, (char *) &n32, 4); appendBinaryStringInfo(buf, (char *) &n32, 4);
break; break;
default: default:
...@@ -270,17 +269,9 @@ pq_sendint(StringInfo buf, int i, int b) ...@@ -270,17 +269,9 @@ pq_sendint(StringInfo buf, int i, int b)
void void
pq_sendint64(StringInfo buf, int64 i) 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 */ appendBinaryStringInfo(buf, (char *) &n64, sizeof(n64));
n32 = (uint32) i;
n32 = htonl(n32);
appendBinaryStringInfo(buf, (char *) &n32, 4);
} }
/* -------------------------------- /* --------------------------------
...@@ -304,7 +295,7 @@ pq_sendfloat4(StringInfo buf, float4 f) ...@@ -304,7 +295,7 @@ pq_sendfloat4(StringInfo buf, float4 f)
} swap; } swap;
swap.f = f; swap.f = f;
swap.i = htonl(swap.i); swap.i = pg_hton32(swap.i);
appendBinaryStringInfo(buf, (char *) &swap.i, 4); appendBinaryStringInfo(buf, (char *) &swap.i, 4);
} }
...@@ -460,11 +451,11 @@ pq_getmsgint(StringInfo msg, int b) ...@@ -460,11 +451,11 @@ pq_getmsgint(StringInfo msg, int b)
break; break;
case 2: case 2:
pq_copymsgbytes(msg, (char *) &n16, 2); pq_copymsgbytes(msg, (char *) &n16, 2);
result = ntohs(n16); result = pg_ntoh16(n16);
break; break;
case 4: case 4:
pq_copymsgbytes(msg, (char *) &n32, 4); pq_copymsgbytes(msg, (char *) &n32, 4);
result = ntohl(n32); result = pg_ntoh32(n32);
break; break;
default: default:
elog(ERROR, "unsupported integer size %d", b); elog(ERROR, "unsupported integer size %d", b);
...@@ -485,20 +476,11 @@ pq_getmsgint(StringInfo msg, int b) ...@@ -485,20 +476,11 @@ pq_getmsgint(StringInfo msg, int b)
int64 int64
pq_getmsgint64(StringInfo msg) pq_getmsgint64(StringInfo msg)
{ {
int64 result; uint64 n64;
uint32 h32;
uint32 l32;
pq_copymsgbytes(msg, (char *) &h32, 4); pq_copymsgbytes(msg, (char *) &n64, sizeof(n64));
pq_copymsgbytes(msg, (char *) &l32, 4);
h32 = ntohl(h32);
l32 = ntohl(l32);
result = h32; return pg_ntoh64(n64);
result <<= 32;
result |= l32;
return result;
} }
/* -------------------------------- /* --------------------------------
......
...@@ -74,8 +74,6 @@ ...@@ -74,8 +74,6 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/param.h> #include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <limits.h> #include <limits.h>
...@@ -107,6 +105,7 @@ ...@@ -107,6 +105,7 @@
#include "miscadmin.h" #include "miscadmin.h"
#include "pg_getopt.h" #include "pg_getopt.h"
#include "pgstat.h" #include "pgstat.h"
#include "port/pg_bswap.h"
#include "postmaster/autovacuum.h" #include "postmaster/autovacuum.h"
#include "postmaster/bgworker_internals.h" #include "postmaster/bgworker_internals.h"
#include "postmaster/fork_process.h" #include "postmaster/fork_process.h"
...@@ -1072,7 +1071,7 @@ PostmasterMain(int argc, char *argv[]) ...@@ -1072,7 +1071,7 @@ PostmasterMain(int argc, char *argv[])
"_postgresql._tcp.", "_postgresql._tcp.",
NULL, NULL,
NULL, NULL,
htons(PostPortNumber), pg_hton16(PostPortNumber),
0, 0,
NULL, NULL,
NULL, NULL,
...@@ -1966,7 +1965,7 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -1966,7 +1965,7 @@ ProcessStartupPacket(Port *port, bool SSLdone)
return STATUS_ERROR; return STATUS_ERROR;
} }
len = ntohl(len); len = pg_ntoh32(len);
len -= 4; len -= 4;
if (len < (int32) sizeof(ProtocolVersion) || if (len < (int32) sizeof(ProtocolVersion) ||
...@@ -2002,7 +2001,7 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -2002,7 +2001,7 @@ ProcessStartupPacket(Port *port, bool SSLdone)
* The first field is either a protocol version number or a special * The first field is either a protocol version number or a special
* request code. * request code.
*/ */
port->proto = proto = ntohl(*((ProtocolVersion *) buf)); port->proto = proto = pg_ntoh32(*((ProtocolVersion *) buf));
if (proto == CANCEL_REQUEST_CODE) if (proto == CANCEL_REQUEST_CODE)
{ {
...@@ -2281,8 +2280,8 @@ processCancelRequest(Port *port, void *pkt) ...@@ -2281,8 +2280,8 @@ processCancelRequest(Port *port, void *pkt)
int i; int i;
#endif #endif
backendPID = (int) ntohl(canc->backendPID); backendPID = (int) pg_ntoh32(canc->backendPID);
cancelAuthCode = (int32) ntohl(canc->cancelAuthCode); cancelAuthCode = (int32) pg_ntoh32(canc->cancelAuthCode);
/* /*
* See if we have a matching backend. In the EXEC_BACKEND case, we can no * See if we have a matching backend. In the EXEC_BACKEND case, we can no
......
...@@ -17,9 +17,6 @@ ...@@ -17,9 +17,6 @@
*/ */
#include "postgres.h" #include "postgres.h"
#include <netinet/in.h>
#include <arpa/inet.h>
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/xact.h" #include "access/xact.h"
#include "catalog/objectaccess.h" #include "catalog/objectaccess.h"
...@@ -28,6 +25,7 @@ ...@@ -28,6 +25,7 @@
#include "libpq/pqformat.h" #include "libpq/pqformat.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "port/pg_bswap.h"
#include "tcop/fastpath.h" #include "tcop/fastpath.h"
#include "tcop/tcopprot.h" #include "tcop/tcopprot.h"
#include "utils/acl.h" #include "utils/acl.h"
...@@ -92,7 +90,7 @@ GetOldFunctionMessage(StringInfo buf) ...@@ -92,7 +90,7 @@ GetOldFunctionMessage(StringInfo buf)
if (pq_getbytes((char *) &ibuf, 4)) if (pq_getbytes((char *) &ibuf, 4))
return EOF; return EOF;
appendBinaryStringInfo(buf, (char *) &ibuf, 4); appendBinaryStringInfo(buf, (char *) &ibuf, 4);
nargs = ntohl(ibuf); nargs = pg_ntoh32(ibuf);
/* For each argument ... */ /* For each argument ... */
while (nargs-- > 0) while (nargs-- > 0)
{ {
...@@ -102,7 +100,7 @@ GetOldFunctionMessage(StringInfo buf) ...@@ -102,7 +100,7 @@ GetOldFunctionMessage(StringInfo buf)
if (pq_getbytes((char *) &ibuf, 4)) if (pq_getbytes((char *) &ibuf, 4))
return EOF; return EOF;
appendBinaryStringInfo(buf, (char *) &ibuf, 4); appendBinaryStringInfo(buf, (char *) &ibuf, 4);
argsize = ntohl(ibuf); argsize = pg_ntoh32(ibuf);
if (argsize < -1) if (argsize < -1)
{ {
/* FATAL here since no hope of regaining message sync */ /* FATAL here since no hope of regaining message sync */
......
...@@ -17,18 +17,15 @@ ...@@ -17,18 +17,15 @@
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
/* for ntohl/htonl */
#include <netinet/in.h>
#include <arpa/inet.h>
/* local includes */ /* local includes */
#include "receivelog.h" #include "receivelog.h"
#include "streamutil.h" #include "streamutil.h"
#include "access/xlog_internal.h" #include "access/xlog_internal.h"
#include "pqexpbuffer.h"
#include "common/fe_memutils.h" #include "common/fe_memutils.h"
#include "datatype/timestamp.h" #include "datatype/timestamp.h"
#include "port/pg_bswap.h"
#include "pqexpbuffer.h"
#define ERRCODE_DUPLICATE_OBJECT "42710" #define ERRCODE_DUPLICATE_OBJECT "42710"
...@@ -576,17 +573,9 @@ feTimestampDifferenceExceeds(TimestampTz start_time, ...@@ -576,17 +573,9 @@ feTimestampDifferenceExceeds(TimestampTz start_time,
void void
fe_sendint64(int64 i, char *buf) fe_sendint64(int64 i, char *buf)
{ {
uint32 n32; uint64 n64 = pg_hton64(i);
/* High order half first, since we're doing MSB-first */ memcpy(buf, &n64, sizeof(n64));
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);
} }
/* /*
...@@ -595,18 +584,9 @@ fe_sendint64(int64 i, char *buf) ...@@ -595,18 +584,9 @@ fe_sendint64(int64 i, char *buf)
int64 int64
fe_recvint64(char *buf) fe_recvint64(char *buf)
{ {
int64 result; uint64 n64;
uint32 h32;
uint32 l32;
memcpy(&h32, buf, 4);
memcpy(&l32, buf + 4, 4);
h32 = ntohl(h32);
l32 = ntohl(l32);
result = h32; memcpy(&n64, buf, sizeof(n64));
result <<= 32;
result |= l32;
return result; return pg_ntoh64(n64);
} }
...@@ -63,7 +63,9 @@ ...@@ -63,7 +63,9 @@
#include "parallel.h" #include "parallel.h"
#include "pg_backup_utils.h" #include "pg_backup_utils.h"
#include "fe_utils/string_utils.h" #include "fe_utils/string_utils.h"
#include "port/pg_bswap.h"
/* Mnemonic macros for indexing the fd array returned by pipe(2) */ /* Mnemonic macros for indexing the fd array returned by pipe(2) */
#define PIPE_READ 0 #define PIPE_READ 0
...@@ -1764,8 +1766,8 @@ pgpipe(int handles[2]) ...@@ -1764,8 +1766,8 @@ pgpipe(int handles[2])
memset((void *) &serv_addr, 0, sizeof(serv_addr)); memset((void *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(0); serv_addr.sin_port = pg_hton16(0);
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); serv_addr.sin_addr.s_addr = pg_hton32(INADDR_LOOPBACK);
if (bind(s, (SOCKADDR *) &serv_addr, len) == SOCKET_ERROR) if (bind(s, (SOCKADDR *) &serv_addr, len) == SOCKET_ERROR)
{ {
write_msg(modulename, "pgpipe: could not bind: error code %d\n", write_msg(modulename, "pgpipe: could not bind: error code %d\n",
......
...@@ -14,10 +14,6 @@ ...@@ -14,10 +14,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
/* for ntohl/htonl */
#include <netinet/in.h>
#include <arpa/inet.h>
#include "pg_rewind.h" #include "pg_rewind.h"
#include "datapagemap.h" #include "datapagemap.h"
#include "fetch.h" #include "fetch.h"
...@@ -28,6 +24,7 @@ ...@@ -28,6 +24,7 @@
#include "libpq-fe.h" #include "libpq-fe.h"
#include "catalog/catalog.h" #include "catalog/catalog.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "port/pg_bswap.h"
static PGconn *conn = NULL; static PGconn *conn = NULL;
...@@ -220,28 +217,6 @@ libpqProcessFileList(void) ...@@ -220,28 +217,6 @@ libpqProcessFileList(void)
PQclear(res); 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 * Runs a query, which returns pieces of files from the remote source data
* directory, and overwrites the corresponding parts of target files with * directory, and overwrites the corresponding parts of target files with
...@@ -318,7 +293,7 @@ receiveFileChunks(const char *sql) ...@@ -318,7 +293,7 @@ receiveFileChunks(const char *sql)
/* Read result set to local variables */ /* Read result set to local variables */
memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int64)); memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int64));
chunkoff = pg_recvint64(chunkoff); chunkoff = pg_ntoh64(chunkoff);
chunksize = PQgetlength(res, 0, 2); chunksize = PQgetlength(res, 0, 2);
filenamelen = PQgetlength(res, 0, 0); filenamelen = PQgetlength(res, 0, 0);
......
...@@ -19,12 +19,9 @@ ...@@ -19,12 +19,9 @@
#include "postgres_fe.h" #include "postgres_fe.h"
#endif #endif
/* for htonl */
#include <netinet/in.h>
#include <arpa/inet.h>
#include "common/base64.h" #include "common/base64.h"
#include "common/scram-common.h" #include "common/scram-common.h"
#include "port/pg_bswap.h"
#define HMAC_IPAD 0x36 #define HMAC_IPAD 0x36
#define HMAC_OPAD 0x5C #define HMAC_OPAD 0x5C
...@@ -109,7 +106,7 @@ scram_SaltedPassword(const char *password, ...@@ -109,7 +106,7 @@ scram_SaltedPassword(const char *password,
uint8 *result) uint8 *result)
{ {
int password_len = strlen(password); int password_len = strlen(password);
uint32 one = htonl(1); uint32 one = pg_hton32(1);
int i, int i,
j; j;
uint8 Ui[SCRAM_KEY_LEN]; uint8 Ui[SCRAM_KEY_LEN];
......
...@@ -47,7 +47,6 @@ ...@@ -47,7 +47,6 @@
#ifdef HAVE_NETINET_TCP_H #ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h> #include <netinet/tcp.h>
#endif #endif
#include <arpa/inet.h>
#endif #endif
#ifdef ENABLE_THREAD_SAFETY #ifdef ENABLE_THREAD_SAFETY
...@@ -73,6 +72,7 @@ static int ldapServiceLookup(const char *purl, PQconninfoOption *options, ...@@ -73,6 +72,7 @@ static int ldapServiceLookup(const char *purl, PQconninfoOption *options,
#include "common/ip.h" #include "common/ip.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#include "port/pg_bswap.h"
#ifndef WIN32 #ifndef WIN32
...@@ -2443,7 +2443,7 @@ keep_going: /* We will come back to here until there is ...@@ -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 * shouldn't since we only got here if the socket is
* write-ready. * write-ready.
*/ */
pv = htonl(NEGOTIATE_SSL_CODE); pv = pg_hton32(NEGOTIATE_SSL_CODE);
if (pqPacketSend(conn, 0, &pv, sizeof(pv)) != STATUS_OK) if (pqPacketSend(conn, 0, &pv, sizeof(pv)) != STATUS_OK)
{ {
appendPQExpBuffer(&conn->errorMessage, appendPQExpBuffer(&conn->errorMessage,
...@@ -3838,10 +3838,10 @@ retry3: ...@@ -3838,10 +3838,10 @@ retry3:
/* Create and send the cancel request packet. */ /* Create and send the cancel request packet. */
crp.packetlen = htonl((uint32) sizeof(crp)); crp.packetlen = pg_hton32((uint32) sizeof(crp));
crp.cp.cancelRequestCode = (MsgType) htonl(CANCEL_REQUEST_CODE); crp.cp.cancelRequestCode = (MsgType) pg_hton32(CANCEL_REQUEST_CODE);
crp.cp.backendPID = htonl(be_pid); crp.cp.backendPID = pg_hton32(be_pid);
crp.cp.cancelAuthCode = htonl(be_key); crp.cp.cancelAuthCode = pg_hton32(be_key);
retry4: retry4:
if (send(tmpsock, (char *) &crp, sizeof(crp), 0) != (int) sizeof(crp)) if (send(tmpsock, (char *) &crp, sizeof(crp), 0) != (int) sizeof(crp))
......
...@@ -33,12 +33,11 @@ ...@@ -33,12 +33,11 @@
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <netinet/in.h> /* for ntohl/htonl */
#include <arpa/inet.h>
#include "libpq-fe.h" #include "libpq-fe.h"
#include "libpq-int.h" #include "libpq-int.h"
#include "libpq/libpq-fs.h" /* must come after sys/stat.h */ #include "libpq/libpq-fs.h" /* must come after sys/stat.h */
#include "port/pg_bswap.h"
#define LO_BUFSIZE 8192 #define LO_BUFSIZE 8192
...@@ -1070,11 +1069,11 @@ lo_hton64(pg_int64 host64) ...@@ -1070,11 +1069,11 @@ lo_hton64(pg_int64 host64)
/* High order half first, since we're doing MSB-first */ /* High order half first, since we're doing MSB-first */
t = (uint32) (host64 >> 32); t = (uint32) (host64 >> 32);
swap.i32[0] = htonl(t); swap.i32[0] = pg_hton32(t);
/* Now the low order half */ /* Now the low order half */
t = (uint32) host64; t = (uint32) host64;
swap.i32[1] = htonl(t); swap.i32[1] = pg_hton32(t);
return swap.i64; return swap.i64;
} }
...@@ -1095,9 +1094,9 @@ lo_ntoh64(pg_int64 net64) ...@@ -1095,9 +1094,9 @@ lo_ntoh64(pg_int64 net64)
swap.i64 = net64; swap.i64 = net64;
result = (uint32) ntohl(swap.i32[0]); result = (uint32) pg_ntoh32(swap.i32[0]);
result <<= 32; result <<= 32;
result |= (uint32) ntohl(swap.i32[1]); result |= (uint32) pg_ntoh32(swap.i32[1]);
return result; return result;
} }
...@@ -33,9 +33,6 @@ ...@@ -33,9 +33,6 @@
#include <signal.h> #include <signal.h>
#include <time.h> #include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef WIN32 #ifdef WIN32
#include "win32.h" #include "win32.h"
#else #else
...@@ -53,6 +50,7 @@ ...@@ -53,6 +50,7 @@
#include "libpq-fe.h" #include "libpq-fe.h"
#include "libpq-int.h" #include "libpq-int.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#include "port/pg_bswap.h"
#include "pg_config_paths.h" #include "pg_config_paths.h"
...@@ -278,14 +276,14 @@ pqGetInt(int *result, size_t bytes, PGconn *conn) ...@@ -278,14 +276,14 @@ pqGetInt(int *result, size_t bytes, PGconn *conn)
return EOF; return EOF;
memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2); memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
conn->inCursor += 2; conn->inCursor += 2;
*result = (int) ntohs(tmp2); *result = (int) pg_ntoh16(tmp2);
break; break;
case 4: case 4:
if (conn->inCursor + 4 > conn->inEnd) if (conn->inCursor + 4 > conn->inEnd)
return EOF; return EOF;
memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4); memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
conn->inCursor += 4; conn->inCursor += 4;
*result = (int) ntohl(tmp4); *result = (int) pg_ntoh32(tmp4);
break; break;
default: default:
pqInternalNotice(&conn->noticeHooks, pqInternalNotice(&conn->noticeHooks,
...@@ -314,12 +312,12 @@ pqPutInt(int value, size_t bytes, PGconn *conn) ...@@ -314,12 +312,12 @@ pqPutInt(int value, size_t bytes, PGconn *conn)
switch (bytes) switch (bytes)
{ {
case 2: case 2:
tmp2 = htons((uint16) value); tmp2 = pg_hton16((uint16) value);
if (pqPutMsgBytes((const char *) &tmp2, 2, conn)) if (pqPutMsgBytes((const char *) &tmp2, 2, conn))
return EOF; return EOF;
break; break;
case 4: case 4:
tmp4 = htonl((uint32) value); tmp4 = pg_hton32((uint32) value);
if (pqPutMsgBytes((const char *) &tmp4, 4, conn)) if (pqPutMsgBytes((const char *) &tmp4, 4, conn))
return EOF; return EOF;
break; break;
...@@ -597,7 +595,7 @@ pqPutMsgEnd(PGconn *conn) ...@@ -597,7 +595,7 @@ pqPutMsgEnd(PGconn *conn)
{ {
uint32 msgLen = conn->outMsgEnd - conn->outMsgStart; uint32 msgLen = conn->outMsgEnd - conn->outMsgStart;
msgLen = htonl(msgLen); msgLen = pg_hton32(msgLen);
memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4); memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4);
} }
......
...@@ -19,17 +19,16 @@ ...@@ -19,17 +19,16 @@
#include "libpq-fe.h" #include "libpq-fe.h"
#include "libpq-int.h" #include "libpq-int.h"
#include "port/pg_bswap.h"
#ifdef WIN32 #ifdef WIN32
#include "win32.h" #include "win32.h"
#else #else
#include <unistd.h> #include <unistd.h>
#include <netinet/in.h>
#ifdef HAVE_NETINET_TCP_H #ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h> #include <netinet/tcp.h>
#endif #endif
#include <arpa/inet.h>
#endif #endif
...@@ -1609,7 +1608,7 @@ pqBuildStartupPacket2(PGconn *conn, int *packetlen, ...@@ -1609,7 +1608,7 @@ pqBuildStartupPacket2(PGconn *conn, int *packetlen,
MemSet(startpacket, 0, sizeof(StartupPacket)); 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 is safe here: postmaster will handle full fields correctly */
strncpy(startpacket->user, conn->pguser, SM_USER); strncpy(startpacket->user, conn->pguser, SM_USER);
......
...@@ -21,16 +21,15 @@ ...@@ -21,16 +21,15 @@
#include "libpq-int.h" #include "libpq-int.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#include "port/pg_bswap.h"
#ifdef WIN32 #ifdef WIN32
#include "win32.h" #include "win32.h"
#else #else
#include <unistd.h> #include <unistd.h>
#include <netinet/in.h>
#ifdef HAVE_NETINET_TCP_H #ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h> #include <netinet/tcp.h>
#endif #endif
#include <arpa/inet.h>
#endif #endif
...@@ -2148,7 +2147,7 @@ build_startup_packet(const PGconn *conn, char *packet, ...@@ -2148,7 +2147,7 @@ build_startup_packet(const PGconn *conn, char *packet,
/* Protocol version comes first. */ /* Protocol version comes first. */
if (packet) if (packet)
{ {
ProtocolVersion pv = htonl(conn->pversion); ProtocolVersion pv = pg_hton32(conn->pversion);
memcpy(packet + packet_len, &pv, sizeof(ProtocolVersion)); memcpy(packet + packet_len, &pv, sizeof(ProtocolVersion));
} }
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "getaddrinfo.h" #include "getaddrinfo.h"
#include "libpq/pqcomm.h" /* needed for struct sockaddr_storage */ #include "libpq/pqcomm.h" /* needed for struct sockaddr_storage */
#include "port/pg_bsawp.h"
#ifdef WIN32 #ifdef WIN32
...@@ -178,7 +179,7 @@ getaddrinfo(const char *node, const char *service, ...@@ -178,7 +179,7 @@ getaddrinfo(const char *node, const char *service,
if (node) if (node)
{ {
if (node[0] == '\0') 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) else if (hints.ai_flags & AI_NUMERICHOST)
{ {
if (!inet_aton(node, &sin.sin_addr)) if (!inet_aton(node, &sin.sin_addr))
...@@ -221,13 +222,13 @@ getaddrinfo(const char *node, const char *service, ...@@ -221,13 +222,13 @@ getaddrinfo(const char *node, const char *service,
else else
{ {
if (hints.ai_flags & AI_PASSIVE) if (hints.ai_flags & AI_PASSIVE)
sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_addr.s_addr = pg_hton32(INADDR_ANY);
else else
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); sin.sin_addr.s_addr = pg_hton32(INADDR_LOOPBACK);
} }
if (service) 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 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
sin.sin_len = sizeof(sin); sin.sin_len = sizeof(sin);
...@@ -402,7 +403,7 @@ getnameinfo(const struct sockaddr *sa, int salen, ...@@ -402,7 +403,7 @@ getnameinfo(const struct sockaddr *sa, int salen,
if (sa->sa_family == AF_INET) if (sa->sa_family == AF_INET)
{ {
ret = snprintf(service, servicelen, "%d", 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) if (ret == -1 || ret >= servicelen)
return EAI_MEMORY; return EAI_MEMORY;
......
...@@ -43,6 +43,8 @@ ...@@ -43,6 +43,8 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <ctype.h> #include <ctype.h>
#include "port/pg_swap.h"
/* /*
* Check whether "cp" is a valid ascii representation * Check whether "cp" is a valid ascii representation
* of an Internet address and convert to a binary address. * of an Internet address and convert to a binary address.
...@@ -142,6 +144,6 @@ inet_aton(const char *cp, struct in_addr *addr) ...@@ -142,6 +144,6 @@ inet_aton(const char *cp, struct in_addr *addr)
break; break;
} }
if (addr) if (addr)
addr->s_addr = htonl(val); addr->s_addr = pg_hton32(val);
return 1; return 1;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment