Browse code

win32: resolv.[ch]

aCaB authored on 2009/10/16 08:47:42
Showing 10 changed files
... ...
@@ -1,3 +1,7 @@
1
+Fri Oct 16 01:46:28 CEST 2009 (acab)
2
+------------------------------------
3
+ * win32: res_query compatible interface
4
+
1 5
 Thu Oct 15 13:06:33 CEST 2009 (acab)
2 6
 ------------------------------------
3 7
  * win32: add freshclam
... ...
@@ -28,10 +28,11 @@
28 28
 
29 29
 #include <string.h>
30 30
 #include <sys/types.h>
31
+#ifndef _WIN32
31 32
 #include <netinet/in.h>
32 33
 #include <arpa/nameser.h>
34
+#endif
33 35
 #include <resolv.h>
34
-#include <sys/types.h>
35 36
 
36 37
 #include "shared/output.h"
37 38
 
... ...
@@ -140,41 +141,6 @@ char *txtquery(const char *domain, unsigned int *ttl)
140 140
     return txt;
141 141
 }
142 142
 
143
-#elif defined(C_WINDOWS)
144
-
145
-/*
146
- * Note: Needs to link with dnsapi.lib.  
147
- * The dll behind this library is available from Windows 2000 onward.
148
- * Written by Mark Pizzolato
149
- */
150
-#include <winsock.h>
151
-#include <string.h>
152
-#include <windows.h>
153
-#include <windns.h>
154
-#include "shared/output.h"
155
-
156
-char *txtquery(const char *domain, unsigned int *ttl)
157
-{
158
-	PDNS_RECORD pDnsRecord;
159
-	char *txt = NULL;
160
-
161
-    *ttl = 0;
162
-    mprintf("*Querying %s\n", domain);
163
-
164
-   if(DnsQuery_UTF8(domain, DNS_TYPE_TEXT, DNS_QUERY_TREAT_AS_FQDN, NULL, &pDnsRecord, NULL) != 0)
165
-	return NULL;
166
-
167
-    if((pDnsRecord->Data.TXT.dwStringCount > 0) && pDnsRecord->Data.TXT.pStringArray[0]) {
168
-	txt = malloc(strlen(pDnsRecord->Data.TXT.pStringArray[0]) + 1);
169
-	if(txt)
170
-	    strcpy(txt, pDnsRecord->Data.TXT.pStringArray[0]);
171
-	*ttl = pDnsRecord->dwTtl;
172
-    }
173
-    DnsRecordListFree(pDnsRecord, DnsFreeRecordList);
174
-
175
-    return txt;
176
-}
177
-
178 143
 #else
179 144
 
180 145
 char *txtquery(const char *domain, unsigned int *ttl)
... ...
@@ -211,9 +211,7 @@ static int wwwconnect(const char *server, const char *proxy, int pport, char *ip
211 211
 		else
212 212
 			port = 8080;
213 213
 
214
-#ifndef	_WIN32
215 214
 		endservent();
216
-#endif
217 215
 	}
218 216
 
219 217
     } else {
... ...
@@ -32,7 +32,7 @@
32 32
 #define CL_THREAD_SAFE 1
33 33
 
34 34
 /* where to look for the config file */
35
-#define CONFDIR "/usr/local/etc"
35
+#define CONFDIR "C:\\ClamAV"
36 36
 
37 37
 /* curses header location */
38 38
 /* #define CURSES_INCLUDE <ncurses.h>
... ...
@@ -83,7 +83,7 @@
83 83
 /* #undef C_SOLARIS */
84 84
 
85 85
 /* Path to virus database directory. */
86
-#define DATADIR "c:\\db"
86
+#define DATADIR "c:\\ClamAV\\db"
87 87
 
88 88
 /* "default FD_SETSIZE value" */
89 89
 #define DEFAULT_FD_SETSIZE 1024
... ...
@@ -304,7 +304,7 @@
304 304
 #define HAVE_RECVMSG 1
305 305
 
306 306
 /* have resolv.h */
307
-/* #undef HAVE_RESOLV_H */
307
+#define HAVE_RESOLV_H 1
308 308
 
309 309
 /* Define signed right shift implementation */
310 310
 #define HAVE_SAR 1
... ...
@@ -1,3 +1,23 @@
1
+/*
2
+ *  Copyright (C) 2009 Sourcefire, Inc.
3
+ *
4
+ *  Authors: aCaB <acab@clamav.net>
5
+ *
6
+ *  This program is free software; you can redistribute it and/or modify
7
+ *  it under the terms of the GNU General Public License version 2 as
8
+ *  published by the Free Software Foundation.
9
+ *
10
+ *  This program is distributed in the hope that it will be useful,
11
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ *  GNU General Public License for more details.
14
+ *
15
+ *  You should have received a copy of the GNU General Public License
16
+ *  along with this program; if not, write to the Free Software
17
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
+ *  MA 02110-1301, USA.
19
+ */
20
+
1 21
 #include <winsock2.h>
2 22
 #include <Ws2tcpip.h>
3 23
 #include "net.h"
... ...
@@ -33,6 +33,7 @@ ssize_t w32_send(int sockfd, const void *buf, size_t len, int flags);
33 33
 ssize_t w32_recv(int sockfd, void *buf, size_t len, int flags);
34 34
 int w32_closesocket(int sockfd);
35 35
 struct servent *w32_getservbyname(const char *name, const char *proto);
36
+#define endservent()
36 37
 int w32_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
37 38
 void w32_freeaddrinfo(struct addrinfo *res);
38 39
 const char *w32_inet_ntop(int af, const void *src, char *dst, socklen_t size);
39 40
new file mode 100644
... ...
@@ -0,0 +1,97 @@
0
+/*
1
+ *  Copyright (C) 2009 Sourcefire, Inc.
2
+ *
3
+ *  Authors: aCaB <acab@clamav.net>
4
+ *
5
+ *  This program is free software; you can redistribute it and/or modify
6
+ *  it under the terms of the GNU General Public License version 2 as
7
+ *  published by the Free Software Foundation.
8
+ *
9
+ *  This program is distributed in the hope that it will be useful,
10
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+ *  GNU General Public License for more details.
13
+ *
14
+ *  You should have received a copy of the GNU General Public License
15
+ *  along with this program; if not, write to the Free Software
16
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ *  MA 02110-1301, USA.
18
+ */
19
+
20
+/* a fake libresolv-like res_query interface */
21
+
22
+#include "resolv.h"
23
+
24
+int res_init(void) {
25
+    return 0;
26
+}
27
+
28
+int res_query(const char *dname, int class, int type, unsigned char *answer, int anslen) {
29
+    DNS_RECORD *rrs, *rr;
30
+    DNS_STATUS s;
31
+    HEADER *h = (HEADER *)answer;
32
+    int ret = -1;
33
+
34
+    if(anslen <= sizeof(HEADER))
35
+	return -1;
36
+
37
+    s = DnsQuery(dname, (WORD)type, DNS_QUERY_BYPASS_CACHE | DNS_QUERY_NO_HOSTS_FILE | DNS_QUERY_DONT_RESET_TTL_VALUES, NULL, &rrs, NULL);
38
+    if(s)
39
+	return -1;
40
+
41
+    /* We don't use the header data */
42
+    h->id = 1;
43
+    answer += sizeof(HEADER);
44
+    anslen -= sizeof(HEADER);
45
+
46
+    rr = rrs;
47
+    do {
48
+	if(rr->wType == (WORD)type || rr->Data.TXT.dwStringCount || rr->Data.TXT.pStringArray[0]) {
49
+	    unsigned int len = strlen(dname), txtlen = strlen(rr->Data.TXT.pStringArray[0]);
50
+	    if(txtlen > 255) continue;
51
+	    len++;
52
+	    if(len*2 + txtlen + 15 > anslen) break;
53
+	    memcpy(answer, dname, len);
54
+	    answer += len;
55
+	    answer[0] = type >> 8; /* type */
56
+	    answer[1] = type;
57
+	    answer[2] = class >> 8; /* class */
58
+	    answer[3] = class & 0xff;
59
+	    answer += 4;
60
+	    memcpy(answer, dname, len);
61
+	    answer += len;
62
+	    answer[0] = type >> 8; /* type */
63
+	    answer[1] = type;
64
+	    answer[2] = class >> 8; /* class */
65
+	    answer[3] = class & 0xff;
66
+	    answer[4] = rr->dwTtl >> 24;
67
+	    answer[5] = rr->dwTtl >> 16;
68
+	    answer[6] = rr->dwTtl >> 8;
69
+	    answer[7] = rr->dwTtl;
70
+	    answer[8] = (txtlen+1) >> 8; /* rdata len */
71
+	    answer[9] = txtlen+1;
72
+	    answer[10] = txtlen;
73
+	    memcpy(&answer[11], rr->Data.TXT.pStringArray[0], txtlen);
74
+	    ret = len*2 + txtlen + 15 + sizeof(HEADER);
75
+	    break;
76
+	}
77
+    } while ((rr = rr->pNext));
78
+
79
+    DnsRecordListFree(rrs, DnsFreeRecordList);
80
+    return ret;
81
+}
82
+
83
+int dn_expand(unsigned char *msg, unsigned char *eomorig, unsigned char *comp_dn, char *exp_dn, int length) {
84
+    int len, maxlen;
85
+
86
+    /* names are simple C strings, not compressed not len encoded */
87
+    if(comp_dn < msg || comp_dn >= eomorig)
88
+	return -1;
89
+    maxlen = eomorig - comp_dn;
90
+    len = strnlen(comp_dn, maxlen) + 1;
91
+    if(len > maxlen || len > length)
92
+	return -1;
93
+    memcpy(exp_dn, msg, len);
94
+    return len;
95
+}
96
+
0 97
new file mode 100644
... ...
@@ -0,0 +1,122 @@
0
+/*
1
+ *  Copyright (C) 2009 Sourcefire, Inc.
2
+ *
3
+ *  Authors: aCaB <acab@clamav.net>
4
+ *
5
+ *  This program is free software; you can redistribute it and/or modify
6
+ *  it under the terms of the GNU General Public License version 2 as
7
+ *  published by the Free Software Foundation.
8
+ *
9
+ *  This program is distributed in the hope that it will be useful,
10
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+ *  GNU General Public License for more details.
13
+ *
14
+ *  You should have received a copy of the GNU General Public License
15
+ *  along with this program; if not, write to the Free Software
16
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ *  MA 02110-1301, USA.
18
+ */
19
+
20
+#ifndef __RESOLV_H
21
+#define __RESOLV_H
22
+
23
+#if HAVE_CONFIG_H
24
+#include "clamav-config.h"
25
+#endif
26
+
27
+#include <Windns.h>
28
+#include "cltypes.h"
29
+
30
+#define C_IN 1
31
+
32
+#define T_A DNS_TYPE_A
33
+#define T_NS DNS_TYPE_NS
34
+#define T_MD DNS_TYPE_MD
35
+#define T_MF DNS_TYPE_MF
36
+#define T_CNAME DNS_TYPE_CNAME
37
+#define T_SOA DNS_TYPE_SOA 0
38
+#define T_MB DNS_TYPE_MB
39
+#define T_MG DNS_TYPE_MG
40
+#define T_MR DNS_TYPE_MR
41
+#define T_NULL DNS_TYPE_NULL
42
+#define T_WKS DNS_TYPE_WKS
43
+#define T_PTR DNS_TYPE_PTR
44
+#define T_HINFO DNS_TYPE_HINFO
45
+#define T_MINFO DNS_TYPE_MINFO
46
+#define T_MX DNS_TYPE_MX
47
+#define T_TXT DNS_TYPE_TEXT
48
+#define T_RP DNS_TYPE_RP
49
+#define T_AFSDB DNS_TYPE_AFSDB
50
+#define T_X25 DNS_TYPE_X25
51
+#define T_ISDN DNS_TYPE_ISDN
52
+#define T_RT DNS_TYPE_RT
53
+#define T_NSAP DNS_TYPE_NSAP
54
+#define T_NSAP_PTR DNS_TYPE_NSAPPTR
55
+#define T_SIG DNS_TYPE_SIG
56
+#define T_KEY DNS_TYPE_KEY
57
+#define T_PX DNS_TYPE_PX
58
+#define T_GPOS DNS_TYPE_GPOS
59
+#define T_AAAA DNS_TYPE_AAAA
60
+#define T_LOC DNS_TYPE_LOC
61
+#define T_NXT DNS_TYPE_NXT
62
+#define T_EID DNS_TYPE_EID
63
+#define T_NIMLOC DNS_TYPE_NIMLOC
64
+#define T_SRV DNS_TYPE_SRV
65
+#define T_ATMA DNS_TYPE_ATMA
66
+#define T_NAPTR DNS_TYPE_NAPTR
67
+#define T_KX DNS_TYPE_KX
68
+#define T_CERT DNS_TYPE_CERT
69
+#define T_A6 DNS_TYPE_A6
70
+#define T_DNAME DNS_TYPE_DNAME
71
+#define T_SINK DNS_TYPE_SINK
72
+#define T_OPT DNS_TYPE_OPT
73
+#define T_DS DNS_TYPE_DS
74
+#define T_RRSIG DNS_TYPE_RRSIG
75
+#define T_NSEC DNS_TYPE_NSEC
76
+#define T_DNSKEY DNS_TYPE_DNSKEY
77
+#define T_DHCID DNS_TYPE_DHCID
78
+#define T_UINFO DNS_TYPE_UINFO
79
+#define T_UID DNS_TYPE_UID
80
+#define T_GID DNS_TYPE_GID
81
+#define T_UNSPEC DNS_TYPE_UNSPEC
82
+#define T_ADDRS DNS_TYPE_ADDRS
83
+#define T_TKEY DNS_TYPE_TKEY
84
+#define T_TSIG DNS_TYPE_TSIG
85
+#define T_IXFR DNS_TYPE_IXFR
86
+#define T_AXFR DNS_TYPE_AXFR
87
+#define T_MAILB DNS_TYPE_MAILB
88
+#define T_MAILA DNS_TYPE_MAILA
89
+#define T_ALL DNS_TYPE_ALL
90
+#define T_ANY DNS_TYPE_ANY
91
+#define T_WINS DNS_TYPE_WINS
92
+#define T_WINSR DNS_TYPE_WINSR
93
+#define T_NBSTAT DNS_TYPE_NBSTAT
94
+
95
+#ifndef DNS_QUERY_NO_HOSTS_FILE
96
+#define DNS_QUERY_NO_HOSTS_FILE 0
97
+#endif
98
+
99
+typedef struct {
100
+	unsigned short	id; /* fake stuff */
101
+} HEADER;
102
+
103
+#define INT16SZ 2
104
+
105
+#define GETSHORT(var, ptr) do { \
106
+    var = ((uint16_t)(*(uint8_t *)ptr++)) << 8; \
107
+    var |= *(uint8_t *)ptr++; \
108
+    } while (0)
109
+
110
+#define GETLONG(var, ptr) do { \
111
+    var = ((uint32_t)(*(uint8_t *)ptr++)) << 24; \
112
+    var = ((uint32_t)(*(uint8_t *)ptr++)) << 16; \
113
+    var = ((uint32_t)(*(uint8_t *)ptr++)) << 8; \
114
+    var |= *(uint8_t *)ptr++; \
115
+    } while (0)
116
+
117
+int res_init(void);
118
+int res_query(const char *dname, int class, int type, unsigned char *answer, int anslen);
119
+int dn_expand(unsigned char *msg, unsigned char *eomorig, unsigned char *comp_dn, char *exp_dn, int length);
120
+
121
+#endif
... ...
@@ -63,6 +63,7 @@
63 63
 			/>
64 64
 			<Tool
65 65
 				Name="VCLinkerTool"
66
+				AdditionalDependencies="dnsapi.lib"
66 67
 				LinkIncremental="1"
67 68
 				GenerateDebugInformation="true"
68 69
 				SubSystem="1"
... ...
@@ -139,6 +140,7 @@
139 139
 			/>
140 140
 			<Tool
141 141
 				Name="VCLinkerTool"
142
+				AdditionalDependencies="dnsapi.lib"
142 143
 				LinkIncremental="1"
143 144
 				GenerateDebugInformation="true"
144 145
 				SubSystem="1"
... ...
@@ -237,6 +239,14 @@
237 237
 				Name="compat"
238 238
 				>
239 239
 				<File
240
+					RelativePath=".\compat\resolv.c"
241
+					>
242
+				</File>
243
+				<File
244
+					RelativePath=".\compat\resolv.h"
245
+					>
246
+				</File>
247
+				<File
240 248
 					RelativePath=".\compat\strptime.c"
241 249
 					>
242 250
 				</File>
... ...
@@ -788,10 +788,6 @@
788 788
 					>
789 789
 				</File>
790 790
 				<File
791
-					RelativePath=".\compat\net.h"
792
-					>
793
-				</File>
794
-				<File
795 791
 					RelativePath=".\compat\snprintf.c"
796 792
 					>
797 793
 				</File>