-
Notifications
You must be signed in to change notification settings - Fork 8.2k
IP address parsing refactoring #1198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
My understanding is that this function is intended for usage convenience first of all, then why:
Why not just take null-terminated string? Especially that there's code like:
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). |
subsys/net/ip/utils.c
Outdated
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
include/net/net_ip.h
Outdated
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that makes sense.
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. |
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. |
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. |
|
@pfalcon Added a new patch to this set that fixes the numeric DNS query issue you reported. |
There was a problem hiding this 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.
subsys/net/ip/utils.c
Outdated
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
subsys/net/ip/utils.c
Outdated
There was a problem hiding this comment.
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.
subsys/net/ip/utils.c
Outdated
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure
subsys/net/ip/utils.c
Outdated
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
subsys/net/ip/utils.c
Outdated
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
subsys/net/ip/utils.c
Outdated
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
New version which adds more unit tests and updates the code according to comments. |
f85f840 to
cf2ee42
Compare
|
@lpereira could you review / re-approve the changes? |
There was a problem hiding this 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.
subsys/net/ip/utils.c
Outdated
There was a problem hiding this comment.
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.
subsys/net/ip/utils.c
Outdated
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 :)
Yes, that makes sense. I sent a new version that does that. Also added more test cases to unit tests. |
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]>
…hyrproject-rtos#1198) Signed-off-by: Cui Yan <[email protected]>
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.