James Carlson wrote:

<snip>

> 847: not your code ... but what does '& 0xffff' accomplish here?

This is the following piece of code (current version of netcat.c):

     784                /* Randomly swap ports. */
     785                if (rflag) {
     786                        int y;
     787                        char *c;
     788
     789                        srandom(time((time_t *)0));
     790
     791                        for (x = 0; x <= (hi - lo); x++) {
     792                                y = (random() & 0xFFFF) % (hi - lo);
     793                                c = portlist[x];
     794                                portlist[x] = portlist[y];
     795                                portlist[y] = c;
     796                        }
     797                }

random(3C) returns long which is 4 bytes in /usr/bin/nc (nc is delivered 
only as 32-bit prog). This means (random() & 0xFFFF) will be just the 2 
less significant bytes of the 4 returned by random().

The purpose of the expression on line 792 is preparation for permuting 
the numbers in (lo,hi) interval. It seems that the goal of the binary 
operation is to cap the random number returned from random() to 16 bits 
(so it fits in the range of usable TCP/UDP ports) and then further limit 
it with the range 'hi - lo'.

Although without the binary operation this will remain functional 
(thanks to the modulo operation), IMHO the idea is logical and should be 
preserved. There is probably a better way how to do it, though. (cast to 
in_port_t ?)


v.
_______________________________________________
opensolaris-code mailing list
opensolaris-code@opensolaris.org
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

Reply via email to