Längenprüfung für Nachricht integriert

Corinna
smittythekid 2026-04-08 20:41:21 +02:00
parent 5099a7b4d6
commit 4c5842aa8f
2 changed files with 17 additions and 2 deletions

View File

@ -2,7 +2,12 @@ Dokumentation Befehle für Konsole
mvn exec:java -Dexec.mainClass="vs.SyslogServer"
> startet den SyslogServer
> Strg + C um Server im Terminal aus Endlosschleife zu beenden
echo "test" | nc -u 127.0.0.1 5514
> Test ob Server auf dem Port 5514 aktiv zuhört; True --> "Nachricht empfangen!"
echo "test" | nc -u -w1 127.0.0.1 5514
> Test ob Server auf dem Port 5514 aktiv zuhört und Nachricht korrekt ankommt
> w1 damit nach einer Sekunde beendet wird, netcat (nc) wartet bei UDP manchmal auf eine Antwort
python3 -c "print('A'*600)" | nc -u -w1 127.0.0.1 5514
> Test Maximallänge überschritten

View File

@ -14,9 +14,13 @@ public class SyslogServer {
server.start(port);
}
private static final int MAX_MESSAGE_SIZE = 480;
public void start(int port) {
System.out.println("Syslog Server started on port " + port);
try{
// Create a DatagramSocket to listen for incoming messages
DatagramSocket socket = new DatagramSocket(port);
@ -32,6 +36,12 @@ public class SyslogServer {
// Extract the message from the packet + data; how many bytes were actually received
int length = packet.getLength();
if (length > MAX_MESSAGE_SIZE) {
System.err.println("Received message exceeds maximum allowed size of " + MAX_MESSAGE_SIZE + " bytes. Message will be ignored.");
continue; // Skip processing this message
}
String message = new String(
packet.getData(), // complete byte array be aware: packet.getData() returns the entire buffer, not just the received data