iPXE
Data Structures | Functions | Variables
nslookup.c File Reference

Standalone name resolution. More...

#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>

Go to the source code of this file.

Data Structures

struct  nslookup
 A name resolution request. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER)
 
 FILE_SECBOOT (PERMITTED)
 
static void nslookup_close (struct nslookup *nslookup, int rc)
 Terminate name resolution. More...
 
static void nslookup_resolv_done (struct nslookup *nslookup, struct sockaddr *sa)
 Handle resolved name. More...
 
static int resolv_setting (struct interface *job, const char *name, const char *setting_name)
 Initiate standalone name resolution. More...
 
int nslookup (const char *name, const char *setting_name)
 Perform (blocking) standalone name resolution. More...
 

Variables

static struct interface_operation nslookup_resolver_operations []
 Name resolution resolver interface operations. More...
 
static struct interface_descriptor nslookup_resolver_desc
 Name resolution resolver interface descriptor. More...
 
static struct interface_operation nslookup_job_operations []
 Name resolution job control interface operations. More...
 
static struct interface_descriptor nslookup_job_desc
 Name resolution job control interface descriptor. More...
 

Detailed Description

Standalone name resolution.

Definition in file nslookup.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ nslookup_close()

static void nslookup_close ( struct nslookup nslookup,
int  rc 
)
static

Terminate name resolution.

Parameters
nslookupName resolution request
rcReason for termination

Definition at line 59 of file nslookup.c.

59  {
60 
61  /* Shut down interfaces */
64 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:279
struct interface job
Job control interface.
Definition: nslookup.c:45
struct interface resolver
Data transfer interface.
Definition: nslookup.c:47
A name resolution request.
Definition: nslookup.c:40

References intf_shutdown(), nslookup::job, rc, and nslookup::resolver.

Referenced by nslookup_resolv_done().

◆ nslookup_resolv_done()

static void nslookup_resolv_done ( struct nslookup nslookup,
struct sockaddr sa 
)
static

Handle resolved name.

Parameters
nslookupName resolution request
saCompleted socket address

Definition at line 72 of file nslookup.c.

73  {
74  struct sockaddr_in *sin;
75  struct sockaddr_in6 *sin6;
76  const struct setting_type *default_type;
77  struct settings *settings;
78  struct setting setting;
79  void *data;
80  size_t len;
81  int rc;
82 
83  /* Extract address */
84  switch ( sa->sa_family ) {
85  case AF_INET:
86  sin = ( ( struct sockaddr_in * ) sa );
87  data = &sin->sin_addr;
88  len = sizeof ( sin->sin_addr );
89  default_type = &setting_type_ipv4;
90  break;
91  case AF_INET6:
92  sin6 = ( ( struct sockaddr_in6 * ) sa );
93  data = &sin6->sin6_addr;
94  len = sizeof ( sin6->sin6_addr );
95  default_type = &setting_type_ipv6;
96  break;
97  default:
98  rc = -ENOTSUP;
99  goto err;
100  }
101 
102  /* Parse specified setting name */
105  &setting ) ) != 0 )
106  goto err;
107 
108  /* Apply default type if necessary */
109  if ( ! setting.type )
110  setting.type = default_type;
111 
112  /* Store in specified setting */
113  if ( ( rc = store_setting ( settings, &setting, data, len ) ) != 0 )
114  goto err;
115 
116  err:
117  /* Terminate name resolution */
119 }
static void nslookup_close(struct nslookup *nslookup, int rc)
Terminate name resolution.
Definition: nslookup.c:59
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define AF_INET6
IPv6 Internet addresses.
Definition: socket.h:65
sa_family_t sa_family
Socket address family.
Definition: socket.h:102
struct sockaddr_in6 sin6
Definition: syslog.c:60
int store_setting(struct settings *settings, const struct setting *setting, const void *data, size_t len)
Store value of setting.
Definition: settings.c:616
IPv4 socket address.
Definition: in.h:85
char * setting_name
Setting name.
Definition: nslookup.c:50
#define ENOTSUP
Operation not supported.
Definition: errno.h:590
ring len
Length.
Definition: dwmac.h:231
struct sockaddr sa
Definition: syslog.c:57
const struct setting_type * type
Setting type.
Definition: settings.h:37
struct settings * autovivify_child_settings(struct settings *parent, const char *name)
Find or create child settings block.
Definition: settings.c:307
A settings block.
Definition: settings.h:133
A setting.
Definition: settings.h:24
struct in_addr sin_addr
IPv4 address.
Definition: in.h:101
uint8_t data[48]
Additional event data.
Definition: ena.h:22
IPv6 socket address.
Definition: in.h:118
struct sockaddr_in sin
Definition: syslog.c:59
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:64
A setting type.
Definition: settings.h:192
int parse_setting_name(char *name, get_child_settings_t get_child, struct settings **settings, struct setting *setting)
Parse setting name.
Definition: settings.c:1529
struct in6_addr sin6_addr
IPv6 address.
Definition: in.h:135
A name resolution request.
Definition: nslookup.c:40

References AF_INET, AF_INET6, autovivify_child_settings(), data, ENOTSUP, len, nslookup_close(), parse_setting_name(), rc, sa, sockaddr::sa_family, nslookup::setting_name, sin, sin6, sockaddr_in6::sin6_addr, sockaddr_in::sin_addr, store_setting(), and setting::type.

◆ resolv_setting()

static int resolv_setting ( struct interface job,
const char *  name,
const char *  setting_name 
)
static

Initiate standalone name resolution.

Parameters
jobParent interface
nameName to resolve
setting_nameSetting name
Return values
rcReturn status code

Definition at line 150 of file nslookup.c.

151  {
152  struct nslookup *nslookup;
153  struct sockaddr sa;
154  char *setting_name_copy;
155  int rc;
156 
157  /* Allocate and initialise structure */
158  nslookup = zalloc ( sizeof ( *nslookup ) + strlen ( setting_name )
159  + 1 /* NUL */ );
160  if ( ! nslookup )
161  return -ENOMEM;
162  ref_init ( &nslookup->refcnt, NULL );
165  &nslookup->refcnt );
166  setting_name_copy = ( ( void * ) ( nslookup + 1 ) );
167  strcpy ( setting_name_copy, setting_name );
168  nslookup->setting_name = setting_name_copy;
169 
170  /* Start name resolution */
171  memset ( &sa, 0, sizeof ( sa ) );
172  if ( ( rc = resolv ( &nslookup->resolver, name, &sa ) ) != 0 )
173  goto err_resolv;
174 
175  /* Attach parent interface, mortalise self, and return */
176  intf_plug_plug ( &nslookup->job, job );
177  ref_put ( &nslookup->refcnt );
178  return 0;
179 
180  err_resolv:
181  ref_put ( &nslookup->refcnt );
182  return rc;
183 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1986
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:65
static struct interface_descriptor nslookup_resolver_desc
Name resolution resolver interface descriptor.
Definition: nslookup.c:128
struct refcnt refcnt
Reference count for this object.
Definition: nslookup.c:42
struct interface job
Job control interface.
Definition: nslookup.c:45
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:108
char * setting_name
Setting name.
Definition: nslookup.c:50
#define ENOMEM
Not enough space.
Definition: errno.h:535
struct interface resolver
Data transfer interface.
Definition: nslookup.c:47
struct sockaddr sa
Definition: syslog.c:57
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:347
Generalized socket address structure.
Definition: socket.h:97
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
static struct interface_descriptor nslookup_job_desc
Name resolution job control interface descriptor.
Definition: nslookup.c:138
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
int nslookup(const char *name, const char *setting_name)
Perform (blocking) standalone name resolution.
Definition: nslookup.c:192
int resolv(struct interface *resolv, const char *name, struct sockaddr *sa)
Start name resolution.
Definition: resolv.c:258
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:204
int setting_name(struct settings *settings, const struct setting *setting, char *buf, size_t len)
Return full setting name.
Definition: settings.c:1607
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
A name resolution request.
Definition: nslookup.c:40
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:107
void * memset(void *dest, int character, size_t len) __nonnull

References ENOMEM, intf_init(), intf_plug_plug(), nslookup::job, memset(), name, nslookup(), nslookup_job_desc, nslookup_resolver_desc, NULL, rc, ref_init, ref_put, nslookup::refcnt, resolv(), nslookup::resolver, sa, nslookup::setting_name, setting_name(), strcpy(), strlen(), and zalloc().

Referenced by nslookup().

◆ nslookup()

int nslookup ( const char *  name,
const char *  setting_name 
)

Perform (blocking) standalone name resolution.

Parameters
nameName to resolve
setting_nameSetting name
Return values
rcReturn status code

Definition at line 192 of file nslookup.c.

192  {
193  int rc;
194 
195  /* Perform name resolution */
196  if ( ( rc = resolv_setting ( &monojob, name, setting_name ) ) == 0 )
197  rc = monojob_wait ( NULL, 0 );
198  if ( rc != 0 ) {
199  printf ( "Could not resolve %s: %s\n", name, strerror ( rc ) );
200  return rc;
201  }
202 
203  return 0;
204 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1986
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:465
int monojob_wait(const char *string, unsigned long timeout)
Wait for single foreground job to complete.
Definition: monojob.c:82
static int resolv_setting(struct interface *job, const char *name, const char *setting_name)
Initiate standalone name resolution.
Definition: nslookup.c:150
struct interface monojob
Definition: monojob.c:57
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
int setting_name(struct settings *settings, const struct setting *setting, char *buf, size_t len)
Return full setting name.
Definition: settings.c:1607
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References monojob, monojob_wait(), name, NULL, printf(), rc, resolv_setting(), setting_name(), and strerror().

Referenced by resolv_setting().

Variable Documentation

◆ nslookup_resolver_operations

struct interface_operation nslookup_resolver_operations[]
static
Initial value:
= {
}
static void nslookup_close(struct nslookup *nslookup, int rc)
Terminate name resolution.
Definition: nslookup.c:59
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:250
void resolv_done(struct interface *intf, struct sockaddr *sa)
Name resolved.
Definition: resolv.c:56
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:33
static void nslookup_resolv_done(struct nslookup *nslookup, struct sockaddr *sa)
Handle resolved name.
Definition: nslookup.c:72
A name resolution request.
Definition: nslookup.c:40

Name resolution resolver interface operations.

Definition at line 122 of file nslookup.c.

◆ nslookup_resolver_desc

struct interface_descriptor nslookup_resolver_desc
static
Initial value:
=
static struct interface_operation nslookup_resolver_operations[]
Name resolution resolver interface operations.
Definition: nslookup.c:122
A name resolver.
Definition: resolv.h:19
#define INTF_DESC_PASSTHRU(object_type, intf, operations, passthru)
Define an object interface descriptor with pass-through interface.
Definition: interface.h:98
A name resolution request.
Definition: nslookup.c:40

Name resolution resolver interface descriptor.

Definition at line 128 of file nslookup.c.

Referenced by resolv_setting().

◆ nslookup_job_operations

struct interface_operation nslookup_job_operations[]
static
Initial value:
= {
}
static void nslookup_close(struct nslookup *nslookup, int rc)
Terminate name resolution.
Definition: nslookup.c:59
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:250
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:33
A name resolution request.
Definition: nslookup.c:40

Name resolution job control interface operations.

Definition at line 133 of file nslookup.c.

◆ nslookup_job_desc

struct interface_descriptor nslookup_job_desc
static
Initial value:
=
static struct interface_operation nslookup_job_operations[]
Name resolution job control interface operations.
Definition: nslookup.c:133
A name resolver.
Definition: resolv.h:19
#define INTF_DESC_PASSTHRU(object_type, intf, operations, passthru)
Define an object interface descriptor with pass-through interface.
Definition: interface.h:98
A name resolution request.
Definition: nslookup.c:40

Name resolution job control interface descriptor.

Definition at line 138 of file nslookup.c.

Referenced by resolv_setting().