src/server/listen.c
int prepareListenerSocket( int port ){
	struct addrinfo *infop = NULL; // Must be initialized to NULL cause it is used as an error flag
	struct addrinfo hint;
	int sockfd = -1; // Must be initialized to -1 cause it is used as an error flag
	int cleanup = 0; // Must be initialized to 0. If it becomes 1 then cleanup must be performed before return from the function
	int status = -1; // Must be initialized to -1. If all ok then it will get the value of the sockfd variable.
	char hostnamebuffer[ 256 ]; // POSIX defines _POSIX_HOST_NAME_MAX as 255 without the terminating nul byte so +1 here
	char portbuffer[ 10 ]; // 10 seems too much but just to be safe...
	const size_t hostnamebuffersize = sizeof( hostnamebuffer ) / sizeof( hostnamebuffer[ 0 ] );
	const size_t portbuffersize = sizeof( portbuffer ) / sizeof( portbuffer[ 0 ] );

	do{
		// Create the socket
		errno = 0;
		if ( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ){ 
			error( "socket() failed in prepareListenerSocket() (%s).\n", strerror( errno ) );
			cleanup = 1; 
			break; 
		}

		// Bind the socket with the address
		( void )memset( portbuffer, 0, portbuffersize );
		( void )memset( hostnamebuffer, 0, hostnamebuffersize );
		( void )memset( &hint, 0, sizeof( hint ) );
		hint.ai_flags = AI_PASSIVE;
		hint.ai_family = AF_INET;
		hint.ai_socktype = SOCK_STREAM;
		hint.ai_protocol = 0;
		if ( gethostname( hostnamebuffer, hostnamebuffersize ) == -1 ){ 
			error( "gethostname() failed in prepareListenerSocket().\n" );
			cleanup = 1; 
			break; 
		}
		errno = 0;
		if ( snprintf( portbuffer, portbuffersize, "%d", port ) == -1 ){ 
			error( "snprintf() failed in prepareListenerSocket() (%s).\n", strerror( errno ) );
			cleanup = 1; 
			break; 
		}
		if ( ( errno = getaddrinfo( hostnamebuffer, portbuffer, &hint, &infop ) ) ){ 
			error( "getaddrinfo() failed in prepareListenerSocket() (%s).\n", gai_strerror( errno ) );
			cleanup = 1; 
			break; 
		}
		errno = 0;
		if ( bind( sockfd, infop->ai_addr, infop->ai_addrlen ) == -1 ){ 
			error( "bind() failed in prepareListenerSocket() (%s).\n", strerror( errno ) );
			cleanup = 1; 
			break; 
		}

		// Make the socket listening for connections
		errno = 0;
		if ( listen( sockfd, SOCKET_BACKLOG ) == -1 ){ 
			error( "listen() failed in prepareListenerSocket() (%s).\n", strerror( errno ) );
			cleanup = 1; 
			break; 
		}

		// If we reach here then all is ok and we must change the value of status to reflect the socket
 		status = sockfd;
	}while ( 0 );

	// Look here for cleaning up
	if ( cleanup ){
		if ( sockfd != -1 ){
			// Not interested in safe_close() here so save errno
			int saved_errno = errno;
			( void )safe_close( sockfd );
			errno = saved_errno;
		}
	}
	// Note that infop must be cleared even if the function was successful	
	if ( infop != NULL ){
		freeaddrinfo( infop );
	}

	// Return the socket. Note that if the socket was not created it has a value of -1
	return ( status );
}
// Create a socket to listen in the local machine at the specified port
Create a socket to listen in the local machine at the specified port
Create a socket to listen in the local machine at the specified port
int prepareListenerSocket( int port ){
	struct addrinfo *infop = NULL; 
	struct addrinfo hint;
	int sockfd = -1; 
	int cleanup = 0; 
	int status = -1; 
	char hostnamebuffer[ 256 ]; 
	char portbuffer[ 10 ]; 
	const size_t hostnamebuffersize = sizeof( hostnamebuffer ) / sizeof( hostnamebuffer[ 0 ] );
	const size_t portbuffersize = sizeof( portbuffer ) / sizeof( portbuffer[ 0 ] );
	do{
		errno = 0;
		if ( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ){ 
			error( "socket() failed in prepareListenerSocket() (%s).\n", strerror( errno ) );
			cleanup = 1; 
			break; 
		}
		( void )memset( portbuffer, 0, portbuffersize );
		( void )memset( hostnamebuffer, 0, hostnamebuffersize );
		( void )memset( &hint, 0, sizeof( hint ) );
		hint.ai_flags = AI_PASSIVE;
		hint.ai_family = AF_INET;
		hint.ai_socktype = SOCK_STREAM;
		hint.ai_protocol = 0;
		if ( gethostname( hostnamebuffer, hostnamebuffersize ) == -1 ){ 
			error( "gethostname() failed in prepareListenerSocket().\n" );
			cleanup = 1; 
			break; 
		}
		errno = 0;
		if ( snprintf( portbuffer, portbuffersize, "%d", port ) == -1 ){ 
			error( "snprintf() failed in prepareListenerSocket() (%s).\n", strerror( errno ) );
			cleanup = 1; 
			break; 
		}
		if ( ( errno = getaddrinfo( hostnamebuffer, portbuffer, &hint, &infop ) ) ){ 
			error( "getaddrinfo() failed in prepareListenerSocket() (%s).\n", gai_strerror( errno ) );
			cleanup = 1; 
			break; 
		}
		errno = 0;
		if ( bind( sockfd, infop->ai_addr, infop->ai_addrlen ) == -1 ){ 
			error( "bind() failed in prepareListenerSocket() (%s).\n", strerror( errno ) );
			cleanup = 1; 
			break; 
		}
		errno = 0;
		if ( listen( sockfd, SOCKET_BACKLOG ) == -1 ){ 
			error( "listen() failed in prepareListenerSocket() (%s).\n", strerror( errno ) );
			cleanup = 1; 
			break; 
		}
 		status = sockfd;
	}while ( 0 );
	if ( cleanup ){
		if ( sockfd != -1 ){
			int saved_errno = errno;
			( void )safe_close( sockfd );
			errno = saved_errno;
		}
	}
	if ( infop != NULL ){
		freeaddrinfo( infop );
	}
	return ( status );
}
int prepareListenerSocket ( int port ) { struct addrinfo * infop = NULL ; struct addrinfo hint ; int sockfd = -1 ; int cleanup = 0 ; int status = -1 ; char hostnamebuffer [ 256 ] ; char portbuffer [ 10 ] ; const size_t hostnamebuffersize = sizeof ( hostnamebuffer ) / sizeof ( hostnamebuffer [ 0 ] ) ; const size_t portbuffersize = sizeof ( portbuffer ) / sizeof ( portbuffer [ 0 ] ) ; do { errno = 0 ; if ( ( sockfd = socket ( AF_INET , SOCK_STREAM , 0 ) ) == -1 ) { error ( " \n " , strerror ( errno ) ) ; cleanup = 1 ; break ; } ( void ) memset ( portbuffer , 0 , portbuffersize ) ; ( void ) memset ( hostnamebuffer , 0 , hostnamebuffersize ) ; ( void ) memset ( & hint , 0 , sizeof ( hint ) ) ; hint . ai_flags = AI_PASSIVE ; hint . ai_family = AF_INET ; hint . ai_socktype = SOCK_STREAM ; hint . ai_protocol = 0 ; if ( gethostname ( hostnamebuffer , hostnamebuffersize ) == -1 ) { error ( " \n " ) ; cleanup = 1 ; break ; } errno = 0 ; if ( snprintf ( portbuffer , portbuffersize , " " , port ) == -1 ) { error ( " \n " , strerror ( errno ) ) ; cleanup = 1 ; break ; } if ( ( errno = getaddrinfo ( hostnamebuffer , portbuffer , & hint , & infop ) ) ) { error ( " \n " , gai_strerror ( errno ) ) ; cleanup = 1 ; break ; } errno = 0 ; if ( bind ( sockfd , infop -> ai_addr , infop -> ai_addrlen ) == -1 ) { error ( " \n " , strerror ( errno ) ) ; cleanup = 1 ; break ; } errno = 0 ; if ( listen ( sockfd , SOCKET_BACKLOG ) == -1 ) { error ( " \n " , strerror ( errno ) ) ; cleanup = 1 ; break ; } status = sockfd ; } while ( 0 ) ; if ( cleanup ) { if ( sockfd != -1 ) { int saved_errno = errno ; ( void ) safe_close ( sockfd ) ; errno = saved_errno ; } } if ( infop != NULL ) { freeaddrinfo ( infop ) ; } return ( status ) ; }
Create a socket to listen in the local machine at the specified port
Create a socket to listen in the local machine at the specified port
// Must be initialized to NULL cause it is used as an error flag // Must be initialized to -1 cause it is used as an error flag // Must be initialized to 0. If it becomes 1 then cleanup must be performed before return from the function // Must be initialized to -1. If all ok then it will get the value of the sockfd variable. // POSIX defines _POSIX_HOST_NAME_MAX as 255 without the terminating nul byte so +1 here // 10 seems too much but just to be safe... // Create the socket // Bind the socket with the address // Make the socket listening for connections // If we reach here then all is ok and we must change the value of status to reflect the socket // Look here for cleaning up // Not interested in safe_close() here so save errno // Note that infop must be cleared even if the function was successful	 // Return the socket. Note that if the socket was not created it has a value of -1