Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. /** Registered name resolvers */
  134. static struct resolver resolvers[0]
  135. __table_start ( struct resolver, resolvers );
  136. static struct resolver resolvers_end[0]
  137. __table_end ( struct resolver, resolvers );
  138. /** A name resolution multiplexer */
  139. struct resolv_mux {
  140. /** Reference counter */
  141. struct refcnt refcnt;
  142. /** Parent name resolution interface */
  143. struct resolv_interface parent;
  144. /** Child name resolution interface */
  145. struct resolv_interface child;
  146. /** Current child resolver */
  147. struct resolver *resolver;
  148. /** Socket address to complete */
  149. struct sockaddr sa;
  150. /** Name to be resolved
  151. *
  152. * Must be at end of structure
  153. */
  154. char name[0];
  155. };
  156. /**
  157. * Try current child name resolver
  158. *
  159. * @v mux Name resolution multiplexer
  160. * @ret rc Return status code
  161. */
  162. static int resolv_mux_try ( struct resolv_mux *mux ) {
  163. struct resolver *resolver = mux->resolver;
  164. int rc;
  165. DBGC ( mux, "RESOLV %p trying method %s\n", mux, resolver->name );
  166. if ( ( rc = resolver->resolv ( &mux->child, mux->name,
  167. &mux->sa ) ) != 0 ) {
  168. DBGC ( mux, "RESOLV %p could not use method %s: %s\n",
  169. mux, resolver->name, strerror ( rc ) );
  170. return rc;
  171. }
  172. return 0;
  173. }
  174. /**
  175. * Handle done() event from child name resolver
  176. *
  177. * @v resolv Child name resolution interface
  178. * @v sa Completed socket address (if successful)
  179. * @v rc Final status code
  180. */
  181. static void resolv_mux_done ( struct resolv_interface *resolv,
  182. struct sockaddr *sa, int rc ) {
  183. struct resolv_mux *mux =
  184. container_of ( resolv, struct resolv_mux, child );
  185. /* Unplug child */
  186. resolv_unplug ( &mux->child );
  187. /* If this resolution succeeded, stop now */
  188. if ( rc == 0 ) {
  189. DBGC ( mux, "RESOLV %p succeeded using method %s\n",
  190. mux, mux->resolver->name );
  191. goto finished;
  192. }
  193. /* Attempt next child resolver, if possible */
  194. mux->resolver++;
  195. if ( mux->resolver >= resolvers_end ) {
  196. DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
  197. goto finished;
  198. }
  199. if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
  200. goto finished;
  201. /* Next resolver is now running */
  202. return;
  203. finished:
  204. resolv_done ( &mux->parent, sa, rc );
  205. }
  206. /** Name resolution multiplexer operations */
  207. static struct resolv_interface_operations resolv_mux_child_ops = {
  208. .done = resolv_mux_done,
  209. };
  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 resolv_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. resolv_init ( &mux->parent, &null_resolv_ops, &mux->refcnt );
  228. resolv_init ( &mux->child, &resolv_mux_child_ops, &mux->refcnt );
  229. mux->resolver = resolvers;
  230. memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
  231. memcpy ( mux->name, name, name_len );
  232. DBGC ( mux, "RESOLV %p attempting to resolve \"%s\"\n", mux, name );
  233. /* Start first resolver in chain. There will always be at
  234. * least one resolver (the numeric resolver), so no need to
  235. * check for the zero-resolvers-available case.
  236. */
  237. if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
  238. goto err;
  239. /* Attach parent interface, mortalise self, and return */
  240. resolv_plug_plug ( &mux->parent, resolv );
  241. ref_put ( &mux->refcnt );
  242. return 0;
  243. err:
  244. ref_put ( &mux->refcnt );
  245. return rc;
  246. }
  247. /***************************************************************************
  248. *
  249. * Named socket opening
  250. *
  251. ***************************************************************************
  252. */
  253. /** A named socket */
  254. struct named_socket {
  255. /** Reference counter */
  256. struct refcnt refcnt;
  257. /** Data transfer interface */
  258. struct xfer_interface xfer;
  259. /** Name resolution interface */
  260. struct resolv_interface resolv;
  261. /** Communication semantics (e.g. SOCK_STREAM) */
  262. int semantics;
  263. /** Stored local socket address, if applicable */
  264. struct sockaddr local;
  265. /** Stored local socket address exists */
  266. int have_local;
  267. };
  268. /** Named socket opener data transfer interface operations */
  269. static struct xfer_interface_operations named_xfer_ops = {
  270. .close = ignore_xfer_close,
  271. .vredirect = ignore_xfer_vredirect,
  272. .seek = ignore_xfer_seek,
  273. .window = no_xfer_window,
  274. .alloc_iob = default_xfer_alloc_iob,
  275. .deliver_iob = xfer_deliver_as_raw,
  276. .deliver_raw = ignore_xfer_deliver_raw,
  277. };
  278. /**
  279. * Handle done() event
  280. *
  281. * @v resolv Name resolution interface
  282. * @v sa Completed socket address (if successful)
  283. * @v rc Final status code
  284. */
  285. static void named_resolv_done ( struct resolv_interface *resolv,
  286. struct sockaddr *sa, int rc ) {
  287. struct named_socket *named =
  288. container_of ( resolv, struct named_socket, resolv );
  289. /* Unplug resolver and nullify data transfer interface */
  290. resolv_unplug ( &named->resolv );
  291. xfer_nullify ( &named->xfer );
  292. /* Redirect if name resolution was successful */
  293. if ( rc == 0 ) {
  294. rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
  295. named->semantics, sa,
  296. ( named->have_local ?
  297. &named->local : NULL ) );
  298. }
  299. /* Close data transfer interface if redirection failed */
  300. if ( rc != 0 )
  301. xfer_close ( &named->xfer, rc );
  302. /* Unplug data transfer interface */
  303. xfer_unplug ( &named->xfer );
  304. }
  305. /** Named socket opener name resolution interface operations */
  306. static struct resolv_interface_operations named_resolv_ops = {
  307. .done = named_resolv_done,
  308. };
  309. /**
  310. * Open named socket
  311. *
  312. * @v semantics Communication semantics (e.g. SOCK_STREAM)
  313. * @v peer Peer socket address to complete
  314. * @v name Name to resolve
  315. * @v local Local socket address, or NULL
  316. * @ret rc Return status code
  317. */
  318. int xfer_open_named_socket ( struct xfer_interface *xfer, int semantics,
  319. struct sockaddr *peer, const char *name,
  320. struct sockaddr *local ) {
  321. struct named_socket *named;
  322. int rc;
  323. /* Allocate and initialise structure */
  324. named = zalloc ( sizeof ( *named ) );
  325. if ( ! named )
  326. return -ENOMEM;
  327. xfer_init ( &named->xfer, &named_xfer_ops, &named->refcnt );
  328. resolv_init ( &named->resolv, &named_resolv_ops, &named->refcnt );
  329. named->semantics = semantics;
  330. if ( local ) {
  331. memcpy ( &named->local, local, sizeof ( named->local ) );
  332. named->have_local = 1;
  333. }
  334. DBGC ( named, "RESOLV %p opening named socket \"%s\"\n",
  335. named, name );
  336. /* Start name resolution */
  337. if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
  338. goto err;
  339. /* Attach parent interface, mortalise self, and return */
  340. xfer_plug_plug ( &named->xfer, xfer );
  341. ref_put ( &named->refcnt );
  342. return 0;
  343. err:
  344. ref_put ( &named->refcnt );
  345. return rc;
  346. }