-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddress4.cpp
233 lines (223 loc) · 6.27 KB
/
Address4.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "Address4.h"
#include "IP4.h"
#include "WSA.h"
namespace Tool
{
namespace Network
{
Address4::Address4(sockaddr_in & address)
{
this->address = address;
}
Address4::Address4(std::string & address)
{
this->address = getAddressFromString((const char*)(address.c_str()));
}
Address4::Address4(std::wstring & address)
{
this->address = getAddressFromString((const wchar_t*)(address.c_str()));
}
Address4::Address4(const char* address)
{
this->address = getAddressFromString((const char*)address);
}
Address4::Address4(const wchar_t* address)
{
this->address = getAddressFromString((const wchar_t*)address);
}
Address4::Address4(IP4 address)
{
this->address.sin_family = AF_INET;
this->address.sin_addr.S_un.S_addr = address.Tow32();
}
Address4::Address4(std::string & iP, uint16_t port)
{
WSA was;
this->address = getAddressFromString((const char*)(iP.c_str()));
this->address.sin_port = htons(port);
}
Address4::Address4(std::wstring & iP, uint16_t port)
{
WSA was;
this->address = getAddressFromString((const wchar_t*)(iP.c_str()));
this->address.sin_port = htons(port);
}
Address4::Address4(const char* iP, uint16_t port)
{
WSA was;
this->address = getAddressFromString((const char*)iP);
this->address.sin_port = htons(port);
}
Address4::Address4(const char* iP, uint16_t port)
{
WSA was;
this->address = getAddressFromString((const char*)iP);
this->address.sin_port = htons(port);
}
Address4::Address4(IP4 address, uint16_t port)
{
WSA was;
this->address.sin_family = AF_INET;
this->address.sin_addr.S_un.S_addr = address.Tow32();
}
sockaddr_in Address4::getAddressFromString(const char* address)
{
WSA was;
sockaddr_in addr;
int addrlen = sizeof(addr);
if(WSAStringToAddressA((LPSTR)address, AF_INET, 0,(sockaddr*) &addr, &addrlen)!= 0)
throw WSAGetLastError();
return addr;
}
sockaddr_in Address4::getAddressFromString(const wchar_t* address)
{
WSA was;
sockaddr_in addr;
int addrlen = sizeof(addr);
if(WSAStringToAddressW((LPWSTR)address, AF_INET, 0,(sockaddr*) &addr, &addrlen)!= 0)
throw WSAGetLastError();
return addr;
}
sockaddr_in Address4::Tosockaddr(void) const
{
return this->address;
}
std::string * Address4::Tostring(void) const
{
WSA was;
std::string* out = new std::string();
uint32_t outlen = INET_ADDRSTRLEN;
out->resize(INET_ADDRSTRLEN);
WSAAddressToStringA((sockaddr*) &(this->address), sizeof(sockaddr_in), 0, (LPSTR)out->c_str(), &outlen);
out->resize(outlen);
return out;
}
std::wstring * Address4::Towstring(void) const
{
WSA was;
std::wstring* out = new std::wstring();
uint32_t outlen = INET_ADDRSTRLEN;
out->resize(INET_ADDRSTRLEN);
WSAAddressToStringW((sockaddr*) &(this->address), sizeof(sockaddr_in), 0, (LPWSTR)out->c_str(), &outlen);
out->resize(outlen);
return out;
}
char* Address4::Tochar(void) const
{
char* out = new c8[INET_ADDRSTRLEN];
GetcharAddress(out);
return out;
}
wchar_t* Address4::Towchar_t(void) const
{
wchar_t* out = new c16[INET_ADDRSTRLEN];
Getwchar_tAddress(out);
return out;
}
uint32_t Address4::GetstringAddress(std::string* address) const
{
WSA was;
uint32_t outlen = INET_ADDRSTRLEN;
address->resize(INET_ADDRSTRLEN);
WSAAddressToStringA((sockaddr*) &(this->address), sizeof(sockaddr_in), 0, (LPSTR)address->c_str(), &outlen);
address->resize(outlen);
return outlen;
return 0;
}
uint32_t Address4::GetwstringAddress(std::wstring* address) const
{
WSA was;
uint32_t outlen = INET_ADDRSTRLEN;
address->resize(INET_ADDRSTRLEN);
WSAAddressToStringW((sockaddr*) &(this->address), sizeof(sockaddr_in), 0, (LPWSTR)address->c_str(), &outlen);
address->resize(outlen);
return outlen;
return 0;
}
uint32_t Address4::GetcharAddress(char* address) const
{
WSA was;
uint32_t outlen;
WSAAddressToStringA((sockaddr*) &(this->address), sizeof(sockaddr_in6), 0, (LPSTR)address, &outlen);
return outlen;
}
uint32_t Address4::Getwchar_tAddress(const wchar_t* address) const
{
WSA was;
uint32_t outlen;
WSAAddressToStringW((sockaddr*) &(this->address), sizeof(sockaddr_in), 0, (LPWSTR)address, &outlen);
return outlen;
}
uint16_t Address4::GetPort(void) const
{
WSA was;
return ntohs(address.sin_port);
}
IP4 Address4::GetIP(void) const
{
return IP4((w32)this->address.sin_addr.S_un.S_addr);
}
std::string * Address4::GetstringIP(void) const
{
char out[INET_ADDRSTRLEN];
GetcharIP(out);
return new std::string((const char*)out);
}
std::wstring * Address4::GetwstringIP(void) const
{
wchar_t out[INET_ADDRSTRLEN];
Getwchar_tIP(out);
return new std::wstring((const wchar_t*)out);
}
char* Address4::GetcharIP(void) const
{
char out* = new c8[INET_ADDRSTRLEN];
GetcharIP(out);
return out;
}
wchar_t* Address4::Getwchar_tIP(void) const
{
wchar_t* out = new wchar_t[INET_ADDRSTRLEN];
Getwchar_tIP(out);
return out;
}
uint32_t Address4::GetstringIP(std::string & iP) const
{
char out[INET_ADDRSTRLEN];
uint32_t outlen;
outlen = GetcharIP(out);
iP.append((const char*)out);
return outlen;
}
uint32_t Address4::GetwstringIP(std::wstring & iP) const
{
c16 out[INET_ADDRSTRLEN];
uint32_t outlen;
outlen = Getwchar_tIP(out);
iP.append((const wchar_t*)out);
return outlen;
}
uint32_t Address4::GetcharIP(const char* iP) const
{
WSA was;
uint32_t outlen = INET_ADDRSTRLEN;
sockaddr_in addr;
addr.sin_addr = this->address.sin_addr;
addr.sin_family = AF_INET;
addr.sin_port = 0;
WSAAddressToStringA((sockaddr*)&addr, sizeof(sockaddr_in), 0, (LPSTR)iP, &outlen);
return outlen;
}
uint32_t Address4::Getwchar_tIP(const wchar_t* iP) const
{
WSA was;
uint32_t outlen = INET_ADDRSTRLEN;
sockaddr_in addr;
addr.sin_addr = this->address.sin_addr;
addr.sin_family = AF_INET;
addr.sin_port = 0;
WSAAddressToStringW((sockaddr*)&addr, sizeof(sockaddr_in), 0, (LPWSTR)iP, &outlen);
return outlen;
}
}
}