Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit da01296

Browse files
Merge pull request #10 from little-bear-labs/jt/con-427_munge
SDP munging
2 parents 688dc3e + f694dbf commit da01296

File tree

5 files changed

+92
-27
lines changed

5 files changed

+92
-27
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"type": "module",
99
"scripts": {
1010
"build": "aegir build",
11-
"clean": "rm -rfv node_modules dist *.lock *-lock.json ",
1211
"test": "aegir test",
1312
"format": "prettier --write src/*.ts"
1413
},
@@ -27,6 +26,7 @@
2726
"@libp2p/interfaces": "^3.0.3",
2827
"@libp2p/logger": "^2.0.0",
2928
"@libp2p/peer-id": "^1.1.15",
29+
"@multiformats/multiaddr": "../js-multiaddr/",
3030
"abortable-iterator": "^4.0.2",
3131
"it-merge": "^1.0.4",
3232
"p-defer": "^4.0.0",

src/error.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class WebRTCTransportError extends Error {
2+
constructor(msg: string) {
3+
super(msg);
4+
this.name = 'WebRTCTransportError';
5+
}
6+
}
7+
8+
export class InvalidArgumentError extends WebRTCTransportError {
9+
constructor(msg: string) {
10+
super(msg);
11+
this.name = 'WebRTC/InvalidArgumentError';
12+
}
13+
}

src/sdp.ts

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { InvalidArgumentError } from './error.js'
12
import { logger } from '@libp2p/logger';
23
import { Multiaddr } from '@multiformats/multiaddr';
34

45
const log = logger('libp2p:webrtc:sdp');
56

6-
const P_XWEBRTC: number = 0x115;
7+
const CERTHASH_CODE: number = 466;
78
const ANSWER_SDP_FORMAT: string = `
89
v=0
910
o=- 0 0 IN %s %s
@@ -37,38 +38,44 @@ function port(ma: Multiaddr): number {
3738
return ma.toOptions().port;
3839
}
3940
function certhash(ma: Multiaddr): string {
40-
let webrtc_value = ma
41-
.stringTuples()
42-
.filter((tup) => tup[0] == P_XWEBRTC)
41+
let tups = ma.stringTuples();
42+
let certhash_value = tups
43+
.filter((tup) => tup[0] == CERTHASH_CODE)
4344
.map((tup) => tup[1])[0];
44-
if (webrtc_value) {
45-
return webrtc_value.split('/')[1];
45+
if (certhash_value) {
46+
return certhash_value;
4647
} else {
47-
throw new Error("Couldn't find a webrtc component of multiaddr:" + ma.toString());
48+
throw new Error("Couldn't find a certhash component of multiaddr:" + ma.toString());
4849
}
4950
}
5051

5152
function ma2sdp(ma: Multiaddr, ufrag: string): string {
52-
return ANSWER_SDP_FORMAT.replace('/%s/', ipv(ma))
53-
.replace('/%s/', ip(ma))
54-
.replace('/%s/', ipv(ma))
55-
.replace('/%s/', ip(ma))
56-
.replace('/%s/', port(ma).toString())
57-
.replace('/%s/', ufrag)
58-
.replace('/%s/', ufrag)
59-
.replace('/%s/', certhash(ma));
53+
return ANSWER_SDP_FORMAT
54+
.replace('%s', ipv(ma))
55+
.replace('%s', ip(ma))
56+
.replace('%s', ipv(ma))
57+
.replace('%s', ip(ma))
58+
.replace('%d', port(ma).toString())
59+
.replace('%s', ufrag)
60+
.replace('%s', ufrag)
61+
.replace('%s', certhash(ma));
6062
}
6163

6264
export function fromMultiAddr(ma: Multiaddr, ufrag: string): RTCSessionDescriptionInit {
6365
return {
64-
type: 'offer',
66+
type: 'answer',
6567
sdp: ma2sdp(ma, ufrag),
6668
};
6769
}
6870

6971
export function munge(desc: RTCSessionDescriptionInit, ufrag: string): RTCSessionDescriptionInit {
70-
//TODO
71-
// desc.sdp.replaceAll(/^a=ice-ufrag=(.*)/, 'a=ice-ufrag=' + ufrag);
72-
// desc.sdp.replaceAll(/^a=ice-pwd=(.*)/, 'a=ice-pwd=' + ufrag);
73-
return desc;
72+
if (desc.sdp) {
73+
desc.sdp = desc.sdp
74+
.replace(/\na=ice-ufrag:[^\n]*\n/, '\na=ice-ufrag:' + ufrag + '\n')
75+
.replace(/\na=ice-pwd:[^\n]*\n/, '\na=ice-pwd:' + ufrag + '\n')
76+
;
77+
return desc;
78+
} else {
79+
throw new InvalidArgumentError("Can't munge a missing SDP");
80+
}
7481
}

test/node.js

-6
This file was deleted.

test/sdp.spec.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Multiaddr } from '@multiformats/multiaddr';
2+
import { expect } from 'chai';
3+
import * as underTest from '../src/sdp.js';
4+
5+
const an_sdp = `
6+
v=0
7+
o=- 0 0 IN IP4 192.168.0.152
8+
s=-
9+
c=IN IP4 192.168.0.152
10+
t=0 0
11+
m=application 2345 UDP/DTLS/SCTP webrtc-datachannel
12+
a=mid:0
13+
a=ice-options:ice2
14+
a=ice-ufrag:MyUserFragment
15+
a=ice-pwd:MyUserFragment
16+
a=fingerprint:mTXVsdGliYXNlIGlzIGF3ZXNvbWUhIFxvLw
17+
a=setup:actpass
18+
a=sctp-port:5000
19+
a=max-message-size:100000
20+
`;
21+
22+
describe('SDP creation', () => {
23+
it('handles simple blue sky easily enough', async () => {
24+
let ma = new Multiaddr('/ip4/192.168.0.152/udp/2345/webrtc/certhash/zYAjKoNbau5KiqmHPmSxYCvn66dA1vLmwbt');
25+
let ufrag = 'MyUserFragment';
26+
let sdp = underTest.fromMultiAddr(ma, ufrag);
27+
expect(sdp.sdp).to.equal(an_sdp);
28+
});
29+
});
30+
31+
describe('SDP munging', () => {
32+
it('does a simple replacement', () => {
33+
let result = underTest.munge({type:'answer',sdp: an_sdp},'someotheruserfragmentstring');
34+
expect(result.sdp).to.equal(`
35+
v=0
36+
o=- 0 0 IN IP4 192.168.0.152
37+
s=-
38+
c=IN IP4 192.168.0.152
39+
t=0 0
40+
m=application 2345 UDP/DTLS/SCTP webrtc-datachannel
41+
a=mid:0
42+
a=ice-options:ice2
43+
a=ice-ufrag:someotheruserfragmentstring
44+
a=ice-pwd:someotheruserfragmentstring
45+
a=fingerprint:mTXVsdGliYXNlIGlzIGF3ZXNvbWUhIFxvLw
46+
a=setup:actpass
47+
a=sctp-port:5000
48+
a=max-message-size:100000
49+
`);
50+
});
51+
});

0 commit comments

Comments
 (0)