Linux: how to send a TCP packet from a specific port?

How to open a raw socket for sending from a specific TCP port? I want all my connections to always go from a number of ports below the ephemerals.

+2
source share
2 answers

If you use raw sockets, just fill in the correct TCP source port in the packet header.

If you use the TCP socket interface ( socket() , connect() and friends) instead, you can set the source port by calling bind() for the client socket - just like you would set the listening port for the server socket.

+6
source

Creating a tcp connection using raw sockets is somewhere between difficult and impossible; you will need to implement the entire tcp protocol in your program, and also prevent the kernel from sending its own responses to packets (if the kernel has an IP address at this address on this interface).

This is probably not what you want. However, if you want to, it is trivial to send tcp frames with any source port you want, since you can specify it in the tcp header, which, of course, if you implement your own TCP layer, you will need to understand.

+1
source

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


All Articles