C from erlang via linked-in driver

Erlang truly is a useful language. It's fast, full-featured and elegant. Like all high-level languages, however, you sometimes need to access legacy or system code written in another language like, you guessed it, C.

I came accross this situation recently when porting my webcrawler to erlang. Under pressure http:request proved to be unreliable when used in a multi-threaded scenario. The solution I chose was to bridge erlang with libcurl by way of an erlang "linked-in driver". While I only used it for retrieving web page content into memory I figure I'll be able to wrap all of libcurl in the future.

C

I'll start out with some C code that will function as the bridge between erlang and libcurl. This is the code that qualifies as a "linked-in" driver. Like all C code there's plenty of plumbing. See my comments for explanation.

Note that I'm using curl's multi interface which doesn't block on I/O.