On Tue, Nov 11, 2008 at 12:20 PM, David Schwartz <[EMAIL PROTECTED]> wrote: > >> Yes. Hence the correct solution would be non-blocking with select()... >> >> Best regards, >> Lutz > > How do you determine (portably) if the socket you got from 'socket' is > inside the legal range for FD_SET? Many platforms, including Linux, will > happilly allow 'socket' to return values that are way out of range for > FD_SET. And FD_SET has no error return.
You are supposed to be able to use the POSIX macro FD_SETSIZE, which is an integer representing the largest file descriptor number that can be indexed in an fd_set type. if( fd <= FD_SETSIZE ) { FD_SET( fd, &read_set ); } else { /* fd is too large! */ } You can also use the sysconf() system call with _SC_OPEN_MAX to get another related upper bound. The fd_set overflow problem can occur whenever the two values are different such that FD_SETSIZE < sysconf( _SC_OPEN_MAX ) Under strict POSIX, this should never be the case, but in practice it occasionally is. I've seen this under HP-UX for instance with certain kernel tuning. What I've done in the past with good portability success it to force the size of the structures to be much larger by using a union. Almost all Unixes just use a packed-bit representation, so as long as you allocate more bytes of storage everything will work. I just pick some much larger maximum, and define my own types. Other than FD_ZERO all the other function macros seem to work just fine on the larger structures in every Unix like system I've seen. #define FD_SETSIZE_LARGE 4096 typedef union large_fd_set_union { fd_set set; unsigned char padding[ (FD_SETSIZE_LARGE/(sizeof(char)*8)) + sizeof(long) ]; } large_fd_set; Then I use them like this: large_fd_set read_set; /* To initialize */ memset( &read_set, 0, sizeof(read_set) ) FD_ZERO( &read_set.set ); /* To set an fd */ FD_SET( fd, &read_set.set ); /* To clear an fd */ FD_CLR( fd, &read_set.set ); /* To test */ if( FD_ISSET( fd, &read_set.set ); -- Deron Meranda ______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List openssl-users@openssl.org Automated List Manager [EMAIL PROTECTED]