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

Fix #39: getaddrinfo numeric error codes are now translated #40

Merged
merged 7 commits into from
Sep 11, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import java.net.StandardSocketOptions
import java.nio.channels.AsynchronousServerSocketChannel
import java.nio.channels.AsynchronousSocketChannel
import java.nio.channels.CompletionHandler
import java.nio.channels.UnsupportedAddressTypeException
import java.util.concurrent.Future
import scala.scalanative.annotation.stub
import scala.scalanative.libc.errno
Expand Down Expand Up @@ -85,15 +86,37 @@ final class EpollAsyncServerSocketChannel private (fd: Int)
hints,
addrinfo
)
if (rtn != 0)
throw new IOException(s"getaddrinfo: ${rtn}")

if (rtn != 0) {
val ex = if (rtn == posix.netdb.EAI_FAMILY) {
new UnsupportedAddressTypeException()
} else {
val msg = s"getaddrinfo: ${SocketHelpers.getGaiErrorMessage(rtn)}"
new IOException(msg)
}

throw ex
}
}

val bindRet = posix.sys.socket.bind(fd, (!addrinfo).ai_addr, (!addrinfo).ai_addrlen)
posix.netdb.freeaddrinfo(!addrinfo)

// posix.errno.EADDRNOTAVAIL becomes available in Scala Native 0.5.0
val EADDRNOTAVAIL =
if (LinktimeInfo.isLinux) 99
else if (LinktimeInfo.isMac) 49
else Int.MaxValue // punt, will never match an errno.

if (bindRet == -1) errno.errno match {
case e if e == posix.errno.EADDRINUSE =>
throw new BindException("Address already in use")
case e if e == EADDRNOTAVAIL =>
// Whis code may have to change when support for a new OS is added.
if (LinktimeInfo.isMac)
throw new BindException("Can't assign requested address")
else // Linux & a bet that an unknownd OS uses good grammer.
throw new BindException("Cannot assign requested address")
case other => throw new IOException(s"bind: $other")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import java.nio.ByteBuffer
import java.nio.channels.AsynchronousSocketChannel
import java.nio.channels.ClosedChannelException
import java.nio.channels.CompletionHandler
import java.nio.channels.UnsupportedAddressTypeException
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import scala.annotation.tailrec
Expand Down Expand Up @@ -195,10 +196,18 @@ final class EpollAsyncSocketChannel private (fd: Int)
hints,
addrinfo
)
if (rtn != 0) {
handler.failed(new IOException(s"getaddrinfo: $rtn"), attachment)
if (rtn == 0) {
true
} else {
val ex = if (rtn == posix.netdb.EAI_FAMILY) {
new UnsupportedAddressTypeException()
} else {
val msg = s"getaddrinfo: ${SocketHelpers.getGaiErrorMessage(rtn)}"
new IOException(msg)
}
handler.failed(ex, attachment)
false
} else true
}
}

if (!continue)
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/epollcat/internal/ch/SocketHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,9 @@ private[ch] object SocketHelpers {
new InetSocketAddress(inetAddr, port)
}

// Return text translation of getaddrinfo (gai) error code.
def getGaiErrorMessage(gaiErrorCode: CInt): String = {
fromCString(posix.netdb.gai_strerror(gaiErrorCode))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2022 Arman Bilge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package java.nio.channels

class UnsupportedAddressTypeException extends IllegalArgumentException
22 changes: 21 additions & 1 deletion tests/shared/src/test/scala/epollcat/TcpSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class TcpSuite extends EpollcatSuite {
.interceptMessage[ConnectException]("Connection refused")
}

test("BindException") {
test("BindException - EADDRINUSE") {
IOServerSocketChannel
.open
.evalTap(_.bind(new InetSocketAddress(0)))
Expand All @@ -173,6 +173,26 @@ class TcpSuite extends EpollcatSuite {
.interceptMessage[BindException]("Address already in use")
}

test("BindException - EADDRNOTAVAIL") {
// 240.0.0.1 is in reserved range 240.0.0.0/24. Used to elicit Exception.
IOServerSocketChannel
.open
.use { ch =>
for {
_ <- ch.bind(new InetSocketAddress("240.0.0.1", 0))
} yield ()
}
.interceptMessage[BindException] {
val osName = System.getProperty("os.name", "unknown").toLowerCase
if (osName.startsWith("linux"))
"Cannot assign requested address"
else if (osName.startsWith("mac"))
"Can't assign requested address"
else
"unknown operating system"
}
}

test("ClosedChannelException") {
IOServerSocketChannel
.open
Expand Down