-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into ci
- Loading branch information
Showing
21 changed files
with
1,732 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.