From 5a908bb1d7b182fdaa534bc8c600788cfa3549a5 Mon Sep 17 00:00:00 2001
From: Simon Tatham <anakin@pobox.com>
Date: Sun, 7 Jan 2001 15:12:20 +0000
Subject: [PATCH] Apply `getservbyname' to the Port Number field in case it's
 non-numeric. Patch due to Christian Biesinger.

[originally from svn r839]
---
 windlg.c | 14 ++++++++++++--
 window.c |  8 ++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/windlg.c b/windlg.c
index 0ffe3c9d..aa483465 100644
--- a/windlg.c
+++ b/windlg.c
@@ -598,6 +598,8 @@ static int GenericMainDlgProc (HWND hwnd, UINT msg,
     CHOOSEFONT cf;
     LOGFONT lf;
     char fontstatic[256];
+    char portname[32];
+    struct servent * service;
     int i;
 
     switch (msg) {
@@ -1105,8 +1107,16 @@ static int GenericMainDlgProc (HWND hwnd, UINT msg,
 				sizeof(cfg.host)-1);
 	    break;
 	  case IDC_PORT:
-	    if (HIWORD(wParam) == EN_CHANGE)
-		MyGetDlgItemInt (hwnd, IDC_PORT, &cfg.port);
+	    if (HIWORD(wParam) == EN_CHANGE) {
+		GetDlgItemText (hwnd, IDC_PORT, portname, 31);
+		if (isdigit(portname[0]))
+		    MyGetDlgItemInt (hwnd, IDC_PORT, &cfg.port);
+		else {
+		    service = getservbyname(portname, NULL);
+		    if (service) cfg.port = ntohs(service->s_port);
+		    else cfg.port = 0;
+		}
+	    }
 	    break;
 	  case IDC_SESSEDIT:
 	    if (HIWORD(wParam) == EN_CHANGE) {
diff --git a/window.c b/window.c
index 2797c53e..46d9f7ee 100644
--- a/window.c
+++ b/window.c
@@ -314,6 +314,14 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
         }
     }
 
+    /* Check for invalid Port number (i.e. zero) */
+    if (cfg.port == 0) {
+        MessageBox(NULL, "Invalid Port Number",
+		   "PuTTY Internal Error", MB_OK |MB_ICONEXCLAMATION);
+        WSACleanup();
+        return 1;
+    }
+
     real_ldisc = (cfg.ldisc_term ? &ldisc_term : &ldisc_simple);
     /* To start with, we use the simple line discipline, so we can
      * type passwords etc without fear of them being echoed... */