summaryrefslogtreecommitdiffstats
path: root/debian/grub-extras/disabled/gpxe/src/core/resolv.c
blob: 6f01f93e94cf03112f760dff4b04db25627b3b7c (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * 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.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <gpxe/in.h>
#include <gpxe/xfer.h>
#include <gpxe/open.h>
#include <gpxe/process.h>
#include <gpxe/resolv.h>

/** @file
 *
 * Name resolution
 *
 */

/***************************************************************************
 *
 * Name resolution interfaces
 *
 ***************************************************************************
 */

/**
 * Name resolution completed
 *
 * @v resolv		Name resolution interface
 * @v sa		Completed socket address (if successful)
 * @v rc		Final status code
 */
void resolv_done ( struct resolv_interface *resolv, struct sockaddr *sa,
		   int rc ) {
	struct resolv_interface *dest = resolv_get_dest ( resolv );

	resolv_unplug ( resolv );
	dest->op->done ( dest, sa, rc );
	resolv_put ( dest );
}

/**
 * Ignore name resolution done() event
 *
 * @v resolv		Name resolution interface
 * @v sa		Completed socket address (if successful)
 * @v rc		Final status code
 */
void ignore_resolv_done ( struct resolv_interface *resolv __unused,
			  struct sockaddr *sa __unused, int rc __unused ) {
	/* Do nothing */
}

/** Null name resolution interface operations */
struct resolv_interface_operations null_resolv_ops = {
	.done		= ignore_resolv_done,
};

/** Null name resolution interface */
struct resolv_interface null_resolv = {
	.intf = {
		.dest = &null_resolv.intf,
		.refcnt = NULL,
	},
	.op = &null_resolv_ops,
};

/***************************************************************************
 *
 * Numeric name resolver
 *
 ***************************************************************************
 */

/** A numeric name resolver */
struct numeric_resolv {
	/** Reference counter */
	struct refcnt refcnt;
	/** Name resolution interface */
	struct resolv_interface resolv;
	/** Process */
	struct process process;
	/** Completed socket address */
	struct sockaddr sa;
	/** Overall status code */
	int rc;
};

static void numeric_step ( struct process *process ) {
	struct numeric_resolv *numeric =
		container_of ( process, struct numeric_resolv, process );
	
	resolv_done ( &numeric->resolv, &numeric->sa, numeric->rc );
	process_del ( process );
}

static int numeric_resolv ( struct resolv_interface *resolv,
			    const char *name, struct sockaddr *sa ) {
	struct numeric_resolv *numeric;
	struct sockaddr_in *sin;

	/* Allocate and initialise structure */
	numeric = zalloc ( sizeof ( *numeric ) );
	if ( ! numeric )
		return -ENOMEM;
	resolv_init ( &numeric->resolv, &null_resolv_ops, &numeric->refcnt );
	process_init ( &numeric->process, numeric_step, &numeric->refcnt );
	memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );

	DBGC ( numeric, "NUMERIC %p attempting to resolve \"%s\"\n",
	       numeric, name );

	/* Attempt to resolve name */
	sin = ( ( struct sockaddr_in * ) &numeric->sa );
	sin->sin_family = AF_INET;
	if ( inet_aton ( name, &sin->sin_addr ) == 0 )
		numeric->rc = -EINVAL;

	/* Attach to parent interface, mortalise self, and return */
	resolv_plug_plug ( &numeric->resolv, resolv );
	ref_put ( &numeric->refcnt );
	return 0;
}

struct resolver numeric_resolver __resolver ( RESOLV_NUMERIC ) = {
	.name = "NUMERIC",
	.resolv = numeric_resolv,
};

/***************************************************************************
 *
 * Name resolution multiplexer
 *
 ***************************************************************************
 */

/** A name resolution multiplexer */
struct resolv_mux {
	/** Reference counter */
	struct refcnt refcnt;
	/** Parent name resolution interface */
	struct resolv_interface parent;

	/** Child name resolution interface */
	struct resolv_interface child;
	/** Current child resolver */
	struct resolver *resolver;

	/** Socket address to complete */
	struct sockaddr sa;
	/** Name to be resolved
	 *
	 * Must be at end of structure
	 */
	char name[0];
};

/**
 * Try current child name resolver
 *
 * @v mux		Name resolution multiplexer
 * @ret rc		Return status code
 */
static int resolv_mux_try ( struct resolv_mux *mux ) {
	struct resolver *resolver = mux->resolver;
	int rc;

	DBGC ( mux, "RESOLV %p trying method %s\n", mux, resolver->name );

	if ( ( rc = resolver->resolv ( &mux->child, mux->name,
				       &mux->sa ) ) != 0 ) {
		DBGC ( mux, "RESOLV %p could not use method %s: %s\n",
		       mux, resolver->name, strerror ( rc ) );
		return rc;
	}

	return 0;
}

/**
 * Handle done() event from child name resolver
 *
 * @v resolv		Child name resolution interface
 * @v sa		Completed socket address (if successful)
 * @v rc		Final status code
 */
static void resolv_mux_done ( struct resolv_interface *resolv,
			      struct sockaddr *sa, int rc ) {
	struct resolv_mux *mux =
		container_of ( resolv, struct resolv_mux, child );

	/* Unplug child */
	resolv_unplug ( &mux->child );

	/* If this resolution succeeded, stop now */
	if ( rc == 0 ) {
		DBGC ( mux, "RESOLV %p succeeded using method %s\n",
		       mux, mux->resolver->name );
		goto finished;
	}

	/* Attempt next child resolver, if possible */
	mux->resolver++;
	if ( mux->resolver >= table_end ( RESOLVERS ) ) {
		DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
		goto finished;
	}
	if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
		goto finished;

	/* Next resolver is now running */
	return;
	
 finished:
	resolv_done ( &mux->parent, sa, rc );
}

/** Name resolution multiplexer operations */
static struct resolv_interface_operations resolv_mux_child_ops = {
	.done		= resolv_mux_done,
};

/**
 * Start name resolution
 *
 * @v resolv		Name resolution interface
 * @v name		Name to resolve
 * @v sa		Socket address to complete
 * @ret rc		Return status code
 */
int resolv ( struct resolv_interface *resolv, const char *name,
	     struct sockaddr *sa ) {
	struct resolv_mux *mux;
	size_t name_len = ( strlen ( name ) + 1 );
	int rc;

	/* Allocate and initialise structure */
	mux = zalloc ( sizeof ( *mux ) + name_len );
	if ( ! mux )
		return -ENOMEM;
	resolv_init ( &mux->parent, &null_resolv_ops, &mux->refcnt );
	resolv_init ( &mux->child, &resolv_mux_child_ops, &mux->refcnt );
	mux->resolver = table_start ( RESOLVERS );
	memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
	memcpy ( mux->name, name, name_len );

	DBGC ( mux, "RESOLV %p attempting to resolve \"%s\"\n", mux, name );

	/* Start first resolver in chain.  There will always be at
	 * least one resolver (the numeric resolver), so no need to
	 * check for the zero-resolvers-available case.
	 */
	if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
		goto err;

	/* Attach parent interface, mortalise self, and return */
	resolv_plug_plug ( &mux->parent, resolv );
	ref_put ( &mux->refcnt );
	return 0;

 err:
	ref_put ( &mux->refcnt );
	return rc;	
}

/***************************************************************************
 *
 * Named socket opening
 *
 ***************************************************************************
 */

/** A named socket */
struct named_socket {
	/** Reference counter */
	struct refcnt refcnt;
	/** Data transfer interface */
	struct xfer_interface xfer;
	/** Name resolution interface */
	struct resolv_interface resolv;
	/** Communication semantics (e.g. SOCK_STREAM) */
	int semantics;
	/** Stored local socket address, if applicable */
	struct sockaddr local;
	/** Stored local socket address exists */
	int have_local;
};

/**
 * Finish using named socket
 *
 * @v named		Named socket
 * @v rc		Reason for finish
 */
static void named_done ( struct named_socket *named, int rc ) {

	/* Close all interfaces */
	resolv_nullify ( &named->resolv );
	xfer_nullify ( &named->xfer );
	xfer_close ( &named->xfer, rc );
}

/**
 * Handle close() event
 *
 * @v xfer		Data transfer interface
 * @v rc		Reason for close
 */
static void named_xfer_close ( struct xfer_interface *xfer, int rc ) {
	struct named_socket *named =
		container_of ( xfer, struct named_socket, xfer );

	named_done ( named, rc );
}

/** Named socket opener data transfer interface operations */
static struct xfer_interface_operations named_xfer_ops = {
	.close		= named_xfer_close,
	.vredirect	= ignore_xfer_vredirect,
	.window		= no_xfer_window,
	.alloc_iob	= default_xfer_alloc_iob,
	.deliver_iob	= xfer_deliver_as_raw,
	.deliver_raw	= ignore_xfer_deliver_raw,
};

/**
 * Handle done() event
 *
 * @v resolv		Name resolution interface
 * @v sa		Completed socket address (if successful)
 * @v rc		Final status code
 */
static void named_resolv_done ( struct resolv_interface *resolv,
				struct sockaddr *sa, int rc ) {
	struct named_socket *named =
		container_of ( resolv, struct named_socket, resolv );

	/* Redirect if name resolution was successful */
	if ( rc == 0 ) {
		rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
				     named->semantics, sa,
				     ( named->have_local ?
				       &named->local : NULL ) );
	}

	/* Terminate resolution */
	named_done ( named, rc );
}

/** Named socket opener name resolution interface operations */
static struct resolv_interface_operations named_resolv_ops = {
	.done		= named_resolv_done,
};

/**
 * Open named socket
 *
 * @v semantics		Communication semantics (e.g. SOCK_STREAM)
 * @v peer		Peer socket address to complete
 * @v name		Name to resolve
 * @v local		Local socket address, or NULL
 * @ret rc		Return status code
 */
int xfer_open_named_socket ( struct xfer_interface *xfer, int semantics,
			     struct sockaddr *peer, const char *name,
			     struct sockaddr *local ) {
	struct named_socket *named;
	int rc;

	/* Allocate and initialise structure */
	named = zalloc ( sizeof ( *named ) );
	if ( ! named )
		return -ENOMEM;
	xfer_init ( &named->xfer, &named_xfer_ops, &named->refcnt );
	resolv_init ( &named->resolv, &named_resolv_ops, &named->refcnt );
	named->semantics = semantics;
	if ( local ) {
		memcpy ( &named->local, local, sizeof ( named->local ) );
		named->have_local = 1;
	}

	DBGC ( named, "RESOLV %p opening named socket \"%s\"\n",
	       named, name );

	/* Start name resolution */
	if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
		goto err;

	/* Attach parent interface, mortalise self, and return */
	xfer_plug_plug ( &named->xfer, xfer );
	ref_put ( &named->refcnt );
	return 0;

 err:
	ref_put ( &named->refcnt );
	return rc;
}