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

RouteGetWithOptions: Add source address option #591

Merged
merged 1 commit into from
Nov 22, 2020
Merged

RouteGetWithOptions: Add source address option #591

merged 1 commit into from
Nov 22, 2020

Conversation

taktv6
Copy link

@taktv6 taktv6 commented Nov 11, 2020

No description provided.

@taktv6 taktv6 changed the title WIP: RouteGetWithOptions: Add source address option RouteGetWithOptions: Add source address option Nov 11, 2020
if family == FAMILY_V4 {
srcAddr = options.SrcAddr.To4()
} else {
srcAddr = options.SrcAddr.To16()
Copy link
Collaborator

Choose a reason for hiding this comment

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

To16 converts the IP address ip to a 16-byte representation

https://golang.org/pkg/net/#IP.To16

Is this what you want here?

Copy link
Author

Choose a reason for hiding this comment

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

When family is != FAMILY_v4 it is v6 and v6 is 128 bits aka 16 bytes. Why shouldn't I want that?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Given type IP []byte and var srcAddr []byte
When family != FAMILY_V4, srcAddr = options.SrcAddr.To16() is as good as srcAddr = options.SrcAddr

Copy link
Author

Choose a reason for hiding this comment

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

That assumption can be wrong though when a user specifies an IPv4 source address when doing a route lookup for an IPv6 target.

Sure, in the end it would of course just not work. But at least the Netlink message is correct this way.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That assumption can be wrong though when a user specifies an IPv4 source

As far as I know a func IPv4(a, b, c, d byte) IP and func ParseIP(<an IPv4 string here") IP will generate the same IPv4-in-IPv6 mapped 16 byte slice.
You need to try a bit harder, but yes you can create a 4 byte slice net.IP, by type converting a 4 byte slice:

b := []byte{10,10,10,1}
ip := net.IP(b)

although this is not the idiomatic way to create an IP in go.
If instead the caller passes a .To4() returned net.IP, then I'd say he intentionally wants things to fail.

This net.IP handling thing which internally stores a IPv4 address into a 16 byte slice net.IP variable, but then it also gives you a To4() method which will return a 4 byte slice net.IP variable is confusing. Looks like other people think the same golang/go#18804

We just need to be careful to not use the a.To16() != nil check as we use the a.To4() != nil check: The latter will tell whether a is a IPv4 address, while the former will not always tell you that ais a IPv6 address.

Anyhow, I see this unnecessary .To16() in several other places in this library.

@aboch
Copy link
Collaborator

aboch commented Nov 22, 2020

LGTM

@aboch aboch merged commit ffba2c8 into vishvananda:master Nov 22, 2020
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