1/*
2 * IXNetSystem.h
3 * Author: Benjamin Sergeant
4 * Copyright (c) 2019 Machine Zone. All rights reserved.
5 */
6
7#pragma once
8
9#ifdef _WIN32
10
11#ifndef WIN32_LEAN_AND_MEAN
12#define WIN32_LEAN_AND_MEAN
13#endif
14
15#include <ws2tcpip.h>
16#include <winsock2.h>
17#include <basetsd.h>
18#include <io.h>
19#include <ws2def.h>
20#include <cerrno>
21
22#undef EWOULDBLOCK
23#undef EAGAIN
24#undef EINPROGRESS
25#undef EBADF
26#undef EINVAL
27
28// map to WSA error codes
29#define EWOULDBLOCK WSAEWOULDBLOCK
30#define EAGAIN WSATRY_AGAIN
31#define EINPROGRESS WSAEINPROGRESS
32#define EBADF WSAEBADF
33#define EINVAL WSAEINVAL
34
35// Define our own poll on Windows, as a wrapper on top of select
36typedef unsigned long int nfds_t;
37
38// pollfd is not defined by some versions of mingw64 since _WIN32_WINNT is too low
39#if _WIN32_WINNT < 0x0600
40struct pollfd
41{
42 int fd; /* file descriptor */
43 short events; /* requested events */
44 short revents; /* returned events */
45};
46
47#define POLLIN 0x001 /* There is data to read. */
48#define POLLOUT 0x004 /* Writing now will not block. */
49#define POLLERR 0x008 /* Error condition. */
50#define POLLHUP 0x010 /* Hung up. */
51#define POLLNVAL 0x020 /* Invalid polling request. */
52#endif
53
54#else
55#include <arpa/inet.h>
56#include <errno.h>
57#include <fcntl.h>
58#include <netdb.h>
59#include <netinet/in.h>
60#include <netinet/ip.h>
61#include <netinet/tcp.h>
62#include <poll.h>
63#include <sys/select.h>
64#include <sys/socket.h>
65#include <sys/stat.h>
66#include <sys/time.h>
67#include <unistd.h>
68#endif
69
70namespace ix
71{
72#ifdef _WIN32
73 typedef SOCKET socket_t;
74#else
75 typedef int socket_t;
76#endif
77
78 bool initNetSystem();
79 bool uninitNetSystem();
80
81 int poll(struct pollfd* fds, nfds_t nfds, int timeout, void** event);
82
83 const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
84 int inet_pton(int af, const char* src, void* dst);
85
86 unsigned short network_to_host_short(unsigned short value);
87} // namespace ix
88