open.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdarg.h>
00022 #include <string.h>
00023 #include <errno.h>
00024 #include <gpxe/xfer.h>
00025 #include <gpxe/uri.h>
00026 #include <gpxe/socket.h>
00027 #include <gpxe/open.h>
00028 
00029 /** @file
00030  *
00031  * Data transfer interface opening
00032  *
00033  */
00034 
00035 /**
00036  * Open URI
00037  *
00038  * @v xfer              Data transfer interface
00039  * @v uri               URI
00040  * @ret rc              Return status code
00041  *
00042  * The URI will be regarded as being relative to the current working
00043  * URI (see churi()).
00044  */
00045 int xfer_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
00046         struct uri_opener *opener;
00047         struct uri *resolved_uri;
00048         int rc = -ENOTSUP;
00049 
00050         /* Resolve URI */
00051         resolved_uri = resolve_uri ( cwuri, uri );
00052         if ( ! resolved_uri )
00053                 return -ENOMEM;
00054 
00055         /* Find opener which supports this URI scheme */
00056         for_each_table_entry ( opener, URI_OPENERS ) {
00057                 if ( strcmp ( resolved_uri->scheme, opener->scheme ) == 0 ) {
00058                         DBGC ( xfer, "XFER %p opening %s URI\n",
00059                                xfer, opener->scheme );
00060                         rc = opener->open ( xfer, resolved_uri );
00061                         goto done;
00062                 }
00063         }
00064         DBGC ( xfer, "XFER %p attempted to open unsupported URI scheme "
00065                "\"%s\"\n", xfer, resolved_uri->scheme );
00066 
00067  done:
00068         uri_put ( resolved_uri );
00069         return rc;
00070 }
00071 
00072 /**
00073  * Open URI string
00074  *
00075  * @v xfer              Data transfer interface
00076  * @v uri_string        URI string (e.g. "http://etherboot.org/kernel")
00077  * @ret rc              Return status code
00078  *
00079  * The URI will be regarded as being relative to the current working
00080  * URI (see churi()).
00081  */
00082 int xfer_open_uri_string ( struct xfer_interface *xfer,
00083                            const char *uri_string ) {
00084         struct uri *uri;
00085         int rc;
00086 
00087         DBGC ( xfer, "XFER %p opening URI %s\n", xfer, uri_string );
00088 
00089         uri = parse_uri ( uri_string );
00090         if ( ! uri )
00091                 return -ENOMEM;
00092 
00093         rc = xfer_open_uri ( xfer, uri );
00094 
00095         uri_put ( uri );
00096         return rc;
00097 }
00098 
00099 /**
00100  * Open socket
00101  *
00102  * @v xfer              Data transfer interface
00103  * @v semantics         Communication semantics (e.g. SOCK_STREAM)
00104  * @v peer              Peer socket address
00105  * @v local             Local socket address, or NULL
00106  * @ret rc              Return status code
00107  */
00108 int xfer_open_socket ( struct xfer_interface *xfer, int semantics,
00109                        struct sockaddr *peer, struct sockaddr *local ) {
00110         struct socket_opener *opener;
00111 
00112         DBGC ( xfer, "XFER %p opening (%s,%s) socket\n", xfer,
00113                socket_semantics_name ( semantics ),
00114                socket_family_name ( peer->sa_family ) );
00115 
00116         for_each_table_entry ( opener, SOCKET_OPENERS ) {
00117                 if ( ( opener->semantics == semantics ) &&
00118                      ( opener->family == peer->sa_family ) ) {
00119                         return opener->open ( xfer, peer, local );
00120                 }
00121         }
00122 
00123         DBGC ( xfer, "XFER %p attempted to open unsupported socket type "
00124                "(%s,%s)\n", xfer, socket_semantics_name ( semantics ),
00125                socket_family_name ( peer->sa_family ) );
00126         return -ENOTSUP;
00127 }
00128 
00129 /**
00130  * Open location
00131  *
00132  * @v xfer              Data transfer interface
00133  * @v type              Location type
00134  * @v args              Remaining arguments depend upon location type
00135  * @ret rc              Return status code
00136  */
00137 int xfer_vopen ( struct xfer_interface *xfer, int type, va_list args ) {
00138         switch ( type ) {
00139         case LOCATION_URI_STRING: {
00140                 const char *uri_string = va_arg ( args, const char * );
00141 
00142                 return xfer_open_uri_string ( xfer, uri_string ); }
00143         case LOCATION_URI: {
00144                 struct uri *uri = va_arg ( args, struct uri * );
00145 
00146                 return xfer_open_uri ( xfer, uri ); }
00147         case LOCATION_SOCKET: {
00148                 int semantics = va_arg ( args, int );
00149                 struct sockaddr *peer = va_arg ( args, struct sockaddr * );
00150                 struct sockaddr *local = va_arg ( args, struct sockaddr * );
00151 
00152                 return xfer_open_socket ( xfer, semantics, peer, local ); }
00153         default:
00154                 DBGC ( xfer, "XFER %p attempted to open unsupported location "
00155                        "type %d\n", xfer, type );
00156                 return -ENOTSUP;
00157         }
00158 }
00159 
00160 /**
00161  * Open location
00162  *
00163  * @v xfer              Data transfer interface
00164  * @v type              Location type
00165  * @v ...               Remaining arguments depend upon location type
00166  * @ret rc              Return status code
00167  */
00168 int xfer_open ( struct xfer_interface *xfer, int type, ... ) {
00169         va_list args;
00170         int rc;
00171 
00172         va_start ( args, type );
00173         rc = xfer_vopen ( xfer, type, args );
00174         va_end ( args );
00175         return rc;
00176 }
00177 
00178 /**
00179  * Reopen location
00180  *
00181  * @v xfer              Data transfer interface
00182  * @v type              Location type
00183  * @v args              Remaining arguments depend upon location type
00184  * @ret rc              Return status code
00185  *
00186  * This will close the existing connection and open a new connection
00187  * using xfer_vopen().  It is intended to be used as a .vredirect
00188  * method handler.
00189  */
00190 int xfer_vreopen ( struct xfer_interface *xfer, int type, va_list args ) {
00191 
00192         /* Close existing connection */
00193         xfer_close ( xfer, 0 );
00194 
00195         /* Open new location */
00196         return xfer_vopen ( xfer, type, args );
00197 }

Generated on Tue Apr 6 20:00:51 2010 for gPXE by  doxygen 1.5.7.1