[Bro] a tree structure with Bro records -- traversal w/ callback (FYI)

Mike Wood muscletot at gmail.com
Tue Dec 4 11:35:47 PST 2007


Thanks for the tip -- I have built my tree as you suggest, storing
nodes in a table and managing the hierarchy simply w/ non-recursive
key references stored in each node.

Now I would like to create a function to walk my tree that takes a
callback function to performn some arbitrary computation on each node
as the tree is traversed. What I'd like is the ability to do the
following:

function callback( i : count ) {
    print fmt("i: %d", i);
}

function caller( f : function(j:count) ) {
    f(13);
}

event bro_init() {
    caller( callback );
}

... but as you will see, lots of type clashing errors are generated
for this code. I have had success defining a global variable for the
callback function and using 'redef' to change its value, but that will
only allow one value for the callback per bro invocation.

AHA! While writing this message, I have discovered the problem -- the
function callback signature and its corresponding function argument
signatures must match *exactly* -- including the argument names. In
the above case, the "i" and "j" cannot be different, they must be the
same. The corrected version of the above would be

function callback( i : count ) {
    print fmt("i: %d", i);
}

function caller( f : function(i:count) ) {
    f(13);
}

event bro_init() {
    caller( callback );
}

.. which prints "i: 13" to stdout. Like I said in the subject, just an FYI.

Thanks,
Mike


On 11/30/07, Vern Paxson <vern at icir.org> wrote:
> > You could fake it up using table[string] of XYZ, where XYZ records include
> > a string that point back into the table.  Not pretty, but seems it should
> > work.
>
> (or, slightly easier, a table[count] of XYZ, along with a counter that you
> just increment every time you add a new node, which becomes its identifier)
>



More information about the Bro mailing list