summaryrefslogtreecommitdiffstats
path: root/src/windows/wintel/telnet.c
blob: a2f5083ca5014ba05d5ef0d4830f267da50d8590 (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/****************************************************************************

  Program: telnet.c

  PURPOSE: Windows networking kernel - Telnet

  FUNCTIONS:

  WinMain() - calls initialization function, processes message loop
  InitApplication() - initializes window data and registers window
  InitInstance() - saves instance handle and creates main window
  MainWndProc() - processes messages
  About() - processes messages for "About" dialog box

  COMMENTS:

  Windows can have several copies of your application running at the
  same time.  The variable hInst keeps track of which instance this
  application is so that processing will be to the correct window.

  ****************************************************************************/

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "telnet.h"
#include "auth.h"

static HANDLE hInst;
static HWND hWnd;
static CONFIG *tmpConfig;
static CONNECTION *con = NULL;
static char hostdata[MAXGETHOSTSTRUCT];
static SCREEN *pScr;
static int debug = 1;

char strTmp[1024];			/* Scratch buffer */
BOOL bAutoConnection = FALSE;
short port_no = 23;
char szUserName[64];			/* Used in auth.c */
char szHostName[64];

#ifdef KRB4
#define WINDOW_CLASS   "K4_telnetWClass"
#endif

#ifdef KRB5
krb5_context k5_context;
#define WINDOW_CLASS   "K5_telnetWClass"
#endif

/*
 *
 * FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
 *
 * PURPOSE: calls initialization function, processes message loop
 *
 * COMMENTS:
 *
 * Windows recognizes this function by name as the initial entry point
 * for the program.  This function calls the application initialization
 * routine, if no other instance of the program is running, and always
 * calls the instance initialization routine.  It then executes a message
 * retrieval and dispatch loop that is the top-level control structure
 * for the remainder of execution.  The loop is terminated when a WM_QUIT
 * message is received, at which time this function exits the application
 * instance by returning the value passed by PostQuitMessage().
 *
 * If this function must abort before entering the message loop, it
 * returns the conventional value NULL.
 */

int PASCAL
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  MSG msg;

  if (!hPrevInstance)
    if (!InitApplication(hInstance))
      return(FALSE);

  /*
   * Perform initializations that apply to a specific instance
   */
  bAutoConnection = parse_cmdline(lpCmdLine);

  if (!InitInstance(hInstance, nCmdShow))
    return(FALSE);

#ifdef _WIN32
  SetDebugErrorLevel(SLE_WARNING);
#endif

  /*
   * Acquire and dispatch messages until a WM_QUIT message is received.
   */
  while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);

    /* Process all non-network messages */
    while (PeekMessage(&msg, NULL, 0, WM_NETWORKEVENT-1, PM_REMOVE) ||
	   PeekMessage(&msg, NULL, WM_NETWORKEVENT+1, (UINT)-1, PM_REMOVE))
    {
	if (msg.message == WM_QUIT)	// Special case: WM_QUIT -- return
	    return msg.wParam;		//   the value from PostQuitMessage

	TranslateMessage(&msg);
	DispatchMessage(&msg);
    }
  }

  return (msg.wParam);		/* Returns the value from PostQuitMessage */
}

/*
 * FUNCTION: InitApplication(HINSTANCE)
 *
 * PURPOSE: Initializes window data and registers window class
 *
 * COMMENTS:
 *
 * This function is called at initialization time only if no other
 * instances of the application are running.  This function performs
 * initialization tasks that can be done once for any number of running
 * instances.
 *
 * In this case, we initialize a window class by filling out a data
 * structure of type WNDCLASS and calling the Windows RegisterClass()
 * function.  Since all instances of this application use the same window
 * class, we only need to do this when the first instance is initialized.
 */

BOOL
InitApplication(HINSTANCE hInstance)
{
  WNDCLASS  wc;

  ScreenInit(hInstance);

  /*
   * Fill in window class structure with parameters that describe the
   * main window.
   */
  wc.style = CS_HREDRAW | CS_VREDRAW; /* Class style(s). */
  wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for
				       * windows of this class.
				       */
  wc.cbClsExtra = 0;                  /* No per-class extra data. */
  wc.cbWndExtra = 0;                  /* No per-window extra data. */
  wc.hInstance = hInstance;           /* Application that owns the class. */
  wc.hIcon = NULL;		      /* LoadIcon(hInstance, "NCSA"); */
  wc.hCursor = NULL;		      /* Cursor(NULL, IDC_ARROW); */
  wc.hbrBackground = NULL;	      /* GetStockObject(WHITE_BRUSH); */
  wc.lpszMenuName =  NULL; 	      /* Name of menu resource in .RC file. */
  wc.lpszClassName = WINDOW_CLASS;    /* Name used in call to CreateWindow. */

  return(RegisterClass(&wc));
}


/*
 * FUNCTION:  InitInstance(HANDLE, int)
 *
 * PURPOSE:  Saves instance handle and creates main window
 *
 * COMMENTS:
 *
 * This function is called at initialization time for every instance of
 * this application.  This function performs initialization tasks that
 * cannot be shared by multiple instances.
 *
 * In this case, we save the instance handle in a static variable and
 * create and display the main program window.
 */
BOOL
InitInstance(HINSTANCE hInstance, int nCmdShow)
{
  int xScreen = 0;
  int yScreen = 0;
  WSADATA wsaData;

  SetScreenInstance(hInstance);

  /*
   * Save the instance handle in static variable, which will be used in
   * many subsequence calls from this application to Windows.
   */
  hInst = hInstance;

  /*
   * Create a main window for this application instance.
   */
  hWnd = CreateWindow(
		      WINDOW_CLASS,	/* See RegisterClass() call. */
		      "TCPWin",		/* Text for window title bar. */
		      WS_SYSMENU,	/* Window style. */
		      xScreen / 3,	/* Default horizontal position. */
		      yScreen / 3,	/* Default vertical position. */
		      xScreen / 3,	/* Default width. */
		      yScreen / 3,	/* Default height. */
		      NULL,		/* Overlapped windows have no parent */
		      NULL,		/* Use the window class menu. */
		      hInstance,	/* This instance owns this window. */
		      NULL);		/* Pointer not needed. */

  if (!hWnd)
    return (FALSE);

  if (WSAStartup(0x0101, &wsaData) != 0) {   /* Initialize the network */
    MessageBox(NULL, "Couldn't initialize Winsock!", NULL,
	       MB_OK | MB_ICONEXCLAMATION);
    return(FALSE);
  }

  if (!OpenTelnetConnection()) {
    WSACleanup();
    return(FALSE);
  }

#ifdef KRB5
  krb5_init_context(&k5_context);
#endif

  return (TRUE);
}

char buf[2048];

/*
 * FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
 *
 * PURPOSE:  Processes messages
 *
 * MESSAGES:
 *
 * WM_COMMAND    - application menu (About dialog box)
 * WM_DESTROY    - destroy window
 */
LRESULT CALLBACK
MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  HGLOBAL hBuffer;
  LPSTR lpBuffer;
  int iEvent, cnt, ret;
  char *tmpCommaLoc;
  struct sockaddr_in remote_addr;
  struct hostent *remote_host;

  switch (message) {
  case WM_MYSCREENCHANGEBKSP:
    if (!con)
      break;
    con->backspace = wParam;
    if (con->backspace == VK_BACK) {
      con->ctrl_backspace = 0x7f;
      WritePrivateProfileString(INI_TELNET, INI_BACKSPACE,
				INI_BACKSPACE_BS, TELNET_INI);
    }
    else {
      con->ctrl_backspace = VK_BACK;
      WritePrivateProfileString(INI_TELNET, INI_BACKSPACE,
				INI_BACKSPACE_DEL, TELNET_INI);
    }
    GetPrivateProfileString(INI_HOSTS, INI_HOST "0", "", buf, 128, TELNET_INI);
    tmpCommaLoc = strchr(buf, ',');
    if (tmpCommaLoc == NULL) {
      strcat (buf, ",");
      tmpCommaLoc = strchr(buf, ',');
    }
    if (tmpCommaLoc) {
      tmpCommaLoc++;
      if (con->backspace == VK_BACK)
	strcpy(tmpCommaLoc, INI_HOST_BS);
      else
	strcpy(tmpCommaLoc, INI_HOST_DEL);
    }
    WritePrivateProfileString(INI_HOSTS, INI_HOST "0", buf, TELNET_INI);
    break;

  case WM_MYSCREENCHAR:
    {
      unsigned char c;

      if (!con)
	break;
      if (wParam == VK_BACK)
	c = con->backspace;
      else if (wParam == 0x7f)
	c = con->ctrl_backspace;
      else if (wParam == VK_SPACE && GetKeyState(VK_CONTROL) < 0)
	c = 0;
      else
	c = wParam;
      TelnetSend(con->ks, &c, 1, 0);
    }
  break;

  case WM_MYCURSORKEY:
    /* Acts as a send through: buffer is lParam and length in wParam */
    if (!con)
      break;
    memcpy(buf, (char *)lParam, wParam);
    TelnetSend (con->ks, buf, wParam, 0);
    break;

  case WM_MYSCREENBLOCK:
    if (!con)
      break;
    hBuffer = (HGLOBAL) wParam;
    lpBuffer = GlobalLock(hBuffer);
    TelnetSend(con->ks, lpBuffer, lstrlen(lpBuffer), 0);
    GlobalUnlock(hBuffer);
    break;

  case WM_MYSCREENCLOSE:
#if 0
    if (con)
    {
	kstream_destroy(con->ks);
	con->ks = NULL;
    }
#endif
    DestroyWindow(hWnd);
    break;

  case WM_QUERYOPEN:
    return(0);
    break;

  case WM_DESTROY:          /* message: window being destroyed */
    if (con)
    {
	kstream_destroy(con->ks);
	free(con);
	WSACleanup();
    }
    PostQuitMessage(0);
    break;

  case WM_NETWORKEVENT:
    iEvent = WSAGETSELECTEVENT(lParam);

    switch (iEvent) {

    case FD_READ:
      if (con == NULL)
	break;
      cnt = kstream_read(con->ks, buf, 1500);
      buf[cnt] = 0;
      parse((CONNECTION *)con, (unsigned char *)buf, cnt);
      ScreenEm(buf, cnt, con->pScreen);
      break;

    case FD_CLOSE:
      kstream_destroy(con->ks);
      free(con);
      con = NULL;
      WSACleanup();
      PostQuitMessage(0);
      break;

    case FD_CONNECT:
      ret = WSAGETSELECTERROR(lParam);
      if (ret) {
	wsprintf(buf, "Error %d on Connect", ret);
	MessageBox(NULL, buf, NULL, MB_OK | MB_ICONEXCLAMATION);
	kstream_destroy(con->ks);
	free(con);
	WSACleanup();
	PostQuitMessage(0);
	break;
      }
      start_negotiation(con->ks);
      break;
    }

    break;

  case WM_HOSTNAMEFOUND:
    ret = WSAGETASYNCERROR(lParam);
    if (ret) {
      wsprintf(buf, "Error %d on GetHostbyName", ret);
      MessageBox(NULL, buf, NULL, MB_OK | MB_ICONEXCLAMATION);
      kstream_destroy(con->ks);
      free(con);
      WSACleanup();
      PostQuitMessage(0);
      break;
    }

    remote_host = (struct hostent *)hostdata;
    remote_addr.sin_family = AF_INET;
    memcpy(&(remote_addr.sin_addr), &(remote_host->h_addr[0]), 4);
    remote_addr.sin_port = htons(port_no);

    connect(con->socket, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr));
    break;

  case WM_MYSCREENSIZE:
    con->width = LOWORD(lParam);		/* width in characters */
    con->height = HIWORD(lParam);		/* height in characters */
    if (con->bResizeable && con->ks)
      send_naws(con);
    wsprintf(buf, "%d", con->height);
    WritePrivateProfileString(INI_TELNET, INI_HEIGHT, buf, TELNET_INI);
    wsprintf(buf, "%d", con->width);
    WritePrivateProfileString(INI_TELNET, INI_WIDTH, buf, TELNET_INI);
    break;

  default:              		/* Passes it on if unproccessed */
    return(DefWindowProc(hWnd, message, wParam, lParam));
  }
  return (0);
}


/*
 *
 * FUNCTION: SaveHostName(hostname, port)
 *
 * PURPOSE: Saves the currently selected host name and port number
 * in the KERBEROS.INI file and returns the preferred backspace
 * setting if one exists for that host.
 *
 * RETURNS: VK_BACK or 0x7f depending on the desired backspace setting.
 */
int
SaveHostName(char *host, int port)
{
  char buf[128];		/* Scratch buffer */
  char fullhost[128];		/* Host & port combination */
  char hostName[10][128];	/* Entries from INI files */
  char *comma;			/* For parsing del/bs info */
  int len;			/* Length of fullhost */
  int n;			/* Number of items written */
  int i;			/* Index */
  int bs;			/* What we return */

  if (port == 23)				/* Default telnet port */
    strcpy(fullhost, host);			/* ...then don't add it on */
  else
    wsprintf(fullhost, "%s %d", host, port);
  len = strlen(fullhost);

  comma = NULL;
  for (i = 0; i < 10; i++) {
    wsprintf(buf, INI_HOST "%d", i); /* INI item to fetch */
    GetPrivateProfileString(INI_HOSTS, buf, "", hostName[i],
			    128, TELNET_INI);

    if (!hostName[i][0])
      break;

    if (strncmp (hostName[i], fullhost, len))	/* A match?? */
      continue;					/* Nope, keep going */
    comma = strchr (hostName[i], ',');
  }

  if (comma) {
    ++comma;					/* Past the comma */
    while (*comma == ' ')			/* Past leading white space */
      ++comma;
    bs = VK_BACK;				/* Default for unknown entry */
    if (_stricmp(comma, INI_HOST_DEL) == 0)
      bs = 0x7f;
  }
  else {					/* No matching entry */
    GetPrivateProfileString(INI_TELNET, INI_BACKSPACE, INI_BACKSPACE_BS,
			    buf, sizeof(buf), TELNET_INI);
    bs = VK_BACK;				/* Default value */
    if (_stricmp(buf, INI_BACKSPACE_DEL) == 0)
      bs = 0x7f;
  }

  /*
   * Build up default host name
   */
  strcpy(buf, fullhost);
  strcat(buf, ", ");
  strcat(buf, (bs == VK_BACK) ? INI_BACKSPACE_BS : INI_BACKSPACE_DEL);
  WritePrivateProfileString(INI_HOSTS, INI_HOST "0", buf, TELNET_INI);

  n = 0;
  for (i = 0; i < 10; i++) {
    if (!hostName[i][0])			/* End of the list? */
      break;
    if (strncmp(hostName[i], fullhost, len) != 0) {
      wsprintf(buf, INI_HOST "%d", ++n);
      WritePrivateProfileString(INI_HOSTS, buf, hostName[i], TELNET_INI);
    }
  }
  return(bs);
}


int
OpenTelnetConnection(void)
{
  int nReturn, ret;
  struct sockaddr_in sockaddr;
  char *p;
  static struct kstream_crypt_ctl_block ctl;
  char buf[128];

  tmpConfig = calloc(sizeof(CONFIG), 1);

  if (bAutoConnection) {
    tmpConfig->title = calloc(lstrlen(szHostName), 1);
    lstrcpy(tmpConfig->title, (char *) szHostName);
  } else {
    nReturn = DoDialog("OPENTELNETDLG", OpenTelnetDlg);
    if (nReturn == FALSE)
      return(FALSE);
  }

  con = (CONNECTION *) GetNewConnection();
  if (con == NULL)
    return(0);

  tmpConfig->width =
    GetPrivateProfileInt(INI_TELNET, INI_WIDTH, DEF_WIDTH, TELNET_INI);

  tmpConfig->height =
    GetPrivateProfileInt(INI_TELNET, INI_HEIGHT, DEF_HEIGHT, TELNET_INI);
  con->width = tmpConfig->width;
  con->height = tmpConfig->height;

  con->backspace = SaveHostName(tmpConfig->title, port_no);

  if (con->backspace == VK_BACK) {
    tmpConfig->backspace = TRUE;
    con->ctrl_backspace = 0x7f;
  } else {
    tmpConfig->backspace = FALSE;
    con->ctrl_backspace = 0x08;
  }

  tmpConfig->hwndTel = hWnd;
  con->pScreen = InitNewScreen(tmpConfig);
  if (!con->pScreen) {
    assert(FALSE);
    free(con->pScreen);
    free(con);
    free(tmpConfig);
    return(-1);
  }

  ret = (SOCKET) socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (ret == SOCKET_ERROR) {
    wsprintf(buf, "Socket error on socket = %d!", WSAGetLastError());
    MessageBox(NULL, buf, NULL, MB_OK | MB_ICONEXCLAMATION);
    if (con->pScreen != NULL)
      DestroyWindow(con->pScreen->hWnd);
    free(con);
    free(tmpConfig);
    return(-1);
  }

  con->socket = ret;

  sockaddr.sin_family = AF_INET;
  sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  sockaddr.sin_port = htons(0);

  ret = bind(con->socket, (struct sockaddr *) &sockaddr,
	     (int) sizeof(struct sockaddr_in));

  if (ret == SOCKET_ERROR) {
    wsprintf(buf, "Socket error on bind!");
    MessageBox(NULL, buf, NULL, MB_OK | MB_ICONEXCLAMATION);
    if (con->pScreen != NULL)
      DestroyWindow(con->pScreen->hWnd);
    free(con);
    free(tmpConfig);
    return(-1);
  }

  WSAAsyncSelect(con->socket, hWnd, WM_NETWORKEVENT,
		 FD_READ | FD_CLOSE | FD_CONNECT);

  lstrcpy(szHostName, tmpConfig->title);
  p = strchr(szHostName, '@');
  if (p != NULL) {
    *p = 0;
    strcpy (szUserName, szHostName);
    strcpy(szHostName, ++p);
  }

  WSAAsyncGetHostByName(hWnd, WM_HOSTNAMEFOUND, szHostName, hostdata,
			MAXGETHOSTSTRUCT);

  ctl.encrypt = auth_encrypt;
  ctl.decrypt = auth_decrypt;
  ctl.init = auth_init;
  ctl.destroy = auth_destroy;

  con->ks = kstream_create_from_fd(con->socket, &ctl, NULL);

  if (con->ks == NULL)
    return(-1);

  kstream_set_buffer_mode(con->ks, 0);

  return(1);
}


CONNECTION *
GetNewConnection(void)
{
  CONNECTION *pCon;

  pCon = calloc(sizeof(CONNECTION), 1);
  if (pCon == NULL)
    return NULL;
  pCon->backspace = TRUE;
  pCon->bResizeable = TRUE;
  return(pCon);
}


int
DoDialog(char *szDialog, DLGPROC lpfnDlgProc)
{
  int nReturn;

  nReturn = DialogBox(hInst, szDialog, hWnd, lpfnDlgProc);
  return (nReturn);
}


/*
 * FUNCTION: OpenTelnetDlg(HWND, unsigned, WORD, LONG)
 *
 * PURPOSE:  Processes messages for "Open New Telnet Connection" dialog box
 *
 * MESSAGES:
 *
 * WM_INITDIALOG - initialize dialog box
 * WM_COMMAND    - Input received
 */
INT_PTR CALLBACK
OpenTelnetDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  char szConnectName[256];
  HDC hDC;
  int xExt, yExt;
  DWORD Ext;
  HWND hEdit;
  int n;
  int iHostNum = 0;
  char tmpName[128];
  char tmpBuf[80];
  char *tmpCommaLoc;

  switch (message) {
  case WM_INITDIALOG:
    hDC = GetDC(hDlg);
    Ext = GetDialogBaseUnits();
    xExt = (190 *LOWORD(Ext)) /4 ;
    yExt = (72 * HIWORD(Ext)) /8 ;
    GetPrivateProfileString(INI_HOSTS, INI_HOST "0", "", tmpName,
			    128, TELNET_INI);
    if (tmpName[0]) {
      tmpCommaLoc = strchr(tmpName, ',');
      if (tmpCommaLoc)
	*tmpCommaLoc = '\0';
      SetDlgItemText(hDlg, TEL_CONNECT_NAME, tmpName);
    }
    hEdit = GetWindow(GetDlgItem(hDlg, TEL_CONNECT_NAME), GW_CHILD);
    while (TRUE) {
      wsprintf(tmpBuf, INI_HOST "%d", iHostNum++);
      GetPrivateProfileString(INI_HOSTS, tmpBuf, "", tmpName,
			      128, TELNET_INI);
      tmpCommaLoc = strchr(tmpName, ',');
      if (tmpCommaLoc)
	*tmpCommaLoc = '\0';
      if (tmpName[0])
	SendDlgItemMessage(hDlg, TEL_CONNECT_NAME, CB_ADDSTRING, 0,
			   (LPARAM) ((LPSTR) tmpName));
      else
	break;
    }
#ifdef FORWARD
    EnableWindow(GetDlgItem(hDlg, IDC_FORWARD), 1);
    SendDlgItemMessage(hDlg, IDC_FORWARD, BM_SETCHECK, forward_flag, 0);
    if (forward_flag)
      EnableWindow(GetDlgItem(hDlg, IDC_FORWARDFORWARD), 1);
    else
      EnableWindow(GetDlgItem(hDlg, IDC_FORWARDFORWARD), 0);
    SendDlgItemMessage(hDlg, IDC_FORWARDFORWARD, BM_SETCHECK,
		       forwardable_flag, 0);
#endif

#ifdef ENCRYPTION
    EnableWindow(GetDlgItem(hDlg, IDC_ENCRYPT), 1);
    SendDlgItemMessage(hDlg, IDC_ENCRYPT,
		       BM_SETCHECK, encrypt_flag, 0);
#endif

    EnableWindow(GetDlgItem(hDlg, TEL_CONNECT_USERID), 1);

    SetWindowPos(hDlg, NULL,
		 (GetSystemMetrics(SM_CXSCREEN)/2)-(xExt/2),
		 (GetSystemMetrics(SM_CYSCREEN)/2)-(yExt/2),
		 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
    ReleaseDC(hDlg, hDC);
    SendMessage(hEdit, WM_USER + 1, 0, 0);
    SendMessage(hDlg, WM_SETFOCUS, 0, 0);
    return (TRUE);

  case WM_COMMAND:
    switch (wParam) {
    case TEL_CANCEL:
    case IDCANCEL:			/* From the menu */
      EndDialog(hDlg, FALSE);
      break;

#ifdef FORWARD
    case IDC_FORWARD:
      forward_flag = (BOOL)SendDlgItemMessage(hDlg, IDC_FORWARD,
					      BM_GETCHECK, 0, 0);
      if (forward_flag)
	EnableWindow(GetDlgItem(hDlg, IDC_FORWARDFORWARD), 1);
      else
	EnableWindow(GetDlgItem(hDlg, IDC_FORWARDFORWARD), 0);
      break;

    case IDC_FORWARDFORWARD:
      forwardable_flag = (BOOL)SendDlgItemMessage(hDlg, IDC_FORWARDFORWARD,
						  BM_GETCHECK, 0, 0);
      break;
#endif

#if ENCRYPTION
    case IDC_ENCRYPT:
      encrypt_flag = (BOOL)SendDlgItemMessage(hDlg, IDC_ENCRYPT,
					      BM_GETCHECK, 0, 0);
      break;
#endif
    case TEL_CONNECT_USERID:
      GetDlgItemText(hDlg, TEL_CONNECT_USERID, szUserName, sizeof(szUserName));
      break;

    case TEL_OK:
      GetDlgItemText(hDlg, TEL_CONNECT_NAME, szConnectName, 256);

      n = parse_cmdline (szConnectName);
      if (! n) {
	MessageBox(hDlg, "You must enter a session name!",
		   NULL, MB_OK);
	break;
      }
      tmpConfig->title = calloc(lstrlen(szHostName) + 1, 1);
      lstrcpy(tmpConfig->title, szConnectName);
      EndDialog(hDlg, TRUE);
      break;
    }
    return (FALSE);
  }
  return(FALSE);
}


/*
 *
 * FUNCTION: TelnetSend(kstream ks, char *buf, int len, int flags)
 *
 * PURPOSE:	This is a replacement for the WinSock send() function, to
 *		send a buffer of characters to an output socket.  It differs
 *		by retrying endlessly if sending the bytes would cause
 *		the send() to block.  <gnu@cygnus.com> observed EWOULDBLOCK
 *		errors when running using TCP Software's PC/TCP 3.0 stack,
 *		even when writing as little as 109 bytes into a socket
 *		that had no more than 9 bytes queued for output.  Note also
 *		that a kstream is used during output rather than a socket
 *		to facilitate encryption.
 *
 *		Eventually, for cleanliness and responsiveness, this
 *		routine should not loop; instead, if the send doesn't
 *		send all the bytes, it should put them into a buffer
 *		and return.  Message handling code would send out the
 *		buffer whenever it gets an FD_WRITE message.
 */
int
TelnetSend(kstream ks, char *buf, int len, int flags)
{
  int writelen;
  int origlen = len;

  while (TRUE) {
    writelen = kstream_write(ks, buf, len);

    if (writelen == len)	   /* Success, first or Nth time */
      return (origlen);

    if (writelen == SOCKET_ERROR) {
      if (WSAGetLastError() != WSAEWOULDBLOCK)
	return (SOCKET_ERROR);	/* Some error */
      /* For WOULDBLOCK, immediately repeat the send. */
    }
    else {
      /* Partial write; update the pointers and retry. */
      len -= writelen;
      buf += writelen;
    }
  }
}


/*
 * Function: Trim leading and trailing white space from a string.
 *
 * Parameters:
 *	s - the string to trim.
 */
void
trim(char *s)
{
  int l;
  int i;

  for (i = 0; s[i]; i++)
    if (s[i] != ' ' && s[i] != '\t')
      break;

  l = strlen(&s[i]);
  memmove(s, &s[i], l + 1);

  for (l--; l >= 0; l--) {
    if (s[l] != ' ' && s[l] != '\t')
      break;
  }
  s[l + 1] = 0;
}


/*
 *
 * Parse_cmdline
 *
 * Reads hostname and port number off the command line.
 *
 * Formats: telnet
 *          telnet <host>
 *          telnet <host> <port no>
 *          telnet -p <port no>
 *
 * Returns: TRUE if we have a hostname
 */
BOOL
parse_cmdline(char *cmdline)
{
  char *ptr;

  *szHostName = '\0';                 /* Nothing yet */
  if (*cmdline == '\0')               /* Empty command line? */
    return(FALSE);

  trim (cmdline);                     /* Remove excess spaces */
  ptr = strchr (cmdline, ' ');        /* Find 2nd token */

  if (ptr != NULL) {                  /* Port number given */
    *ptr++ = '\0';                  /* Separate into 2 words */
    port_no = atoi (ptr);
  }

  if (*cmdline != '-' && *cmdline != '/') {   /* Host name given */
    lstrcpy (szHostName, cmdline);
    return(TRUE);
  }

  return(FALSE);
}

#ifdef DEBUG
void
hexdump(char *msg, unsigned char *st, int cnt)
{
  int i;
  char strTmp[128];

  OutputDebugString("\r\n");
  if (msg != NULL) {
    OutputDebugString(msg);
    OutputDebugString("\r\n");
  }
  for(i = 0 ; i < cnt ; i++) {
    int j;

    for(j = 0 ; (j < 16) && ((i + j) < cnt) ; j++) {
      wsprintf(strTmp,"%02x  ", st[i + j]);
      if (j == 8)
	OutputDebugString("| ");
      OutputDebugString(strTmp);
    }
    i += j - 1;
    OutputDebugString("\r\n");
  } /* end for */
}
#endif