summaryrefslogtreecommitdiffstats
path: root/src/core/open.c
blob: ffcb4e292c8fe1a491394ed75f35380c2b1b918c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <gpxe/xfer.h>
#include <gpxe/uri.h>
#include <gpxe/socket.h>
#include <gpxe/open.h>

/** @file
 *
 * Data transfer interface opening
 *
 */

/** Registered URI openers */
static struct uri_opener uri_openers[0]
	__table_start ( struct uri_opener, uri_openers );
static struct uri_opener uri_openers_end[0]
	__table_end ( struct uri_opener, uri_openers );

/** Registered socket openers */
static struct socket_opener socket_openers[0]
	__table_start ( struct socket_opener, socket_openers );
static struct socket_opener socket_openers_end[0]
	__table_end ( struct socket_opener, socket_openers );

/**
 * Open URI
 *
 * @v xfer		Data-transfer interface
 * @v uri_string	URI string (e.g. "http://etherboot.org/kernel")
 * @ret rc		Return status code
 */
int open_uri ( struct xfer_interface *xfer, const char *uri_string ) {
	struct uri *uri;
	struct uri_opener *opener;

	DBGC ( xfer, "XFER %p opening URI %s\n", xfer, uri_string );

	uri = parse_uri ( uri_string );
	if ( ! uri )
		return -ENOMEM;

	for ( opener = uri_openers ; opener < uri_openers_end ; opener++ ) {
		if ( strcmp ( uri->scheme, opener->scheme ) == 0 ) {
			return opener->open ( xfer, uri );
		}
	}

	DBGC ( xfer, "XFER %p attempted to open unsupported URI scheme "
	       "\"%s\"\n", xfer, uri->scheme );
	free_uri ( uri );
	return -ENOTSUP;
}

/**
 * Open socket
 *
 * @v xfer		Data-transfer interface
 * @v domain		Communication domain (e.g. PF_INET)
 * @v type		Communication semantics (e.g. SOCK_STREAM)
 */
int open_socket ( struct xfer_interface *xfer,
		  int domain, int type, struct sockaddr *sa ) {
	struct socket_opener *opener;

	DBGC ( xfer, "XFER %p opening (%s,%s) socket\n", xfer,
	       socket_domain_name ( domain ), socket_type_name ( type ) );

	for ( opener = socket_openers; opener < socket_openers_end; opener++ ){
		if ( ( opener->domain == domain ) &&
		     ( opener->type == type ) ) {
			return opener->open ( xfer, sa );
		}
	}

	DBGC ( xfer, "XFER %p attempted to open unsupported socket type "
	       "(%s,%s)\n", xfer, socket_domain_name ( domain ),
	       socket_type_name ( type ) );
	return -ENOTSUP;
}

/**
 * Open location
 *
 * @v xfer		Data-transfer interface
 * @v type		Location type
 * @v args		Remaining arguments depend upon location type
 * @ret rc		Return status code
 */
int vopen ( struct xfer_interface *xfer, int type, va_list args ) {
	switch ( type ) {
	case LOCATION_URI: {
		const char *uri_string = va_arg ( args, const char * );

		return open_uri ( xfer, uri_string ); }
	case LOCATION_SOCKET: {
		int domain = va_arg ( args, int );
		int type = va_arg ( args, int );
		struct sockaddr *sa = va_arg ( args, struct sockaddr * );

		return open_socket ( xfer, domain, type, sa ); }
	default:
		DBGC ( xfer, "XFER %p attempted to open unsupported location "
		       "type %d\n", xfer, type );
		return -ENOTSUP;
	}
}

/**
 * Open location
 *
 * @v xfer		Data-transfer interface
 * @v type		Location type
 * @v ...		Remaining arguments depend upon location type
 * @ret rc		Return status code
 */
int open ( struct xfer_interface *xfer, int type, ... ) {
	va_list args;
	int rc;

	va_start ( args, type );
	rc = vopen ( xfer, type, args );
	va_end ( args );
	return rc;
}