-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Daniel Quinlan writes: > [EMAIL PROTECTED] writes: > > > What's the probability that I run into an already used port with the > > new probably_unused_spamd_port() code? Less than 1 per mill? Ask > > Murphy... > > The only chance of a collision is if the port is listed in > /etc/services. My system only has 3 TCP ports above 32768 listed. So > if my math is right, that's a 0.0000003% chance of a collision between > two processes. The purely random code had a 0.1% chance of a collision > between two processes (running at the same time which could happen), > mostly because it only used 1000 ports. A 32768-port random version > would have a 0.003% chance of a collision. > > > The routine now tries to ask netstat if that port is already in use. > > I tested the pattern on Linux, FreeBSD and Windows. If netstat can't > > be run, no harm is done, the routine will just work as before. The > > grep is pretty broad, it might also catch a remote port; then it just > > tries the next random one. (Hey Murphy, it really can't hit a used > > port ten times, can it?) > > I'm not a big fan of shell calls, but it looks (untested) like it'll > work on Windows XP too. wow guys -- overkill ;) I think both approaches are wrong. Firstly, checking services seems pointless, because if you ask me, there's actually a *low* likelihood that processes listening on high ports will be listed in /etc/services at all. Here's why: 1. I've heard of very few "official" services on ports > 32768 in general. So I'd surmise that if one is running, the user who started it just picked a port at random. 2. typically a daemon running on a high port will be something that was started by a user instead of "root", and users don't have write perms on /etc/services. Finally, mss' approach is wrong because it's too inefficient, requiring (another) command be forked every time a t script starts. easier, portable way to check if a port is in use: use Socket to connect() to it, and regenerate a new port if the connect succeeds. No fork overhead, no portability worries. - --j. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) Comment: Exmh CVS iD8DBQFBp9mIMJF5cimLx9ARAn4YAJkB/aTNG9Gm/oGcV+53CVwQnWRiEACgtdkE c/A9EwOAKmpB+b+vmyscqgA= =MO4Z -----END PGP SIGNATURE-----