Skip to content
Snippets Groups Projects
mongoose.c 167 KiB
Newer Older
Sergey Lyubka's avatar
Sergey Lyubka committed
// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
// Copyright (c) 2013-2014 Cesanta Software Limited
Sergey Lyubka's avatar
Sergey Lyubka committed
// All rights reserved
//
// This library is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation. For the terms of this
// license, see <http://www.gnu.org/licenses/>.
//
// You are free to use this library under the terms of the GNU General
// Public License, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// Alternatively, you can license this library under a commercial
// license, as set out in <http://cesanta.com/>.
#ifdef NOEMBED_NET_SKELETON
#include "net_skeleton.h"
#else
Sergey Lyubka's avatar
Sergey Lyubka committed
// net_skeleton start
Sergey Lyubka's avatar
Sergey Lyubka committed
// Copyright (c) 2014 Cesanta Software Limited
// All rights reserved
//
// This software is dual-licensed: you can redistribute it and/or modify
Sergey Lyubka's avatar
Sergey Lyubka committed
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation. For the terms of this
// license, see <http://www.gnu.org/licenses/>.
//
// You are free to use this software under the terms of the GNU General
Sergey Lyubka's avatar
Sergey Lyubka committed
// Public License, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// Alternatively, you can license this software under a commercial
Sergey Lyubka's avatar
Sergey Lyubka committed
// license, as set out in <http://cesanta.com/>.

#ifndef NS_SKELETON_HEADER_INCLUDED
#define NS_SKELETON_HEADER_INCLUDED

#define NS_SKELETON_VERSION "2.1.0"
Sergey Lyubka's avatar
Sergey Lyubka committed

#undef UNICODE                  // Use ANSI WinAPI functions
#undef _UNICODE                 // Use multibyte encoding on Windows
#define _MBCS                   // Use multibyte encoding on Windows
#define _INTEGRAL_MAX_BITS 64   // Enable _stati64() on Windows
Sergey Lyubka's avatar
Sergey Lyubka committed
#define _CRT_SECURE_NO_WARNINGS // Disable deprecation warning in VS2005+
Sergey Lyubka's avatar
Sergey Lyubka committed
#undef WIN32_LEAN_AND_MEAN      // Let windows.h always include winsock2.h
Miodrag Milanovic's avatar
Miodrag Milanovic committed
#ifdef __Linux__
#define _XOPEN_SOURCE 600       // For flockfile() on Linux
Sergey Lyubka's avatar
Sergey Lyubka committed
#define __STDC_FORMAT_MACROS    // <inttypes.h> wants this for C++
#define __STDC_LIMIT_MACROS     // C++ wants that for INT64_MAX
#ifndef _LARGEFILE_SOURCE
Sergey Lyubka's avatar
Sergey Lyubka committed
#define _LARGEFILE_SOURCE       // Enable fseeko() and ftello() functions
Sergey Lyubka's avatar
Sergey Lyubka committed
#define _FILE_OFFSET_BITS 64    // Enable 64-bit file offsets

#ifdef _MSC_VER
#pragma warning (disable : 4127)  // FD_SET() emits warning, disable it
#pragma warning (disable : 4204)  // missing c99 support
#endif

Sergey Lyubka's avatar
Sergey Lyubka committed
#if defined(_WIN32) && !defined(MONGOOSE_NO_CGI)
#define MONGOOSE_ENABLE_THREADS   /* Windows uses stdio threads for CGI */
#endif

#ifndef MONGOOSE_ENABLE_THREADS
#define NS_DISABLE_THREADS
#endif

#ifdef __OS2__
#define _MMAP_DECLARED          // Prevent dummy mmap() declaration in stdio.h
#endif

Sergey Lyubka's avatar
Sergey Lyubka committed
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
Sergey Lyubka's avatar
Sergey Lyubka committed
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Sergey Lyubka's avatar
Sergey Lyubka committed
#include <signal.h>
Sergey Lyubka's avatar
Sergey Lyubka committed

#ifdef _WIN32
Daniel O'Connell's avatar
Daniel O'Connell committed
#ifdef _MSC_VER
Sergey Lyubka's avatar
Sergey Lyubka committed
#pragma comment(lib, "ws2_32.lib")    // Linking with winsock library
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
Daniel O'Connell's avatar
Daniel O'Connell committed
#endif
#ifndef FD_SETSIZE
#define FD_SETSIZE 1024
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
Sergey Lyubka's avatar
Sergey Lyubka committed
#include <windows.h>
#include <process.h>
#ifndef EINPROGRESS
#define EINPROGRESS WSAEINPROGRESS
#endif
#ifndef EWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
#endif
#ifndef __func__
#define STRX(x) #x
#define STR(x) STRX(x)
#define __func__ __FILE__ ":" STR(__LINE__)
#endif
#ifndef va_copy
#define va_copy(x,y) x = y
#endif // MINGW #defines va_copy
#define snprintf _snprintf
#define vsnprintf _vsnprintf
Daniel O'Connell's avatar
Daniel O'Connell committed
#define sleep(x) Sleep((x) * 1000)
Sergey Lyubka's avatar
Sergey Lyubka committed
#define to64(x) _atoi64(x)
typedef int socklen_t;
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned __int64 uint64_t;
typedef __int64   int64_t;
typedef SOCKET sock_t;
typedef struct _stati64 ns_stat_t;
Sergey Lyubka's avatar
Sergey Lyubka committed
#ifndef S_ISDIR
#define S_ISDIR(x) ((x) & _S_IFDIR)
#endif
Sergey Lyubka's avatar
Sergey Lyubka committed
#else
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <pthread.h>
#include <stdarg.h>
#include <unistd.h>
#include <arpa/inet.h>  // For inet_pton() when NS_ENABLE_IPV6 is defined
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/select.h>
#define closesocket(x) close(x)
#ifndef __OS2__
Sergey Lyubka's avatar
Sergey Lyubka committed
#define __cdecl
#else
#include <sys/time.h>
typedef int socklen_t;
#endif
Sergey Lyubka's avatar
Sergey Lyubka committed
#define INVALID_SOCKET (-1)
#define to64(x) strtoll(x, NULL, 10)
typedef int sock_t;
typedef struct stat ns_stat_t;
Sergey Lyubka's avatar
Sergey Lyubka committed
#endif

#ifdef NS_ENABLE_DEBUG
#define DBG(x) do { printf("%-20s ", __func__); printf x; putchar('\n'); \
  fflush(stdout); } while(0)
#else
#define DBG(x)
#endif

Sergey Lyubka's avatar
Sergey Lyubka committed
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
Sergey Lyubka's avatar
Sergey Lyubka committed

#ifdef NS_ENABLE_SSL
#ifdef __APPLE__
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <openssl/ssl.h>
#else
typedef void *SSL;
typedef void *SSL_CTX;
#endif

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

union socket_address {
  struct sockaddr sa;
  struct sockaddr_in sin;
#ifdef NS_ENABLE_IPV6
  struct sockaddr_in6 sin6;
#else
  struct sockaddr sin6;
Sergey Lyubka's avatar
Sergey Lyubka committed
#endif
};

// Describes chunk of memory
struct ns_str {
  const char *p;
  size_t len;
};

Sergey Lyubka's avatar
Sergey Lyubka committed
// IO buffers interface
Sergey Lyubka's avatar
Sergey Lyubka committed
struct iobuf {
Loading
Loading full blame...