updates.
[silc.git] / win32 / tests / testi2.cpp
1 // testi2.cpp : Defines the entry point for the application.\r
2 //\r
3 \r
4 #define FD_SETSIZE 5000\r
5 #include "stdafx.h"\r
6 #include "resource.h"\r
7 #include <winsock2.h>\r
8 #include <mswsock.h>\r
9 extern "C"\r
10 {\r
11 #define FD_SETSIZE 5000\r
12 #include "silcincludes.h"\r
13 #include "clientlibincludes.h"\r
14 }\r
15 \r
16 #define MAX_LOADSTRING 100\r
17 \r
18 // Global Variables:\r
19 HINSTANCE hInst;                                                                // current instance\r
20 TCHAR szTitle[MAX_LOADSTRING];                                                          // The title bar text\r
21 TCHAR szWindowClass[MAX_LOADSTRING];                                                            // The title bar text\r
22 \r
23 // Foward declarations of functions included in this code module:\r
24 ATOM                            MyRegisterClass(HINSTANCE hInstance);\r
25 BOOL                            InitInstance(HINSTANCE, int);\r
26 LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);\r
27 LRESULT CALLBACK        About(HWND, UINT, WPARAM, LPARAM);\r
28 \r
29 void silc_op_say(SilcClient client, SilcClientConnection conn, \r
30                  SilcClientMessageType type, char *msg, ...)\r
31 {\r
32         va_list vp;\r
33         char message[2048];\r
34 \r
35         memset(message, 0, sizeof(message));\r
36         strncat(message, "\n***  ", 5);\r
37 \r
38         va_start(vp, msg);\r
39         vsprintf(message + 5, msg, vp);\r
40         va_end(vp);\r
41 \r
42         MessageBox( NULL, (char *)message, "say", MB_OK | MB_ICONINFORMATION );\r
43 }\r
44 \r
45 void silc_notify(SilcClient client, SilcClientConnection conn, \r
46                  SilcNotifyType type, ...)\r
47 {\r
48 \r
49 }\r
50 \r
51 void silc_connect(SilcClient client, SilcClientConnection conn, int success)\r
52 {\r
53 \r
54 }\r
55 \r
56 int silc_auth_meth(SilcClient client, SilcClientConnection conn,\r
57                          char *hostname, uint16 port,\r
58                          SilcProtocolAuthMeth *auth_meth,\r
59                          unsigned char **auth_data,\r
60                          uint32 *auth_data_len)\r
61 {\r
62     *auth_meth = SILC_AUTH_NONE;\r
63         return TRUE;\r
64 }\r
65 void silc_verify_public_key(SilcClient client, SilcClientConnection conn,\r
66                             SilcSocketType conn_type, unsigned char *pk, \r
67                             uint32 pk_len, SilcSKEPKType pk_type,\r
68                             SilcVerifyPublicKey completion, void *context)\r
69 {\r
70   completion(TRUE, context);\r
71 }\r
72 \r
73 void silc_command_reply(SilcClient client, SilcClientConnection conn,\r
74                         SilcCommandPayload cmd_payload, int success,\r
75                         SilcCommand command, SilcCommandStatus status, ...)\r
76 {\r
77 \r
78 }\r
79 \r
80 /* SILC client operations */\r
81 SilcClientOperations ops = {\r
82   silc_op_say,\r
83         NULL,\r
84         NULL,\r
85         silc_notify,\r
86         NULL,\r
87         silc_command_reply,\r
88         silc_connect,\r
89         NULL,\r
90         silc_auth_meth,\r
91         silc_verify_public_key,\r
92 };\r
93 \r
94 SILC_TASK_CALLBACK(connect_client)\r
95 {\r
96   SilcClient client = (SilcClient)context;\r
97         silc_client_connect_to_server(client, 1334, "leevi.kuo.fi.ssh.com", NULL);\r
98 }\r
99 \r
100 void silc_log(char *message)\r
101 {\r
102 }\r
103 \r
104 void silc_debugl(char *file, char *function, \r
105                                                                 int line, char *message)\r
106 {\r
107         char m[5000];\r
108         memcpy(m, message, strlen(message));\r
109         m[strlen(message)] = '\n';\r
110         m[strlen(message) + 1] = 0;\r
111         OutputDebugString(m);\r
112 }\r
113 \r
114 void silc_hexdumpl(char *file, char *function, \r
115                                                            int line, unsigned char *data_in,\r
116                                                            uint32 data_len, char *message)\r
117 {\r
118   int i, k;\r
119   int off, pos, count;\r
120   unsigned char *data = (unsigned char *)data_in;\r
121         char m[10000], *cp;\r
122         int len = data_len;\r
123         \r
124 //      memset(m, 0, sizeof(m));\r
125 \r
126         cp = m;\r
127   snprintf(cp, 10000, "%s:%d: %s\n", function, line, message);\r
128         cp += strlen(cp);\r
129 \r
130   k = 0;\r
131   off = len % 16;\r
132   pos = 0;\r
133   count = 16;\r
134   while (1) {\r
135 \r
136     if (off) {\r
137       if ((len - pos) < 16 && (len - pos <= len - off))\r
138                                 count = off;\r
139     } else {\r
140       if (pos == len)\r
141                                 count = 0;\r
142     }\r
143     if (off == len)\r
144       count = len;\r
145 \r
146     if (count) {\r
147       snprintf(cp, sizeof(m), "%08X  ", k++ * 16);\r
148                         cp += strlen(cp);\r
149                 }\r
150 \r
151     for (i = 0; i < count; i++) {\r
152       snprintf(cp, sizeof(m), "%02X ", data[pos + i]);\r
153                         cp += strlen(cp);\r
154       \r
155       if ((i + 1) % 4 == 0) {\r
156                                 snprintf(cp, sizeof(m), " ");\r
157                                 cp += strlen(cp);\r
158                         }\r
159                 }\r
160 \r
161     if (count && count < 16) {\r
162       int j;\r
163       \r
164       for (j = 0; j < 16 - count; j++) {\r
165                                 snprintf(cp, sizeof(m), "   ");\r
166                                 cp += strlen(cp);\r
167                 \r
168                                 if ((j + count + 1) % 4 == 0) {\r
169                                         snprintf(cp, sizeof(m), " ");\r
170                                         cp += strlen(cp);\r
171                                 }\r
172                         }\r
173     }\r
174           \r
175     for (i = 0; i < count; i++) {\r
176       char ch;\r
177       \r
178       if (data[pos] < 32 || data[pos] >= 127)\r
179                                 ch = '.';\r
180       else\r
181                                 ch = data[pos];\r
182 \r
183       snprintf(cp, sizeof(m), "%c", ch);\r
184                         cp += strlen(cp);\r
185       pos++;\r
186     }\r
187 \r
188     if (count) {\r
189       snprintf(cp, sizeof(m), "\n");\r
190                         cp += strlen(cp);\r
191                 }\r
192 \r
193     if (count < 16)\r
194       break;\r
195   }\r
196         \r
197         OutputDebugString(m);\r
198         MessageBox( NULL, (char *)m, "hexdump", MB_OK | MB_ICONINFORMATION );\r
199 }\r
200 \r
201 static int \r
202 silc_create_key_pair(char *pkcs_name, int bits, char *path,\r
203                             char *identifier, \r
204                             SilcPublicKey *ret_pub_key,\r
205                             SilcPrivateKey *ret_prv_key)\r
206 {\r
207   SilcPKCS pkcs;\r
208   SilcPublicKey pub_key;\r
209   SilcPrivateKey prv_key;\r
210   SilcRng rng;\r
211   unsigned char *key;\r
212   uint32 key_len;\r
213   char pkfile[256], prvfile[256];\r
214 \r
215   if (!pkcs_name || !path)\r
216     return FALSE;\r
217 \r
218   if (!bits)\r
219     bits = 1024;\r
220 \r
221   rng = silc_rng_alloc();\r
222   silc_rng_init(rng);\r
223   silc_rng_global_init(rng);\r
224 \r
225   /* Generate keys */\r
226   silc_pkcs_alloc((const unsigned char *)pkcs_name, &pkcs);\r
227   pkcs->pkcs->init(pkcs->context, bits, rng);\r
228 \r
229   /* Save public key into file */\r
230   key = silc_pkcs_get_public_key(pkcs, &key_len);\r
231   pub_key = silc_pkcs_public_key_alloc(pkcs->pkcs->name, identifier,\r
232                                        key, key_len);\r
233   *ret_pub_key = pub_key;\r
234 \r
235   memset(key, 0, sizeof(key_len));\r
236   silc_free(key);\r
237 \r
238   /* Save private key into file */\r
239   key = silc_pkcs_get_private_key(pkcs, &key_len);\r
240   prv_key = silc_pkcs_private_key_alloc(pkcs->pkcs->name, key, key_len);\r
241   *ret_prv_key = prv_key;\r
242 \r
243   memset(key, 0, sizeof(key_len));\r
244   silc_free(key);\r
245 \r
246   silc_rng_free(rng);\r
247   silc_pkcs_free(pkcs);\r
248 \r
249   return TRUE;\r
250 }\r
251 \r
252 \r
253 int APIENTRY WinMain(HINSTANCE hInstance,\r
254                      HINSTANCE hPrevInstance,\r
255                      LPSTR     lpCmdLine,\r
256                      int       nCmdShow)\r
257 {\r
258         // TODO: Place code here.\r
259         MSG msg;\r
260         HACCEL hAccelTable;\r
261         HANDLE h;\r
262         HANDLE handles[100];\r
263         SOCKET s;\r
264         unsigned int k;\r
265         WSAEVENT e, e2, e3;\r
266         int ret;\r
267         DWORD ready;\r
268         HMODULE mod;\r
269 \r
270         // Initialize global strings\r
271         LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);\r
272         LoadString(hInstance, IDC_TESTI2, szWindowClass, MAX_LOADSTRING);\r
273         MyRegisterClass(hInstance);\r
274 \r
275         // Perform application initialization:\r
276         if (!InitInstance (hInstance, nCmdShow)) \r
277         {\r
278                 return FALSE;\r
279         }\r
280 \r
281         hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTI2);\r
282 \r
283         {\r
284                 SilcSchedule sched;     \r
285                 SilcClient client;\r
286 \r
287                 silc_net_win32_init();\r
288                 client = silc_client_alloc(&ops, NULL, NULL, "SILC-1.0-0.5.1");\r
289                 client->realname = "pekka riikonen";\r
290                 client->username = "priikone";\r
291                 client->hostname = "leevi.kuo.fi.ssh.com";\r
292 \r
293                 silc_cipher_register_default();\r
294                 silc_pkcs_register_default();\r
295                 silc_hash_register_default();\r
296                 silc_hmac_register_default();\r
297 \r
298                 silc_debug = TRUE;\r
299                 silc_log_set_debug_callbacks(silc_debugl, silc_hexdumpl);\r
300 \r
301                 silc_create_key_pair("rsa", 1024, "kk", "UN=priikone, HN=pelle.kuo.fi.ssh.com", \r
302                                                                                                 &client->public_key, &client->private_key);\r
303 \r
304                 silc_client_init(client);\r
305 \r
306                 silc_schedule_task_add(client->schedule, 0, connect_client, \r
307                                                                 client, 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL); \r
308 \r
309                 silc_client_run(client);                \r
310         }\r
311         \r
312         return msg.wParam;\r
313 }\r
314 \r
315 //\r
316 //  FUNCTION: MyRegisterClass()\r
317 //\r
318 //  PURPOSE: Registers the window class.\r
319 //\r
320 //  COMMENTS:\r
321 //\r
322 //    This function and its usage is only necessary if you want this code\r
323 //    to be compatible with Win32 systems prior to the 'RegisterClassEx'\r
324 //    function that was added to Windows 95. It is important to call this function\r
325 //    so that the application will get 'well formed' small icons associated\r
326 //    with it.\r
327 //\r
328 ATOM MyRegisterClass(HINSTANCE hInstance)\r
329 {\r
330         WNDCLASSEX wcex;\r
331 \r
332         wcex.cbSize = sizeof(WNDCLASSEX); \r
333 \r
334         wcex.style                      = CS_HREDRAW | CS_VREDRAW;\r
335         wcex.lpfnWndProc        = (WNDPROC)WndProc;\r
336         wcex.cbClsExtra         = 0;\r
337         wcex.cbWndExtra         = 0;\r
338         wcex.hInstance          = hInstance;\r
339         wcex.hIcon                      = LoadIcon(hInstance, (LPCTSTR)IDI_TESTI2);\r
340         wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);\r
341         wcex.hbrBackground      = (HBRUSH)(COLOR_WINDOW+1);\r
342         wcex.lpszMenuName       = (LPCSTR)IDC_TESTI2;\r
343         wcex.lpszClassName      = szWindowClass;\r
344         wcex.hIconSm            = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);\r
345 \r
346         return RegisterClassEx(&wcex);\r
347 }\r
348 \r
349 //\r
350 //   FUNCTION: InitInstance(HANDLE, int)\r
351 //\r
352 //   PURPOSE: Saves instance handle and creates main window\r
353 //\r
354 //   COMMENTS:\r
355 //\r
356 //        In this function, we save the instance handle in a global variable and\r
357 //        create and display the main program window.\r
358 //\r
359 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)\r
360 {\r
361    HWND hWnd;\r
362 \r
363    hInst = hInstance; // Store instance handle in our global variable\r
364 \r
365    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,\r
366       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);\r
367 \r
368    if (!hWnd)\r
369    {\r
370                         LPVOID lpMsgBuf;\r
371                         FormatMessage( \r
372                           FORMAT_MESSAGE_ALLOCATE_BUFFER | \r
373                           FORMAT_MESSAGE_FROM_SYSTEM | \r
374                           FORMAT_MESSAGE_IGNORE_INSERTS,\r
375                           NULL,\r
376                           GetLastError(),\r
377                           0, // Default language\r
378                           (LPTSTR) &lpMsgBuf,\r
379                           0,\r
380                           NULL \r
381                         );\r
382                         // Process any inserts in lpMsgBuf.\r
383                         // ...\r
384                         // Display the string.\r
385                         MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );\r
386                         // Free the buffer.\r
387                         LocalFree( lpMsgBuf );\r
388 \r
389       return FALSE;\r
390    }\r
391 \r
392    ShowWindow(hWnd, nCmdShow);\r
393    UpdateWindow(hWnd);\r
394 \r
395    return TRUE;\r
396 }\r
397 \r
398 //\r
399 //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)\r
400 //\r
401 //  PURPOSE:  Processes messages for the main window.\r
402 //\r
403 //  WM_COMMAND  - process the application menu\r
404 //  WM_PAINT    - Paint the main window\r
405 //  WM_DESTROY  - post a quit message and return\r
406 //\r
407 //\r
408 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)\r
409 {\r
410         int wmId, wmEvent;\r
411         PAINTSTRUCT ps;\r
412         HDC hdc;\r
413         TCHAR szHello[MAX_LOADSTRING];\r
414         LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);\r
415 \r
416         switch (message) \r
417         {\r
418                 case WM_COMMAND:\r
419                         wmId    = LOWORD(wParam); \r
420                         wmEvent = HIWORD(wParam); \r
421                         // Parse the menu selections:\r
422                         switch (wmId)\r
423                         {\r
424                                 case IDM_ABOUT:\r
425                                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);\r
426                                    break;\r
427                                 case IDM_EXIT:\r
428                                    DestroyWindow(hWnd);\r
429                                    break;\r
430                                 default:\r
431                                    return DefWindowProc(hWnd, message, wParam, lParam);\r
432                         }\r
433                         break;\r
434                 case WM_PAINT:\r
435                         hdc = BeginPaint(hWnd, &ps);\r
436                         // TODO: Add any drawing code here...\r
437                         RECT rt;\r
438                         GetClientRect(hWnd, &rt);\r
439                         DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);\r
440                         EndPaint(hWnd, &ps);\r
441                         break;\r
442                 case WM_DESTROY:\r
443                         PostQuitMessage(0);\r
444                         break;\r
445                 default:\r
446                         return DefWindowProc(hWnd, message, wParam, lParam);\r
447    }\r
448    return 0;\r
449 }\r
450 \r
451 // Mesage handler for about box.\r
452 LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
453 {\r
454         switch (message)\r
455         {\r
456                 case WM_INITDIALOG:\r
457                                 return TRUE;\r
458 \r
459                 case WM_COMMAND:\r
460                         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) \r
461                         {\r
462                                 EndDialog(hDlg, LOWORD(wParam));\r
463                                 return TRUE;\r
464                         }\r
465                         break;\r
466         }\r
467     return FALSE;\r
468 }\r