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.

open.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <stdarg.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <ipxe/xfer.h>
  23. #include <ipxe/uri.h>
  24. #include <ipxe/socket.h>
  25. #include <ipxe/open.h>
  26. /** @file
  27. *
  28. * Data transfer interface opening
  29. *
  30. */
  31. /**
  32. * Find opener for URI scheme
  33. *
  34. * @v scheme URI scheme
  35. * @ret opener Opener, or NULL
  36. */
  37. struct uri_opener * xfer_uri_opener ( const char *scheme ) {
  38. struct uri_opener *opener;
  39. for_each_table_entry ( opener, URI_OPENERS ) {
  40. if ( strcmp ( scheme, opener->scheme ) == 0 )
  41. return opener;
  42. }
  43. return NULL;
  44. }
  45. /**
  46. * Open URI
  47. *
  48. * @v intf Data transfer interface
  49. * @v uri URI
  50. * @ret rc Return status code
  51. *
  52. * The URI will be regarded as being relative to the current working
  53. * URI (see churi()).
  54. */
  55. int xfer_open_uri ( struct interface *intf, struct uri *uri ) {
  56. struct uri_opener *opener;
  57. struct uri *resolved_uri;
  58. int rc;
  59. /* Resolve URI */
  60. resolved_uri = resolve_uri ( cwuri, uri );
  61. if ( ! resolved_uri ) {
  62. rc = -ENOMEM;
  63. goto err_resolve_uri;
  64. }
  65. /* Find opener which supports this URI scheme */
  66. opener = xfer_uri_opener ( resolved_uri->scheme );
  67. if ( ! opener ) {
  68. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " attempted to open "
  69. "unsupported URI scheme \"%s\"\n",
  70. INTF_DBG ( intf ), resolved_uri->scheme );
  71. rc = -ENOTSUP;
  72. goto err_opener;
  73. }
  74. /* Call opener */
  75. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " opening %s URI\n",
  76. INTF_DBG ( intf ), resolved_uri->scheme );
  77. if ( ( rc = opener->open ( intf, resolved_uri ) ) != 0 ) {
  78. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " could not open: "
  79. "%s\n", INTF_DBG ( intf ), strerror ( rc ) );
  80. goto err_open;
  81. }
  82. err_open:
  83. err_opener:
  84. uri_put ( resolved_uri );
  85. err_resolve_uri:
  86. return rc;
  87. }
  88. /**
  89. * Open URI string
  90. *
  91. * @v intf Data transfer interface
  92. * @v uri_string URI string (e.g. "http://ipxe.org/kernel")
  93. * @ret rc Return status code
  94. *
  95. * The URI will be regarded as being relative to the current working
  96. * URI (see churi()).
  97. */
  98. int xfer_open_uri_string ( struct interface *intf,
  99. const char *uri_string ) {
  100. struct uri *uri;
  101. int rc;
  102. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " opening URI %s\n",
  103. INTF_DBG ( intf ), uri_string );
  104. uri = parse_uri ( uri_string );
  105. if ( ! uri )
  106. return -ENOMEM;
  107. rc = xfer_open_uri ( intf, uri );
  108. uri_put ( uri );
  109. return rc;
  110. }
  111. /**
  112. * Open socket
  113. *
  114. * @v intf Data transfer interface
  115. * @v semantics Communication semantics (e.g. SOCK_STREAM)
  116. * @v peer Peer socket address
  117. * @v local Local socket address, or NULL
  118. * @ret rc Return status code
  119. */
  120. int xfer_open_socket ( struct interface *intf, int semantics,
  121. struct sockaddr *peer, struct sockaddr *local ) {
  122. struct socket_opener *opener;
  123. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " opening (%s,%s) socket\n",
  124. INTF_DBG ( intf ), socket_semantics_name ( semantics ),
  125. socket_family_name ( peer->sa_family ) );
  126. for_each_table_entry ( opener, SOCKET_OPENERS ) {
  127. if ( ( opener->semantics == semantics ) &&
  128. ( opener->family == peer->sa_family ) ) {
  129. return opener->open ( intf, peer, local );
  130. }
  131. }
  132. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " attempted to open "
  133. "unsupported socket type (%s,%s)\n",
  134. INTF_DBG ( intf ), socket_semantics_name ( semantics ),
  135. socket_family_name ( peer->sa_family ) );
  136. return -ENOTSUP;
  137. }
  138. /**
  139. * Open location
  140. *
  141. * @v intf Data transfer interface
  142. * @v type Location type
  143. * @v args Remaining arguments depend upon location type
  144. * @ret rc Return status code
  145. */
  146. int xfer_vopen ( struct interface *intf, int type, va_list args ) {
  147. switch ( type ) {
  148. case LOCATION_URI_STRING: {
  149. const char *uri_string = va_arg ( args, const char * );
  150. return xfer_open_uri_string ( intf, uri_string ); }
  151. case LOCATION_URI: {
  152. struct uri *uri = va_arg ( args, struct uri * );
  153. return xfer_open_uri ( intf, uri ); }
  154. case LOCATION_SOCKET: {
  155. int semantics = va_arg ( args, int );
  156. struct sockaddr *peer = va_arg ( args, struct sockaddr * );
  157. struct sockaddr *local = va_arg ( args, struct sockaddr * );
  158. return xfer_open_socket ( intf, semantics, peer, local ); }
  159. default:
  160. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " attempted to "
  161. "open unsupported location type %d\n",
  162. INTF_DBG ( intf ), type );
  163. return -ENOTSUP;
  164. }
  165. }
  166. /**
  167. * Open location
  168. *
  169. * @v intf Data transfer interface
  170. * @v type Location type
  171. * @v ... Remaining arguments depend upon location type
  172. * @ret rc Return status code
  173. */
  174. int xfer_open ( struct interface *intf, int type, ... ) {
  175. va_list args;
  176. int rc;
  177. va_start ( args, type );
  178. rc = xfer_vopen ( intf, type, args );
  179. va_end ( args );
  180. return rc;
  181. }
  182. /**
  183. * Reopen location
  184. *
  185. * @v intf Data transfer interface
  186. * @v type Location type
  187. * @v args Remaining arguments depend upon location type
  188. * @ret rc Return status code
  189. *
  190. * This will close the existing connection and open a new connection
  191. * using xfer_vopen(). It is intended to be used as a .vredirect
  192. * method handler.
  193. */
  194. int xfer_vreopen ( struct interface *intf, int type, va_list args ) {
  195. /* Close existing connection */
  196. intf_restart ( intf, 0 );
  197. /* Open new location */
  198. return xfer_vopen ( intf, type, args );
  199. }