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.

resolv.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <gpxe/in.h>
  24. #include <gpxe/xfer.h>
  25. #include <gpxe/open.h>
  26. #include <gpxe/process.h>
  27. #include <gpxe/resolv.h>
  28. /** @file
  29. *
  30. * Name resolution
  31. *
  32. */
  33. /***************************************************************************
  34. *
  35. * Name resolution interfaces
  36. *
  37. ***************************************************************************
  38. */
  39. /**
  40. * Name resolution completed
  41. *
  42. * @v resolv Name resolution interface
  43. * @v sa Completed socket address (if successful)
  44. * @v rc Final status code
  45. */
  46. void resolv_done ( struct resolv_interface *resolv, struct sockaddr *sa,
  47. int rc ) {
  48. struct resolv_interface *dest = resolv_get_dest ( resolv );
  49. resolv_unplug ( resolv );
  50. dest->op->done ( dest, sa, rc );
  51. resolv_put ( dest );
  52. }
  53. /**
  54. * Ignore name resolution done() event
  55. *
  56. * @v resolv Name resolution interface
  57. * @v sa Completed socket address (if successful)
  58. * @v rc Final status code
  59. */
  60. void ignore_resolv_done ( struct resolv_interface *resolv __unused,
  61. struct sockaddr *sa __unused, int rc __unused ) {
  62. /* Do nothing */
  63. }
  64. /** Null name resolution interface operations */
  65. struct resolv_interface_operations null_resolv_ops = {
  66. .done = ignore_resolv_done,
  67. };
  68. /** Null name resolution interface */
  69. struct resolv_interface null_resolv = {
  70. .intf = {
  71. .dest = &null_resolv.intf,
  72. .refcnt = NULL,
  73. },
  74. .op = &null_resolv_ops,
  75. };
  76. /***************************************************************************
  77. *
  78. * Numeric name resolver
  79. *
  80. ***************************************************************************
  81. */
  82. /** A numeric name resolver */
  83. struct numeric_resolv {
  84. /** Reference counter */
  85. struct refcnt refcnt;
  86. /** Name resolution interface */
  87. struct resolv_interface resolv;
  88. /** Process */
  89. struct process process;
  90. /** Completed socket address */
  91. struct sockaddr sa;
  92. /** Overall status code */
  93. int rc;
  94. };
  95. static void numeric_step ( struct process *process ) {
  96. struct numeric_resolv *numeric =
  97. container_of ( process, struct numeric_resolv, process );
  98. resolv_done ( &numeric->resolv, &numeric->sa, numeric->rc );
  99. process_del ( process );
  100. }
  101. static int numeric_resolv ( struct resolv_interface *resolv,
  102. const char *name, struct sockaddr *sa ) {
  103. struct numeric_resolv *numeric;
  104. struct sockaddr_in *sin;
  105. /* Allocate and initialise structure */
  106. numeric = zalloc ( sizeof ( *numeric ) );
  107. if ( ! numeric )
  108. return -ENOMEM;
  109. resolv_init ( &numeric->resolv, &null_resolv_ops, &numeric->refcnt );
  110. process_init ( &numeric->process, numeric_step, &numeric->refcnt );
  111. memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );
  112. DBGC ( numeric, "NUMERIC %p attempting to resolve \"%s\"\n",
  113. numeric, name );
  114. /* Attempt to resolve name */
  115. sin = ( ( struct sockaddr_in * ) &numeric->sa );
  116. sin->sin_family = AF_INET;
  117. if ( inet_aton ( name, &sin->sin_addr ) == 0 )
  118. numeric->rc = -EINVAL;
  119. /* Attach to parent interface, mortalise self, and return */
  120. resolv_plug_plug ( &numeric->resolv, resolv );
  121. ref_put ( &numeric->refcnt );
  122. return 0;
  123. }
  124. struct resolver numeric_resolver __resolver ( RESOLV_NUMERIC ) = {
  125. .name = "NUMERIC",
  126. .resolv = numeric_resolv,
  127. };
  128. /***************************************************************************
  129. *
  130. * Name resolution multiplexer
  131. *
  132. ***************************************************************************
  133. */
  134. /** A name resolution multiplexer */
  135. struct resolv_mux {
  136. /** Reference counter */
  137. struct refcnt refcnt;
  138. /** Parent name resolution interface */
  139. struct resolv_interface parent;
  140. /** Child name resolution interface */
  141. struct resolv_interface child;
  142. /** Current child resolver */
  143. struct resolver *resolver;
  144. /** Socket address to complete */
  145. struct sockaddr sa;
  146. /** Name to be resolved
  147. *
  148. * Must be at end of structure
  149. */
  150. char name[0];
  151. };
  152. /**
  153. * Try current child name resolver
  154. *
  155. * @v mux Name resolution multiplexer
  156. * @ret rc Return status code
  157. */
  158. static int resolv_mux_try ( struct resolv_mux *mux ) {
  159. struct resolver *resolver = mux->resolver;
  160. int rc;
  161. DBGC ( mux, "RESOLV %p trying method %s\n", mux, resolver->name );
  162. if ( ( rc = resolver->resolv ( &mux->child, mux->name,
  163. &mux->sa ) ) != 0 ) {
  164. DBGC ( mux, "RESOLV %p could not use method %s: %s\n",
  165. mux, resolver->name, strerror ( rc ) );
  166. return rc;
  167. }
  168. return 0;
  169. }
  170. /**
  171. * Handle done() event from child name resolver
  172. *
  173. * @v resolv Child name resolution interface
  174. * @v sa Completed socket address (if successful)
  175. * @v rc Final status code
  176. */
  177. static void resolv_mux_done ( struct resolv_interface *resolv,
  178. struct sockaddr *sa, int rc ) {
  179. struct resolv_mux *mux =
  180. container_of ( resolv, struct resolv_mux, child );
  181. /* Unplug child */
  182. resolv_unplug ( &mux->child );
  183. /* If this resolution succeeded, stop now */
  184. if ( rc == 0 ) {
  185. DBGC ( mux, "RESOLV %p succeeded using method %s\n",
  186. mux, mux->resolver->name );
  187. goto finished;
  188. }
  189. /* Attempt next child resolver, if possible */
  190. mux->resolver++;
  191. if ( mux->resolver >= table_end ( RESOLVERS ) ) {
  192. DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
  193. goto finished;
  194. }
  195. if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
  196. goto finished;
  197. /* Next resolver is now running */
  198. return;
  199. finished:
  200. resolv_done ( &mux->parent, sa, rc );
  201. }
  202. /** Name resolution multiplexer operations */
  203. static struct resolv_interface_operations resolv_mux_child_ops = {
  204. .done = resolv_mux_done,
  205. };
  206. /**
  207. * Start name resolution
  208. *
  209. * @v resolv Name resolution interface
  210. * @v name Name to resolve
  211. * @v sa Socket address to complete
  212. * @ret rc Return status code
  213. */
  214. int resolv ( struct resolv_interface *resolv, const char *name,
  215. struct sockaddr *sa ) {
  216. struct resolv_mux *mux;
  217. size_t name_len = ( strlen ( name ) + 1 );
  218. int rc;
  219. /* Allocate and initialise structure */
  220. mux = zalloc ( sizeof ( *mux ) + name_len );
  221. if ( ! mux )
  222. return -ENOMEM;
  223. resolv_init ( &mux->parent, &null_resolv_ops, &mux->refcnt );
  224. resolv_init ( &mux->child, &resolv_mux_child_ops, &mux->refcnt );
  225. mux->resolver = table_start ( RESOLVERS );
  226. memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
  227. memcpy ( mux->name, name, name_len );
  228. DBGC ( mux, "RESOLV %p attempting to resolve \"%s\"\n", mux, name );
  229. /* Start first resolver in chain. There will always be at
  230. * least one resolver (the numeric resolver), so no need to
  231. * check for the zero-resolvers-available case.
  232. */
  233. if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
  234. goto err;
  235. /* Attach parent interface, mortalise self, and return */
  236. resolv_plug_plug ( &mux->parent, resolv );
  237. ref_put ( &mux->refcnt );
  238. return 0;
  239. err:
  240. ref_put ( &mux->refcnt );
  241. return rc;
  242. }
  243. /***************************************************************************
  244. *
  245. * Named socket opening
  246. *
  247. ***************************************************************************
  248. */
  249. /** A named socket */
  250. struct named_socket {
  251. /** Reference counter */
  252. struct refcnt refcnt;
  253. /** Data transfer interface */
  254. struct xfer_interface xfer;
  255. /** Name resolution interface */
  256. struct resolv_interface resolv;
  257. /** Communication semantics (e.g. SOCK_STREAM) */
  258. int semantics;
  259. /** Stored local socket address, if applicable */
  260. struct sockaddr local;
  261. /** Stored local socket address exists */
  262. int have_local;
  263. };
  264. /**
  265. * Finish using named socket
  266. *
  267. * @v named Named socket
  268. * @v rc Reason for finish
  269. */
  270. static void named_done ( struct named_socket *named, int rc ) {
  271. /* Close all interfaces */
  272. resolv_nullify ( &named->resolv );
  273. xfer_nullify ( &named->xfer );
  274. xfer_close ( &named->xfer, rc );
  275. }
  276. /**
  277. * Handle close() event
  278. *
  279. * @v xfer Data transfer interface
  280. * @v rc Reason for close
  281. */
  282. static void named_xfer_close ( struct xfer_interface *xfer, int rc ) {
  283. struct named_socket *named =
  284. container_of ( xfer, struct named_socket, xfer );
  285. named_done ( named, rc );
  286. }
  287. /** Named socket opener data transfer interface operations */
  288. static struct xfer_interface_operations named_xfer_ops = {
  289. .close = named_xfer_close,
  290. .vredirect = ignore_xfer_vredirect,
  291. .window = no_xfer_window,
  292. .alloc_iob = default_xfer_alloc_iob,
  293. .deliver_iob = xfer_deliver_as_raw,
  294. .deliver_raw = ignore_xfer_deliver_raw,
  295. };
  296. /**
  297. * Handle done() event
  298. *
  299. * @v resolv Name resolution interface
  300. * @v sa Completed socket address (if successful)
  301. * @v rc Final status code
  302. */
  303. static void named_resolv_done ( struct resolv_interface *resolv,
  304. struct sockaddr *sa, int rc ) {
  305. struct named_socket *named =
  306. container_of ( resolv, struct named_socket, resolv );
  307. /* Redirect if name resolution was successful */
  308. if ( rc == 0 ) {
  309. rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
  310. named->semantics, sa,
  311. ( named->have_local ?
  312. &named->local : NULL ) );
  313. }
  314. /* Terminate resolution */
  315. named_done ( named, rc );
  316. }
  317. /** Named socket opener name resolution interface operations */
  318. static struct resolv_interface_operations named_resolv_ops = {
  319. .done = named_resolv_done,
  320. };
  321. /**
  322. * Open named socket
  323. *
  324. * @v semantics Communication semantics (e.g. SOCK_STREAM)
  325. * @v peer Peer socket address to complete
  326. * @v name Name to resolve
  327. * @v local Local socket address, or NULL
  328. * @ret rc Return status code
  329. */
  330. int xfer_open_named_socket ( struct xfer_interface *xfer, int semantics,
  331. struct sockaddr *peer, const char *name,
  332. struct sockaddr *local ) {
  333. struct named_socket *named;
  334. int rc;
  335. /* Allocate and initialise structure */
  336. named = zalloc ( sizeof ( *named ) );
  337. if ( ! named )
  338. return -ENOMEM;
  339. xfer_init ( &named->xfer, &named_xfer_ops, &named->refcnt );
  340. resolv_init ( &named->resolv, &named_resolv_ops, &named->refcnt );
  341. named->semantics = semantics;
  342. if ( local ) {
  343. memcpy ( &named->local, local, sizeof ( named->local ) );
  344. named->have_local = 1;
  345. }
  346. DBGC ( named, "RESOLV %p opening named socket \"%s\"\n",
  347. named, name );
  348. /* Start name resolution */
  349. if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
  350. goto err;
  351. /* Attach parent interface, mortalise self, and return */
  352. xfer_plug_plug ( &named->xfer, xfer );
  353. ref_put ( &named->refcnt );
  354. return 0;
  355. err:
  356. ref_put ( &named->refcnt );
  357. return rc;
  358. }