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
264
265
266
|
/*
* This code implements communication based on Miranda netlib library
*
* (c) majvan 2002-2004
*/
#include "..\yamn.h"
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
BOOL SSLLoaded=FALSE;
HANDLE hNetlibUser=NULL;
void __stdcall SSL_DebugLog(const char *fmt, ...)
{
char str[ 4096 ];
va_list vararg;
va_start( vararg, fmt );
int tBytes = _vsnprintf( str, sizeof(str)-1, fmt, vararg );
if ( tBytes == 0 )
return;
if ( tBytes > 0 )
str[ tBytes ] = 0;
else
str[ sizeof(str)-1 ] = 0;
CallService(MS_NETLIB_LOG, (WPARAM)hNetlibUser, (LPARAM)str);
va_end( vararg );
}
HANDLE RegisterNLClient(const char *name)
{
static NETLIBUSER nlu={0};
char desc[128];
sprintf(desc, Translate("%s connection"),name);
#ifdef DEBUG_COMM
DebugLog(CommFile,"<Register PROXY support>");
#endif
nlu.cbSize = sizeof(nlu);
nlu.flags = NUF_OUTGOING | NUF_HTTPCONNS;
nlu.szDescriptiveName=desc;
nlu.szSettingsModule=(char *)name;
hNetlibUser=(HANDLE)CallService(MS_NETLIB_REGISTERUSER,0,(LPARAM)&nlu);
#ifdef DEBUG_COMM
if (NULL==hNetlibUser)
DebugLog(CommFile,"<error></Register PROXY support>\n");
else
DebugLog(CommFile,"</Register PROXY support>\n");
#endif
return hNetlibUser;
}
//Move connection to SSL
void CNLClient::SSLify() throw(DWORD) {
#ifdef DEBUG_COMM
SSL_DebugLog("Staring SSL...");
#endif
int socket = CallService(MS_NETLIB_GETSOCKET, (WPARAM)hConnection, 0);
if (socket != INVALID_SOCKET)
{
#ifdef DEBUG_COMM
SSL_DebugLog("Staring netlib core SSL");
#endif
if (CallService(MS_NETLIB_STARTSSL, (WPARAM)hConnection, 0))
{
#ifdef DEBUG_COMM
SSL_DebugLog("Netlib core SSL started");
#endif
isTLSed = true;
SSLLoaded = TRUE;
return;
}
}
//ssl could not be created
throw NetworkError = (DWORD)ESSL_CREATESSL;
}
//Connects to the server through the sock
//if not success, exception is throwed
void CNLClient::Connect(const char* servername,const int port) throw(DWORD)
{
NETLIBOPENCONNECTION nloc;
NetworkError=SystemError=0;
isTLSed = false;
#ifdef DEBUG_COMM
DebugLog(CommFile,"<connect>\n");
#endif
try
{
nloc.cbSize=sizeof(NETLIBOPENCONNECTION);
nloc.szHost=servername;
nloc.wPort=port;
nloc.flags=0;
if (NULL==(hConnection=(HANDLE)CallService(MS_NETLIB_OPENCONNECTION,(WPARAM)hNetlibUser,(LPARAM)&nloc)))
{
SystemError=WSAGetLastError();
throw NetworkError=(DWORD)ENL_CONNECT;
}
#ifdef DEBUG_COMM
DebugLog(CommFile,"</connect>\n");
#endif
return;
}
catch(...)
{
#ifdef DEBUG_COMM
DebugLog(CommFile,"<error></connect>\n");
#endif
throw;
}
}
//Performs a simple query
// query- command to send
int CNLClient::LocalNetlib_Send(HANDLE hConn,const char *buf,int len,int flags) {
if (isTLSed)
{
#ifdef DEBUG_COMM
SSL_DebugLog("SSL send: %s", buf);
#endif
}
NETLIBBUFFER nlb={(char*)buf,len,flags};
return CallService(MS_NETLIB_SEND,(WPARAM)hConn,(LPARAM)&nlb);
}
void CNLClient::Send(const char *query) throw(DWORD)
{
unsigned int Sent;
if (NULL==query)
return;
if (hConnection==NULL)
return;
#ifdef DEBUG_COMM
DebugLog(CommFile,"<send>%s",query);
#endif
try
{
if ((SOCKET_ERROR==(Sent=LocalNetlib_Send(hConnection,query,(int)strlen(query),MSG_DUMPASTEXT))) || Sent != (unsigned int)strlen(query))
{
SystemError=WSAGetLastError();
throw NetworkError=(DWORD)ENL_SEND;
}
#ifdef DEBUG_COMM
DebugLog(CommFile,"</send>\n");
#endif
}
catch(...)
{
#ifdef DEBUG_COMM
DebugLog(CommFile,"<error></send>\n");
#endif
throw;
}
}
//Reads data from socket
// buf- buffer where to store max. buflen of received characters
// if buf is NULL, creates buffer of buflen size
// buf is NULL by default
//You need free() returned buffer, which can be allocated in this function
//if not success, exception is throwed
int CNLClient::LocalNetlib_Recv(HANDLE hConn,char *buf,int len,int flags) {
NETLIBBUFFER nlb={buf,len,flags};
int iReturn = CallService(MS_NETLIB_RECV,(WPARAM)hConn,(LPARAM)&nlb);
if (isTLSed)
{
#ifdef DEBUG_COMM
SSL_DebugLog("SSL recv: %s", buf);
#endif
}
return iReturn;
}
char* CNLClient::Recv(char *buf,int buflen) throw(DWORD)
{
#ifdef DEBUG_COMM
DebugLog(CommFile,"<reading>");
#endif
try
{
if (buf==NULL)
buf=(char *)malloc(sizeof(char)*(buflen+1));
if (buf==NULL)
throw NetworkError=(DWORD)ENL_RECVALLOC;
if (!isTLSed)
{
NETLIBSELECT nls;
memset(&nls, 0, sizeof(NETLIBSELECT));
nls.cbSize = sizeof(NETLIBSELECT);
nls.dwTimeout = 60000;
nls.hReadConns[0] = hConnection;
switch (CallService(MS_NETLIB_SELECT, 0, (LPARAM) &nls))
{
case SOCKET_ERROR:
free(buf);
SystemError=WSAGetLastError();
throw NetworkError = (DWORD) ENL_RECV;
case 0: // time out!
free(buf);
throw NetworkError = (DWORD) ENL_TIMEOUT;
}
}
ZeroMemory(buf,buflen);
if (SOCKET_ERROR==(Rcv=LocalNetlib_Recv(hConnection,buf,buflen,MSG_DUMPASTEXT)))
{
free(buf);
SystemError=WSAGetLastError();
throw NetworkError=(DWORD)ENL_RECV;
}
if (!Rcv)
{
free(buf);
SystemError=WSAGetLastError();
throw NetworkError=(DWORD)ENL_RECV;
}
#ifdef DEBUG_COMM
*(buf+Rcv)=0; //end the buffer to write it to file
DebugLog(CommFile,"%s",buf);
DebugLog(CommFile,"</reading>\n");
#endif
return(buf);
}
catch(...)
{
#ifdef DEBUG_COMM
DebugLog(CommFile,"<error></reading>\n");
#endif
throw;
}
}
//Closes netlib connection
void CNLClient::Disconnect()
{
Netlib_CloseHandle(hConnection);
hConnection=(HANDLE)NULL;
}
//Uninitializes netlib library
void UnregisterNLClient()
{
#ifdef DEBUG_COMM
DebugLog(CommFile,"<Unregister PROXY support>");
#endif
Netlib_CloseHandle(hNetlibUser);
hNetlibUser=(HANDLE)NULL;
#ifdef DEBUG_COMM
DebugLog(CommFile,"</Unregister PROXY support>\n");
#endif
}
|