Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0ba99c8

Browse files
committed
Replace most usages of ntoh[ls] and hton[sl] with pg_bswap.h.
All postgres internal usages are replaced, it's just libpq exampleusages that haven't been converted. External users of libpq can'tgenerally rely on including postgres internal headers.Note that this includes replacing open-coded byte swapping of 64bitintegers (using two 32 bit swaps) with a single 64bit swap.Where it looked applicable, I have removed netinet/in.h andarpa/inet.h usage, which previously provided the relevantfunctionality. It's perfectly possible that I missed other reasons forincluding those, the buildfarm will tell.Author: Andres FreundDiscussion:https://postgr.es/m/20170927172019.gheidqy6xvlxb325@alap3.anarazel.de
1 parent1f2830f commit0ba99c8

File tree

20 files changed

+99
-175
lines changed

20 files changed

+99
-175
lines changed

‎contrib/pgcrypto/crypt-des.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,10 @@
6262

6363
#include"postgres.h"
6464
#include"miscadmin.h"
65+
#include"port/pg_bswap.h"
6566

6667
#include"px-crypt.h"
6768

68-
/* for ntohl/htonl */
69-
#include<netinet/in.h>
70-
#include<arpa/inet.h>
71-
7269
#define_PASSWORD_EFMT1 '_'
7370

7471
staticconstchar_crypt_a64[]=
@@ -408,8 +405,8 @@ des_setkey(const char *key)
408405
if (!des_initialised)
409406
des_init();
410407

411-
rawkey0=ntohl(*(constuint32*)key);
412-
rawkey1=ntohl(*(constuint32*) (key+4));
408+
rawkey0=pg_ntoh32(*(constuint32*)key);
409+
rawkey1=pg_ntoh32(*(constuint32*) (key+4));
413410

414411
if ((rawkey0 |rawkey1)
415412
&&rawkey0==old_rawkey0
@@ -634,15 +631,15 @@ des_cipher(const char *in, char *out, long salt, int count)
634631
/* copy data to avoid assuming input is word-aligned */
635632
memcpy(buffer,in,sizeof(buffer));
636633

637-
rawl=ntohl(buffer[0]);
638-
rawr=ntohl(buffer[1]);
634+
rawl=pg_ntoh32(buffer[0]);
635+
rawr=pg_ntoh32(buffer[1]);
639636

640637
retval=do_des(rawl,rawr,&l_out,&r_out,count);
641638
if (retval)
642639
returnretval;
643640

644-
buffer[0]=htonl(l_out);
645-
buffer[1]=htonl(r_out);
641+
buffer[0]=pg_hton32(l_out);
642+
buffer[1]=pg_hton32(r_out);
646643

647644
/* copy data to avoid assuming output is word-aligned */
648645
memcpy(out,buffer,sizeof(buffer));

‎contrib/uuid-ossp/uuid-ossp.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414
#include"postgres.h"
1515

1616
#include"fmgr.h"
17+
#include"port/pg_bswap.h"
1718
#include"utils/builtins.h"
1819
#include"utils/uuid.h"
1920

20-
/* for ntohl/htonl */
21-
#include<netinet/in.h>
22-
#include<arpa/inet.h>
23-
2421
/*
2522
* It's possible that there's more than one uuid.h header file present.
2623
* We expect configure to set the HAVE_ symbol for only the one we want.
@@ -90,16 +87,16 @@ typedef struct
9087

9188
#defineUUID_TO_NETWORK(uu) \
9289
do { \
93-
uu.time_low =htonl(uu.time_low); \
94-
uu.time_mid =htons(uu.time_mid); \
95-
uu.time_hi_and_version =htons(uu.time_hi_and_version); \
90+
uu.time_low =pg_hton32(uu.time_low); \
91+
uu.time_mid =pg_hton16(uu.time_mid); \
92+
uu.time_hi_and_version =pg_hton16(uu.time_hi_and_version); \
9693
} while (0)
9794

9895
#defineUUID_TO_LOCAL(uu) \
9996
do { \
100-
uu.time_low =ntohl(uu.time_low); \
101-
uu.time_mid =ntohs(uu.time_mid); \
102-
uu.time_hi_and_version =ntohs(uu.time_hi_and_version); \
97+
uu.time_low =pg_ntoh32(uu.time_low); \
98+
uu.time_mid =pg_ntoh16(uu.time_mid); \
99+
uu.time_hi_and_version =pg_ntoh16(uu.time_hi_and_version); \
103100
} while (0)
104101

105102
#defineUUID_V3_OR_V5(uu,v) \

‎src/backend/commands/copy.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include<ctype.h>
1818
#include<unistd.h>
1919
#include<sys/stat.h>
20-
#include<netinet/in.h>
21-
#include<arpa/inet.h>
2220

2321
#include"access/heapam.h"
2422
#include"access/htup_details.h"
@@ -38,6 +36,7 @@
3836
#include"optimizer/planner.h"
3937
#include"nodes/makefuncs.h"
4038
#include"parser/parse_relation.h"
39+
#include"port/pg_bswap.h"
4140
#include"rewrite/rewriteHandler.h"
4241
#include"storage/fd.h"
4342
#include"tcop/tcopprot.h"
@@ -671,7 +670,7 @@ CopySendInt32(CopyState cstate, int32 val)
671670
{
672671
uint32buf;
673672

674-
buf=htonl((uint32)val);
673+
buf=pg_hton32((uint32)val);
675674
CopySendData(cstate,&buf,sizeof(buf));
676675
}
677676

@@ -690,7 +689,7 @@ CopyGetInt32(CopyState cstate, int32 *val)
690689
*val=0;/* suppress compiler warning */
691690
return false;
692691
}
693-
*val= (int32)ntohl(buf);
692+
*val= (int32)pg_ntoh32(buf);
694693
return true;
695694
}
696695

@@ -702,7 +701,7 @@ CopySendInt16(CopyState cstate, int16 val)
702701
{
703702
uint16buf;
704703

705-
buf=htons((uint16)val);
704+
buf=pg_hton16((uint16)val);
706705
CopySendData(cstate,&buf,sizeof(buf));
707706
}
708707

@@ -719,7 +718,7 @@ CopyGetInt16(CopyState cstate, int16 *val)
719718
*val=0;/* suppress compiler warning */
720719
return false;
721720
}
722-
*val= (int16)ntohs(buf);
721+
*val= (int16)pg_ntoh16(buf);
723722
return true;
724723
}
725724

‎src/backend/libpq/auth.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include<sys/param.h>
1919
#include<sys/socket.h>
2020
#include<netinet/in.h>
21-
#include<arpa/inet.h>
2221
#include<unistd.h>
2322
#ifdefHAVE_SYS_SELECT_H
2423
#include<sys/select.h>
@@ -33,6 +32,7 @@
3332
#include"libpq/pqformat.h"
3433
#include"libpq/scram.h"
3534
#include"miscadmin.h"
35+
#include"port/pg_bswap.h"
3636
#include"replication/walsender.h"
3737
#include"storage/ipc.h"
3838
#include"utils/backend_random.h"
@@ -2840,7 +2840,7 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
28402840
radius_packet*receivepacket=&radius_recv_pack;
28412841
char*radius_buffer= (char*)&radius_send_pack;
28422842
char*receive_buffer= (char*)&radius_recv_pack;
2843-
int32service=htonl(RADIUS_AUTHENTICATE_ONLY);
2843+
int32service=pg_hton32(RADIUS_AUTHENTICATE_ONLY);
28442844
uint8*cryptvector;
28452845
intencryptedpasswordlen;
28462846
uint8encryptedpassword[RADIUS_MAX_PASSWORD_LENGTH];
@@ -2948,7 +2948,7 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
29482948

29492949
/* Length needs to be in network order on the wire */
29502950
packetlength=packet->length;
2951-
packet->length=htons(packet->length);
2951+
packet->length=pg_hton16(packet->length);
29522952

29532953
sock=socket(serveraddrs[0].ai_family,SOCK_DGRAM,0);
29542954
if (sock==PGINVALID_SOCKET)
@@ -3074,19 +3074,19 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
30743074
}
30753075

30763076
#ifdefHAVE_IPV6
3077-
if (remoteaddr.sin6_port!=htons(port))
3077+
if (remoteaddr.sin6_port!=pg_hton16(port))
30783078
#else
3079-
if (remoteaddr.sin_port!=htons(port))
3079+
if (remoteaddr.sin_port!=pg_hton16(port))
30803080
#endif
30813081
{
30823082
#ifdefHAVE_IPV6
30833083
ereport(LOG,
30843084
(errmsg("RADIUS response from %s was sent from incorrect port: %d",
3085-
server,ntohs(remoteaddr.sin6_port))));
3085+
server,pg_ntoh16(remoteaddr.sin6_port))));
30863086
#else
30873087
ereport(LOG,
30883088
(errmsg("RADIUS response from %s was sent from incorrect port: %d",
3089-
server,ntohs(remoteaddr.sin_port))));
3089+
server,pg_ntoh16(remoteaddr.sin_port))));
30903090
#endif
30913091
continue;
30923092
}
@@ -3098,11 +3098,11 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi
30983098
continue;
30993099
}
31003100

3101-
if (packetlength!=ntohs(receivepacket->length))
3101+
if (packetlength!=pg_ntoh16(receivepacket->length))
31023102
{
31033103
ereport(LOG,
31043104
(errmsg("RADIUS response from %s has corrupt length: %d (actual length %d)",
3105-
server,ntohs(receivepacket->length),packetlength)));
3105+
server,pg_ntoh16(receivepacket->length),packetlength)));
31063106
continue;
31073107
}
31083108

‎src/backend/libpq/ifaddr.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
#ifdefHAVE_NETINET_TCP_H
2828
#include<netinet/tcp.h>
2929
#endif
30-
#include<arpa/inet.h>
3130
#include<sys/file.h>
3231

3332
#include"libpq/ifaddr.h"
33+
#include"port/pg_bswap.h"
3434

3535
staticintrange_sockaddr_AF_INET(conststructsockaddr_in*addr,
3636
conststructsockaddr_in*netaddr,
@@ -144,7 +144,7 @@ pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family)
144144
&0xffffffffUL;
145145
else
146146
maskl=0;
147-
mask4.sin_addr.s_addr=htonl(maskl);
147+
mask4.sin_addr.s_addr=pg_hton32(maskl);
148148
memcpy(mask,&mask4,sizeof(mask4));
149149
break;
150150
}
@@ -568,7 +568,7 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
568568
/* addr 127.0.0.1/8 */
569569
memset(&addr,0,sizeof(addr));
570570
addr.sin_family=AF_INET;
571-
addr.sin_addr.s_addr=ntohl(0x7f000001);
571+
addr.sin_addr.s_addr=pg_ntoh32(0x7f000001);
572572
memset(&mask,0,sizeof(mask));
573573
pg_sockaddr_cidr_mask(&mask,"8",AF_INET);
574574
run_ifaddr_callback(callback,cb_data,

‎src/backend/libpq/pqcomm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
#ifdefHAVE_NETINET_TCP_H
8282
#include<netinet/tcp.h>
8383
#endif
84-
#include<arpa/inet.h>
8584
#ifdefHAVE_UTIME_H
8685
#include<utime.h>
8786
#endif
@@ -92,6 +91,7 @@
9291
#include"common/ip.h"
9392
#include"libpq/libpq.h"
9493
#include"miscadmin.h"
94+
#include"port/pg_bswap.h"
9595
#include"storage/ipc.h"
9696
#include"utils/guc.h"
9797
#include"utils/memutils.h"
@@ -1286,7 +1286,7 @@ pq_getmessage(StringInfo s, int maxlen)
12861286
returnEOF;
12871287
}
12881288

1289-
len=ntohl(len);
1289+
len=pg_ntoh32(len);
12901290

12911291
if (len<4||
12921292
(maxlen>0&&len>maxlen))
@@ -1569,7 +1569,7 @@ socket_putmessage(char msgtype, const char *s, size_t len)
15691569
{
15701570
uint32n32;
15711571

1572-
n32=htonl((uint32) (len+4));
1572+
n32=pg_hton32((uint32) (len+4));
15731573
if (internal_putbytes((char*)&n32,4))
15741574
gotofail;
15751575
}

‎src/backend/libpq/pqformat.c

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,11 @@
7272
#include"postgres.h"
7373

7474
#include<sys/param.h>
75-
#include<netinet/in.h>
76-
#include<arpa/inet.h>
7775

7876
#include"libpq/libpq.h"
7977
#include"libpq/pqformat.h"
8078
#include"mb/pg_wchar.h"
79+
#include"port/pg_bswap.h"
8180

8281

8382
/* --------------------------------
@@ -246,11 +245,11 @@ pq_sendint(StringInfo buf, int i, int b)
246245
appendBinaryStringInfo(buf, (char*)&n8,1);
247246
break;
248247
case2:
249-
n16=htons((uint16)i);
248+
n16=pg_hton16((uint16)i);
250249
appendBinaryStringInfo(buf, (char*)&n16,2);
251250
break;
252251
case4:
253-
n32=htonl((uint32)i);
252+
n32=pg_hton32((uint32)i);
254253
appendBinaryStringInfo(buf, (char*)&n32,4);
255254
break;
256255
default:
@@ -270,17 +269,9 @@ pq_sendint(StringInfo buf, int i, int b)
270269
void
271270
pq_sendint64(StringInfobuf,int64i)
272271
{
273-
uint32n32;
274-
275-
/* High order half first, since we're doing MSB-first */
276-
n32= (uint32) (i >>32);
277-
n32=htonl(n32);
278-
appendBinaryStringInfo(buf, (char*)&n32,4);
272+
uint64n64=pg_hton64(i);
279273

280-
/* Now the low order half */
281-
n32= (uint32)i;
282-
n32=htonl(n32);
283-
appendBinaryStringInfo(buf, (char*)&n32,4);
274+
appendBinaryStringInfo(buf, (char*)&n64,sizeof(n64));
284275
}
285276

286277
/* --------------------------------
@@ -304,7 +295,7 @@ pq_sendfloat4(StringInfo buf, float4 f)
304295
}swap;
305296

306297
swap.f=f;
307-
swap.i=htonl(swap.i);
298+
swap.i=pg_hton32(swap.i);
308299

309300
appendBinaryStringInfo(buf, (char*)&swap.i,4);
310301
}
@@ -460,11 +451,11 @@ pq_getmsgint(StringInfo msg, int b)
460451
break;
461452
case2:
462453
pq_copymsgbytes(msg, (char*)&n16,2);
463-
result=ntohs(n16);
454+
result=pg_ntoh16(n16);
464455
break;
465456
case4:
466457
pq_copymsgbytes(msg, (char*)&n32,4);
467-
result=ntohl(n32);
458+
result=pg_ntoh32(n32);
468459
break;
469460
default:
470461
elog(ERROR,"unsupported integer size %d",b);
@@ -485,20 +476,11 @@ pq_getmsgint(StringInfo msg, int b)
485476
int64
486477
pq_getmsgint64(StringInfomsg)
487478
{
488-
int64result;
489-
uint32h32;
490-
uint32l32;
479+
uint64n64;
491480

492-
pq_copymsgbytes(msg, (char*)&h32,4);
493-
pq_copymsgbytes(msg, (char*)&l32,4);
494-
h32=ntohl(h32);
495-
l32=ntohl(l32);
481+
pq_copymsgbytes(msg, (char*)&n64,sizeof(n64));
496482

497-
result=h32;
498-
result <<=32;
499-
result |=l32;
500-
501-
returnresult;
483+
returnpg_ntoh64(n64);
502484
}
503485

504486
/* --------------------------------

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp