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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
|
/* $OpenBSD: getinfo.c,v 1.4 1998/09/03 02:01:39 deraadt Exp $ */
/*
* ++Copyright++ 1985, 1989
* -
* Copyright (c) 1985, 1989
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* -
* Portions Copyright (c) 1993 by Digital Equipment Corporation.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies, and that
* the name of Digital Equipment Corporation not be used in advertising or
* publicity pertaining to distribution of the document or software without
* specific, written prior permission.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
* -
* --Copyright--
*/
#ifndef lint
#if 0
static char sccsid[] = "@(#)getinfo.c 5.26 (Berkeley) 3/21/91";
static char rcsid[] = "$From: getinfo.c,v 8.6 1996/12/02 09:17:24 vixie Exp $";
#else
static char rcsid[] = "$OpenBSD: getinfo.c,v 1.4 1998/09/03 02:01:39 deraadt Exp $";
#endif
#endif /* not lint */
/*
******************************************************************************
*
* getinfo.c --
*
* Routines to create requests to name servers
* and interpret the answers.
*
* Adapted from 4.3BSD BIND gethostnamadr.c
*
******************************************************************************
*/
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <stdio.h>
#include <ctype.h>
#include "res.h"
#include "conf/portability.h"
extern char *_res_resultcodes[];
extern char *res_skip();
static char *addr_list[MAXADDRS + 1];
static char *host_aliases[MAXALIASES];
static int host_aliases_len[MAXALIASES];
static u_char hostbuf[BUFSIZ+1];
typedef struct {
char *name;
char *domain[MAXDOMAINS];
int numDomains;
char *address[MAXADDRS];
int numAddresses;
} ServerTable;
ServerTable server[MAXSERVERS];
typedef union {
HEADER qb1;
u_char qb2[PACKETSZ*2];
} querybuf;
typedef union {
int32_t al;
char ac;
} align;
#define GetShort(cp) _getshort(cp); cp += INT16SZ;
/*
******************************************************************************
*
* GetAnswer --
*
* Interprets an answer packet and retrieves the following
* information:
*
* Results:
* SUCCESS the info was retrieved.
* NO_INFO the packet did not contain an answer.
* NONAUTH non-authoritative information was found.
* ERROR the answer was malformed.
* Other errors returned in the packet header.
*
******************************************************************************
*/
static int
GetAnswer(nsAddrPtr, queryType, msg, msglen, iquery, hostPtr, isServer)
struct in_addr *nsAddrPtr;
char *msg;
int queryType;
int msglen;
Boolean iquery;
register HostInfo *hostPtr;
Boolean isServer;
{
register HEADER *headerPtr;
register u_char *cp;
querybuf answer;
char **aliasPtr;
u_char *eom, *bp;
char **addrPtr;
char *namePtr;
char *dnamePtr;
int type, class;
int qdcount, ancount, arcount, nscount, buflen;
int origClass;
int numAliases = 0;
int numAddresses = 0;
int n, i, j;
int len;
int dlen;
int status;
int numServers;
Boolean haveAnswer;
Boolean printedAnswers = FALSE;
/*
* If the hostPtr was used before, free up the calloc'd areas.
*/
FreeHostInfoPtr(hostPtr);
status = SendRequest(nsAddrPtr, msg, msglen, (char *) &answer,
sizeof(answer), &n);
if (status != SUCCESS) {
if (_res.options & RES_DEBUG2)
printf("SendRequest failed\n");
return (status);
}
if (n > sizeof(answer)) {
if (_res.options & RES_DEBUG2)
printf("SendRequest overflowed\n");
return (-1);
}
eom = (u_char *) &answer + n;
headerPtr = (HEADER *) &answer;
if (headerPtr->rcode != NOERROR) {
return (headerPtr->rcode);
}
qdcount = ntohs(headerPtr->qdcount);
ancount = ntohs(headerPtr->ancount);
arcount = ntohs(headerPtr->arcount);
nscount = ntohs(headerPtr->nscount);
/*
* If there are no answer, n.s. or additional records
* then return with an error.
*/
if (ancount == 0 && nscount == 0 && arcount == 0) {
return (NO_INFO);
}
bp = hostbuf;
buflen = sizeof(hostbuf);
cp = (u_char *) &answer + HFIXEDSZ;
/* Skip over question section. */
while (qdcount-- > 0) {
cp += dn_skipname(cp, eom) + QFIXEDSZ;
}
aliasPtr = host_aliases;
addrPtr = addr_list;
haveAnswer = FALSE;
/*
* Scan through the answer resource records.
* Answers for address query types are saved.
* Other query type answers are just printed.
*/
if (ancount != 0) {
if (!isServer && !headerPtr->aa) {
printf("Non-authoritative answer:\n");
}
if (queryType != T_A && !(iquery && queryType == T_PTR)) {
while (--ancount >= 0 && cp < eom) {
if ((cp = (u_char *)Print_rr(cp,
(char *)&answer, eom, stdout)) == NULL) {
return(ERROR);
}
}
printedAnswers = TRUE;
} else {
while (--ancount >= 0 && cp < eom) {
n = dn_expand(answer.qb2, eom, cp, (char *)bp, buflen);
if (n < 0) {
return(ERROR);
}
cp += n;
type = GetShort(cp);
class = GetShort(cp);
cp += INT32SZ; /* skip TTL */
dlen = GetShort(cp);
if (type == T_CNAME) {
/*
* Found an alias.
*/
cp += dlen;
if (aliasPtr >= &host_aliases[MAXALIASES-1]) {
continue;
}
*aliasPtr++ = (char *)bp;
n = strlen((char *)bp) + 1;
host_aliases_len[numAliases] = n;
numAliases++;
bp += n;
buflen -= n;
continue;
} else if (type == T_PTR) {
/*
* Found a "pointer" to the real name.
*/
n = dn_expand(answer.qb2, eom, cp, (char *)bp, buflen);
if (n < 0) {
cp += n;
continue;
}
cp += n;
len = strlen((char *)bp) + 1;
hostPtr->name = Calloc(1, len);
bcopy(bp, hostPtr->name, len);
haveAnswer = TRUE;
break;
} else if (type != T_A) {
cp += dlen;
continue;
}
if (haveAnswer) {
/*
* If we've already got 1 address, we aren't interested
* in addresses with a different length or class.
*/
if (dlen != hostPtr->addrLen) {
cp += dlen;
continue;
}
if (class != origClass) {
cp += dlen;
continue;
}
} else {
/*
* First address: record its length and class so we
* only save additonal ones with the same attributes.
*/
hostPtr->addrLen = dlen;
origClass = class;
hostPtr->addrType = (class == C_IN) ? AF_INET : AF_UNSPEC;
len = strlen((char *)bp) + 1;
hostPtr->name = Calloc(1, len);
bcopy(bp, hostPtr->name, len);
}
bp += (((u_long)bp) % sizeof(align));
if (bp + dlen >= &hostbuf[sizeof(hostbuf)]) {
if (_res.options & RES_DEBUG) {
printf("Size (%d) too big\n", dlen);
}
break;
}
if (numAddresses >= MAXADDRS) {
printf("MAXADDRS exceeded: skipping address\n");
cp += dlen;
continue;
}
bcopy(cp, *addrPtr++ = (char *)bp, dlen);
bp +=dlen;
cp += dlen;
numAddresses++;
haveAnswer = TRUE;
}
}
}
if ((queryType == T_A || queryType == T_PTR) && haveAnswer) {
/*
* Go through the alias and address lists and return them
* in the hostPtr variable.
*/
if (numAliases > 0) {
hostPtr->aliases =
(char **) Calloc(1 + numAliases, sizeof(char *));
for (i = 0; i < numAliases; i++) {
hostPtr->aliases[i] = Calloc(1, host_aliases_len[i]);
bcopy(host_aliases[i],
hostPtr->aliases[i],
host_aliases_len[i]);
}
hostPtr->aliases[i] = NULL;
}
if (numAddresses > 0) {
hostPtr->addrList =
(char **)Calloc(1+numAddresses, sizeof(char *));
for (i = 0; i < numAddresses; i++) {
hostPtr->addrList[i] = Calloc(1, hostPtr->addrLen);
bcopy(addr_list[i], hostPtr->addrList[i], hostPtr->addrLen);
}
hostPtr->addrList[i] = NULL;
}
#ifdef verbose
if (headerPtr->aa || nscount == 0) {
hostPtr->servers = NULL;
return (SUCCESS);
}
#else
hostPtr->servers = NULL;
return (SUCCESS);
#endif
}
/*
* At this point, for the T_A query type, only empty answers remain.
* For other query types, additional information might be found
* in the additional resource records part.
*/
if (!headerPtr->aa && (queryType != T_A) && (nscount > 0 || arcount > 0)) {
if (printedAnswers) {
putchar('\n');
}
printf("Authoritative answers can be found from:\n");
}
cp = (u_char *)res_skip((char *) &answer, 2, eom);
numServers = 0;
if (queryType != T_A) {
/*
* If we don't need to save the record, just print it.
*/
while (--nscount >= 0 && cp < eom) {
if ((cp = (u_char *)Print_rr(cp,
(char *) &answer, eom, stdout)) == NULL) {
return(ERROR);
}
}
} else {
while (--nscount >= 0 && cp < eom) {
/*
* Go through the NS records and retrieve the names of hosts
* that serve the requested domain.
*/
n = dn_expand(answer.qb2, eom, cp, (char *)bp, buflen);
if (n < 0) {
return(ERROR);
}
cp += n;
len = strlen((char *)bp) + 1;
dnamePtr = Calloc(1, len); /* domain name */
bcopy(bp, dnamePtr, len);
type = GetShort(cp);
class = GetShort(cp);
cp += INT32SZ; /* skip TTL */
dlen = GetShort(cp);
if (type != T_NS) {
cp += dlen;
} else {
Boolean found;
n = dn_expand(answer.qb2, eom, cp, (char *)bp, buflen);
if (n < 0) {
return(ERROR);
}
cp += n;
len = strlen((char *)bp) + 1;
namePtr = Calloc(1, len); /* server host name */
bcopy(bp, namePtr, len);
/*
* Store the information keyed by the server host name.
*/
found = FALSE;
for (j = 0; j < numServers; j++) {
if (strcmp(namePtr, server[j].name) == 0) {
found = TRUE;
free(namePtr);
break;
}
}
if (found) {
server[j].numDomains++;
if (server[j].numDomains <= MAXDOMAINS) {
server[j].domain[server[j].numDomains-1] = dnamePtr;
}
} else {
if (numServers >= MAXSERVERS) {
break;
}
server[numServers].name = namePtr;
server[numServers].domain[0] = dnamePtr;
server[numServers].numDomains = 1;
server[numServers].numAddresses = 0;
numServers++;
}
}
}
}
/*
* Additional resource records contain addresses of servers.
*/
cp = (u_char *)res_skip((char *) &answer, 3, eom);
if (queryType != T_A) {
/*
* If we don't need to save the record, just print it.
*/
while (--arcount >= 0 && cp < eom) {
if ((cp = (u_char *)Print_rr(cp,
(char *) &answer, eom, stdout)) == NULL) {
return(ERROR);
}
}
} else {
while (--arcount >= 0 && cp < eom) {
n = dn_expand(answer.qb2, eom, cp, (char *)bp, buflen);
if (n < 0) {
break;
}
cp += n;
type = GetShort(cp);
class = GetShort(cp);
cp += INT32SZ; /* skip TTL */
dlen = GetShort(cp);
if (type != T_A) {
cp += dlen;
continue;
} else {
for (j = 0; j < numServers; j++) {
if (strcmp((char *)bp, server[j].name) == 0) {
server[j].numAddresses++;
if (server[j].numAddresses <= MAXADDRS) {
server[j].address[server[j].numAddresses-1] =
Calloc(1,dlen);
bcopy(cp,
server[j].address[server[j].numAddresses-1],dlen);
break;
}
}
}
cp += dlen;
}
}
}
/*
* If we are returning name server info, transfer it to the hostPtr.
*/
if (numServers > 0) {
hostPtr->servers = (ServerInfo **)
Calloc(numServers+1, sizeof(ServerInfo *));
for (i = 0; i < numServers; i++) {
hostPtr->servers[i] = (ServerInfo *) Calloc(1, sizeof(ServerInfo));
hostPtr->servers[i]->name = server[i].name;
hostPtr->servers[i]->domains = (char **)
Calloc(server[i].numDomains+1,sizeof(char *));
for (j = 0; j < server[i].numDomains; j++) {
hostPtr->servers[i]->domains[j] = server[i].domain[j];
}
hostPtr->servers[i]->domains[j] = NULL;
hostPtr->servers[i]->addrList = (char **)
Calloc(server[i].numAddresses+1,sizeof(char *));
for (j = 0; j < server[i].numAddresses; j++) {
hostPtr->servers[i]->addrList[j] = server[i].address[j];
}
hostPtr->servers[i]->addrList[j] = NULL;
}
hostPtr->servers[i] = NULL;
}
switch (queryType) {
case T_A:
return NONAUTH;
case T_PTR:
if (iquery)
return NO_INFO;
/* fall through */
default:
return SUCCESS;
}
}
/*
*******************************************************************************
*
* GetHostInfo --
*
* Retrieves host name, address and alias information
* for a domain.
*
* Algorithm from res_search().
*
* Results:
* ERROR - res_mkquery failed.
* + return values from GetAnswer()
*
*******************************************************************************
*/
int
GetHostInfoByName(nsAddrPtr, queryClass, queryType, name, hostPtr, isServer)
struct in_addr *nsAddrPtr;
int queryClass;
int queryType;
char *name;
HostInfo *hostPtr;
Boolean isServer;
{
int n;
register int result;
register char **domain;
const char *cp;
Boolean got_nodata = FALSE;
struct in_addr ina;
Boolean tried_as_is = FALSE;
/* Catch explicit addresses */
if ((queryType == T_A) && IsAddr(name, &ina)) {
hostPtr->name = Calloc(strlen(name)+3, 1);
(void)sprintf(hostPtr->name,"[%s]",name);
hostPtr->aliases = NULL;
hostPtr->servers = NULL;
hostPtr->addrType = AF_INET;
hostPtr->addrLen = INADDRSZ;
hostPtr->addrList = (char **)Calloc(2, sizeof(char *));
hostPtr->addrList[0] = Calloc(INT32SZ, sizeof(char));
bcopy((char *)&ina, hostPtr->addrList[0], INADDRSZ);
hostPtr->addrList[1] = NULL;
return(SUCCESS);
}
result = NXDOMAIN;
for (cp = name, n = 0; *cp; cp++)
if (*cp == '.')
n++;
if (n == 0 && (cp = hostalias(name))) {
printf("Aliased to \"%s\"\n\n", cp);
return (GetHostDomain(nsAddrPtr, queryClass, queryType,
cp, (char *)NULL, hostPtr, isServer));
}
/*
* If there are dots in the name already, let's just give it a try
* 'as is'. The threshold can be set with the "ndots" option.
*/
if (n >= (int)_res.ndots) {
result = GetHostDomain(nsAddrPtr, queryClass, queryType,
name, (char *)NULL, hostPtr, isServer);
if (result == SUCCESS)
return (result);
if (result == NO_INFO)
got_nodata++;
tried_as_is++;
}
/*
* We do at least one level of search if
* - there is no dot and RES_DEFNAME is set, or
* - there is at least one dot, there is no trailing dot,
* and RES_DNSRCH is set.
*/
if ((n == 0 && _res.options & RES_DEFNAMES) ||
(n != 0 && *--cp != '.' && _res.options & RES_DNSRCH))
for (domain = _res.dnsrch; *domain; domain++) {
result = GetHostDomain(nsAddrPtr, queryClass, queryType,
name, *domain, hostPtr, isServer);
/*
* If no server present, give up.
* If name isn't found in this domain,
* keep trying higher domains in the search list
* (if that's enabled).
* On a NO_INFO error, keep trying, otherwise
* a wildcard entry of another type could keep us
* from finding this entry higher in the domain.
* If we get some other error (negative answer or
* server failure), then stop searching up,
* but try the input name below in case it's fully-qualified.
*/
if (result == SUCCESS || result == NO_RESPONSE)
return result;
if (result == NO_INFO)
got_nodata++;
if ((result != NXDOMAIN && result != NO_INFO) ||
(_res.options & RES_DNSRCH) == 0)
break;
}
/* if we have not already tried the name "as is", do that now.
* note that we do this regardless of how many dots were in the
* name or whether it ends with a dot.
*/
if (!tried_as_is &&
(result = GetHostDomain(nsAddrPtr, queryClass, queryType,
name, (char *)NULL, hostPtr, isServer)
) == SUCCESS)
return (result);
if (got_nodata)
result = NO_INFO;
return (result);
}
/*
* Perform a query on the concatenation of name and domain,
* removing a trailing dot from name if domain is NULL.
*/
GetHostDomain(nsAddrPtr, queryClass, queryType, name, domain, hostPtr, isServer)
struct in_addr *nsAddrPtr;
int queryClass;
int queryType;
char *name, *domain;
HostInfo *hostPtr;
Boolean isServer;
{
querybuf buf;
char nbuf[2*MAXDNAME+2];
char *longname = nbuf;
int n;
if (domain == NULL) {
/*
* Check for trailing '.';
* copy without '.' if present.
*/
n = strlen(name) - 1;
if (name[n] == '.' && n < sizeof(nbuf) - 1) {
bcopy(name, nbuf, n);
nbuf[n] = '\0';
} else
longname = name;
} else {
(void)sprintf(nbuf, "%.*s.%.*s",
MAXDNAME, name, MAXDNAME, domain);
longname = nbuf;
}
n = res_mkquery(QUERY, longname, queryClass, queryType,
NULL, 0, 0, buf.qb2, sizeof(buf));
if (n < 0) {
if (_res.options & RES_DEBUG) {
printf("Res_mkquery failed\n");
}
return (ERROR);
}
n = GetAnswer(nsAddrPtr, queryType, (char *)&buf, n, 0, hostPtr, isServer);
/*
* GetAnswer didn't find a name, so set it to the specified one.
*/
if (n == NONAUTH) {
if (hostPtr->name == NULL) {
int len = strlen(longname) + 1;
hostPtr->name = Calloc(len, sizeof(char));
bcopy(longname, hostPtr->name, len);
}
}
return(n);
}
/*
*******************************************************************************
*
* GetHostInfoByAddr --
*
* Performs a PTR lookup in in-addr.arpa to find the host name
* that corresponds to the given address.
*
* Results:
* ERROR - res_mkquery failed.
* + return values from GetAnswer()
*
*******************************************************************************
*/
int
GetHostInfoByAddr(nsAddrPtr, address, hostPtr)
struct in_addr *nsAddrPtr;
struct in_addr *address;
HostInfo *hostPtr;
{
int n;
querybuf buf;
char qbuf[MAXDNAME];
char *p = (char *) &address->s_addr;
(void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
((unsigned)p[3] & 0xff),
((unsigned)p[2] & 0xff),
((unsigned)p[1] & 0xff),
((unsigned)p[0] & 0xff));
n = res_mkquery(QUERY, qbuf, C_IN, T_PTR, NULL, 0, NULL,
buf.qb2, sizeof buf);
if (n < 0) {
if (_res.options & RES_DEBUG) {
printf("res_mkquery() failed\n");
}
return (ERROR);
}
n = GetAnswer(nsAddrPtr, T_PTR, (char *) &buf, n, 1, hostPtr, 1);
if (n == SUCCESS) {
hostPtr->addrType = AF_INET;
hostPtr->addrLen = 4;
hostPtr->addrList = (char **)Calloc(2, sizeof(char *));
hostPtr->addrList[0] = Calloc(INT32SZ, sizeof(char));
bcopy((char *)p, hostPtr->addrList[0], INADDRSZ);
hostPtr->addrList[1] = NULL;
}
return n;
}
/*
*******************************************************************************
*
* FreeHostInfoPtr --
*
* Deallocates all the calloc'd areas for a HostInfo variable.
*
*******************************************************************************
*/
void
FreeHostInfoPtr(hostPtr)
register HostInfo *hostPtr;
{
int i, j;
if (hostPtr->name != NULL) {
free(hostPtr->name);
hostPtr->name = NULL;
}
if (hostPtr->aliases != NULL) {
i = 0;
while (hostPtr->aliases[i] != NULL) {
free(hostPtr->aliases[i]);
i++;
}
free((char *)hostPtr->aliases);
hostPtr->aliases = NULL;
}
if (hostPtr->addrList != NULL) {
i = 0;
while (hostPtr->addrList[i] != NULL) {
free(hostPtr->addrList[i]);
i++;
}
free((char *)hostPtr->addrList);
hostPtr->addrList = NULL;
}
if (hostPtr->servers != NULL) {
i = 0;
while (hostPtr->servers[i] != NULL) {
if (hostPtr->servers[i]->name != NULL) {
free(hostPtr->servers[i]->name);
}
if (hostPtr->servers[i]->domains != NULL) {
j = 0;
while (hostPtr->servers[i]->domains[j] != NULL) {
free(hostPtr->servers[i]->domains[j]);
j++;
}
free((char *)hostPtr->servers[i]->domains);
}
if (hostPtr->servers[i]->addrList != NULL) {
j = 0;
while (hostPtr->servers[i]->addrList[j] != NULL) {
free(hostPtr->servers[i]->addrList[j]);
j++;
}
free((char *)hostPtr->servers[i]->addrList);
}
free((char *)hostPtr->servers[i]);
i++;
}
free((char *)hostPtr->servers);
hostPtr->servers = NULL;
}
}
|