Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

open.c 5.1KB

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