[Saga-devel] saga SVN commit 3312: /trunk/saga/saga/adaptors/utils/
amerzky at cct.lsu.edu
amerzky at cct.lsu.edu
Sat Jan 17 05:00:54 CST 2009
User: amerzky
Date: 2009/01/17 05:00 AM
Modified:
/trunk/saga/saga/adaptors/utils/
utils.cpp, utils.hpp
Log:
add some simple network helper fuctions.
A
File Changes:
Directory: /trunk/saga/saga/adaptors/utils/
===========================================
File [modified]: utils.cpp
Delta lines: +138 -5
===================================================================
--- trunk/saga/saga/adaptors/utils/utils.cpp 2009-01-17 10:59:24 UTC (rev 3311)
+++ trunk/saga/saga/adaptors/utils/utils.cpp 2009-01-17 11:00:43 UTC (rev 3312)
@@ -4,11 +4,17 @@
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
-#ifndef SAGA_UTILS_HPP
-#define SAGA_UTILS_HPP
+#include <sys/socket.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <sys/utsname.h>
-#include <saga/saga/adaptors/utils/ini/ini.hpp>
-#include <saga/saga/adaptors/utils/process/process.hpp>
+#include <set>
+#include <string>
+#include <vector>
+
+#include <saga/saga/adaptors/utils/utils.hpp>
namespace saga
{
@@ -134,10 +140,137 @@
return out;
}
+ bool host_is_localhost (std::string host)
+ {
+ if ( host == "" )
+ {
+ host = "localhost";
+ }
+ if ( host == saga::adaptors::utils::get_localhost () )
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+
+ // find FQHN for localhost
+ std::string get_localhost (void)
+ {
+ std::vector <std::string> localhosts = saga::adaptors::utils::get_localhosts ();
+
+ // return first hostname which looks like a FQHN
+ for ( int i = 0; i < localhosts.size (); i++ )
+ {
+ if ( std::string::npos != localhosts[i].find (".") )
+ return localhosts[i];
+ }
+
+ // none found
+ return "";
+ }
+
+ // find FQHN of localhost. We return any hostname we find which has
+ // a '.' in its name.
+ std::vector <std::string> get_localhosts (void)
+ {
+ std::set <std::string> initial_names;
+ std::set <std::string> names;
+ std::vector <std::string> ret;
+
+ // default initial candidate
+ initial_names.insert ("localhost");
+
+ // try to get hostname from uname
+ struct utsname uts;
+
+ if ( ! ::uname (&uts) )
+ {
+ initial_names.insert (uts.nodename);
+ }
+
+ // try gethostname()
+ char hostname[256];
+ if ( 0 == ::gethostname (hostname, 255) )
+ {
+ initial_names.insert (hostname);
+ }
+
+
+ // for all entries in the current set, find aliases via gethostbyname
+ std::set <std::string> :: iterator it;
+
+ for ( it = initial_names.begin ();
+ it != initial_names.end ();
+ it++ )
+ {
+ std::string initial_name = (*it);
+
+ // the initial name is a candidate of course
+ names.insert (initial_name);
+
+ // get hostinfo for this one
+ struct hostent * he = ::gethostbyname (initial_name.c_str ());
+
+ if ( NULL != he )
+ {
+ {
+ // scan all aliases we found
+ int j = 0;
+ while ( he->h_aliases[j] != NULL )
+ {
+ names.insert (he->h_aliases[j]);
+ j++;
+ }
+ }
+
+
+ // get all addresses from hostinfo, and check them, too
+ {
+ struct in_addr ** addr_list = (struct in_addr **) he->h_addr_list;
+
+ for ( int i = 0; addr_list[i] != NULL; i++)
+ {
+ struct hostent * he2 = ::gethostbyaddr (inet_ntoa (*addr_list[i]),
+ he->h_length,
+ he->h_addrtype);
+ if ( NULL != he2 )
+ {
+ {
+ // scan all aliases we found
+ int j = 0;
+ while ( he2->h_aliases[j] != NULL )
+ {
+ names.insert (he2->h_aliases[j]);
+ j++;
+ }
+ }
+ }
+ }
+ }
+ }
+ } // got hostinfo for all initiali names
+
+ // translate set into vector
+ for ( it = names.begin ();
+ it != names.end ();
+ it++ )
+ {
+ ret.push_back (*it);
+ }
+
+ return ret;
+ }
+
+ bool url_is_local (const saga::url & url)
+ {
+ return host_is_localhost (url.get_host ());
+ }
+
} // namespace utils
} // namespace adaptors
} // namespace saga
-#endif // SAGA_UTILS_HPP
File [modified]: utils.hpp
Delta lines: +8 -0
===================================================================
--- trunk/saga/saga/adaptors/utils/utils.hpp 2009-01-17 10:59:24 UTC (rev 3311)
+++ trunk/saga/saga/adaptors/utils/utils.hpp 2009-01-17 11:00:43 UTC (rev 3312)
@@ -4,9 +4,11 @@
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
+
#ifndef SAGA_UTILS_HPP
#define SAGA_UTILS_HPP
+#include <saga/saga/url.hpp>
#include <saga/saga/adaptors/utils/ini/ini.hpp>
#include <saga/saga/adaptors/utils/process/process.hpp>
@@ -26,6 +28,12 @@
std::vector <std::string> tac (std::vector <std::string> in);
std::vector <std::string> grep (std::string pattern,
std::vector <std::string> in);
+
+ std::string get_localhost (void);
+ std::vector <std::string> get_localhosts (void);
+ bool host_is_localhost (std::string host);
+ bool url_is_local (saga::url url );
+
} // namespace utils
} // namespace adaptors
} // namespace saga
More information about the saga-devel
mailing list