Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

nslookup.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2012 Patrick Plenefisch <phplenefisch@wpi.edu>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <ipxe/resolv.h>
  25. #include <ipxe/tcpip.h>
  26. #include <ipxe/monojob.h>
  27. #include <ipxe/settings.h>
  28. #include <usr/nslookup.h>
  29. /** @file
  30. *
  31. * Standalone name resolution
  32. *
  33. */
  34. /** A name resolution request */
  35. struct nslookup {
  36. /** Reference count for this object */
  37. struct refcnt refcnt;
  38. /** Job control interface */
  39. struct interface job;
  40. /** Data transfer interface */
  41. struct interface resolver;
  42. /** Setting name */
  43. const char *setting_name;
  44. };
  45. /**
  46. * Terminate name resolution
  47. *
  48. * @v nslookup Name resolution request
  49. * @v rc Reason for termination
  50. */
  51. static void nslookup_close ( struct nslookup *nslookup, int rc ) {
  52. /* Shut down interfaces */
  53. intf_shutdown ( &nslookup->resolver, rc );
  54. intf_shutdown ( &nslookup->job, rc );
  55. }
  56. /**
  57. * Handle resolved name
  58. *
  59. * @v nslookup Name resolution request
  60. * @v sa Completed socket address
  61. */
  62. static void nslookup_resolv_done ( struct nslookup *nslookup,
  63. struct sockaddr *sa ) {
  64. struct sockaddr_in *sin;
  65. struct setting_type *type;
  66. void *data;
  67. size_t len;
  68. int rc;
  69. /* Extract address */
  70. switch ( sa->sa_family ) {
  71. case AF_INET:
  72. sin = ( ( struct sockaddr_in * ) sa );
  73. data = &sin->sin_addr;
  74. len = sizeof ( sin->sin_addr );
  75. type = &setting_type_ipv4;
  76. break;
  77. default:
  78. rc = -ENOTSUP;
  79. goto err;
  80. }
  81. /* Save in specified setting */
  82. if ( ( rc = store_named_setting ( nslookup->setting_name, type,
  83. data, len ) ) != 0 )
  84. goto err;
  85. err:
  86. /* Terminate name resolution */
  87. nslookup_close ( nslookup, rc );
  88. }
  89. /** Name resolution resolver interface operations */
  90. static struct interface_operation nslookup_resolver_operations[] = {
  91. INTF_OP ( resolv_done, struct nslookup *, nslookup_resolv_done ),
  92. INTF_OP ( intf_close, struct nslookup *, nslookup_close ),
  93. };
  94. /** Name resolution resolver interface descriptor */
  95. static struct interface_descriptor nslookup_resolver_desc =
  96. INTF_DESC_PASSTHRU ( struct nslookup, resolver,
  97. nslookup_resolver_operations, job );
  98. /** Name resolution job control interface operations */
  99. static struct interface_operation nslookup_job_operations[] = {
  100. INTF_OP ( intf_close, struct nslookup *, nslookup_close ),
  101. };
  102. /** Name resolution job control interface descriptor */
  103. static struct interface_descriptor nslookup_job_desc =
  104. INTF_DESC_PASSTHRU ( struct nslookup, job,
  105. nslookup_job_operations, resolver );
  106. /**
  107. * Initiate standalone name resolution
  108. *
  109. * @v job Parent interface
  110. * @v name Name to resolve
  111. * @v setting_name Setting name
  112. * @ret rc Return status code
  113. */
  114. static int resolv_setting ( struct interface *job, const char *name,
  115. const char *setting_name ) {
  116. struct nslookup *nslookup;
  117. struct sockaddr sa;
  118. char *setting_name_copy;
  119. int rc;
  120. /* Allocate and initialise structure */
  121. nslookup = zalloc ( sizeof ( *nslookup ) + strlen ( setting_name )
  122. + 1 /* NUL */ );
  123. if ( ! nslookup )
  124. return -ENOMEM;
  125. ref_init ( &nslookup->refcnt, NULL );
  126. intf_init ( &nslookup->job, &nslookup_job_desc, &nslookup->refcnt );
  127. intf_init ( &nslookup->resolver, &nslookup_resolver_desc,
  128. &nslookup->refcnt );
  129. setting_name_copy = ( ( void * ) ( nslookup + 1 ) );
  130. strcpy ( setting_name_copy, setting_name );
  131. nslookup->setting_name = setting_name_copy;
  132. /* Start name resolution */
  133. memset ( &sa, 0, sizeof ( sa ) );
  134. if ( ( rc = resolv ( &nslookup->resolver, name, &sa ) ) != 0 )
  135. goto err_resolv;
  136. /* Attach parent interface, mortalise self, and return */
  137. intf_plug_plug ( &nslookup->job, job );
  138. ref_put ( &nslookup->refcnt );
  139. return 0;
  140. err_resolv:
  141. ref_put ( &nslookup->refcnt );
  142. return rc;
  143. }
  144. /**
  145. * Perform (blocking) standalone name resolution
  146. *
  147. * @v name Name to resolve
  148. * @v setting_name Setting name
  149. * @ret rc Return status code
  150. */
  151. int nslookup ( const char *name, const char *setting_name ) {
  152. int rc;
  153. /* Perform name resolution */
  154. if ( ( rc = resolv_setting ( &monojob, name, setting_name ) ) == 0 )
  155. rc = monojob_wait ( NULL );
  156. if ( rc != 0 ) {
  157. printf ( "Could not resolve %s: %s\n", name, strerror ( rc ) );
  158. return rc;
  159. }
  160. return 0;
  161. }