1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-26 23:43:30 +03:00

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
This commit is contained in:
Andres Freund
2017-10-01 15:36:14 -07:00
parent 1f2830f9df
commit 0ba99c84e8
20 changed files with 99 additions and 175 deletions

View File

@@ -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;
}

View File

@@ -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,

View File

@@ -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;
}

View File

@@ -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);
}
/* --------------------------------