Get original destination IP and port with Linux 2.4 iptables redirect?

Discussion in 'Perl' started by Lincoln Yeoh, Nov 12, 2003.

  1. Lincoln Yeoh

    Lincoln Yeoh Guest

    Sorry to repost this but I still haven't figured it out and there
    weren't any responses.
    ---
    Say I use iptables to redirect tcp connections to my perl proxy
    servers. How then do I get the original destination IP address and tcp
    port?

    On FreeBSD I just use ipfw and fwd and then following works:
    $daddr=$client->sockhost;
    $dport=$client->sockport;

    And then my various proxies work transparently.

    But on Linux I'm supposed to use some FD options:
    e.g.
    getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, &dst_addr, &slen)

    What's a good way to do this with perl? Working examples would be very
    helpful.

    I've tried perl's getsockopt but replacing OPTNAME with
    SO_ORIGINAL_DST doesn't work - it's not defined.

    perl -f getsockopt
    getsockopt SOCKET,LEVEL,OPTNAME

    I've tried specifying a numerical 80 for OPTNAME but not sure how to
    get the address etc.

    Thanks,
    Link.
    Lincoln Yeoh, Nov 12, 2003
    #1
    1. Advertising

  2. Lincoln Yeoh

    Ben Morrow Guest

    Lincoln Yeoh <> wrote:
    > Say I use iptables to redirect tcp connections to my perl proxy
    > servers. How then do I get the original destination IP address and tcp
    > port?
    >
    > On FreeBSD I just use ipfw and fwd and then following works:
    > $daddr=$client->sockhost;
    > $dport=$client->sockport;
    >
    > And then my various proxies work transparently.
    >
    > But on Linux I'm supposed to use some FD options:
    > e.g.
    > getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, &dst_addr, &slen)
    >
    > What's a good way to do this with perl? Working examples would be very
    > helpful.
    >
    > I've tried perl's getsockopt but replacing OPTNAME with
    > SO_ORIGINAL_DST doesn't work - it's not defined.
    >
    > perl -f getsockopt
    > getsockopt SOCKET,LEVEL,OPTNAME
    >
    > I've tried specifying a numerical 80 for OPTNAME but not sure how to
    > get the address etc.


    The call in Perl is the same as C, except that return values are
    returned instead of being passed by reference. So

    use Socket qw/inet_ntoa/;

    my $packed_addr = getsockopt $SOCK, SOL_IP, SO_ORIGINAL_DST;
    my $addr = inet_ntoa $packed_addr;

    (untested) should work. To get the values of the constants you will
    need to poke around in your headers... on my machine, SOL_IP is
    defined to be 0 in <bits/in.h> and SO_ORIGINAL_DST to be 80 in
    <linux/netfilter_ipv4/ip_nat.h>. You could try throwing those headers
    at h2ph, just for a laugh :), or you could just put use constant
    statements at the top of your program.

    Ben

    --
    EAT
    KIDS (...er, whoops...)
    FOR
    99p
    Ben Morrow, Nov 12, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Wolfe
    Replies:
    1
    Views:
    2,236
    Joerg Jooss
    Apr 28, 2005
  2. Replies:
    1
    Views:
    1,689
    Esmond Pitt
    May 13, 2007
  3. Lincoln Yeoh
    Replies:
    0
    Views:
    96
    Lincoln Yeoh
    Oct 17, 2003
  4. Lincoln Yeoh
    Replies:
    1
    Views:
    168
    Ben Morrow
    Nov 12, 2003
  5. Replies:
    3
    Views:
    122
Loading...

Share This Page