diff options
author | Hans Nilsson <[email protected]> | 2018-04-20 11:51:05 +0200 |
---|---|---|
committer | Hans Nilsson <[email protected]> | 2018-04-20 11:51:05 +0200 |
commit | 0343b68b053ee97b2ef70ea07b9aa87e8842c591 (patch) | |
tree | 27e2f211cb8082896699873dce95fa71f2c3f8b7 /lib/erl_interface/src/connect/ei_connect.c | |
parent | f15f615f9185077b02c011ac28257e8365c01e75 (diff) | |
parent | f2c1d537dc28ffbde5d42aedec70bf4c6574c3ea (diff) | |
download | otp-0343b68b053ee97b2ef70ea07b9aa87e8842c591.tar.gz otp-0343b68b053ee97b2ef70ea07b9aa87e8842c591.tar.bz2 otp-0343b68b053ee97b2ef70ea07b9aa87e8842c591.zip |
Merge branch 'maint-20' into maint
* maint-20:
Updated OTP version
Prepare release
erl_interface: Optimize latin1_to_utf8 and friend
inets: Fix broken httpc options handling
erl_interface: Fix ei_connect
ssh: Fix server crashes for exit-normal signals
ssh: Fix ssh_sftpd:handle_op not returning State
ic: Optimize oe_ei_encode_atom
Diffstat (limited to 'lib/erl_interface/src/connect/ei_connect.c')
-rw-r--r-- | lib/erl_interface/src/connect/ei_connect.c | 63 |
1 files changed, 60 insertions, 3 deletions
diff --git a/lib/erl_interface/src/connect/ei_connect.c b/lib/erl_interface/src/connect/ei_connect.c index ea9ecb31d5..5c01223e3d 100644 --- a/lib/erl_interface/src/connect/ei_connect.c +++ b/lib/erl_interface/src/connect/ei_connect.c @@ -583,6 +583,54 @@ static int cnct(uint16 port, struct in_addr *ip_addr, int addr_len, unsigned ms) return s; } /* cnct */ + +/* + * Same as ei_gethostbyname_r, but also handles ERANGE error + * and may allocate larger buffer with malloc. + */ +static +struct hostent *dyn_gethostbyname_r(const char *name, + struct hostent *hostp, + char **buffer_p, + int buflen, + int *h_errnop) +{ + char* buf = *buffer_p; + struct hostent *hp; + + while (1) { + hp = ei_gethostbyname_r(name, hostp, buf, buflen, h_errnop); + if (hp) { + *buffer_p = buf; + break; + } + + if (*h_errnop != ERANGE) { + if (buf != *buffer_p) + free(buf); + break; + } + + buflen *= 2; + if (buf == *buffer_p) + buf = malloc(buflen); + else { + char* buf2 = realloc(buf, buflen); + if (buf2) + buf = buf2; + else { + free(buf); + buf = NULL; + } + } + if (!buf) { + *h_errnop = ENOMEM; + break; + } + } + return hp; +} + /* * Set up a connection to a given Node, and * interchange hand shake messages with it. @@ -597,8 +645,10 @@ int ei_connect_tmo(ei_cnode* ec, char *nodename, unsigned ms) /* these are needed for the call to gethostbyname_r */ struct hostent host; char buffer[1024]; + char *buf = buffer; int ei_h_errno; #endif /* !win32 */ + int res; /* extract the host and alive parts from nodename */ if (!(hostname = strchr(nodename,'@'))) { @@ -611,7 +661,7 @@ int ei_connect_tmo(ei_cnode* ec, char *nodename, unsigned ms) } #ifndef __WIN32__ - hp = ei_gethostbyname_r(hostname,&host,buffer,1024,&ei_h_errno); + hp = dyn_gethostbyname_r(hostname,&host,&buf,sizeof(buffer),&ei_h_errno); if (hp == NULL) { char thishostname[EI_MAXHOSTNAMELEN+1]; /* gethostname requies len to be max(hostname) + 1*/ @@ -627,7 +677,7 @@ int ei_connect_tmo(ei_cnode* ec, char *nodename, unsigned ms) } if (strcmp(hostname,thishostname) == 0) /* Both nodes on same standalone host, use loopback */ - hp = ei_gethostbyname_r("localhost",&host,buffer,1024,&ei_h_errno); + hp = dyn_gethostbyname_r("localhost",&host,&buf,sizeof(buffer),&ei_h_errno); if (hp == NULL) { EI_TRACE_ERR2("ei_connect", "Can't find host for %s: %d\n",nodename,ei_h_errno); @@ -663,7 +713,14 @@ int ei_connect_tmo(ei_cnode* ec, char *nodename, unsigned ms) } } #endif /* win32 */ - return ei_xconnect_tmo(ec, (Erl_IpAddr) *hp->h_addr_list, alivename, ms); + + res = ei_xconnect_tmo(ec, (Erl_IpAddr) *hp->h_addr_list, alivename, ms); + +#ifndef __WIN32__ + if (buf != buffer) + free(buf); +#endif + return res; } /* ei_connect */ int ei_connect(ei_cnode* ec, char *nodename) |