-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocketLibSystem.cpp
More file actions
84 lines (73 loc) · 1.36 KB
/
SocketLibSystem.cpp
File metadata and controls
84 lines (73 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "SocketLibSystem.h"
#include "SocketLibErrors.h"
#pragma warning(disable:4996)
namespace SocketLib
{
#ifdef WIN32
class System
{
protected:
WSADATA m_WSAData;
public:
System()
{
WSAStartup(MAKEWORD(2, 2), &m_WSAData);
}
~System()
{
WSACleanup();
}
};
System g_system;
#endif // WIN32
bool IsIPAddress(const std::string p_address)
{
for (size_t i = 0; i < p_address.length(); ++i)
{
if ((p_address[i] < '0' || p_address[i] > '9') && p_address[i] != '.')
{
return false;
}
}
return true;
}
ipaddress GetIPAddress(const std::string p_address)
{
if (IsIPAddress(p_address))
{
ipaddress addr = inet_addr(p_address.c_str());
if (addr == INADDR_NONE)
{
throw Exception(EDNSNotFound);
}
return addr;
}
else
{
hostent* host = gethostbyname(p_address.c_str());
if (host == 0)
{
throw Exception(GetError(false));
}
return *((ipaddress*)host->h_addr);
}
}
std::string GetIPString(ipaddress p_address)
{
char* str = inet_ntoa(*((in_addr*)&p_address));
if (str == 0)
{
return std::string("Invalid IP Address");
}
return std::string(str);
}
std::string GetHostNameString(ipaddress p_address)
{
hostent* host = gethostbyaddr((char*)&p_address, 4, AF_INET);
if (host == 0)
{
throw Exception(GetError(false));
}
return std::string(host->h_name);
}
}