1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 1× 1× 1× 1× 1× 16× 16× 16× 16× 1× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 1× 1× 9× 9× 9× 90× 9× 9× 1× 7× 7× 7× 7× 1× 2× 2× 2× 1× 1× 1× 1× 1× 1× 1× | /** * Created by alykoshin on 7/16/14. */ 'use strict'; var debug = require('mini-debug'); var Enum = require('mini-enum'); //--------------------------------------------------------------------------------------------------------------------// // http://tools.ietf.org/html/rfc5245#section-15.1 // // candidate-attribute = "candidate" ":" foundation SP component-id SP // transport SP // priority SP // connection-address SP ;from RFC 4566 // port ;port from RFC 4566 // SP cand-type // [SP rel-addr] // [SP rel-port] // *(SP extension-att-name SP // extension-att-value) // // foundation = 1*32ice-char // component-id = 1*5DIGIT // transport = "UDP" / transport-extension // transport-extension = token ; from RFC 3261 // priority = 1*10DIGIT // cand-type = "typ" SP candidate-types // candidate-types = "host" / "srflx" / "prflx" / "relay" / token // rel-addr = "raddr" SP connection-address // rel-port = "rport" SP port // extension-att-name = byte-string ;from RFC 4566 // extension-att-value = byte-string // ice-char = ALPHA / DIGIT / "+" / "/" // // ------------------------------------------------------------------------------ // From RFC3261: // token = 1*(alphanum / "-" / "." / "!" / "%" / "*" // / "_" / "+" / "`" / "'" / "~" ) // // transport-param = "transport=" // ( "udp" / "tcp" / "sctp" / "tls" // / other-transport) // other-transport = token //------------------------------------------------------------------------------ // Chrome example: // a=candidate:1832966643 1 tcp 1509957375 192.168.1.32 0 typ host generation 0 // a=candidate:3587398871 1 udp 1845501695 123.123.123.123 46670 typ srflx raddr 192.168.1.32 rport 46670 generation 0 // a=candidate:3454638549 1 udp 33562367 123.12.123.123 64560 typ relay raddr 123.123.123.123 rport 48485 generation 0 // // Firefox example - no 'a=' part !!! : (and now for Chrome too) // candidate:0 1 UDP 2122252543 192.168.1.32 45166 typ host // candidate:2 1 UDP 92274687 123.123.12.123 49185 typ relay raddr 12.123.12.123 rport 49185 // // 2015-08-24 - new version of ICE in Chrome: // candidate:599991555 2 udp 2122260222 192.168.1.32 49827 typ host generation 0 // {"candidate":"candidate:3689538886 1 udp 2122199807 0124:4567:89ab:cdef:6deb:9894:734:f75f 32950 typ host generation 0","sdpMid":"video","sdpMLineIndex":1} /** * @type ICE_TYPE * @const */ var ICE_TYPE = Enum( { HOST: 'host', SRFLX: 'srflx', PRFLX: 'prflx', RELAY: 'relay' }); /** * @type ICE_TRANSPORT * @const */ var ICE_TRANSPORT = Enum({ TCP: 'tcp', UDP: 'udp' }); /** * @typedef {{foundation:string, component_id:string, transport: string, priority: string, localIP: string, localPort: string, type: string, remoteIP: string, remotePort: string, generation: string}} ParsedIce */ /** * Validate ICE Candidate Object (minimal) * * @param candObj * @returns boolean */ function validate(candObj) { var res = ICE_TYPE.check(candObj.type.toLowerCase()); res = res && ICE_TRANSPORT.check(candObj.transport.toLowerCase()); Iif (!res) { debug.warn('wrtc-ice-cand-parse: validate(): candObj validation failed'); } return res; } /** * * @param {string} candidateString * @returns {ParsedIce} */ function parse(candidateString) { // token = 1*(alphanum / "-" / "." / "!" / "%" / "*" // / "_" / "+" / "`" / "'" / "~" ) var token_re = '[0-9a-zA-Z\\-\\.!\\%\\*_\\+\\`\\\'\\~]+'; // ice-char = ALPHA / DIGIT / "+" / "/" var ice_char_re = '[a-zA-Z0-9\\+\\/]+'; // foundation = 1*32ice-char var foundation_re = ice_char_re; // component-id = 1*5DIGIT var component_id_re = '[0-9]{1,5}'; // transport = "UDP" / transport-extension // transport-extension = token ; from RFC 3261 var transport_re = token_re; // priority = 1*10DIGIT var priority_re = '[0-9]{1,10}'; // connection-address SP ; from RFC 4566 var connection_address_v4_re = '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}'; var connection_address_v6_re = '\\:?(?:[0-9a-fA-F]{0,4}\\:?)+'; // fde8:cd2d:634c:6b00:6deb:9894:734:f75f var connection_address_re = '(?:'+connection_address_v4_re +')|(?:' + connection_address_v6_re+')'; // port ; port from RFC 4566 var port_re = '[0-9]{1,5}'; // cand-type = "typ" SP candidate-types // candidate-types = "host" / "srflx" / "prflx" / "relay" / token var cand_type_re = token_re; // ICE_TYPE.HOST + '|' + // ICE_TYPE.SRFLX + '|' + // ICE_TYPE.PRFLX + '|' + // ICE_TYPE.RELAY ; var ICE_RE = '(?:a=)?candidate:(' + foundation_re + ')' + // candidate:599991555 // 'a=' not passed for Firefox (and now for Chrome too) '\\s' + '(' + component_id_re + ')' + // 2 '\\s' + '(' + transport_re + ')' + // udp '\\s' + '(' + priority_re + ')' + // 2122260222 '\\s' + '(' + connection_address_re + ')' + // 192.168.1.32 || fde8:cd2d:634c:6b00:6deb:9894:734:f75f '\\s' + '(' + port_re + ')' + // 49827 '\\s' + 'typ' + // typ '\\s' + '(' + cand_type_re + ')' + // host '(?:' + '\\s' + 'raddr' + '\\s' + '(' + connection_address_re + ')' + '\\s' + 'rport' + '\\s' + '(' + port_re + ')' + ')?' + '(?:' + '\\s' + 'generation' + // generation '\\s' + '(' + '\\d+' + ')' + // 0 ')?' ; var pattern = new RegExp( ICE_RE); var parsed = candidateString.match(pattern); // debug.log('parseIceCandidate(): candidateString:', candidateString); // debug.log('parseIceCandidate(): pattern:', pattern); // debug.log('parseIceCandidate(): parsed:', parsed); // Check if the string was successfully parsed if ( !parsed ) { debug.warn('parseIceCandidate(): parsed is empty: \'' + parsed + '\''); return null; } //var type = parsed[7] ? parsed[7] : null; //ICE_TYPE.check(type.toLowerCase()); // //var transport = parsed[3]; //ICE_TRANSPORT.check(transport.toLowerCase()); var propNames = [ 'foundation', 'component_id', 'transport', 'priority', 'localIP', 'localPort', 'type', 'remoteIP', 'remotePort', 'generation' ]; var candObj = {}; for (var i=0; i<propNames.length; i++) { candObj[ propNames[i] ] = parsed[i+1]; } validate(candObj); //var candObj = { // foundation: parsed[1], // component_id: parsed[2], // transport: transport, // priority: parsed[4], // localIP: parsed[5], // localPort: parsed[6], // type: type, // remoteIP: parsed[8], // remotePort: parsed[9], // generation: parsed[10] //}; return candObj; } /** * * @param {ParsedIce} iceCandObj * @param {{oldChrome:boolean}} [options] */ var stringify = function(iceCandObj, options) { options = options || {}; options.oldChrome = options.oldChrome || false; var s = (options.oldChrome ? 'a=' : '') + 'candidate:' + iceCandObj.foundation + ''+ ' ' + iceCandObj.component_id + '' + ' ' + iceCandObj.transport + '' + ' ' + iceCandObj.priority + '' + ' ' + iceCandObj.localIP + '' + ' ' + iceCandObj.localPort + '' + ' typ ' + iceCandObj.type + '' + (iceCandObj.remoteIP ? ' raddr ' + iceCandObj.remoteIP + '' : '') + (iceCandObj.remotePort ? ' rport ' + iceCandObj.remotePort + '' : '') + (iceCandObj.generation ? ' generation ' + iceCandObj.generation + '' : ''); return s; }; /** * * * @param {string} candStr * @returns {boolean} */ function isRelay( candStr ) { debug.warn('isRelay(): deprecated.'); var candObj = parse( candStr ); return (candObj.type && candObj.type.toLowerCase() === ICE_TYPE.RELAY); } //////////////////////////////////////////////////////////////////////////////// Eif (typeof module !== 'undefined') { module.exports = { parse: parse, stringify: stringify, validate: validate, isRelay: isRelay }; } Eif (typeof window !== 'undefined') { window.parseIceCandidate = parse; window.stringifyIceCandidate = stringify; window.validateIceCandidate = validate; window.isRelayIceCandidate = isRelay; } |