Bug 40990

Summary: Cannot bind port or ip address for outgoing UDP socket when using SysLogAppender
Product: Log4j Reporter: C N <cn.monkey.1125>
Component: AppenderAssignee: log4j-dev <log4j-dev>
Status: NEEDINFO ---    
Severity: normal CC: thorbjoern
Priority: P2 Keywords: JDK1.5
Version: 1.2   
Target Milestone: ---   
Hardware: Other   
OS: Linux   

Description C N 2006-11-17 00:35:08 UTC
I am currently using Log4J with SysLogAppender with the following 
configuration file:

The configuration file I used included the following:
----------------------------------------------------------
log4j.appender.syslog2=org.apache.log4j.net.SyslogAppender
log4j.appender.syslog2.SyslogHost=localhost
log4j.appender.syslog2.Facility=local2
log4j.appender.syslog2.layout=org.apache.log4j.PatternLayout
log4j.appender.syslog2.layout.ConversionPattern=%p  (%L) -%m%n
log4j.rootLogger=DEBUG, syslog2
----------------------------------------------------------

I noticed that a random UDP port is being opened by the Java application as 
shown:

---------------------------------------------------------------------------
linux:~ # netstat -lpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address   Foreign Address  State PID/Program name
udp        0      0 127.0.0.1:514   0.0.0.0:*              15994/syslog-ng
udp        0      0 :::21457        :::*                   1364/java
---------------------------------------------------------------------------

For security purposes, I would like to be able to bind this socket to a 
configurable port and ip address. 

I am currently using 1.2.14. Looking at the source code, I noticed that the 
SysLogWriter currently uses the default constructor DatagramSocket() which 
assigns a random port number and is bounded to a wildcard address.

SysLogWriter.java (line 100 - 107):

    try {
      this.ds = new DatagramSocket();
    }
    catch (SocketException e) {
      e.printStackTrace(); 
      LogLog.error("Could not instantiate DatagramSocket to " + host +
			 ". All logging will FAIL.", e);
    }

I suggest that a fix should be implemented to use a parameterized constructor 
instead such that users may set configurations such that the socket can be 
bounded to a user-defined port and/or local address.
Comment 1 Elias Ross 2007-01-28 04:29:53 UTC
Agree this is important, especially with multi-homed machines.
Comment 2 Thorbjørn Ravn Andersen 2008-07-03 12:28:03 UTC
(In reply to comment #0)


> I suggest that a fix should be implemented to use a parameterized constructor 
> instead such that users may set configurations such that the socket can be 
> bounded to a user-defined port and/or local address.

Is it possible to write a testcase for this?  If so, would you do so?

What would be reasonable names for these parameters?  port and inetaddress?