2 * $Id: squid_mswin.h,v 1.1 2006/09/02 13:30:54 serassio Exp $
4 * AUTHOR: Guido Serassio & Andrey Shorin
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #define ACL WindowsACL
35 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
37 #define _CRT_SECURE_NO_DEPRECATE
38 #pragma warning( disable : 4290 )
39 #pragma warning( disable : 4996 )
43 #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64
44 # define __USE_FILE_OFFSET64 1
47 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
49 typedef unsigned short uint16_t;
50 typedef unsigned int uint32_t;
51 typedef unsigned __int64
uint64_t;
55 #if defined __USE_FILE_OFFSET64
56 typedef int64_t off_t
;
57 typedef uint64_t ino_t
;
61 typedef unsigned long ino_t
;
65 #define INT64_MAX _I64_MAX
66 #define INT64_MIN _I64_MIN
69 #include "default_config_file.h"
70 /* Some tricks for MS Compilers */
72 #pragma include_alias(<dirent.h>, <direct.h>)
73 #define THREADLOCAL __declspec(thread)
75 #elif defined(__GNUC__) /* gcc environment */
77 #define THREADLOCAL __attribute__((section(".tls")))
81 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
82 #define alloca _alloca
87 #define fdopen _fdopen
88 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
89 #define fileno _fileno
90 #define fstat _fstati64
92 #define ftruncate WIN32_ftruncate
93 #define getcwd _getcwd
94 #define getpid _getpid
95 #define getrusage WIN32_getrusage
96 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
97 #define lseek _lseeki64
98 #define memccpy _memccpy
99 #define mkdir(p) _mkdir(p)
100 #define mktemp _mktemp
102 #define pclose _pclose
103 #define pipe WIN32_pipe
105 #define putenv _putenv
106 #define setmode _setmode
107 #define sleep(t) Sleep((t)*1000)
108 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
109 #define snprintf _snprintf
110 #define stat _stati64
111 #define strcasecmp _stricmp
112 #define strdup _strdup
113 #define strlwr _strlwr
114 #define strncasecmp _strnicmp
115 #define tempnam _tempnam
117 #define truncate WIN32_truncate
119 #define unlink _unlink
120 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
121 #define vsnprintf _vsnprintf
124 #define O_RDONLY _O_RDONLY
125 #define O_WRONLY _O_WRONLY
126 #define O_RDWR _O_RDWR
127 #define O_APPEND _O_APPEND
129 #define O_CREAT _O_CREAT
130 #define O_TRUNC _O_TRUNC
131 #define O_EXCL _O_EXCL
133 #define O_TEXT _O_TEXT
134 #define O_BINARY _O_BINARY
135 #define O_RAW _O_BINARY
136 #define O_TEMPORARY _O_TEMPORARY
137 #define O_NOINHERIT _O_NOINHERIT
138 #define O_SEQUENTIAL _O_SEQUENTIAL
139 #define O_RANDOM _O_RANDOM
142 #define S_IFMT _S_IFMT
143 #define S_IFDIR _S_IFDIR
144 #define S_IFCHR _S_IFCHR
145 #define S_IFREG _S_IFREG
146 #define S_IREAD _S_IREAD
147 #define S_IWRITE _S_IWRITE
148 #define S_IEXEC _S_IEXEC
151 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
152 #define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
155 #define SIGHUP 1 /* hangup */
156 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
157 #define SIGBUS 10 /* bus error */
158 #define SIGPIPE 13 /* write on a pipe with no one to read it */
159 #define SIGCHLD 20 /* to parent on child stop or exit */
160 #define SIGUSR1 30 /* user defined signal 1 */
161 #define SIGUSR2 31 /* user defined signal 2 */
163 typedef unsigned short int ushort
;
168 char *pw_name
; /* user name */
169 char *pw_passwd
; /* user password */
170 uid_t pw_uid
; /* user id */
171 gid_t pw_gid
; /* group id */
172 char *pw_gecos
; /* real name */
173 char *pw_dir
; /* home directory */
174 char *pw_shell
; /* shell program */
178 char *gr_name
; /* group name */
179 char *gr_passwd
; /* group password */
180 gid_t gr_gid
; /* group id */
181 char **gr_mem
; /* group members */
185 long f_type
; /* type of filesystem (see below) */
186 long f_bsize
; /* optimal transfer block size */
187 long f_blocks
; /* total data blocks in file system */
188 long f_bfree
; /* free blocks in fs */
189 long f_bavail
; /* free blocks avail to non-superuser */
190 long f_files
; /* total file nodes in file system */
191 long f_ffree
; /* free file nodes in fs */
192 long f_fsid
; /* file system id */
193 long f_namelen
; /* maximum length of filenames */
194 long f_spare
[6]; /* spare for later */
199 int tz_minuteswest
; /* minutes west of Greenwich */
200 int tz_dsttime
; /* type of dst correction */
203 #define CHANGE_FD_SETSIZE 1
204 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
205 #define FD_SETSIZE SQUID_MAXFD
211 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
212 /* Hack to suppress compiler warnings on FD_SET() & FD_CLR() */
213 #pragma warning (push)
214 #pragma warning (disable:4142)
216 /* prevent inclusion of wingdi.h */
219 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
220 #pragma warning (pop)
225 #include <sys/types.h>
228 typedef char * caddr_t
;
234 #define EISCONN WSAEISCONN
235 #define EINPROGRESS WSAEINPROGRESS
236 #define EWOULDBLOCK WSAEWOULDBLOCK
237 #define EALREADY WSAEALREADY
238 #define ETIMEDOUT WSAETIMEDOUT
239 #define ECONNREFUSED WSAECONNREFUSED
240 #define ECONNRESET WSAECONNRESET
241 #define ENOTCONN WSAENOTCONN
242 #define ERESTART WSATRY_AGAIN
245 #define h_errno errno /* we'll set it ourselves */
248 #define FD_CLR(fd, set) do { \
250 SOCKET __sock = fd_table[fd].win32.handle; \
251 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count ; __i++) { \
252 if (((fd_set FAR *)(set))->fd_array[__i] == __sock) { \
253 while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
254 ((fd_set FAR *)(set))->fd_array[__i] = \
255 ((fd_set FAR *)(set))->fd_array[__i+1]; \
258 ((fd_set FAR *)(set))->fd_count--; \
265 #define FD_SET(fd, set) do { \
267 SOCKET __sock = fd_table[fd].win32.handle; \
268 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
269 if (((fd_set FAR *)(set))->fd_array[__i] == (__sock)) { \
273 if (__i == ((fd_set FAR *)(set))->fd_count) { \
274 if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
275 ((fd_set FAR *)(set))->fd_array[__i] = (__sock); \
276 ((fd_set FAR *)(set))->fd_count++; \
282 #define FD_ISSET(fd, set) Win32__WSAFDIsSet(fd, (fd_set FAR *)(set))
284 /* internal to Microsoft CRTLIB */
286 long osfhnd
; /* underlying OS file HANDLE */
287 char osfile
; /* attributes of file (e.g., open in text mode?) */
288 char pipech
; /* one char buffer for handles opened on pipes */
291 CRITICAL_SECTION lock
;
295 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
296 #define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)) )
297 #define _osfile(i) ( _pioinfo(i)->osfile )
298 #define _osfhnd(i) ( _pioinfo(i)->osfhnd )
299 #define FOPEN 0x01 /* file handle open */
301 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
303 SQUIDCEXTERN _CRTIMP ioinfo
* __pioinfo
[];
304 SQUIDCEXTERN
int __cdecl
_free_osfhnd(int);
306 #elif defined(__MINGW32__) /* MinGW environment */
308 __MINGW_IMPORT ioinfo
* __pioinfo
[];
309 SQUIDCEXTERN
int _free_osfhnd(int);
313 SQUIDCEXTERN THREADLOCAL
int ws32_result
;
315 #define strerror(e) WIN32_strerror(e)
322 char l_so_type
[sizeof(int)];
323 int l_so_type_siz
= sizeof(l_so_type
);
324 SOCKET sock
= _get_osfhandle(fd
);
326 if (::getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, l_so_type
, &l_so_type_siz
) == 0){
328 if (closesocket(sock
) == SOCKET_ERROR
) {
329 errno
= WSAGetLastError();
340 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
343 #define _S_IREAD 0x0100
347 #define _S_IWRITE 0x0080
351 int open(const char *filename
, int oflag
, int pmode
= 0)
353 return _open(filename
, oflag
, pmode
& (_S_IREAD
| _S_IWRITE
));
358 int read(int fd
, void * buf
, size_t siz
)
360 char l_so_type
[sizeof(int)];
361 int l_so_type_siz
= sizeof(l_so_type
);
362 SOCKET sock
= _get_osfhandle(fd
);
364 if (::getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, l_so_type
, &l_so_type_siz
) == 0)
365 return ::recv(sock
, (char FAR
*) buf
, (int)siz
, 0);
367 return _read(fd
, buf
, (unsigned int)siz
);
371 int write(int fd
, const void * buf
, size_t siz
)
373 char l_so_type
[sizeof(int)];
374 int l_so_type_siz
= sizeof(l_so_type
);
375 SOCKET sock
= _get_osfhandle(fd
);
377 if (::getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, l_so_type
, &l_so_type_siz
) == 0)
378 return ::send(sock
, (char FAR
*) buf
, siz
, 0);
380 return _write(fd
, buf
, siz
);
384 char *index(const char *s
, int c
)
386 return (char *)strchr(s
,c
);
392 int accept(int s
, struct sockaddr
* a
, int * l
)
395 if ((result
= ::accept(_get_osfhandle(s
), a
, l
)) == INVALID_SOCKET
) {
396 if (WSAEMFILE
== (errno
= WSAGetLastError()))
401 return _open_osfhandle(result
, 0);
405 int bind(int s
, struct sockaddr
* n
, int l
)
407 if (::bind(_get_osfhandle(s
),n
,l
) == SOCKET_ERROR
) {
408 errno
= WSAGetLastError();
416 int connect(int s
, const struct sockaddr
* n
, int l
)
418 if (::connect(_get_osfhandle(s
),n
,l
) == SOCKET_ERROR
) {
419 if (WSAEMFILE
== (errno
= WSAGetLastError()))
428 struct hostent
* gethostbyname (const char *n
)
430 HOSTENT FAR
* result
;
431 if ((result
= ::gethostbyname(n
)) == NULL
)
432 errno
= WSAGetLastError();
435 #define gethostbyname(n) Squid::gethostbyname(n)
438 SERVENT FAR
* getservbyname (const char * n
, const char * p
)
440 SERVENT FAR
* result
;
441 if ((result
= ::getservbyname(n
, p
)) == NULL
)
442 errno
= WSAGetLastError();
445 #define getservbyname(n,p) Squid::getservbyname(n,p)
448 HOSTENT FAR
* gethostbyaddr(const char * a
, int l
, int t
)
450 HOSTENT FAR
* result
;
451 if ((result
= ::gethostbyaddr(a
, l
, t
)) == NULL
)
452 errno
= WSAGetLastError();
455 #define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
458 int getsockname(int s
, struct sockaddr
* n
, int * l
)
460 if ((::getsockname(_get_osfhandle(s
), n
, l
)) == SOCKET_ERROR
) {
461 errno
= WSAGetLastError();
469 int gethostname(char * n
, size_t l
)
471 if ((::gethostname(n
, l
)) == SOCKET_ERROR
) {
472 errno
= WSAGetLastError();
478 #define gethostname(n,l) Squid::gethostname(n,l)
481 int getsockopt(int s
, int l
, int o
, void * v
, int * n
)
484 if ((::getsockopt(_get_osfhandle(s
), l
, o
,(char *) v
, n
)) == SOCKET_ERROR
) {
485 errno
= WSAGetLastError();
492 /* Simple ioctl() emulation */
494 int ioctl(int s
, int c
, void * a
)
496 if ((::ioctlsocket(_get_osfhandle(s
), c
, (u_long FAR
*)a
)) == SOCKET_ERROR
) {
497 errno
= WSAGetLastError();
505 int ioctlsocket(int s
, long c
, u_long FAR
* a
)
507 if ((::ioctlsocket(_get_osfhandle(s
), c
, a
)) == SOCKET_ERROR
) {
508 errno
= WSAGetLastError();
516 int listen(int s
, int b
)
518 if (::listen(_get_osfhandle(s
), b
) == SOCKET_ERROR
) {
519 if (WSAEMFILE
== (errno
= WSAGetLastError()))
526 #define listen(s,b) Squid::listen(s,b)
529 int recv(int s
, void * b
, size_t l
, int f
)
532 if ((result
= ::recv(_get_osfhandle(s
), (char *)b
, l
, f
)) == SOCKET_ERROR
) {
533 errno
= WSAGetLastError();
541 int recvfrom(int s
, void * b
, size_t l
, int f
, struct sockaddr
* fr
, int * fl
)
544 if ((result
= ::recvfrom(_get_osfhandle(s
), (char *)b
, l
, f
, fr
, fl
)) == SOCKET_ERROR
) {
545 errno
= WSAGetLastError();
553 int select(int n
, fd_set
* r
, fd_set
* w
, fd_set
* e
, struct timeval
* t
)
556 if ((result
= ::select(n
,r
,w
,e
,t
)) == SOCKET_ERROR
) {
557 errno
= WSAGetLastError();
563 #define select(n,r,w,e,t) Squid::select(n,r,w,e,t)
566 int send(int s
, const void * b
, size_t l
, int f
)
569 if ((result
= ::send(_get_osfhandle(s
), (char *)b
, l
, f
)) == SOCKET_ERROR
) {
570 errno
= WSAGetLastError();
578 int sendto(int s
, const void * b
, size_t l
, int f
, const struct sockaddr
* t
, int tl
)
581 if ((result
= ::sendto(_get_osfhandle(s
), (char *)b
, l
, f
, t
, tl
)) == SOCKET_ERROR
) {
582 errno
= WSAGetLastError();
590 int setsockopt(SOCKET s
, int l
, int o
, const char * v
, int n
)
594 socket
= ((s
== INVALID_SOCKET
) ? s
: (SOCKET
)_get_osfhandle((int)s
));
596 if (::setsockopt(socket
, l
, o
, v
, n
) == SOCKET_ERROR
) {
597 errno
= WSAGetLastError();
603 #define setsockopt(s,l,o,v,n) Squid::setsockopt(s,l,o,v,n)
606 int shutdown(int s
, int h
)
608 if (::shutdown(_get_osfhandle(s
),h
) == SOCKET_ERROR
) {
609 errno
= WSAGetLastError();
617 int socket(int f
, int t
, int p
)
620 if ((result
= ::socket(f
, t
, p
)) == INVALID_SOCKET
) {
621 if (WSAEMFILE
== (errno
= WSAGetLastError()))
626 return _open_osfhandle(result
, 0);
628 #define socket(f,t,p) Squid::socket(f,t,p)
631 int WSAAsyncSelect(int s
, HWND h
, unsigned int w
, long e
)
633 if (::WSAAsyncSelect(_get_osfhandle(s
), h
, w
, e
) == SOCKET_ERROR
) {
634 errno
= WSAGetLastError();
641 #undef WSADuplicateSocket
643 int WSADuplicateSocket(int s
, DWORD n
, LPWSAPROTOCOL_INFO l
)
646 if (::WSADuplicateSocketW(_get_osfhandle(s
), n
, l
) == SOCKET_ERROR
) {
648 if (::WSADuplicateSocketA(_get_osfhandle(s
), n
, l
) == SOCKET_ERROR
) {
650 errno
= WSAGetLastError();
659 int WSASocket(int a
, int t
, int p
, LPWSAPROTOCOL_INFO i
, GROUP g
, DWORD f
)
663 if ((result
= ::WSASocketW(a
, t
, p
, i
, g
, f
)) == INVALID_SOCKET
) {
665 if ((result
= ::WSASocketA(a
, t
, p
, i
, g
, f
)) == INVALID_SOCKET
) {
667 if (WSAEMFILE
== (errno
= WSAGetLastError()))
672 return _open_osfhandle(result
, 0);
675 } /* namespace Squid */
677 #else /* #ifdef __cplusplus */
678 #define connect(s,n,l) \
679 (SOCKET_ERROR == connect(_get_osfhandle(s),n,l) ? \
680 (WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1, -1) : 0)
681 #define gethostbyname(n) \
682 (NULL == ((HOSTENT FAR*)(ws32_result = (int)gethostbyname(n))) ? \
683 (errno = WSAGetLastError()), (HOSTENT FAR*)NULL : (HOSTENT FAR*)ws32_result)
684 #define gethostname(n,l) \
685 (SOCKET_ERROR == gethostname(n,l) ? \
686 (errno = WSAGetLastError()), -1 : 0)
687 #define recv(s,b,l,f) \
688 (SOCKET_ERROR == (ws32_result = recv(_get_osfhandle(s),b,l,f)) ? \
689 (errno = WSAGetLastError()), -1 : ws32_result)
690 #define sendto(s,b,l,f,t,tl) \
691 (SOCKET_ERROR == (ws32_result = sendto(_get_osfhandle(s),b,l,f,t,tl)) ? \
692 (errno = WSAGetLastError()), -1 : ws32_result)
693 #define select(n,r,w,e,t) \
694 (SOCKET_ERROR == (ws32_result = select(n,r,w,e,t)) ? \
695 (errno = WSAGetLastError()), -1 : ws32_result)
696 #define socket(f,t,p) \
697 (INVALID_SOCKET == ((SOCKET)(ws32_result = (int)socket(f,t,p))) ? \
698 ((WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1), -1) : \
699 (SOCKET)_open_osfhandle(ws32_result,0))
700 #define write _write /* Needed in util.c */
701 #define open _open /* Needed in win32lib.c */
702 #endif /* #ifdef __cplusplus */
704 #define RUSAGE_SELF 0 /* calling process */
705 #define RUSAGE_CHILDREN -1 /* terminated child processes */
708 struct timeval ru_utime
; /* user time used */
709 struct timeval ru_stime
; /* system time used */
710 long ru_maxrss
; /* integral max resident set size */
711 long ru_ixrss
; /* integral shared text memory size */
712 long ru_idrss
; /* integral unshared data size */
713 long ru_isrss
; /* integral unshared stack size */
714 long ru_minflt
; /* page reclaims */
715 long ru_majflt
; /* page faults */
716 long ru_nswap
; /* swaps */
717 long ru_inblock
; /* block input operations */
718 long ru_oublock
; /* block output operations */
719 long ru_msgsnd
; /* messages sent */
720 long ru_msgrcv
; /* messages received */
721 long ru_nsignals
; /* signals received */
722 long ru_nvcsw
; /* voluntary context switches */
723 long ru_nivcsw
; /* involuntary context switches */
728 SQUIDCEXTERN
int chroot (const char *);
729 SQUIDCEXTERN
int ftruncate(int, off_t
);
730 SQUIDCEXTERN
int gettimeofday(struct timeval
* ,void *);
731 SQUIDCEXTERN
int kill(pid_t
, int);
732 SQUIDCEXTERN
int statfs(const char *, struct statfs
*);
733 SQUIDCEXTERN
int truncate(const char *, off_t
);
734 SQUIDCEXTERN
const char * wsastrerror(int);
735 SQUIDCEXTERN
struct passwd
*getpwnam(char *);
736 SQUIDCEXTERN
struct group
*getgrnam(char *);
737 SQUIDCEXTERN uid_t
geteuid(void);
738 SQUIDCEXTERN uid_t
getuid(void);
739 SQUIDCEXTERN
int setuid(uid_t
);
740 SQUIDCEXTERN
int seteuid(uid_t
);
741 SQUIDCEXTERN gid_t
getgid(void);
742 SQUIDCEXTERN gid_t
getegid(void);
743 SQUIDCEXTERN
int setgid(gid_t
);
744 SQUIDCEXTERN
int setegid(gid_t
);
745 SQUIDCEXTERN
const char *WIN32_strerror(int);
746 SQUIDCEXTERN
void WIN32_maperror(unsigned long);