Bind () in tcp client

I am trying to change the source IP address for tcp package. Code snippet below

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 to a specific port works fine, but when I tried to bind to a different IP address, binding to an error

 bind: Cannot assign requested address 

I also tried setting the socket parameter as follows:]

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

and then it crashes with the same error.

How can I change my source IP address for a package created 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.

+4
source share
2 answers

You will need to use the raw socket and create the ip and tcp headers (where you can set the desired IP address (spoofing).

 raw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 

You can check the linux man 7 raw page

+1
source

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

0
source

Source: https://habr.com/ru/post/1489845/


All Articles