Index: src/bro.bif =================================================================== --- src/bro.bif (revision 5823) +++ src/bro.bif (working copy) @@ -2816,7 +2816,73 @@ return location; %} + +%%{ +#ifdef HAVE_LIBMAGIC +extern "C" { +#include +} +#endif +%%} +function identify_magic_descr%(data: string%): string + %{ + const char* descr = 0; + +#ifdef HAVE_LIBMAGIC + static bool libmagic_descr_initialized = false; + static magic_t magic_descr = 0; + + if( !libmagic_descr_initialized ) + { + libmagic_descr_initialized = true; + magic_descr = magic_open(MAGIC_NONE); + + if ( ! &magic_descr ) + { + error(fmt("can't init libmagic (description): %s", magic_error(magic_descr))); + } + else if ( magic_load(magic_descr, 0) < 0 ) + { + error(fmt("can't load magic file: %s", magic_error(magic_descr))); + magic_close(magic_descr); + magic_descr = 0; + } + } + descr = magic_buffer(magic_descr, data->Bytes(), data->Len()); +#endif + return new StringVal(descr ? descr : ""); + %} + +function identify_magic_mime%(data: string%): string + %{ + const char* mime = 0; + +#ifdef HAVE_LIBMAGIC + static bool libmagic_mime_initialized = false; + static magic_t magic_mime = 0; + + if( !libmagic_mime_initialized ) + { + libmagic_mime_initialized = true; + magic_mime = magic_open(MAGIC_MIME); + + if ( ! &magic_mime ) + { + error(fmt("can't init libmagic (mime): %s", magic_error(magic_mime))); + } + else if ( magic_load(magic_mime, 0) < 0 ) + { + error(fmt("can't load magic file: %s", magic_error(magic_mime))); + magic_close(magic_mime); + magic_mime = 0; + } + } + mime = magic_buffer(magic_mime, data->Bytes(), data->Len()); +#endif + return new StringVal(mime ? mime : ""); + %} + # Returns true if connection has been received externally. function is_external_connection%(c: connection%) : bool %{