Skip to content

Conversation

@jukkar
Copy link
Member

@jukkar jukkar commented Aug 20, 2017

As there is more uses for IP address parsing, create a generic net_ipaddr_parse() function for it and replace current functionality in DNS resolver by it. Update the unit tests to test the parsing function.

@pfalcon
Copy link
Contributor

pfalcon commented Aug 21, 2017

My understanding is that this function is intended for usage convenience first of all, then why:

@param str_len Length of the string to be parsed.

Why not just take null-terminated string?

Especially that there's code like:

ptr = strstr(str, "]");

This won't work properly with given length (e.g. if we want to parse address is a larger buffer, it may find "]" outside the string we want to parse).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this port parsing be rewritten somehow to be common for both IPv6 & IPv4, instead of being duplicated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, I did not see too much reason to create a function for this as it is just couple of lines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this should specify explicitly what will be on sockaddr if port is not specified (e.g. port field set to 0, or yet better not touched, to let the caller to initialize it to default value prior to call).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that makes sense.

@pfalcon
Copy link
Contributor

pfalcon commented Aug 21, 2017

@jukkar: Also, as DNS was mentioned, pls have a look at GH-3992 when you get a chance (implementing that definitely goes to 1.10).

@jukkar
Copy link
Member Author

jukkar commented Aug 21, 2017

Why not just take null-terminated string?

If we want to parse address from middle of a string. This way we do not need to copy to temporary buffer and parse that.

@jukkar
Copy link
Member Author

jukkar commented Aug 21, 2017

This won't work properly with given length (e.g. if we want to parse address is a larger buffer, it may find "]" outside the string we want to parse).

The lengths are checked later in the function. I have unit tests that should check this.

@jukkar
Copy link
Member Author

jukkar commented Aug 21, 2017

The lengths are checked later in the function. I have unit tests that should check this.

After checking the unit tests, I did not actually test this condition. I will send a new version that addresses this issue.

@jukkar
Copy link
Member Author

jukkar commented Aug 21, 2017

Also, as DNS was mentioned, pls have a look at GH-3992 when you get a chance (implementing that definitely goes to 1.10).

As I commented in bug, we should fix the DNS resolver. One simple solution we could use, is to call this new net_ipaddr_parse() in DNS resolver, and if that fails, then try to resolve from DNS server.

@jukkar jukkar added this to the v1.9 milestone Aug 22, 2017
@jukkar
Copy link
Member Author

jukkar commented Aug 22, 2017

@pfalcon Added a new patch to this set that fixes the numeric DNS query issue you reported.

pfalcon
pfalcon previously approved these changes Aug 22, 2017
Copy link
Contributor

@pfalcon pfalcon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes and adding numeric addresses support to DNS query! This fails CI so, but let me proactively approve this, as I'm going on vacation the next day.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errno has to be set to 0 before calling strtol(). Might be a good idea to use strtoul() instead since it's unsigned anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we do not need errno when checking the conversion failures if we use endptr as suggested in strtoul() manual page.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While int and long are the same size, it's better to use the same return type for strtol() here: long int.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If using strtoul() instead, you might change the range check with a cast: if ((unsigned long)(unsigned short)tmp != tmp). This often generates tighter code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use memchr() here: since there's a str_len parameter, a user might pass a non-zero-delimited string. Moreover, strstr() is overkill to scan for a single character.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, we can also then remove the second ptr check in next line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Why not a for (count = i = 0; str[i]; i++) ... instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and "for (count = i = 0; str[i] && i < str_len; i++) {" is even better as it checks the string length too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these always servers? Wouldn't host be better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left over when the code was in DNS resolve library, I will convert to "host" instead.

@jukkar
Copy link
Member Author

jukkar commented Aug 23, 2017

New version which adds more unit tests and updates the code according to comments.

@jukkar jukkar mentioned this pull request Aug 28, 2017
@jukkar jukkar force-pushed the ipaddr-parse branch 2 times, most recently from f85f840 to cf2ee42 Compare August 29, 2017 19:51
@jukkar
Copy link
Member Author

jukkar commented Aug 29, 2017

@lpereira could you review / re-approve the changes?

Copy link
Member

@lpereira lpereira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could net_ipaddr_parse() be either split into smaller functions, with one per configuration variant, or at least split between parse_ipv4 and parse_ipv6 functions? The amount of copy-paste in parsing code worries me a bit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This passed from the other review, but strnlen() should be used here, since str is not guaranteed to have a NUL terminator. Other uses of strlen() here should be changed. Apparently, minimal libc does not have a strnlen() implementation, but it's trivial to add one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have alloca() support? If so, these things copying str into ipaddr could become just a call to strndupa(). Soletta has a simple implementation that you can borrow (same license, Intel owns the copyright).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not introduce yet another dependecy here :)

@jukkar
Copy link
Member Author

jukkar commented Aug 30, 2017

Could net_ipaddr_parse() be either split into smaller functions, with one per configuration variant, or at least split between parse_ipv4 and parse_ipv6 functions? The amount of copy-paste in parsing code worries me a bit.

Yes, that makes sense. I sent a new version that does that. Also added more test cases to unit tests.

jukkar added 4 commits August 30, 2017 16:06
The net_ipaddr_parse() will take a string with optional port
number and convert its information into struct sockaddr.
The format of the IP string can be:
     192.0.2.1:80
     192.0.2.42
     [2001:db8::1]:8080
     [2001:db8::2]
     2001:db::42

Signed-off-by: Jukka Rissanen <[email protected]>
Add IPv4 and IPv6 address parsing tests.

Signed-off-by: Jukka Rissanen <[email protected]>
This commit removes IP address parsing from DNS init and
replaces it by call to net_ipaddr_parse().

Signed-off-by: Jukka Rissanen <[email protected]>
If the query name is already in numeric format, there is no
need to send the query string to DNS server as we can just
convert it ourselves.

Jira: ZEP-2562

Signed-off-by: Jukka Rissanen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants