For example: Firefox uses random local port numbers to connect to websites. (Dozens of different local ports can be used at any given time.) Is it possible to force a program to only use specific local ports ? (Force only the local port, the target port could be any.)
-
1Is it a program that you are writing or an existing program? I don't think you can do this without changing the source code of the program. Also, why do you want to do this? Keep in mind that the reason that TCP/IP clients use random local ports is so that they don't have to deal with a port being busy. If you assign the program to only use a specific port, only one process will be able to make a request at one time.– Zhehao MaoJun 22, 2011 at 15:45
-
1Why? <extrachars>– Shane MaddenJun 22, 2011 at 15:45
-
@Shane: Web-browsers try to concurrently fetch multiple resources (images, CSS, JS etc) for a page. To do this they need multiple connections, each TCP connection to the same server (& to port 80 say) needs a different source port otherwise the individual connections cannot be distinguished.– RedGrittyBrickJun 22, 2011 at 16:02
-
@RedGrittyBrick Sorry - didn't make that clear, but my "why" was in response to the original post, not @Zhehao's comment.– Shane MaddenJun 22, 2011 at 16:23
-
@Shane: Ahh - I misunderstood, sorry.– RedGrittyBrickJun 22, 2011 at 16:26
2 Answers
I don't know of any way to do this short of modifying the source code, and even then, Firefox may be calling closed netcode on Windows (ie, it might be using Visual C++ libraries).
I'm confused why you'd want to do this. Most firewalls have different rules for outgoing and incoming connections, and limiting outgoing ports is quite unusual as they're listening only for traffic from a specific TCP session. Destination ports are much more security sensitive as they are open and listening with no established sessions.
Write a library that intercepts and rewrites the bind(2)
call, and use $LD_PRELOAD
to load it underneath the application of choice.