Skip to content

Commit 850f52a

Browse files
committed
Add UnixSocketAddress and GenSocketAddress, add Wildcard aliases for hosts and socket addresses
1 parent 86ff54b commit 850f52a

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2018 Comcast Cable Communications Management, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.comcast.ip4s
18+
19+
/** Supertype for types that specify address information for opening a socket.
20+
*
21+
* There are two built-in subtypes:
22+
* - [[SocketAddress]] - which specifies address and port info for IPv4/IPv6 sockets
23+
* - [[UnixSocketAddress]] - which specifies the path to a unix domain socket
24+
*
25+
* This trait is left open for extension, allowing other address types to be defined.
26+
* When using this trait, pattern match on the supported subtypes.
27+
*/
28+
trait GenSocketAddress

shared/src/main/scala/com/comcast/ip4s/Host.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ sealed abstract class IpAddress extends IpAddressPlatform with Host with Seriali
196196
/** Maps a type-preserving function across this IP address. */
197197
def transform(v4: Ipv4Address => Ipv4Address, v6: Ipv6Address => Ipv6Address): this.type
198198

199+
/** Returns true if this address is either 0.0.0.0 or ::. */
200+
def isWildcard: Boolean =
201+
fold(_ == Ipv4Address.Wildcard, _ == Ipv6Address.Wildcard)
202+
199203
/** Returns true if this address is in the multicast range. */
200204
def isMulticast: Boolean
201205

@@ -425,6 +429,9 @@ final class Ipv4Address private (protected val bytes: Array[Byte]) extends IpAdd
425429

426430
object Ipv4Address extends Ipv4AddressCompanionPlatform {
427431

432+
/** Wildcard IPv4 address - 0.0.0.0 */
433+
val Wildcard: Ipv4Address = fromBytes(0, 0, 0, 0)
434+
428435
/** First IP address in the IPv4 multicast range. */
429436
val MulticastRangeStart: Ipv4Address = fromBytes(224, 0, 0, 0)
430437

@@ -699,6 +706,10 @@ final class Ipv6Address private (protected val bytes: Array[Byte]) extends IpAdd
699706

700707
object Ipv6Address extends Ipv6AddressCompanionPlatform {
701708

709+
/** Wildcard IPv6 address - 0.0.0.0 */
710+
val Wildcard: Ipv6Address =
711+
fromBytes(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
712+
702713
/** First IP address in the IPv6 multicast range. */
703714
val MulticastRangeStart: Ipv6Address =
704715
fromBytes(255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

shared/src/main/scala/com/comcast/ip4s/Port.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ object Port {
4242
final val MinValue = 0
4343
final val MaxValue = 65535
4444

45+
final val Wildcard: Port = new Port(0)
46+
4547
def fromInt(value: Int): Option[Port] =
4648
if (value >= MinValue && value <= MaxValue) Some(new Port(value)) else None
4749

shared/src/main/scala/com/comcast/ip4s/SocketAddress.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import cats.syntax.all._
2121

2222
/** An IP address of the specified type and a port number. Used to describe the source or destination of a socket.
2323
*/
24-
final case class SocketAddress[+A <: Host](host: A, port: Port) extends SocketAddressPlatform[A] {
24+
final case class SocketAddress[+A <: Host](host: A, port: Port) extends SocketAddressPlatform[A] with GenSocketAddress {
2525
override def toString: String =
2626
host match {
2727
case _: Ipv6Address => s"[$host]:$port"
@@ -34,6 +34,10 @@ final case class SocketAddress[+A <: Host](host: A, port: Port) extends SocketAd
3434
}
3535

3636
object SocketAddress extends SocketAddressCompanionPlatform {
37+
38+
/** Alias for 0.0.0.0:0. */
39+
val Wildcard: SocketAddress[Host] = SocketAddress(Ipv4Address.Wildcard, Port.Wildcard)
40+
3741
def fromString(value: String): Option[SocketAddress[Host]] =
3842
fromStringIp(value) orElse fromStringHostname(value) orElse fromStringIDN(value)
3943

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2018 Comcast Cable Communications Management, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.comcast.ip4s
18+
19+
import cats.{Order, Show}
20+
21+
final case class UnixSocketAddress(path: String) extends GenSocketAddress with Ordered[UnixSocketAddress] {
22+
override def toString: String = path
23+
override def compare(that: UnixSocketAddress): Int = path.compare(that.path)
24+
}
25+
26+
object UnixSocketAddress {
27+
28+
/** Alias for the address with an empty path. */
29+
final val Wildcard: UnixSocketAddress = UnixSocketAddress("")
30+
31+
implicit val order: Order[UnixSocketAddress] = Order.fromComparable[UnixSocketAddress]
32+
implicit val show: Show[UnixSocketAddress] = Show.fromToString[UnixSocketAddress]
33+
}

0 commit comments

Comments
 (0)