namespace Msp {
namespace Net {
-SockAddr *resolve(const string &s, Family family)
+SockAddr *resolve(const string &host, const string &serv, Family family)
{
- string host, serv;
- if(s[0]=='[')
- {
- unsigned bracket = s.find(']');
- host = s.substr(1, bracket-1);
- unsigned colon = s.find(':', bracket);
- if(colon!=string::npos)
- serv = s.substr(colon+1);
- }
- else
- {
- unsigned colon = s.find(':');
- if(colon!=string::npos)
- {
- host = s.substr(0, colon);
- serv = s.substr(colon+1);
- }
- else
- host = s;
- }
-
+ const char *chost = (host.empty() ? 0 : host.c_str());
+ const char *cserv = (serv.empty() ? 0 : serv.c_str());
unsigned flags = 0;
if(host=="*")
{
flags = AI_PASSIVE;
- host = string();
+ chost = 0;
}
addrinfo hints = { flags, family_to_sys(family), 0, 0, 0, 0, 0, 0 };
addrinfo *res;
- const char *chost = (host.empty() ? 0 : host.c_str());
- const char *cserv = (serv.empty() ? 0 : serv.c_str());
+
int err = getaddrinfo(chost, cserv, &hints, &res);
if(err==0)
{
#endif
}
+SockAddr *resolve(const string &str, Family family)
+{
+ string host, serv;
+ if(str[0]=='[')
+ {
+ unsigned bracket = str.find(']');
+ host = str.substr(1, bracket-1);
+ unsigned colon = str.find(':', bracket);
+ if(colon!=string::npos)
+ serv = str.substr(colon+1);
+ }
+ else
+ {
+ unsigned colon = str.find(':');
+ if(colon!=string::npos)
+ {
+ host = str.substr(0, colon);
+ serv = str.substr(colon+1);
+ }
+ else
+ host = str;
+ }
+
+ return resolve(host, serv, family);
+}
+
/*sockaddr sa;
unsigned size = fill_sockaddr(sa);
char hst[128];
class SockAddr;
+/** Resolves host and service names into a socket address. If host is empty,
+the loopback address will be used. If host is "*", the wildcard address will
+be used. If service is empty, a socket address with a null service will be
+returned. With the IP families, these are not very useful. */
+SockAddr *resolve(const std::string &, const std::string &, Family = UNSPEC);
+
+/** And overload of resolve() that takes host and service as a single string,
+separated by a colon. If the host part contains colons, such as is the case
+with a numeric IPv6 address, it must be enclosed in brackets. */
SockAddr *resolve(const std::string &, Family = UNSPEC);
} // namespace Net