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

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