Skip to content

RouteGetWithOptions: Add source address option#591

Merged
aboch merged 1 commit intovishvananda:masterfrom
taktv6:get_from
Nov 22, 2020
Merged

RouteGetWithOptions: Add source address option#591
aboch merged 1 commit intovishvananda:masterfrom
taktv6:get_from

Conversation

@taktv6
Copy link
Copy Markdown

@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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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