Browse code

win32: use non tls (s)rand(), allow nonblocking sockets

aCaB authored on 2010/03/21 05:08:13
Showing 9 changed files
... ...
@@ -387,7 +387,10 @@ static void *acceptloop_th(void *arg)
387 387
 
388 388
 	    if (new_sd >= 0) {
389 389
 		int ret, flags;
390
-
390
+#ifdef _WIN32
391
+		sock_set_nonblock(new_fd);
392
+		logg("^Can't set socket to nonblocking mode, errno %d\n", errno);
393
+#else
391 394
 #ifdef F_GETFL
392 395
 		flags = fcntl(new_sd, F_GETFL, 0);
393 396
 		if (flags != -1) {
... ...
@@ -401,6 +404,7 @@ static void *acceptloop_th(void *arg)
401 401
 #else
402 402
 		logg("^Nonblocking sockets not available!\n");
403 403
 #endif
404
+#endif
404 405
 		logg("$Got new connection, FD %d\n", new_sd);
405 406
 		pthread_mutex_lock(recv_fds->buf_mutex);
406 407
 		ret = fds_add(recv_fds, new_sd, 0, commandtimeout);
... ...
@@ -493,7 +493,7 @@
493 493
 /* #undef USE_SYSLOG */
494 494
 
495 495
 /* Version number of package */
496
-#define VERSION "devel-clamav-0.96rc1-16-g89e12ee"
496
+#define VERSION "devel-clamav-0.96rc1-19-gc491083"
497 497
 
498 498
 /* Version suffix for package */
499 499
 #define VERSION_SUFFIX ""
... ...
@@ -350,6 +350,14 @@ int w32_shutdown(int sockfd, int how) {
350 350
     return 0;
351 351
 }
352 352
 
353
+int sock_set_nonblock(int sockfd) {
354
+    u_long arg = 1;
355
+    if(ioctlsocket((SOCKET)sockfd, FIONBIO, &arg)) {
356
+	wsock2errno();
357
+	return -1;
358
+    }
359
+    return 0;
360
+}
353 361
 
354 362
 struct w32polldata {
355 363
     HANDLE setme;
... ...
@@ -43,5 +43,6 @@ int poll_with_event(struct pollfd *fds, int nfds, int timeout, HANDLE event);
43 43
 int w32_accept(SOCKET sockfd, const struct sockaddr *addr, socklen_t *addrlen);
44 44
 int w32_listen(int sockfd, int backlog);
45 45
 int w32_shutdown(int sockfd, int how);
46
+int sock_set_nonblock(int sockfd);
46 47
 
47 48
 #endif
48 49
new file mode 100644
... ...
@@ -0,0 +1,35 @@
0
+/*
1
+ *  Copyright (C) 2010 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 non TLS based and non thread safe canonical rand() implementation */
21
+
22
+#include <stdlib.h>
23
+#include "random.h"
24
+
25
+static unsigned long next = 1;
26
+int w32_rand(void) {
27
+    next = next * 1103515245 + 12345;
28
+    return((unsigned)(next/65536) % (RAND_MAX+1));
29
+}
30
+
31
+void w32_srand(unsigned int seed) {
32
+    next = seed;
33
+}
34
+
0 35
new file mode 100644
... ...
@@ -0,0 +1,27 @@
0
+/*
1
+ *  Copyright (C) 2010 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 __RANDOM_H
21
+#define __RANDOM_H
22
+
23
+int w32_rand(void);
24
+void w32_srand(unsigned int seed);
25
+
26
+#endif
... ...
@@ -117,79 +117,82 @@ EXPORTS cli_hex2str @44261 NONAME
117 117
 EXPORTS cli_hex2ui @44262 NONAME
118 118
 
119 119
 ; compatibility layer, tommath, zlib
120
-EXPORTS gettimeofday @44263 NONAME
121
-EXPORTS opendir @44264 NONAME
122
-EXPORTS readdir @44265 NONAME
123
-EXPORTS closedir @44266 NONAME
124
-EXPORTS safe_open @44267 NONAME
125
-EXPORTS snprintf @44268 NONAME
126
-EXPORTS mp_init @44269 NONAME
127
-EXPORTS mp_read_radix @44270 NONAME
128
-EXPORTS mp_clear @44271 NONAME
129
-EXPORTS sha256_init @44272 NONAME
130
-EXPORTS sha256_update @44273 NONAME
131
-EXPORTS sha256_final @44274 NONAME
132
-EXPORTS optget @44275 NONAME
133
-EXPORTS optparse @44276 NONAME
134
-EXPORTS optfree @44277 NONAME
135
-EXPORTS w32_glob @44278 NONAME
136
-EXPORTS dirname @44279 NONAME
137
-EXPORTS basename @44280 NONAME
138
-EXPORTS clam_options @44281 NONAME DATA
120
+EXPORTS w32_srand @44263 NONAME
121
+EXPORTS w32_rand @44264 NONAME
122
+EXPORTS gettimeofday @44265 NONAME
123
+EXPORTS opendir @44266 NONAME
124
+EXPORTS readdir @44267 NONAME
125
+EXPORTS closedir @44268 NONAME
126
+EXPORTS safe_open @44269 NONAME
127
+EXPORTS snprintf @44270 NONAME
128
+EXPORTS mp_init @44271 NONAME
129
+EXPORTS mp_read_radix @44272 NONAME
130
+EXPORTS mp_clear @44273 NONAME
131
+EXPORTS sha256_init @44274 NONAME
132
+EXPORTS sha256_update @44275 NONAME
133
+EXPORTS sha256_final @44276 NONAME
134
+EXPORTS optget @44277 NONAME
135
+EXPORTS optparse @44278 NONAME
136
+EXPORTS optfree @44279 NONAME
137
+EXPORTS w32_glob @44280 NONAME
138
+EXPORTS dirname @44281 NONAME
139
+EXPORTS basename @44282 NONAME
140
+EXPORTS clam_options @44283 NONAME DATA
139 141
 
140 142
 ; zlib
141
-EXPORTS gzopen @44282 NONAME
142
-EXPORTS gzgets @44283 NONAME
143
-EXPORTS gzdopen @44284 NONAME
144
-EXPORTS gzclose @44285 NONAME
145
-EXPORTS gzwrite @44286 NONAME
143
+EXPORTS gzopen @44284 NONAME
144
+EXPORTS gzgets @44285 NONAME
145
+EXPORTS gzdopen @44286 NONAME
146
+EXPORTS gzclose @44287 NONAME
147
+EXPORTS gzwrite @44288 NONAME
146 148
 
147 149
 ; pthreads
148
-EXPORTS pthread_mutex_lock @44287 NONAME
149
-EXPORTS pthread_mutex_unlock @44288 NONAME
150
-EXPORTS pthread_mutex_destroy @44289 NONAME
151
-EXPORTS pthread_once @44290 NONAME
152
-EXPORTS pthread_getspecific @44291 NONAME
153
-EXPORTS pthread_setspecific @44292 NONAME
154
-EXPORTS pthread_create @44293 NONAME
155
-EXPORTS pthread_cond_timedwait @44294 NONAME
156
-EXPORTS pthread_cond_init @44295 NONAME
157
-EXPORTS pthread_cond_broadcast @44296 NONAME
158
-EXPORTS pthread_cond_signal @44297 NONAME
159
-EXPORTS pthread_cond_destroy @44298 NONAME
160
-EXPORTS pthread_join @44299 NONAME
161
-EXPORTS pthread_key_create @44300 NONAME
162
-EXPORTS pthread_cond_wait @44301 NONAME
163
-EXPORTS pthread_attr_init @44302 NONAME
164
-EXPORTS pthread_attr_setdetachstate @44303 NONAME
165
-EXPORTS pthread_attr_destroy @44304 NONAME
166
-EXPORTS pthread_mutex_init @44305 NONAME
150
+EXPORTS pthread_mutex_lock @44289 NONAME
151
+EXPORTS pthread_mutex_unlock @44290 NONAME
152
+EXPORTS pthread_mutex_destroy @44291 NONAME
153
+EXPORTS pthread_once @44292 NONAME
154
+EXPORTS pthread_getspecific @44293 NONAME
155
+EXPORTS pthread_setspecific @44294 NONAME
156
+EXPORTS pthread_create @44295 NONAME
157
+EXPORTS pthread_cond_timedwait @44296 NONAME
158
+EXPORTS pthread_cond_init @44297 NONAME
159
+EXPORTS pthread_cond_broadcast @44298 NONAME
160
+EXPORTS pthread_cond_signal @44299 NONAME
161
+EXPORTS pthread_cond_destroy @44300 NONAME
162
+EXPORTS pthread_join @44301 NONAME
163
+EXPORTS pthread_key_create @44302 NONAME
164
+EXPORTS pthread_cond_wait @44303 NONAME
165
+EXPORTS pthread_attr_init @44304 NONAME
166
+EXPORTS pthread_attr_setdetachstate @44305 NONAME
167
+EXPORTS pthread_attr_destroy @44306 NONAME
168
+EXPORTS pthread_mutex_init @44307 NONAME
167 169
 
168 170
 ; winsock bridge and compatibility functions
169
-EXPORTS htonl @44306 NONAME
170
-EXPORTS htons @44307 NONAME
171
-EXPORTS ntohl @44308 NONAME
172
-EXPORTS ntohs @44309 NONAME
173
-EXPORTS __WSAFDIsSet @44310 NONAME
174
-EXPORTS w32_socket @44311 NONAME
175
-EXPORTS w32_getsockopt @44312 NONAME
176
-EXPORTS w32_setsockopt @44313 NONAME
177
-EXPORTS w32_bind @44314 NONAME
178
-EXPORTS w32_listen @44315 NONAME
179
-EXPORTS w32_accept @44316 NONAME
180
-EXPORTS w32_connect @44317 NONAME
181
-EXPORTS w32_shutdown @44318 NONAME
182
-EXPORTS w32_send @44319 NONAME
183
-EXPORTS w32_recv @44320 NONAME
184
-EXPORTS w32_closesocket @44321 NONAME
185
-EXPORTS w32_getservbyname @44322 NONAME
186
-EXPORTS w32_getaddrinfo @44323 NONAME
187
-EXPORTS w32_freeaddrinfo @44324 NONAME
188
-EXPORTS w32_inet_ntop @44325 NONAME
189
-EXPORTS w32_gethostbyname @44326 NONAME
190
-EXPORTS w32_select @44327 NONAME
191
-EXPORTS poll_with_event @44328 NONAME
192
-EXPORTS w32_stat @44329 NONAME
193
-EXPORTS w32_strerror @44330 NONAME
194
-EXPORTS w32_strerror_r @44331 NONAME
195
-EXPORTS inet_addr @44332 NONAME
171
+EXPORTS htonl @44308 NONAME
172
+EXPORTS htons @44309 NONAME
173
+EXPORTS ntohl @44310 NONAME
174
+EXPORTS ntohs @44311 NONAME
175
+EXPORTS __WSAFDIsSet @44312 NONAME
176
+EXPORTS w32_socket @44313 NONAME
177
+EXPORTS w32_getsockopt @44314 NONAME
178
+EXPORTS w32_setsockopt @44315 NONAME
179
+EXPORTS w32_bind @44316 NONAME
180
+EXPORTS w32_listen @44317 NONAME
181
+EXPORTS w32_accept @44318 NONAME
182
+EXPORTS w32_connect @44319 NONAME
183
+EXPORTS w32_shutdown @44320 NONAME
184
+EXPORTS w32_send @44321 NONAME
185
+EXPORTS w32_recv @44322 NONAME
186
+EXPORTS w32_closesocket @44323 NONAME
187
+EXPORTS w32_getservbyname @44324 NONAME
188
+EXPORTS w32_getaddrinfo @44325 NONAME
189
+EXPORTS w32_freeaddrinfo @44326 NONAME
190
+EXPORTS w32_inet_ntop @44327 NONAME
191
+EXPORTS w32_gethostbyname @44328 NONAME
192
+EXPORTS w32_select @44329 NONAME
193
+EXPORTS poll_with_event @44330 NONAME
194
+EXPORTS w32_stat @44331 NONAME
195
+EXPORTS w32_strerror @44332 NONAME
196
+EXPORTS w32_strerror_r @44333 NONAME
197
+EXPORTS inet_addr @44334 NONAME
198
+EXPORTS sock_set_nonblock @44335 NONAME
... ...
@@ -812,6 +812,10 @@
812 812
 					>
813 813
 				</File>
814 814
 				<File
815
+					RelativePath=".\compat\random.c"
816
+					>
817
+				</File>
818
+				<File
815 819
 					RelativePath=".\compat\snprintf.c"
816 820
 					>
817 821
 				</File>
... ...
@@ -16,6 +16,7 @@
16 16
 #include "net.h"
17 17
 #include "w32_errno.h"
18 18
 #include "w32_stat.h"
19
+#include "random.h"
19 20
 
20 21
 #ifndef __cplusplus
21 22
 typedef unsigned short mode_t;
... ...
@@ -30,6 +31,8 @@ typedef unsigned short mode_t;
30 30
 
31 31
 char *strptime(const char *s, const char *format, struct tm *tm);
32 32
 
33
+#define srand w32_srand
34
+#define rand w32_rand
33 35
 #define socket w32_socket
34 36
 #define getsockopt w32_getsockopt
35 37
 #define setsockopt w32_setsockopt