diff options
author | Steve Huston <shuston@riverace.com> | 2001-08-24 14:11:50 +0000 |
---|---|---|
committer | Steve Huston <shuston@riverace.com> | 2001-08-24 14:11:50 +0000 |
commit | 930a565a52c0c09165ad00a5a8c4d60153e6af78 (patch) | |
tree | bb62cceecdf1292ebba7e53a1cb5475f1d3248e9 | |
parent | f8714515267da473663c4b97ebf9ec6bcc030fbf (diff) | |
download | ATCD-930a565a52c0c09165ad00a5a8c4d60153e6af78.tar.gz |
ChangeLogTag:Fri Aug 24 10:03:29 2001 Steve Huston <shuston@riverace.com>
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | ChangeLogs/ChangeLog-02a | 9 | ||||
-rw-r--r-- | ChangeLogs/ChangeLog-03a | 9 | ||||
-rw-r--r-- | ace/INET_Addr.cpp | 151 | ||||
-rw-r--r-- | ace/INET_Addr.h | 3 |
5 files changed, 128 insertions, 53 deletions
diff --git a/ChangeLog b/ChangeLog index 88fada9ec2a..e7dd81c005a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Fri Aug 24 10:03:29 2001 Steve Huston <shuston@riverace.com> + + * ace/INET_Addr.(h cpp): Applied changes submitted by + Alain Decamps <Alain.Decamps@PIDPA.be> to insure that + get_host_name (char hostname[], size_t len) const always + nul-terminates the hostname string. If the host lookup is + successful, but the supplied buffer is too short, the function + returns -1, errno == ENOSPC. + Thu Aug 23 22:01:11 2001 Balachandran Natarajan <bala@cs.wustl.edu> * tests/Logging_Strategy_Test.cpp: Reverted this change "Thu Aug diff --git a/ChangeLogs/ChangeLog-02a b/ChangeLogs/ChangeLog-02a index 88fada9ec2a..e7dd81c005a 100644 --- a/ChangeLogs/ChangeLog-02a +++ b/ChangeLogs/ChangeLog-02a @@ -1,3 +1,12 @@ +Fri Aug 24 10:03:29 2001 Steve Huston <shuston@riverace.com> + + * ace/INET_Addr.(h cpp): Applied changes submitted by + Alain Decamps <Alain.Decamps@PIDPA.be> to insure that + get_host_name (char hostname[], size_t len) const always + nul-terminates the hostname string. If the host lookup is + successful, but the supplied buffer is too short, the function + returns -1, errno == ENOSPC. + Thu Aug 23 22:01:11 2001 Balachandran Natarajan <bala@cs.wustl.edu> * tests/Logging_Strategy_Test.cpp: Reverted this change "Thu Aug diff --git a/ChangeLogs/ChangeLog-03a b/ChangeLogs/ChangeLog-03a index 88fada9ec2a..e7dd81c005a 100644 --- a/ChangeLogs/ChangeLog-03a +++ b/ChangeLogs/ChangeLog-03a @@ -1,3 +1,12 @@ +Fri Aug 24 10:03:29 2001 Steve Huston <shuston@riverace.com> + + * ace/INET_Addr.(h cpp): Applied changes submitted by + Alain Decamps <Alain.Decamps@PIDPA.be> to insure that + get_host_name (char hostname[], size_t len) const always + nul-terminates the hostname string. If the host lookup is + successful, but the supplied buffer is too short, the function + returns -1, errno == ENOSPC. + Thu Aug 23 22:01:11 2001 Balachandran Natarajan <bala@cs.wustl.edu> * tests/Logging_Strategy_Test.cpp: Reverted this change "Thu Aug diff --git a/ace/INET_Addr.cpp b/ace/INET_Addr.cpp index ede242401c6..3cf3137344a 100644 --- a/ace/INET_Addr.cpp +++ b/ace/INET_Addr.cpp @@ -497,6 +497,92 @@ ACE_INET_Addr::get_host_name (char hostname[], { ACE_TRACE ("ACE_INET_Addr::get_host_name"); + int result; + if (len > 1) + { + result = get_host_name_i(hostname,len); + if (result < 0) + { + if (result == -2) + { + result = -1; + // We know that hostname is nul-terminated + } + else + { + //result == -1; + // This could be worse than hostname[len -1] = '\0'? + hostname[0] = '\0'; + } + } + } + else + { + if (len == 1) + { + hostname[0] = '\0'; + } + result = -1; + } + return result; +} + +#if defined (ACE_HAS_WCHAR) +int +ACE_INET_Addr::get_host_name (wchar_t hostname[], + size_t len) const +{ + ACE_TRACE ("ACE_INET_Addr::get_host_name"); + + char char_hostname [MAXHOSTNAMELEN + 1]; + + // We have a build in limitation of MAXHOSTNAMELEN + if (len > MAXHOSTNAMELEN + 1) + len = MAXHOSTNAMELEN + 1; + + // Call the char version + int result = this->get_host_name (char_hostname, len); + + // And copy it over, if successful + if (result == 0) + ACE_OS_String::strcpy (hostname, + ACE_Ascii_To_Wide (char_hostname).wchar_rep ()); + + return result; +} +#endif /* ACE_HAS_WCHAR */ + +// Return the character representation of the hostname. + +const char * +ACE_INET_Addr::get_host_name (void) const +{ + ACE_TRACE ("ACE_INET_Addr::get_host_name"); + + static char name[MAXHOSTNAMELEN + 1]; + if (this->get_host_name (name, MAXHOSTNAMELEN + 1) == -1) + ACE_OS::strcpy (name, "<unknown>"); + return name; +} + +void +ACE_INET_Addr::set_port_number (u_short port_number, + int encode) +{ + ACE_TRACE ("ACE_INET_Addr::set_port_number"); + + if (encode) + port_number = htons (port_number); + + this->inet_addr_.sin_port = port_number; +} + +// returns -2 when the hostname is truncated +int +ACE_INET_Addr::get_host_name_i (char hostname[], size_t len) const +{ + ACE_TRACE ("ACE_INET_Addr::get_host_name_i"); + if (this->inet_addr_.sin_addr.s_addr == INADDR_ANY) { if (ACE_OS::hostname (hostname, len) == -1) @@ -527,7 +613,8 @@ ACE_INET_Addr::get_host_name (char hostname[], int error = 0; #if defined (CHORUS) || (defined (DIGITAL_UNIX) && defined (__GNUC__)) - hostent *hp = ACE_OS::gethostbyaddr ((char *) &this->inet_addr_.sin_addr, + hostent *hp = ACE_OS::gethostbyaddr ((char *) +&this->inet_addr_.sin_addr, a_len, this->addr_type_); if (hp == 0) @@ -555,9 +642,17 @@ ACE_INET_Addr::get_host_name (char hostname[], return -1; if (ACE_OS::strlen (hp->h_name) >= len) - { + { + // We know the length, so use memcpy + if (len > 0) + { + ACE_OS::memcpy(hostname,hp->h_name,len - 1); + hostname[len-1]= '\0'; + } errno = ENOSPC; - return -1; + return -2; // -2 Means that we have a good string + // Using errno looks ok, but ENOSPC could be set on + // other places. } ACE_OS::strcpy (hostname, hp->h_name); @@ -565,53 +660,3 @@ ACE_INET_Addr::get_host_name (char hostname[], #endif /* VXWORKS */ } } - -#if defined (ACE_HAS_WCHAR) -int -ACE_INET_Addr::get_host_name (wchar_t hostname[], - size_t len) const -{ - ACE_TRACE ("ACE_INET_Addr::get_host_name"); - - char char_hostname [MAXHOSTNAMELEN + 1]; - - // We have a build in limitation of MAXHOSTNAMELEN - if (len > MAXHOSTNAMELEN + 1) - len = MAXHOSTNAMELEN + 1; - - // Call the char version - int result = this->get_host_name (char_hostname, len); - - // And copy it over, if successful - if (result == 0) - ACE_OS_String::strcpy (hostname, - ACE_Ascii_To_Wide (char_hostname).wchar_rep ()); - - return result; -} -#endif /* ACE_HAS_WCHAR */ - -// Return the character representation of the hostname. - -const char * -ACE_INET_Addr::get_host_name (void) const -{ - ACE_TRACE ("ACE_INET_Addr::get_host_name"); - - static char name[MAXHOSTNAMELEN + 1]; - if (this->get_host_name (name, MAXHOSTNAMELEN + 1) == -1) - ACE_OS::strcpy (name, "<unknown>"); - return name; -} - -void -ACE_INET_Addr::set_port_number (u_short port_number, - int encode) -{ - ACE_TRACE ("ACE_INET_Addr::set_port_number"); - - if (encode) - port_number = htons (port_number); - - this->inet_addr_.sin_port = port_number; -} diff --git a/ace/INET_Addr.h b/ace/INET_Addr.h index 7facacf8548..9d362ec75d4 100644 --- a/ace/INET_Addr.h +++ b/ace/INET_Addr.h @@ -276,6 +276,9 @@ public: ACE_ALLOC_HOOK_DECLARE; private: + /// Insure that @arg hostname is properly null-terminated. + int get_host_name_i (char hostname[], size_t hostnamelen) const; + /// Underlying representation. sockaddr_in inet_addr_; |