# Type conversion and table initialization

**URL:** <https://community.zeek.org/t/type-conversion-and-table-initialization/1135>\
**Category:** Zeek\
**Created:** [February 27, 2007, 11:25pm UTC](https://community.zeek.org/t/type-conversion-and-table-initialization/1135 "2007-02-27T23:25:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Dopheide1](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Mike\_Dopheide1](https://community.zeek.org/u/Mike_Dopheide1)\
**Post date:** [February 27, 2007, 11:25pm UTC](https://community.zeek.org/t/type-conversion-and-table-initialization/1135/1 "2007-02-27T23:25:08Z")

</div>

It's been a couple weeks since I had a problem, so now I've got two. 🙂

1) hex-string to addr type conversion

I've got a udp packet that contains an IP address in the packet  
contents[\*]. I can easily grab it with sub\_bytes() and end up with a  
string like "\x8d\x8e\xde!" [\*\*]. I'd like to convert that to an addr so I can do comparisons easily. After looking though the \*.bif.bro files for conversion functions I'm stuck. There's to\_addr(), but the string would need to actually be the IP address and not a hex representation.

2) table of set initialization (curiosity)

I have something like this that works:  
global myset: set[addr] = {192.168.1.1};  
global mytable: table[string] of set[addr] = {  
&nbsp;&nbsp;["blaa"] = myset,  
};

When I try to combine that into one it breaks:  
global mytable: table[string] of set[addr] = {  
&nbsp;&nbsp;["blaa"] = 192.168.1.1,  
};

bad tag in Val::CONVERTER (addr/table)

I read somewhere that 'bad tag' is an internal error and I should never see it. I saw it. 🙂

-Mike

[\*] -- It's a klog request for afs-kaserver3 through kaforwarder and  
fakeka. So the originating requester's IP is stored in the epoch time  
field of the RX packet. Whee!

[\*\*] -- 141.142.222.33

---

<div class="post-metadata">

**Author:** ![robin](https://yyz1.discourse-cdn.com/flex011/user_avatar/community.zeek.org/robin/32/599_2.png) [@robin](https://community.zeek.org/u/robin)\
**Post date:** [February 28, 2007, 5:39pm UTC](https://community.zeek.org/t/type-conversion-and-table-initialization/1135/2 "2007-02-28T17:39:55Z")

</div>

> 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.

> global myset: set[addr] = {192.168.1.1};  
> global mytable: table[string] of set[addr] = {  
> &nbsp;&nbsp;["blaa"] = myset,  
> };

Yeah, unfortunately this is the way to do it. Clumsy, but we don't  
have set constructors at the moment.

> &nbsp;&nbsp;["blaa"] = 192.168.1.1,

The problem is here that you're initialiazing a set[addr] with just  
an addr (rather than a set of addrs). The natural way would be:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["blaa"] = { 192.168.1.1 }

But that is not supported.

> I read somewhere that 'bad tag' is an internal error and I should never  
> see it.

That's true, at least in theory. 🙂 In practice, there are a couple  
of places where this can happen when types don't match. Not nice but  
hasn't been a high priority so far to fix.

Robin

---

<div class="post-metadata">

**Author:** ![Mark\_Dedlow](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mark\_Dedlow](https://community.zeek.org/u/Mark_Dedlow)\
**Post date:** [February 28, 2007, 10:06pm UTC](https://community.zeek.org/t/type-conversion-and-table-initialization/1135/3 "2007-02-28T22:06:07Z")

</div>

Robin Sommer 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int a,b,c,d;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int r;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = sscanf(str-\>CheckString(), "%x.%x.%x.%x", &a, &b, &c, &d);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( r != 4 )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_time("hex addr not parseable");  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new AddrVal(dotted\_to\_addr(fmt("%d.%d.%d.%d", a,b,c,d)));  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%}

You may need to tweak the format, eg, for the \x format, to something  
like "\\x%x\\x%x\\x%x\\x%x".

Mark

---

<div class="post-metadata">

**Author:** ![Mike\_Dopheide1](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Mike\_Dopheide1](https://community.zeek.org/u/Mike_Dopheide1)\
**Post date:** [February 28, 2007, 10:12pm UTC](https://community.zeek.org/t/type-conversion-and-table-initialization/1135/4 "2007-02-28T22:12:56Z")

</div>

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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char\* x = new char[16];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const u\_char\* sp = s-\>Bytes();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(s-\>Len() != 4){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf(x,"0.0.0.0");  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf(x,"%i.%i.%i.%i",sp[0],sp[1],sp[2],sp[3]);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Val\* ret = new AddrVal(x);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete [] x;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ret;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%}

Mark Dedlow wrote:

[rawstring\_to\_addr.patch](https://community.zeek.org/uploads/short-url/gX97zb01gjdncCtXfvetEkaNo4S.patch) (657 Bytes)

---

<div class="post-metadata">

**Author:** ![Mike\_Dopheide1](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Mike\_Dopheide1](https://community.zeek.org/u/Mike_Dopheide1)\
**Post date:** [July 11, 2007, 12:35am UTC](https://community.zeek.org/t/type-conversion-and-table-initialization/1135/5 "2007-07-11T00:35:56Z")

</div>

Would it be possible to get this added in before the 1.3 release?

Thanks,  
Mike

Mike Dopheide wrote:

---

<div class="post-metadata">

**Author:** ![Vern](https://yyz1.discourse-cdn.com/flex011/user_avatar/community.zeek.org/vern/32/630_2.png) [@Vern](https://community.zeek.org/u/Vern)\
**Post date:** [July 12, 2007, 11:01am UTC](https://community.zeek.org/t/type-conversion-and-table-initialization/1135/6 "2007-07-12T11:01:47Z")

</div>

> Would it be possible to get this added in before the 1.3 release?

Please try the appended and let me know if it works as you need.

&nbsp;&nbsp;&nbsp;&nbsp;Vern

Index: src/bro.bif

---

<div class="post-metadata">

**Author:** ![Mike\_Dopheide1](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Mike\_Dopheide1](https://community.zeek.org/u/Mike_Dopheide1)\
**Post date:** [July 12, 2007, 6:33pm UTC](https://community.zeek.org/t/type-conversion-and-table-initialization/1135/7 "2007-07-12T18:33:32Z")

</div>

Vern,

That works great. The only issue is that the packets I'm dealing with have it in host order rather than network order.

-Mike

Vern Paxson wrote:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/zeek/original/1X/f09d732bc2cc7c7cc7e35db67cf4e1d5233ce7a7.png) [@system](https://community.zeek.org/u/system)\
**Post date:** [May 6, 2022, 3:38pm UTC](https://community.zeek.org/t/type-conversion-and-table-initialization/1135/8 "2022-05-06T15:38:11Z")

</div>


