3 Mayıs 2017 Çarşamba

asio sll context Sınıfı

Giriş
Şu satırı dahil ederiz.
#include <boost/asio/ssl.hpp>
Constructor - method
TLS V1.2 ise daha düşük sürümdeki şunlar desteklenir. Daha düşük protokol desteği için Windows'ta ayarları değiştirmek gerekebilir.
TLS v. 1.1, TLS v. 1.0, SSL 3.0, SSL 2.0
Şöyle yaparız.
asio::ssl::context ctx (asio::ssl::context::tlsv12_server);
veya şöyle yaparız.
asio::ssl::context ctx(asio::ssl::context::tlsv12_client);
veya şöyle yaparız.
asio::ssl::context ctx (asio::ssl::context::tlsv11); 
veya şöyle yaparız.
asio::ssl::context ctx (asio::ssl::context::sslv23_client);
veya şöyle yaparız.
asio::ssl::context ctx (asio::ssl::context::sslv23)
Constructor - io_service + method
Bu constructor deprecated. Kullanılmamalı. Şöyle yaparız.
boost::asio::io_service ios;
asio::ssl::context ctx {ios, asio::ssl::context::method::sslv23_client};
add_certificate_authority metodu
Şöyle yaparız.
std::vector<char> caCrtBuffer = ...;
ctx.add_certificate_authority(asio::buffer(caCrtBuffer, caCrtBuffer.size()));
impl metodu
Şöyle yaparız.
SSL_CTX* impl = ctx.impl(); 
load_verify_file metodu
CA sertifikaların olduğu dosyayı yükler. Şöyle yaparız.
ctx.load_verify_file(filename);
Şöyle yaparız.
ctx.load_verify_file("ca.pem");
Sertifiları windows'ta almak için şöyle yaparız.
#include <windows.h>
#include <Wincrypt.h>

std::vector<std::string> system_root_certificates()
{
  std::vector<std::string> certs;

  HCERTSTORE hStore;
  PCCERT_CONTEXT pCertContext = NULL;

  if (!(hStore = ::CertOpenStore(
              CERT_STORE_PROV_SYSTEM_A,
              0,
              NULL,
              CERT_SYSTEM_STORE_CURRENT_USER,
              "Root")))
        return certs;

  do
  {
    if (pCertContext = ::CertFindCertificateInStore(
                  hStore,
                  X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
                  0,
                  CERT_FIND_ANY,
                  NULL,
                  pCertContext))
    {
      certs.push_back("-----BEGIN CERTIFICATE-----\n" +
          Base64::encode_copy(std::string((char *) pCertContext->pbCertEncoded,
                              (size_t) pCertContext->cbCertEncoded), true) +
                            "\n-----END CERTIFICATE-----\n");
      }
  } while (pCertContext);

  if (!pCertContext)
    ::CertFreeCertificateContext(pCertContext);

  ::CertCloseStore(
              hStore,
              CERT_CLOSE_STORE_FORCE_FLAG);

  return certs;
}
Dosya oluşturmak için şöyle yaparız.
...
for (size_t i = 0; i < certs.size(); ++i)
    certFile << certs[i];
...
native_handle metodu
Şöyle yaparız.
SSL_CIPHER* handle = ctx->native_handle(); 
set_default_verify_paths metodu
Açıklaması şöyle.
Trusted certificates are often installed or updated via the OS, browsers, or individual packages. For instance, in the *nix world, the certificates are often available through the ca-certificates package, and the certificates are installed to locations that boost::asio::ssl::context::set_default_verify_paths() will find.
OpenSSL kurulumunun oluşturduğu CA'ları kullanır. Bu dizin sanırım SSL_CERT_DIR ortam değişkeni ile belirtiliyor.
Örnek
Şöyle yaparız.
// set security
ctx.set_default_verify_paths();
set_options metodu
Örnek 1
Şöyle yaparız.
ctx.set_options(SSL_OP_NO_TICKET);
Örnek
SSLv2 istemiyorsak şöyle yaparız.
ctx.set_options
(
  boost::asio::ssl::context::default_workarounds |
  boost::asio::ssl::context::no_sslv2
);
Örnek 
Bir çok sunucu SSLV3 ve altını desteklemiyor. Sunucular şu hatayı verirler.
Error: sslv3 alert handshake failure
Bu desteği istemediğimizi belirtmek için şöyle yaparız.
ctx.set_options(boost::asio::ssl::context::default_workarounds |
                boost::asio::ssl::context::no_sslv2 |
                boost::asio::ssl::context::no_sslv3);
Örnek
Şöyle yaparız.
try {
  ctx.set_options(boost::asio::ssl::context::default_workarounds |
            boost::asio::ssl::context::no_sslv2 |
            boost::asio::ssl::context::no_sslv3 |
            boost::asio::ssl::context::single_dh_use);
}
catch (std::exception& e) {
  std::cout << e.what() << std::endl;
}
Örnek
Şöyle yaparız.
ctx.set_options(asio::ssl::context::default_workarounds |   asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3 |   asio::ssl::context::no_tlsv1 | asio::ssl::context::no_tlsv1_1 |   asio::ssl::context::single_dh_use);
Örnek
Şöyle yaparız.
ctx.set_options( asio::ssl::context::default_workarounds |
                 asio::ssl::context::no_sslv2 |
                 asio::ssl::context::single_dh_use);
set_password_callback metodu
Şöyle yaparız.
ctx.set_password_callback (&password_callback);   
set_verify_callback metodu
Örnek ver. ssl::stream sınıfında da aynı metod mevcut.

set_verify_mode metodu
İstemci tarafında şöyle yaparız.
ctx.set_verify_mode(asio::ssl::verify_peer |
                    asio::ssl::verify_fail_if_no_peer_cert);
user_certificate_chain metodu
SSL sunucusunda kullanılır. Şöyle yaparız.
boost::asio::const_buffer cert = ...;
ctx.use_certificate_chain (cert);
Şöyle yaparız.
std::vector<char> buf = ...; ctx.use_certificate_chain (asio::buffer(buf, userCrtBuffer.size()));
use_certificate_chain_file metodu
Şöyle yaparız.
ctx.use_certificate_chain_file("server.pem");
user_certificate_file metodu
Şöyle yaparız.
ctx.use_certificate_file ("public_key.pem", ssl::context::pem);
use_private_key metodu
SSL sunucusunda kullanılır. Şöyle yaparız.
boost::asio::const_buffer key =...;
ctx.use_private_key(key, boost::asio::ssl::context::pem);
Şöyle yaparız.
std::vector<char> userKeyBuffer = ctx.use_private_key (asio::buffer(userKeyBuffer, userKeyBuffer.size()),    asio::ssl::context::pem);
use_private_key_file metodu
Şöyle yaparız.
ctx.use_private_key_file("server.pem", boost::asio::ssl::context::pem);
use_rsa_private_key_file metodu
Şöyle yaparız.
ctx.use_rsa_private_key_file ("private_key.pem", ssl::context::pem);
use_tmp_dh metodu
Şöyle yaparız.
std::vector<char> dhParamsBuffer = ...
ctx.use_tmp_dh (asio::buffer(dhParamsBuffer, dhParamsBuffer.size()));

1 Mayıs 2017 Pazartesi

fusion

Giriş
Açıklaması şöyle
Fusion is a library and a framework similar to both STL and the boost MPL. The structure is modeled after MPL, which is modeled after STL. It is named "fusion" because the library is reminiscent of the "fusion" of compile time meta-programming with runtime programming. The library inherently has some interesting flavors and characteristics of both MPL and STL. It lives in the twilight zone between compile time meta-programming and run time programming. STL containers work on values. MPL containers work on types. Fusion containers work on both types and values.
map Sınıfı
Şu satırı dahil ederiz.
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/map.hpp>
Elimizde bir map olsun
namespace fields {
  struct rgb;
  struct gray;
  struct keypoints;
  struct edges;
  struct objects;
}

typedef fusion::map<
  fusion::pair<fields::rgb,       string>,
  fusion::pair<fields::gray,      int>,
  fusion::pair<fields::keypoints, int>,
  fusion::pair<fields::edges,     int>,
  fusion::pair<fields::objects,   double>
> Fields;
Constructor
Şöyle yaparız
Fields A_map;
Free Style Metodlar
at_key metodu
Şöyle yaparız.
template<typename field>
void Add(typename fusion::result_of::value_at_key<Fields, field>::type data) {
  fusion::at_key<field>(A_map) = data;
}
fold metodu
Şöyle yaparız.
#include <vector>
#include <boost/fusion/container/map.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/fold.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>
namespace fusion = boost::fusion;

struct A;
struct B;
struct C;
std::vector<int> va = { 4, 5, 6 };
std::vector<char> vb = { 'a', 'b', 'c' };
std::vector<double> vc = { 10., 11., 12. };    

template< typename FusionIteratorT, typename Ret>
struct initialize_map_with_fusion_vector {

  template<typename T>
  Ret operator()(FusionIteratorT& i,  T& val) {
    val = fusion::deref(i);
    return fusion::next(i);
  } 
};

void use_fold_with_fusion_vector() {
  using M = 
  fusion::map<fusion::pair<A, std::vector<int>>, 
    fusion::pair<B, std::vector<char>>, fusion::pair<C, std::vector<double>>>;
  using V = 
  fusion::vector<std::vector<int>, std::vector<char>, std::vector<double>>;
  auto fm = M();
  auto fv = V(va, vb, vc);
  using FusionIteratorT = decltype(fusion::begin(fv));
  using Ret = fusion::result_of::next<fusion::result_of::begin<decltype(fv)>>;
  fusion::fold(fm, fusion::begin(fv),
    initialize_map_with_fusion_vector<FusionIteratorT, Ret > ());
}

interprocess interprocess_mutex Sınıfı

Giriş
Şu satırı dahil ederiz.
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
Kullanımı
Şöyle yaparız.
// mutex for writing to the queue.
ip::interprocess_mutex mutex = ...;

{
  ip::scoped_lock<decltype(mutex)> lock{mutex};
  ...
}

serialization linkleme

Giriş
Linklemek için şu satırı dahil ederiz.
-lboost_serialization veya -lboost_wserialization
Şöyle yaparız.

g++ ScenarioResult.cpp -lboost_serialization

serizaliton BOOST Desteği

boost variant
Şu satırı dahil ederiz.
#include <boost/serialization/variant.hpp>

serialization xml_iarchive Sınıfı

Giriş
Şu satırı dahil ederiz.
#include <boost/archive/xml_iarchive.hpp> 
Bu sınıf sadece xml_oarchive sınıfının ürettiği formattaki xml belgelerini okuyabilir.

Constructor - file + flags
Şöyle yaparız.
std::ifstream file("myfile.xml");

try
{
  unsigned int flags = boost::archive::no_header;
  boost::archive::xml_iarchive ia (file, flags);
  Foo foo;
  ia >> BOOST_SERIALIZATION_NVP(foo);
}
catch (boost::archive::archive_exception ex)
{
  // blah
}