public inbox for development@lists.ipfire.org
 help / color / mirror / Atom feed
* [PATCH 1/3] lldp.cgi: Add mission validation for description field
@ 2025-11-15 10:07 Stefan Schantl
  2025-11-15 10:07 ` [PATCH 2/3] lldp.cgi: Call binary for peers and do json stuff only if the service is enabled Stefan Schantl
  2025-11-15 10:07 ` [PATCH 3/3] lldp.cgi: Show discovered peers in alphabetical order Stefan Schantl
  0 siblings, 2 replies; 3+ messages in thread
From: Stefan Schantl @ 2025-11-15 10:07 UTC (permalink / raw)
  To: development; +Cc: Stefan Schantl

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 html/cgi-bin/lldp.cgi | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/html/cgi-bin/lldp.cgi b/html/cgi-bin/lldp.cgi
index 755d3dc46..9e30faa92 100644
--- a/html/cgi-bin/lldp.cgi
+++ b/html/cgi-bin/lldp.cgi
@@ -46,8 +46,13 @@ if ($cgiparams{"ACTION"} eq $Lang::tr{'save'}) {
 		$settings{'ENABLED'} = $cgiparams{'ENABLED'};
 	}
 
-	# XXX Validate the description
-	$settings{"DESCRIPTION"} = $cgiparams{"DESCRIPTION"};
+	# Validate the description
+	if (($cgiparams{"DESCRIPTION"} eq "") || ($cgiparams{"DESCRIPTION"} =~ /^[A-Za-z0-9_\-]+$/)) {
+		$settings{"DESCRIPTION"} = $cgiparams{"DESCRIPTION"};
+	} else {
+		# Add error message about invalid characters in description.
+		push(@errormessages, "$Lang::tr{'lldp invalid description'}");
+	}
 
 	# Don't continue on error
 	goto MAIN if (scalar @errormessages);
@@ -81,6 +86,9 @@ MAIN:
 		"ENABLED" => ($settings{"ENABLED"} eq "on") ? "checked" : "",
 	);
 
+	# Description field, defaults to CGI input otherwise use configured description.
+	my $description = $cgiparams{'DESCRIPTION'} // $settings{'DESCRIPTION'};
+
 	print <<END;
 		<form method="POST" action="">
 			<table class="form">
@@ -94,7 +102,7 @@ MAIN:
 				<tr>
 					<td>$Lang::tr{'description'}</td>
 					<td>
-						<input type="text" name="DESCRIPTION" value="$settings{'DESCRIPTION'}" />
+						<input type="text" name="DESCRIPTION" value="$description" />
 					</td>
 				</tr>
 
-- 
2.47.3



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/3] lldp.cgi: Call binary for peers and do json stuff only if the service is enabled
  2025-11-15 10:07 [PATCH 1/3] lldp.cgi: Add mission validation for description field Stefan Schantl
@ 2025-11-15 10:07 ` Stefan Schantl
  2025-11-15 10:07 ` [PATCH 3/3] lldp.cgi: Show discovered peers in alphabetical order Stefan Schantl
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Schantl @ 2025-11-15 10:07 UTC (permalink / raw)
  To: development; +Cc: Stefan Schantl

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 html/cgi-bin/lldp.cgi | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/html/cgi-bin/lldp.cgi b/html/cgi-bin/lldp.cgi
index 9e30faa92..194f4ec82 100644
--- a/html/cgi-bin/lldp.cgi
+++ b/html/cgi-bin/lldp.cgi
@@ -116,21 +116,21 @@ MAIN:
 END
 	&Header::closebox();
 
-	# Load data about all peers
-	my @output = &General::system_output("lldpctl", "-f", "json0");
+	# Show a list with all peers if the service is enabled
+	if ($settings{"ENABLED"} eq "on") {
+		# Load data about all peers
+		my @output = &General::system_output("lldpctl", "-f", "json0");
 
-	my $json;
+		my $json;
 
-	# Parse the JSON output
-	eval {
-		$json = decode_json join("\n", @output);
-		1;
-	} or do {
-		$json = undef;
-	};
+		# Parse the JSON output
+		eval {
+			$json = decode_json join("\n", @output);
+			1;
+		} or do {
+			$json = undef;
+		};
 
-	# Show a list with all peers if the service is enabled
-	if ($settings{"ENABLED"} eq "on") {
 		&Header::opensection($Lang::tr{'lldp neighbors'});
 
 		# Fetch the interface object
-- 
2.47.3



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 3/3] lldp.cgi: Show discovered peers in alphabetical order
  2025-11-15 10:07 [PATCH 1/3] lldp.cgi: Add mission validation for description field Stefan Schantl
  2025-11-15 10:07 ` [PATCH 2/3] lldp.cgi: Call binary for peers and do json stuff only if the service is enabled Stefan Schantl
@ 2025-11-15 10:07 ` Stefan Schantl
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Schantl @ 2025-11-15 10:07 UTC (permalink / raw)
  To: development; +Cc: Stefan Schantl

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 html/cgi-bin/lldp.cgi | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/html/cgi-bin/lldp.cgi b/html/cgi-bin/lldp.cgi
index 194f4ec82..a1b1d4bf9 100644
--- a/html/cgi-bin/lldp.cgi
+++ b/html/cgi-bin/lldp.cgi
@@ -39,6 +39,9 @@ my @errormessages = ();
 my %settings = ();
 &General::readhash("${General::swroot}/lldp/settings", \%settings);
 
+# Hash which will contain any discovered peers.
+my %peerhash = ();
+
 # Save on main page
 if ($cgiparams{"ACTION"} eq $Lang::tr{'save'}) {
 	# Store whether enabled or not
@@ -136,6 +139,14 @@ END
 		# Fetch the interface object
 		my $interface = $json->{"lldp"}[0]->{"interface"};
 
+		# Loop through all detected peers and add their sent names as keys
+		# and their data as values to the peerhash.
+		foreach my $peer (@{ $interface}) {
+			my $name = &Header::escape($peer->{"chassis"}[0]->{"name"}[0]->{"value"});
+
+			$peerhash{$name} = $peer;
+		}
+
 		print <<END;
 			<table class='tbl'>
 				<tr>
@@ -165,16 +176,17 @@ END
 				</tr>
 END
 
-				foreach my $peer (@{ $interface }) {
-					my $intf = $peer->{"name"};
-					my $proto = $peer->{"via"};
+				# Sort the detected peers alphabetically and loop over them.
+				foreach my $peer (sort { $a cmp $b } keys %peerhash) {
+					my $intf = $peerhash{$peer}{"name"};
+					my $proto = $peerhash{$peer}{"via"};
 					my $name = "";
 					my $descr = "";
 					my $port_name = "";
 					my $vlan_id = "";
 
 					# Fetch the chassis
-					foreach my $chassis (@{ $peer->{"chassis"} }) {
+					foreach my $chassis (@{ $peerhash{$peer}{"chassis"} }) {
 						$name = &Header::escape(
 							$chassis->{"name"}[0]->{"value"}
 						);;
@@ -187,12 +199,12 @@ END
 					}
 
 					# Fetch the port
-					foreach my $port (@{ $peer->{"port"} }) {
+					foreach my $port (@{ $peerhash{$peer}{"port"} }) {
 						$port_name = $port->{"descr"}[0]->{"value"};
 					}
 
 					# Fetch the VLAN
-					foreach my $vlan (@{ $peer->{"vlan"} }) {
+					foreach my $vlan (@{ $peerhash{$peer}{"vlan"} }) {
 						$vlan_id = $vlan->{"vlan-id"};
 					}
 
@@ -226,7 +238,7 @@ END
 				}
 
 				# Show a message if there are no neighbors
-				unless (scalar @{ $interface }) {
+				unless (keys %peerhash) {
 					print <<END;
 						<tr>
 							<td colspan="6" style="text-align: center;">
-- 
2.47.3



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-11-15 10:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-15 10:07 [PATCH 1/3] lldp.cgi: Add mission validation for description field Stefan Schantl
2025-11-15 10:07 ` [PATCH 2/3] lldp.cgi: Call binary for peers and do json stuff only if the service is enabled Stefan Schantl
2025-11-15 10:07 ` [PATCH 3/3] lldp.cgi: Show discovered peers in alphabetical order Stefan Schantl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox