您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

POSIX.m 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright @ 2017-present Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "RCTBridgeModule.h"
  17. #include <arpa/inet.h>
  18. #include <netdb.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. @interface POSIX : NSObject<RCTBridgeModule>
  22. @end
  23. @implementation POSIX
  24. RCT_EXPORT_MODULE();
  25. RCT_EXPORT_METHOD(getaddrinfo:(NSString *)hostname
  26. resolve:(RCTPromiseResolveBlock)resolve
  27. reject:(RCTPromiseRejectBlock)reject) {
  28. int err;
  29. struct addrinfo *res;
  30. NSString *rejectCode;
  31. if (0 == (err = getaddrinfo(hostname.UTF8String, NULL, NULL, &res))) {
  32. int af = res->ai_family;
  33. struct sockaddr *sa = res->ai_addr;
  34. void *addr;
  35. switch (af) {
  36. case AF_INET:
  37. addr = &(((struct sockaddr_in *) sa)->sin_addr);
  38. break;
  39. case AF_INET6:
  40. addr = &(((struct sockaddr_in6 *) sa)->sin6_addr);
  41. break;
  42. default:
  43. addr = NULL;
  44. break;
  45. }
  46. if (addr) {
  47. char v[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
  48. if (inet_ntop(af, addr, v, sizeof(v))) {
  49. resolve([NSString stringWithUTF8String:v]);
  50. } else {
  51. err = errno;
  52. rejectCode = @"inet_ntop";
  53. }
  54. } else {
  55. err = EAFNOSUPPORT;
  56. rejectCode = @"EAFNOSUPPORT";
  57. }
  58. freeaddrinfo(res);
  59. } else {
  60. rejectCode = @"getaddrinfo";
  61. }
  62. if (0 != err) {
  63. NSError *error
  64. = [NSError errorWithDomain:NSPOSIXErrorDomain
  65. code:err
  66. userInfo:nil];
  67. reject(rejectCode, error.localizedDescription, error);
  68. }
  69. }
  70. @end