Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6bdb7aa

Browse files
committed
libpq can now talk to either 3.0 or 2.0 protocol servers. It first tries
protocol 3, then falls back to 2 if postmaster rejects the startup packet with an old-format error message. A side benefit of the rewrite is that SSL-encrypted connections can now be made without blocking. (I think, anyway, but do not have a good way to test.)
1 parent 152ce7a commit 6bdb7aa

File tree

13 files changed

+3333
-1553
lines changed

13 files changed

+3333
-1553
lines changed

src/backend/libpq/ip.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/libpq/ip.c,v 1.7 2003/04/22 03:52:56 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/libpq/ip.c,v 1.8 2003/06/08 17:42:59 tgl Exp $
1212
*
1313
* This file and the IPV6 implementation were initially provided by
1414
* Nigel Kukard <[email protected]>, Linux Based Systems Design
@@ -72,27 +72,29 @@ getaddrinfo2(const char *hostname, const char *servname,
7272

7373

7474
/*
75-
* freeaddrinfo2 - free IPv6 addrinfo structures
75+
* freeaddrinfo2 - free addrinfo structures for IPv4, IPv6, or Unix
7676
*/
7777
void
78-
freeaddrinfo2(int hint_ai_family, struct addrinfo *ai)
78+
freeaddrinfo2(struct addrinfo *ai)
7979
{
80-
#ifdef HAVE_UNIX_SOCKETS
81-
if (hint_ai_family == AF_UNIX)
80+
if (ai != NULL)
8281
{
83-
struct addrinfo *p;
84-
85-
while (ai != NULL)
82+
#ifdef HAVE_UNIX_SOCKETS
83+
if (ai->ai_family == AF_UNIX)
8684
{
87-
p = ai;
88-
ai = ai->ai_next;
89-
free(p->ai_addr);
90-
free(p);
85+
while (ai != NULL)
86+
{
87+
struct addrinfo *p = ai;
88+
89+
ai = ai->ai_next;
90+
free(p->ai_addr);
91+
free(p);
92+
}
9193
}
92-
}
93-
else
94+
else
9495
#endif /* HAVE_UNIX_SOCKETS */
95-
freeaddrinfo(ai);
96+
freeaddrinfo(ai);
97+
}
9698
}
9799

98100

src/backend/libpq/pqcomm.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
3131
* Portions Copyright (c) 1994, Regents of the University of California
3232
*
33-
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.154 2003/05/29 19:15:34 tgl Exp $
33+
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.155 2003/06/08 17:43:00 tgl Exp $
3434
*
3535
*-------------------------------------------------------------------------
3636
*/
@@ -242,16 +242,15 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
242242
{
243243
elog(LOG, "server socket failure: getaddrinfo2(): %s",
244244
gai_strerror(ret));
245-
if (addrs != NULL)
246-
freeaddrinfo2(hint.ai_family, addrs);
245+
freeaddrinfo2(addrs);
247246
return STATUS_ERROR;
248247
}
249248

250249
if ((fd = socket(family, SOCK_STREAM, 0)) < 0)
251250
{
252251
elog(LOG, "server socket failure: socket(): %s",
253252
strerror(errno));
254-
freeaddrinfo2(hint.ai_family, addrs);
253+
freeaddrinfo2(addrs);
255254
return STATUS_ERROR;
256255
}
257256

@@ -262,7 +261,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
262261
{
263262
elog(LOG, "server socket failure: setsockopt(SO_REUSEADDR): %s",
264263
strerror(errno));
265-
freeaddrinfo2(hint.ai_family, addrs);
264+
freeaddrinfo2(addrs);
266265
return STATUS_ERROR;
267266
}
268267
}
@@ -279,7 +278,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
279278
sock_path);
280279
else
281280
elog(LOG, "\tIf not, wait a few seconds and retry.");
282-
freeaddrinfo2(hint.ai_family, addrs);
281+
freeaddrinfo2(addrs);
283282
return STATUS_ERROR;
284283
}
285284

@@ -288,7 +287,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
288287
{
289288
if (Setup_AF_UNIX() != STATUS_OK)
290289
{
291-
freeaddrinfo2(hint.ai_family, addrs);
290+
freeaddrinfo2(addrs);
292291
return STATUS_ERROR;
293292
}
294293
}
@@ -308,12 +307,12 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
308307
{
309308
elog(LOG, "server socket failure: listen(): %s",
310309
strerror(errno));
311-
freeaddrinfo2(hint.ai_family, addrs);
310+
freeaddrinfo2(addrs);
312311
return STATUS_ERROR;
313312
}
314313

315314
*fdP = fd;
316-
freeaddrinfo2(hint.ai_family, addrs);
315+
freeaddrinfo2(addrs);
317316
return STATUS_OK;
318317

319318
}

src/include/libpq/ip.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 2003, PostgreSQL Global Development Group
77
*
8-
* $Id: ip.h,v 1.3 2003/04/02 00:49:28 tgl Exp $
8+
* $Id: ip.h,v 1.4 2003/06/08 17:43:00 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -19,7 +19,7 @@
1919
extern int getaddrinfo2(const char *hostname, const char *servname,
2020
const struct addrinfo *hintp,
2121
struct addrinfo **result);
22-
extern void freeaddrinfo2(int hint_ai_family, struct addrinfo *ai);
22+
extern void freeaddrinfo2(struct addrinfo *ai);
2323

2424
extern char *SockAddr_ntop(const SockAddr *sa, char *dst, size_t cnt,
2525
int v4conv);

src/interfaces/libpq/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# Copyright (c) 1994, Regents of the University of California
66
#
7-
# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.79 2003/05/10 02:05:50 momjian Exp $
7+
# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.80 2003/06/08 17:43:00 tgl Exp $
88
#
99
#-------------------------------------------------------------------------
1010

@@ -21,7 +21,7 @@ SO_MINOR_VERSION= 1
2121
override CPPFLAGS := -I$(srcdir) $(CPPFLAGS) -DFRONTEND -DSYSCONFDIR='"$(sysconfdir)"'
2222

2323
OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \
24-
pqexpbuffer.o pqsignal.o fe-secure.o \
24+
fe-protocol2.o fe-protocol3.o pqexpbuffer.o pqsignal.o fe-secure.o \
2525
dllist.o md5.o ip.o wchar.o encnames.o \
2626
$(filter crypt.o getaddrinfo.o inet_aton.o snprintf.o strerror.o path.o, $(LIBOBJS))
2727

src/interfaces/libpq/fe-auth.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* exceed INITIAL_EXPBUFFER_SIZE (currently 256 bytes).
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.78 2003/05/16 04:58:03 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.79 2003/06/08 17:43:00 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -559,7 +559,11 @@ pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
559559
default:
560560
return STATUS_ERROR;
561561
}
562-
ret = pqPacketSend(conn, 'p', crypt_pwd, strlen(crypt_pwd) + 1);
562+
/* Packet has a message type as of protocol 3.0 */
563+
if (PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
564+
ret = pqPacketSend(conn, 'p', crypt_pwd, strlen(crypt_pwd) + 1);
565+
else
566+
ret = pqPacketSend(conn, 0, crypt_pwd, strlen(crypt_pwd) + 1);
563567
if (areq == AUTH_REQ_MD5)
564568
free(crypt_pwd);
565569
return ret;

0 commit comments

Comments
 (0)