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

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