Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

open.c 5.9KB

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