You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ipv6utils.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { NativeModules } from 'react-native';
  2. import { RTCSessionDescription } from 'react-native-webrtc';
  3. /**
  4. * Synthesizes IPv6 addresses on iOS in order to support IPv6 NAT64 networks.
  5. *
  6. * @param {RTCSessionDescription} sdp - The RTCSessionDescription which
  7. * specifies the configuration of the remote end of the connection.
  8. * @private
  9. * @returns {Promise}
  10. */
  11. export function synthesizeIPv6Addresses(sdp) {
  12. return (
  13. new Promise(resolve => resolve(_synthesizeIPv6Addresses0(sdp)))
  14. .then(({ ips, lines }) =>
  15. Promise.all(Array.from(ips.values()))
  16. .then(() => _synthesizeIPv6Addresses1(sdp, ips, lines))
  17. ));
  18. }
  19. /* eslint-disable max-depth */
  20. /**
  21. * Synthesizes an IPv6 address from a specific IPv4 address.
  22. *
  23. * @param {string} ipv4 - The IPv4 address from which an IPv6 address is to be
  24. * synthesized.
  25. * @returns {Promise<?string>} A {@code Promise} which gets resolved with the
  26. * IPv6 address synthesized from the specified {@code ipv4} or a falsy value to
  27. * be treated as inability to synthesize an IPv6 address from the specified
  28. * {@code ipv4}.
  29. */
  30. const _synthesizeIPv6FromIPv4Address = (function() {
  31. // POSIX.getaddrinfo
  32. const { POSIX } = NativeModules;
  33. if (POSIX) {
  34. const { getaddrinfo } = POSIX;
  35. if (typeof getaddrinfo === 'function') {
  36. return ipv4 =>
  37. getaddrinfo(/* hostname */ ipv4, /* servname */ undefined)
  38. .then(([ { ai_addr: ipv6 } ]) => ipv6);
  39. }
  40. }
  41. // NAT64AddrInfo.getIPv6Address
  42. const { NAT64AddrInfo } = NativeModules;
  43. if (NAT64AddrInfo) {
  44. const { getIPv6Address } = NAT64AddrInfo;
  45. if (typeof getIPv6Address === 'function') {
  46. return getIPv6Address;
  47. }
  48. }
  49. // There's no POSIX.getaddrinfo or NAT64AddrInfo.getIPv6Address.
  50. return ip => Promise.resolve(ip);
  51. })();
  52. /**
  53. * Begins the asynchronous synthesis of IPv6 addresses.
  54. *
  55. * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
  56. * for which IPv6 addresses will be synthesized.
  57. * @private
  58. * @returns {{
  59. * ips: Map,
  60. * lines: Array
  61. * }}
  62. */
  63. function _synthesizeIPv6Addresses0(sessionDescription) {
  64. const sdp = sessionDescription.sdp;
  65. let start = 0;
  66. const lines = [];
  67. const ips = new Map();
  68. do {
  69. const end = sdp.indexOf('\r\n', start);
  70. let line;
  71. if (end === -1) {
  72. line = sdp.substring(start);
  73. // Break out of the loop at the end of the iteration.
  74. start = undefined;
  75. } else {
  76. line = sdp.substring(start, end);
  77. start = end + 2;
  78. }
  79. if (line.startsWith('a=candidate:')) {
  80. const candidate = line.split(' ');
  81. if (candidate.length >= 10 && candidate[6] === 'typ') {
  82. const ip4s = [ candidate[4] ];
  83. let abort = false;
  84. for (let i = 8; i < candidate.length; ++i) {
  85. if (candidate[i] === 'raddr') {
  86. ip4s.push(candidate[++i]);
  87. break;
  88. }
  89. }
  90. for (const ip of ip4s) {
  91. if (ip.indexOf(':') === -1) {
  92. ips.has(ip)
  93. || ips.set(ip, new Promise((resolve, reject) => {
  94. const v = ips.get(ip);
  95. if (v && typeof v === 'string') {
  96. resolve(v);
  97. } else {
  98. _synthesizeIPv6FromIPv4Address(ip).then(
  99. value => {
  100. if (!value
  101. || value.indexOf(':') === -1
  102. || value === ips.get(ip)) {
  103. ips.delete(ip);
  104. } else {
  105. ips.set(ip, value);
  106. }
  107. resolve(value);
  108. },
  109. reject);
  110. }
  111. }));
  112. } else {
  113. abort = true;
  114. break;
  115. }
  116. }
  117. if (abort) {
  118. ips.clear();
  119. break;
  120. }
  121. line = candidate;
  122. }
  123. }
  124. lines.push(line);
  125. } while (start);
  126. return {
  127. ips,
  128. lines
  129. };
  130. }
  131. /* eslint-enable max-depth */
  132. /**
  133. * Completes the asynchronous synthesis of IPv6 addresses.
  134. *
  135. * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
  136. * for which IPv6 addresses are being synthesized.
  137. * @param {Map} ips - A Map of IPv4 addresses found in the specified
  138. * sessionDescription to synthesized IPv6 addresses.
  139. * @param {Array} lines - The lines of the specified sessionDescription.
  140. * @private
  141. * @returns {RTCSessionDescription} A RTCSessionDescription that represents the
  142. * result of the synthesis of IPv6 addresses.
  143. */
  144. function _synthesizeIPv6Addresses1(sessionDescription, ips, lines) {
  145. if (ips.size === 0) {
  146. return sessionDescription;
  147. }
  148. for (let l = 0; l < lines.length; ++l) {
  149. const candidate = lines[l];
  150. if (typeof candidate !== 'string') {
  151. let ip4 = candidate[4];
  152. let ip6 = ips.get(ip4);
  153. ip6 && (candidate[4] = ip6);
  154. for (let i = 8; i < candidate.length; ++i) {
  155. if (candidate[i] === 'raddr') {
  156. ip4 = candidate[++i];
  157. (ip6 = ips.get(ip4)) && (candidate[i] = ip6);
  158. break;
  159. }
  160. }
  161. lines[l] = candidate.join(' ');
  162. }
  163. }
  164. return new RTCSessionDescription({
  165. sdp: lines.join('\r\n'),
  166. type: sessionDescription.type
  167. });
  168. }