summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/usr/nslookup.c
blob: eb2b08b426a1b16f62bb9ecc3b7eadd90f40ec83 (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
/*
 * Copyright (C) 2012 Patrick Plenefisch <phplenefisch@wpi.edu>.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ipxe/resolv.h>
#include <ipxe/tcpip.h>
#include <ipxe/monojob.h>
#include <ipxe/settings.h>
#include <usr/nslookup.h>

/** @file
 *
 * Standalone name resolution
 *
 */

/** A name resolution request */
struct nslookup {
	/** Reference count for this object */
	struct refcnt refcnt;

	/** Job control interface */
	struct interface job;
	/** Data transfer interface */
	struct interface resolver;

	/** Setting name */
	char *setting_name;
};

/**
 * Terminate name resolution
 *
 * @v nslookup		Name resolution request
 * @v rc		Reason for termination
 */
static void nslookup_close ( struct nslookup *nslookup, int rc ) {

	/* Shut down interfaces */
	intf_shutdown ( &nslookup->resolver, rc );
	intf_shutdown ( &nslookup->job, rc );
}

/**
 * Handle resolved name
 *
 * @v nslookup		Name resolution request
 * @v sa		Completed socket address
 */
static void nslookup_resolv_done ( struct nslookup *nslookup,
				   struct sockaddr *sa ) {
	struct sockaddr_in *sin;
	struct sockaddr_in6 *sin6;
	const struct setting_type *default_type;
	struct settings *settings;
	struct setting setting;
	void *data;
	size_t len;
	int rc;

	/* Extract address */
	switch ( sa->sa_family ) {
	case AF_INET:
		sin = ( ( struct sockaddr_in * ) sa );
		data = &sin->sin_addr;
		len = sizeof ( sin->sin_addr );
		default_type = &setting_type_ipv4;
		break;
	case AF_INET6:
		sin6 = ( ( struct sockaddr_in6 * ) sa );
		data = &sin6->sin6_addr;
		len = sizeof ( sin6->sin6_addr );
		default_type = &setting_type_ipv6;
		break;
	default:
		rc = -ENOTSUP;
		goto err;
	}

	/* Parse specified setting name */
	if ( ( rc = parse_setting_name ( nslookup->setting_name,
					 autovivify_child_settings, &settings,
					 &setting ) ) != 0 )
		goto err;

	/* Apply default type if necessary */
	if ( ! setting.type )
		setting.type = default_type;

	/* Store in specified setting */
	if ( ( rc = store_setting ( settings, &setting, data, len ) ) != 0 )
		goto err;

 err:
	/* Terminate name resolution */
	nslookup_close ( nslookup, rc );
}

/** Name resolution resolver interface operations */
static struct interface_operation nslookup_resolver_operations[] = {
	INTF_OP ( resolv_done, struct nslookup *, nslookup_resolv_done ),
	INTF_OP ( intf_close, struct nslookup *, nslookup_close ),
};

/** Name resolution resolver interface descriptor */
static struct interface_descriptor nslookup_resolver_desc =
	INTF_DESC_PASSTHRU ( struct nslookup, resolver,
			     nslookup_resolver_operations, job );

/** Name resolution job control interface operations */
static struct interface_operation nslookup_job_operations[] = {
	INTF_OP ( intf_close, struct nslookup *, nslookup_close ),
};

/** Name resolution job control interface descriptor */
static struct interface_descriptor nslookup_job_desc =
	INTF_DESC_PASSTHRU ( struct nslookup, job,
			     nslookup_job_operations, resolver );

/**
 * Initiate standalone name resolution
 *
 * @v job		Parent interface
 * @v name		Name to resolve
 * @v setting_name	Setting name
 * @ret rc		Return status code
 */
static int resolv_setting ( struct interface *job, const char *name,
			    const char *setting_name ) {
	struct nslookup *nslookup;
	struct sockaddr sa;
	char *setting_name_copy;
	int rc;

	/* Allocate and initialise structure */
	nslookup = zalloc ( sizeof ( *nslookup ) + strlen ( setting_name )
			    + 1 /* NUL */ );
	if ( ! nslookup )
		return -ENOMEM;
	ref_init ( &nslookup->refcnt, NULL );
	intf_init ( &nslookup->job, &nslookup_job_desc, &nslookup->refcnt );
	intf_init ( &nslookup->resolver, &nslookup_resolver_desc,
		    &nslookup->refcnt );
	setting_name_copy = ( ( void * ) ( nslookup + 1 ) );
	strcpy ( setting_name_copy, setting_name );
	nslookup->setting_name = setting_name_copy;

	/* Start name resolution */
	memset ( &sa, 0, sizeof ( sa ) );
	if ( ( rc = resolv ( &nslookup->resolver, name, &sa ) ) != 0 )
		goto err_resolv;

	/* Attach parent interface, mortalise self, and return */
	intf_plug_plug ( &nslookup->job, job );
	ref_put ( &nslookup->refcnt );
	return 0;

 err_resolv:
	ref_put ( &nslookup->refcnt );
	return rc;
}

/**
 * Perform (blocking) standalone name resolution
 *
 * @v name		Name to resolve
 * @v setting_name	Setting name
 * @ret rc		Return status code
 */
int nslookup ( const char *name, const char *setting_name ) {
	int rc;

	/* Perform name resolution */
	if ( ( rc = resolv_setting ( &monojob, name, setting_name ) ) == 0 )
		rc = monojob_wait ( NULL, 0 );
	if ( rc != 0 ) {
		printf ( "Could not resolve %s: %s\n", name, strerror ( rc ) );
		return rc;
	}

	return 0;
}