<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/quick-and-clean-dmarc-record-processing-with-inline-rcpp/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2018/02/06/quick-and-clean-dmarc-record-processing-with-inline-rcpp/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Much of what I need to do for work-work involves using tools that are (for the moment) not in R. Today, I needed to test the validity of (and other processing on) <a rel="nofollow" href="https://dmarc.org/" target="_blank">DMARC records</a> and I’m <em>loathe</em> to either reinvent the wheel or reticulate bits from a fragmented programming language ecosystem unless absolutely necessary. Thankfully, there’s <a rel="nofollow" href="http://www.trusteddomain.org/opendmarc/libopendmarc/index.html" target="_blank"><code>libopendmarc</code></a> which works well on sane operating systems, but it is a C library that needs an interface to use in R.</p>
<p>However, I also <em>really</em> didn’t want to start a new package for this <em>just</em> yet (there will eventually be one, though, and I prefer working in a package context for Rcpp work). I just needed to run <a rel="nofollow" href="http://www.trusteddomain.org/opendmarc/libopendmarc/opendmarc_policy_store_dmarc.html" target="_blank"><code>opendmarc_policy_store_dmarc()</code></a> against a decent-sized chunk of domain names and already-retrieved DMARC <code>TXT</code> records. So, I decided to write a small “inline” <code>cppFunction()</code> to get’er done.</p>
<p>Why am I blogging about this?</p>
<p>Despite growing popularity and a nice <a rel="nofollow" href="http://gallery.rcpp.org/" target="_blank">examples site</a>, many newcomers to <code>Rcpp</code> (literally <em>the</em> way you want to go when it comes to bridging C[++] and R) still voice discontent about there not being enough “easy” examples. Granted, they are quitely likely looking for full-bore tutorials covering a different, explicit use cases. The aforelinked Gallery has some of those and there <em>are</em> codified examples in — literally — <a rel="nofollow" href="https://github.com/eddelbuettel/rcppexamples" target="_blank"><code>rcppexamples</code></a>. But, there definitely needs to be more blog posts, books and such linking to them and expanding upon them.</p>
<p>Having mentioned that I’m using <code>cppFunction()</code>, one could, further, ask <em>“<code>cppFunction()</code> has a help page with an example, so why blather about using it?”</em>. Fair point! And, there <em>is</em> a reason which was hinted at in the opening paragraph.</p>
<p>I need to use <code>libopendmarc</code> and that requires making a “plugin” if I’m going to do this “inline”. For some other DMARC processing I also need to use <code>libresolv</code> since the library needs to make DNS requests and uses <code>resolv</code>. You don’t need a plugin for a package version as you just need to boilerplate some “find these libraries and get their paths right for <code>Makevars.in</code>” and add the linking code in there as well. Here, we need to register two plugins that provide metdata for the magic that happens under the covers when <code>Rcpp</code> takes your inline code, compiles it and makes the function shared object available in R.</p>
<p>Plugins can be complex and do transformations, but the two I needed to write are just helping ensure the right <code>#include</code> lines are there along with the right linker libraries. Here they are:</p>
<pre><code>library(Rcpp)

registerPlugin(
  name = &quot;libresolv&quot;,
  plugin = function(x) {
    list(
      includes = &quot;&quot;,
      env = list(PKG_LIBS=&quot;-lresolv&quot;)
    )
  }
)

registerPlugin(
  name = &quot;libopendmarc&quot;,
  plugin = function(x) {
    list(
      includes = &quot;#include </code></pre>