iPXE
resolv.c
Go to the documentation of this file.
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 =
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 */
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 
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 );
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 */
145 
146  /** Child name resolution interface */
147  struct interface child;
148  /** Current child 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 */
232 };
233 
234 /** Name resolution multiplexer child interface descriptor */
237  parent );
238 
239 /** Name resolution multiplexer parent interface operations */
241  INTF_OP ( intf_close, struct resolv_mux *, resmux_close ),
242 };
243 
244 /** Name resolution multiplexer parent interface descriptor */
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 */
309  /** Communication semantics (e.g. SOCK_STREAM) */
311  /** Stored local socket address, if applicable */
312  struct sockaddr local;
313  /** Stored local socket address exists */
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 */
344 };
345 
346 /** Named socket opener data transfer interface descriptor */
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 */
386 };
387 
388 /** Named socket opener resolver interface descriptor */
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 }
static void named_resolv_done(struct named_socket *named, struct sockaddr *sa)
Name resolved.
Definition: resolv.c:357
A process.
Definition: process.h:17
An object interface operation.
Definition: interface.h:17
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:249
#define INTF_INTF_FMT
printf() format string for INTF_INTF_DBG()
Definition: interface.h:297
A numeric name resolver.
Definition: resolv.c:81
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:343
struct interface xfer
Data transfer interface.
Definition: resolv.c:306
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:278
static struct interface_operation named_xfer_ops[]
Named socket opener data transfer interface operations.
Definition: resolv.c:341
static void resmux_close(struct resolv_mux *mux, int rc)
Close name resolution multiplexer.
Definition: resolv.c:188
struct resolver * resolver
Current child resolver.
Definition: resolv.c:149
struct interface_descriptor null_intf_desc
Null interface descriptor.
Definition: interface.c:61
#define table_start(table)
Get start of linker table.
Definition: tables.h:282
static struct interface_operation resmux_child_op[]
Name resolution multiplexer child interface operations.
Definition: resolv.c:230
int semantics
Communication semantics (e.g.
Definition: resolv.c:310
void resolv_done(struct interface *intf, struct sockaddr *sa)
Name resolved.
Definition: resolv.c:55
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
Error codes.
struct refcnt refcnt
Reference counter.
Definition: resolv.c:304
int have_local
Stored local socket address exists.
Definition: resolv.c:314
static size_t named_window(struct named_socket *named __unused)
Check flow control window.
Definition: resolv.c:335
Name resolution.
#define INTF_INTF_DBG(intf, dest)
printf() arguments for representing an object interface pair
Definition: interface.h:306
struct interface resolv
Name resolution interface.
Definition: resolv.c:85
static void process_init(struct process *process, struct process_descriptor *desc, struct refcnt *refcnt)
Initialise process and add to process list.
Definition: process.h:161
#define INTF_COL(intf)
Find debugging colourisation for an object interface.
Definition: interface.h:281
#define DBGC(...)
Definition: compiler.h:505
A process descriptor.
Definition: process.h:31
int rc
Overall status code.
Definition: resolv.c:91
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:107
A name resolution multiplexer.
Definition: resolv.c:140
#define PROC_DESC_ONCE(object_type, process, _step)
Define a process descriptor for a process that runs only once.
Definition: process.h:97
struct refcnt refcnt
Reference counter.
Definition: resolv.c:83
int sock_aton(const char *string, struct sockaddr *sa)
Parse socket address.
Definition: socket.c:59
struct sockaddr sa
Socket address to complete.
Definition: resolv.c:152
struct interface parent
Parent name resolution interface.
Definition: resolv.c:144
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition: xfer.c:116
void * intf_object(struct interface *intf)
Get pointer to object containing object interface.
Definition: interface.c:159
static int resmux_try(struct resolv_mux *mux)
Try current child name resolver.
Definition: resolv.c:166
Data transfer interfaces.
struct interface * intf
Original interface.
Definition: interface.h:158
A reference counter.
Definition: refcnt.h:26
static struct interface_descriptor named_resolv_desc
Named socket opener resolver interface descriptor.
Definition: resolv.c:389
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
char name[0]
Name to be resolved.
Definition: resolv.c:157
An object interface.
Definition: interface.h:124
struct sockaddr local
Stored local socket address, if applicable.
Definition: resolv.c:312
A named socket.
Definition: resolv.c:302
struct sockaddr sa
Definition: syslog.c:55
static void * dest
Definition: strings.h:176
static int numeric_resolv(struct interface *resolv, const char *name, struct sockaddr *sa)
Definition: resolv.c:104
void intf_unplug(struct interface *intf)
Unplug an object interface.
Definition: interface.c:117
Generalized socket address structure.
Definition: socket.h:96
An object interface descriptor.
Definition: interface.h:55
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
int(* resolv)(struct interface *resolv, const char *name, struct sockaddr *sa)
Start name resolution.
Definition: resolv.h:28
static void numeric_step(struct numeric_resolv *numeric)
Definition: resolv.c:94
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
static struct interface_operation resmux_parent_op[]
Name resolution multiplexer parent interface operations.
Definition: resolv.c:240
Location is a socket.
Definition: open.h:43
static struct interface_descriptor resmux_child_desc
Name resolution multiplexer child interface descriptor.
Definition: resolv.c:235
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
Processes.
Data transfer interface opening.
struct process process
Process.
Definition: resolv.c:87
static struct interface_operation named_resolv_op[]
Named socket opener resolver interface operations.
Definition: resolv.c:383
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
struct refcnt refcnt
Reference counter.
Definition: resolv.c:142
void intf_nullify(struct interface *intf)
Ignore all further operations on an object interface.
Definition: interface.c:129
A name resolver.
Definition: resolv.h:18
void intf_put(struct interface *intf)
Decrement reference count on an object interface.
Definition: interface.c:149
static void resmux_child_close(struct resolv_mux *mux, int rc)
Child finished resolution.
Definition: resolv.c:201
const char * name
Name of this resolver (e.g.
Definition: resolv.h:20
struct interface resolv
Name resolution interface.
Definition: resolv.c:308
#define table_end(table)
Get end of linker table.
Definition: tables.h:308
int resolv(struct interface *resolv, const char *name, struct sockaddr *sa)
Start name resolution.
Definition: resolv.c:257
#define INTF_DESC_PASSTHRU(object_type, intf, operations, passthru)
Define an object interface descriptor with pass-through interface.
Definition: interface.h:97
static struct interface_descriptor resmux_parent_desc
Name resolution multiplexer parent interface descriptor.
Definition: resolv.c:245
struct resolver numeric_resolver __resolver(RESOLV_NUMERIC)
static struct process_descriptor numeric_process_desc
Definition: resolv.c:101
struct mschapv2_challenge peer
Peer challenge.
Definition: mschapv2.h:12
static struct interface_descriptor named_xfer_desc
Named socket opener data transfer interface descriptor.
Definition: resolv.c:347
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:203
struct interface child
Child name resolution interface.
Definition: resolv.c:147
Socket addresses.
static void named_close(struct named_socket *named, int rc)
Terminate named socket opener.
Definition: resolv.c:323
#define resolv_done_TYPE(object_type)
Definition: resolv.h:45
#define RESOLV_NUMERIC
Numeric resolver priority.
Definition: resolv.h:33
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
#define intf_get_dest_op(intf, type, dest)
Get object interface destination and operation method.
Definition: interface.h:269
#define RESOLVERS
Resolvers table.
Definition: resolv.h:39
int xfer_open_named_socket(struct interface *xfer, int semantics, struct sockaddr *peer, const char *name, struct sockaddr *local)
Open named socket.
Definition: resolv.c:402
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
struct sockaddr sa
Completed socket address.
Definition: resolv.c:89
int xfer_redirect(struct interface *intf, int type,...)
Send redirection event.
Definition: xfer.c:238