Reputation: 3082
How to open a raw socket for sending from specific TCP port? I want to have all my connections always go from a range of ports below ephemerals.
Upvotes: 3
Views: 5687
Reputation: 63538
Making a tcp connection using raw sockets is somewhere between difficult and impossible; you'd need to implement the entire tcp protocol in your program AND also stop the kernel from sending its own replies to the packets (if the kernel has IP bound on that address on that interface).
This is probably not what you want. However, if you did want it, it is trivial to send tcp frames with any source port you want, as you get to specify it in the tcp header, which of course, if you're implementing your own TCP layer, you'll need to understand.
Upvotes: 1
Reputation: 86333
If you are using raw sockets, then just fill in the correct TCP source port in the packet header.
If, instead, you are using the TCP socket interface (socket()
, connect()
and friends), then you can set the source port by calling the bind()
system call for the client socket - exactly as you would to set the listening port for the server socket.
Upvotes: 6