OpenBus SDK C++ TAO  2.1.0.0RC
 Tudo Classes Namespaces Funções Definições de tipos
Connection.hpp
1 // -*- coding: iso-8859-1-unix -*-
2 
9 #ifndef TECGRAF_SDK_OPENBUS_OPENBUS_CONNECTION_HPP
10 #define TECGRAF_SDK_OPENBUS_OPENBUS_CONNECTION_HPP
11 
12 #include "scsC.h"
13 #include "openbus/idl.hpp"
14 #include "openbus/detail/decl.hpp"
15 #include "openbus/detail/interceptors/orb_initializer.hpp"
16 #ifndef TECGRAF_SDK_OPENBUS_OPENBUS_DETAIL_LRUCACHE_HPP
17 #define TECGRAF_SDK_OPENBUS_OPENBUS_DETAIL_LRUCACHE_HPP
18 #include "openbus/detail/lru_cache.hpp"
19 #endif
20 #include "openbus/orb_initializer.hpp"
21 #include "openbus/detail/openssl/private_key.hpp"
22 
23 #include <boost/array.hpp>
24 #include <boost/function.hpp>
25 #include <boost/scoped_ptr.hpp>
26 #include <boost/thread.hpp>
27 #include <boost/shared_ptr.hpp>
28 #include <memory>
29 #include <stdexcept>
30 #include <string>
31 #include <vector>
32 #include <cstring>
33 
37 namespace openbus
38 {
39  class OpenBusContext;
40  class LoginCache;
41  class RenewLogin;
42  class PublicKey;
43 
44  namespace interceptors
45  {
46  struct orb_info;
47  struct ServerInterceptor;
48  struct ClientInterceptor;
49  }
50 }
51 
52 namespace openbus
53 {
54 struct OPENBUS_SDK_DECL WrongBus : public std::exception
55 {
56  const char *what() const throw()
57  {
58  return "openbus::WrongBus";
59  }
60 };
61 
62 struct OPENBUS_SDK_DECL BusChanged : public std::exception
63 {
64  const char *what() const throw()
65  {
66  return "openbus::BusChanged";
67  }
68 };
69 
70 struct OPENBUS_SDK_DECL AlreadyLoggedIn : public std::exception
71 {
72  const char *what() const throw()
73  {
74  return "openbus::AlreadyLoggedIn";
75  }
76 };
77 
78 struct OPENBUS_SDK_DECL InvalidLoginProcess : public std::exception
79 {
80  const char *what() const throw()
81  {
82  return "openbus::InvalidLoginProcess";
83  }
84 };
85 
86 struct OPENBUS_SDK_DECL InvalidPropertyValue : public std::exception
87 {
88  InvalidPropertyValue(const std::string &p, const std::string &v) throw()
89  : property(p), value(v)
90  {
91  }
92 
93  ~InvalidPropertyValue() throw()
94  {
95  }
96 
97  const char *what() const throw()
98  {
99  return "openbus::InvalidPropertyValue";
100  }
101 
102  const std::string property;
103  const std::string value;
104 };
105 
106 struct OPENBUS_SDK_DECL InvalidBusAddress : public std::exception
107 {
108  const char *what() const throw()
109  {
110  return "openbus::InvalidBusAddress";
111  }
112 };
113 
114 class Connection;
115 
128 class OPENBUS_SDK_DECL SharedAuthSecret
129 {
130 public:
136  void cancel();
137 
142  std::string busid() const
143  {
144  return busid_;
145  }
146 private:
149  const std::string &bus_id,
150  idl::access::LoginProcess_var,
151  idl::legacy::access::LoginProcess_var,
152  const idl::core::OctetSeq &secret,
153  interceptors::ORBInitializer *);
154 
155  std::string busid_;
156  idl::access::LoginProcess_var login_process_;
157  idl::legacy::access::LoginProcess_var legacy_login_process_;
158  idl::core::OctetSeq secret_;
159  interceptors::ORBInitializer *orb_initializer_;
160  friend class OpenBusContext;
161  friend class Connection;
162 };
163 
189 class OPENBUS_SDK_DECL Connection
190 {
191 public:
192  typedef std::vector<std::pair<std::string, std::string> >
193  ConnectionProperties;
209  typedef boost::function<void (Connection & conn, idl::access::LoginInfo login)>
211 
235  void loginByPassword(
236  const std::string &entity,
237  const std::string &password,
238  const std::string &domain = "");
239 
264  void loginByCertificate(const std::string &entity, EVP_PKEY *key);
265 
286  SharedAuthSecret startSharedAuth();
287 
309  void loginBySharedAuth(const SharedAuthSecret &secret);
310 
324  bool logout();
325 
348  void onInvalidLogin(InvalidLoginCallback_t p);
349 
354  InvalidLoginCallback_t onInvalidLogin() const;
355 
360  const idl::access::LoginInfo *login() const;
361 
365  const std::string busid() const;
366  ~Connection();
367 private:
371  Connection(CORBA::Object_var,
372  CORBA::ORB_ptr,
373  interceptors::ORBInitializer *,
374  OpenBusContext &,
375  EVP_PKEY *access_key,
376  bool legacy_support);
377 
381  Connection(const std::string host,
382  const unsigned short port,
383  CORBA::ORB_ptr,
384  interceptors::ORBInitializer *,
385  OpenBusContext &,
386  EVP_PKEY *access_key,
387  bool legacy_support);
388 
389  Connection(const Connection &);
390  Connection &operator=(const Connection &);
391 
392  static void renewLogin(Connection &conn, idl::access::AccessControl_ptr acs,
393  OpenBusContext &ctx, idl::access::ValidityTime t);
394 
395  void login(idl::access::LoginInfo &loginInfo,
396  idl::access::ValidityTime validityTime);
397 
398  void checkBusid() const;
399  bool _logout(bool local = true);
400  CORBA::ORB_ptr orb() const
401  {
402  return _orb;
403  }
404 
405  idl::access::LoginRegistry_var login_registry() const
406  {
407  return _login_registry;
408  }
409 
410  idl::access::AccessControl_var access_control() const
411  {
412  return _access_control;
413  }
414 
415  const idl::access::LoginInfo *_login() const
416  {
417  boost::lock_guard<boost::mutex> lock(_mutex);;
418  return _loginInfo.get();
419  }
420 
421  idl::offers::OfferRegistry_var getOfferRegistry() const
422  {
423  return _offer_registry;
424  }
425 
426  idl::access::LoginRegistry_var getLoginRegistry() const
427  {
428  return _login_registry;
429  }
430 
431  void init();
432 
433  idl::access::LoginInfo get_login();
434 
435  scs::core::IComponent_var _iComponent;
436  const std::string _host;
437  const unsigned short _port;
438  interceptors::ORBInitializer * _orb_init;
439  CORBA::ORB_ptr _orb;
440  boost::thread _renewLogin;
441  mutable boost::mutex _mutex;
442  boost::scoped_ptr<idl::access::LoginInfo> _loginInfo, _invalid_login;
443  InvalidLoginCallback_t _onInvalidLogin;
444 
445  enum State
446  {
447  LOGGED,
448  UNLOGGED,
449  INVALID
450  } _state;
451 
452  /* Variaveis que sao modificadas somente no construtor. */
453  OpenBusContext &_openbusContext;
454  PrivateKey _key;
455  idl::access::AccessControl_var _access_control;
456  idl::access::LoginRegistry_var _login_registry;
457  idl::offers::OfferRegistry_var _offer_registry;
458  boost::scoped_ptr<LoginCache> _loginCache;
459  std::string _busid;
460  boost::scoped_ptr<PublicKey> _buskey;
461  bool _legacy_support;
462  idl::legacy::access::AccessControl_var _legacy_access_control;
463  idl::legacy_support::LegacyConverter_var _legacy_converter;
464 
465 
466  CORBA::Object_var _component_ref;
467 
468  struct SecretSession
469  {
470  SecretSession()
471  : id(0)
472  , ticket(0)
473  , is_legacy(false)
474  {
475  secret.fill(0);
476  }
477  CORBA::ULong id;
478  std::string remote_id, entity;
479  boost::array<unsigned char, secret_size> secret;
480  CORBA::ULong ticket;
481  bool is_legacy;
482  friend bool operator==(const SecretSession &lhs, const SecretSession &rhs);
483  friend bool operator!=(const SecretSession &lhs, const SecretSession &rhs);
484  };
485  typedef LRUCache<hash_value, std::string> profile2login_LRUCache;
486  profile2login_LRUCache _profile2login;
487  LRUCache<std::string, SecretSession> _login2session;
488 
489  friend struct openbus::interceptors::ServerInterceptor;
490  friend struct openbus::interceptors::ClientInterceptor;
491  friend class openbus::OpenBusContext;
492  friend bool operator==(const SecretSession &lhs, const SecretSession &rhs);
493  friend bool operator!=(const SecretSession &lhs, const SecretSession &rhs);
494 };
495 
496 inline bool operator==(const Connection::SecretSession &lhs,
497  const Connection::SecretSession &rhs)
498 {
499  return lhs.id == rhs.id
500  && lhs.remote_id == rhs.remote_id
501  && lhs.secret == rhs.secret
502  && lhs.ticket == rhs.ticket;
503 }
504 
505 inline bool operator!=(const Connection::SecretSession &lhs,
506  const Connection::SecretSession &rhs)
507 {
508  return !(lhs == rhs);
509 }
510 
511 }
512 
513 #endif
openbus
Definition: Connection.hpp:37
Segredo para compartilhamento de autenticação.
Definition: Connection.hpp:128
std::string busid() const
Retorna o identificador do barramento em que o segredo pode ser utilizado.
Definition: Connection.hpp:142
boost::function< void(Connection &conn, idl::access::LoginInfo login)> InvalidLoginCallback_t
Callback de login inválido.
Definition: Connection.hpp:210
Conexão para acesso identificado a um barramento.
Definition: Connection.hpp:189