Find the answer to your Linux question:
Results 1 to 4 of 4
Hello! I was wondering if anyone had a solution to forwarding to a range. To elaborate, I run a daemon that is unable to handle the userload on one port, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    4

    Port forwarding to random port range


    Hello! I was wondering if anyone had a solution to forwarding to a range. To elaborate, I run a daemon that is unable to handle the userload on one port, so I run 25 processes on 25 different ports, port 1000 -> 1025. I would like to have one stable port number to give out however, so I would like port 800 to randomly forward to 1000 -> 1025 per connection. Any help would make my day!

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    732
    How about balance, a command line load balancing app.

    Grab, compile and then tell it to load balance everything coming into port 800 to many localhost ports.

    Code:
    balance 800 localhost:1000 localhost:1001 <snipped> localhost:1025
    and so on. It goes on a round-robin rather than random, but if that is a problem you could probably hack the source to make it do exactly what you want.

    Let us know how you get on,

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Posts
    4

    Almost!

    Excellent suggestion Chris! It does work however the only problem is now I lose the users IP address when it hits the daemon.

    Any thoughts on how to do this and keep the users IP address in tact?

    Thank you for your help!

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    732
    OK, scratch the easy option.

    Plan B is use some of the load balancing in the firewall. This may not work, some of these features are pretty new, my distro won't do it without some patching for example.

    Anyway, take a look at this advanced iptables article.

    The interesting part would look like this:

    Code:
    iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 800 -m state --state NEW -m nth --counter 0 --every 4 --packet 0 -j DNAT --to-destination 192.168.0.5:1000
    iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 800 -m state --state NEW -m nth --counter 0 --every 4 --packet 1 -j DNAT --to-destination 192.168.0.6:1001
    iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 800 -m state --state NEW -m nth --counter 0 --every 4 --packet 2 -j DNAT --to-destination 192.168.0.7:1002
    iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 800 -m state --state NEW -m nth --counter 0 --every 4 --packet 3 -j DNAT --to-destination 192.168.0.8:1003
    This should set things up for four ports if you adapt the obvious points to match your setup.

    If you get any errors then post them here and we'll see what we can make of them.

    Let us know how you get on,

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •