From 1f0aff03c025b7e69718a4393fce19e9685cdacd Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 21 May 2000 21:19:53 +0000
Subject: [PATCH] Remove calls to getprotobyname(), which we now know leaks
 memory on some platforms --- and I also see that it is documented as not
 thread- safe on HPUX and possibly other platforms.  No good reason not to
 just use IPPROTO_TCP constant from <netinet/in.h> instead.

---
 src/backend/libpq/pqcomm.c        | 13 +++----------
 src/interfaces/libpq/fe-connect.c | 15 +++------------
 2 files changed, 6 insertions(+), 22 deletions(-)

diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index 7d2f7eb7d68..aec4f34c7d2 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -29,7 +29,7 @@
  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- *	$Id: pqcomm.c,v 1.90 2000/05/20 13:10:54 ishii Exp $
+ *	$Id: pqcomm.c,v 1.91 2000/05/21 21:19:53 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -360,19 +360,12 @@ StreamConnection(int server_fd, Port *port)
 		return STATUS_ERROR;
 	}
 
-	/* select TCP_NODELAY option if it's a TCP connection */
+	/* select NODELAY and KEEPALIVE options if it's a TCP connection */
 	if (port->laddr.sa.sa_family == AF_INET)
 	{
-		struct protoent *pe;
 		int			on = 1;
 
-		pe = getprotobyname("TCP");
-		if (pe == NULL)
-		{
-			perror("postmaster: StreamConnection: getprotobyname");
-			return STATUS_ERROR;
-		}
-		if (setsockopt(port->sock, pe->p_proto, TCP_NODELAY,
+		if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY,
 					   &on, sizeof(on)) < 0)
 		{
 			perror("postmaster: StreamConnection: setsockopt(TCP_NODELAY)");
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index c400a7ebb16..400bab2f7d5 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.126 2000/04/12 17:17:14 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.127 2000/05/21 21:19:53 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -29,6 +29,7 @@
 #include <sys/socket.h>
 #include <unistd.h>
 #include <netdb.h>
+#include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <arpa/inet.h>
 #endif
@@ -625,19 +626,9 @@ connectMakeNonblocking(PGconn *conn)
 static int
 connectNoDelay(PGconn *conn)
 {
-	struct protoent *pe;
 	int			on = 1;
 
-	pe = getprotobyname("TCP");
-	if (pe == NULL)
-	{
-		printfPQExpBuffer(&conn->errorMessage,
-						  "connectNoDelay() -- "
-						  "getprotobyname failed: errno=%d\n%s\n",
-						  errno, strerror(errno));
-		return 0;
-	}
-	if (setsockopt(conn->sock, pe->p_proto, TCP_NODELAY,
+	if (setsockopt(conn->sock, IPPROTO_TCP, TCP_NODELAY,
 #ifdef WIN32
 				   (char *)
 #endif
-- 
GitLab