001:
002:
003:
004:
005:
006:
007:
008:
009:
010:
011:
012:
013:
014:
015:
016:
017:
018:
019:
020:
021:
022:
023:
024:
025:
026:
027:
028:
029:
030:
031:
032:
033:
034:
035:
036:
037:
038:
039:
040:
041:
042:
043:
044:
045:
046:
047:
048:
049:
050:
051:
052:
053:
054:
055:
056:
057:
058:
059:
060:
061:
062:
063:
064:
065:
066:
067:
068:
069:
070:
071:
072:
073:
074:
075:
076:
077:
078:
079:
080:
081:
082:
083:
084:
085:
086:
087:
088:
089:
090:
091:
092:
093:
094:
095:
096:
097:
098:
099:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:

/*
* Copyright (c), Zeriph Enterprises
* All rights reserved.
*
* Contributor(s):
* Zechariah Perez, omni (at) zeriph (dot) com
*
* THIS SOFTWARE IS PROVIDED BY ZERIPH AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS 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 ZERIPH AND 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.
*/
#if !defined(OMNI_NET_DEF_HPP)
#define OMNI_NET_DEF_HPP 1
#include <omni/defs/global.hpp>
#include <omni/strings.hpp>
#include <sys/types.h>
#include <sys/stat.h>
#include <csignal>
#if defined(OMNI_OS_WIN)
#include <io.h>
#include <ws2tcpip.h>
#if !defined(OMNI_WIN_NO_PRAGMA_WS2_32_LIB)
#pragma comment(lib, "ws2_32.lib")
#endif
#else
#if !defined(_XOPEN_SOURCE_EXTENDED)
#define _XOPEN_SOURCE_EXTENDED 1
#endif
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <fcntl.h>
#endif
#define OMNI_SOCKADDR_IN_T_FW struct sockaddr_in
#define OMNI_SOCKADDR_IN6_T_FW struct sockaddr_in6
#define OMNI_SOCKADDR_UN_T_FW struct sockaddr_un
#define OMNI_SOCKADDR_T_FW struct sockaddr
#if !defined(OMNI_SOMAXCONN)
#if defined(SOMAXCONN)
#define OMNI_SOMAXCONN SOMAXCONN
#else
#define OMNI_SOMAXCONN 128
#endif
#endif
#if !defined(OMNI_SOCKET_DEFAULT_GET_HOST_PORT)
#define OMNI_SOCKET_DEFAULT_GET_HOST_PORT 80
#endif
#if !defined(OMNI_SOCKET_DEFAULT_BACKLOG)
#define OMNI_SOCKET_DEFAULT_BACKLOG OMNI_SOMAXCONN
#endif
#if defined(OMNI_WIN_API)
#define OMNI_SOCKET_OPEN_FW(af, t, p) ::WSASocket(af, static_cast<int>(t), static_cast<int>(p), NULL, 0, WSA_FLAG_OVERLAPPED)
#define OMNI_SOCKET_ACCEPT_FW(s, a, l) ::WSAAccept(s, a, l, NULL, NULL)
#define OMNI_SOCKET_CONNECT_FW(s, sa, l) ::WSAConnect(s, sa, l, NULL, NULL, NULL, NULL)
#define OMNI_SOCKET_CLOSE_FW(s) ::closesocket(s)
#else
#define OMNI_SOCKET_OPEN_FW(af, t, p) ::socket(af, static_cast<int>(t), static_cast<int>(p))
#define OMNI_SOCKET_ACCEPT_FW(s, a, l) ::accept(s, a, l)
#define OMNI_SOCKET_CONNECT_FW(s, sa, l) ::connect(s, sa, l)
#define OMNI_SOCKET_CLOSE_FW(s) ::close(s)
#endif
#if defined(OMNI_OS_WIN)
#define OMNI_SOCKET_T_FW SOCKET
#define OMNI_SOCKLEN_FW int
#define OMNI_SOCKET_XFR_T_FW char
#define OMNI_INVALID_SOCKET INVALID_SOCKET
#define OMNI_SOCK_SYSERR_FW SOCKET_ERROR
#define OMNI_SOCKET_ERR_FW ::WSAGetLastError()
#if !defined(OMNI_WINSOCK_HIGH)
#define OMNI_WINSOCK_HIGH 2
#endif
#if !defined(OMNI_WINSOCK_LOW)
#define OMNI_WINSOCK_LOW 2
#endif
#define OMNI_SOCK_RECEIVE_FW SD_RECEIVE
#define OMNI_SOCK_SEND_FW SD_SEND
#define OMNI_SOCK_BOTH_FW SD_BOTH
#define OMNI_MSG_EOR 0
#else
#define OMNI_SOCKET_T_FW int
#define OMNI_SOCKLEN_FW socklen_t
#define OMNI_SOCKET_XFR_T_FW void
#define OMNI_INVALID_SOCKET -1
#define OMNI_SOCK_SYSERR_FW -1
#define OMNI_SOCKET_ERR_FW errno
#define OMNI_SOCK_RECEIVE_FW SHUT_RD
#define OMNI_SOCK_SEND_FW SHUT_WR
#define OMNI_SOCK_BOTH_FW SHUT_RDWR
#define OMNI_MSG_EOR MSG_EOR
#endif
#if defined(OMNI_OS_WIN)
namespace omni {
namespace net {
inline int wsa_init()
{
WSADATA sdata;
int err = ::WSAStartup(MAKEWORD(OMNI_WINSOCK_HIGH, OMNI_WINSOCK_LOW), &sdata);
if (err != 0) {
// Could not get the winsock dll, fail since can't create socket
OMNI_DBGEV("a system error occurred in WSAStartUp: ", err)
}
return err;
}
inline void wsa_close()
{
::WSACleanup();
}
class wsa_info {
public:
wsa_info() : m_err(-1)
{ this->open(); }
explicit wsa_info(bool val) : m_err(-1)
{ OMNI_UNUSED(val); }
~wsa_info()
{ if (this->m_err == 0) { omni::net::wsa_close(); } }
void close()
{ omni::net::wsa_close(); this->m_err = -1; }
int error() const
{ return this->m_err; }
bool open() { if (this->m_err != 0)
{ this->m_err = omni::net::wsa_init(); } return (this->m_err == 0); }
operator bool() const
{ return this->m_err == 0; }
void swap(wsa_info& other)
{ if (this != &other) { std::swap(this->m_err, other.m_err); } }
private:
int m_err;
};
}
}
#endif
#endif // OMNI_NET_DEF_HPP