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

Commitba11258

Browse files
committed
When reporting the server as not responding, if the hostname was
supplied, also print the IP address. This allows IPv4 and IPv6 failuresto be distinguished. Also useful when a hostname resolves to multipleIP addresses.Also, remove use of inet_ntoa() and use our own inet_net_ntop() in allplaces, including in libpq, because it is thread-safe.
1 parent725d52d commitba11258

File tree

9 files changed

+313
-250
lines changed

9 files changed

+313
-250
lines changed

‎doc/src/sgml/libpq.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ PGconn *PQconnectdbParams(const char **keywords, const char **values, int expand
170170
If <literal>host</> is specified without <literal>hostaddr</>,
171171
a host name lookup occurs.
172172
If <literal>hostaddr</> is specified without <literal>host</>,
173-
the value for <literal>hostaddr</> gives the server address.
173+
the value for <literal>hostaddr</> gives the servernetworkaddress.
174174
The connection attempt will fail in any of the cases where a
175175
host name is required.
176176
If both <literal>host</> and <literal>hostaddr</> are specified,
177-
the value for <literal>hostaddr</> gives the server address.
177+
the value for <literal>hostaddr</> gives the servernetworkaddress.
178178
The value for <literal>host</> is ignored unless needed for
179179
authentication or verification purposes, in which case it will be
180180
used as the host name. Note that authentication is likely to fail

‎src/backend/utils/adt/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ OBJS = acl.o arrayfuncs.o array_userfuncs.o arrayutils.o bool.o \
2323
oid.o oracle_compat.o pseudotypes.o rowtypes.o\
2424
regexp.o regproc.o ruleutils.o selfuncs.o\
2525
tid.o timestamp.o varbit.o varchar.o varlena.o version.o xid.o\
26-
network.o mac.oinet_net_ntop.o inet_net_pton.o\
26+
network.o mac.oinet_cidr_ntop.o inet_net_pton.o\
2727
ri_triggers.o pg_lzcompress.o pg_locale.o formatting.o\
2828
ascii.o quote.o pgstatfuncs.o encode.o dbsize.o genfile.o trigfuncs.o\
2929
tsginidx.o tsgistidx.o tsquery.o tsquery_cleanup.o tsquery_gist.o\

‎src/backend/utils/adt/inet_net_ntop.crenamed to‎src/backend/utils/adt/inet_cidr_ntop.c

Lines changed: 0 additions & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,14 @@ static const char rcsid[] = "Id: inet_net_ntop.c,v 1.1.2.2 2004/03/09 09:17:27 m
3232
#include"utils/inet.h"
3333

3434

35-
#defineNS_IN6ADDRSZ 16
36-
#defineNS_INT16SZ 2
37-
3835
#ifdefSPRINTF_CHAR
3936
#defineSPRINTF(x) strlen(sprintf/**/x)
4037
#else
4138
#defineSPRINTF(x) ((size_t)sprintf x)
4239
#endif
4340

44-
staticchar*inet_net_ntop_ipv4(constu_char*src,intbits,
45-
char*dst,size_tsize);
4641
staticchar*inet_cidr_ntop_ipv4(constu_char*src,intbits,
4742
char*dst,size_tsize);
48-
staticchar*inet_net_ntop_ipv6(constu_char*src,intbits,
49-
char*dst,size_tsize);
5043
staticchar*inet_cidr_ntop_ipv6(constu_char*src,intbits,
5144
char*dst,size_tsize);
5245

@@ -300,231 +293,3 @@ inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
300293
errno=EMSGSIZE;
301294
return (NULL);
302295
}
303-
304-
305-
/*
306-
* char *
307-
* inet_net_ntop(af, src, bits, dst, size)
308-
*convert host/network address from network to presentation format.
309-
*"src"'s size is determined from its "af".
310-
* return:
311-
*pointer to dst, or NULL if an error occurred (check errno).
312-
* note:
313-
*192.5.5.1/28 has a nonzero host part, which means it isn't a network
314-
*as called for by inet_net_pton() but it can be a host address with
315-
*an included netmask.
316-
* author:
317-
*Paul Vixie (ISC), October 1998
318-
*/
319-
char*
320-
inet_net_ntop(intaf,constvoid*src,intbits,char*dst,size_tsize)
321-
{
322-
switch (af)
323-
{
324-
casePGSQL_AF_INET:
325-
return (inet_net_ntop_ipv4(src,bits,dst,size));
326-
casePGSQL_AF_INET6:
327-
return (inet_net_ntop_ipv6(src,bits,dst,size));
328-
default:
329-
errno=EAFNOSUPPORT;
330-
return (NULL);
331-
}
332-
}
333-
334-
/*
335-
* static char *
336-
* inet_net_ntop_ipv4(src, bits, dst, size)
337-
*convert IPv4 network address from network to presentation format.
338-
*"src"'s size is determined from its "af".
339-
* return:
340-
*pointer to dst, or NULL if an error occurred (check errno).
341-
* note:
342-
*network byte order assumed. this means 192.5.5.240/28 has
343-
*0b11110000 in its fourth octet.
344-
* author:
345-
*Paul Vixie (ISC), October 1998
346-
*/
347-
staticchar*
348-
inet_net_ntop_ipv4(constu_char*src,intbits,char*dst,size_tsize)
349-
{
350-
char*odst=dst;
351-
char*t;
352-
intlen=4;
353-
intb;
354-
355-
if (bits<0||bits>32)
356-
{
357-
errno=EINVAL;
358-
return (NULL);
359-
}
360-
361-
/* Always format all four octets, regardless of mask length. */
362-
for (b=len;b>0;b--)
363-
{
364-
if (size <=sizeof".255")
365-
gotoemsgsize;
366-
t=dst;
367-
if (dst!=odst)
368-
*dst++='.';
369-
dst+=SPRINTF((dst,"%u",*src++));
370-
size-= (size_t) (dst-t);
371-
}
372-
373-
/* don't print masklen if 32 bits */
374-
if (bits!=32)
375-
{
376-
if (size <=sizeof"/32")
377-
gotoemsgsize;
378-
dst+=SPRINTF((dst,"/%u",bits));
379-
}
380-
381-
return (odst);
382-
383-
emsgsize:
384-
errno=EMSGSIZE;
385-
return (NULL);
386-
}
387-
388-
staticint
389-
decoct(constu_char*src,intbytes,char*dst,size_tsize)
390-
{
391-
char*odst=dst;
392-
char*t;
393-
intb;
394-
395-
for (b=1;b <=bytes;b++)
396-
{
397-
if (size <=sizeof"255.")
398-
return (0);
399-
t=dst;
400-
dst+=SPRINTF((dst,"%u",*src++));
401-
if (b!=bytes)
402-
{
403-
*dst++='.';
404-
*dst='\0';
405-
}
406-
size-= (size_t) (dst-t);
407-
}
408-
return (dst-odst);
409-
}
410-
411-
staticchar*
412-
inet_net_ntop_ipv6(constu_char*src,intbits,char*dst,size_tsize)
413-
{
414-
/*
415-
* Note that int32_t and int16_t need only be "at least" large enough to
416-
* contain a value of the specified size. On some systems, like Crays,
417-
* there is no such thing as an integer variable with 16 bits. Keep this
418-
* in mind if you think this function should have been coded to use
419-
* pointer overlays. All the world's not a VAX.
420-
*/
421-
chartmp[sizeof"ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
422-
char*tp;
423-
struct
424-
{
425-
intbase,
426-
len;
427-
}best,cur;
428-
u_intwords[NS_IN6ADDRSZ /NS_INT16SZ];
429-
inti;
430-
431-
if ((bits<-1)|| (bits>128))
432-
{
433-
errno=EINVAL;
434-
return (NULL);
435-
}
436-
437-
/*
438-
* Preprocess: Copy the input (bytewise) array into a wordwise array. Find
439-
* the longest run of 0x00's in src[] for :: shorthanding.
440-
*/
441-
memset(words,'\0',sizeofwords);
442-
for (i=0;i<NS_IN6ADDRSZ;i++)
443-
words[i /2] |= (src[i] << ((1- (i %2)) <<3));
444-
best.base=-1;
445-
cur.base=-1;
446-
best.len=0;
447-
cur.len=0;
448-
for (i=0;i< (NS_IN6ADDRSZ /NS_INT16SZ);i++)
449-
{
450-
if (words[i]==0)
451-
{
452-
if (cur.base==-1)
453-
cur.base=i,cur.len=1;
454-
else
455-
cur.len++;
456-
}
457-
else
458-
{
459-
if (cur.base!=-1)
460-
{
461-
if (best.base==-1||cur.len>best.len)
462-
best=cur;
463-
cur.base=-1;
464-
}
465-
}
466-
}
467-
if (cur.base!=-1)
468-
{
469-
if (best.base==-1||cur.len>best.len)
470-
best=cur;
471-
}
472-
if (best.base!=-1&&best.len<2)
473-
best.base=-1;
474-
475-
/*
476-
* Format the result.
477-
*/
478-
tp=tmp;
479-
for (i=0;i< (NS_IN6ADDRSZ /NS_INT16SZ);i++)
480-
{
481-
/* Are we inside the best run of 0x00's? */
482-
if (best.base!=-1&&i >=best.base&&
483-
i< (best.base+best.len))
484-
{
485-
if (i==best.base)
486-
*tp++=':';
487-
continue;
488-
}
489-
/* Are we following an initial run of 0x00s or any real hex? */
490-
if (i!=0)
491-
*tp++=':';
492-
/* Is this address an encapsulated IPv4? */
493-
if (i==6&&best.base==0&& (best.len==6||
494-
(best.len==7&&words[7]!=0x0001)||
495-
(best.len==5&&words[5]==0xffff)))
496-
{
497-
intn;
498-
499-
n=decoct(src+12,4,tp,sizeoftmp- (tp-tmp));
500-
if (n==0)
501-
{
502-
errno=EMSGSIZE;
503-
return (NULL);
504-
}
505-
tp+=strlen(tp);
506-
break;
507-
}
508-
tp+=SPRINTF((tp,"%x",words[i]));
509-
}
510-
511-
/* Was it a trailing run of 0x00's? */
512-
if (best.base!=-1&& (best.base+best.len)==
513-
(NS_IN6ADDRSZ /NS_INT16SZ))
514-
*tp++=':';
515-
*tp='\0';
516-
517-
if (bits!=-1&&bits!=128)
518-
tp+=SPRINTF((tp,"/%u",bits));
519-
520-
/*
521-
* Check for overflow, copy, and we're done.
522-
*/
523-
if ((size_t) (tp-tmp)>size)
524-
{
525-
errno=EMSGSIZE;
526-
return (NULL);
527-
}
528-
strcpy(dst,tmp);
529-
return (dst);
530-
}

‎src/include/port.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,4 +437,8 @@ extern void qsort_arg(void *base, size_t nel, size_t elsize,
437437
/* port/chklocale.c */
438438
externintpg_get_encoding_from_locale(constchar*ctype);
439439

440+
/* port/inet_net_ntop.c */
441+
externchar*inet_net_ntop(intaf,constvoid*src,intbits,
442+
char*dst,size_tsize);
443+
440444
#endif/* PG_PORT_H */

‎src/include/utils/builtins.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,9 +795,7 @@ extern Datum chr (PG_FUNCTION_ARGS);
795795
externDatumrepeat(PG_FUNCTION_ARGS);
796796
externDatumascii(PG_FUNCTION_ARGS);
797797

798-
/* inet_net_ntop.c */
799-
externchar*inet_net_ntop(intaf,constvoid*src,intbits,
800-
char*dst,size_tsize);
798+
/* inet_cidr_ntop.c */
801799
externchar*inet_cidr_ntop(intaf,constvoid*src,intbits,
802800
char*dst,size_tsize);
803801

‎src/interfaces/libpq/fe-connect.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,16 +960,39 @@ connectFailureMessage(PGconn *conn, int errorno)
960960
else
961961
#endif/* HAVE_UNIX_SOCKETS */
962962
{
963+
charhost_addr[NI_MAXHOST];
964+
booldisplay_host_addr;
965+
structsockaddr_in*host_addr_struct= (structsockaddr_in*)
966+
&conn->raddr.addr;
967+
968+
/*
969+
*Optionally display the network address with the hostname.
970+
*This is useful to distinguish between IPv4 and IPv6 connections.
971+
*/
972+
if (conn->pghostaddr!=NULL)
973+
strlcpy(host_addr,conn->pghostaddr,NI_MAXHOST);
974+
elseif (inet_net_ntop(conn->addr_cur->ai_family,&host_addr_struct->sin_addr,
975+
host_addr_struct->sin_family==AF_INET ?32 :128,
976+
host_addr,sizeof(host_addr))==NULL)
977+
strcpy(host_addr,"???");
978+
979+
display_host_addr= !conn->pghostaddr&&
980+
strcmp(conn->pghost,host_addr)!=0;
981+
963982
appendPQExpBuffer(&conn->errorMessage,
964983
libpq_gettext("could not connect to server: %s\n"
965-
"\tIs the server running on host \"%s\"and accepting\n"
984+
"\tIs the server running on host \"%s\"%s%s%sand accepting\n"
966985
"\tTCP/IP connections on port %s?\n"),
967986
SOCK_STRERROR(errorno,sebuf,sizeof(sebuf)),
968987
conn->pghostaddr
969988
?conn->pghostaddr
970989
: (conn->pghost
971990
?conn->pghost
972991
:"???"),
992+
/* display the IP address only if not already output */
993+
display_host_addr ?"(" :"",
994+
display_host_addr ?host_addr :"",
995+
display_host_addr ?") " :"",
973996
conn->pgport);
974997
}
975998
}

‎src/port/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ include $(top_builddir)/src/Makefile.global
3030
overrideCPPFLAGS := -I$(top_builddir)/src/port -DFRONTEND$(CPPFLAGS)
3131
LIBS +=$(PTHREAD_LIBS)
3232

33-
OBJS =$(LIBOBJS) chklocale.o dirmod.o exec.o noblock.o path.o\
33+
OBJS =$(LIBOBJS) chklocale.o dirmod.o exec.oinet_net_ntop.onoblock.o path.o\
3434
pgsleep.o pgstrcasecmp.o qsort.o qsort_arg.o sprompt.o thread.o
3535
ifneq (,$(filter$(PORTNAME),cygwin win32))
3636
OBJS += pipe.o

‎src/port/getaddrinfo.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,14 @@ getnameinfo(const struct sockaddr * sa, int salen,
388388

389389
if (node)
390390
{
391-
intret=-1;
392-
393391
if (sa->sa_family==AF_INET)
394392
{
395-
char*p;
396-
397-
p=inet_ntoa(((structsockaddr_in*)sa)->sin_addr);
398-
ret=snprintf(node,nodelen,"%s",p);
393+
if (inet_net_ntop(AF_INET, ((structsockaddr_in*)sa)->sin_addr,
394+
sa->sa_family==AF_INET ?32 :128,
395+
node,nodelen)==NULL)
396+
returnEAI_MEMORY;
399397
}
400-
if (ret==-1||ret>nodelen)
398+
else
401399
returnEAI_MEMORY;
402400
}
403401

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp