Chapter 3. Using Broccoli

Table of Contents
3.1. Obtaining information about your build using broccoli-config
3.2. Suggestions for instrumenting applications
3.3. The Broccoli API


3.1. Obtaining information about your build using broccoli-config

Similarly to many other software packages, the Broccoli distribution provides a script that you can use to obtain details about your Broccoli setup. The script currently provides the following flags:

The --cflags and --libs flags are the suggested way of obtaining the necessary information for integrating Broccoli into your build environment. It is generally recommended to use broccoli-config for this purpose, rather than, say, develop new autoconf tests. If you use the autoconf/automake tools, we recommend something along the following lines for your configure script:

dnl ##################################################
dnl # Check for Broccoli
dnl ##################################################
AC_ARG_WITH(broccoli-config,
    AC_HELP_STRING([--with-broccoli-config=FILE], [Use given broccoli-config]),
    [ brocfg="$withval" ],
    [ AC_PATH_GENERIC(broccoli,,
          brocfg="broccoli-config",
          AC_MSG_ERROR(Cannot find Broccoli: Is broccoli-config in path? Use more fertilizer?)) ])

broccoli_libs=`$brocfg --libs`
broccoli_cflags=`$brocfg --cflags`
AC_SUBST(broccoli_libs)
AC_SUBST(broccoli_cflags)
      

You can then use the compiler/linker flags in your Makefile.in/ams by substituting in the values accordingly, which might look as follows:

CFLAGS = -W -Wall -g -DFOOBAR @broccoli_cflags@
LDFLAGS = -L/usr/lib/foobar @broccoli_libs@
      

3.2. Suggestions for instrumenting applications

Often you will want to make existing applications Bro-aware, that is, instrument them so that they can send and receive Bro events at appropriate moments in the execution flow. This will involve modifying an existing code tree, so care needs to be taken to avoid unwanted side effects. By protecting the instrumented code with #ifdef/#endif statements you can still build the original application, using the instrumented source tree. The broccoli-config script helps you in doing so because it already adds -DBROCCOLI to the compiler flags reported when run with the --cflags option:

cpk25@localhost:/home/cpk25 > broccoli-config --cflags
-I/usr/local/include -I/usr/local/include -DBROCCOLI
      

So simply surround all inserted code with a preprocessor check for BROCCOLI and you will be able to build the original application as soon as BROCCOLI is not defined.


3.3. The Broccoli API

Time for some code. In the code snippets below we will introduce variables whenever context requires them and not necessarily when C requires them. The library does not require calling a global initialization function. In order to make the API known, include broccoli.h:

#ifdef BROCCOLI
#include <broccoli.h>
#endif
      

Note

A note on Broccoli's memory management philosophy:

Broccoli generally does not release objects you allocate. The approach taken is "you clean up what you allocate."


3.3.1. Initialization

Broccoli requires global initialization before most of its other other functions can be used. Generally, the way to initialize Broccoli is as follows:

bro_init(NULL);
        

The argument to bro_init() provides optional initialization context, and may be kept NULL for normal use. If required, you may allocate a BroCtx structure locally, initialize it using bro_ctx_init(), fill in additional values as required and and subsequently pass it to bro_init():

BroCtx ctx;
bro_ctx_init(&ctx);
/* Make adjustments to the context structure as required... */
bro_init(&ctx);
        

Caution

The BroCtx structure currently contains a set of five different callback function pointers. These are required for thread-safe operation of OpenSSL (Broccoli itself is thread-safe). If you intend to use Broccoli in a multithreaded environment, you need to implement functions and register them via the BroCtx structure. The O'Reilly book "Network Security with OpenSSL" by Viega et al. shows how to implement these callbacks.

Caution

You must call