OW_SLPProvider.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_Types.hpp"
00038 #include "OW_Format.hpp"
00039 #include "OW_Logger.hpp"
00040 #include "OW_CppPolledProviderIFC.hpp"
00041 #include "OW_ConfigOpts.hpp"
00042 #include "OW_CIMOMLocatorSLP.hpp"
00043 #include "OW_SocketAddress.hpp"
00044 #include "OW_CIMObjectPath.hpp"
00045 #include "OW_CIMProperty.hpp"
00046 #include "OW_CIMOMHandleIFC.hpp"
00047 #include "OW_CIMException.hpp"
00048 #include "OW_CIMValue.hpp"
00049 
00050 // TODO: Replace this with a string from uname
00051 #ifdef OW_GNU_LINUX
00052 #define OW_STRPLATFORM "Linux"
00053 #endif
00054 #ifdef OW_SOLARIS
00055 #define OW_STRPLATFORM "Solaris"
00056 #endif
00057 #ifndef OW_STRPLATFORM
00058 #error "OW_STRPLATFORM is undefined"
00059 #endif
00060 extern "C"
00061 {
00062 #include <slp.h>
00063 }
00064 
00065 namespace OW_NAMESPACE
00066 {
00067 
00068 static const Int32 POLLING_INTERVAL = 60 * 5;
00069 static const Int32 INITIAL_POLLING_INTERVAL = 5;
00070 extern "C"
00071 {
00072 static
00073 void
00074 slpRegReport(SLPHandle hdl, SLPError errArg, void* cookie)
00075 {
00076    if (errArg < SLP_OK)
00077    {
00078       LoggerRef* pLogger = (LoggerRef*)cookie;
00079       OW_LOG_ERROR((*pLogger), Format("cimom received error durring SLP registration: %1",
00080          (int)errArg));
00081    }
00082 }
00083 }
00084 
00085 namespace
00086 {
00087 
00088 struct slpHandleCloser
00089 {
00090    slpHandleCloser(SLPHandle& hdl) : m_hdl(hdl) {}
00091    ~slpHandleCloser() { SLPClose(m_hdl); }
00092 
00093    SLPHandle& m_hdl;
00094 };
00095 
00096 const String COMPONENT_NAME("ow.provider.SLPProvider");
00097 
00098 }
00099 
00100 class SLPProvider : public CppPolledProviderIFC
00101 {
00102 public:
00107    virtual Int32 getInitialPollingInterval(const ProviderEnvironmentIFCRef &env)
00108    {
00109       // TODO: Fix this up to use provider instances instead of just config file options, which may not reflect the real state of the cimom.
00110       if (env->getConfigItem(ConfigOpts::SLP_ENABLE_ADVERTISEMENT_opt, OW_DEFAULT_SLP_ENABLE_ADVERTISEMENT).equalsIgnoreCase("false"))
00111       {
00112          return 0;
00113       }
00114       Int32 rval = INITIAL_POLLING_INTERVAL;
00115       OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), Format(
00116          "SLPProvider::getInitialPollingInterval returning %1",
00117          INITIAL_POLLING_INTERVAL).c_str());
00118       m_httpsPort = env->getConfigItem(ConfigOpts::HTTP_SERVER_HTTPS_PORT_opt, OW_DEFAULT_HTTP_SERVER_HTTPS_PORT);
00119       m_httpPort = env->getConfigItem(ConfigOpts::HTTP_SERVER_HTTP_PORT_opt, OW_DEFAULT_HTTP_SERVER_HTTP_PORT);
00120       Int32 httpsPort = 0, httpPort = 0;
00121       try
00122       {
00123          httpsPort = m_httpsPort.toInt32();
00124       }
00125       catch (const StringConversionException&)
00126       {
00127       }
00128       try
00129       {
00130          httpPort = m_httpPort.toInt32();
00131       }
00132       catch (const StringConversionException&)
00133       {
00134       }
00135       if (httpsPort < 1 && httpPort < 1)
00136       {
00137          return 0;
00138       }
00139       m_useDigest = env->getConfigItem(ConfigOpts::HTTP_SERVER_USE_DIGEST_opt)
00140             .equalsIgnoreCase("true");
00141       m_allowAnonymous = env->getConfigItem(ConfigOpts::ALLOW_ANONYMOUS_opt, OW_DEFAULT_ALLOW_ANONYMOUS)
00142             .equalsIgnoreCase("true");
00143    
00144       
00145       m_interopSchemaNamespace = env->getConfigItem(ConfigOpts::INTEROP_SCHEMA_NAMESPACE_opt, OW_DEFAULT_INTEROP_SCHEMA_NAMESPACE);
00146 
00147       m_serviceId = "unknown";
00148       try
00149       {
00150          CIMObjectPathArray a = env->getCIMOMHandle()->enumInstanceNamesA(m_interopSchemaNamespace, "CIM_ObjectManager");
00151          if (a.size() == 1)
00152          {
00153             const CIMObjectPath& om = a[0];
00154             m_serviceId = om.getKeyT("Name").getValueT().toString();
00155          }
00156       }
00157       catch (CIMException& e)
00158       {
00159          OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), Format("SLP provider caught (%1) when executing enumInstanceNames(%2, \"CIM_ObjectManager\")", e, m_interopSchemaNamespace));
00160          OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), "SLP provider unable to determine service-id");
00161       }
00162 
00163       m_queryEnabled = !env->getConfigItem(ConfigOpts::WQL_LIB_opt, OW_DEFAULT_WQL_LIB).empty();
00164       m_indicationEnabled = !env->getConfigItem(ConfigOpts::DISABLE_INDICATIONS_opt, OW_DEFAULT_DISABLE_INDICATIONS).equalsIgnoreCase("true");
00165 
00166       
00167 
00168       return rval;
00169    }
00179    virtual Int32 poll(const ProviderEnvironmentIFCRef &env)
00180    {
00181       doSlpRegister(env);
00182       return POLLING_INTERVAL;
00183    }
00184 private:
00185    String m_httpsPort;
00186    String m_httpPort;
00187    bool m_useDigest;
00188    bool m_allowAnonymous;
00189    String m_serviceId;
00190    String m_interopSchemaNamespace;
00191    bool m_queryEnabled;
00192    bool m_indicationEnabled;
00193 
00194    void doSlpRegister(const ProviderEnvironmentIFCRef& env)
00195    {
00196       SLPError err;
00197       SLPHandle slpHandle;
00198       if ((err = SLPOpen("en", SLP_FALSE, &slpHandle)) != SLP_OK)
00199       {
00200          OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), Format("SLPProvider::doSlpRegister - SLPOpenFailed: %1",
00201             err).c_str());
00202          return;
00203       }
00204       slpHandleCloser closer(slpHandle);
00205 
00206       /*
00207       String attributes(
00208          "(namespace=root),(implementation=OpenWbem),(version="OW_VERSION"),"
00209          "(query-language=WBEMSQL2),(host-os="OW_STRPLATFORM")");
00210       if (!m_allowAnonymous)
00211       {
00212          attributes += ",(authentication=";
00213          if (m_useDigest)
00214          {
00215             attributes += "Digest)";
00216          }
00217          else
00218          {
00219             attributes += "Basic)";
00220          }
00221       }
00222       */
00223       StringBuffer attributes;
00224 
00225       // service-hi-name - Optional
00226 
00227       // service-hi-description - Optional
00228 
00229       // service-id
00230       attributes += "(service-id=";
00231       attributes += m_serviceId;
00232       attributes += ')';
00233 
00234       // CommunicationMechanism
00235       attributes += ",(CommunicationMechanism=cim-xml)";
00236 
00237       // OtherCommunicationMechanismDescription
00238 
00239       // InteropSchemaNamespace
00240       attributes += ",(InteropSchemaNamespace=";
00241       attributes += m_interopSchemaNamespace;
00242       attributes += ')';
00243 
00244       // ProtocolVersion
00245       attributes += ",(ProtocolVersion=1.1)";
00246 
00247       // FunctionalProfilesSupported
00248       attributes += ",(FunctionalProfilesSupported=";
00249       attributes +=
00250          "Basic Read"
00251 #ifndef OW_DISABLE_SCHEMA_MANIPULATION
00252          ",Schema Manipulation"
00253 #endif
00254 #ifndef OW_DISABLE_INSTANCE_MANIPULATION
00255          ",Basic Write"
00256          ",Instance Manipulation"
00257 #endif
00258 #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00259          ",Association Traversal"
00260 #endif
00261 #ifndef OW_DISABLE_QUALIFIER_DECLARATION
00262          ",Qualifier Declaration"
00263 #endif
00264          ;
00265       if (m_queryEnabled)
00266       {
00267          attributes += ",Query Execution";
00268       }
00269       if (m_indicationEnabled)
00270       {
00271          attributes += ",Indications";
00272       }
00273       attributes += ")";
00274 
00275       // FunctionalProfileDescriptions - Only use if we put "Other" in FunctionalProfilesSupported
00276 
00277       // MultipleOperationsSupported
00278       attributes += ",(MultipleOperationsSupported=true)";
00279 
00280       // AuthenticationMechanismsSupported
00281       attributes += ",(AuthenticationMechanismsSupported=";
00282       if (m_allowAnonymous) // this takes precedence over the other schemes
00283       {
00284          attributes += "None)";
00285       }
00286       else if (m_useDigest)
00287       {
00288          attributes += "Digest)";
00289       }
00290       else
00291       {
00292          attributes += "Basic)";
00293       }
00294 
00295       // AuthenticationMechansimDescriptions - Only use if we put "Other" in AuthenticationMechanismsSupported
00296 
00297       // Namespace - Optional, and it may be more information that we want to advertise.  Evaluate whether we want this when we've got more experience with it.
00298       // Classinfo - Options, won't do it for the same reason as Namespace
00299 
00300       // RegisteredProfilesSupported - TODO
00301       
00302 
00303 
00304 
00305       String hostname = SocketAddress::getAnyLocalHost().getName();
00306       StringArray urls;
00307       try
00308       {
00309          if (m_httpPort.toInt32() > 0)
00310          {
00311             String newUrl = "http://";
00312             newUrl += hostname + ":" + m_httpPort;
00313             urls.push_back(newUrl);
00314          }
00315       }
00316       catch (const StringConversionException&)
00317       {
00318       }
00319       try
00320       {
00321          if (m_httpsPort.toInt32() > 0)
00322          {
00323             String newUrl = "https://";
00324             newUrl += hostname + ":" + m_httpsPort;
00325             urls.push_back(newUrl);
00326          }
00327       }
00328       catch (const StringConversionException&)
00329       {
00330       }
00331       for (size_t i = 0; i < urls.size(); i++)
00332       {
00333          String urlString;
00334          urlString = OW_CIMOM_SLP_URL_PREFIX;
00335          urlString += urls[i];
00336          // Register URL with SLP
00337          LoggerRef lgr = env->getLogger(COMPONENT_NAME);
00338          err = SLPReg(slpHandle,    // SLP Handle from open
00339             urlString.c_str(),      // Service URL
00340             POLLING_INTERVAL+60,    // Length of time registration last
00341             0,                      // Service type (not used)
00342             attributes.c_str(),     // Attributes string
00343             SLP_TRUE,               // Fresh registration (Always true for OpenSLP)
00344             slpRegReport,           // Call back for registration error reporting
00345             &lgr);                  // Give cimom handle to callback
00346          if (err != SLP_OK)
00347          {
00348             OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), Format("cimom failed to register url with SLP: %1",
00349                urlString).c_str());
00350          }
00351          else
00352          {
00353             OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), Format("cimom registered service url with SLP: %1",
00354                urlString).c_str());
00355          }
00356       }
00357    }
00358 };
00359 
00360 } // end namespace OW_NAMESPACE
00361 
00362 OW_PROVIDERFACTORY(OpenWBEM::SLPProvider, owslpprovider)
00363 

Generated on Thu Feb 9 08:48:11 2006 for openwbem by  doxygen 1.4.6