1 #include <sys/socket.h>
6 #include "allocator_thread.h"
9 #ifndef HAVE_SOCK_CLOEXEC
10 #define SOCK_CLOEXEC 0
13 //static enum dns_lookup_flavor dns_flavor;
14 #define dns_flavor rdns_get_flavor()
16 static struct sockaddr_in rdns_server
;
18 size_t rdns_daemon_get_host_for_ip(ip_type4 ip
, char* readbuf
) {
20 .h
.msgtype
= ATM_GETNAME
,
21 .h
.datalen
= htons(4),
24 int fd
= socket(AF_INET
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
25 sendto(fd
, &msg
, sizeof(msg
.h
)+4, 0, (void*)&rdns_server
, sizeof(rdns_server
));
26 recvfrom(fd
, &msg
, sizeof msg
, 0, (void*)0, (void*)0);
28 msg
.h
.datalen
= ntohs(msg
.h
.datalen
);
29 if(!msg
.h
.datalen
|| msg
.h
.datalen
> 256) return 0;
30 memcpy(readbuf
, msg
.m
.host
, msg
.h
.datalen
);
31 return msg
.h
.datalen
- 1;
34 static ip_type4
rdns_daemon_get_ip_for_host(char* host
, size_t len
) {
36 .h
.msgtype
= ATM_GETIP
,
38 if(len
>= 256) return IPT4_INT(-1);
39 memcpy(msg
.m
.host
, host
, len
+1);
40 msg
.h
.datalen
= htons(len
+1);
41 int fd
= socket(AF_INET
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
42 sendto(fd
, &msg
, sizeof(msg
.h
)+len
+1, 0, (void*)&rdns_server
, sizeof(rdns_server
));
43 recvfrom(fd
, &msg
, sizeof msg
, 0, (void*)0, (void*)0);
45 if(ntohs(msg
.h
.datalen
) != 4) return IPT4_INT(-1);
49 const char *rdns_resolver_string(enum dns_lookup_flavor flavor
) {
50 static const char tab
[][7] = {
52 [DNSLF_FORKEXEC
] = "old",
53 [DNSLF_RDNS_THREAD
] = "thread",
54 [DNSLF_RDNS_DAEMON
] = "daemon",
59 void rdns_init(enum dns_lookup_flavor flavor
) {
60 static int init_done
= 0;
61 if(!init_done
) switch(flavor
) {
62 case DNSLF_RDNS_THREAD
:
65 case DNSLF_RDNS_DAEMON
:
72 void rdns_set_daemon(struct sockaddr_in
* addr
) {
77 enum dns_lookup_flavor
rdns_get_flavor(void) {
82 size_t rdns_get_host_for_ip(ip_type4 ip
, char* readbuf
) {
84 case DNSLF_RDNS_THREAD
: return at_get_host_for_ip(ip
, readbuf
);
85 case DNSLF_RDNS_DAEMON
: return rdns_daemon_get_host_for_ip(ip
, readbuf
);
91 ip_type4
rdns_get_ip_for_host(char* host
, size_t len
) {
93 case DNSLF_RDNS_THREAD
: return at_get_ip_for_host(host
, len
);
94 case DNSLF_RDNS_DAEMON
: return rdns_daemon_get_ip_for_host(host
, len
);