Skip to content

Add Socket::IPAddress predicates and constants#6710

Merged
sdogruyol merged 5 commits intocrystal-lang:masterfrom
straight-shoota:jm/feature/socket-ipaddress-predicates
Oct 15, 2018
Merged

Add Socket::IPAddress predicates and constants#6710
sdogruyol merged 5 commits intocrystal-lang:masterfrom
straight-shoota:jm/feature/socket-ipaddress-predicates

Conversation

@straight-shoota
Copy link
Copy Markdown
Member

This PR adds methods to query information of IPAddress instances

  • Socket::IPAddress#loopback? returns whether the address is a loopback address (127.0.0.0/24 or ::1)
  • Socket::IPAddress#unspecified? returns whether the address is unspecified (0.0.0.0 or ::)

Default values for LOOPBACK, UNSPECIFIED and BROADCAST are also added as constants for both IPv4 and IPv6.

@straight-shoota straight-shoota force-pushed the jm/feature/socket-ipaddress-predicates branch from 9dff90a to d396d5d Compare September 12, 2018 22:17
@straight-shoota straight-shoota force-pushed the jm/feature/socket-ipaddress-predicates branch from d396d5d to fa4de53 Compare September 12, 2018 22:48
if family == Family::INET
@addr4.not_nil!.s_addr & 0x00000000ff_u32 == 0x0000007f_u32
else
@address == LOOPBACK6
Copy link
Copy Markdown
Contributor

@bew bew Sep 13, 2018

Choose a reason for hiding this comment

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

What if the address is 0000:0000:0000:0000:0000:0000:0000:0001 ? It should work too IMO

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, true. But maybe it's better to normalize the address...

@straight-shoota straight-shoota force-pushed the jm/feature/socket-ipaddress-predicates branch from 4915338 to 314ac75 Compare September 13, 2018 10:12
@straight-shoota straight-shoota force-pushed the jm/feature/socket-ipaddress-predicates branch from 314ac75 to 0a43ed2 Compare September 13, 2018 10:14
@straight-shoota straight-shoota force-pushed the jm/feature/socket-ipaddress-predicates branch from 26528aa to a15d4f1 Compare October 3, 2018 09:23
private def ipv6_addr8(addr : LibC::In6Addr)
{% if flag?(:darwin) || flag?(:openbsd) || flag?(:freebsd) %}
addr.__u6_addr.__u6_addr8
{% elsif flag?(:linux) && flag?(:musl) %}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

there's a double space here.
Also, can you indent the code in the macro conditions?

@straight-shoota straight-shoota force-pushed the jm/feature/socket-ipaddress-predicates branch from 747e21a to 8b0756a Compare October 3, 2018 13:25
puts "Listening on http://#{address}"
if host == "0.0.0.0"
puts "WARNING running playground with 0.0.0.0 is unsecure."
if address.unspecified?
Copy link
Copy Markdown
Member

@RX14 RX14 Oct 3, 2018

Choose a reason for hiding this comment

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

Perhaps a later PR to make this have better behaviour: test for not-loopback not 0.0.0.0 specifically

Copy link
Copy Markdown
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

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

Thank you @straight-shoota 👍

@sdogruyol sdogruyol merged commit cc22351 into crystal-lang:master Oct 15, 2018
@sdogruyol sdogruyol added this to the 0.27.0 milestone Oct 15, 2018
@straight-shoota straight-shoota deleted the jm/feature/socket-ipaddress-predicates branch October 15, 2018 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants