Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into ci
Browse files Browse the repository at this point in the history
  • Loading branch information
lchenut committed Mar 7, 2024
2 parents 597b38f + d525da3 commit e4b98d3
Show file tree
Hide file tree
Showing 21 changed files with 1,732 additions and 213 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Nim-Webrtc

![Stability: experimental](https://img.shields.io/badge/stability-experimental-orange.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![License: Apache](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

A simple WebRTC stack first implemented for [libp2p WebRTC direct transport](https://github.com/libp2p/specs/blob/master/webrtc/webrtc-direct.md).
It uses a wrapper from two different C libraries:
- [usrsctp]() for the SCTP stack
- [mbedtls]() for the DTLS stack

## Usage

## Installation

## TODO

## License

Licensed and distributed under either of

* MIT license: [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT

or

* Apache License, Version 2.0, ([LICENSE-APACHEv2](LICENSE-APACHEv2) or http://www.apache.org/licenses/LICENSE-2.0)

at your option. This file may not be copied, modified, or distributed except according to those terms.
2 changes: 1 addition & 1 deletion build.sh → build_usrsctp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fi
cd "${root}/usrsctp" && ./bootstrap && ./configure && make && cd -

# add prelude
cat "${root}/prelude.nim" > "${outputFile}"
cat "${root}/prelude_usrsctp.nim" > "${outputFile}"

# assemble list of C files to be compiled
for file in `find ${root}/usrsctp/usrsctplib -name '*.c'`; do
Expand Down
24 changes: 24 additions & 0 deletions examples/ping.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import chronos, stew/byteutils
import ../webrtc/udp_connection
import ../webrtc/stun/stun_connection
import ../webrtc/dtls/dtls
import ../webrtc/sctp

proc main() {.async.} =
let laddr = initTAddress("127.0.0.1:4244")
let udp = UdpConn()
udp.init(laddr)
let stun = StunConn()
stun.init(udp, laddr)
let dtls = Dtls()
dtls.init(stun, laddr)
let sctp = Sctp()
sctp.init(dtls, laddr)
let conn = await sctp.connect(initTAddress("127.0.0.1:4242"), sctpPort = 13)
while true:
await conn.write("ping".toBytes)
let msg = await conn.read()
echo "Received: ", string.fromBytes(msg.data)
await sleepAsync(1.seconds)

waitFor(main())
30 changes: 30 additions & 0 deletions examples/pong.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import chronos, stew/byteutils
import ../webrtc/udp_connection
import ../webrtc/stun/stun_connection
import ../webrtc/dtls/dtls
import ../webrtc/sctp

proc sendPong(conn: SctpConn) {.async.} =
var i = 0
while true:
let msg = await conn.read()
echo "Received: ", string.fromBytes(msg.data)
await conn.write(("pong " & $i).toBytes)
i.inc()

proc main() {.async.} =
let laddr = initTAddress("127.0.0.1:4242")
let udp = UdpConn()
udp.init(laddr)
let stun = StunConn()
stun.init(udp, laddr)
let dtls = Dtls()
dtls.init(stun, laddr)
let sctp = Sctp()
sctp.init(dtls, laddr)
sctp.listen(13)
while true:
let conn = await sctp.accept()
asyncSpawn conn.sendPong()

waitFor(main())
25 changes: 25 additions & 0 deletions examples/sctp_both.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import chronos, stew/byteutils
import ../webrtc/sctp as sc

let sctp = Sctp.new(port = 4242)
proc serv(fut: Future[void]) {.async.} =
sctp.startServer(13)
fut.complete()
let conn = await sctp.listen()
echo "await read()"
let msg = await conn.read()
echo "read() finished"
echo "Receive: ", string.fromBytes(msg)
await conn.close()
sctp.stopServer()

proc main() {.async.} =
let fut = Future[void]()
asyncSpawn serv(fut)
await fut
let address = TransportAddress(initTAddress("127.0.0.1:4242"))
let conn = await sctp.connect(address, sctpPort = 13)
await conn.write("test".toBytes)
await conn.close()

waitFor(main())
14 changes: 14 additions & 0 deletions examples/sctp_client.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import chronos, stew/byteutils
import ../webrtc/sctp

proc main() {.async.} =
let
sctp = Sctp.new(port = 4244)
address = TransportAddress(initTAddress("127.0.0.1:4242"))
conn = await sctp.connect(address, sctpPort = 13)
await conn.write("test".toBytes)
let msg = await conn.read()
echo "Client read() finished ; receive: ", string.fromBytes(msg)
await conn.close()

waitFor(main())
13 changes: 13 additions & 0 deletions examples/sctp_server.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import chronos, stew/byteutils
import ../webrtc/sctp

proc main() {.async.} =
let sctp = Sctp.new(port = 4242)
sctp.startServer(13)
let conn = await sctp.listen()
let msg = await conn.read()
echo "Receive: ", string.fromBytes(msg)
await conn.close()
sctp.stopServer()

waitFor(main())
File renamed without changes.
25 changes: 25 additions & 0 deletions tests/testdatachannel.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ../webrtc/datachannel
import chronos/unittest2/asynctests
import binary_serialization

suite "DataChannel encoding":
test "DataChannelOpenMessage":
let msg = @[
0x03'u8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72]
check msg == Binary.encode(Binary.decode(msg, DataChannelMessage))
check Binary.decode(msg, DataChannelMessage).openMessage ==
DataChannelOpenMessage(
channelType: Reliable,
priority: 0,
reliabilityParameter: 0,
labelLength: 3,
protocolLength: 3,
label: @[102, 111, 111],
protocol: @[98, 97, 114]
)

test "DataChannelAck":
let msg = @[0x02'u8]
check msg == Binary.encode(Binary.decode(msg, DataChannelMessage))
check Binary.decode(msg, DataChannelMessage).messageType == Ack
14 changes: 14 additions & 0 deletions tests/teststun.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import ../webrtc/stun
import ./asyncunit
import binary_serialization

suite "Stun suite":
test "Stun encoding/decoding with padding":
let msg = @[ 0x00'u8, 0x01, 0x00, 0xa4, 0x21, 0x12, 0xa4, 0x42, 0x75, 0x6a, 0x58, 0x46, 0x42, 0x58, 0x4e, 0x72, 0x6a, 0x50, 0x4d, 0x2b, 0x00, 0x06, 0x00, 0x63, 0x6c, 0x69, 0x62, 0x70, 0x32, 0x70, 0x2b, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63, 0x2b, 0x76, 0x31, 0x2f, 0x62, 0x71, 0x36, 0x67, 0x69, 0x43, 0x75, 0x4a, 0x38, 0x6e, 0x78, 0x59, 0x46, 0x4a, 0x36, 0x43, 0x63, 0x67, 0x45, 0x59, 0x58, 0x58, 0x2f, 0x78, 0x51, 0x58, 0x56, 0x4c, 0x74, 0x39, 0x71, 0x7a, 0x3a, 0x6c, 0x69, 0x62, 0x70, 0x32, 0x70, 0x2b, 0x77, 0x65, 0x62, 0x72, 0x74, 0x63, 0x2b, 0x76, 0x31, 0x2f, 0x62, 0x71, 0x36, 0x67, 0x69, 0x43, 0x75, 0x4a, 0x38, 0x6e, 0x78, 0x59, 0x46, 0x4a, 0x36, 0x43, 0x63, 0x67, 0x45, 0x59, 0x58, 0x58, 0x2f, 0x78, 0x51, 0x58, 0x56, 0x4c, 0x74, 0x39, 0x71, 0x7a, 0x00, 0xc0, 0x57, 0x00, 0x04, 0x00, 0x00, 0x03, 0xe7, 0x80, 0x2a, 0x00, 0x08, 0x86, 0x63, 0xfd, 0x45, 0xa9, 0xe5, 0x4c, 0xdb, 0x00, 0x24, 0x00, 0x04, 0x6e, 0x00, 0x1e, 0xff, 0x00, 0x08, 0x00, 0x14, 0x16, 0xff, 0x70, 0x8d, 0x97, 0x0b, 0xd6, 0xa3, 0x5b, 0xac, 0x8f, 0x4c, 0x85, 0xe6, 0xa6, 0xac, 0xaa, 0x7a, 0x68, 0x27, 0x80, 0x28, 0x00, 0x04, 0x79, 0x5e, 0x03, 0xd8 ]
check msg == encode(StunMessage.decode(msg))

test "Error while decoding":
let msgLengthFailed = @[ 0x00'u8, 0x01, 0x00, 0xa4, 0x21, 0x12, 0xa4, 0x42, 0x75, 0x6a, 0x58, 0x46, 0x42, 0x58, 0x4e, 0x72, 0x6a, 0x50, 0x4d ]
expect AssertionDefect: discard StunMessage.decode(msgLengthFailed)
let msgAttrFailed = @[ 0x00'u8, 0x01, 0x00, 0x08, 0x21, 0x12, 0xa4, 0x42, 0x75, 0x6a, 0x58, 0x46, 0x42, 0x58, 0x4e, 0x72, 0x6a, 0x50, 0x4d, 0x2b, 0x28, 0x00, 0x05, 0x79, 0x5e, 0x03, 0xd8 ]
expect AssertionDefect: discard StunMessage.decode(msgAttrFailed)
9 changes: 7 additions & 2 deletions webrtc.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ version = "0.0.1"
author = "Status Research & Development GmbH"
description = "Webrtc stack"
license = "MIT"
#installDirs = @["usrsctp"]
installDirs = @["usrsctp", "webrtc"]

requires "nim >= 1.2.0",
"chronicles >= 0.10.2",
"chronos >= 3.0.6"
"chronos >= 3.0.6",
"https://github.com/status-im/nim-binary-serialization.git",
"https://github.com/status-im/nim-mbedtls.git"

proc runTest(filename: string) =
discard
Loading

0 comments on commit e4b98d3

Please sign in to comment.