4

I am trying to change the source ip address for a tcp packet. Code snippet as given bellow

 bzero(&clientaddr,sizeof(clientaddr));
 clientaddr.sin_family = AF_INET;
 clientaddr.sin_addr.s_addr=inet_addr("172.16.2.10");
 clientaddr.sin_port=htons(8080);
 if (bind(sockfd, (struct sockaddr *) &clientaddr, 
   sizeof(clientaddr)) < 0) 
{
    perror("bind");
}

Binding a particular port is working fine, but when i tried to bind with a diffrent ip adress, the bind is failing with error

bind: Cannot assign requested address

I also tried by setting the socket option as follows,]

setsockopt (sockfd, SOL_IP, IP_TRANSPARENT, &n1, sizeof(int));*

then also its failing with same error.

How could i change my source ip address for packet, originated from my PC. Please help me, its for a proxy application.

OS :Linux 2.6.37-tproxy #1 SMP Wed Apr 3 23:34:00 IST 2013 x86_64 x86_64 x86_64 GNU/Linux

Thanks in advance.

TamiL
  • 2,706
  • 4
  • 24
  • 44

2 Answers2

1

You will need to use a raw socket and create the ip and tcp headers (where you will be able to set the desired IP Address (spoofing).

raw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);

You can check the linux manual page man 7 raw

krouis
  • 129
  • 4
0

You can only bind() to an IP address that is local to your computer, i.e. implemented by one of your network interfaces.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Are you familiar with the Linux transparent proxy functionality? See the tproxy.txt in kernel documenttion and `man 7 ip` at IP_TRANSPARENT option. – thuovila Jul 05 '13 at 14:21