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