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.

resolv.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <ipxe/xfer.h>
  29. #include <ipxe/open.h>
  30. #include <ipxe/process.h>
  31. #include <ipxe/socket.h>
  32. #include <ipxe/resolv.h>
  33. /** @file
  34. *
  35. * Name resolution
  36. *
  37. */
  38. /***************************************************************************
  39. *
  40. * Name resolution interfaces
  41. *
  42. ***************************************************************************
  43. */
  44. /**
  45. * Name resolved
  46. *
  47. * @v intf Object interface
  48. * @v sa Completed socket address (if successful)
  49. */
  50. void resolv_done ( struct interface *intf, struct sockaddr *sa ) {
  51. struct interface *dest;
  52. resolv_done_TYPE ( void * ) *op =
  53. intf_get_dest_op ( intf, resolv_done, &dest );
  54. void *object = intf_object ( dest );
  55. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " resolv_done\n",
  56. INTF_INTF_DBG ( intf, dest ) );
  57. if ( op ) {
  58. op ( object, sa );
  59. } else {
  60. /* Default is to ignore resolutions */
  61. }
  62. intf_put ( dest );
  63. }
  64. /***************************************************************************
  65. *
  66. * Numeric name resolver
  67. *
  68. ***************************************************************************
  69. */
  70. /** A numeric name resolver */
  71. struct numeric_resolv {
  72. /** Reference counter */
  73. struct refcnt refcnt;
  74. /** Name resolution interface */
  75. struct interface resolv;
  76. /** Process */
  77. struct process process;
  78. /** Completed socket address */
  79. struct sockaddr sa;
  80. /** Overall status code */
  81. int rc;
  82. };
  83. static void numeric_step ( struct numeric_resolv *numeric ) {
  84. if ( numeric->rc == 0 )
  85. resolv_done ( &numeric->resolv, &numeric->sa );
  86. intf_shutdown ( &numeric->resolv, numeric->rc );
  87. }
  88. static struct process_descriptor numeric_process_desc =
  89. PROC_DESC_ONCE ( struct numeric_resolv, process, numeric_step );
  90. static int numeric_resolv ( struct interface *resolv,
  91. const char *name, struct sockaddr *sa ) {
  92. struct numeric_resolv *numeric;
  93. /* Allocate and initialise structure */
  94. numeric = zalloc ( sizeof ( *numeric ) );
  95. if ( ! numeric )
  96. return -ENOMEM;
  97. ref_init ( &numeric->refcnt, NULL );
  98. intf_init ( &numeric->resolv, &null_intf_desc, &numeric->refcnt );
  99. process_init ( &numeric->process, &numeric_process_desc,
  100. &numeric->refcnt );
  101. memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );
  102. /* Attempt to resolve name */
  103. numeric->rc = sock_aton ( name, &numeric->sa );
  104. /* Attach to parent interface, mortalise self, and return */
  105. intf_plug_plug ( &numeric->resolv, resolv );
  106. ref_put ( &numeric->refcnt );
  107. return 0;
  108. }
  109. struct resolver numeric_resolver __resolver ( RESOLV_NUMERIC ) = {
  110. .name = "NUMERIC",
  111. .resolv = numeric_resolv,
  112. };
  113. /***************************************************************************
  114. *
  115. * Name resolution multiplexer
  116. *
  117. ***************************************************************************
  118. */
  119. /** A name resolution multiplexer */
  120. struct resolv_mux {
  121. /** Reference counter */
  122. struct refcnt refcnt;
  123. /** Parent name resolution interface */
  124. struct interface parent;
  125. /** Child name resolution interface */
  126. struct interface child;
  127. /** Current child resolver */
  128. struct resolver *resolver;
  129. /** Socket address to complete */
  130. struct sockaddr sa;
  131. /** Name to be resolved
  132. *
  133. * Must be at end of structure
  134. */
  135. char name[0];
  136. };
  137. /**
  138. * Try current child name resolver
  139. *
  140. * @v mux Name resolution multiplexer
  141. * @ret rc Return status code
  142. */
  143. static int resmux_try ( struct resolv_mux *mux ) {
  144. struct resolver *resolver = mux->resolver;
  145. int rc;
  146. DBGC ( mux, "RESOLV %p trying method %s\n", mux, resolver->name );
  147. if ( ( rc = resolver->resolv ( &mux->child, mux->name,
  148. &mux->sa ) ) != 0 ) {
  149. DBGC ( mux, "RESOLV %p could not use method %s: %s\n",
  150. mux, resolver->name, strerror ( rc ) );
  151. return rc;
  152. }
  153. return 0;
  154. }
  155. /**
  156. * Close name resolution multiplexer
  157. *
  158. * @v mux Name resolution multiplexer
  159. * @v rc Reason for close
  160. */
  161. static void resmux_close ( struct resolv_mux *mux, int rc ) {
  162. /* Shut down all interfaces */
  163. intf_shutdown ( &mux->child, rc );
  164. intf_shutdown ( &mux->parent, rc );
  165. }
  166. /**
  167. * Child finished resolution
  168. *
  169. * @v mux Name resolution multiplexer
  170. * @v rc Return status code
  171. */
  172. static void resmux_child_close ( struct resolv_mux *mux, int rc ) {
  173. /* Restart child interface */
  174. intf_restart ( &mux->child, rc );
  175. /* If this resolution succeeded, stop now */
  176. if ( rc == 0 ) {
  177. DBGC ( mux, "RESOLV %p succeeded using method %s\n",
  178. mux, mux->resolver->name );
  179. goto finished;
  180. }
  181. /* Attempt next child resolver, if possible */
  182. mux->resolver++;
  183. if ( mux->resolver >= table_end ( RESOLVERS ) ) {
  184. DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
  185. goto finished;
  186. }
  187. if ( ( rc = resmux_try ( mux ) ) != 0 )
  188. goto finished;
  189. /* Next resolver is now running */
  190. return;
  191. finished:
  192. resmux_close ( mux, rc );
  193. }
  194. /** Name resolution multiplexer child interface operations */
  195. static struct interface_operation resmux_child_op[] = {
  196. INTF_OP ( intf_close, struct resolv_mux *, resmux_child_close ),
  197. };
  198. /** Name resolution multiplexer child interface descriptor */
  199. static struct interface_descriptor resmux_child_desc =
  200. INTF_DESC_PASSTHRU ( struct resolv_mux, child, resmux_child_op,
  201. parent );
  202. /** Name resolution multiplexer parent interface operations */
  203. static struct interface_operation resmux_parent_op[] = {
  204. INTF_OP ( intf_close, struct resolv_mux *, resmux_close ),
  205. };
  206. /** Name resolution multiplexer parent interface descriptor */
  207. static struct interface_descriptor resmux_parent_desc =
  208. INTF_DESC_PASSTHRU ( struct resolv_mux, parent, resmux_parent_op,
  209. child );
  210. /**
  211. * Start name resolution
  212. *
  213. * @v resolv Name resolution interface
  214. * @v name Name to resolve
  215. * @v sa Socket address to complete
  216. * @ret rc Return status code
  217. */
  218. int resolv ( struct interface *resolv, const char *name,
  219. struct sockaddr *sa ) {
  220. struct resolv_mux *mux;
  221. size_t name_len = ( strlen ( name ) + 1 );
  222. int rc;
  223. /* Allocate and initialise structure */
  224. mux = zalloc ( sizeof ( *mux ) + name_len );
  225. if ( ! mux )
  226. return -ENOMEM;
  227. ref_init ( &mux->refcnt, NULL );
  228. intf_init ( &mux->parent, &resmux_parent_desc, &mux->refcnt );
  229. intf_init ( &mux->child, &resmux_child_desc, &mux->refcnt );
  230. mux->resolver = table_start ( RESOLVERS );
  231. if ( sa )
  232. memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
  233. memcpy ( mux->name, name, name_len );
  234. DBGC ( mux, "RESOLV %p attempting to resolve \"%s\"\n", mux, name );
  235. /* Start first resolver in chain. There will always be at
  236. * least one resolver (the numeric resolver), so no need to
  237. * check for the zero-resolvers-available case.
  238. */
  239. if ( ( rc = resmux_try ( mux ) ) != 0 )
  240. goto err;
  241. /* Attach parent interface, mortalise self, and return */
  242. intf_plug_plug ( &mux->parent, resolv );
  243. ref_put ( &mux->refcnt );
  244. return 0;
  245. err:
  246. ref_put ( &mux->refcnt );
  247. return rc;
  248. }
  249. /***************************************************************************
  250. *
  251. * Named socket opening
  252. *
  253. ***************************************************************************
  254. */
  255. /** A named socket */
  256. struct named_socket {
  257. /** Reference counter */
  258. struct refcnt refcnt;
  259. /** Data transfer interface */
  260. struct interface xfer;
  261. /** Name resolution interface */
  262. struct interface resolv;
  263. /** Communication semantics (e.g. SOCK_STREAM) */
  264. int semantics;
  265. /** Stored local socket address, if applicable */
  266. struct sockaddr local;
  267. /** Stored local socket address exists */
  268. int have_local;
  269. };
  270. /**
  271. * Terminate named socket opener
  272. *
  273. * @v named Named socket
  274. * @v rc Reason for termination
  275. */
  276. static void named_close ( struct named_socket *named, int rc ) {
  277. /* Shut down interfaces */
  278. intf_shutdown ( &named->resolv, rc );
  279. intf_shutdown ( &named->xfer, rc );
  280. }
  281. /**
  282. * Check flow control window
  283. *
  284. * @v named Named socket
  285. * @ret len Length of window
  286. */
  287. static size_t named_window ( struct named_socket *named __unused ) {
  288. /* Not ready for data until we have redirected away */
  289. return 0;
  290. }
  291. /** Named socket opener data transfer interface operations */
  292. static struct interface_operation named_xfer_ops[] = {
  293. INTF_OP ( xfer_window, struct named_socket *, named_window ),
  294. INTF_OP ( intf_close, struct named_socket *, named_close ),
  295. };
  296. /** Named socket opener data transfer interface descriptor */
  297. static struct interface_descriptor named_xfer_desc =
  298. INTF_DESC_PASSTHRU ( struct named_socket, xfer, named_xfer_ops,
  299. resolv );
  300. /**
  301. * Name resolved
  302. *
  303. * @v named Named socket
  304. * @v sa Completed socket address
  305. */
  306. static void named_resolv_done ( struct named_socket *named,
  307. struct sockaddr *sa ) {
  308. int rc;
  309. /* Nullify data transfer interface */
  310. intf_nullify ( &named->xfer );
  311. /* Redirect data-xfer interface */
  312. if ( ( rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
  313. named->semantics, sa,
  314. ( named->have_local ?
  315. &named->local : NULL ) ) ) != 0 ) {
  316. /* Redirection failed - do not unplug data-xfer interface */
  317. DBGC ( named, "NAMED %p could not redirect: %s\n",
  318. named, strerror ( rc ) );
  319. } else {
  320. /* Redirection succeeded - unplug data-xfer interface */
  321. DBGC ( named, "NAMED %p redirected successfully\n", named );
  322. intf_unplug ( &named->xfer );
  323. }
  324. /* Terminate named socket opener */
  325. named_close ( named, rc );
  326. }
  327. /** Named socket opener resolver interface operations */
  328. static struct interface_operation named_resolv_op[] = {
  329. INTF_OP ( intf_close, struct named_socket *, named_close ),
  330. INTF_OP ( resolv_done, struct named_socket *, named_resolv_done ),
  331. };
  332. /** Named socket opener resolver interface descriptor */
  333. static struct interface_descriptor named_resolv_desc =
  334. INTF_DESC_PASSTHRU ( struct named_socket, resolv, named_resolv_op,
  335. xfer );
  336. /**
  337. * Open named socket
  338. *
  339. * @v semantics Communication semantics (e.g. SOCK_STREAM)
  340. * @v peer Peer socket address to complete
  341. * @v name Name to resolve
  342. * @v local Local socket address, or NULL
  343. * @ret rc Return status code
  344. */
  345. int xfer_open_named_socket ( struct interface *xfer, int semantics,
  346. struct sockaddr *peer, const char *name,
  347. struct sockaddr *local ) {
  348. struct named_socket *named;
  349. int rc;
  350. /* Allocate and initialise structure */
  351. named = zalloc ( sizeof ( *named ) );
  352. if ( ! named )
  353. return -ENOMEM;
  354. ref_init ( &named->refcnt, NULL );
  355. intf_init ( &named->xfer, &named_xfer_desc, &named->refcnt );
  356. intf_init ( &named->resolv, &named_resolv_desc, &named->refcnt );
  357. named->semantics = semantics;
  358. if ( local ) {
  359. memcpy ( &named->local, local, sizeof ( named->local ) );
  360. named->have_local = 1;
  361. }
  362. DBGC ( named, "NAMED %p opening \"%s\"\n",
  363. named, name );
  364. /* Start name resolution */
  365. if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
  366. goto err;
  367. /* Attach parent interface, mortalise self, and return */
  368. intf_plug_plug ( &named->xfer, xfer );
  369. ref_put ( &named->refcnt );
  370. return 0;
  371. err:
  372. ref_put ( &named->refcnt );
  373. return rc;
  374. }