Skip to content

Commit 7136628

Browse files
authored
Merge pull request #648 from Comcast/topic/unix
Add UnixSocketAddress and GenSocketAddress, add Wildcard aliases for …
2 parents 10adf3b + 2a0d772 commit 7136628

File tree

6 files changed

+92
-2
lines changed

6 files changed

+92
-2
lines changed

build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
7676
if (tlIsScala3.value) Nil
7777
else List("org.scala-lang" % "scala-reflect" % scalaVersion.value % "provided")
7878
},
79-
scalacOptions := scalacOptions.value.filterNot(_ == "-source:3.0-migration")
79+
scalacOptions := scalacOptions.value.filterNot(_ == "-source:3.0-migration"),
80+
Compile / doc / scalacOptions ++= (if (scalaVersion.value.startsWith("2.")) Seq("-nowarn")
81+
else Nil)
8082
)
8183
.settings(
8284
libraryDependencies ++= Seq(
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
abstract class GenSocketAddress private[ip4s] () {
29+
30+
/** Downcasts this address to a `SocketAddress[IpAddress]`.
31+
*
32+
* @throws UnsupportedOperationException if this address is not a `SocketAddress[IpAddress]`
33+
*/
34+
def asIpUnsafe: SocketAddress[IpAddress] = this match {
35+
case addr @ SocketAddress(_: IpAddress, _) => addr.asInstanceOf[SocketAddress[IpAddress]]
36+
case other =>
37+
throw new UnsupportedOperationException(s"asIpUnsafe only supported on SocketAddress[IpAddress]: $other")
38+
}
39+
}

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: 8 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 GenSocketAddress with SocketAddressPlatform[A] {
2525
override def toString: String =
2626
host match {
2727
case _: Ipv6Address => s"[$host]:$port"
@@ -34,6 +34,13 @@ 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+
41+
/** Alias for 0.0.0.0:port. */
42+
def port(port: Port): SocketAddress[Host] = SocketAddress(Ipv4Address.Wildcard, port)
43+
3744
def fromString(value: String): Option[SocketAddress[Host]] =
3845
fromStringIp(value) orElse fromStringHostname(value) orElse fromStringIDN(value)
3946

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
implicit val order: Order[UnixSocketAddress] = Order.fromComparable[UnixSocketAddress]
28+
implicit val show: Show[UnixSocketAddress] = Show.fromToString[UnixSocketAddress]
29+
}

0 commit comments

Comments
 (0)