Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACE_UNIX_Addr supports abstract path in Linux. #870

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions ACE/ace/UNIX_Addr.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ace/UNIX_Addr.h"
#include "ace/Min_Max.h"

#if !defined (ACE_LACKS_UNIX_DOMAIN_SOCKETS)

Expand Down Expand Up @@ -40,9 +41,17 @@ ACE_UNIX_Addr::string_to_addr (const char addr[])
ACE_OS::strsncpy (this->unix_addr_.sun_path, addr,
sizeof this->unix_addr_.sun_path);

size_t const len = ACE_OS::strlen (this->unix_addr_.sun_path);
#if defined (ACE_LINUX)
if (*this->unix_addr_.sun_path == '@') // abstract path
{
*this->unix_addr_.sun_path = 0;
}
#endif /* ACE_LINUX */

this->set_size (sizeof this->unix_addr_ -
sizeof (this->unix_addr_.sun_path) +
ACE_OS::strlen (this->unix_addr_.sun_path));
len);
return 0;
}

Expand All @@ -51,9 +60,23 @@ ACE_UNIX_Addr::string_to_addr (const char addr[])
int
ACE_UNIX_Addr::addr_to_string (ACE_TCHAR s[], size_t len) const
{
ACE_OS::strsncpy (s,
ACE_TEXT_CHAR_TO_TCHAR (this->unix_addr_.sun_path),
len);
if (!s || !len)
return -1;

size_t i = 0;
#if defined (ACE_LINUX)
if (!*this->unix_addr_.sun_path && this->unix_addr_.sun_path[1])
{
if (len == 1)
return -1;

s[0] = '@';
i = 1;
}
#endif /* ACE_LINUX */
ACE_OS::strsncpy (s + i,
ACE_TEXT_CHAR_TO_TCHAR (this->unix_addr_.sun_path + i),
len - i);
return 0;
}

Expand Down Expand Up @@ -114,7 +137,17 @@ ACE_UNIX_Addr::set (const sockaddr_un *un, int len)
(void) ACE_OS::memset ((void *) &this->unix_addr_, 0,
sizeof this->unix_addr_);
this->unix_addr_.sun_family = AF_UNIX;
#if defined (ACE_LINUX)
int const n = ACE_MIN (len - int (sizeof this->unix_addr_ -
sizeof (this->unix_addr_.sun_path)),
int (sizeof (this->unix_addr_.sun_path)));
if (n > 0)
{
memcpy (this->unix_addr_.sun_path, un->sun_path, n);
}
#else
ACE_OS::strcpy (this->unix_addr_.sun_path, un->sun_path);
#endif /* ACE_LINUX */
this->base_set (AF_UNIX, len);
return 0;
}
Expand All @@ -135,10 +168,18 @@ ACE_UNIX_Addr::set (const char rendezvous_point[])
rendezvous_point,
sizeof this->unix_addr_.sun_path);

size_t const len = ACE_OS::strlen (this->unix_addr_.sun_path);
#if defined (ACE_LINUX)
if (*this->unix_addr_.sun_path == '@') // abstract path
{
*this->unix_addr_.sun_path = 0;
}
#endif /* ACE_LINUX */

this->ACE_Addr::base_set (AF_UNIX,
sizeof this->unix_addr_ -
sizeof (this->unix_addr_.sun_path) +
ACE_OS::strlen (this->unix_addr_.sun_path));
len);
return 0;
}

Expand Down
13 changes: 10 additions & 3 deletions ACE/ace/UNIX_Addr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ ACE_UNIX_Addr::set (const wchar_t rendezvous_point[])
ACE_INLINE bool
ACE_UNIX_Addr::operator == (const ACE_UNIX_Addr &sap) const
{
return ACE_OS::strncmp (this->unix_addr_.sun_path,
sap.unix_addr_.sun_path,
sizeof this->unix_addr_.sun_path) == 0;
size_t i = 0;
#if defined (ACE_LINUX)
if (!*this->unix_addr_.sun_path && !*sap.unix_addr_.sun_path)
{
i = 1;
}
#endif /* ACE_LINUX */
return ACE_OS::strncmp (this->unix_addr_.sun_path + i,
sap.unix_addr_.sun_path + i,
sizeof (this->unix_addr_.sun_path) - 1) == 0;
}

// Compare two addresses for inequality.
Expand Down
42 changes: 36 additions & 6 deletions ACE/tests/UNIX_Addr_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,56 @@ int run_main (int, ACE_TCHAR *[])
addr.set (path);
ACE_TEST_ASSERT (addr.get_size () > origin);
ACE_TEST_ASSERT (addr.addr_to_string (buf, sizeof (buf)) == 0);
ACE_TEST_ASSERT (strcmp(path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
ACE_TEST_ASSERT (strcmp(path, addr.get_path_name ()) == 0);
ACE_TEST_ASSERT (strcmp (path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
ACE_TEST_ASSERT (strcmp (path, addr.get_path_name ()) == 0);

// Set longer path by ACE_UNIX_Addr::string_to_addr
origin = addr.get_size ();
path = "/tmp/unix_addr_test";
addr.string_to_addr (path);
ACE_TEST_ASSERT (addr.get_size () > origin);
ACE_TEST_ASSERT (addr.addr_to_string (buf, sizeof (buf)) == 0);
ACE_TEST_ASSERT (strcmp(path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
ACE_TEST_ASSERT (strcmp(path, addr.get_path_name ()) == 0);
ACE_TEST_ASSERT (strcmp (path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
ACE_TEST_ASSERT (strcmp (path, addr.get_path_name ()) == 0);

// Set shorter path by ACE_UNIX_Addr::string_to_addr
origin = addr.get_size ();
path = "/tmp/test";
addr.string_to_addr (path);
ACE_TEST_ASSERT (addr.get_size () < origin);
ACE_TEST_ASSERT (addr.addr_to_string (buf, sizeof (buf)) == 0);
ACE_TEST_ASSERT (strcmp(path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
ACE_TEST_ASSERT (strcmp(path, addr.get_path_name ()) == 0);
ACE_TEST_ASSERT (strcmp (path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
ACE_TEST_ASSERT (strcmp (path, addr.get_path_name ()) == 0);

// Bounds checking
path = "/tmp/bounds.test";
addr.set (path);
ACE_TEST_ASSERT (addr.addr_to_string (nullptr, sizeof (buf)) == -1);
ACE_TEST_ASSERT (addr.addr_to_string (buf, 0) == -1);

#if defined(ACE_LINUX)
// Bounds checking for abstract path
path = "@/tmp/bounds.test";
addr.set (path);
ACE_TEST_ASSERT (addr.addr_to_string (buf, 1) == -1);

// Set abstract path by set.
path = "@/tmp/ace.test";
addr.set (path);
ACE_TEST_ASSERT (addr.addr_to_string (buf, sizeof (buf)) == 0);
ACE_TEST_ASSERT (strcmp (path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
ACE_TEST_ASSERT (*addr.get_path_name () == '\0');
ACE_TEST_ASSERT (strcmp (path + 1, addr.get_path_name () + 1) == 0);

// Set abstract path by string_to_addr.
path = "@/tmp/unix_addr_test";
addr.string_to_addr (path);
ACE_TEST_ASSERT (addr.addr_to_string (buf, sizeof (buf)) == 0);
ACE_TEST_ASSERT (strcmp (path, ACE_TEXT_ALWAYS_CHAR(buf)) == 0);
ACE_TEST_ASSERT (*addr.get_path_name () == '\0');
ACE_TEST_ASSERT (strcmp (path + 1, addr.get_path_name () + 1) == 0);
#endif // ACE_LINUX

#endif // ! ACE_LACKS_UNIX_DOMAIN_SOCKETS

ACE_END_TEST;
Expand Down
Loading