X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=src%2Fnetwork.c;h=b953882f613a0355c0057552dfbffde17d5bb439;hb=a6c4c0c8d8224cdc455f0d184e91d0cf881f0077;hp=e264205525e34ca3432137938cc382be3dcebd25;hpb=36eddd2c06ce9b09c83102353deb213e065e901d;p=dockapps%2Fwmnotify.git diff --git a/src/network.c b/src/network.c index e264205..b953882 100644 --- a/src/network.c +++ b/src/network.c @@ -52,202 +52,206 @@ char tx_buffer[WMNOTIFY_BUFSIZE + 1]; char rx_buffer[WMNOTIFY_BUFSIZE + 1]; -int -SocketOpen( char *server_name, int port ) +int SocketOpen(char *server_name, int port) { - int status; - int sock_fd = -1; - struct hostent *hostinfo; - struct sockaddr_in serv_addr; - - hostinfo = gethostbyname(server_name); - if( hostinfo == NULL ) { - herror( PACKAGE ); - ErrorLocation( __FILE__, __LINE__ ); - goto error; - } - - /* Open socket for Stream (TCP) */ - sock_fd = socket( PF_INET, SOCK_STREAM, 0 ); - if( sock_fd < 0 ) { - perror( PACKAGE ); - ErrorLocation( __FILE__, __LINE__ ); - goto error; - } - + int status; + int sock_fd = -1; + struct hostent *hostinfo; + struct sockaddr_in serv_addr; + + hostinfo = gethostbyname(server_name); + if (hostinfo == NULL) { + herror(PACKAGE); + ErrorLocation(__FILE__, __LINE__); + goto error; + } + + /* Open socket for Stream (TCP) */ + sock_fd = socket(PF_INET, SOCK_STREAM, 0); + if (sock_fd < 0) { + perror(PACKAGE); + ErrorLocation(__FILE__, __LINE__); + goto error; + } + /*---Initialize server address/port struct---*/ - serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(port); - serv_addr.sin_addr = *((struct in_addr *) hostinfo->h_addr ); - memset( &( serv_addr.sin_zero ), '\0', 8 ); /* Clear the rest of the structure. */ - - if( wmnotify_infos.debug ) { - printf( " Server IP = %s\n", inet_ntoa( serv_addr.sin_addr ) ); - printf( " Server port = %d\n", ntohs(serv_addr.sin_port) ); - } - - /* Establishing connection. */ - status = connect( sock_fd, (struct sockaddr *) &(serv_addr), sizeof(serv_addr) ); - if( status < 0 ) { - perror( PACKAGE ); - ErrorLocation( __FILE__, __LINE__ ); - goto error; - } - - end: - return sock_fd; - - error: - if( sock_fd >= 0 ) { - status = close( sock_fd ); - if( status < 0 ) { - perror( PACKAGE ); - ErrorLocation( __FILE__, __LINE__ ); - } - } - - sock_fd = -1; - goto end; + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + serv_addr.sin_addr = *((struct in_addr *)hostinfo->h_addr); + memset(&(serv_addr.sin_zero), '\0', 8); /* Clear rest of structure. */ + + if (wmnotify_infos.debug) { + printf(" Server IP = %s\n", + inet_ntoa(serv_addr.sin_addr)); + printf(" Server port = %d\n", ntohs(serv_addr.sin_port)); + } + + /* Establishing connection. */ + status = connect(sock_fd, (struct sockaddr *)&(serv_addr), + sizeof(serv_addr)); + if (status < 0) { + perror(PACKAGE); + ErrorLocation(__FILE__, __LINE__); + goto error; + } + +end: + return sock_fd; + +error: + if (sock_fd >= 0) { + status = close(sock_fd); + if (status < 0) { + perror(PACKAGE); + ErrorLocation(__FILE__, __LINE__); + } + } + + sock_fd = -1; + goto end; } -int -ConnectionEstablish( char *server_name, int port ) +int ConnectionEstablish(char *server_name, int port) { - int len; - char rx_buffer[1024]; /* Temporary... */ + int len; + char rx_buffer[1024]; /* Temporary... */ - wmnotify_infos.sock_fd = SocketOpen( wmnotify_infos.server_name, wmnotify_infos.port ); - if( wmnotify_infos.sock_fd < 0 ) { - goto error; - } + wmnotify_infos.sock_fd = + SocketOpen(wmnotify_infos.server_name, wmnotify_infos.port); + if (wmnotify_infos.sock_fd < 0) + goto error; #if HAVE_SSL - if( wmnotify_infos.use_ssl == true ) { - int status; - status = InitSSL( wmnotify_infos.sock_fd ); - if( status != EXIT_SUCCESS ) { - goto error; - } - } -#endif - - /* Testing connection. */ - len = WmnotifyGetResponse( rx_buffer, 1024 ); - if( len < 0 ) { - goto error; - } - - if( wmnotify_infos.debug ) { - rx_buffer[len] = 0; - printf(" Connect response:\n%s\n", rx_buffer ); - } - - return EXIT_SUCCESS; - - error: - return EXIT_FAILURE; + if (wmnotify_infos.use_ssl == true) { + int status; + status = InitSSL(wmnotify_infos.sock_fd); + if (status != EXIT_SUCCESS) + goto error; + } +#endif + + /* Testing connection. */ + len = WmnotifyGetResponse(rx_buffer, 1024); + if (len < 0) + goto error; + + if (wmnotify_infos.debug) { + rx_buffer[len] = 0; + printf(" Connect response:\n%s\n", rx_buffer); + } + + return EXIT_SUCCESS; + +error: + return EXIT_FAILURE; } -int -ConnectionTerminate( void ) +int ConnectionTerminate(void) { #if HAVE_SSL - if( wmnotify_infos.use_ssl == true ) { - SSL_free( ssl_infos.ssl ); /* release connection state */ - } + if (wmnotify_infos.use_ssl == true) + SSL_free(ssl_infos.ssl); /* release connection state */ #endif - close( wmnotify_infos.sock_fd ); /* close socket */ + close(wmnotify_infos.sock_fd); /* close socket */ #if HAVE_SSL - if( wmnotify_infos.use_ssl == true ) { - SSL_CTX_free( ssl_infos.ctx ); /* release context */ - } + if (wmnotify_infos.use_ssl == true) + SSL_CTX_free(ssl_infos.ctx); /* release context */ #endif - - return EXIT_SUCCESS; + + return EXIT_SUCCESS; } -int -WmnotifySendData( char *buffer, int size ) +int WmnotifySendData(char *buffer, int size) { - int len; + int len; #if HAVE_SSL - if( wmnotify_infos.use_ssl == true ) { - len = SSL_write( ssl_infos.ssl, buffer, size ); /* Encrypt & send message */ - if( len <= 0 ) { - SSL_get_error( ssl_infos.ssl, len ); - len = -1; - } - - return len; - } -#endif /* HAVE_SSL */ - - /* if errno = EINTR, it means the operation was interrupted by a signal before any data was - * sent. We must retry the operation in this case. */ - do { - len = send( wmnotify_infos.sock_fd, buffer, size, SEND_FLAGS ); - } - while( ( len < 0 ) && ( errno == EINTR ) ); - - if( len < 0 ) { - perror( PACKAGE ); - ErrorLocation( __FILE__, __LINE__ ); - } - - return len; + if (wmnotify_infos.use_ssl == true) { + /* Encrypt & send message */ + len = SSL_write(ssl_infos.ssl, buffer, size); + if (len <= 0) { + SSL_get_error(ssl_infos.ssl, len); + len = -1; + } + + return len; + } +#endif /* HAVE_SSL */ + + /* + * if errno = EINTR, it means the operation was interrupted by a signal + * before any data was sent. We must retry the operation in this case. + */ + do { + len = send(wmnotify_infos.sock_fd, buffer, size, SEND_FLAGS); + } while ((len < 0) && (errno == EINTR)); + + if (len < 0) { + perror(PACKAGE); + ErrorLocation(__FILE__, __LINE__); + } + + return len; } -int -WmnotifyGetResponse( char *buffer, int max_size ) +int WmnotifyGetResponse(char *buffer, int max_size) { - int len; + int len; #if HAVE_SSL - if( wmnotify_infos.use_ssl == true ) { - len = SSL_read( ssl_infos.ssl, buffer, max_size ); /* Get reply & decrypt. */ - switch( SSL_get_error( ssl_infos.ssl, len ) ) { - case SSL_ERROR_NONE: - /* Success. */ - break; - case SSL_ERROR_ZERO_RETURN: - fprintf( stderr, "%s: SSL_read() connection closed.\n", PACKAGE ); - break; - case SSL_ERROR_SYSCALL: - fprintf( stderr, "%s: SSL_read() I/O error.\n", PACKAGE ); - goto ssl_error; - case SSL_ERROR_SSL: - fprintf( stderr, "%s: SSL_read() protocol error.\n", PACKAGE ); - goto ssl_error; - default: - fprintf( stderr, "%s: SSL_read() error.\n", PACKAGE ); - goto ssl_error; - } - - return len; - - ssl_error: - return -1; - } + if (wmnotify_infos.use_ssl == true) { + /* Get reply & decrypt. */ + len = SSL_read(ssl_infos.ssl, buffer, max_size); + switch (SSL_get_error(ssl_infos.ssl, len)) { + case SSL_ERROR_NONE: + /* Success. */ + break; + case SSL_ERROR_ZERO_RETURN: + fprintf(stderr, + "%s: SSL_read() connection closed.\n", + PACKAGE); + break; + case SSL_ERROR_SYSCALL: + fprintf(stderr, "%s: SSL_read() I/O error.\n", + PACKAGE); + goto ssl_error; + case SSL_ERROR_SSL: + fprintf(stderr, "%s: SSL_read() protocol error.\n", + PACKAGE); + goto ssl_error; + default: + fprintf(stderr, "%s: SSL_read() error.\n", + PACKAGE); + goto ssl_error; + } + + return len; + +ssl_error: + return -1; + } #endif /* HAVE_SSL */ - /* if errno = EINTR, it means the operation was interrupted by a signal before any data was - * read. We must retry the operation in this case. */ - do { - len = recv( wmnotify_infos.sock_fd, buffer, max_size, RECV_FLAGS ); - } - while( ( len < 0 ) && ( errno == EINTR ) ); - - if( len < 0 ) { - perror( PACKAGE ); - ErrorLocation( __FILE__, __LINE__ ); - } - - return len; + /* + * if errno = EINTR, it means the operation was interrupted by a signal + * before any data was read. We must retry the operation in this case. + */ + do { + len = + recv(wmnotify_infos.sock_fd, buffer, max_size, + RECV_FLAGS); + } while ((len < 0) && (errno == EINTR)); + + if (len < 0) { + perror(PACKAGE); + ErrorLocation(__FILE__, __LINE__); + } + + return len; }