nim-quic/tests/quic/helpers/client.nim

62 lines
2.0 KiB
Nim

import quic/openarray
import ngtcp2
import ids
import encrypt
import decrypt
import hp
import keys
import settings
import params
import crypto
let zeroKey = Key()
var randomId: ngtcp2_cid
proc clientInitial(connection: ptr ngtcp2_conn, user_data: pointer): cint {.cdecl.} =
connection.install0RttKey(zeroKey)
connection.submitCryptoData()
proc receiveCryptoData(connection: ptr ngtcp2_conn, level: ngtcp2_crypto_level, offset: uint64, data: ptr uint8, datalen: uint, userData: pointer): cint {.cdecl.} =
var params = decodeTransportParameters(toOpenArray(data, datalen))
params.original_dcid = randomId
assert 0 == ngtcp2_conn_set_remote_transport_params(connection, addr params)
connection.installHandshakeKeys(zeroKey, zeroKey)
ngtcp2_conn_handshake_completed(connection)
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} =
discard
proc handshakeCompleted(connection: ptr ngtcp2_conn, userData: pointer): cint {.cdecl.} =
connection.install1RttKeys(zeroKey, zeroKey)
proc setupClient*(path: ptr ngtcp2_path, sourceId: ptr ngtcp2_cid, destinationId: ptr ngtcp2_cid): ptr ngtcp2_conn =
randomId = destinationId[]
var callbacks: ngtcp2_conn_callbacks
callbacks.client_initial = clientInitial
callbacks.recv_crypto_data = receiveCryptoData
callbacks.decrypt = dummyDecrypt
callbacks.encrypt = dummyEncrypt
callbacks.hp_mask = dummyHpMask
callbacks.get_new_connection_id = getNewConnectionId
callbacks.recv_crypto_data = receiveCryptoData
callbacks.update_key = updateKey
callbacks.handshake_completed = handshakeCompleted
var settings = defaultSettings()
assert 0 == ngtcp2_conn_client_new(
addr result,
destinationId,
sourceId,
path,
cast[uint32](NGTCP2_PROTO_VER),
addr callbacks,
addr settings,
nil,
nil
)