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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. * Child resolved name
  157. *
  158. * @v mux Name resolution multiplexer
  159. * @v sa Completed socket address
  160. */
  161. static void resmux_child_resolv_done ( struct resolv_mux *mux,
  162. struct sockaddr *sa ) {
  163. DBGC ( mux, "RESOLV %p resolved \"%s\" to %s using method %s\n",
  164. mux, mux->name, sock_ntoa ( sa ), mux->resolver->name );
  165. /* Pass resolution to parent */
  166. resolv_done ( &mux->parent, sa );
  167. }
  168. /**
  169. * Child finished resolution
  170. *
  171. * @v mux Name resolution multiplexer
  172. * @v rc Return status code
  173. */
  174. static void resmux_child_close ( struct resolv_mux *mux, int rc ) {
  175. /* Restart child interface */
  176. intf_restart ( &mux->child, rc );
  177. /* If this resolution succeeded, stop now */
  178. if ( rc == 0 ) {
  179. DBGC ( mux, "RESOLV %p succeeded using method %s\n",
  180. mux, mux->resolver->name );
  181. goto finished;
  182. }
  183. /* Attempt next child resolver, if possible */
  184. mux->resolver++;
  185. if ( mux->resolver >= table_end ( RESOLVERS ) ) {
  186. DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
  187. goto finished;
  188. }
  189. if ( ( rc = resmux_try ( mux ) ) != 0 )
  190. goto finished;
  191. /* Next resolver is now running */
  192. return;
  193. finished:
  194. intf_shutdown ( &mux->parent, rc );
  195. }
  196. /** Name resolution multiplexer child interface operations */
  197. static struct interface_operation resmux_child_op[] = {
  198. INTF_OP ( resolv_done, struct resolv_mux *, resmux_child_resolv_done ),
  199. INTF_OP ( intf_close, struct resolv_mux *, resmux_child_close ),
  200. };
  201. /** Name resolution multiplexer child interface descriptor */
  202. static struct interface_descriptor resmux_child_desc =
  203. INTF_DESC ( struct resolv_mux, child, resmux_child_op );
  204. /**
  205. * Start name resolution
  206. *
  207. * @v resolv Name resolution interface
  208. * @v name Name to resolve
  209. * @v sa Socket address to complete
  210. * @ret rc Return status code
  211. */
  212. int resolv ( struct interface *resolv, const char *name,
  213. struct sockaddr *sa ) {
  214. struct resolv_mux *mux;
  215. size_t name_len = ( strlen ( name ) + 1 );
  216. int rc;
  217. /* Allocate and initialise structure */
  218. mux = zalloc ( sizeof ( *mux ) + name_len );
  219. if ( ! mux )
  220. return -ENOMEM;
  221. ref_init ( &mux->refcnt, NULL );
  222. intf_init ( &mux->parent, &null_intf_desc, &mux->refcnt );
  223. intf_init ( &mux->child, &resmux_child_desc, &mux->refcnt );
  224. mux->resolver = table_start ( RESOLVERS );
  225. if ( sa )
  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 = resmux_try ( mux ) ) != 0 )
  234. goto err;
  235. /* Attach parent interface, mortalise self, and return */
  236. intf_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 interface xfer;
  255. /** Name resolution interface */
  256. struct 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. * Terminate named socket opener
  266. *
  267. * @v named Named socket
  268. * @v rc Reason for termination
  269. */
  270. static void named_close ( struct named_socket *named, int rc ) {
  271. /* Shut down interfaces */
  272. intf_shutdown ( &named->resolv, rc );
  273. intf_shutdown ( &named->xfer, rc );
  274. }
  275. /**
  276. * Check flow control window
  277. *
  278. * @v named Named socket
  279. * @ret len Length of window
  280. */
  281. static size_t named_window ( struct named_socket *named __unused ) {
  282. /* Not ready for data until we have redirected away */
  283. return 0;
  284. }
  285. /** Named socket opener data transfer interface operations */
  286. static struct interface_operation named_xfer_ops[] = {
  287. INTF_OP ( xfer_window, struct named_socket *, named_window ),
  288. INTF_OP ( intf_close, struct named_socket *, named_close ),
  289. };
  290. /** Named socket opener data transfer interface descriptor */
  291. static struct interface_descriptor named_xfer_desc =
  292. INTF_DESC ( struct named_socket, xfer, named_xfer_ops );
  293. /**
  294. * Name resolved
  295. *
  296. * @v named Named socket
  297. * @v sa Completed socket address
  298. */
  299. static void named_resolv_done ( struct named_socket *named,
  300. struct sockaddr *sa ) {
  301. int rc;
  302. /* Nullify data transfer interface */
  303. intf_nullify ( &named->xfer );
  304. /* Redirect data-xfer interface */
  305. if ( ( rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
  306. named->semantics, sa,
  307. ( named->have_local ?
  308. &named->local : NULL ) ) ) != 0 ) {
  309. /* Redirection failed - do not unplug data-xfer interface */
  310. DBGC ( named, "NAMED %p could not redirect: %s\n",
  311. named, strerror ( rc ) );
  312. } else {
  313. /* Redirection succeeded - unplug data-xfer interface */
  314. DBGC ( named, "NAMED %p redirected successfully\n", named );
  315. intf_unplug ( &named->xfer );
  316. }
  317. /* Terminate named socket opener */
  318. named_close ( named, rc );
  319. }
  320. /** Named socket opener resolver interface operations */
  321. static struct interface_operation named_resolv_op[] = {
  322. INTF_OP ( intf_close, struct named_socket *, named_close ),
  323. INTF_OP ( resolv_done, struct named_socket *, named_resolv_done ),
  324. };
  325. /** Named socket opener resolver interface descriptor */
  326. static struct interface_descriptor named_resolv_desc =
  327. INTF_DESC ( struct named_socket, resolv, named_resolv_op );
  328. /**
  329. * Open named socket
  330. *
  331. * @v semantics Communication semantics (e.g. SOCK_STREAM)
  332. * @v peer Peer socket address to complete
  333. * @v name Name to resolve
  334. * @v local Local socket address, or NULL
  335. * @ret rc Return status code
  336. */
  337. int xfer_open_named_socket ( struct interface *xfer, int semantics,
  338. struct sockaddr *peer, const char *name,
  339. struct sockaddr *local ) {
  340. struct named_socket *named;
  341. int rc;
  342. /* Allocate and initialise structure */
  343. named = zalloc ( sizeof ( *named ) );
  344. if ( ! named )
  345. return -ENOMEM;
  346. ref_init ( &named->refcnt, NULL );
  347. intf_init ( &named->xfer, &named_xfer_desc, &named->refcnt );
  348. intf_init ( &named->resolv, &named_resolv_desc, &named->refcnt );
  349. named->semantics = semantics;
  350. if ( local ) {
  351. memcpy ( &named->local, local, sizeof ( named->local ) );
  352. named->have_local = 1;
  353. }
  354. DBGC ( named, "NAMED %p opening \"%s\"\n",
  355. named, name );
  356. /* Start name resolution */
  357. if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
  358. goto err;
  359. /* Attach parent interface, mortalise self, and return */
  360. intf_plug_plug ( &named->xfer, xfer );
  361. ref_put ( &named->refcnt );
  362. return 0;
  363. err:
  364. ref_put ( &named->refcnt );
  365. return rc;
  366. }