Skip to content

Commit

Permalink
Better support for IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Feb 19, 2024
1 parent e8f7562 commit c494fc4
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/core/socket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package coaxial

import nettlesome.*
import parasite.*
import hypotenuse.*
import fulminate.*
import hieroglyph.*
import spectacular.*
Expand Down Expand Up @@ -247,12 +248,16 @@ object Bindable:
val packet = jn.DatagramPacket(array, 1472)
val socket = binding.receive(packet)
val address = packet.getSocketAddress.nn.asInstanceOf[jn.InetSocketAddress]
val ip = address.getAddress.nn match

val ip = (address.getAddress.nn: @unchecked) match
case ip: jn.Inet4Address =>
val bytes: Array[Byte] = ip.getAddress.nn
Ipv4(bytes(0), bytes(1), bytes(2), bytes(3))
case _ => ??? // FIXME


case ip: jn.Inet6Address =>
val bytes: Array[Byte] = ip.getAddress.nn
Ipv6(Long(bytes.take(8).immutable(using Unsafe)), Long(bytes.drop(8).immutable(using Unsafe)))

UdpPacket(array.slice(0, packet.getLength).immutable(using Unsafe), ip, UdpPort.unsafe(address.getPort))

def transmit(socket: jn.DatagramSocket, input: UdpPacket, response: UdpResponse): Unit = response match
Expand All @@ -261,10 +266,15 @@ object Bindable:
case UdpResponse.Reply(data) =>
val sender = input.sender

val ip: jn.InetAddress = input.sender match
case ip: Ipv4 => jn.InetAddress.getByAddress(Array[Byte](ip.byte0.toByte, ip.byte1.toByte, ip.byte2.toByte, ip.byte3.toByte)).nn
case _ => ??? // FIXME

val ip: jn.InetAddress = (input.sender: @unchecked) match
case ip: Ipv4 =>
val array = Array[Byte](ip.byte0.toByte, ip.byte1.toByte, ip.byte2.toByte, ip.byte3.toByte)
jn.InetAddress.getByAddress(array).nn

case ip: Ipv6 =>
val array = IArray.from(ip.highBits.bits.bytes ++ ip.lowBits.bits.bytes).mutable(using Unsafe)
jn.InetAddress.getByAddress(array).nn

val packet = jn.DatagramPacket(data.mutable(using Unsafe), data.length, ip, input.port.number)
socket.send(packet)

Expand Down

0 comments on commit c494fc4

Please sign in to comment.