Test code for init_socket (ignore the bogus return at the end for now):
int init_socket( int mudport )
{
struct addrinfo hints, *res, *ptr;
char mport[MIL];
int sock[16];
int rc, count=0;
int x = 1;
memset( &hints, 0, sizeof hints );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
snprintf( mport, MIL, "%d", mudport );
if( (rc = getaddrinfo( NULL, mport, &hints, &res )) < 0 )
{
perror( "Init_socket: getaddrinfo" );
exit(1);
}
ptr = res;
while( ptr )
{
log_printf( "Count: %d", count );
log_printf( "Family: %d, Socktype: %d, Proto: %d", ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
if( (sock[count] = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol )) < 0 )
{
perror( "Init_socket: socket" );
exit(1);
}
log_printf( "Sock: %d", sock[count] );
if( setsockopt( sock[count], SOL_SOCKET, SO_REUSEADDR, ( void * )&x, sizeof( x ) ) < 0 )
{
perror( "Init_socket: SO_REUSEADDR" );
close( sock[count] );
exit( 1 );
}
if( bind( sock[count], ptr->ai_addr, ptr->ai_addrlen ) < 0 )
{
perror( "Init_socket: bind" );
close( sock[count] );
exit( 1 );
}
if( listen( sock[count], 50 ) < 0 )
{
perror( "Init_socket: listen" );
close( sock[count] );
exit( 1 );
}
count++;
ptr = ptr->ai_next;
}
return sock[count];
}
Result:
Thu Dec 29 03:45:58 2011 :: Initializing socket
Thu Dec 29 03:45:58 2011 :: Count: 0
Thu Dec 29 03:45:58 2011 :: Family: 2, Socktype: 1, Proto: 6
Thu Dec 29 03:45:58 2011 :: Sock: 3
Thu Dec 29 03:45:58 2011 :: Count: 1
Thu Dec 29 03:45:58 2011 :: Family: 10, Socktype: 1, Proto: 6
Thu Dec 29 03:45:58 2011 :: Sock: 4
Init_socket: bind: Address already in use
So it's back to the same old crap as before. Not allowing to bind everything to one port, despite the obvious fact that other services on the box are doing so without issue. Something is clearly missing here but I have no idea what.