This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsIRC_Socket.cpp
263 lines (222 loc) · 6.82 KB
/
zsIRC_Socket.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// zsIRC_Socket.cpp: implementation of the zsIRC_Socket class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "zsIRC.h"
#include "zsIRC_Socket.h"
#include "zsIRC_Settings.h"
#include <objbase.h>
#include <initguid.h>
#if UNDER_CE >= 420
#include <connmgr.h>
#endif
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
zsIRC_Socket::zsIRC_Socket()
{
sock = NULL;
}
zsIRC_Socket::~zsIRC_Socket()
{
}
void zsIRC_Socket::Log(ZSIRC_LOGGER log, void * u, TCHAR * szFormat, ...) {
if (!log) return;
TCHAR msg[2000];
va_list ap;
va_start(ap, szFormat);
_vsntprintf(msg, 2000, szFormat, ap);
va_end(ap);
log(u,msg);
}
int SSLValidateCertHook(
DWORD dwType, // in
LPVOID pvArg, // in
DWORD dwChainLen, // in
LPBLOB pCertChain, // in
DWORD dwFlags) // in
{
if (dwFlags & SSL_CERT_FLAG_ISSUER_UNKNOWN) {
switch (SETUP.nServerSSLBehaviour) {
case ZSIRC_SSLBEHAVIOUR_ALWAYSACCEPT:
{
return SSL_ERR_OKAY;
} break;
case ZSIRC_SSLBEHAVIOUR_NEVERACCEPT:
{
return SSL_ERR_CERT_UNKNOWN;
} break;
case ZSIRC_SSLBEHAVIOUR_PROMPTUSER:
default:
{
if (MessageBox(GetForegroundWindow(),
_T("The certificate issuer for this server is unknown. Still accept the certificate?"),
_T("zsIRC SSL"),MB_YESNO | MB_ICONQUESTION)==IDNO) {
return SSL_ERR_CERT_UNKNOWN;
}
return SSL_ERR_OKAY;
} break;
}
}
return SSL_ERR_OKAY;
}
int zsIRC_Socket::Connect(char * sServerName, int port, ZSIRC_LOGGER log, void * u, bool bSecure)
{
WSADATA WSData;
if(WSAStartup(MAKEWORD(1, 1), &WSData)) {
DWORD e = WSAGetLastError();
Log(log,u,_T("WSAStartup failed: %d"),e);
return 0;
}
unsigned int addr = inet_addr(sServerName);
if (addr == INADDR_NONE) {
hostent * hostEntry = gethostbyname(sServerName);
if (!hostEntry) {
DWORD e = WSAGetLastError();
Log(log,u,_T("Unable to resolve host: %d"),e);
return 0;
}
LPIN_ADDR pa = (LPIN_ADDR)(hostEntry->h_addr);
Log(log,u,_T("Resolved to %d.%d.%d.%d..."),
pa->S_un.S_un_b.s_b1,
pa->S_un.S_un_b.s_b2,
pa->S_un.S_un_b.s_b3,
pa->S_un.S_un_b.s_b4);
addr = pa->S_un.S_addr;
if (addr == INADDR_NONE) {
Log(log,u,_T("Failed to create sensible address!"));
return 0;
}
} else {
Log(log,u,_T("Address is an IP, skipping resolve..."));
}
sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sock==INVALID_SOCKET) {
DWORD e = WSAGetLastError();
Log(log,u,_T("Unable to create socket: %d"),e);
return 0;
}
if (bSecure) {
SSLVALIDATECERTHOOK hook;
DWORD dwVal = SO_SEC_SSL;
if(setsockopt(sock, SOL_SOCKET, SO_SECURE, (LPSTR)&dwVal, sizeof(DWORD)) != 0) {
Log(log,u,_T("Unable to set SockOpt: %d"));
return FALSE;
}
hook.HookFunc = SSLValidateCertHook;
hook.pvArg = (PVOID) sock;
if(WSAIoctl(sock,
SO_SSL_SET_VALIDATE_CERT_HOOK,
&hook, sizeof(SSLVALIDATECERTHOOK),
NULL, 0, NULL, NULL, NULL))
{
Log(log,u,_T("WSAIoctl SO_SSL_SET_VALIDATE_CERT_HOOK failed: %d"),WSAGetLastError());
return 0;
}
/*
dwFlags = SSL_FLAG_DEFER_HANDSHAKE;
if(WSAIoctl(sock, SO_SSL_SET_FLAGS, &dwFlags, sizeof(DWORD), NULL, 0, NULL, NULL, NULL))
{
Log(log,u,_T("WSAIoctl SO_SSL_SET_FLAGS failed: %d"),WSAGetLastError());
return 0;
}
*/
}
SOCKADDR_IN serverInfo;
ZeroMemory(&serverInfo,sizeof(SOCKADDR_IN));
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.S_un.S_addr = addr;
serverInfo.sin_port = htons(port);
if (connect(sock,(LPSOCKADDR)&serverInfo,sizeof(sockaddr))==SOCKET_ERROR) {
DWORD e = WSAGetLastError();
Log(log,u,_T("Unable to connect: %d"),e);
return 0;
}
if (bSecure) {
DWORD cbReturned;
SSLCONNECTIONINFO ConnectionInfo;
/*
char szName[2];
szName[0] = (char) GetCurrentThreadId();
szName[1] = '\0';
if(WSAIoctl(sock, SO_SSL_PERFORM_HANDSHAKE, szName, 2, NULL, 0, NULL, NULL, NULL))
{
Log(log,u,_T("WSAIoctl SO_SSL_PERFORM_HANDSHAKE failed: %d"),WSAGetLastError());
return 0;
}
*/
if (WSAIoctl(sock, SO_SSL_GET_CONNECTION_INFO, NULL, 0, &ConnectionInfo, sizeof(ConnectionInfo), &cbReturned, NULL,NULL))
{
Log(log,u,_T("WSAIoctl SO_SSL_GET_CONNECTION_INFO failed: %d"),WSAGetLastError());
return 0;
}
#ifdef _DEBUG
Log(log,u,_T("[ssl] Protocol: 0x%08x"),ConnectionInfo.dwProtocol);
Log(log,u,_T("[ssl] Cipher: 0x%08x"), ConnectionInfo.aiCipher);
Log(log,u,_T("[ssl] Cipher strength: %d"),ConnectionInfo.dwCipherStrength);
Log(log,u,_T("[ssl] Hash: 0x%08x"), ConnectionInfo.aiHash);
Log(log,u,_T("[ssl] Hash strength: %d"), ConnectionInfo.dwHashStrength);
Log(log,u,_T("[ssl] Key exchange: 0x%08x"),ConnectionInfo.aiExch);
Log(log,u,_T("[ssl] Key exchange strength: %d"),ConnectionInfo.dwExchStrength);
#endif
}
return 1;
}
int zsIRC_Socket::Read( void * pData, unsigned int iLen)
{
return recv(sock, (char*)pData, iLen, 0);
}
int zsIRC_Socket::Write( void * pData, unsigned int iLen)
{
return send(sock, (char*)pData, iLen, 0);
}
int zsIRC_Socket::Disconnect() {
if (sock) {
closesocket(sock);
WSACleanup();
}
sock=NULL;
return 1;
}
int zsIRC_Socket::hasData()
{
timeval timeout;
timeout.tv_sec=0;
timeout.tv_usec=0;
fd_set conn;
FD_ZERO(&conn);
FD_SET(sock, &conn);
return !(select(sock+1, &conn, NULL, NULL, &timeout)<=0);
}
int zsIRC_Socket::isConnected()
{
return (sock!=NULL);
}
#if UNDER_CE >= 420
#pragma comment(lib,"cellcore.lib")
#endif
int zsIRC_Socket::InitializeGPRS(ZSIRC_LOGGER log, void * u) {
#if UNDER_CE >= 420
CONNMGR_CONNECTIONINFO connInfo;
ZeroMemory(&connInfo, sizeof(CONNMGR_CONNECTIONINFO));
connInfo.cbSize = sizeof(CONNMGR_CONNECTIONINFO);
connInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
connInfo.dwPriority = CONNMGR_PRIORITY_HIPRIBKGND;
connInfo.guidDestNet = IID_DestNetInternet;
HANDLE hConnection = NULL;
DWORD dwStatus = 0;
DWORD dwTimeout = 5000;
if (FAILED(ConnMgrEstablishConnectionSync(&connInfo, &hConnection, dwTimeout, &dwStatus)))
{
DWORD e = WSAGetLastError();
Log(log,u,_T("Unable to initialize GRPS connection: %d"), e);
return 0;
}
#endif
return 1;
}