Skip to content
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

Fixes binhex/arch-qbittorrentvpn#120 #45

Merged
merged 1 commit into from
Nov 4, 2024

Conversation

davidgibbons
Copy link
Contributor

Arch ipcalc doesn't include network anymore:
$ ipcalc 10.0.0.1 255.255.255.255
Address: 10.0.0.1 00001010.00000000.00000000.00000001
Netmask: 255.255.255.255 = 32 11111111.11111111.11111111.11111111
Wildcard: 0.0.0.0 00000000.00000000.00000000.00000000
=>
Hostroute: 10.0.0.1 00001010.00000000.00000000.00000001
Hosts/Net: 1 Class A, Private Internet

The redhat package from Aur does.

@binhex
Copy link
Owner

binhex commented Nov 4, 2024

Rather than switching tool i think a tweak to the regex should suffice:-

ipcalc -b 10.0.0.1 255.255.255.0 | grep -P -o -m 1 "(?<=Netmask:).*" | grep -P -o -m 1 "\d{1,2}(\s+)?$"

works for me:-

[root@106f54fbed10 /]#  ipcalc -b 10.0.0.1 255.255.255.0 | grep -P -o -m 1 "(?<=Netmask:).*" | grep -P -o -m 1 "\d{1,2}(\s+)?$"
24   
[root@106f54fbed10 /]#  ipcalc -b 10.0.0.1 255.0.0.0 | grep -P -o -m 1 "(?<=Netmask:).*" | grep -P -o -m 1 "\d{1,2}(\s+)?$"
8        
[root@106f54fbed10 /]#  ipcalc -b 10.0.0.1 255.255.0.0 | grep -P -o -m 1 "(?<=Netmask:).*" | grep -P -o -m 1 "\d{1,2}(\s+)?$"
16     
[root@106f54fbed10 /]#  ipcalc -b 10.0.0.1 255.255.255.0 | grep -P -o -m 1 "(?<=Netmask:).*" | grep -P -o -m 1 "\d{1,2}(\s+)?$"
24   
[root@106f54fbed10 /]#  ipcalc -b 10.0.0.1 255.255.255.255 | grep -P -o -m 1 "(?<=Netmask:).*" | grep -P -o -m 1 "\d{1,2}(\s+)?$"
32 

@davidgibbons
Copy link
Contributor Author

davidgibbons commented Nov 4, 2024

From the linked issue, it sounded like the expected output would be the Netmask and Network together, not just a bit count mask. Testing more and I'm seeing it's a more complex issue. The network line is in there -- sometimes, but not if the mask is 255.255.255.255 I wonder if that should just be checked and trigger different logic?

Could do some version of `if $dockermask == "255.255.255.255"; "${docker_ip}/32" ? As far as I can tell it's just that specific case of ipcalc not behaving the same.

@binhex
Copy link
Owner

binhex commented Nov 4, 2024

From the linked issue, it sounded like the expected output would be the Netmask and Network together, not just a bit count mask. Testing more and I'm seeing it's a more complex issue. The network line is in there -- sometimes, but not if the mask is 255.255.255.255 I wonder if that should just be checked and trigger different logic?

Could do some version of `if d o c k e r m a s k == " 255.255 .255 .255 " ; " {docker_ip}/32" ? As far as I can tell it's just that specific case of ipcalc not behaving the same.

Yep i think you are correct, it does look like the issue is only for networks with a single host, as in /32, if that is the case (and it does look that way) then a single if statement should suffice as you suggested.

@binhex
Copy link
Owner

binhex commented Nov 4, 2024

Something like this:-

		# convert netmask into cidr format, strip leading spaces
		if [[ "${docker_mask}" == "255.255.255.255" ]]; then
			# edge case where ipcalc does not work for networks with a single host, so we specify the cidr mask manually
			docker_network_cidr="${docker_ip}/32"
		else
			docker_network_cidr=$(ipcalc "${docker_ip}" "${docker_mask}" | grep -P -o -m 1 "(?<=Network:)\s+[^\s]+" | sed -e 's/^[[:space:]]*//')
		fi

patch for ipcalc output is different with a 255.255.255.255 netmask.
@davidgibbons davidgibbons force-pushed the fix_docker_network_cidr branch from 88343b9 to 3a6ecc6 Compare November 4, 2024 18:23
@binhex binhex merged commit 0c490b2 into binhex:master Nov 4, 2024
@binhex
Copy link
Owner

binhex commented Nov 4, 2024

Thanks for your input

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.

2 participants