14 #ifndef SQUID_COMPAT_OS_MSWINDOWS_H
15 #define SQUID_COMPAT_OS_MSWINDOWS_H
40 #define ACL WindowsACL
43 #define _CRT_SECURE_NO_DEPRECATE
44 #pragma warning( disable : 4290 )
45 #pragma warning( disable : 4996 )
49 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
50 # define __USE_FILE_OFFSET64 1
55 #if defined(__USE_FILE_OFFSET64)
56 typedef uint64_t ino_t;
58 typedef unsigned long ino_t;
61 #define INT64_MAX _I64_MAX
62 #define INT64_MIN _I64_MIN
64 #include "default_config_file.h"
67 #define THREADLOCAL __declspec(thread)
69 #elif defined(__GNUC__)
71 #define THREADLOCAL __attribute__((section(".tls")))
77 #define alloca _alloca
78 #define fileno _fileno
79 #define fstat _fstati64
80 #define lseek _lseeki64
81 #define memccpy _memccpy
82 #define mktemp _mktemp
83 #define snprintf _snprintf
85 #define strcasecmp _stricmp
86 #define strlwr _strlwr
87 #define strncasecmp _strnicmp
88 #define tempnam _tempnam
89 #define vsnprintf _vsnprintf
93 #if defined(_MSC_VER) || _SQUID_CYGWIN_
95 #define ftruncate WIN32_ftruncate
96 SQUIDCEXTERN int WIN32_truncate(
const char *pathname, off_t length);
97 #define truncate WIN32_truncate
104 #define fdopen _fdopen
105 #define getcwd _getcwd
106 #define getpid _getpid
107 #define mkdir(p,F) mkdir((p))
108 #define pclose _pclose
110 #define putenv _putenv
111 #define setmode _setmode
112 #define sleep(t) Sleep((t)*1000)
114 #define unlink _unlink
117 #define O_RDONLY _O_RDONLY
120 #define O_WRONLY _O_WRONLY
123 #define O_RDWR _O_RDWR
126 #define O_APPEND _O_APPEND
129 #define O_CREAT _O_CREAT
132 #define O_TRUNC _O_TRUNC
135 #define O_EXCL _O_EXCL
138 #define O_TEXT _O_TEXT
141 #define O_BINARY _O_BINARY
144 #define O_RAW _O_BINARY
147 #define O_TEMPORARY _O_TEMPORARY
150 #define O_NOINHERIT _O_NOINHERIT
153 #define O_SEQUENTIAL _O_SEQUENTIAL
156 #define O_RANDOM _O_RANDOM
163 #define S_IFMT _S_IFMT
166 #define S_IFDIR _S_IFDIR
169 #define S_IFCHR _S_IFCHR
172 #define S_IFREG _S_IFREG
175 #define S_IREAD _S_IREAD
178 #define S_IWRITE _S_IWRITE
181 #define S_IEXEC _S_IEXEC
205 #if defined(_MSC_VER)
206 #define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
217 #if defined(_MSC_VER)
239 #if !HAVE_GETTIMEOFDAY
246 #define CHANGE_FD_SETSIZE 1
247 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
248 #define FD_SETSIZE SQUID_MAXFD
254 #include <winsock2.h>
259 #include <ws2tcpip.h>
262 #if (EAI_NODATA == EAI_NONAME)
264 #define EAI_NODATA WSANO_DATA
267 #if defined(_MSC_VER)
269 #pragma warning (push)
270 #pragma warning (disable:4142)
277 #if defined(_MSC_VER)
278 #pragma warning (pop)
283 #ifndef _PATH_DEVNULL
284 #define _PATH_DEVNULL "NUL"
288 #define EISCONN WSAEISCONN
291 #define EINPROGRESS WSAEINPROGRESS
294 #define EWOULDBLOCK WSAEWOULDBLOCK
297 #define EALREADY WSAEALREADY
300 #define ETIMEDOUT WSAETIMEDOUT
303 #define ECONNREFUSED WSAECONNREFUSED
306 #define ECONNRESET WSAECONNRESET
309 #define ENOTCONN WSAENOTCONN
312 #define ERESTART WSATRY_AGAIN
315 #define EAFNOSUPPORT WSAEAFNOSUPPORT
318 #define ENETUNREACH WSAENETUNREACH
321 #define ENOTSUP WSAEOPNOTSUPP
324 #define ECONNABORTED WSAECONNABORTED
328 #define h_errno errno
331 #define FD_CLR(fd, set) do { \
333 SOCKET __sock = _get_osfhandle(fd); \
334 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count ; __i++) { \
335 if (((fd_set FAR *)(set))->fd_array[__i] == __sock) { \
336 while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
337 ((fd_set FAR *)(set))->fd_array[__i] = \
338 ((fd_set FAR *)(set))->fd_array[__i+1]; \
341 ((fd_set FAR *)(set))->fd_count--; \
348 #define FD_SET(fd, set) do { \
350 SOCKET __sock = _get_osfhandle(fd); \
351 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
352 if (((fd_set FAR *)(set))->fd_array[__i] == (__sock)) { \
356 if (__i == ((fd_set FAR *)(set))->fd_count) { \
357 if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
358 ((fd_set FAR *)(set))->fd_array[__i] = (__sock); \
359 ((fd_set FAR *)(set))->fd_count++; \
365 #define FD_ISSET(fd, set) Win32__WSAFDIsSet(fd, (fd_set FAR *)(set))
374 CRITICAL_SECTION lock;
378 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
379 #define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)) )
380 #define _osfile(i) ( _pioinfo(i)->osfile )
381 #define _osfhnd(i) ( _pioinfo(i)->osfhnd )
386 #if defined(_MSC_VER)
393 #if defined(__cplusplus)
398 char l_so_type[
sizeof(
int)];
399 int l_so_type_siz =
sizeof(l_so_type);
400 SOCKET sock = _get_osfhandle(fd);
402 if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0) {
404 if (closesocket(sock) == SOCKET_ERROR) {
405 errno = WSAGetLastError();
415 #if defined(_MSC_VER)
418 #define _S_IREAD 0x0100
422 #define _S_IWRITE 0x0080
426 open(
const char *filename,
int oflag,
int pmode = 0)
428 return _open(filename, oflag, pmode & (_S_IREAD | _S_IWRITE));
433 read(
int fd,
void * buf,
size_t siz)
435 char l_so_type[
sizeof(
int)];
436 int l_so_type_siz =
sizeof(l_so_type);
437 SOCKET sock = _get_osfhandle(fd);
439 if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
440 return ::recv(sock, (
char FAR *) buf, (
int)siz, 0);
442 return _read(fd, buf, (
unsigned int)siz);
446 write(
int fd,
const void * buf,
size_t siz)
448 char l_so_type[
sizeof(
int)];
449 int l_so_type_siz =
sizeof(l_so_type);
450 SOCKET sock = _get_osfhandle(fd);
452 if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
453 return ::send(sock, (
char FAR *) buf, siz, 0);
455 return _write(fd, buf, siz);
459 #include <functional>
479 accept(
int s,
struct sockaddr * a,
socklen_t * l)
482 if ((result = ::accept(_get_osfhandle(s), a, l)) == INVALID_SOCKET) {
483 if (WSAEMFILE == (errno = WSAGetLastError()))
487 return _open_osfhandle(result, 0);
489 #define accept(s,a,l) Squid::accept(s,a,reinterpret_cast<socklen_t*>(l))
492 bind(
int s,
const struct sockaddr * n,
socklen_t l)
494 if (::bind(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
495 errno = WSAGetLastError();
500 #define bind(s,n,l) Squid::bind(s,n,l)
503 connect(
int s,
const struct sockaddr * n,
socklen_t l)
505 if (::connect(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
506 if (WSAEMFILE == (errno = WSAGetLastError()))
512 #define connect(s,n,l) Squid::connect(s,n,l)
514 inline struct hostent *
515 gethostbyname(
const char *n) {
516 HOSTENT FAR * result;
517 if ((result = ::gethostbyname(n)) ==
NULL)
518 errno = WSAGetLastError();
521 #define gethostbyname(n) Squid::gethostbyname(n)
524 getservbyname(
const char * n,
const char * p)
526 SERVENT FAR * result;
527 if ((result = ::getservbyname(n, p)) ==
NULL)
528 errno = WSAGetLastError();
531 #define getservbyname(n,p) Squid::getservbyname(n,p)
534 gethostbyaddr(
const void * a,
size_t l,
int t)
536 HOSTENT FAR * result;
537 if ((result = ::gethostbyaddr((
const char*)a, l, t)) ==
NULL)
538 errno = WSAGetLastError();
541 #define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
544 getsockname(
int s,
struct sockaddr * n,
socklen_t * l)
547 if (::getsockname(_get_osfhandle(s), n, &i) == SOCKET_ERROR) {
548 errno = WSAGetLastError();
553 #define getsockname(s,a,l) Squid::getsockname(s,a,reinterpret_cast<socklen_t*>(l))
556 gethostname(
char * n,
size_t l)
558 if ((::gethostname(n, l)) == SOCKET_ERROR) {
559 errno = WSAGetLastError();
564 #define gethostname(n,l) Squid::gethostname(n,l)
567 getsockopt(
int s,
int l,
int o,
void * v,
socklen_t * n)
570 if ((::getsockopt(_get_osfhandle(s), l, o,(
char *) v, n)) == SOCKET_ERROR) {
571 errno = WSAGetLastError();
576 #define getsockopt(s,l,o,v,n) Squid::getsockopt(s,l,o,v,n)
580 ioctl(
int s,
int c,
void * a)
582 if ((::ioctlsocket(_get_osfhandle(s), c, (u_long FAR *)a)) == SOCKET_ERROR) {
583 errno = WSAGetLastError();
588 #define ioctl(s,c,a) Squid::ioctl(s,c,a)
591 ioctlsocket(
int s,
long c, u_long FAR * a)
593 if ((::ioctlsocket(_get_osfhandle(s), c, a)) == SOCKET_ERROR) {
594 errno = WSAGetLastError();
599 #define ioctlsocket(s,c,a) Squid::ioctlsocket(s,c,a)
604 if (::listen(_get_osfhandle(s), b) == SOCKET_ERROR) {
605 if (WSAEMFILE == (errno = WSAGetLastError()))
611 #define listen(s,b) Squid::listen(s,b)
614 recv(
int s,
void * b,
size_t l,
int f)
617 if ((result = ::recv(_get_osfhandle(s), (
char *)b, l, f)) == SOCKET_ERROR) {
618 errno = WSAGetLastError();
623 #define recv(s,b,l,f) Squid::recv(s,b,l,f)
626 recvfrom(
int s,
void * b,
size_t l,
int f,
struct sockaddr * fr,
socklen_t * fl)
630 if ((result = ::recvfrom(_get_osfhandle(s), (
char *)b, l, f, fr, &ifl)) == SOCKET_ERROR) {
631 errno = WSAGetLastError();
636 #define recvfrom(s,b,l,f,r,n) Squid::recvfrom(s,b,l,f,r,reinterpret_cast<socklen_t*>(n))
639 select(
int n, fd_set * r, fd_set * w, fd_set * e,
struct timeval * t)
642 if ((result = ::select(n,r,w,e,t)) == SOCKET_ERROR) {
643 errno = WSAGetLastError();
648 #define select(n,r,w,e,t) Squid::select(n,r,w,e,t)
651 send(
int s,
const char * b,
size_t l,
int f)
654 if ((result = ::send(_get_osfhandle(s), b, l, f)) == SOCKET_ERROR) {
655 errno = WSAGetLastError();
660 #define send(s,b,l,f) Squid::send(s,reinterpret_cast<const char*>(b),l,f)
663 sendto(
int s,
const void * b,
size_t l,
int f,
const struct sockaddr * t,
socklen_t tl)
666 if ((result = ::sendto(_get_osfhandle(s), (
char *)b, l, f, t, tl)) == SOCKET_ERROR) {
667 errno = WSAGetLastError();
672 #define sendto(a,b,l,f,t,n) Squid::sendto(a,b,l,f,t,n)
675 setsockopt(SOCKET s,
int l,
int o,
const void * v,
socklen_t n)
679 socket = ((s == INVALID_SOCKET) ? s : (SOCKET)_get_osfhandle((
int)s));
681 if (::setsockopt(socket, l, o, (
const char *)v, n) == SOCKET_ERROR) {
682 errno = WSAGetLastError();
687 #define setsockopt(s,l,o,v,n) Squid::setsockopt(s,l,o,v,n)
690 shutdown(
int s,
int h)
692 if (::shutdown(_get_osfhandle(s),h) == SOCKET_ERROR) {
693 errno = WSAGetLastError();
698 #define shutdown(s,h) Squid::shutdown(s,h)
701 socket(
int f,
int t,
int p)
704 if ((result = ::socket(f, t, p)) == INVALID_SOCKET) {
705 if (WSAEMFILE == (errno = WSAGetLastError()))
709 return _open_osfhandle(result, 0);
711 #define socket(f,t,p) Squid::socket(f,t,p)
716 return _pipe(pipefd,4096,_O_BINARY);
718 #define pipe(a) Squid::pipe(a)
721 WSAAsyncSelect(
int s, HWND h,
unsigned int w,
long e)
723 if (::WSAAsyncSelect(_get_osfhandle(s), h, w, e) == SOCKET_ERROR) {
724 errno = WSAGetLastError();
729 #define WSAAsyncSelect(s,h,w,e) Squid::WSAAsyncSelect(s,h,w,e)
731 #undef WSADuplicateSocket
733 WSADuplicateSocket(
int s, DWORD n, LPWSAPROTOCOL_INFO l)
736 if (::WSADuplicateSocketW(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
738 if (::WSADuplicateSocketA(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
740 errno = WSAGetLastError();
745 #define WSADuplicateSocket(s,n,l) Squid::WSADuplicateSocket(s,n,l)
749 WSASocket(
int a,
int t,
int p, LPWSAPROTOCOL_INFO i, GROUP g, DWORD f)
753 if ((result = ::WSASocketW(a, t, p, i, g, f)) == INVALID_SOCKET) {
755 if ((result = ::WSASocketA(a, t, p, i, g, f)) == INVALID_SOCKET) {
757 if (WSAEMFILE == (errno = WSAGetLastError()))
761 return _open_osfhandle(result, 0);
763 #define WSASocket(a,t,p,i,g,f) Squid::WSASocket(a,t,p,i,g,f)
768 #define connect(s,n,l) \
769 (SOCKET_ERROR == connect(_get_osfhandle(s),n,l) ? \
770 (WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1, -1) : 0)
771 #define gethostbyname(n) \
772 (NULL == ((HOSTENT FAR*)(ws32_result = (int)gethostbyname(n))) ? \
773 (errno = WSAGetLastError()), (HOSTENT FAR*)NULL : (HOSTENT FAR*)ws32_result)
774 #define gethostname(n,l) \
775 (SOCKET_ERROR == gethostname(n,l) ? \
776 (errno = WSAGetLastError()), -1 : 0)
777 #define recv(s,b,l,f) \
778 (SOCKET_ERROR == (ws32_result = recv(_get_osfhandle(s),b,l,f)) ? \
779 (errno = WSAGetLastError()), -1 : ws32_result)
780 #define sendto(s,b,l,f,t,tl) \
781 (SOCKET_ERROR == (ws32_result = sendto(_get_osfhandle(s),b,l,f,t,tl)) ? \
782 (errno = WSAGetLastError()), -1 : ws32_result)
783 #define select(n,r,w,e,t) \
784 (SOCKET_ERROR == (ws32_result = select(n,r,w,e,t)) ? \
785 (errno = WSAGetLastError()), -1 : ws32_result)
786 #define socket(f,t,p) \
787 (INVALID_SOCKET == ((SOCKET)(ws32_result = (int)socket(f,t,p))) ? \
788 ((WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1), -1) : \
789 (SOCKET)_open_osfhandle(ws32_result,0))
797 #include <sys/time.h>
799 #if HAVE_SYS_RESOURCE_H
800 #include <sys/resource.h>
802 #define RUSAGE_SELF 0
803 #define RUSAGE_CHILDREN -1
806 struct timeval ru_utime;
807 struct timeval ru_stime;
873 #if !HAVE_GETPAGESIZE
875 size_t getpagesize();
876 #define HAVE_GETPAGESIZE 2
880 SQUIDCEXTERN int Win32__WSAFDIsSet(
int fd, fd_set* set);
890 #define ACL WindowsACL
891 #define _MSWIN_ACL_WAS_NOT_DEFINED 1
894 #if _MSWIN_ACL_WAS_NOT_DEFINED
896 #undef _MSWIN_ACL_WAS_NOT_DEFINED
906 #define LOG_WARNING 4
910 #define LOG_DAEMON (3<<3)
912 void openlog(
const char *ident,
int logopt,
int facility);
913 void syslog(
int priority,
const char *fmt, ...);
917 void WIN32_maperror(
unsigned long WIN32_oserrno);