Make usrsctp works & add a single treaded client

This commit is contained in:
Ludovic Chenut 2022-10-28 14:35:56 +02:00
parent 42ec914844
commit 7ba2452e94
No known key found for this signature in database
GPG Key ID: D9A59B1907F1D50C
5 changed files with 115 additions and 33 deletions

View File

@ -1,5 +1,6 @@
#!/bin/bash
root=$(dirname "$0")
outputFile="${root}/webrtc/usrsctp.nim"
# install nimterop, if not already installed
if ! [ -x "$(command -v toast)" ]; then
@ -7,28 +8,31 @@ if ! [ -x "$(command -v toast)" ]; then
fi
# run make on usrsctp sources
cd "${root}/usrsctp" && ./bootstrap && ./configure && make && cd "${root}"
cd "${root}/usrsctp" && ./bootstrap && ./configure && make && cd -
# add prelude
cat "${root}/prelude.nim" > "${root}/usrsctp.nim"
# dividing line
echo >> "${root}/usrsctp.nim"
cat "${root}/prelude.nim" > "${outputFile}"
# assemble list of C files to be compiled
for file in `find ${root}/usrsctp/usrsctplib -name '*.c'`; do
compile="${compile} --compile=${file}"
done
LIBCFLAGS="$(grep "^LIBCFLAGS = " "${root}/usrsctp/Makefile" | cut -d' ' -f3- | sed 's/-D/--defines=/g')"
# generate nim wrapper with nimterop
toast \
$compile \
--pnim \
--preprocess \
--noHeader \
--defines=NGTCP2_STATICLIB \
$LIBCFLAGS \
--replace=sockaddr=SockAddr \
--replace=SockAddr_storage=Sockaddr_storage \
--replace=SockAddr_in=Sockaddr_in \
--replace=SockAddr_conn=Sockaddr_conn \
--replace=socklen_t=SockLen \
--includeDirs="${root}/usrsctp/usrsctplib" \
"${root}/usrsctp/usrsctplib/usrsctp.h" >> "${root}/usrsctp.nim"
"${root}/usrsctp/usrsctplib/usrsctp.h" >> "${outputFile}"
sed -i 's/\bpassC\b/passc/g' "${outputFile}"

View File

@ -1,15 +1,11 @@
import os
import strformat
import strformat, os
# Socket definitions
import nativesockets
when defined(windows):
{.passl: "-lws2_32".}
# C include directories
# C include directory
const root = currentSourcePath.parentDir
const sourceInclude = root/"sources"/"lib"/"includes"
const buildInclude = root/"build"/"lib"/"includes"
const usrsctpInclude = root/"usrsctp"/"usrsctplib"
{.passc: fmt"-I{usrsctpInclude}".}
{.passc: fmt"-I{sourceInclude} -I{buildInclude}".}

6
webrtc.nimble Normal file
View File

@ -0,0 +1,6 @@
packageName = "webrtc"
version = "0.0.1"
author = "Status Research & Development GmbH"
description = ""
licence = ""
installDirs = @["usrsctp"]

79
webrtc/st_client.nim Normal file
View File

@ -0,0 +1,79 @@
import sequtils, bitops
import chronos, posix
import usrsctp
import stew/ranges/ptr_arith
const IPPROTO_SCTP = 132
let ta = initTAddress("127.0.0.1:4244")
let tar = initTAddress("127.0.0.1:4242")
proc discardFunc(transp: DatagramTransport, raddr: TransportAddress): Future[void] {.async.} = discard
proc connOutput(address: pointer,
buffer: pointer,
length: uint,
tos: uint8,
set_df: uint8): cint {.cdecl.} =
echo "====> connOutput: ", usrsctp_dumppacket(buffer, length, SCTP_DUMP_OUTBOUND)
let dg: ptr DatagramTransport = cast[ptr DatagramTransport](address)
proc testSend() {.async.} =
try:
let buf = @(buffer.makeOpenArray(byte, int(length)))
echo "START await sendTo START"
await sendTo(dg[], tar, buf, int(length))
echo "STOP await sendTo STOP"
except CatchableError as exc:
echo "Failure: ", exc.msg
asyncSpawn testSend()
echo "connOutput <===="
var connected = false
proc handleUpcall(sock: ptr socket, arg: pointer, length: cint) {.cdecl.} =
let e = usrsctp_get_events(sock)
echo "handleUpcall: event = ", e
if bitor(e, SCTP_EVENT_WRITE) != 0 and not connected:
echo "connect"
connected = true
elif bitor(e, SCTP_EVENT_READ) != 0:
echo "recv"
else:
echo "/!\\ ERROR /!\\"
proc printf(format: cstring) {.cdecl, varargs.} = echo "printf"
proc handleEvents(dg: DatagramTransport, sock: ptr socket, sconn_addr: pointer) {.async.} =
await sleepAsync(3.seconds)
proc main {.async, gcsafe.} =
let fut = newFuture[void]()
var p: pointer
proc clientMark(transp: DatagramTransport, raddr: TransportAddress): Future[void] {.async.} =
var msg = transp.getMessage()
echo "Client Mark: ", usrsctp_dumppacket(addr msg[0], uint(msg.len), SCTP_DUMP_INBOUND)
usrsctp_conninput(p, addr msg[0], uint(msg.len), 0)
var dg = newDatagramTransport(clientMark, remote=tar, local=ta)
p = cast[pointer](addr dg)
usrsctp_init_nothreads(0, connOutput, printf)
discard usrsctp_sysctl_set_sctp_ecn_enable(1)
usrsctp_register_address(p)
let sock = usrsctp_socket(AF_CONN, posix.SOCK_STREAM, IPPROTO_SCTP, nil, nil, 0, nil)
var on: int = 1
doAssert 0 == usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_RECVRCVINFO, addr on, sizeof(on).SockLen)
doAssert 0 == usrsctp_set_non_blocking(sock, 1)
doAssert 0 == usrsctp_set_upcall(sock, handleUpcall, nil)
var sconn: Sockaddr_conn
sconn.sconn_family = AF_CONN
sconn.sconn_port = htons(0)
sconn.sconn_addr = nil
doAssert 0 == usrsctp_bind(sock, cast[ptr SockAddr](addr sconn), sizeof(sconn).SockLen)
sconn.sconn_family = AF_CONN
sconn.sconn_port = htons(13)
sconn.sconn_addr = p
let connErr = usrsctp_connect(sock, cast[ptr SockAddr](addr sconn), sizeof(sconn).SockLen)
doAssert 0 == connErr or errno == EINPROGRESS, ($errno)
await handleEvents(dg, sock, sconn.sconn_addr)
waitFor(main())

View File

@ -1,22 +1,17 @@
import os
import strformat
import strformat, os
# Socket definitions
import nativesockets
when defined(windows):
{.passl: "-lws2_32".}
# C include directories
# C include directory
const root = currentSourcePath.parentDir
const sourceInclude = root/"sources"/"lib"/"includes"
const buildInclude = root/"build"/"lib"/"includes"
const usrsctpInclude = root/"usrsctp"/"usrsctplib"
{.passc: fmt"-I{sourceInclude} -I{buildInclude}".}
{.passc: fmt"-I{usrsctpInclude}".}
# Generated @ 2022-10-27T10:36:30+02:00
# Generated @ 2022-10-28T14:25:24+02:00
# Command line:
# /home/lchenut/.nimble/pkgs/nimterop-0.6.13/nimterop/toast --compile=./usrsctp/usrsctplib/netinet/sctp_input.c --compile=./usrsctp/usrsctplib/netinet/sctp_asconf.c --compile=./usrsctp/usrsctplib/netinet/sctp_pcb.c --compile=./usrsctp/usrsctplib/netinet/sctp_usrreq.c --compile=./usrsctp/usrsctplib/netinet/sctp_cc_functions.c --compile=./usrsctp/usrsctplib/netinet/sctp_auth.c --compile=./usrsctp/usrsctplib/netinet/sctp_userspace.c --compile=./usrsctp/usrsctplib/netinet/sctp_output.c --compile=./usrsctp/usrsctplib/netinet/sctp_callout.c --compile=./usrsctp/usrsctplib/netinet/sctp_crc32.c --compile=./usrsctp/usrsctplib/netinet/sctp_sysctl.c --compile=./usrsctp/usrsctplib/netinet/sctp_sha1.c --compile=./usrsctp/usrsctplib/netinet/sctp_timer.c --compile=./usrsctp/usrsctplib/netinet/sctputil.c --compile=./usrsctp/usrsctplib/netinet/sctp_bsd_addr.c --compile=./usrsctp/usrsctplib/netinet/sctp_peeloff.c --compile=./usrsctp/usrsctplib/netinet/sctp_indata.c --compile=./usrsctp/usrsctplib/netinet/sctp_ss_functions.c --compile=./usrsctp/usrsctplib/user_socket.c --compile=./usrsctp/usrsctplib/netinet6/sctp6_usrreq.c --compile=./usrsctp/usrsctplib/user_mbuf.c --compile=./usrsctp/usrsctplib/user_environment.c --compile=./usrsctp/usrsctplib/user_recv_thread.c --pnim --preprocess --noHeader --defines=NGTCP2_STATICLIB --replace=sockaddr=SockAddr --replace=SockAddr_storage=Sockaddr_storage --replace=socklen_t=SockLen --includeDirs=./usrsctp/usrsctplib ./usrsctp/usrsctplib/usrsctp.h
# /home/lchenut/.nimble/pkgs/nimterop-0.6.13/nimterop/toast --compile=./usrsctp/usrsctplib/netinet/sctp_input.c --compile=./usrsctp/usrsctplib/netinet/sctp_asconf.c --compile=./usrsctp/usrsctplib/netinet/sctp_pcb.c --compile=./usrsctp/usrsctplib/netinet/sctp_usrreq.c --compile=./usrsctp/usrsctplib/netinet/sctp_cc_functions.c --compile=./usrsctp/usrsctplib/netinet/sctp_auth.c --compile=./usrsctp/usrsctplib/netinet/sctp_userspace.c --compile=./usrsctp/usrsctplib/netinet/sctp_output.c --compile=./usrsctp/usrsctplib/netinet/sctp_callout.c --compile=./usrsctp/usrsctplib/netinet/sctp_crc32.c --compile=./usrsctp/usrsctplib/netinet/sctp_sysctl.c --compile=./usrsctp/usrsctplib/netinet/sctp_sha1.c --compile=./usrsctp/usrsctplib/netinet/sctp_timer.c --compile=./usrsctp/usrsctplib/netinet/sctputil.c --compile=./usrsctp/usrsctplib/netinet/sctp_bsd_addr.c --compile=./usrsctp/usrsctplib/netinet/sctp_peeloff.c --compile=./usrsctp/usrsctplib/netinet/sctp_indata.c --compile=./usrsctp/usrsctplib/netinet/sctp_ss_functions.c --compile=./usrsctp/usrsctplib/user_socket.c --compile=./usrsctp/usrsctplib/netinet6/sctp6_usrreq.c --compile=./usrsctp/usrsctplib/user_mbuf.c --compile=./usrsctp/usrsctplib/user_environment.c --compile=./usrsctp/usrsctplib/user_recv_thread.c --pnim --preprocess --noHeader --defines=SCTP_PROCESS_LEVEL_LOCKS --defines=SCTP_SIMPLE_ALLOCATOR --defines=__Userspace__ --replace=sockaddr=SockAddr --replace=SockAddr_storage=Sockaddr_storage --replace=SockAddr_in=Sockaddr_in --replace=SockAddr_conn=Sockaddr_conn --replace=socklen_t=SockLen --includeDirs=./usrsctp/usrsctplib ./usrsctp/usrsctplib/usrsctp.h
# const 'SCTP_PACKED' has unsupported value '__attribute__((packed))'
# const 'SCTP_INACTIVE' has unsupported value '0x0002 /* neither SCTP_ADDR_REACHABLE'
@ -25,8 +20,10 @@ const buildInclude = root/"build"/"lib"/"includes"
{.experimental: "codeReordering".}
{.passC: "-DNGTCP2_STATICLIB".}
{.passC: "-I./usrsctp/usrsctplib".}
{.passc: "-DSCTP_PROCESS_LEVEL_LOCKS".}
{.passc: "-DSCTP_SIMPLE_ALLOCATOR".}
{.passc: "-D__Userspace__".}
{.passc: "-I./usrsctp/usrsctplib".}
{.compile: "./usrsctp/usrsctplib/netinet/sctp_input.c".}
{.compile: "./usrsctp/usrsctplib/netinet/sctp_asconf.c".}
{.compile: "./usrsctp/usrsctplib/netinet/sctp_pcb.c".}
@ -280,15 +277,15 @@ type
verification_tag*: uint32
crc32c*: uint32
SockAddr_conn* {.bycopy.} = object
Sockaddr_conn* {.bycopy.} = object
sconn_family*: uint16
sconn_port*: uint16
sconn_addr*: pointer
sctp_sockstore* {.union, bycopy.} = object
sin*: SockAddr_in
sin6*: SockAddr_in6
sconn*: SockAddr_conn
sin*: Sockaddr_in
sin6*: Sockaddr_in6
sconn*: Sockaddr_conn
sa*: SockAddr
sctp_rcvinfo* {.bycopy.} = object