@@ -143,15 +143,14 @@ serialize_plan(Plan *plan, const char *sourceText, ParamListInfo params)
143
143
rlen1 = pg_b64_encode (sparams ,sparams_len ,params_container );
144
144
Assert (rlen >=rlen1 );
145
145
146
- GetMyServerName ( & host , & port );
146
+ host = GetMyServerName ( & port );
147
147
serverName = serializeServer (host ,port );
148
148
query = palloc0 (qlen + plen + rlen + strlen (serverName )+ 100 );
149
149
sprintf (query ,"SELECT public.pg_exec_plan('%s', '%s', '%s', '%s');" ,
150
150
query_container ,plan_container ,params_container ,
151
151
serverName );
152
152
153
153
pfree (serverName );
154
- pfree (host );
155
154
pfree (query_container );
156
155
pfree (plan_container );
157
156
pfree (sparams );
@@ -239,7 +238,7 @@ EstablishDMQConnections(const lcontext *context, const char *serverName,
239
238
char * host ;
240
239
int port ;
241
240
242
- GetMyServerName ( & host , & port );
241
+ host = GetMyServerName ( & port );
243
242
sprintf (senderName ,"%s-%d" ,host ,port );
244
243
FSExtractServerName ((Oid )sid ,& host ,& port );
245
244
sprintf (receiverName ,"%s-%d" ,host ,port );
@@ -689,10 +688,40 @@ localize_plan(Plan *node, lcontext *context)
689
688
return false;
690
689
}
691
690
691
+
692
+ #include <unistd.h>
693
+ #include <sys/types.h>
694
+ #include <sys/socket.h>
695
+ #include <sys/ioctl.h>
696
+ #include <netinet/in.h>
697
+ #include <net/if.h>
698
+ #include <arpa/inet.h>
699
+ #include "common/ip.h"
700
+
692
701
const char * LOCALHOST = "localhost" ;
702
+
703
+ static char *
704
+ get_hostname (const char * sipaddr )
705
+ {
706
+ char * hostname ;
707
+ struct addrinfo hintp ;
708
+ struct addrinfo * result ;
709
+
710
+ struct sockaddr_storage saddr ;
711
+ int res ;
712
+
713
+ if ((res = pg_getaddrinfo_all (sipaddr ,NULL ,& hintp ,& result ))!= 0 )
714
+ elog (FATAL ,"Cannot resolve network address %s, error=%d." ,sipaddr ,res );
715
+ memcpy (& saddr ,result -> ai_addr ,result -> ai_addrlen );
716
+ hostname = (char * )palloc0 (NI_MAXHOST );
717
+ if (pg_getnameinfo_all (& saddr ,result -> ai_addrlen ,hostname ,NI_MAXHOST ,
718
+ NULL ,0 ,0 )!= 0 )
719
+ elog (FATAL ,"Cannot resolve network name" );
720
+ return hostname ;
721
+ }
693
722
/*
694
723
* fsid - foreign server oid.
695
- * host - returns C-stringwith foreign server host name
724
+ * host - returns C-stringcontained foreign server host name
696
725
* port - returns foreign server port number.
697
726
*/
698
727
void
@@ -709,59 +738,31 @@ FSExtractServerName(Oid fsid, char **host, int *port)
709
738
DefElem * def = (DefElem * )lfirst (lc );
710
739
711
740
if (strcmp (def -> defname ,"host" )== 0 )
712
- hostname = pstrdup ( defGetString (def ) );
741
+ hostname = defGetString (def );
713
742
else if (strcmp (def -> defname ,"port" )== 0 )
714
743
* port = strtol (defGetString (def ),NULL ,10 );
715
744
}
716
745
717
746
if (!hostname )
718
- hostname = pstrdup (LOCALHOST );
747
+ hostname = GetMyServerName (NULL );
748
+ else
749
+ {
750
+ hostname = get_hostname (hostname );
751
+ /* Convert foreign server address to network host name. */
752
+ }
719
753
* host = hostname ;
720
754
}
721
755
722
- #include <unistd.h>
723
- #include <sys/types.h>
724
- #include <sys/socket.h>
725
- #include <sys/ioctl.h>
726
- #include <netinet/in.h>
727
- #include <net/if.h>
728
- #include <arpa/inet.h>
729
- #include "common/ip.h"
730
-
731
- void
732
- GetMyServerName (char * * host ,int * port )
756
+ char *
757
+ GetMyServerName (int * port )
733
758
{
734
- int fd ;
735
- struct ifreq ifr ;
736
- struct addrinfo hintp ;
737
- struct addrinfo * result ;
738
- char * sipaddr ;
739
- struct sockaddr_storage saddr ;
740
- int res ;
741
-
742
- fd = socket (AF_INET ,SOCK_DGRAM ,0 );
743
-
744
- /* I want to get an IPv4 IP address */
745
- ifr .ifr_addr .sa_family = AF_INET ;
746
-
747
- /* I want IP address attached to "eth0" */
748
- strncpy (ifr .ifr_name ,network_interface ,IFNAMSIZ - 1 );
749
- ioctl (fd ,SIOCGIFADDR ,& ifr );
750
- close (fd );
751
-
752
- MemSet (& hintp ,0 ,sizeof (hintp ));
753
- hintp .ai_family = AF_INET ;
754
- hintp .ai_flags = AI_ALL ;
755
- sipaddr = inet_ntoa (((struct sockaddr_in * )& ifr .ifr_addr )-> sin_addr );
756
- if ((res = pg_getaddrinfo_all (sipaddr ,NULL ,& hintp ,& result ))!= 0 )
757
- elog (FATAL ,"Cannot resolve network address %s, error=%d." ,sipaddr ,res );
758
- memcpy (& saddr ,result -> ai_addr ,result -> ai_addrlen );
759
- * host = (char * )palloc0 (NI_MAXHOST );
760
- if (pg_getnameinfo_all (& saddr ,result -> ai_addrlen ,* host ,NI_MAXHOST ,
761
- NULL ,0 ,0 )!= 0 )
762
- elog (FATAL ,"Cannot resolve network name" );
763
-
764
- * port = PostPortNumber ;
759
+ char * host = (char * )palloc0 (HOST_NAME_MAX + 1 );
760
+
761
+ if (gethostname (host ,HOST_NAME_MAX )!= 0 )
762
+ elog (FATAL ,"An error on resolving local hostname was thrown" );
763
+ if (port != NULL )
764
+ * port = PostPortNumber ;
765
+ return host ;
765
766
}
766
767
767
768
char *