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 getRemoteAddress() #16

Merged
merged 3 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import epollcat.unsafe.EpollRuntime

import java.io.IOException
import java.net.ConnectException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.SocketAddress
import java.net.SocketOption
Expand All @@ -36,6 +37,7 @@ import scala.scalanative.annotation.stub
import scala.scalanative.libc.errno
import scala.scalanative.posix
import scala.scalanative.posix.netdbOps._
import scala.scalanative.posix.netinet.inOps._
import scala.scalanative.unsafe._
import scala.scalanative.unsigned._

Expand All @@ -45,7 +47,6 @@ final class EpollAsyncSocketChannel private (fd: Int) extends AsynchronousSocket

private[this] var _isOpen: Boolean = true
private[this] var outputShutdown: Boolean = false
private[this] var remoteAddress: SocketAddress = null
private[this] var readReady: Boolean = false
private[this] var readCallback: Runnable = null
private[this] var writeReady: Boolean = false
Expand Down Expand Up @@ -85,7 +86,22 @@ final class EpollAsyncSocketChannel private (fd: Int) extends AsynchronousSocket
this
}

def getRemoteAddress(): SocketAddress = remoteAddress
def getRemoteAddress(): SocketAddress = {
val addr = stackalloc[posix.netinet.in.sockaddr_in]()
val len = stackalloc[posix.sys.socket.socklen_t]()
!len = sizeof[posix.sys.socket.sockaddr].toUInt
if (posix
.sys
.socket
.getpeername(fd, addr.asInstanceOf[Ptr[posix.sys.socket.sockaddr]], len) == -1)
throw new IOException(s"getpeername: ${errno.errno}")
val port = posix.arpa.inet.htons(addr.sin_port).toInt
val addrBytes = addr.sin_addr.at1.asInstanceOf[Ptr[Byte]]
val inetAddr = InetAddress.getByAddress(
Array(addrBytes(0), addrBytes(1), addrBytes(2), addrBytes(3))
)
new InetSocketAddress(inetAddr, port)
}

@stub
def read[A](
Expand Down Expand Up @@ -214,7 +230,6 @@ final class EpollAsyncSocketChannel private (fd: Int) extends AsynchronousSocket
return handler.failed(new IOException(s"getsockopt: ${errno.errno}"), attachment)

if (!optval == 0) {
remoteAddress = remote
handler.completed(null, attachment)
} else {
val ex = !optval match {
Expand Down
23 changes: 23 additions & 0 deletions tests/shared/src/test/scala/epollcat/tcp/TcpSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class TcpSuite extends EpollcatSuite {

def localAddress: IO[SocketAddress] =
IO(ch.getLocalAddress())

def remoteAddress: IO[SocketAddress] =
IO(ch.getRemoteAddress())
}

object IOSocketChannel {
Expand Down Expand Up @@ -164,6 +167,26 @@ class TcpSuite extends EpollcatSuite {
}
}

test("local and remote addresses") {
IOServerSocketChannel.open.evalTap(_.bind(new InetSocketAddress("127.0.0.1", 0))).use {
server =>
IOSocketChannel.open.use { clientCh =>
server.localAddress.flatMap(clientCh.connect(_)) *>
server.accept.use { serverCh =>
for {
serverLocal <- serverCh.localAddress
serverRemote <- serverCh.remoteAddress
clientLocal <- clientCh.localAddress
clientRemote <- clientCh.remoteAddress
} yield {
assertEquals(clientRemote, serverLocal)
assertEquals(serverRemote, clientLocal)
}
}
}
}
}

test("read after shutdownInput") {
IOServerSocketChannel
.open
Expand Down