From 35d758647de0ee799a03ac8a0633e3d873b055f6 Mon Sep 17 00:00:00 2001
From: Uwe Risse <dg2ycb@gmx.de>
Date: Sun, 23 Oct 2022 13:05:29 +0200
Subject: [PATCH] Check the network address of the UDP server for errors before
 accepting it. Thanks to Mike W9MDB.

---
 Configuration.cpp         | 24 ++++++++++++++++++++++++
 Network/MessageClient.cpp |  1 +
 2 files changed, 25 insertions(+)

diff --git a/Configuration.cpp b/Configuration.cpp
index e45bc99af..919d13400 100644
--- a/Configuration.cpp
+++ b/Configuration.cpp
@@ -1388,7 +1388,9 @@ void Configuration::impl::initialize_models ()
   ui_->TX_audio_source_button_group->button (rig_params_.audio_source)->setChecked (true);
   ui_->CAT_poll_interval_spin_box->setValue (rig_params_.poll_interval);
   ui_->opCallEntry->setText (opCall_);
+  ui_->udp_server_line_edit->setEnabled(false);
   ui_->udp_server_line_edit->setText (udp_server_name_);
+  ui_->udp_server_line_edit->setEnabled(true);
   on_udp_server_line_edit_editingFinished ();
   ui_->udp_server_port_spin_box->setValue (udp_server_port_);
   load_network_interfaces (ui_->udp_interfaces_combo_box, udp_interface_names_);
@@ -2449,6 +2451,27 @@ void Configuration::impl::on_udp_server_line_edit_textChanged (QString const&)
 
 void Configuration::impl::on_udp_server_line_edit_editingFinished ()
 {
+  if (this->isVisible())
+  {
+    int q1,q2,q3,q4;
+    char tmpbuf[2];
+    int n = sscanf(ui_->udp_server_line_edit->text ().trimmed ().toLatin1(), "%d.%d.%d.%d.%1s", &q1, &q2, &q3, &q4, tmpbuf);
+    const char *iperr;
+    switch(n)
+    {
+      case 0: iperr = "Error before first number";break;
+      case 1: iperr = "Error between first and second number";break;
+      case 2: iperr = "Error between second and third number";break;
+      case 3: iperr = "Error between third and fourth number";break;
+      case 4: iperr = ""; break;
+      default: iperr = "Unknown error n=" + n;
+    }
+    if (n != 4)
+    {
+       MessageBox::warning_message (this, tr ("Error in network address"), tr (iperr));
+       return;
+    }
+
   if (udp_server_name_edited_)
     {
       auto const& server = ui_->udp_server_line_edit->text ().trimmed ();
@@ -2468,6 +2491,7 @@ void Configuration::impl::on_udp_server_line_edit_editingFinished ()
           check_multicast (ha);
         }
     }
+  }
 }
 
 void Configuration::impl::host_info_results (QHostInfo host_info)
diff --git a/Network/MessageClient.cpp b/Network/MessageClient.cpp
index 94f564bc5..1991f1593 100644
--- a/Network/MessageClient.cpp
+++ b/Network/MessageClient.cpp
@@ -137,6 +137,7 @@ void MessageClient::impl::host_info_results (QHostInfo host_info)
   if (QHostInfo::NoError != host_info.error ())
     {
       Q_EMIT self_->error ("UDP server DNS lookup failed: " + host_info.errorString ());
+      return;
     }
   else
     {