add debug output; use different certs and keys for client and server
This commit is contained in:
parent
db92c3f48f
commit
87239d45e7
49
quicp2p.nim
49
quicp2p.nim
|
@ -20,8 +20,10 @@ from openssl import DLLSSLName, EVP_PKEY
|
||||||
from posix import IOVec
|
from posix import IOVec
|
||||||
from strutils import parseUInt
|
from strutils import parseUInt
|
||||||
|
|
||||||
const certChainPath = "./certs/server-certchain.pem"
|
const serverCertChainPath = "./certs/server-certchain.pem"
|
||||||
const keyPath = "./certs/server-cert.key"
|
const serverKeyPath = "./certs/server-cert.key"
|
||||||
|
const clientCertChainPath = "./certs/server-certchain.pem"
|
||||||
|
const clientKeyPath = "./certs/server-cert.key"
|
||||||
|
|
||||||
type
|
type
|
||||||
QuicP2PContext = ref object
|
QuicP2PContext = ref object
|
||||||
|
@ -66,6 +68,7 @@ proc onReceiveReset(stream: ptr quicly_stream_t, err: cint) {.cdecl.} =
|
||||||
|
|
||||||
proc onServerReceive(stream: ptr quicly_stream_t, offset: csize_t, src: pointer,
|
proc onServerReceive(stream: ptr quicly_stream_t, offset: csize_t, src: pointer,
|
||||||
len: csize_t) {.cdecl.} =
|
len: csize_t) {.cdecl.} =
|
||||||
|
echo "onServerReceive"
|
||||||
if quicly_streambuf_ingress_receive(stream, offset, src, len) != 0:
|
if quicly_streambuf_ingress_receive(stream, offset, src, len) != 0:
|
||||||
return
|
return
|
||||||
let input = quicly_streambuf_ingress_get(stream)
|
let input = quicly_streambuf_ingress_get(stream)
|
||||||
|
@ -77,6 +80,7 @@ proc onServerReceive(stream: ptr quicly_stream_t, offset: csize_t, src: pointer,
|
||||||
|
|
||||||
proc onClientReceive(stream: ptr quicly_stream_t, offset: csize_t,
|
proc onClientReceive(stream: ptr quicly_stream_t, offset: csize_t,
|
||||||
src: pointer, len: csize_t) {.cdecl.} =
|
src: pointer, len: csize_t) {.cdecl.} =
|
||||||
|
echo "onClientReceive"
|
||||||
if quicly_streambuf_ingress_receive(stream, offset, src, len) != 0:
|
if quicly_streambuf_ingress_receive(stream, offset, src, len) != 0:
|
||||||
return
|
return
|
||||||
let input = quicly_streambuf_ingress_get(stream)
|
let input = quicly_streambuf_ingress_get(stream)
|
||||||
|
@ -109,13 +113,17 @@ proc usage() =
|
||||||
|
|
||||||
proc onServerStreamOpen(self: ptr quicly_stream_open_t,
|
proc onServerStreamOpen(self: ptr quicly_stream_open_t,
|
||||||
stream: ptr quicly_stream_t): cint {.cdecl.} =
|
stream: ptr quicly_stream_t): cint {.cdecl.} =
|
||||||
|
echo "onServerStreamOpen"
|
||||||
result = quicly_streambuf_create(stream, sizeof(quicly_streambuf_t).csize_t)
|
result = quicly_streambuf_create(stream, sizeof(quicly_streambuf_t).csize_t)
|
||||||
stream.callbacks = addr streamCallbacksServer
|
stream.callbacks = addr streamCallbacksServer
|
||||||
|
|
||||||
proc onClientStreamOpen(self: ptr quicly_stream_open_t,
|
proc onClientStreamOpen(self: ptr quicly_stream_open_t,
|
||||||
stream: ptr quicly_stream_t): cint {.cdecl.} =
|
stream: ptr quicly_stream_t): cint {.cdecl.} =
|
||||||
|
echo "onClientStreamOpen"
|
||||||
result = quicly_streambuf_create(stream, sizeof(quicly_streambuf_t).csize_t)
|
result = quicly_streambuf_create(stream, sizeof(quicly_streambuf_t).csize_t)
|
||||||
stream.callbacks = addr streamCallbacksClient
|
stream.callbacks = addr streamCallbacksClient
|
||||||
|
let msg = "hello server"
|
||||||
|
discard quicly_streambuf_egress_write(stream, msg.cstring, msg.len().csize_t)
|
||||||
|
|
||||||
proc handleMsg(ctx: QuicP2PContext, msg: string, isServer: bool) =
|
proc handleMsg(ctx: QuicP2PContext, msg: string, isServer: bool) =
|
||||||
var offset: csize_t = 0
|
var offset: csize_t = 0
|
||||||
|
@ -136,13 +144,15 @@ proc handleMsg(ctx: QuicP2PContext, msg: string, isServer: bool) =
|
||||||
conn = c
|
conn = c
|
||||||
break
|
break
|
||||||
if conn != nil:
|
if conn != nil:
|
||||||
|
echo "quicly_receive"
|
||||||
discard quicly_receive(conn, nil, addr sockAddr, addr decoded)
|
discard quicly_receive(conn, nil, addr sockAddr, addr decoded)
|
||||||
elif isServer:
|
elif isServer:
|
||||||
|
echo "quicly_accept"
|
||||||
discard quicly_accept(addr conn, addr ctx.quiclyCtx, nil, addr sockAddr,
|
discard quicly_accept(addr conn, addr ctx.quiclyCtx, nil, addr sockAddr,
|
||||||
addr decoded, nil, addr ctx.nextCid, nil)
|
addr decoded, nil, addr ctx.nextCid, nil)
|
||||||
ctx.connections.add(conn)
|
ctx.connections.add(conn)
|
||||||
|
|
||||||
proc initContext(sock: AsyncSocket,
|
proc initContext(sock: AsyncSocket, certChainPath: string, keyPath: string,
|
||||||
streamOpenCb: typeof(quicly_stream_open_t.cb)):
|
streamOpenCb: typeof(quicly_stream_open_t.cb)):
|
||||||
QuicP2PContext =
|
QuicP2PContext =
|
||||||
var tlsCtx = ptls_context_t(randomBytes: ptlsOpensslRandomBytes,
|
var tlsCtx = ptls_context_t(randomBytes: ptlsOpensslRandomBytes,
|
||||||
|
@ -177,33 +187,40 @@ proc sendPackets(ctx: QuicP2PContext) =
|
||||||
dgramsBuf.len().csize_t)
|
dgramsBuf.len().csize_t)
|
||||||
case sendResult:
|
case sendResult:
|
||||||
of 0:
|
of 0:
|
||||||
for d in dgrams:
|
if dgramCount > 0:
|
||||||
var sockLen = quicly_get_socklen(addr dstAddr.sa)
|
echo &"sending {dgramCount} datagrams"
|
||||||
asyncCheck sendTo(ctx.sock.getFd().AsyncFD, d.iov_base, d.iov_len.int,
|
for i in 0 .. dgramCount - 1:
|
||||||
addr dstAddr.sa, sockLen)
|
#echo "dstAddr.sa: ", dstAddr.sa
|
||||||
|
var sockLen = quicly_get_socklen(addr dstAddr.sa)
|
||||||
|
asyncCheck sendTo(ctx.sock.getFd().AsyncFD, dgrams[i].iov_base,
|
||||||
|
dgrams[i].iov_len.int, addr dstAddr.sa, sockLen)
|
||||||
of QUICLY_ERROR_FREE_CONNECTION:
|
of QUICLY_ERROR_FREE_CONNECTION:
|
||||||
ctx.connections.del(ctx.connections.find(c))
|
ctx.connections.del(ctx.connections.find(c))
|
||||||
quicly_free(c)
|
quicly_free(c)
|
||||||
else:
|
else:
|
||||||
raise newException(ValueError, &"quicly_send returned {sendResult}")
|
raise newException(ValueError, &"quicly_send returned {sendResult}")
|
||||||
|
|
||||||
proc receive(ctx: QuicP2PContext, sock: AsyncSocket, isServer: bool) {.async.} =
|
proc receive(ctx: QuicP2PContext, isServer: bool) {.async.} =
|
||||||
while true:
|
while true:
|
||||||
let msg = await sock.recv(BufferSize)
|
let msg = await ctx.sock.recv(BufferSize)
|
||||||
handleMsg(ctx, msg, isServer)
|
echo &"received {msg.len()} bytes"
|
||||||
|
if msg.len > 0:
|
||||||
|
handleMsg(ctx, msg, isServer)
|
||||||
|
|
||||||
proc main() =
|
proc main() =
|
||||||
var ctx: QuicP2PContext
|
var ctx: QuicP2PContext
|
||||||
let sock = newAsyncSocket(sockType = SOCK_DGRAM, protocol = IPPROTO_UDP)
|
let sock = newAsyncSocket(sockType = SOCK_DGRAM, protocol = IPPROTO_UDP,
|
||||||
|
buffered = false)
|
||||||
case paramCount():
|
case paramCount():
|
||||||
of 1:
|
of 1:
|
||||||
let portNumber = paramStr(1).parseUInt()
|
let portNumber = paramStr(1).parseUInt()
|
||||||
if portNumber > uint16.high:
|
if portNumber > uint16.high:
|
||||||
usage()
|
usage()
|
||||||
quit(1)
|
quit(1)
|
||||||
ctx = initContext(sock, onServerStreamOpen)
|
|
||||||
sock.bindAddr(Port(portNumber))
|
sock.bindAddr(Port(portNumber))
|
||||||
asyncCheck receive(ctx, sock, true)
|
ctx = initContext(sock, serverCertChainPath, serverKeyPath,
|
||||||
|
onServerStreamOpen)
|
||||||
|
asyncCheck receive(ctx, true)
|
||||||
|
|
||||||
of 2:
|
of 2:
|
||||||
let hostname = paramStr(1)
|
let hostname = paramStr(1)
|
||||||
|
@ -211,7 +228,8 @@ proc main() =
|
||||||
if portNumber > uint16.high:
|
if portNumber > uint16.high:
|
||||||
usage()
|
usage()
|
||||||
quit(1)
|
quit(1)
|
||||||
ctx = initContext(sock, onClientStreamOpen)
|
ctx = initContext(sock, clientCertChainPath, clientKeyPath,
|
||||||
|
onClientStreamOpen)
|
||||||
var conn: ptr quicly_conn_t
|
var conn: ptr quicly_conn_t
|
||||||
let hostent = getHostByName(hostname)
|
let hostent = getHostByName(hostname)
|
||||||
if hostent.addrList.len == 0:
|
if hostent.addrList.len == 0:
|
||||||
|
@ -231,7 +249,7 @@ proc main() =
|
||||||
ctx.connections.add(conn)
|
ctx.connections.add(conn)
|
||||||
var stream: ptr quicly_stream_t
|
var stream: ptr quicly_stream_t
|
||||||
discard quicly_open_stream(conn, addr stream, 0)
|
discard quicly_open_stream(conn, addr stream, 0)
|
||||||
asyncCheck receive(ctx, sock, false)
|
asyncCheck receive(ctx, false)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
usage()
|
usage()
|
||||||
|
@ -239,7 +257,6 @@ proc main() =
|
||||||
|
|
||||||
while true:
|
while true:
|
||||||
let nextTimeout = ctx.getRelativeTimeout()
|
let nextTimeout = ctx.getRelativeTimeout()
|
||||||
echo "nextTimeout: ", nextTimeout
|
|
||||||
poll(nextTimeout)
|
poll(nextTimeout)
|
||||||
ctx.sendPackets()
|
ctx.sendPackets()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue