[Bro] Type conversion and table initialization
Mike Dopheide
dopheide at ncsa.uiuc.edu
Wed Feb 28 14:12:56 PST 2007
Haha.. you beat me to it. Here's what I was about to submit (patch
attached):
# Returns an addr from a string
function rawstring_to_addr%(s: string%): addr
%{
char* x = new char[16];
const u_char* sp = s->Bytes();
if(s->Len() != 4){
sprintf(x,"0.0.0.0");
}else{
sprintf(x,"%i.%i.%i.%i",sp[0],sp[1],sp[2],sp[3]);
}
Val* ret = new AddrVal(x);
delete [] x;
return ret;
%}
Mark Dedlow wrote:
> Robin Sommer wrote:
>> On Tue, Feb 27, 2007 at 17:25 -0600, Mike Dopheide wrote:
>>
>>> I've got a udp packet that contains an IP address in the packet
>>> contents[*].
>>
>> Hmmm... can't think of any other way than adding a new built-in
>> ffunction specifically for this.
>
> I think this is as simple as adding to bro.bif:
>
> function hex_to_addr%(str: string%): addr
> %{
> int a,b,c,d;
> int r;
> r = sscanf(str->CheckString(), "%x.%x.%x.%x", &a, &b, &c, &d);
> if ( r != 4 )
> run_time("hex addr not parseable");
> return new AddrVal(dotted_to_addr(fmt("%d.%d.%d.%d", a,b,c,d)));
> %}
>
>
> You may need to tweak the format, eg, for the \x format, to something
> like "\\x%x\\x%x\\x%x\\x%x".
>
> Mark
>
>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: rawstring_to_addr.patch
Url: http://mailman.ICSI.Berkeley.EDU/pipermail/bro/attachments/20070228/779d155b/attachment.ksh
More information about the Bro
mailing list