2009年11月25日 星期三

Netfilter: Look up real destination IP address and port number after NATed

A user space server (or proxy daemon) may need to know the original IP address and port number of a connection before the connection is NATed (usually port redirection). This can be done easily with Netfilter/Linux. The steps are:
  1. accept() a new connection: c = accept(s, (struct sockaddr*) &sin, &sinlen)
  2. getsockopt(c, &sin, SOL_IP, SO_ORIGINAL_DST, &sin, &tsinlen).
The original destination IP address and port number can be then extracted from sin.sin_addr and sin.sin_port.

2 則留言: