2020-10-12 08:13:20 +00:00
|
|
|
import chronos
|
|
|
|
import ngtcp2
|
2020-10-07 12:31:53 +00:00
|
|
|
import ../packets
|
|
|
|
import ../openarray
|
2020-09-10 15:36:53 +00:00
|
|
|
import encrypt
|
|
|
|
import decrypt
|
|
|
|
import hp
|
|
|
|
import ids
|
2020-10-05 12:25:44 +00:00
|
|
|
import keys
|
2020-10-01 13:40:20 +00:00
|
|
|
import settings
|
2020-10-06 10:00:59 +00:00
|
|
|
import crypto
|
2020-10-07 09:27:58 +00:00
|
|
|
import connection
|
2020-10-12 08:13:20 +00:00
|
|
|
import path
|
2020-09-10 15:36:53 +00:00
|
|
|
|
2020-10-05 14:59:52 +00:00
|
|
|
let zeroKey = Key()
|
2020-09-10 15:36:53 +00:00
|
|
|
|
|
|
|
proc receiveClientInitial(connection: ptr ngtcp2_conn, dcid: ptr ngtcp2_cid, userData: pointer): cint {.cdecl.} =
|
2020-10-05 14:59:52 +00:00
|
|
|
connection.install0RttKey(zeroKey)
|
|
|
|
connection.installHandshakeKeys(zeroKey, zeroKey)
|
2020-10-05 14:16:23 +00:00
|
|
|
|
2020-09-10 15:36:53 +00:00
|
|
|
proc receiveCryptoData(connection: ptr ngtcp2_conn, level: ngtcp2_crypto_level, offset: uint64, data: ptr uint8, datalen: uint, userData: pointer): cint {.cdecl.} =
|
2020-10-07 08:14:27 +00:00
|
|
|
connection.handleCryptoData(toOpenArray(data, datalen))
|
2020-10-06 10:00:59 +00:00
|
|
|
connection.submitCryptoData()
|
2020-09-16 12:23:18 +00:00
|
|
|
ngtcp2_conn_handshake_completed(connection)
|
2020-09-10 15:36:53 +00:00
|
|
|
|
2020-09-15 11:11:15 +00:00
|
|
|
proc updateKey(conn: ptr ngtcp2_conn, rx_secret: ptr uint8, tx_secret: ptr uint8, rx_aead_ctx: ptr ngtcp2_crypto_aead_ctx, rx_iv: ptr uint8, tx_aead_ctx: ptr ngtcp2_crypto_aead_ctx, tx_iv: ptr uint8, current_rx_secret: ptr uint8, current_tx_secret: ptr uint8, secretlen: uint, user_data: pointer): cint {.cdecl} =
|
2020-10-05 14:02:34 +00:00
|
|
|
discard
|
2020-09-15 11:11:15 +00:00
|
|
|
|
2020-10-05 14:27:40 +00:00
|
|
|
proc handshakeCompleted(connection: ptr ngtcp2_conn, userData: pointer): cint {.cdecl.} =
|
2020-10-05 14:59:52 +00:00
|
|
|
connection.install1RttKeys(zeroKey, zeroKey)
|
2020-09-15 11:11:15 +00:00
|
|
|
|
2020-10-12 08:13:20 +00:00
|
|
|
proc newServerConnection(local, remote: TransportAddress, source, destination: ngtcp2_cid): Connection =
|
2020-09-10 15:36:53 +00:00
|
|
|
var callbacks: ngtcp2_conn_callbacks
|
|
|
|
callbacks.recv_client_initial = receiveClientInitial
|
|
|
|
callbacks.recv_crypto_data = receiveCryptoData
|
|
|
|
callbacks.decrypt = dummyDecrypt
|
|
|
|
callbacks.encrypt = dummyEncrypt
|
|
|
|
callbacks.hp_mask = dummyHpMask
|
|
|
|
callbacks.get_new_connection_id = getNewConnectionId
|
2020-09-15 11:11:15 +00:00
|
|
|
callbacks.update_key = updateKey
|
|
|
|
callbacks.handshake_completed = handshakeCompleted
|
2020-09-10 15:36:53 +00:00
|
|
|
|
2020-10-05 12:21:08 +00:00
|
|
|
var settings = defaultSettings()
|
2020-10-07 08:06:13 +00:00
|
|
|
settings.transport_params.original_dcid = destination
|
2020-09-10 15:36:53 +00:00
|
|
|
|
2020-10-07 11:55:47 +00:00
|
|
|
let id = randomConnectionId()
|
2020-10-12 08:13:20 +00:00
|
|
|
let path = newPath(local, remote)
|
2020-10-07 11:55:47 +00:00
|
|
|
|
2020-10-07 09:27:58 +00:00
|
|
|
var conn: ptr ngtcp2_conn
|
2020-09-15 11:13:40 +00:00
|
|
|
assert 0 == ngtcp2_conn_server_new(
|
2020-10-07 09:27:58 +00:00
|
|
|
addr conn,
|
2020-10-07 07:38:33 +00:00
|
|
|
unsafeAddr source,
|
2020-10-07 11:55:47 +00:00
|
|
|
unsafeAddr id,
|
2020-10-12 08:13:20 +00:00
|
|
|
path.toPathPtr,
|
2020-09-10 15:36:53 +00:00
|
|
|
cast[uint32](NGTCP2_PROTO_VER),
|
|
|
|
addr callbacks,
|
|
|
|
addr settings,
|
|
|
|
nil,
|
|
|
|
nil
|
|
|
|
)
|
2020-10-07 11:55:47 +00:00
|
|
|
|
2020-10-07 09:45:19 +00:00
|
|
|
Connection(conn: conn, path: path)
|
2020-10-07 08:32:11 +00:00
|
|
|
|
|
|
|
proc extractIds(datagram: Datagram): tuple[source, destination: ngtcp2_cid] =
|
|
|
|
var packetVersion: uint32
|
|
|
|
var packetDestinationId: ptr uint8
|
|
|
|
var packetDestinationIdLen: uint
|
|
|
|
var packetSourceId: ptr uint8
|
|
|
|
var packetSourceIdLen: uint
|
|
|
|
assert 0 == ngtcp2_pkt_decode_version_cid(addr packetVersion, addr packetDestinationId, addr packetDestinationIdLen, addr packetSourceId, addr packetSourceIdLen, unsafeAddr datagram[0], datagram.len.uint, DefaultConnectionIdLength)
|
|
|
|
result.source = connectionId(packetSourceId, packetSourceIdLen)
|
|
|
|
result.destination = connectionId(packetDestinationId, packetDestinationIdLen)
|
|
|
|
|
2020-10-12 08:13:20 +00:00
|
|
|
proc newServerConnection*(local, remote: TransportAddress, datagram: Datagram): Connection =
|
2020-10-07 08:32:11 +00:00
|
|
|
let (source, destination) = extractIds(datagram)
|
2020-10-12 08:13:20 +00:00
|
|
|
newServerConnection(local, remote, source, destination)
|