367 lines
9.2 KiB
HTML
367 lines
9.2 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
|
||
|
<title>res_ninit()--Initialize res Structure</title>
|
||
|
<!-- Begin Header Records ========================================== -->
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<!-- Unix8 SCRIPT J converted by B2H R4.1 (346) (CMS) by V2KEA304 -->
|
||
|
<!-- at RCHVMW2 on 17 Feb 1999 at 11:05:09 -->
|
||
|
<!-- Edited by Kersten Feb 02 -->
|
||
|
<!--End Header Records -->
|
||
|
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
|
||
|
</head>
|
||
|
<body>
|
||
|
<a name="Top_Of_Page"></a>
|
||
|
<!-- Java sync-link -->
|
||
|
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
|
||
|
</script>
|
||
|
|
||
|
<h2>res_ninit()--Initialize res Structure</h2>
|
||
|
|
||
|
<div class="box" style="width: 60%;">
|
||
|
<br>
|
||
|
Syntax<br>
|
||
|
<pre>
|
||
|
#include <sys/types.h>
|
||
|
#include <netinet/in.h>
|
||
|
#include <arpa/nameser.h>
|
||
|
#include <resolv.h>
|
||
|
|
||
|
int res_ninit(state* <em>res</em>)
|
||
|
|
||
|
</pre>
|
||
|
|
||
|
<br>
|
||
|
Service Program Name: QSOSRV2 <br>
|
||
|
<!-- iddvc RMBR -->
|
||
|
<br>
|
||
|
Default Public Authority: *USE<br>
|
||
|
<!-- iddvc RMBR -->
|
||
|
<br>
|
||
|
Threadsafe: Yes<br>
|
||
|
<!-- iddvc RMBR -->
|
||
|
<br>
|
||
|
</div>
|
||
|
|
||
|
<p>The <em>res_ninit()</em> function is similar to <em>res_init()</em> but it
|
||
|
uses a user-declared <strong>res</strong> pointer instead of the shared
|
||
|
<strong>_res</strong>.</p>
|
||
|
|
||
|
<p>For a description of this function and more information on the parameters,
|
||
|
authorities required, return values, error conditions, error messages, usage
|
||
|
notes, and related information, see <a href="resini.htm">res_init()</a>--Initialize
|
||
|
_res Structure.</p>
|
||
|
|
||
|
<h3>Parameters</h3>
|
||
|
|
||
|
<dl>
|
||
|
<dt><strong>res</strong></dt>
|
||
|
|
||
|
<dd>(Input/Output) The pointer to the <strong>state</strong> structure.</dd>
|
||
|
</dl>
|
||
|
|
||
|
<p>The RES_INIT and RES_XINIT options flags must be initialized to zero before
|
||
|
the first call to any resolver API or the <strong>res</strong> structure will
|
||
|
not be properly initialized. For example:</p>
|
||
|
|
||
|
<pre>
|
||
|
state res;
|
||
|
res.options &= ~ (RES_INIT | RES_XINIT);
|
||
|
int n = res_ninit(&res);
|
||
|
</pre>
|
||
|
|
||
|
<br>
|
||
|
<h3>Return Value</h3>
|
||
|
|
||
|
<p><em>res_ninit()</em> returns an integer. Possible values are:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>-1 (unsuccessful)</li>
|
||
|
|
||
|
<li>0 (successful)</li>
|
||
|
</ul>
|
||
|
|
||
|
<br>
|
||
|
<h3>Error Conditions</h3>
|
||
|
<p>When the <em>res_ninit()</em> function fails, <em>errno</em> can be set to
|
||
|
one of the following:</p>
|
||
|
<dl>
|
||
|
|
||
|
<dt><em>[EFAULT]</em></dt>
|
||
|
<dd><p>The system detected a pointer that was invalid
|
||
|
while attempting to access an input pointer.</p></dd>
|
||
|
|
||
|
|
||
|
<dt><em>[EINVAL]</em></dt>
|
||
|
<dd><p>The <strong>res</strong> appears to have been
|
||
|
previously initialized but the reserved field is not set to zeros.</p></dd>
|
||
|
|
||
|
</dl>
|
||
|
|
||
|
<br>
|
||
|
<br>
|
||
|
|
||
|
|
||
|
<h3>Related Information</h3>
|
||
|
|
||
|
<ul>
|
||
|
<li><a href="hstrerror.htm">hstrerror()</a>--Retrieve Resolver Error
|
||
|
Message<br>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li><a href="resini.htm">res_init()</a>--Initialize _res Structure<br>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li><a href="resfindzonecut.htm">res_findzonecut()</a>--Find the Enclosing Zone and
|
||
|
Servers<br>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li><a href="reshostalias.htm">res_hostalias()</a>--Retrieve the host alias<br>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li><a href="resnclose.htm">res_nclose()</a>--Close Socket and Reset res
|
||
|
Structure<br>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li><a href="resnmkquery.htm">res_nmkquery()</a>--Place Domain Query in
|
||
|
Buffer<br>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li><a href="resnquery.htm">res_nquery()</a>--Send Domain Query<br>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li><a href="resnsearch.htm">res_nsearch()</a>--Search for Domain Name<br>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li><a href="resnsend.htm">res_nsend()</a>--Send Buffered Domain Query<br>
|
||
|
<br>
|
||
|
</li>
|
||
|
|
||
|
<li><a href="resxlt.htm">res_xlate()</a>--Translate DNS Packets</li>
|
||
|
</ul>
|
||
|
|
||
|
<br>
|
||
|
<h3>Example</h3>
|
||
|
|
||
|
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
|
||
|
for information pertaining to code examples.</p>
|
||
|
|
||
|
<p>The following example shows how <strong>res_ninit()</strong> is used and how
|
||
|
initialization defaults can be changed after initialization:</p>
|
||
|
|
||
|
<pre>
|
||
|
#include <stdio.h>
|
||
|
#include <errno.h>
|
||
|
#include <netinet/in.h>
|
||
|
#include <arpa/nameser.h>
|
||
|
#include <resolv.h>
|
||
|
#include <netdb.h>
|
||
|
|
||
|
/* Declare update records - a zone record, a pre-requisite record, and
|
||
|
an update record */
|
||
|
ns_updrec update_records[] =
|
||
|
{
|
||
|
{
|
||
|
{NULL,NULL},
|
||
|
{NULL,&update_records[1]},
|
||
|
ns_s_zn, /* a zone record */
|
||
|
"mydomain.ibm.com.",
|
||
|
ns_c_in,
|
||
|
ns_t_soa,
|
||
|
0,
|
||
|
NULL,
|
||
|
0,
|
||
|
0,
|
||
|
NULL,
|
||
|
NULL,
|
||
|
0
|
||
|
},
|
||
|
{
|
||
|
{NULL,NULL},
|
||
|
{&update_records[0],&update_records[2]},
|
||
|
ns_s_pr, /* pre-req record */
|
||
|
"mypc.mydomain.ibm.com.",
|
||
|
ns_c_in,
|
||
|
ns_t_a,
|
||
|
0,
|
||
|
NULL,
|
||
|
0,
|
||
|
ns_r_nxdomain, /* record must not exist */
|
||
|
NULL,
|
||
|
NULL,
|
||
|
0
|
||
|
},
|
||
|
{
|
||
|
{NULL,NULL},
|
||
|
{&update_records[1],NULL},
|
||
|
ns_s_ud, /* update record */
|
||
|
"mypc.mydomain.ibm.com.",
|
||
|
ns_c_in,
|
||
|
ns_t_a,
|
||
|
10,
|
||
|
(unsigned char *)"10.10.10.10",
|
||
|
11,
|
||
|
ns_uop_add, /* to be added */
|
||
|
NULL,
|
||
|
NULL,
|
||
|
0
|
||
|
}
|
||
|
};
|
||
|
|
||
|
void main()
|
||
|
{
|
||
|
struct state res;
|
||
|
int result;
|
||
|
unsigned char update_buffer[2048];
|
||
|
int buffer_length = sizeof update_buffer;
|
||
|
|
||
|
unsigned char answer_buffer[2048];
|
||
|
|
||
|
/* Turn off the init flags so that the structure will be initialized
|
||
|
*/
|
||
|
res.options &= ~ (RES_INIT | RES_XINIT);
|
||
|
|
||
|
result = res_ninit(&res);
|
||
|
|
||
|
/* Put processing here to check the result and handle errors
|
||
|
*/
|
||
|
|
||
|
/* We choose to use TCP and not UDP, so set the appropriate option now
|
||
|
that the res variable has been initialized.
|
||
|
*/
|
||
|
res.options |= RES_USEVC;
|
||
|
|
||
|
/* Send a query for mypc.mydomain.ibm.com address records
|
||
|
*/
|
||
|
result = res_nquerydomain(&res,"mypc", "mydomain.ibm.com.", ns_c_in, ns_t_a,
|
||
|
update_buffer, buffer_length);
|
||
|
|
||
|
/* Sample error handling and printing errors
|
||
|
*/
|
||
|
if (result == -1)
|
||
|
{
|
||
|
printf("\nquery domain failed. result = %d \nerrno: %d: %s \nh_errno: %d: %s",
|
||
|
result,
|
||
|
errno, strerror(errno),
|
||
|
h_errno, hstrerror(h_errno));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/* The output on a failure will be:
|
||
|
|
||
|
query domain failed. result = -1
|
||
|
errno: 0: There is no error.
|
||
|
h_errno: 5: Unknown host
|
||
|
|
||
|
*/
|
||
|
|
||
|
{
|
||
|
/* Build an update buffer (packet to be sent) from the update records
|
||
|
*/
|
||
|
result = res_nmkupdate(&res, update_records, update_buffer, buffer_length);
|
||
|
|
||
|
/* Put processing here to check the result and handle errors
|
||
|
*/
|
||
|
}
|
||
|
|
||
|
{
|
||
|
char zone_name[NS_MAXDNAME];
|
||
|
size_t zone_name_size = sizeof zone_name;
|
||
|
struct sockaddr_in s_address;
|
||
|
struct in_addr addresses[1];
|
||
|
int number_addresses = 1;
|
||
|
|
||
|
/* Find the DNS server that is authoritative for the domain
|
||
|
that we want to update
|
||
|
*/
|
||
|
|
||
|
result = res_findzonecut(&res, "mypc.mydomain.ibm.com", ns_c_in, 0,
|
||
|
zone_name, zone_name_size,
|
||
|
addresses, number_addresses);
|
||
|
|
||
|
/* Put processing here to check the result and handle errors
|
||
|
*/
|
||
|
|
||
|
/* Check if the DNS server found is one of our regular
|
||
|
DNS addresses
|
||
|
*/
|
||
|
|
||
|
s_address.sin_addr = addresses[0];
|
||
|
s_address.sin_family = res.nsaddr_list[0].sin_family;
|
||
|
s_address.sin_port = res.nsaddr_list[0].sin_port;
|
||
|
memset(s_address.sin_zero, 0x00, 8);
|
||
|
|
||
|
result = res_nisourserver(&res, &s_address);
|
||
|
|
||
|
/* Put processing here to check the result and handle errors
|
||
|
*/
|
||
|
|
||
|
/* Set the DNS address found with res_findzonecut into the res
|
||
|
structure. We will send the (TSIG signed) update to that DNS.
|
||
|
*/
|
||
|
|
||
|
res.nscount = 1;
|
||
|
res.nsaddr_list[0] = s_address;
|
||
|
}
|
||
|
|
||
|
{
|
||
|
ns_tsig_key my_key = {
|
||
|
"my-long-key", /* This key must exist on the DNS */
|
||
|
NS_TSIG_ALG_HMAC_MD5,
|
||
|
(unsigned char*) "abcdefghijklmnopqrstuvwx",
|
||
|
24
|
||
|
};
|
||
|
|
||
|
/* Send a TSIG signed update to the DNS
|
||
|
*/
|
||
|
result = res_nsendsigned(&res, update_buffer, result,
|
||
|
&my_key,
|
||
|
answer_buffer, sizeof answer_buffer);
|
||
|
|
||
|
/* Put processing here to check the result and handle errors
|
||
|
*/
|
||
|
}
|
||
|
|
||
|
/* The res_findzonecut(), res_nmkupdate(), and res_nsendsigned() could
|
||
|
be replaced with one call to res_nupdate() using update_records[1]
|
||
|
to skip the zone record::
|
||
|
|
||
|
result = res_nupdate(&res, &update_records[1], &my_key);
|
||
|
|
||
|
*/
|
||
|
return;
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<br>
|
||
|
|
||
|
|
||
|
<hr>
|
||
|
API introduced: V5R1
|
||
|
|
||
|
<hr>
|
||
|
<center>
|
||
|
<table cellpadding="2" cellspacing="2">
|
||
|
<tr align="center">
|
||
|
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
|
||
|
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a> </td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</center>
|
||
|
</body>
|
||
|
</html>
|
||
|
|