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.1KB

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