2020-04-20 03:46:05 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* */
|
|
|
|
/* Host.h - Basic header file to provide cross-platform solutions via */
|
|
|
|
/* macros, conditional compilation, etc. */
|
|
|
|
/* */
|
|
|
|
/* Author : Mark Carrier (mark@carrierlabs.com) */
|
|
|
|
/* */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Copyright (c) 2007 CarrierLabs, LLC. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* 4. The name "CarrierLabs" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* mark@carrierlabs.com.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK CARRIER OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*----------------------------------------------------------------------------*/
|
2020-04-27 10:06:51 +02:00
|
|
|
#pragma once
|
2020-04-20 03:46:05 +02:00
|
|
|
|
2020-04-27 09:25:29 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <assert.h>
|
2020-04-20 03:46:05 +02:00
|
|
|
#include <limits.h>
|
|
|
|
|
2020-04-27 09:37:39 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* */
|
|
|
|
/* OS Identification */
|
|
|
|
/* */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(__WIN64__) || defined(_WIN) || defined(__WIN__)
|
|
|
|
#ifndef _WIN32
|
|
|
|
#define _WIN32 // why do I even bother?
|
|
|
|
#endif
|
|
|
|
#elif defined(linux) || defined(__linux) || defined(__linux__) || defined(__unix) || defined(__unix__)
|
|
|
|
#define _LINUX
|
|
|
|
#endif
|
|
|
|
|
2020-04-20 03:46:05 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* */
|
|
|
|
/* Type Definition Macros */
|
|
|
|
/* */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#ifndef __WORDSIZE
|
|
|
|
/* Assume 32 */
|
|
|
|
#define __WORDSIZE 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_LINUX) || defined(_DARWIN)
|
2020-04-27 09:56:22 +02:00
|
|
|
typedef int SOCKET;
|
2020-04-20 03:46:05 +02:00
|
|
|
#endif
|
|
|
|
|
2020-04-27 09:17:08 +02:00
|
|
|
#ifdef _WIN32
|
2020-04-20 03:46:05 +02:00
|
|
|
struct iovec {
|
|
|
|
void *iov_base;
|
|
|
|
size_t iov_len;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2020-04-27 09:17:08 +02:00
|
|
|
#ifdef _WIN32
|
2020-04-20 03:46:05 +02:00
|
|
|
typedef int socklen_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef htonll
|
|
|
|
#ifdef _BIG_ENDIAN
|
|
|
|
#define htonll(x) (x)
|
|
|
|
#define ntohll(x) (x)
|
|
|
|
#else
|
2020-04-27 09:25:29 +02:00
|
|
|
#define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
|
|
|
|
#define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
|
2020-04-20 03:46:05 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* */
|
|
|
|
/* Socket Macros */
|
|
|
|
/* */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2020-04-27 09:17:08 +02:00
|
|
|
#ifdef _WIN32
|
2020-04-20 03:46:05 +02:00
|
|
|
#define SHUT_RD 0
|
|
|
|
#define SHUT_WR 1
|
|
|
|
#define SHUT_RDWR 2
|
|
|
|
#define ACCEPT(a,b,c) accept(a,b,c)
|
|
|
|
#define CONNECT(a,b,c) connect(a,b,c)
|
|
|
|
#define CLOSE(a) closesocket(a)
|
|
|
|
#define READ(a,b,c) read(a,b,c)
|
|
|
|
#define RECV(a,b,c,d) recv(a, (char *)b, c, d)
|
|
|
|
#define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, (int *)f)
|
|
|
|
#define RECV_FLAGS MSG_WAITALL
|
2020-04-27 09:25:29 +02:00
|
|
|
#define SELECT(a,b,c,d,e) select((int32_t)a,b,c,d,e)
|
2020-04-20 03:46:05 +02:00
|
|
|
#define SEND(a,b,c,d) send(a, (const char *)b, (int)c, d)
|
|
|
|
#define SENDTO(a,b,c,d,e,f) sendto(a, (const char *)b, (int)c, d, e, f)
|
|
|
|
#define SEND_FLAGS 0
|
|
|
|
#define SENDFILE(a,b,c,d) sendfile(a, b, c, d)
|
|
|
|
#define SET_SOCKET_ERROR(x,y) errno=y
|
|
|
|
#define SOCKET_ERROR_INTERUPT EINTR
|
|
|
|
#define SOCKET_ERROR_TIMEDOUT EAGAIN
|
|
|
|
#define WRITE(a,b,c) write(a,b,c)
|
|
|
|
#define WRITEV(a,b,c) Writev(b, c)
|
|
|
|
#define GETSOCKOPT(a,b,c,d,e) getsockopt(a,b,c,(char *)d, (int *)e)
|
|
|
|
#define SETSOCKOPT(a,b,c,d,e) setsockopt(a,b,c,(char *)d, (int)e)
|
|
|
|
#define GETHOSTBYNAME(a) gethostbyname(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_LINUX) || defined(_DARWIN)
|
|
|
|
#define ACCEPT(a,b,c) accept(a,b,c)
|
|
|
|
#define CONNECT(a,b,c) connect(a,b,c)
|
|
|
|
#define CLOSE(a) close(a)
|
|
|
|
#define READ(a,b,c) read(a,b,c)
|
|
|
|
#define RECV(a,b,c,d) recv(a, (void *)b, c, d)
|
|
|
|
#define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, f)
|
|
|
|
#define RECV_FLAGS MSG_WAITALL
|
|
|
|
#define SELECT(a,b,c,d,e) select(a,b,c,d,e)
|
2020-04-27 09:25:29 +02:00
|
|
|
#define SEND(a,b,c,d) send(a, (const int8_t *)b, c, d)
|
|
|
|
#define SENDTO(a,b,c,d,e,f) sendto(a, (const int8_t *)b, c, d, e, f)
|
2020-04-20 03:46:05 +02:00
|
|
|
#define SEND_FLAGS 0
|
|
|
|
#define SENDFILE(a,b,c,d) sendfile(a, b, c, d)
|
|
|
|
#define SET_SOCKET_ERROR(x,y) errno=y
|
|
|
|
#define SOCKET_ERROR_INTERUPT EINTR
|
|
|
|
#define SOCKET_ERROR_TIMEDOUT EAGAIN
|
|
|
|
#define WRITE(a,b,c) write(a,b,c)
|
|
|
|
#define WRITEV(a,b,c) writev(a, b, c)
|
|
|
|
#define GETSOCKOPT(a,b,c,d,e) getsockopt((int)a,(int)b,(int)c,(void *)d,(socklen_t *)e)
|
|
|
|
#define SETSOCKOPT(a,b,c,d,e) setsockopt((int)a,(int)b,(int)c,(const void *)d,(int)e)
|
|
|
|
#define GETHOSTBYNAME(a) gethostbyname(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* */
|
|
|
|
/* File Macros */
|
|
|
|
/* */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#define STRUCT_STAT struct stat
|
|
|
|
#define LSTAT(x,y) lstat(x,y)
|
|
|
|
#define FILE_HANDLE FILE *
|
|
|
|
#define CLEARERR(x) clearerr(x)
|
|
|
|
#define FCLOSE(x) fclose(x)
|
|
|
|
#define FEOF(x) feof(x)
|
|
|
|
#define FERROR(x) ferror(x)
|
|
|
|
#define FFLUSH(x) fflush(x)
|
|
|
|
#define FILENO(s) fileno(s)
|
|
|
|
#define FOPEN(x,y) fopen(x, y)
|
|
|
|
//#define FREAD(a,b,c,d) fread(a, b, c, d)
|
|
|
|
#define FSTAT(s, st) fstat(FILENO(s), st)
|
|
|
|
//#define FWRITE(a,b,c,d) fwrite(a, b, c, d)
|
|
|
|
#define STAT_BLK_SIZE(x) ((x).st_blksize)
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* */
|
|
|
|
/* Misc Macros */
|
|
|
|
/* */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2020-04-27 09:17:08 +02:00
|
|
|
#if defined(_WIN32)
|
2020-04-20 03:46:05 +02:00
|
|
|
#define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x)
|
|
|
|
#else
|
|
|
|
#define GET_CLOCK_COUNT(x) gettimeofday(x, NULL)
|
|
|
|
#endif
|
|
|
|
|
2020-04-27 09:17:08 +02:00
|
|
|
#if defined(_WIN32)
|
2020-04-20 03:46:05 +02:00
|
|
|
#define STRTOULL(x) _atoi64(x)
|
|
|
|
#else
|
|
|
|
#define STRTOULL(x) strtoull(x, NULL, 10)
|
|
|
|
#endif
|
|
|
|
|
2020-04-27 09:17:08 +02:00
|
|
|
#if defined(_WIN32)
|
2020-04-20 03:46:05 +02:00
|
|
|
#define SNPRINTF _snprintf
|
|
|
|
#define PRINTF printf
|
|
|
|
#define VPRINTF vprintf
|
|
|
|
#define FPRINTF fprintf
|
|
|
|
#else
|
|
|
|
#define SNPRINTF snprintf
|
|
|
|
#define PRINTF printf
|
|
|
|
#define VPRINTF vprintf
|
|
|
|
#define FPRINTF fprintf
|
|
|
|
#endif
|