From: Fredrik Tolf Date: Tue, 29 Mar 2011 06:13:19 +0000 (+0200) Subject: Bind SCGI server to localhost by default, and allow overriding. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=c39ae6d24864244eab704dea7307f769f2f2e9da;p=jsvc.git Bind SCGI server to localhost by default, and allow overriding. --- diff --git a/src/dolda/jsvc/scgi/DirServer.java b/src/dolda/jsvc/scgi/DirServer.java index 209d1a4..ef38a93 100644 --- a/src/dolda/jsvc/scgi/DirServer.java +++ b/src/dolda/jsvc/scgi/DirServer.java @@ -98,17 +98,23 @@ public class DirServer extends Server { } private static void usage(PrintStream out) { - out.println("usage: dolda.jsvc.scgi.DirServer [-h] [-e CHARSET] [-d DATADIR] PORT"); + out.println("usage: dolda.jsvc.scgi.DirServer [-h] [-B BINDADDR] [-e CHARSET] [-d DATADIR] PORT"); } public static void main(String[] args) { - PosixArgs opt = PosixArgs.getopt(args, "he:d:"); + PosixArgs opt = PosixArgs.getopt(args, "he:d:B:"); if(opt == null) { usage(System.err); System.exit(1); } String charset = null; File datroot = null; + InetAddress bindaddr; + try { + bindaddr = InetAddress.getLocalHost(); + } catch(UnknownHostException e) { + throw(new Error("WTF?!", e)); + } for(char c : opt.parsed()) { switch(c) { case 'e': @@ -121,6 +127,14 @@ public class DirServer extends Server { System.exit(1); } break; + case 'B': + try { + bindaddr = InetAddress.getByName(opt.arg); + } catch(UnknownHostException e) { + System.err.println(opt.arg + ": no such host"); + System.exit(1); + } + break; case 'h': usage(System.out); return; @@ -134,10 +148,12 @@ public class DirServer extends Server { env.initvm(); int port = Integer.parseInt(opt.rest[0]); ServerSocket sk; + SocketAddress saddr = new InetSocketAddress(bindaddr, port); try { - sk = new ServerSocket(port); + sk = new ServerSocket(); + sk.bind(saddr); } catch(IOException e) { - System.err.println("could not bind to port " + port + ": " + e.getMessage()); + System.err.println("could not bind to " + saddr + ": " + e.getMessage()); System.exit(1); return; /* Because javac is stupid. :-/ */ }