iPXE
nslookup.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Patrick Plenefisch <phplenefisch@wpi.edu>.
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 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <ipxe/resolv.h>
27 #include <ipxe/tcpip.h>
28 #include <ipxe/monojob.h>
29 #include <ipxe/settings.h>
30 #include <usr/nslookup.h>
31 
32 /** @file
33  *
34  * Standalone name resolution
35  *
36  */
37 
38 /** A name resolution request */
39 struct nslookup {
40  /** Reference count for this object */
41  struct refcnt refcnt;
42 
43  /** Job control interface */
44  struct interface job;
45  /** Data transfer interface */
47 
48  /** Setting name */
49  char *setting_name;
50 };
51 
52 /**
53  * Terminate name resolution
54  *
55  * @v nslookup Name resolution request
56  * @v rc Reason for termination
57  */
58 static void nslookup_close ( struct nslookup *nslookup, int rc ) {
59 
60  /* Shut down interfaces */
63 }
64 
65 /**
66  * Handle resolved name
67  *
68  * @v nslookup Name resolution request
69  * @v sa Completed socket address
70  */
71 static void nslookup_resolv_done ( struct nslookup *nslookup,
72  struct sockaddr *sa ) {
73  struct sockaddr_in *sin;
74  struct sockaddr_in6 *sin6;
75  const struct setting_type *default_type;
76  struct settings *settings;
77  struct setting setting;
78  void *data;
79  size_t len;
80  int rc;
81 
82  /* Extract address */
83  switch ( sa->sa_family ) {
84  case AF_INET:
85  sin = ( ( struct sockaddr_in * ) sa );
86  data = &sin->sin_addr;
87  len = sizeof ( sin->sin_addr );
88  default_type = &setting_type_ipv4;
89  break;
90  case AF_INET6:
91  sin6 = ( ( struct sockaddr_in6 * ) sa );
92  data = &sin6->sin6_addr;
93  len = sizeof ( sin6->sin6_addr );
94  default_type = &setting_type_ipv6;
95  break;
96  default:
97  rc = -ENOTSUP;
98  goto err;
99  }
100 
101  /* Parse specified setting name */
104  &setting ) ) != 0 )
105  goto err;
106 
107  /* Apply default type if necessary */
108  if ( ! setting.type )
109  setting.type = default_type;
110 
111  /* Store in specified setting */
112  if ( ( rc = store_setting ( settings, &setting, data, len ) ) != 0 )
113  goto err;
114 
115  err:
116  /* Terminate name resolution */
118 }
119 
120 /** Name resolution resolver interface operations */
123  INTF_OP ( intf_close, struct nslookup *, nslookup_close ),
124 };
125 
126 /** Name resolution resolver interface descriptor */
130 
131 /** Name resolution job control interface operations */
133  INTF_OP ( intf_close, struct nslookup *, nslookup_close ),
134 };
135 
136 /** Name resolution job control interface descriptor */
138  INTF_DESC_PASSTHRU ( struct nslookup, job,
140 
141 /**
142  * Initiate standalone name resolution
143  *
144  * @v job Parent interface
145  * @v name Name to resolve
146  * @v setting_name Setting name
147  * @ret rc Return status code
148  */
149 static int resolv_setting ( struct interface *job, const char *name,
150  const char *setting_name ) {
151  struct nslookup *nslookup;
152  struct sockaddr sa;
153  char *setting_name_copy;
154  int rc;
155 
156  /* Allocate and initialise structure */
157  nslookup = zalloc ( sizeof ( *nslookup ) + strlen ( setting_name )
158  + 1 /* NUL */ );
159  if ( ! nslookup )
160  return -ENOMEM;
161  ref_init ( &nslookup->refcnt, NULL );
164  &nslookup->refcnt );
165  setting_name_copy = ( ( void * ) ( nslookup + 1 ) );
166  strcpy ( setting_name_copy, setting_name );
167  nslookup->setting_name = setting_name_copy;
168 
169  /* Start name resolution */
170  memset ( &sa, 0, sizeof ( sa ) );
171  if ( ( rc = resolv ( &nslookup->resolver, name, &sa ) ) != 0 )
172  goto err_resolv;
173 
174  /* Attach parent interface, mortalise self, and return */
175  intf_plug_plug ( &nslookup->job, job );
176  ref_put ( &nslookup->refcnt );
177  return 0;
178 
179  err_resolv:
180  ref_put ( &nslookup->refcnt );
181  return rc;
182 }
183 
184 /**
185  * Perform (blocking) standalone name resolution
186  *
187  * @v name Name to resolve
188  * @v setting_name Setting name
189  * @ret rc Return status code
190  */
191 int nslookup ( const char *name, const char *setting_name ) {
192  int rc;
193 
194  /* Perform name resolution */
195  if ( ( rc = resolv_setting ( &monojob, name, setting_name ) ) == 0 )
196  rc = monojob_wait ( NULL, 0 );
197  if ( rc != 0 ) {
198  printf ( "Could not resolve %s: %s\n", name, strerror ( rc ) );
199  return rc;
200  }
201 
202  return 0;
203 }
static void nslookup_close(struct nslookup *nslookup, int rc)
Terminate name resolution.
Definition: nslookup.c:58
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
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:278
#define AF_INET6
IPv6 Internet addresses.
Definition: socket.h:64
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
int monojob_wait(const char *string, unsigned long timeout)
Wait for single foreground job to complete.
Definition: monojob.c:81
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.
Standalone name resolution.
static struct interface_descriptor nslookup_resolver_desc
Name resolution resolver interface descriptor.
Definition: nslookup.c:127
struct refcnt refcnt
Reference count for this object.
Definition: nslookup.c:41
Name resolution.
sa_family_t sa_family
Socket address family.
Definition: socket.h:101
struct interface job
Job control interface.
Definition: nslookup.c:44
struct sockaddr_in6 sin6
Definition: syslog.c:58
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:107
int store_setting(struct settings *settings, const struct setting *setting, const void *data, size_t len)
Store value of setting.
Definition: settings.c:615
IPv4 socket address.
Definition: in.h:84
char * setting_name
Setting name.
Definition: nslookup.c:49
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
A reference counter.
Definition: refcnt.h:26
static int resolv_setting(struct interface *job, const char *name, const char *setting_name)
Initiate standalone name resolution.
Definition: nslookup.c:149
Single foreground job.
#define ENOMEM
Not enough space.
Definition: errno.h:534
static struct interface_operation nslookup_job_operations[]
Name resolution job control interface operations.
Definition: nslookup.c:132
FILE_LICENCE(GPL2_OR_LATER)
An object interface.
Definition: interface.h:124
struct interface resolver
Data transfer interface.
Definition: nslookup.c:46
struct sockaddr sa
Definition: syslog.c:55
Transport-network layer interface.
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:346
const struct setting_type * type
Setting type.
Definition: settings.h:36
struct settings * autovivify_child_settings(struct settings *parent, const char *name)
Find or create child settings block.
Definition: settings.c:306
struct interface monojob
Definition: monojob.c:56
Configuration settings.
static struct interface_operation nslookup_resolver_operations[]
Name resolution resolver interface operations.
Definition: nslookup.c:121
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
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
static struct interface_descriptor nslookup_job_desc
Name resolution job control interface descriptor.
Definition: nslookup.c:137
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
A settings block.
Definition: settings.h:132
A setting.
Definition: settings.h:23
int nslookup(const char *name, const char *setting_name)
Perform (blocking) standalone name resolution.
Definition: nslookup.c:191
struct in_addr sin_addr
IPv4 address.
Definition: in.h:100
static void nslookup_resolv_done(struct nslookup *nslookup, struct sockaddr *sa)
Handle resolved name.
Definition: nslookup.c:71
A name resolver.
Definition: resolv.h:18
uint8_t data[48]
Additional event data.
Definition: ena.h:22
IPv6 socket address.
Definition: in.h:117
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
struct sockaddr_in sin
Definition: syslog.c:57
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:203
int setting_name(struct settings *settings, const struct setting *setting, char *buf, size_t len)
Return full setting name.
Definition: settings.c:1606
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:63
A setting type.
Definition: settings.h:191
int parse_setting_name(char *name, get_child_settings_t get_child, struct settings **settings, struct setting *setting)
Parse setting name.
Definition: settings.c:1528
struct in6_addr sin6_addr
IPv6 address.
Definition: in.h:134
A name resolution request.
Definition: nslookup.c:39
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
void * memset(void *dest, int character, size_t len) __nonnull