Home » Uncategorized » Linksys WRT54GL automated DHCP renew

Linksys WRT54GL automated DHCP renew

RouterResetter I’ve been having trouble with my Internet connection at home – from time to time the connection stalled and I had to make router renew IP via DHCP to get connection back (any my router is Linksys WRT54GL).

Don’t know whose fault it is – router or provider (I blame provider because this tends to happen more often at night, and I don’t think my router has a clock in it (-; ), but I’m not that good at networks to find the problem and fix it (especially if it’s at provider side – I can never convince them that something that happens “once in a while… oh wait, it just happened, yes!” (-: is their fault).

So I decided to make a little program that would do this for me – check Internet connection on timely basis, and make router renew IP if connection is dead (by sending POST request to it’s web interface page), so I wouldn’t have to do it myself (and also could leave downloads for night (-: ).

Which language/technology to choose? (Under Linux you could’ve think of some shell or Perl script for this, but for Windows the choice is not so obvious. And for OS X?) Of-course I’ve chosen Java – because I like it and I write mostly in it, but also for crossplatformness (-:

And as my router is configured to present it’s web interface on HTTPS this is also a good opportunity to learn/tell a bit about working with HTTPS and security certificates in Java.

So, the main tasks were:
– write Java code that could do authentication on Router web-interface via HTTPS and sending POST request with some params to “apply.cgi” page there (most tricky);
– code for checking Internet connection (getting Google’s main page and checking it for HTTP status 2xx and “Google” seems Ok way);
– glue code for this all + GUI and related stuff (necessary but irrelevant for our little lesson).

Getting ahead, here’s the final code: http://mvmn.pastebin.com/f3db229e7
It uses Commons HTTP Client 3.1, and thus depends on commons-httpclient-3.1.jar and it’s dependencies, which are apache-mime4j-0.6.jar, commons-codec-1.3.jar and commons-logging-1.1.1.jar.

If you’re not a developer but you need this stuff, you can get ZIP with the program packaged in JAR file here. But you’ll have to download the four abovementioned JARs and put in same folder where you’ll unzip the wrt54gl-resetter.jar. To make it easier, here are the links:
1. commons-httpclient-3.1.jar
2. apache-mime4j-0.6.jar
3. commons-codec-1.3.jar
4. commons-logging-1.1.1.jar
(note: links link to ZIP files, you’ll have to unzip JARs out of them).

So what about SSL certificates and Java? If your certificate is not recognized you’ll have to download install certificate in keystore, but this is not what we want to do, right? Any dirty and ugly way to skip SSL certificate check is good for us – we just want this little utility work without other hassle.

Well, this is described already for example in this article – you have to make your own instance of javax.net.ssl.X509TrustManager, let’s say acceptAllX509TrustManager, which will trust everything. OK so far, but what’s next?

Next comes something like this:

javax.net.ssl.SSLContext context = javax.net.ssl.SSLContext.getInstance("SSL");
context.init(null, acceptAllX509TrustManager, new SecureRandom());
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());

We’re making javax.net.ssl.SSLContext with our acceptAllX509TrustManager, so far so good, and then we register it’s SocketFactory as javax.net.ssl.HttpsURLConnection’s DefaultSSLSocketFactory. Not good – we’re supposed to use Commons HTTPClient, so this socket factory won’t be used. What to do?

The way that I’ve found is to create new org.apache.commons.httpclient.protocol.Protocol that will use org.apache.commons.httpclient.protocol.ProtocolSocketFactory instance as wrapper for above mentioned javax.net.ssl.SSLSocketFactory, and register it using Protocol.registerProtocol().

The code is:

Protocol myhttps = new Protocol("https", new ProtocolSocketFactory() {
SSLSocketFactory socketFactory;public ProtocolSocketFactory setSocketFactory(SSLSocketFactory socketFactory) {
this.socketFactory = socketFactory;
return this;
}public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return socketFactory.createSocket(host, port);
}

public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException, UnknownHostException {
return socketFactory.createSocket(host, port, localAddress, localPort);
}

public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException,
ConnectTimeoutException {
return socketFactory.createSocket(host, port, localAddress, localPort);
}

}.setSocketFactory(context.getSocketFactory()), 443);

Protocol.registerProtocol("https", myhttps);

Ok, looks like complete equivalent of abovementioned code but for HTTPClient’s SSLSocketFactory. And it works as expected – great!

Final words: nicely packaged as JAR file with MANIFEST.MF mentioning all dependencies this util+libs can be easily (re)distributed and used under all J2SE 5 supporting platforms. Enjoy.

Advertisements

14 thoughts on “Linksys WRT54GL automated DHCP renew

  1. Hello,
    this is a wonderful application! I am using Linksys router WRT54G. Nevertheless I decided to try your program. I was able to start the applet and it found the router and everything but when the connection drops it is unable to re-establish it? Is it because of the different router model? Are there any settings that can be changed in POST request by an amateur user like me in order to succeed in releasing and renewing the DHCP of the router.
    Regards,
    Bob

    • Hello Boris,

      Yes, this must be because of version. We can try to fix it, but I will need your help because I don’t have router of your version.

      Do you know what Firebug is? If you could use it to look up request+parameters set to your router when you click “DHCP renew” button in your router web-interface, I believe I’d be able to modify my program accordingly, so it would work for your version.

      • Thank you very much for your response! I’ll try to do what you asked and will send you the results. As I am on holiday now I will write you back in few days. Thank you again.
        Boris

        • No problem. I believe it’d be just a small change, so no problem to make it at all.
          When you’re back from vacation, write me a comment if you need some hints on Firebug and what to do with it.

          • Hello again! I am back from my holiday and hope that we can solve my problem :)
            I have installed firebug add-on. But it seems that I need your help now! Where exactly can I find request and parameters as on firebug window there are so many tabs? Can you tell me what exactly should I do when pressing Renew DHCP button.
            Thank you very much,
            Regards,
            Bob
            Regards,
            Bob

  2. Actually I could find something in Net tab and there are also several sub-tabs. I noticed 5 requests in HTML subtab after clicking the Renew button. Is it what you are looking for? And how can I send you the results? Regards,
    Bob

    • (-:
      Hi Bob,
      Just when I was typing about the Net tab you’ve found it. Cool (-:
      It all should be pretty easy actually.

      As you see on Net tab, first request goes for the page, and rest for CSS stylesheets, JS JavaScript files, images etc. To see request details you just click the triangle near it (on the left before URL). So open the first request – inside there are tabs, you’ll probably see “Parameters” and “Headers” – the first ones.

      What you have to do is just make a screenshot of “Parameters” and “Headers” tabs, put it to image hosting and post here the link. (Or email me to google’o’mail, “sauron” dot “inbox”).

      That’s all, I’ll take care of the rest.

      Regards, Mykola.

    • Nice!

      The “conn_stats” parameter puzzled me a bit, but let’s try without it first – maybe it’s not needed.

      Download this JAR: http://cachexy.ho.com.ua/dl/wrt54g-resetter.jar , and put it instead of wrt54gl-resetter.jar (delete this one) in folder where you have rest of JARs (apache-mime4j-0.6.jar, commons-codec-1.3.jar etc).
      And try it! (-:

      If it won’t work, please also send me source of the page where renew button is. I’ll try to parse out value for “conn_stats” parameter from there.

      Regards, Mykola.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s