2 * Copyright (C) 2012 Patrick Plenefisch <phplenefisch@wpi.edu>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
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>
34 * Standalone name resolution
38 /** A name resolution request */
40 /** Reference count for this object */
43 /** Job control interface */
45 /** Data transfer interface */
46 struct interface resolver;
53 * Terminate name resolution
55 * @v nslookup Name resolution request
56 * @v rc Reason for termination
58 static void nslookup_close ( struct nslookup *nslookup, int rc ) {
60 /* Shut down interfaces */
61 intf_shutdown ( &nslookup->resolver, rc );
62 intf_shutdown ( &nslookup->job, rc );
66 * Handle resolved name
68 * @v nslookup Name resolution request
69 * @v sa Completed socket address
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;
83 switch ( sa->sa_family ) {
85 sin = ( ( struct sockaddr_in * ) sa );
86 data = &sin->sin_addr;
87 len = sizeof ( sin->sin_addr );
88 default_type = &setting_type_ipv4;
91 sin6 = ( ( struct sockaddr_in6 * ) sa );
92 data = &sin6->sin6_addr;
93 len = sizeof ( sin6->sin6_addr );
94 default_type = &setting_type_ipv6;
101 /* Parse specified setting name */
102 if ( ( rc = parse_setting_name ( nslookup->setting_name,
103 autovivify_child_settings, &settings,
107 /* Apply default type if necessary */
108 if ( ! setting.type )
109 setting.type = default_type;
111 /* Store in specified setting */
112 if ( ( rc = store_setting ( settings, &setting, data, len ) ) != 0 )
116 /* Terminate name resolution */
117 nslookup_close ( nslookup, rc );
120 /** Name resolution resolver interface operations */
121 static struct interface_operation nslookup_resolver_operations[] = {
122 INTF_OP ( resolv_done, struct nslookup *, nslookup_resolv_done ),
123 INTF_OP ( intf_close, struct nslookup *, nslookup_close ),
126 /** Name resolution resolver interface descriptor */
127 static struct interface_descriptor nslookup_resolver_desc =
128 INTF_DESC_PASSTHRU ( struct nslookup, resolver,
129 nslookup_resolver_operations, job );
131 /** Name resolution job control interface operations */
132 static struct interface_operation nslookup_job_operations[] = {
133 INTF_OP ( intf_close, struct nslookup *, nslookup_close ),
136 /** Name resolution job control interface descriptor */
137 static struct interface_descriptor nslookup_job_desc =
138 INTF_DESC_PASSTHRU ( struct nslookup, job,
139 nslookup_job_operations, resolver );
142 * Initiate standalone name resolution
144 * @v job Parent interface
145 * @v name Name to resolve
146 * @v setting_name Setting name
147 * @ret rc Return status code
149 static int resolv_setting ( struct interface *job, const char *name,
150 const char *setting_name ) {
151 struct nslookup *nslookup;
153 char *setting_name_copy;
156 /* Allocate and initialise structure */
157 nslookup = zalloc ( sizeof ( *nslookup ) + strlen ( setting_name )
161 ref_init ( &nslookup->refcnt, NULL );
162 intf_init ( &nslookup->job, &nslookup_job_desc, &nslookup->refcnt );
163 intf_init ( &nslookup->resolver, &nslookup_resolver_desc,
165 setting_name_copy = ( ( void * ) ( nslookup + 1 ) );
166 strcpy ( setting_name_copy, setting_name );
167 nslookup->setting_name = setting_name_copy;
169 /* Start name resolution */
170 memset ( &sa, 0, sizeof ( sa ) );
171 if ( ( rc = resolv ( &nslookup->resolver, name, &sa ) ) != 0 )
174 /* Attach parent interface, mortalise self, and return */
175 intf_plug_plug ( &nslookup->job, job );
176 ref_put ( &nslookup->refcnt );
180 ref_put ( &nslookup->refcnt );
185 * Perform (blocking) standalone name resolution
187 * @v name Name to resolve
188 * @v setting_name Setting name
189 * @ret rc Return status code
191 int nslookup ( const char *name, const char *setting_name ) {
194 /* Perform name resolution */
195 if ( ( rc = resolv_setting ( &monojob, name, setting_name ) ) == 0 )
196 rc = monojob_wait ( NULL, 0 );
198 printf ( "Could not resolve %s: %s\n", name, strerror ( rc ) );