Golang — C++ interoperability:

Sharmini Ravindran
Feb 14, 2019 · 7 min read
Image for post
Image for post

The reincarnation of OpenVPN’s C++ library

At Mysterium Network we are working on the world’s 1st decentralized VPN. Our project is built on Golang (Go). Go is a statically compiled language, which offers a rich standard library. Go is syntactically similar to C but comes out as the winner when it comes to memory safety, garbage collection, structural typing, and CSP style concurrency.

There are many libraries written in C or C++. When you wish to use these libraries within Golang, there are two approaches:

Several projects have gone down this road. Wireguard® has done this, check out some of their libraries.

There are other tools that can help with calling java or objective C code into Golang, but everything goes through an intermediary. At a fundamental level, there is interoperability between C and Golang.

In this article, we will be talking about integrating C++ OpenVPN 3 library into a Golang Mysterium Node.

As mentioned above, we are using OpenVPN under the hood. This was our first protocol and it was used as an external binary (executable file).

This basically means that a Mysterium Node and OpenVPN are two different processes which communicate using OpenVPN config and IPC (local sockets to be exact).

Now, this has some limitations — for example, software distribution becomes complicated as you also need to distribute OpenVPN binary with each Mysterium Node — two steps, never great for UX.

It was workable for a proof of concept or very early versions, but as we moved to mobile platforms, this approach became very complicated or even not feasible — especially when considering iOS.

To solve this challenge, we decided to find a way to integrate OpenVPN into our Golang project directly. Also, we decided that this package could be useful for others, that’s how this library was born.

Openvpn3 to the rescue.

Openvpn3 is the official library maintained by OpenVPN team and is being used in almost all platforms as client or connector to OpenVPN server. Also, it’s written in C++ which came with some obstacles we needed to solve.

Our first obstacle was that C++ code cannot be directly called by Golang (Cgo to be exact).
We needed to make small changes to the OpenVPN library itself to export OpenVPN Client as C callable code. This can be found here, and it’s basically a go compatible entry point to the OpenVPN library.

Then there is how Golang treats C code itself (cgo).
The problem was that Golang and it’s package management systems expect that all libraries are source files (i.e. there is no or very limited binary package management). And OpenVPN3 library build process was very over complicated and not easily expressed in a Go way.

So our decision was to compile that library in advance for all platforms we currently support or produce binaries for (arm family (android ios), amd64 family (Windows, Linux, some simulators). As we use Linux for our automatic build system, we had to set up all compilers and SDKs in one place — but that’s for another blog post. Sign up to our newsletter to hear more about what we’re building.

Our heavily patched docker image is heavily borrowed from Karalabe. The result was a single header file (very simple) and a bunch of static libraries for each platform/OS we needed.

Image for post
Image for post

We also had to ensure that these binaries were Go gettable (the go way to fetch a library from GitHub).
We simply committed those libraries to Go repo along with all supporting Go code (which is available at mysterium.network/go-openvpn/openvpn3). Not the best way to distribute the software, but our target was a go gettable library.

Now the easy part 😏 — to call Openvpn3 functions from Go.

It’s quite easy doable. The following examples are simple calls of C functions exported by OpenVPN library (our C wrapper):

Image for post
Image for post
Image for post
Image for post

And here come problems:

  1. First of all, strict rules as to what can and cannot be passed to C code and vice versa, for example — you cannot pass go function reference to C code.
  2. The openvpn3 client also depends heavily on callback functions. One way to approach this was to use only static functions for callbacks. However, this would have limited the flexibility and usability of the library.
    A hybrid solution was to define customisable callback functions in Go and register them in a map with function ids. Static functions in the OpenVPN3 client would then dispatch respective callbacks to registered functions with corresponding ids.
    Here is how it works (let’s take state event callback function as an example):

User defines normal go structures with methods, which satisfies interfaces expected by callback registry:

Image for post
Image for post
Image for post
Image for post

Structure is passed to callback registry which is essentially global id -> callback map:

Image for post
Image for post

What happens next, callbacks registry inserts user provided structure with methods, and creates a C structure, ready to be passed to C code, but instead of passing go function reference to C code, it passes id which is simply key to callback map and an exported go function (with special comment).

Image for post
Image for post

When C code wants to inform user of state changes, it calls static go function and one of the parameters is id. That id is then passed to callback registry to find and call apprioprate user defined callback.

Most of the dragons started rearing their heads when it came to linking the Go packages with OpenVPN static libraries.

The biggest issue was that — the library was built with C++ compiler, but golang cgo used C compiler by default. As a result, all weird and ugly errors began to raise at the linking stage. So if you see similar errors as in example — you are not alone:

Image for post
Image for post

After hours of stack overflow exploration, a simple workaround was to put a empty .cpp file inside the package which uses “C” imports. That way cgo was tricked into using the c++ linker which already had c++ library by default.

There are several other issues we faced but that again is for another blog post. Stay tuned.

In conclusion

When using new technologies like Golang you have to sometimes go off-chain to find solutions that will help you use existing libraries so that you don’t have to start everything from scratch. However, as most solutions in IT, it’s not a silver bullet.

Key takeaways

  • Precompiled libraries on their own poses security risk — potential library users cannot be sure what is exactly compiled in, as there is no code to review
  • Each OS and architecture combination has to have a separate version of the same library
  • iOS framework problem — iOS framework lib (provided by gomobile tool) is a static library itself. So any other dependencies are linked but not combined into the framework — need to do it as a separate step
  • It’s simply not a go way — golang usually expects all source needed for the package, to be in one place.

Connect with our project

Please be sure to follow and subscribe to the following:

Website https://mysterium.network

Twitter https://twitter.com/MysteriumNet


Reddit https://www.reddit.com/r/MysteriumNetwork

Facebook https://www.facebook.com/MysteriumNet

Steemit https://steemit.com/@mysteriumnetwork

Bitcointalk https://bitcointalk.org/index.php?topic=1895626.0

English — https://t.me/Mysterium_Network

Rules & FAQ — https://t.me/MysteriumRulesAndFAQ

Announcements — https://t.me/MysteriumOfficialAnnouncements

Node Testing — https://t.me/mysterium_network_nodes

MysteriumVPN Testing — https://t.me/joinchat/I5-aG0z_3SA6PLgQBCOXlA

中文 / Chinese — https://t.me/MysteriumChineseChat

русский / Russian https://t.me/mystRU

Español / Spanish — https://t.me/mysterium_network_espanol

*WireGuard” and the “WireGuard” logo are registered trademarks of Jason A. Donenfeld.

Mysterium Network

Decentralized VPN built on blockchain …

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store