-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.c
85 lines (72 loc) · 1.86 KB
/
new.c
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
/*
* Copyright (c) Regents of The University of Michigan
* See COPYING.
*/
#include <inttypes.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "denser.h"
#include "internal.h"
/*
* Creates a new DNSR structure which will be used for all future denser
* calls. This only fails on system error. Other functions have been moved
* out of this routine so they can provide better error reporting via
* the DNSR->d_errno.
*
* The returned dnsr handle is configured for recursion. Can be changed with
* dnsr_config( ).
*
* Return Values:
* DNSR * success
* NULL error - check errno
*/
DNSR *
dnsr_new(void) {
DNSR * dnsr;
struct timeval tv;
if (gettimeofday(&tv, NULL) != 0) {
return (NULL);
}
srand((unsigned int)getpid() ^ tv.tv_usec ^ tv.tv_sec);
if ((dnsr = calloc(1, sizeof(DNSR))) == NULL) {
return (NULL);
}
dnsr->d_nsresp = -1;
if ((dnsr->d_fd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
DEBUG(perror("dnsr_open: AF_INET6 socket"));
}
if ((dnsr->d_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
DEBUG(perror("dnsr_open: AF_INET socket"));
}
if ((dnsr->d_fd6 < 0) && (dnsr->d_fd < 0)) {
free(dnsr);
return (NULL);
}
/* XXX - do we need to check error here? */
dnsr_config(dnsr, DNSR_FLAG_RECURSION, DNSR_FLAG_ON);
return (dnsr);
}
void
dnsr_free(DNSR *dnsr) {
if (dnsr == NULL) {
return;
}
if (dnsr->d_fd >= 0) {
if (close(dnsr->d_fd) != 0) {
DEBUG(perror("dnsr_free: close"));
}
}
if (dnsr->d_fd6 >= 0) {
if (close(dnsr->d_fd6) != 0) {
DEBUG(perror("dnsr_free: close"));
}
}
free(dnsr);
}