Something I wish I knew how to do years ago – SO_ORIGINAL_DST – Proxy Related

Note to future self, something that I’ve been doing completely inefficiently in the past, getting the destination IP address of a redirected packet from iptables/linux in an official manner!

Edit: I was trying to read this code block snippet and I’m not sure how the source address is used here other than the TCP connection socket file descriptor possibly used which might mean it doesn’t work for UDP redirects…?

https://github.com/darkk/redsocks/blob/master/base.c#L216

static int getdestaddr_iptables(int fd, const struct sockaddr_in *client, const struct sockaddr_in *bindaddr, struct sockaddr_in *destaddr)
{
    socklen_t socklen = sizeof(*destaddr);
    int error;

    error = getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, destaddr, &socklen);
    if (error) {
        log_errno(LOG_WARNING, "getsockopt");
        return -1;
    }
    return 0;
}

Leave a comment