nim-quic/tests/quic/helpers/server.nim

56 lines
1.9 KiB
Nim
Raw Normal View History

import quic/openarray
import ngtcp2
import encrypt
import decrypt
import hp
import ids
import keys
import settings
import params
import crypto
let zeroKey = Key()
proc receiveClientInitial(connection: ptr ngtcp2_conn, dcid: ptr ngtcp2_cid, userData: pointer): cint {.cdecl.} =
connection.install0RttKey(zeroKey)
connection.installHandshakeKeys(zeroKey, zeroKey)
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))
assert 0 == ngtcp2_conn_set_remote_transport_params(connection, addr params)
connection.submitCryptoData()
2020-09-16 12:23:18 +00:00
ngtcp2_conn_handshake_completed(connection)
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} =
discard
2020-09-15 11:11:15 +00:00
proc handshakeCompleted(connection: ptr ngtcp2_conn, userData: pointer): cint {.cdecl.} =
connection.install1RttKeys(zeroKey, zeroKey)
2020-09-15 11:11:15 +00:00
2020-09-16 12:19:58 +00:00
proc setupServer*(path: ptr ngtcp2_path, sourceId: ptr ngtcp2_cid, destinationId: ptr ngtcp2_cid): ptr ngtcp2_conn =
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-10-05 12:21:08 +00:00
var settings = defaultSettings()
assert 0 == ngtcp2_conn_server_new(
addr result,
2020-09-16 12:19:58 +00:00
destinationId,
sourceId,
2020-09-15 11:13:09 +00:00
path,
cast[uint32](NGTCP2_PROTO_VER),
addr callbacks,
addr settings,
nil,
nil
)