Discovery port 8888 integriert; doppelte Datei aus tests entfernt

Corinna
smittythekid 2026-04-08 21:29:45 +02:00
parent 4c5842aa8f
commit 4534883782
2 changed files with 54 additions and 107 deletions

View File

@ -6,7 +6,6 @@ import java.net.InetAddress;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.io.IOException; import java.io.IOException;
public class SyslogServer { public class SyslogServer {
public static void main(String[] args) { public static void main(String[] args) {
int port = 5514; // Default syslog port: 514, but using 5514 to avoid permission issues int port = 5514; // Default syslog port: 514, but using 5514 to avoid permission issues
@ -14,53 +13,88 @@ public class SyslogServer {
server.start(port); server.start(port);
} }
private void runDiscoveryListener() {
try {
DatagramSocket discoverySocket = new DatagramSocket(8888); // Port for discovery
byte[] bufferDiscovery = new byte[256];
System.out.println("Discovery Listener started on port 8888");
while (true) {
DatagramPacket discoveryRequest = new DatagramPacket(bufferDiscovery, bufferDiscovery.length);
discoverySocket.receive(discoveryRequest);
InetAddress clientAddress = discoveryRequest.getAddress();
int clientPort = discoveryRequest.getPort();
System.out.println(
"Discovery request received from: " + clientAddress.getHostAddress() + ":" + clientPort);
// Send a response back to the client
byte[] responseData = "SYSLOG_SERVER".getBytes(StandardCharsets.UTF_8);
DatagramPacket discoveryResponse = new DatagramPacket(
responseData,
responseData.length,
clientAddress,
clientPort);
discoverySocket.send(discoveryResponse);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static final int MAX_MESSAGE_SIZE = 480; private static final int MAX_MESSAGE_SIZE = 480;
public void start(int port) { public void start(int port) {
System.out.println("Syslog Server started on port " + port); System.out.println("Syslog Server started on port " + port);
// Discovery Listener in a separate thread
new Thread(() -> runDiscoveryListener()).start();
try {
try{
// Create a DatagramSocket to listen for incoming messages // Create a DatagramSocket to listen for incoming messages
DatagramSocket socket = new DatagramSocket(port); DatagramSocket socket = new DatagramSocket(port);
// Buffer to hold incoming messages // Buffer to hold incoming messages
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
while(true){ while (true) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length); DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Wait for a message to be received (blocking call) // Wait for a message to be received (blocking call)
socket.receive(packet); socket.receive(packet);
// Extract the message from the packet + data; how many bytes were actually received // Extract the message from the packet + data; how many bytes were actually
// received
int length = packet.getLength(); int length = packet.getLength();
if (length > MAX_MESSAGE_SIZE) { if (length > MAX_MESSAGE_SIZE) {
System.err.println("Received message exceeds maximum allowed size of " + MAX_MESSAGE_SIZE + " bytes. Message will be ignored."); System.err.println("Received message exceeds maximum allowed size of " + MAX_MESSAGE_SIZE
+ " bytes. Message will be ignored.");
continue; // Skip processing this message continue; // Skip processing this message
} }
String message = new String( String message = new String(
packet.getData(), // complete byte array be aware: packet.getData() returns the entire buffer, not just the received data packet.getData(), // complete byte array be aware: packet.getData() returns the entire buffer,
// not just the received data
packet.getOffset(), // get the offset where the data starts packet.getOffset(), // get the offset where the data starts
length, length,
StandardCharsets.UTF_8); // Convert the byte array to a string using UTF-8 encoding StandardCharsets.UTF_8); // Convert the byte array to a string using UTF-8 encoding
System.out.println("Nachricht received: " + message);
// Show Client IP and Port // Show Client IP and Port
InetAddress clientAddress = packet.getAddress(); InetAddress clientAddress = packet.getAddress();
int clientPort = packet.getPort(); int clientPort = packet.getPort();
System.out.println("From: " + clientAddress.getHostAddress() + ":" + clientPort); System.out.println("From: " + clientAddress.getHostAddress() + ":" + clientPort);
System.out.println("Message received: " + message);
} }
} }
catch(IOException e){ catch (IOException e) {
System.err.println("Could not start server: " + e.getMessage()); System.err.println("Could not start server: " + e.getMessage());
return; return;
} }

View File

@ -1,87 +0,0 @@
package vs;
/**
* helper class for RFC 5424 (https://datatracker.ietf.org/doc/html/rfc5424)
* compliant log messages as immutable Java objects - representation of a subset
* of printable strings of specific length
*
* @author Sandro Leuchter
*
*/
public abstract class AsciiChars {
private final String value;
public String value() {
return this.value;
}
protected AsciiChars(int length, String value) {
if (value != null) {
if (value.length() > length) {
throw new IllegalArgumentException(
"Stringlänge = " + value.length() + " > " + length
);
}
for (int c : value.getBytes()) {
if (c < 33 || c > 126) {
throw new IllegalArgumentException(
"Stringinhalt nicht printable US-ASCII ohne Space"
);
}
}
}
this.value = value;
}
@Override
public String toString() {
if (value() == null || value().length() == 0) {
return "-";
} else {
return value();
}
}
public static final class L004 extends AsciiChars {
public L004(String value) {
super(4, value);
}
}
public static final class L012 extends AsciiChars {
public L012(String value) {
super(12, value);
}
}
public static final class L032 extends AsciiChars {
public L032(String value) {
super(32, value);
}
}
public static final class L048 extends AsciiChars {
public L048(String value) {
super(48, value);
}
}
public static final class L128 extends AsciiChars {
public L128(String value) {
super(128, value);
}
}
public static final class L255 extends AsciiChars {
public L255(String value) {
super(255, value);
}
}
}