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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_string URI string (e.g. "http://etherboot.org/kernel")
  45. * @ret rc Return status code
  46. */
  47. int xfer_open_uri ( struct xfer_interface *xfer, const char *uri_string ) {
  48. struct uri *uri;
  49. struct uri_opener *opener;
  50. DBGC ( xfer, "XFER %p opening URI %s\n", xfer, uri_string );
  51. uri = parse_uri ( uri_string );
  52. if ( ! uri )
  53. return -ENOMEM;
  54. for ( opener = uri_openers ; opener < uri_openers_end ; opener++ ) {
  55. if ( strcmp ( uri->scheme, opener->scheme ) == 0 ) {
  56. return opener->open ( xfer, uri );
  57. }
  58. }
  59. DBGC ( xfer, "XFER %p attempted to open unsupported URI scheme "
  60. "\"%s\"\n", xfer, uri->scheme );
  61. free_uri ( uri );
  62. return -ENOTSUP;
  63. }
  64. /**
  65. * Open socket
  66. *
  67. * @v xfer Data transfer interface
  68. * @v domain Communication domain (e.g. PF_INET)
  69. * @v type Communication semantics (e.g. SOCK_STREAM)
  70. */
  71. int xfer_open_socket ( struct xfer_interface *xfer,
  72. int domain, int type, struct sockaddr *sa ) {
  73. struct socket_opener *opener;
  74. DBGC ( xfer, "XFER %p opening (%s,%s) socket\n", xfer,
  75. socket_domain_name ( domain ), socket_type_name ( type ) );
  76. for ( opener = socket_openers; opener < socket_openers_end; opener++ ){
  77. if ( ( opener->domain == domain ) &&
  78. ( opener->type == type ) ) {
  79. return opener->open ( xfer, sa );
  80. }
  81. }
  82. DBGC ( xfer, "XFER %p attempted to open unsupported socket type "
  83. "(%s,%s)\n", xfer, socket_domain_name ( domain ),
  84. socket_type_name ( type ) );
  85. return -ENOTSUP;
  86. }
  87. /**
  88. * Open location
  89. *
  90. * @v xfer Data transfer interface
  91. * @v type Location type
  92. * @v args Remaining arguments depend upon location type
  93. * @ret rc Return status code
  94. */
  95. int xfer_vopen ( struct xfer_interface *xfer, int type, va_list args ) {
  96. switch ( type ) {
  97. case LOCATION_URI: {
  98. const char *uri_string = va_arg ( args, const char * );
  99. return xfer_open_uri ( xfer, uri_string ); }
  100. case LOCATION_SOCKET: {
  101. int domain = va_arg ( args, int );
  102. int type = va_arg ( args, int );
  103. struct sockaddr *sa = va_arg ( args, struct sockaddr * );
  104. return xfer_open_socket ( xfer, domain, type, sa ); }
  105. default:
  106. DBGC ( xfer, "XFER %p attempted to open unsupported location "
  107. "type %d\n", xfer, type );
  108. return -ENOTSUP;
  109. }
  110. }
  111. /**
  112. * Open location
  113. *
  114. * @v xfer Data transfer interface
  115. * @v type Location type
  116. * @v ... Remaining arguments depend upon location type
  117. * @ret rc Return status code
  118. */
  119. int xfer_open ( struct xfer_interface *xfer, int type, ... ) {
  120. va_list args;
  121. int rc;
  122. va_start ( args, type );
  123. rc = xfer_vopen ( xfer, type, args );
  124. va_end ( args );
  125. return rc;
  126. }