[process] Include process name in debug messages
[ipxe.git] / src / core / resolv.c
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., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <ipxe/xfer.h>
31 #include <ipxe/open.h>
32 #include <ipxe/process.h>
33 #include <ipxe/socket.h>
34 #include <ipxe/resolv.h>
35
36 /** @file
37 *
38 * Name resolution
39 *
40 */
41
42 /***************************************************************************
43 *
44 * Name resolution interfaces
45 *
46 ***************************************************************************
47 */
48
49 /**
50 * Name resolved
51 *
52 * @v intf Object interface
53 * @v sa Completed socket address (if successful)
54 */
55 void resolv_done ( struct interface *intf, struct sockaddr *sa ) {
56 struct interface *dest;
57 resolv_done_TYPE ( void * ) *op =
58 intf_get_dest_op ( intf, resolv_done, &dest );
59 void *object = intf_object ( dest );
60
61 DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " resolv_done\n",
62 INTF_INTF_DBG ( intf, dest ) );
63
64 if ( op ) {
65 op ( object, sa );
66 } else {
67 /* Default is to ignore resolutions */
68 }
69
70 intf_put ( dest );
71 }
72
73 /***************************************************************************
74 *
75 * Numeric name resolver
76 *
77 ***************************************************************************
78 */
79
80 /** A numeric name resolver */
81 struct numeric_resolv {
82 /** Reference counter */
83 struct refcnt refcnt;
84 /** Name resolution interface */
85 struct interface resolv;
86 /** Process */
87 struct process process;
88 /** Completed socket address */
89 struct sockaddr sa;
90 /** Overall status code */
91 int rc;
92 };
93
94 static void numeric_step ( struct numeric_resolv *numeric ) {
95
96 if ( numeric->rc == 0 )
97 resolv_done ( &numeric->resolv, &numeric->sa );
98 intf_shutdown ( &numeric->resolv, numeric->rc );
99 }
100
101 static struct process_descriptor numeric_process_desc =
102 PROC_DESC_ONCE ( struct numeric_resolv, process, numeric_step );
103
104 static int numeric_resolv ( struct interface *resolv,
105 const char *name, struct sockaddr *sa ) {
106 struct numeric_resolv *numeric;
107
108 /* Allocate and initialise structure */
109 numeric = zalloc ( sizeof ( *numeric ) );
110 if ( ! numeric )
111 return -ENOMEM;
112 ref_init ( &numeric->refcnt, NULL );
113 intf_init ( &numeric->resolv, &null_intf_desc, &numeric->refcnt );
114 process_init ( &numeric->process, &numeric_process_desc,
115 &numeric->refcnt );
116 memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );
117
118 /* Attempt to resolve name */
119 numeric->rc = sock_aton ( name, &numeric->sa );
120
121 /* Attach to parent interface, mortalise self, and return */
122 intf_plug_plug ( &numeric->resolv, resolv );
123 ref_put ( &numeric->refcnt );
124 return 0;
125 }
126
127 struct resolver numeric_resolver __resolver ( RESOLV_NUMERIC ) = {
128 .name = "NUMERIC",
129 .resolv = numeric_resolv,
130 };
131
132 /***************************************************************************
133 *
134 * Name resolution multiplexer
135 *
136 ***************************************************************************
137 */
138
139 /** A name resolution multiplexer */
140 struct resolv_mux {
141 /** Reference counter */
142 struct refcnt refcnt;
143 /** Parent name resolution interface */
144 struct interface parent;
145
146 /** Child name resolution interface */
147 struct interface child;
148 /** Current child resolver */
149 struct resolver *resolver;
150
151 /** Socket address to complete */
152 struct sockaddr sa;
153 /** Name to be resolved
154 *
155 * Must be at end of structure
156 */
157 char name[0];
158 };
159
160 /**
161 * Try current child name resolver
162 *
163 * @v mux Name resolution multiplexer
164 * @ret rc Return status code
165 */
166 static int resmux_try ( struct resolv_mux *mux ) {
167 struct resolver *resolver = mux->resolver;
168 int rc;
169
170 DBGC ( mux, "RESOLV %p trying method %s\n", mux, resolver->name );
171
172 if ( ( rc = resolver->resolv ( &mux->child, mux->name,
173 &mux->sa ) ) != 0 ) {
174 DBGC ( mux, "RESOLV %p could not use method %s: %s\n",
175 mux, resolver->name, strerror ( rc ) );
176 return rc;
177 }
178
179 return 0;
180 }
181
182 /**
183 * Close name resolution multiplexer
184 *
185 * @v mux Name resolution multiplexer
186 * @v rc Reason for close
187 */
188 static void resmux_close ( struct resolv_mux *mux, int rc ) {
189
190 /* Shut down all interfaces */
191 intf_shutdown ( &mux->child, rc );
192 intf_shutdown ( &mux->parent, rc );
193 }
194
195 /**
196 * Child finished resolution
197 *
198 * @v mux Name resolution multiplexer
199 * @v rc Return status code
200 */
201 static void resmux_child_close ( struct resolv_mux *mux, int rc ) {
202
203 /* Restart child interface */
204 intf_restart ( &mux->child, rc );
205
206 /* If this resolution succeeded, stop now */
207 if ( rc == 0 ) {
208 DBGC ( mux, "RESOLV %p succeeded using method %s\n",
209 mux, mux->resolver->name );
210 goto finished;
211 }
212
213 /* Attempt next child resolver, if possible */
214 mux->resolver++;
215 if ( mux->resolver >= table_end ( RESOLVERS ) ) {
216 DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
217 goto finished;
218 }
219 if ( ( rc = resmux_try ( mux ) ) != 0 )
220 goto finished;
221
222 /* Next resolver is now running */
223 return;
224
225 finished:
226 resmux_close ( mux, rc );
227 }
228
229 /** Name resolution multiplexer child interface operations */
230 static struct interface_operation resmux_child_op[] = {
231 INTF_OP ( intf_close, struct resolv_mux *, resmux_child_close ),
232 };
233
234 /** Name resolution multiplexer child interface descriptor */
235 static struct interface_descriptor resmux_child_desc =
236 INTF_DESC_PASSTHRU ( struct resolv_mux, child, resmux_child_op,
237 parent );
238
239 /** Name resolution multiplexer parent interface operations */
240 static struct interface_operation resmux_parent_op[] = {
241 INTF_OP ( intf_close, struct resolv_mux *, resmux_close ),
242 };
243
244 /** Name resolution multiplexer parent interface descriptor */
245 static struct interface_descriptor resmux_parent_desc =
246 INTF_DESC_PASSTHRU ( struct resolv_mux, parent, resmux_parent_op,
247 child );
248
249 /**
250 * Start name resolution
251 *
252 * @v resolv Name resolution interface
253 * @v name Name to resolve
254 * @v sa Socket address to complete
255 * @ret rc Return status code
256 */
257 int resolv ( struct interface *resolv, const char *name,
258 struct sockaddr *sa ) {
259 struct resolv_mux *mux;
260 size_t name_len = ( strlen ( name ) + 1 );
261 int rc;
262
263 /* Allocate and initialise structure */
264 mux = zalloc ( sizeof ( *mux ) + name_len );
265 if ( ! mux )
266 return -ENOMEM;
267 ref_init ( &mux->refcnt, NULL );
268 intf_init ( &mux->parent, &resmux_parent_desc, &mux->refcnt );
269 intf_init ( &mux->child, &resmux_child_desc, &mux->refcnt );
270 mux->resolver = table_start ( RESOLVERS );
271 if ( sa )
272 memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
273 memcpy ( mux->name, name, name_len );
274
275 DBGC ( mux, "RESOLV %p attempting to resolve \"%s\"\n", mux, name );
276
277 /* Start first resolver in chain. There will always be at
278 * least one resolver (the numeric resolver), so no need to
279 * check for the zero-resolvers-available case.
280 */
281 if ( ( rc = resmux_try ( mux ) ) != 0 )
282 goto err;
283
284 /* Attach parent interface, mortalise self, and return */
285 intf_plug_plug ( &mux->parent, resolv );
286 ref_put ( &mux->refcnt );
287 return 0;
288
289 err:
290 ref_put ( &mux->refcnt );
291 return rc;
292 }
293
294 /***************************************************************************
295 *
296 * Named socket opening
297 *
298 ***************************************************************************
299 */
300
301 /** A named socket */
302 struct named_socket {
303 /** Reference counter */
304 struct refcnt refcnt;
305 /** Data transfer interface */
306 struct interface xfer;
307 /** Name resolution interface */
308 struct interface resolv;
309 /** Communication semantics (e.g. SOCK_STREAM) */
310 int semantics;
311 /** Stored local socket address, if applicable */
312 struct sockaddr local;
313 /** Stored local socket address exists */
314 int have_local;
315 };
316
317 /**
318 * Terminate named socket opener
319 *
320 * @v named Named socket
321 * @v rc Reason for termination
322 */
323 static void named_close ( struct named_socket *named, int rc ) {
324 /* Shut down interfaces */
325 intf_shutdown ( &named->resolv, rc );
326 intf_shutdown ( &named->xfer, rc );
327 }
328
329 /**
330 * Check flow control window
331 *
332 * @v named Named socket
333 * @ret len Length of window
334 */
335 static size_t named_window ( struct named_socket *named __unused ) {
336 /* Not ready for data until we have redirected away */
337 return 0;
338 }
339
340 /** Named socket opener data transfer interface operations */
341 static struct interface_operation named_xfer_ops[] = {
342 INTF_OP ( xfer_window, struct named_socket *, named_window ),
343 INTF_OP ( intf_close, struct named_socket *, named_close ),
344 };
345
346 /** Named socket opener data transfer interface descriptor */
347 static struct interface_descriptor named_xfer_desc =
348 INTF_DESC_PASSTHRU ( struct named_socket, xfer, named_xfer_ops,
349 resolv );
350
351 /**
352 * Name resolved
353 *
354 * @v named Named socket
355 * @v sa Completed socket address
356 */
357 static void named_resolv_done ( struct named_socket *named,
358 struct sockaddr *sa ) {
359 int rc;
360
361 /* Nullify data transfer interface */
362 intf_nullify ( &named->xfer );
363
364 /* Redirect data-xfer interface */
365 if ( ( rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
366 named->semantics, sa,
367 ( named->have_local ?
368 &named->local : NULL ) ) ) != 0 ) {
369 /* Redirection failed - do not unplug data-xfer interface */
370 DBGC ( named, "NAMED %p could not redirect: %s\n",
371 named, strerror ( rc ) );
372 } else {
373 /* Redirection succeeded - unplug data-xfer interface */
374 DBGC ( named, "NAMED %p redirected successfully\n", named );
375 intf_unplug ( &named->xfer );
376 }
377
378 /* Terminate named socket opener */
379 named_close ( named, rc );
380 }
381
382 /** Named socket opener resolver interface operations */
383 static struct interface_operation named_resolv_op[] = {
384 INTF_OP ( intf_close, struct named_socket *, named_close ),
385 INTF_OP ( resolv_done, struct named_socket *, named_resolv_done ),
386 };
387
388 /** Named socket opener resolver interface descriptor */
389 static struct interface_descriptor named_resolv_desc =
390 INTF_DESC_PASSTHRU ( struct named_socket, resolv, named_resolv_op,
391 xfer );
392
393 /**
394 * Open named socket
395 *
396 * @v semantics Communication semantics (e.g. SOCK_STREAM)
397 * @v peer Peer socket address to complete
398 * @v name Name to resolve
399 * @v local Local socket address, or NULL
400 * @ret rc Return status code
401 */
402 int xfer_open_named_socket ( struct interface *xfer, int semantics,
403 struct sockaddr *peer, const char *name,
404 struct sockaddr *local ) {
405 struct named_socket *named;
406 int rc;
407
408 /* Allocate and initialise structure */
409 named = zalloc ( sizeof ( *named ) );
410 if ( ! named )
411 return -ENOMEM;
412 ref_init ( &named->refcnt, NULL );
413 intf_init ( &named->xfer, &named_xfer_desc, &named->refcnt );
414 intf_init ( &named->resolv, &named_resolv_desc, &named->refcnt );
415 named->semantics = semantics;
416 if ( local ) {
417 memcpy ( &named->local, local, sizeof ( named->local ) );
418 named->have_local = 1;
419 }
420
421 DBGC ( named, "NAMED %p opening \"%s\"\n",
422 named, name );
423
424 /* Start name resolution */
425 if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
426 goto err;
427
428 /* Attach parent interface, mortalise self, and return */
429 intf_plug_plug ( &named->xfer, xfer );
430 ref_put ( &named->refcnt );
431 return 0;
432
433 err:
434 ref_put ( &named->refcnt );
435 return rc;
436 }