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.0KB

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