this post was submitted on 15 Nov 2023
1012 points (95.7% liked)

Programmer Humor

19564 readers
1261 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 11 points 1 year ago (8 children)

And dupe check. 0.0.0.0 and 000.000.000.000 may both be valid, but they resolve the same

[–] [email protected] 3 points 1 year ago* (last edited 1 year ago) (5 children)

Definitely, tho if you store it as a u32 that is fixed magically. Because 1.2.3.4 and 1.02.003.04 both map to the same number.

What I mean by storing it as a u32 is to convert it to a number, similar to how the IP gets sent over the wire, so for v4:

octet[3] | octet[2] << 8 | octet[1] << 16 | octet[0] << 24

or in more human terms:

(fourth octet) + (third octet * 256) + (second octet * 256^2) + (first octet * 256^3)
[–] [email protected] 2 points 1 year ago (2 children)

Because 1.2.3.4 and 1.02.003.04 both map to the same number.

But 10.20.30.40 and 010.020.030.040 map to different numbers. It's often best to reject IPv4 addresses with leading zeroes to avoid the decimal vs. octal ambiguity.

[–] [email protected] 2 points 1 year ago (1 children)

I don't know why anyone would write their IPs in octal, but fair point

[–] [email protected] 2 points 1 year ago

It's not about how people write them, it's how parsers parse them. IPv4 has been around since 1982, and most parsers interpret leading zeros as octal.

load more comments (2 replies)
load more comments (4 replies)