-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Refactor getBestPublicIp for all valid ips #1132
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1132 +/- ##
==========================================
- Coverage 41.43% 41.38% -0.06%
==========================================
Files 59 59
Lines 4245 4265 +20
Branches 2323 2338 +15
==========================================
+ Hits 1759 1765 +6
- Misses 990 996 +6
- Partials 1496 1504 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@sgourdas thx for the PR but can you please fix the code so the CI can pass? |
@kelson42 ready |
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.
Almost good, but
$ kiwix-serve --port=8080 -i 127.0.0.1 /home/kelson/Downloads/wikipedia_en_ray_charles_maxi_2023-12.zim
The Kiwix server is running and can be accessed in the local network at: http://192.168.0.168:8080 and http://::1:808
Should return only http://127.0.0.1:8080
as kiwix-serve should only listen on this IP
5276f1c
to
b2a7704
Compare
Still not working fine:
@sgourdas Please test all of this completly. |
@kelson42 I tested this and it was working fine for me. Tested again and I get:
|
Have you built both new kiwix tools and libkiwix versions? If yes, can you provide more details like if despite the printed message the server is available in browser? |
@sgourdas This will wait tomorrow, I kind of suspect something wrong in my testing because of the renaming of the |
b2a7704
to
eb3e76a
Compare
6803fa4
to
1a09276
Compare
@sgourdas Still wrong! You are "cheating" (taking for IPV4 the localhost instead of the public IP)!
|
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 think I started reviewing while the PR was being updated. My feeling is that my feedback doesn't pave the path to this PR being merged on next iteration, it seems like we will have to make at least a couple more back-and-forths.
Sorry for this. This PR is not up to standard and I have to revise all the changes. When it started, I expected it to be more straight forward and it has become very unstructured. Will try to get back ASAP. |
d87643d
to
97ad823
Compare
Seems to work now! How do we secure that only valid IP are given/accepted for |
Perfect. Currently, we are using |
@sgourdas This IP
|
What should be the proper behaviour for this case in your opinion? I think this should be a topic for another PR if it add features that were not present before. |
@sgourdas OK, I have open kiwix/kiwix-tools#709 |
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.
LGTM from a user perspective
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 think that we should close #1136 and bring its enhancement into this PR.
97ad823
to
ad3de64
Compare
5cf5c95
to
c8a11be
Compare
@@ -16,7 +16,7 @@ | |||
|
|||
namespace kiwix { | |||
|
|||
enum class IpMode { ipv4, ipv6, all }; | |||
enum class IpMode { ipv4, ipv6, all, flex }; |
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.
To me flex
is not an intuitive term for the intended mode of operation.
In the coding style that I got used to, enum
members names must be in UPPERCASE. In this case that would have helped to avoid the problem with auto
being a keyword in C++. Please rename existing members of IpMode
in a separate commit, then add a new member IpMode::AUTO
.
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 have the feeling I have missed something... what is his "glex" option!? Where does it come from?
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 (!ipv4 && !ipv6) { | ||
const std::string& addr = m_addr.addr.empty() ? m_addr.addr6 : m_addr.addr; | ||
std::cerr << "IP address " << addr << " is not valid" << std::endl; |
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 restore the previous text of the error message
bool validMode4 = (mode == IpMode::ipv4 || mode == IpMode::all); // Mode is valid to support ipv4 | ||
bool validMode6 = (mode == IpMode::ipv6 || mode == IpMode::all); // Mode is valid to support ipv6 |
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 think that these can be moved to the bottom of the function. The function will consist of two parts
- select the best public IPv4 and IPv6 addresses regardless of the mode requested (the logic of this will be slightly simpler compared to the current code)
- compose the result from those IPv4 and IPv6 addresses based on the requested mode
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 am debating whether we should do a mode check inside this function at all. The name getBestPublicIps
I think better suits a behavior where the whole IpAdress
struct is filled. IMO mode checking should be moved outside this function.
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.
Agree. In fact, when writing my previous comment I considered proposing to package the first part (covered by the first bullet) into a separate function IpAddress getBestPublicIps(void)
. I even think that you proposed something like that earlier.
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 even think that you proposed something like that earlier.
Indeed, you did.
for (const auto& [_, interfaceIps] : interfaces) { | ||
if (!bestPublicIps.addr.empty() && validMode4 && !interfaceIps.addr.empty() && interfaceIps.addr.find(prefix) == 0) | ||
bestPublicIps.addr = interfaceIps.addr; | ||
if (!bestPublicIps.addr6.empty() && validMode6 && !interfaceIps.addr6.empty()) |
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.
Is the interfaceIps.addr.find(prefix) == 0
check omitted intentionally?
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, the reason it is omitted is because we only provide v4 prefixes.
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.
Would it be beneficial to provide some for v6?
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, the reason it is omitted is because we only provide v4 prefixes.
The purpose of this piece of code is to select an IP address from a local network, though I am not sure why the 169.254
prefix (see https://en.wikipedia.org/wiki/Link-local_address) has not been included. Your selection of an IPv6 address fails to meet that criteria. Why don't you filter based on the same IPv4 check (interfaceIps.addr.find(prefix) == 0
) but use the IPv6 address (interfaceIps.addr6
)?
Would it be beneficial to provide some for v6?
According to https://en.wikipedia.org/wiki/Link-local_address#IPv6
- In IPv6, unicast link-local addresses are assigned from the block
fe80::/10
however, unfortunately, it seems like fe80
cannot be used as a prefix because only its higher 10 bits matter. If we decide to support it a slightly more complex check will be required. But let's not go after it now.
* @param addr the IP address to check. | ||
* @return true if the IP address is available on the system. | ||
*/ | ||
bool ipAvailable(const IpAddress& addr); |
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 function need to be a part of the public API?
IpAddress
can be a pair of addresses (one IPv4 and the other IPv6). How is this function supposed to work if both components are non-empty?
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 function need to be a part of the public API?
I think it can be a useful function to have available
IpAddress
can be a pair of addresses (one IPv4 and the other IPv6). How is this function supposed to work if both components are non-empty?
Current use case was to check only one ip (in the case of the user giving one). I will refactor it to take as a parameter the mode as well and check accordingly.
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 function need to be a part of the public API?
I think it can be a useful function to have available
When dealing with public API that's not a good argument. Public API is a commitment. Exposing something just because it's useful may lead to unjustified maintenance overhead (like having to deal with backward compatibility issues). Let's not bloat our public API without necessity.
Sorry for the lack of feedback here. Getting back tomorrow morning. |
This refactors
getBestPublicIp(bool)
togetBestPublicIps(IpMode)
so it can correctly return all attached ips for both protocols ipv4 and ipv6.Fixes kiwix/kiwix-tools#703
Fixes kiwix/kiwix-tools#709