2 * $Id: squid_mswin.h,v 1.10 2008/02/26 21:49:33 amosjeffries Exp $
4 * AUTHOR: Andrey Shorin <tolsty@tushino.com>
5 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #ifndef _INC_SQUID_MSWIN_H
35 #define _INC_SQUID_MSWIN_H
41 #define ACL WindowsACL
42 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
44 #define _CRT_SECURE_NO_DEPRECATE
45 #pragma warning( disable : 4290 )
46 #pragma warning( disable : 4996 )
50 #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64
51 # define __USE_FILE_OFFSET64 1
54 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
56 typedef unsigned char uint8_t;
57 typedef unsigned short uint16_t;
58 typedef unsigned int uint32_t;
59 typedef unsigned __int64
uint64_t;
63 #if defined __USE_FILE_OFFSET64
64 typedef int64_t off_t
;
65 typedef uint64_t ino_t
;
69 typedef unsigned long ino_t
;
73 #define INT64_MAX _I64_MAX
74 #define INT64_MIN _I64_MIN
76 #include "default_config_file.h"
77 /* Some tricks for MS Compilers */
79 #pragma include_alias(<dirent.h>, <direct.h>)
80 #define THREADLOCAL __declspec(thread)
82 #elif defined(__GNUC__) /* gcc environment */
84 #define THREADLOCAL __attribute__((section(".tls")))
88 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
89 #define alloca _alloca
94 #define fdopen _fdopen
95 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
96 #define fileno _fileno
97 #define fstat _fstati64
99 #define ftruncate WIN32_ftruncate
100 #define getcwd _getcwd
101 #define getpid _getpid
102 #define getrusage WIN32_getrusage
103 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
104 #define lseek _lseeki64
105 #define memccpy _memccpy
106 #define mkdir(p) _mkdir(p)
107 #define mktemp _mktemp
109 #define pclose _pclose
110 #define pipe WIN32_pipe
112 #define putenv _putenv
113 #define setmode _setmode
114 #define sleep(t) Sleep((t)*1000)
115 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
116 #define snprintf _snprintf
117 #define stat _stati64
118 #define strcasecmp _stricmp
119 #define strdup _strdup
120 #define strlwr _strlwr
121 #define strncasecmp _strnicmp
122 #define tempnam _tempnam
124 #define truncate WIN32_truncate
126 #define unlink _unlink
127 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
128 #define vsnprintf _vsnprintf
131 #define O_RDONLY _O_RDONLY
132 #define O_WRONLY _O_WRONLY
133 #define O_RDWR _O_RDWR
134 #define O_APPEND _O_APPEND
136 #define O_CREAT _O_CREAT
137 #define O_TRUNC _O_TRUNC
138 #define O_EXCL _O_EXCL
140 #define O_TEXT _O_TEXT
141 #define O_BINARY _O_BINARY
142 #define O_RAW _O_BINARY
143 #define O_TEMPORARY _O_TEMPORARY
144 #define O_NOINHERIT _O_NOINHERIT
145 #define O_SEQUENTIAL _O_SEQUENTIAL
146 #define O_RANDOM _O_RANDOM
149 #define S_IFMT _S_IFMT
150 #define S_IFDIR _S_IFDIR
151 #define S_IFCHR _S_IFCHR
152 #define S_IFREG _S_IFREG
153 #define S_IREAD _S_IREAD
154 #define S_IWRITE _S_IWRITE
155 #define S_IEXEC _S_IEXEC
158 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
159 #define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
162 #define SIGHUP 1 /* hangup */
163 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
164 #define SIGBUS 10 /* bus error */
165 #define SIGPIPE 13 /* write on a pipe with no one to read it */
166 #define SIGCHLD 20 /* to parent on child stop or exit */
167 #define SIGUSR1 30 /* user defined signal 1 */
168 #define SIGUSR2 31 /* user defined signal 2 */
170 typedef unsigned short int ushort
;
175 char *pw_name
; /* user name */
176 char *pw_passwd
; /* user password */
177 uid_t pw_uid
; /* user id */
178 gid_t pw_gid
; /* group id */
179 char *pw_gecos
; /* real name */
180 char *pw_dir
; /* home directory */
181 char *pw_shell
; /* shell program */
185 char *gr_name
; /* group name */
186 char *gr_passwd
; /* group password */
187 gid_t gr_gid
; /* group id */
188 char **gr_mem
; /* group members */
192 long f_type
; /* type of filesystem (see below) */
193 long f_bsize
; /* optimal transfer block size */
194 long f_blocks
; /* total data blocks in file system */
195 long f_bfree
; /* free blocks in fs */
196 long f_bavail
; /* free blocks avail to non-superuser */
197 long f_files
; /* total file nodes in file system */
198 long f_ffree
; /* free file nodes in fs */
199 long f_fsid
; /* file system id */
200 long f_namelen
; /* maximum length of filenames */
201 long f_spare
[6]; /* spare for later */
204 #ifndef HAVE_GETTIMEOFDAY
207 int tz_minuteswest
; /* minutes west of Greenwich */
208 int tz_dsttime
; /* type of dst correction */
212 #define CHANGE_FD_SETSIZE 1
213 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
214 #define FD_SETSIZE SQUID_MAXFD
220 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
221 #include <winsock2.h>
223 #include <ws2tcpip.h>
224 #if (EAI_NODATA == EAI_NONAME)
226 #define EAI_NODATA WSANO_DATA
228 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
229 /* Hack to suppress compiler warnings on FD_SET() & FD_CLR() */
230 #pragma warning (push)
231 #pragma warning (disable:4142)
233 /* prevent inclusion of wingdi.h */
236 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
237 #pragma warning (pop)
242 #include <sys/types.h>
245 typedef char * caddr_t
;
251 #define EISCONN WSAEISCONN
252 #define EINPROGRESS WSAEINPROGRESS
253 #define EWOULDBLOCK WSAEWOULDBLOCK
254 #define EALREADY WSAEALREADY
255 #define ETIMEDOUT WSAETIMEDOUT
256 #define ECONNREFUSED WSAECONNREFUSED
257 #define ECONNRESET WSAECONNRESET
258 #define ENOTCONN WSAENOTCONN
259 #define ERESTART WSATRY_AGAIN
260 #define EAFNOSUPPORT WSAEAFNOSUPPORT
263 #define h_errno errno /* we'll set it ourselves */
266 #define FD_CLR(fd, set) do { \
268 SOCKET __sock = fd_table[fd].win32.handle; \
269 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count ; __i++) { \
270 if (((fd_set FAR *)(set))->fd_array[__i] == __sock) { \
271 while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
272 ((fd_set FAR *)(set))->fd_array[__i] = \
273 ((fd_set FAR *)(set))->fd_array[__i+1]; \
276 ((fd_set FAR *)(set))->fd_count--; \
283 #define FD_SET(fd, set) do { \
285 SOCKET __sock = fd_table[fd].win32.handle; \
286 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
287 if (((fd_set FAR *)(set))->fd_array[__i] == (__sock)) { \
291 if (__i == ((fd_set FAR *)(set))->fd_count) { \
292 if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
293 ((fd_set FAR *)(set))->fd_array[__i] = (__sock); \
294 ((fd_set FAR *)(set))->fd_count++; \
300 #define FD_ISSET(fd, set) Win32__WSAFDIsSet(fd, (fd_set FAR *)(set))
302 /* internal to Microsoft CRTLIB */
304 long osfhnd
; /* underlying OS file HANDLE */
305 char osfile
; /* attributes of file (e.g., open in text mode?) */
306 char pipech
; /* one char buffer for handles opened on pipes */
309 CRITICAL_SECTION lock
;
313 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
314 #define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)) )
315 #define _osfile(i) ( _pioinfo(i)->osfile )
316 #define _osfhnd(i) ( _pioinfo(i)->osfhnd )
317 #define FOPEN 0x01 /* file handle open */
319 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
321 SQUIDCEXTERN _CRTIMP ioinfo
* __pioinfo
[];
322 SQUIDCEXTERN
int __cdecl
_free_osfhnd(int);
324 #elif defined(__MINGW32__) /* MinGW environment */
326 __MINGW_IMPORT ioinfo
* __pioinfo
[];
327 SQUIDCEXTERN
int _free_osfhnd(int);
331 SQUIDCEXTERN THREADLOCAL
int ws32_result
;
333 #define strerror(e) WIN32_strerror(e)
340 char l_so_type
[sizeof(int)];
341 int l_so_type_siz
= sizeof(l_so_type
);
342 SOCKET sock
= _get_osfhandle(fd
);
344 if (::getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, l_so_type
, &l_so_type_siz
) == 0){
346 if (closesocket(sock
) == SOCKET_ERROR
) {
347 errno
= WSAGetLastError();
358 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
361 #define _S_IREAD 0x0100
365 #define _S_IWRITE 0x0080
369 int open(const char *filename
, int oflag
, int pmode
= 0)
371 return _open(filename
, oflag
, pmode
& (_S_IREAD
| _S_IWRITE
));
376 int read(int fd
, void * buf
, size_t siz
)
378 char l_so_type
[sizeof(int)];
379 int l_so_type_siz
= sizeof(l_so_type
);
380 SOCKET sock
= _get_osfhandle(fd
);
382 if (::getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, l_so_type
, &l_so_type_siz
) == 0)
383 return ::recv(sock
, (char FAR
*) buf
, (int)siz
, 0);
385 return _read(fd
, buf
, (unsigned int)siz
);
389 int write(int fd
, const void * buf
, size_t siz
)
391 char l_so_type
[sizeof(int)];
392 int l_so_type_siz
= sizeof(l_so_type
);
393 SOCKET sock
= _get_osfhandle(fd
);
395 if (::getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, l_so_type
, &l_so_type_siz
) == 0)
396 return ::send(sock
, (char FAR
*) buf
, siz
, 0);
398 return _write(fd
, buf
, siz
);
402 char *index(const char *s
, int c
)
404 return (char *)strchr(s
,c
);
407 /** \cond AUTODOCS-IGNORE */
412 int accept(int s
, struct sockaddr
* a
, size_t * l
)
415 if ((result
= ::accept(_get_osfhandle(s
), a
, (int *)l
)) == INVALID_SOCKET
) {
416 if (WSAEMFILE
== (errno
= WSAGetLastError()))
421 return _open_osfhandle(result
, 0);
425 int bind(int s
, struct sockaddr
* n
, int l
)
427 if (::bind(_get_osfhandle(s
),n
,l
) == SOCKET_ERROR
) {
428 errno
= WSAGetLastError();
436 int connect(int s
, const struct sockaddr
* n
, int l
)
438 if (::connect(_get_osfhandle(s
),n
,l
) == SOCKET_ERROR
) {
439 if (WSAEMFILE
== (errno
= WSAGetLastError()))
448 struct hostent
* gethostbyname (const char *n
)
450 HOSTENT FAR
* result
;
451 if ((result
= ::gethostbyname(n
)) == NULL
)
452 errno
= WSAGetLastError();
455 #define gethostbyname(n) Squid::gethostbyname(n)
458 SERVENT FAR
* getservbyname (const char * n
, const char * p
)
460 SERVENT FAR
* result
;
461 if ((result
= ::getservbyname(n
, p
)) == NULL
)
462 errno
= WSAGetLastError();
465 #define getservbyname(n,p) Squid::getservbyname(n,p)
468 HOSTENT FAR
* gethostbyaddr(const char * a
, int l
, int t
)
470 HOSTENT FAR
* result
;
471 if ((result
= ::gethostbyaddr(a
, l
, t
)) == NULL
)
472 errno
= WSAGetLastError();
475 #define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
478 int getsockname(int s
, struct sockaddr
* n
, size_t * l
)
480 if ((::getsockname(_get_osfhandle(s
), n
, (int *)l
)) == SOCKET_ERROR
) {
481 errno
= WSAGetLastError();
489 int gethostname(char * n
, size_t l
)
491 if ((::gethostname(n
, l
)) == SOCKET_ERROR
) {
492 errno
= WSAGetLastError();
498 #define gethostname(n,l) Squid::gethostname(n,l)
501 int getsockopt(int s
, int l
, int o
, void * v
, int * n
)
504 if ((::getsockopt(_get_osfhandle(s
), l
, o
,(char *) v
, n
)) == SOCKET_ERROR
) {
505 errno
= WSAGetLastError();
512 /* Simple ioctl() emulation */
514 int ioctl(int s
, int c
, void * a
)
516 if ((::ioctlsocket(_get_osfhandle(s
), c
, (u_long FAR
*)a
)) == SOCKET_ERROR
) {
517 errno
= WSAGetLastError();
525 int ioctlsocket(int s
, long c
, u_long FAR
* a
)
527 if ((::ioctlsocket(_get_osfhandle(s
), c
, a
)) == SOCKET_ERROR
) {
528 errno
= WSAGetLastError();
536 int listen(int s
, int b
)
538 if (::listen(_get_osfhandle(s
), b
) == SOCKET_ERROR
) {
539 if (WSAEMFILE
== (errno
= WSAGetLastError()))
546 #define listen(s,b) Squid::listen(s,b)
549 int recv(int s
, void * b
, size_t l
, int f
)
552 if ((result
= ::recv(_get_osfhandle(s
), (char *)b
, l
, f
)) == SOCKET_ERROR
) {
553 errno
= WSAGetLastError();
561 int recvfrom(int s
, void * b
, size_t l
, int f
, struct sockaddr
* fr
, size_t * fl
)
564 if ((result
= ::recvfrom(_get_osfhandle(s
), (char *)b
, l
, f
, fr
, (int *)fl
)) == SOCKET_ERROR
) {
565 errno
= WSAGetLastError();
573 int select(int n
, fd_set
* r
, fd_set
* w
, fd_set
* e
, struct timeval
* t
)
576 if ((result
= ::select(n
,r
,w
,e
,t
)) == SOCKET_ERROR
) {
577 errno
= WSAGetLastError();
583 #define select(n,r,w,e,t) Squid::select(n,r,w,e,t)
586 int send(int s
, const void * b
, size_t l
, int f
)
589 if ((result
= ::send(_get_osfhandle(s
), (char *)b
, l
, f
)) == SOCKET_ERROR
) {
590 errno
= WSAGetLastError();
598 int sendto(int s
, const void * b
, size_t l
, int f
, const struct sockaddr
* t
, int tl
)
601 if ((result
= ::sendto(_get_osfhandle(s
), (char *)b
, l
, f
, t
, tl
)) == SOCKET_ERROR
) {
602 errno
= WSAGetLastError();
610 int setsockopt(SOCKET s
, int l
, int o
, const char * v
, int n
)
614 socket
= ((s
== INVALID_SOCKET
) ? s
: (SOCKET
)_get_osfhandle((int)s
));
616 if (::setsockopt(socket
, l
, o
, v
, n
) == SOCKET_ERROR
) {
617 errno
= WSAGetLastError();
623 #define setsockopt(s,l,o,v,n) Squid::setsockopt(s,l,o,v,n)
626 int shutdown(int s
, int h
)
628 if (::shutdown(_get_osfhandle(s
),h
) == SOCKET_ERROR
) {
629 errno
= WSAGetLastError();
637 int socket(int f
, int t
, int p
)
640 if ((result
= ::socket(f
, t
, p
)) == INVALID_SOCKET
) {
641 if (WSAEMFILE
== (errno
= WSAGetLastError()))
646 return _open_osfhandle(result
, 0);
648 #define socket(f,t,p) Squid::socket(f,t,p)
651 int WSAAsyncSelect(int s
, HWND h
, unsigned int w
, long e
)
653 if (::WSAAsyncSelect(_get_osfhandle(s
), h
, w
, e
) == SOCKET_ERROR
) {
654 errno
= WSAGetLastError();
661 #undef WSADuplicateSocket
663 int WSADuplicateSocket(int s
, DWORD n
, LPWSAPROTOCOL_INFO l
)
666 if (::WSADuplicateSocketW(_get_osfhandle(s
), n
, l
) == SOCKET_ERROR
) {
668 if (::WSADuplicateSocketA(_get_osfhandle(s
), n
, l
) == SOCKET_ERROR
) {
670 errno
= WSAGetLastError();
679 int WSASocket(int a
, int t
, int p
, LPWSAPROTOCOL_INFO i
, GROUP g
, DWORD f
)
683 if ((result
= ::WSASocketW(a
, t
, p
, i
, g
, f
)) == INVALID_SOCKET
) {
685 if ((result
= ::WSASocketA(a
, t
, p
, i
, g
, f
)) == INVALID_SOCKET
) {
687 if (WSAEMFILE
== (errno
= WSAGetLastError()))
692 return _open_osfhandle(result
, 0);
695 } /* namespace Squid */
697 #else /* #ifdef __cplusplus */
698 #define connect(s,n,l) \
699 (SOCKET_ERROR == connect(_get_osfhandle(s),n,l) ? \
700 (WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1, -1) : 0)
701 #define gethostbyname(n) \
702 (NULL == ((HOSTENT FAR*)(ws32_result = (int)gethostbyname(n))) ? \
703 (errno = WSAGetLastError()), (HOSTENT FAR*)NULL : (HOSTENT FAR*)ws32_result)
704 #define gethostname(n,l) \
705 (SOCKET_ERROR == gethostname(n,l) ? \
706 (errno = WSAGetLastError()), -1 : 0)
707 #define recv(s,b,l,f) \
708 (SOCKET_ERROR == (ws32_result = recv(_get_osfhandle(s),b,l,f)) ? \
709 (errno = WSAGetLastError()), -1 : ws32_result)
710 #define sendto(s,b,l,f,t,tl) \
711 (SOCKET_ERROR == (ws32_result = sendto(_get_osfhandle(s),b,l,f,t,tl)) ? \
712 (errno = WSAGetLastError()), -1 : ws32_result)
713 #define select(n,r,w,e,t) \
714 (SOCKET_ERROR == (ws32_result = select(n,r,w,e,t)) ? \
715 (errno = WSAGetLastError()), -1 : ws32_result)
716 #define socket(f,t,p) \
717 (INVALID_SOCKET == ((SOCKET)(ws32_result = (int)socket(f,t,p))) ? \
718 ((WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1), -1) : \
719 (SOCKET)_open_osfhandle(ws32_result,0))
720 #define write _write /* Needed in util.c */
721 #define open _open /* Needed in win32lib.c */
722 #endif /* #ifdef __cplusplus */
724 #define RUSAGE_SELF 0 /* calling process */
725 #define RUSAGE_CHILDREN -1 /* terminated child processes */
728 struct timeval ru_utime
; /* user time used */
729 struct timeval ru_stime
; /* system time used */
730 long ru_maxrss
; /* integral max resident set size */
731 long ru_ixrss
; /* integral shared text memory size */
732 long ru_idrss
; /* integral unshared data size */
733 long ru_isrss
; /* integral unshared stack size */
734 long ru_minflt
; /* page reclaims */
735 long ru_majflt
; /* page faults */
736 long ru_nswap
; /* swaps */
737 long ru_inblock
; /* block input operations */
738 long ru_oublock
; /* block output operations */
739 long ru_msgsnd
; /* messages sent */
740 long ru_msgrcv
; /* messages received */
741 long ru_nsignals
; /* signals received */
742 long ru_nvcsw
; /* voluntary context switches */
743 long ru_nivcsw
; /* involuntary context switches */
749 #endif /* _SQUID_WIN32_ */
751 #endif /* _INC_SQUID_MSWIN_H */