fix missing bracket in solaris codepath (#589)
[rofl0r-proxychains-ng.git] / src / rdns.c
blobd9496b1f1132a4b5cd05e6596c30c00a34f50ec1
1 #include <sys/socket.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "rdns.h"
6 #include "allocator_thread.h"
7 #include "remotedns.h"
9 #ifndef HAVE_SOCK_CLOEXEC
10 #define SOCK_CLOEXEC 0
11 #endif
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) {
19 struct at_msg msg = {
20 .h.msgtype = ATM_GETNAME,
21 .h.datalen = htons(4),
22 .m.ip = ip,
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);
27 close(fd);
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) {
35 struct at_msg msg = {
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);
44 close(fd);
45 if(ntohs(msg.h.datalen) != 4) return IPT4_INT(-1);
46 return msg.m.ip;
49 const char *rdns_resolver_string(enum dns_lookup_flavor flavor) {
50 static const char tab[][7] = {
51 [DNSLF_LIBC] = "off",
52 [DNSLF_FORKEXEC] = "old",
53 [DNSLF_RDNS_THREAD] = "thread",
54 [DNSLF_RDNS_DAEMON] = "daemon",
56 return tab[flavor];
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:
63 at_init();
64 break;
65 case DNSLF_RDNS_DAEMON:
66 default:
67 break;
69 init_done = 1;
72 void rdns_set_daemon(struct sockaddr_in* addr) {
73 rdns_server = *addr;
76 #if 0
77 enum dns_lookup_flavor rdns_get_flavor(void) {
78 return dns_flavor;
80 #endif
82 size_t rdns_get_host_for_ip(ip_type4 ip, char* readbuf) {
83 switch(dns_flavor) {
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);
86 default:
87 abort();
91 ip_type4 rdns_get_ip_for_host(char* host, size_t len) {
92 switch(dns_flavor) {
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);
95 default:
96 abort();