dht icon indicating copy to clipboard operation
dht copied to clipboard

bdecoder

Open ghazel opened this issue 7 years ago • 12 comments

A few notes:

  • fixes several bugs in the old parser
  • does not require NULL termination of the packet
  • does not use the heap
  • recursive, but tracks depth. could abort at a max depth, but even a full packet of [[[[... doesn't add much to the stack

ghazel avatar Feb 16 '18 20:02 ghazel

That looks very good.

Rather than walking the dictionary multiple times, once for each key, I suggest we walk it just once, filling in the structures on the fly, doing the loop in bdecode_find_key only once. This implies that either we make the low-level bdecoding code know about which keys we're interested in, or passing around a data structure that maps key to pointer to field (or key to offset within a struct, doesn't matter).

On a related note, I suggest creating a struct that contains all the data we're interested in, allocate it in the main loop (on the stack) and having parse_message fill in the fields rather than the current nonsense.

Please make sure that all of your functions are declared static unless they're prefixed with dht_.

(Feel free to push --force, Github deals well with that.)

jech avatar Feb 16 '18 21:02 jech

Line 2848, please use a while loop.

jech avatar Feb 16 '18 21:02 jech

The parser is designed to be progressive, always returning a pointer to the next character. Since memmem always started over as well, I left that alone.

If the code can be very careful to check for keys in lexographic order, it could simply update buf/buflen.

ghazel avatar Feb 16 '18 21:02 ghazel

I'm pretty sure your code will be both simpler and cleaner if you do a single pass over the dictionary. Something like

while(not at end) {
    foo = get_next_entry()
    if foo->name == "port"
        result->port = atoi(foo->value)
    else if foo->name == "q"
        etc
}

After you've reached the end of the dictionary, you've filled in all the fields of result, and you return success.

jech avatar Feb 16 '18 23:02 jech

Greg, can you please check my branch bdecode and tell me if it's okay to commit that under your name?

jech avatar Feb 17 '18 01:02 jech

I rebased on master and updated the PR. (and then came here and saw your edited comment :/)

Yes, feel free to use my name. Also make sure you get the want = -1 and while changes from my updated version.

ghazel avatar Feb 17 '18 09:02 ghazel

Also make sure you get the want = -1

Right.

and while changes

?

jech avatar Feb 17 '18 14:02 jech

and while changes

?

The change in bdecode_int to a while loop, as you requested here https://github.com/jech/dht/pull/31#issuecomment-366359583

ghazel avatar Feb 17 '18 20:02 ghazel

Anything left to merge this in?

ghazel avatar Mar 20 '18 22:03 ghazel

Anything left to merge this in?

I want to tweak it some more, but I'm really busy right now. Sorry.

jech avatar Mar 21 '18 00:03 jech