Skip to content

Commit 2ad1506

Browse files
committed
Fix GetDomainFromHostName to get the domain name correctly
1 parent 6c78bff commit 2ad1506

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

src/platform/Darwin/DnssdImpl.cpp

+6-16
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,14 @@ std::string GetDomainFromHostName(const char * hostnameWithDomain)
148148
{
149149
std::string hostname = std::string(hostnameWithDomain);
150150

151-
// Find the last occurence of '.'
152-
size_t last_pos = hostname.find_last_of(".");
151+
// Find the first occurence of '.'
152+
size_t first_pos = hostname.find(".");
153153

154-
// The last occurence of the dot should be the last character in the hostname with domain.
155-
VerifyOrReturnValue((last_pos != std::string::npos && (last_pos == strlen(hostnameWithDomain) - 1)), std::string());
154+
// if not found, return empty string
155+
VerifyOrReturnValue(first_pos != std::string::npos, std::string());
156156

157-
// Get a substring without last '.'
158-
std::string substring = hostname.substr(0, last_pos);
159-
160-
// Find the last occurence of '.' in the substring created above.
161-
size_t pos = substring.find_last_of(".");
162-
if (pos != std::string::npos)
163-
{
164-
// Return the domain name between the last 2 occurences of '.' including the trailing dot'.'.
165-
return std::string(hostname.substr(pos + 1, last_pos));
166-
}
167-
168-
return std::string();
157+
// Get a substring after the first occurence of '.' to the end of the string
158+
return hostname.substr(first_pos + 1, hostname.size());
169159
}
170160

171161
/**

0 commit comments

Comments
 (0)