Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4544034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:36:53+00:00 2026-05-21T15:36:53+00:00

Using this thing I can obtain original destination IP address of socket(PF_INET, SOCK_DGRAM, 0)

  • 0

Using this thing I can obtain original destination IP address of socket(PF_INET, SOCK_DGRAM, 0) socket.

How to get original destination port?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:36:54+00:00Added an answer on May 21, 2026 at 3:36 pm

    Depends on the redirection mechanism. If you are using REDIRECT (which is NAT under the hood), then you need to use SO_ORIGINAL_DST, or libnetfilter_conntrack to query the original destination of the connection before NAT was applied. However since you can serve several connections with the same listener socket, this lookup has to be done for every packet.

    You can experiment with libnetfilter_conntrack and the services it provides using the conntrack command line tool.

    An alterntive is to use TPROXY for the redirection, which was meant to be used in cases like this. There you can get the original destination of the packet using an ancillirary message using recvmsg(). The key to look for is the IP_RECVORIGDST setsockopt.

    More information on TPROXY can be found in the kernel Documentation directory, in a file called tproxy.txt. It is a bit difficult to use, but works more reliably as it is implemented by the stack, rather than the packet filtering subsystem.

    Edited: to add how to query UDP destination addresses with TProxy.

    1. open an UDP socket, bind it to 0.0.0.0 or to a more specific IP
    2. you enable IP_RECVORIGDST via setsockopt(fd, SOL_IP, IP_RECVORIGDSTADDR, …)
    3. you use recvmsg() instead of recvfrom()/recv() to receive frames
    4. recvmsg() will return the packet and a series of ancillary messages,
    5. iterate the ancillary messages, and locate the CMSG block with level SOL_IP, index IP_ORIGDSTADDR
    6. this CMSG block will contain a struct sockaddr_in with both the IP and the port information.

    Edited: SO_ORIGINAL_DST vs. udp

    SO_ORIGINAL_DST should work with udp sockets, however the kernel doesn’t let you specify the connection endpoints, it’ll use the socket that you call SO_ORIGINAL_DST on to get this address information.

    This means that it’ll only work, if the UDP socket is properly bound (to the redirected-to address/port) and connected (to the client in question). Your listener socket is probably bound to 0.0.0.0 and is servicing not just a single, but mulitple clients.

    However, you don’t need to use your actual listener socket to query the destination address. Since since UDP doesn’t transmit datagrams on connection establishment, you can create a new UDP socket, bind it to the redirect address and connect it to the client (whose address you know since it sent the first packet to your listener anyway). Then you could use this socket to run SO_ORIGINAL_DST on, however there are culprits:

    1. once you open such a socket, the kernel will prefer that if the client would send additional packets after the first one, not your listener socket
    2. this is inherently racy, since by the time your application has a chance to call SO_ORIGINAL_DST, the conntrack entry may have timed out.
    3. it is slow and a lot of overhead

    The TProxy based method is clearly better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can draw many things using this : NSString *imagePath = [[NSBundle mainBundle] pathForResource:@dummy2.png
I am using the SQL Server PHP Driver, I think this question can be
I think that this problem can be sorted using reflection (a technology which I'm
I'm using this css file but one thing that is annoying me is the
Recently I was using Mechanize for this kind of thing, but I want to
I have this code (C#): using System.Collections.Generic; namespace ConsoleApplication1 { public struct Thing {
Okay so I can't figure this out. Like a file that I using grep
Whenever I have an application using ActiveRecord I get this ConnectionTimeoutError - but always
I can do this using explode (or have fun with with strrpos ), but
Using Android TelephonyManager an application can obtain the state of data activity over the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.