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

Canonical form for IP addresses #18

Closed
tegefaulkes opened this issue Apr 26, 2023 · 7 comments
Closed

Canonical form for IP addresses #18

tegefaulkes opened this issue Apr 26, 2023 · 7 comments
Assignees
Labels
development Standard development r&d:polykey:core activity 4 End to End Networking behind Consumer NAT Devices

Comments

@tegefaulkes
Copy link
Contributor

Specification

There are multiple ways to represent IP addresses. IPv4 specifically has 3 ways, IPv6 mapped form ::ffff:127.0.0.1, normal form, 127.0.0.1 and the hex form ::ffff:7f00:1. Theses 3 forms represent the same IPv4 address.

If we need to store this address in the database we can't use all 3 representations although they are equivalent. We need a canonical form for storing and manipulating the address.

To this end we need to create a helper class that stores the address as the canonical form, we can use the raw byte form representing an A address for IPv4 and an AAAA address for IPv6. Theses are 4 and 16 bytes respectively.

From the canonical byte form we can map the address to all supported formats.

Additional context

Tasks

  1. Create a canonical format for IP addresses for comparing and storing in a database.
  2. Create mapping functions for converting between formats using the canonical form as the in-between.
@tegefaulkes
Copy link
Contributor Author

As far as implementation for this goes. I'm thinking we have a Host class that extends a Uint8Array and a formatting transform methods to it.

The host address will be stored in the raw byte form, IPv4 will be 4 bytes while IPv6 is 16. From here we can map to and from the following formats. IPv4, IPv6 mapped IPv4, IPv6 and there was a hex format for IPv4 I think? Each of these formats will have a from and to method for them.

We can call this CanonicalHost?

@CMCDragonkai
Copy link
Member

I thought the ip-num library we are using already have classes for this.

I want to maintain a distinction between Host and Hostname. And the types should be kept simple.

This is because downstream systems will take only strings. And we need these opaque types to classify the different strings. Any class used for advanced operations is more of a transformer to and from these different forms.

@CMCDragonkai
Copy link
Member

Honestly I prefer mapping functions. Do not use classes or objects here. It will get confusing when doing serialisation or deserialisation.

@tegefaulkes
Copy link
Contributor Author

For IPv4 and IPv6 mapped IPv4 the canonical form will be the IPv4 dot decimal format. Comparisons should be done in this decimal format. For IPv4 the only format is the IPv6 hex format, IPv6 should be stored in that format.

As for the conversion methods we need ones for

  1. generic helper all -> canonical
  2. mapped hex -> IPv4 dec
  3. mapped dec -> IPv4 dec
  4. IPv4 dec -> mapped hex
  5. IPv4 dec -> mapped dec

tegefaulkes added a commit that referenced this issue May 10, 2023
* added conversions from V4 to V6 mapped, both Dec and hex types.
* Added isX for mappedV4 dec and hex.
added a helper toCanonicalIp to convert all format types to the canonical form.

* Related #18

[ci skip]
@tegefaulkes
Copy link
Contributor Author

I've expanded the transforms. Mostly added ipv4 -> ipv6 mapped hex and dec form, and isX checkers for the IPv6 mapped hex and dec form.

I also added a function that transforms all formats to the canonical format. It's pretty simple in that it converts all the mapped forms to IPv4 dec, and all normal IPv6 and IPv4 are left unchanged

I've updated the tests with the changes, adding new tests where needed,

I don't know if this fully covers this issue so I haven't resolved it yet. Let me know if I'm missing anything or mis-interpreted something. @CMCDragonkai

@tegefaulkes
Copy link
Contributor Author

I'm going to merge the changes and resolve this. Anything more should be addressed in polykey with respect to the node graph. I'll make a new issue in Polykey repo for this?

@CMCDragonkai
Copy link
Member

These functions may be more useful in PK than in js-quic. It all depends on integration. Which is why we must start now in order to iron out any problems.

CMCDragonkai pushed a commit that referenced this issue May 17, 2023
* added conversions from V4 to V6 mapped, both Dec and hex types.
* Added isX for mappedV4 dec and hex.
added a helper toCanonicalIp to convert all format types to the canonical form.

* Fixes #18

[ci skip]
@CMCDragonkai CMCDragonkai added the r&d:polykey:core activity 4 End to End Networking behind Consumer NAT Devices label Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development Standard development r&d:polykey:core activity 4 End to End Networking behind Consumer NAT Devices
Development

No branches or pull requests

2 participants