From 5d8cbff94734b78fda5701c68b9b01580501bafa Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Sun, 19 Jul 2009 22:51:05 +0000 Subject: [PATCH] 2009-07-20 00:48 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) * contrib/hbssl/ssl.c + contrib/hbssl/hbssl.c * contrib/hbssl/Makefile + HB_SSL_READ_ALL() and HB_SSL_READ_LINE() rewritten. (HB_SSL_READ_ALL() isn't fully optimal, and maybe there are errors, please check.) + Added SSL_GET_RFD(), SSL_GET_WFD(). * include/Makefile + include/hbapinet.h * source/rtl/hbinet.c + Moved some basic declarations to new API header, mainly to export hb_select[Read|Write]*() functions. We will also need such header in the future when we implement C level socket/networking API. Current solution is quite messy since the header has to pull Windows headers which may not be friendly with all usages/include order. * source/rtl/hbinet.c * HB_SOCKET_STRUCT renamed to HB_SOCKET. + Added PHB_SOCKET type. + Added hb_selectReadFD(), hb_selectWriteFD() which are similar to hb_selectReadSocket() and hb_selectWriteSocket() but expect raw FD plus explicit timeout values. These function names and solutions are tentative to solve SSL integration with Harbour, and hopefully in the future we will have a clean net API, the current one is very messy. * source/vm/itemapi.c ! hb_itemPutCPtr(), hb_itemPutCLPtr() fixed to put empty string to item if szText is NULL and length is non-zero, instead of trying to free NULL pointer causing internal error. * include/hbextlng.ch * Corrected header to be in sync with CDP and not to repeat the filename. --- harbour/ChangeLog | 42 +++++++ harbour/contrib/hbssl/Makefile | 1 + harbour/contrib/hbssl/hbssl.c | 205 +++++++++++++++++++++++++++++++ harbour/contrib/hbssl/ssl.c | 215 ++++----------------------------- harbour/include/Makefile | 1 + harbour/include/hbapinet.h | 96 +++++++++++++++ harbour/include/hbextlng.ch | 2 +- harbour/source/rtl/hbinet.c | 165 +++++++++++++++---------- harbour/source/vm/itemapi.c | 10 +- 9 files changed, 476 insertions(+), 261 deletions(-) create mode 100644 harbour/contrib/hbssl/hbssl.c create mode 100644 harbour/include/hbapinet.h diff --git a/harbour/ChangeLog b/harbour/ChangeLog index be7a2ba726..70dab44d19 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -17,6 +17,48 @@ past entries belonging to author(s): Viktor Szakats. */ +2009-07-20 00:48 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) + * contrib/hbssl/ssl.c + + contrib/hbssl/hbssl.c + * contrib/hbssl/Makefile + + HB_SSL_READ_ALL() and HB_SSL_READ_LINE() rewritten. + (HB_SSL_READ_ALL() isn't fully optimal, and maybe there + are errors, please check.) + + Added SSL_GET_RFD(), SSL_GET_WFD(). + + * include/Makefile + + include/hbapinet.h + * source/rtl/hbinet.c + + Moved some basic declarations to new API header, + mainly to export hb_select[Read|Write]*() functions. + We will also need such header in the future when + we implement C level socket/networking API. Current + solution is quite messy since the header has to + pull Windows headers which may not be friendly with + all usages/include order. + + * source/rtl/hbinet.c + * HB_SOCKET_STRUCT renamed to HB_SOCKET. + + Added PHB_SOCKET type. + + Added hb_selectReadFD(), hb_selectWriteFD() which + are similar to hb_selectReadSocket() and + hb_selectWriteSocket() but expect raw FD plus explicit + timeout values. + These function names and solutions are tentative + to solve SSL integration with Harbour, and hopefully + in the future we will have a clean net API, the current + one is very messy. + + * source/vm/itemapi.c + ! hb_itemPutCPtr(), hb_itemPutCLPtr() fixed to put empty + string to item if szText is NULL and length is non-zero, + instead of trying to free NULL pointer causing internal + error. + + * include/hbextlng.ch + * Corrected header to be in sync with CDP and not + to repeat the filename. + 2009-07-19 13:56 UTC-0800 Pritpal Bedi (pritpal@vouchcac.com) * contrib/hbqt/generator/hbqtgen.prg * contrib/hbqt/generator/qt45.qtp diff --git a/harbour/contrib/hbssl/Makefile b/harbour/contrib/hbssl/Makefile index 78ea83ea55..4c711c73a8 100644 --- a/harbour/contrib/hbssl/Makefile +++ b/harbour/contrib/hbssl/Makefile @@ -26,6 +26,7 @@ ifneq ($(strip $(HB_INC_OPENSSL_OK)),) HB_USER_CFLAGS += $(foreach d, $(HB_INC_OPENSSL_OK), -I$(d)) C_SOURCES=\ + hbssl.c \ ssl.c \ sslbio.c \ sslciph.c \ diff --git a/harbour/contrib/hbssl/hbssl.c b/harbour/contrib/hbssl/hbssl.c new file mode 100644 index 0000000000..ec0f72e041 --- /dev/null +++ b/harbour/contrib/hbssl/hbssl.c @@ -0,0 +1,205 @@ +/* + * $Id$ + */ + +/* + * Harbour Project source code: + * OpenSSL API (SSL) - Harbour extensions + * + * Copyright 2009 Viktor Szakats (harbour.01 syenar.hu) + * www - http://www.harbour-project.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/). + * + * As a special exception, the Harbour Project gives permission for + * additional uses of the text contained in its release of Harbour. + * + * The exception is that, if you link the Harbour libraries with other + * files to produce an executable, this does not by itself cause the + * resulting executable to be covered by the GNU General Public License. + * Your use of that executable is in no way restricted on account of + * linking the Harbour library code into it. + * + * This exception does not however invalidate any other reasons why + * the executable file might be covered by the GNU General Public License. + * + * This exception applies only to the code released by the Harbour + * Project under the name Harbour. If you copy code from other + * Harbour Project or Free Software Foundation releases into a copy of + * Harbour, as the General Public License permits, the exception does + * not apply to the code that you add in this way. To avoid misleading + * anyone as to the status of such modified files, you must delete + * this exception notice from them. + * + * If you write modifications of your own for Harbour, it is your choice + * whether to permit this exception to apply to your modifications. + * If you do not wish that, delete this exception notice. + * + */ + +#include "hbapi.h" +#include "hbapierr.h" +#include "hbapiitm.h" +#include "hbapinet.h" +#include "hbvm.h" + +#include "hbssl.h" + +#if defined( HB_API_NET ) + +HB_FUNC( HB_SSL_READ_ALL ) +{ + if( hb_SSL_is( 1 ) ) + { + SSL * ssl = hb_SSL_par( 1 ); + + if( ssl ) + { + int iMax = ISNUM( 3 ) ? hb_parni( 3 ) : INT_MAX; + int iTimeout = ISNUM( 4 ) ? hb_parni( 4 ) : -1; + int iBufferSize = HB_ISNUM( 5 ) ? hb_parni( 5 ) : 80; + + int iPos = 0; + int iAllocated = 0; + char * retval = NULL; + + for( ;; ) + { + char buffer[ 1 ]; + int iLen; + + if( SSL_pending( ssl ) || + hb_selectReadFD( ( HB_SOCKET_T ) SSL_get_fd( ssl ), iTimeout ) ) + { + iLen = SSL_read( ssl, &buffer, 1 ); + + if( iLen == SSL_ERROR_WANT_READ ) + continue; + } + else + break; + + if( iLen <= 0 ) + { + if( retval ) + hb_xfree( retval ); + + hb_storc( NULL, 2 ); + hb_retni( iLen ); + return; + } + + if( iPos == iAllocated ) + { + iAllocated += iBufferSize; + retval = ( char * ) hb_xrealloc( retval, iAllocated ); + } + + retval[ iPos++ ] = buffer[ 0 ]; + + if( iPos == iMax ) + break; + } + + if( retval ) + { + if( ! hb_storclen_buffer( retval, iPos, 2 ) ) + hb_xfree( retval ); + } + else + hb_storc( NULL, 2 ); + + hb_retni( iPos ); + } + } + else + hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); +} + +HB_FUNC( HB_SSL_READ_LINE ) +{ + if( hb_SSL_is( 1 ) ) + { + SSL * ssl = hb_SSL_par( 1 ); + + if( ssl ) + { + int iMax = ISNUM( 3 ) ? hb_parni( 3 ) : INT_MAX; + int iTimeout = ISNUM( 4 ) ? hb_parni( 4 ) : -1; + int iBufferSize = HB_ISNUM( 5 ) ? hb_parni( 5 ) : 80; + + int iPos = 0; + int iAllocated = 0; + char * retval = NULL; + + for( ;; ) + { + char buffer[ 1 ]; + int iLen; + + if( SSL_pending( ssl ) || + hb_selectReadFD( ( HB_SOCKET_T ) SSL_get_fd( ssl ), iTimeout ) ) + { + iLen = SSL_read( ssl, &buffer, 1 ); + + if( iLen == SSL_ERROR_WANT_READ ) + continue; + } + else + break; + + if( iLen <= 0 ) + { + if( retval ) + hb_xfree( retval ); + + hb_storc( NULL, 2 ); + hb_retni( iLen ); + return; + } + else if( buffer[ 0 ] == '\r' ) + continue; + else if( buffer[ 0 ] == '\n' ) + break; + + if( iPos == iAllocated ) + { + iAllocated += iBufferSize; + retval = ( char * ) hb_xrealloc( retval, iAllocated ); + } + + retval[ iPos++ ] = buffer[ 0 ]; + + if( iPos == iMax ) + break; + } + + if( retval ) + { + if( ! hb_storclen_buffer( retval, iPos, 2 ) ) + hb_xfree( retval ); + } + else + hb_storc( NULL, 2 ); + + hb_retni( iPos ); + } + } + else + hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); +} + +#endif diff --git a/harbour/contrib/hbssl/ssl.c b/harbour/contrib/hbssl/ssl.c index f2079c644c..00b30cca5f 100644 --- a/harbour/contrib/hbssl/ssl.c +++ b/harbour/contrib/hbssl/ssl.c @@ -53,7 +53,6 @@ #include "hbapi.h" #include "hbapierr.h" #include "hbapiitm.h" -#include "hbvm.h" #include "hbssl.h" @@ -423,194 +422,6 @@ HB_FUNC( SSL_READ ) hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); } -/* Based on HB_INETRECVALL() */ -HB_FUNC( HB_SSL_READ_ALL ) -{ - fprintf( stdout, "ALL: SSL_READ: ENTER\n" ); - fflush( stdout ); - - if( hb_SSL_is( 1 ) ) - { - SSL * ssl = hb_SSL_par( 1 ); - - if( ssl ) - { - PHB_ITEM pBuffer = hb_param( 2, HB_IT_STRING ); - char * buffer; - ULONG ulLen; - int iLen, iMaxLen, iReceived; - int iTimeElapsed, iTimeout = hb_parni( 4 ); - - if( hb_itemGetWriteCL( pBuffer, &buffer, &ulLen ) ) - iLen = ( int ) ulLen; - else - { - iLen = 0; - buffer = NULL; - } - - if( HB_ISNUM( 3 ) ) - { - iMaxLen = hb_parni( 3 ); - if( iLen < iMaxLen ) - iMaxLen = iLen; - } - else - iMaxLen = iLen; - - hb_vmUnlock(); - - iReceived = 0; - iTimeElapsed = 0; - - fprintf( stdout, "ALL: SSL_READ: START\n" ); - fflush( stdout ); - - do - { - if( SSL_pending( ssl ) > 0 ) - { - iLen = SSL_read( ssl, buffer + iReceived, iMaxLen - iReceived ); - - fprintf( stdout, "ALL: SSL_READ: %d\n", iLen ); - fflush( stdout ); - - if( iLen > 0 ) - iReceived += iLen; - } - else - { - iTimeElapsed += iTimeout; - - hb_vmLock(); - - hb_retni( iReceived ); - return; - } - } - while( iReceived < iMaxLen && iLen > 0 ); - - hb_vmLock(); - - if( iLen == 0 ) - hb_retni( iLen ); - else if( iLen < 0 ) - hb_retni( iLen ); - else - hb_retni( iReceived ); - } - } - else - hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); -} - -/* Based on HB_INETRECVLINE() */ -HB_FUNC( HB_SSL_READ_LINE ) -{ - fprintf( stdout, "ALL: SSL_READ_LINE: ENTER\n" ); - fflush( stdout ); - - if( hb_SSL_is( 1 ) ) - { - SSL * ssl = hb_SSL_par( 1 ); - - if( ssl ) - { - static const char * szPattern = "\r\n"; - - PHB_ITEM pResult = hb_param( 2, HB_IT_BYREF ); - PHB_ITEM pMaxSize = hb_param( 3, HB_IT_NUMERIC ); - PHB_ITEM pBufferSize = hb_param( 4, HB_IT_NUMERIC ); - - char cChar = '\0'; - char * Buffer; - int iAllocated, iBufferSize, iMax; - int iLen = 0, iPatLen; - int iPos = 0, iTimeElapsed, iTimeout = hb_parni( 4 ); - - iBufferSize = pBufferSize ? hb_itemGetNI( pBufferSize ) : 80; - iMax = pMaxSize ? hb_itemGetNI( pMaxSize ) : 0; - - Buffer = ( char * ) hb_xgrab( iBufferSize ); - iAllocated = iBufferSize; - iTimeElapsed = 0; - iPatLen = ( int ) strlen( szPattern ); - - fprintf( stdout, "ALL: SSL_READ_LINE: START\n" ); - fflush( stdout ); - - do - { - if( iPos == iAllocated - 1 ) - { - iAllocated += iBufferSize; - Buffer = ( char * ) hb_xrealloc( Buffer, iAllocated ); - } - - fprintf( stdout, "ALL: SSL_READ_LINE: BEFORE READ\n" ); - fflush( stdout ); - - if( SSL_pending( ssl ) > 0 ) - { - iLen = SSL_read( ssl, &cChar, 1 ); - - fprintf( stdout, "ALL: SSL_READ_LINE: %d\n", iLen ); - fflush( stdout ); - - if( iLen > 0 ) - { - Buffer[ iPos++ ] = cChar; - /* verify endsequence recognition automata status */ - if( iPos >= iPatLen && - memcmp( Buffer + iPos - iPatLen, szPattern, iPatLen ) == 0 ) - { - break; - } - } - else - { - iTimeElapsed += iTimeout; - break; - } - } - else - break; - } - while( iMax == 0 || iPos < iMax ); - - if( iLen <= 0 ) - { - if( pResult ) - hb_itemPutNI( pResult, iLen ); - - hb_xfree( Buffer ); - } - else - { - if( iMax == 0 || iPos < iMax ) - { - iPos -= iPatLen; - - if( pResult ) - hb_itemPutNI( pResult, iPos ); - - hb_retclen_buffer( Buffer, iPos ); - } - else - { - if( pResult ) - hb_itemPutNI( pResult, -2 ); - - hb_xfree( Buffer ); - hb_retc_null(); - } - } - } - } - else - hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); -} - HB_FUNC( SSL_PEEK ) { if( hb_SSL_is( 1 ) ) @@ -973,6 +784,32 @@ HB_FUNC( SSL_GET_FD ) hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); } +HB_FUNC( SSL_GET_RFD ) +{ + if( hb_SSL_is( 1 ) ) + { + SSL * ssl = hb_SSL_par( 1 ); + + if( ssl ) + hb_retni( SSL_get_rfd( ssl ) ); + } + else + hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); +} + +HB_FUNC( SSL_GET_WFD ) +{ + if( hb_SSL_is( 1 ) ) + { + SSL * ssl = hb_SSL_par( 1 ); + + if( ssl ) + hb_retni( SSL_get_wfd( ssl ) ); + } + else + hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); +} + HB_FUNC( SSL_GET_QUIET_SHUTDOWN ) { if( hb_SSL_is( 1 ) ) diff --git a/harbour/include/Makefile b/harbour/include/Makefile index 8e5f8b8a26..4921ec9205 100644 --- a/harbour/include/Makefile +++ b/harbour/include/Makefile @@ -16,6 +16,7 @@ C_HEADERS=\ hbapigt.h \ hbapiitm.h \ hbapilng.h \ + hbapinet.h \ hbapirdd.h \ hbassert.h \ hbatomic.h \ diff --git a/harbour/include/hbapinet.h b/harbour/include/hbapinet.h new file mode 100644 index 0000000000..ba5ec52e01 --- /dev/null +++ b/harbour/include/hbapinet.h @@ -0,0 +1,96 @@ +/* + * $Id$ + */ + +/* + * Harbour Project source code: + * Networking API + * + * Copyright 2009 Viktor Szakats (harbour.01 syenar.hu) + * www - http://www.harbour-project.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/). + * + * As a special exception, the Harbour Project gives permission for + * additional uses of the text contained in its release of Harbour. + * + * The exception is that, if you link the Harbour libraries with other + * files to produce an executable, this does not by itself cause the + * resulting executable to be covered by the GNU General Public License. + * Your use of that executable is in no way restricted on account of + * linking the Harbour library code into it. + * + * This exception does not however invalidate any other reasons why + * the executable file might be covered by the GNU General Public License. + * + * This exception applies only to the code released by the Harbour + * Project under the name Harbour. If you copy code from other + * Harbour Project or Free Software Foundation releases into a copy of + * Harbour, as the General Public License permits, the exception does + * not apply to the code that you add in this way. To avoid misleading + * anyone as to the status of such modified files, you must delete + * this exception notice from them. + * + * If you write modifications of your own for Harbour, it is your choice + * whether to permit this exception to apply to your modifications. + * If you do not wish that, delete this exception notice. + * + */ + +#ifndef HB_APINET_H_ +#define HB_APINET_H_ + +#include "hbapi.h" +#include "hbapiitm.h" + +#if ! defined( HB_OS_DOS ) + +#define HB_API_NET + +HB_EXTERN_BEGIN + +#if defined( HB_OS_WIN ) + #define _WINSOCKAPI_ /* Prevents inclusion of winsock.h in windows.h */ + #define HB_SOCKET_T SOCKET + #include + #include +#else + #define HB_SOCKET_T int +#endif + +typedef struct _HB_SOCKET +{ + HB_SOCKET_T com; + struct sockaddr_in remote; + char szErrorText[ 128 ]; + const char * pszErrorText; + int iErrorCode; + int iCount; + int iTimeout; + int iTimeLimit; + PHB_ITEM pPeriodicBlock; +} HB_SOCKET, * PHB_SOCKET; + +extern int hb_selectReadFD( HB_SOCKET_T fd, int iTimeout ); +extern int hb_selectWriteFD( HB_SOCKET_T fd, int iTimeout ); +extern int hb_selectReadSocket( PHB_SOCKET pSocket ); +extern int hb_selectWriteSocket( PHB_SOCKET pSocket ); + +HB_EXTERN_END + +#endif + +#endif /* HB_APINET_H_ */ diff --git a/harbour/include/hbextlng.ch b/harbour/include/hbextlng.ch index 246fbb7e36..f219be42b7 100644 --- a/harbour/include/hbextlng.ch +++ b/harbour/include/hbextlng.ch @@ -4,7 +4,7 @@ /* * Harbour Project source code: - * Harbour hbextlng.ch contrib external header + * All HB_LANG_* externals. * * Copyright 2009 April White * www - http://www.harbour-project.org diff --git a/harbour/source/rtl/hbinet.c b/harbour/source/rtl/hbinet.c index b3705f1afc..daa0903287 100644 --- a/harbour/source/rtl/hbinet.c +++ b/harbour/source/rtl/hbinet.c @@ -62,6 +62,7 @@ #include "hbapi.h" #include "hbapiitm.h" +#include "hbapinet.h" #include "hbapierr.h" #include "hbvm.h" @@ -82,11 +83,6 @@ #include #if defined( HB_OS_WIN ) - #define _WINSOCKAPI_ /* Prevents inclusion of winsock.h in windows.h */ - #define HB_SOCKET_T SOCKET - #include - #include - #define HB_INET_CLOSE( x ) closesocket( x ) #else @@ -94,8 +90,6 @@ #define _XOPEN_SOURCE_EXTENDED #endif - #define HB_SOCKET_T int - #include #if defined( HB_OS_OS2 ) #if defined( __WATCOMC__ ) @@ -125,20 +119,7 @@ #define HB_INET_CLOSE( x ) close( x ) #endif - typedef struct _HB_SOCKET_STRUCT - { - HB_SOCKET_T com; - struct sockaddr_in remote; - char szErrorText[ 128 ]; - const char * pszErrorText; - int iErrorCode; - int iCount; - int iTimeout; - int iTimeLimit; - PHB_ITEM pPeriodicBlock; - } HB_SOCKET_STRUCT; - - #define HB_PARSOCKET( n ) ( ( HB_SOCKET_STRUCT * ) hb_parptrGC( hb_inetSocketFinalize, n ) ) + #define HB_PARSOCKET( n ) ( ( PHB_SOCKET ) hb_parptrGC( hb_inetSocketFinalize, n ) ) #define HB_SOCKET_ZERO_ERROR( s ) \ do { \ @@ -193,8 +174,8 @@ #define HB_SOCKET_INIT( s, p ) \ do { \ - s = ( HB_SOCKET_STRUCT * ) hb_gcAlloc( sizeof( HB_SOCKET_STRUCT ), hb_inetSocketFinalize ); \ - memset( s, '\0', sizeof( HB_SOCKET_STRUCT ) ); \ + s = ( PHB_SOCKET ) hb_gcAlloc( sizeof( HB_SOCKET ), hb_inetSocketFinalize ); \ + memset( s, '\0', sizeof( HB_SOCKET ) ); \ s->com = ( HB_SOCKET_T ) -1; \ s->iTimeout = -1; \ s->iTimeLimit = -1; \ @@ -280,7 +261,7 @@ static const char * s_inetCRLF = "\r\n"; /* JC1: we need it volatile to be minimally thread safe. */ static volatile int s_iSessions = 0; -static BOOL hb_inetIsOpen( HB_SOCKET_STRUCT * Socket ) +static BOOL hb_inetIsOpen( PHB_SOCKET Socket ) { if( Socket->com == ( HB_SOCKET_T ) -1 ) { @@ -290,9 +271,59 @@ static BOOL hb_inetIsOpen( HB_SOCKET_STRUCT * Socket ) return TRUE; } +int hb_selectReadFD( HB_SOCKET_T fd, int iTimeout ) +{ + fd_set set; + struct timeval tv; + int iResult; + + hb_vmUnlock(); + + FD_ZERO( &set ); + FD_SET( fd, &set ); + + if( iTimeout == -1 ) + iResult = select( fd + 1, &set, NULL, NULL, NULL ); + else + { + tv.tv_sec = iTimeout / 1000; + tv.tv_usec = ( iTimeout % 1000 ) * 1000; + iResult = select( fd + 1, &set, NULL, NULL, &tv ); + } + + hb_vmLock(); + + return iResult > 0 ? FD_ISSET( fd, &set ) : 0; +} + +int hb_selectWriteFD( HB_SOCKET_T fd, int iTimeout ) +{ + fd_set set; + struct timeval tv; + int iResult; + + hb_vmUnlock(); + + FD_ZERO( &set ); + FD_SET( fd, &set ); + + if( iTimeout == -1 ) + iResult = select( fd + 1, NULL, &set, NULL, NULL ); + else + { + tv.tv_sec = iTimeout / 1000; + tv.tv_usec = ( iTimeout % 1000 ) * 1000; + iResult = select( fd + 1, NULL, &set, NULL, &tv ); + } + + hb_vmLock(); + + return iResult > 0 ? FD_ISSET( fd, &set ) : 0; +} + /* Useful utility function to have a timeout; */ -static int hb_selectReadSocket( HB_SOCKET_STRUCT * Socket ) +int hb_selectReadSocket( PHB_SOCKET Socket ) { fd_set set; struct timeval tv; @@ -317,7 +348,7 @@ static int hb_selectReadSocket( HB_SOCKET_STRUCT * Socket ) return iResult > 0 ? FD_ISSET( Socket->com, &set ) : 0; } -static int hb_selectWriteSocket( HB_SOCKET_STRUCT * Socket ) +int hb_selectWriteSocket( PHB_SOCKET Socket ) { fd_set set; struct timeval tv; @@ -343,7 +374,7 @@ static int hb_selectWriteSocket( HB_SOCKET_STRUCT * Socket ) } #if defined( HB_OS_WIN ) -static int hb_selectWriteExceptSocket( HB_SOCKET_STRUCT * Socket ) +static int hb_selectWriteExceptSocket( PHB_SOCKET Socket ) { fd_set set, eset; struct timeval tv; @@ -377,7 +408,7 @@ static int hb_selectWriteExceptSocket( HB_SOCKET_STRUCT * Socket ) #endif /*** Utilty to access host DNS */ -static struct hostent * hb_getHosts( const char * name, HB_SOCKET_STRUCT * Socket ) +static struct hostent * hb_getHosts( const char * name, PHB_SOCKET Socket ) { struct hostent * Host = NULL; @@ -424,7 +455,7 @@ static struct hostent * hb_getHosts( const char * name, HB_SOCKET_STRUCT * Socke /*** Setup the non-blocking method **/ -static void hb_socketSetNonBlocking( HB_SOCKET_STRUCT * Socket ) +static void hb_socketSetNonBlocking( PHB_SOCKET Socket ) { #if defined( HB_OS_WIN ) u_long mode = 1; @@ -444,7 +475,7 @@ static void hb_socketSetNonBlocking( HB_SOCKET_STRUCT * Socket ) /*** Setup the blocking method **/ -static void hb_socketSetBlocking( HB_SOCKET_STRUCT * Socket ) +static void hb_socketSetBlocking( PHB_SOCKET Socket ) { #if defined( HB_OS_WIN ) u_long mode = 0; @@ -463,7 +494,7 @@ static void hb_socketSetBlocking( HB_SOCKET_STRUCT * Socket ) /*** Utility to connect to a defined remote address ***/ -static int hb_socketConnect( HB_SOCKET_STRUCT * Socket ) +static int hb_socketConnect( PHB_SOCKET Socket ) { int iErr1; #if ! defined( HB_OS_WIN ) @@ -538,7 +569,7 @@ static int hb_socketConnect( HB_SOCKET_STRUCT * Socket ) static HB_GARBAGE_FUNC( hb_inetSocketFinalize ) { - HB_SOCKET_STRUCT * Socket = ( HB_SOCKET_STRUCT * ) Cargo; + PHB_SOCKET Socket = ( PHB_SOCKET ) Cargo; if( Socket->com != ( HB_SOCKET_T ) -1 ) { @@ -599,7 +630,7 @@ HB_FUNC( HB_INETCLEANUP ) HB_FUNC( HB_INETCREATE ) { PHB_ITEM pSocket = NULL; - HB_SOCKET_STRUCT * Socket; + PHB_SOCKET Socket; HB_SOCKET_INIT( Socket, pSocket ); if( HB_ISNUM( 1 ) ) @@ -610,7 +641,7 @@ HB_FUNC( HB_INETCREATE ) HB_FUNC( HB_INETCLOSE ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -644,7 +675,7 @@ HB_FUNC( HB_INETCLOSE ) HB_FUNC( HB_INETFD ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -663,7 +694,7 @@ HB_FUNC( HB_INETFD ) HB_FUNC( HB_INETSTATUS ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) hb_retni( Socket->com == ( HB_SOCKET_T ) -1 ? -1 : 1 ); /* TODO: hb_retni( Socket->status ); */ @@ -674,7 +705,7 @@ HB_FUNC( HB_INETSTATUS ) /* Prepared, but still not used; being in wait for comments HB_FUNC( HB_INETSTATUSDESC ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -694,7 +725,7 @@ HB_FUNC( HB_INETSTATUSDESC ) HB_FUNC( HB_INETERRORCODE ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) hb_retni( Socket->iErrorCode ); @@ -704,7 +735,7 @@ HB_FUNC( HB_INETERRORCODE ) HB_FUNC( HB_INETERRORDESC ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) hb_retc( Socket->pszErrorText ); @@ -714,7 +745,7 @@ HB_FUNC( HB_INETERRORDESC ) HB_FUNC( HB_INETCLEARERROR ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) HB_SOCKET_ZERO_ERROR( Socket ); @@ -724,7 +755,7 @@ HB_FUNC( HB_INETCLEARERROR ) HB_FUNC( HB_INETCOUNT ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) hb_retni( Socket->iCount ); @@ -734,7 +765,7 @@ HB_FUNC( HB_INETCOUNT ) HB_FUNC( HB_INETADDRESS ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) hb_retc( inet_ntoa( Socket->remote.sin_addr ) ); @@ -744,7 +775,7 @@ HB_FUNC( HB_INETADDRESS ) HB_FUNC( HB_INETPORT ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) hb_retni( ntohs( Socket->remote.sin_port ) ); @@ -755,7 +786,7 @@ HB_FUNC( HB_INETPORT ) HB_FUNC( HB_INETTIMEOUT ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -770,7 +801,7 @@ HB_FUNC( HB_INETTIMEOUT ) HB_FUNC( HB_INETCLEARTIMEOUT ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) Socket->iTimeout = -1; @@ -780,7 +811,7 @@ HB_FUNC( HB_INETCLEARTIMEOUT ) HB_FUNC( HB_INETTIMELIMIT ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -795,7 +826,7 @@ HB_FUNC( HB_INETTIMELIMIT ) HB_FUNC( HB_INETCLEARTIMELIMIT ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) Socket->iTimeLimit = -1; @@ -805,7 +836,7 @@ HB_FUNC( HB_INETCLEARTIMELIMIT ) HB_FUNC( HB_INETPERIODCALLBACK ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -827,7 +858,7 @@ HB_FUNC( HB_INETPERIODCALLBACK ) HB_FUNC( HB_INETCLEARPERIODCALLBACK ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -843,7 +874,7 @@ HB_FUNC( HB_INETCLEARPERIODCALLBACK ) HB_FUNC( HB_INETGETSNDBUFSIZE ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -867,7 +898,7 @@ HB_FUNC( HB_INETGETSNDBUFSIZE ) HB_FUNC( HB_INETGETRCVBUFSIZE ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -891,7 +922,7 @@ HB_FUNC( HB_INETGETRCVBUFSIZE ) HB_FUNC( HB_INETSETSNDBUFSIZE ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -914,7 +945,7 @@ HB_FUNC( HB_INETSETSNDBUFSIZE ) HB_FUNC( HB_INETSETRCVBUFSIZE ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); if( Socket ) { @@ -943,7 +974,7 @@ HB_FUNC( HB_INETSETRCVBUFSIZE ) static void s_inetRecvInternal( int iMode ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); PHB_ITEM pBuffer = hb_param( 2, HB_IT_STRING ); char * buffer; ULONG ulLen; @@ -1064,7 +1095,7 @@ HB_FUNC( HB_INETRECVALL ) static void s_inetRecvPattern( const char * szPattern ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); PHB_ITEM pResult = hb_param( 2, HB_IT_BYREF ); PHB_ITEM pMaxSize = hb_param( 3, HB_IT_NUMERIC ); PHB_ITEM pBufferSize = hb_param( 4, HB_IT_NUMERIC ); @@ -1196,7 +1227,7 @@ HB_FUNC( HB_INETRECVLINE ) HB_FUNC( HB_INETRECVENDBLOCK ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); PHB_ITEM pProto = hb_param( 2, HB_IT_ARRAY | HB_IT_STRING ); PHB_ITEM pResult = hb_param( 3, HB_IT_BYREF ); PHB_ITEM pMaxSize = hb_param( 4, HB_IT_NUMERIC ); @@ -1396,7 +1427,7 @@ HB_FUNC( HB_INETRECVENDBLOCK ) HB_FUNC( HB_INETDATAREADY ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); int iVal; fd_set rfds; struct timeval tv = { 0, 0 }; @@ -1440,7 +1471,7 @@ HB_FUNC( HB_INETDATAREADY ) static void s_inetSendInternal( int iMode ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); PHB_ITEM pBuffer = hb_param( 2, HB_IT_STRING ); const char * Buffer; int iLen, iSent, iSend; @@ -1610,7 +1641,7 @@ HB_FUNC( HB_INETGETALIAS ) HB_FUNC( HB_INETSERVER ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 2 ); + PHB_SOCKET Socket = HB_PARSOCKET( 2 ); PHB_ITEM pSocket = NULL; const char * szAddress; int iPort; @@ -1691,8 +1722,8 @@ HB_FUNC( HB_INETSERVER ) HB_FUNC( HB_INETACCEPT ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); - HB_SOCKET_STRUCT * NewSocket; + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET NewSocket; HB_SOCKET_T incoming = 0; int iError = EAGAIN; struct sockaddr_in si_remote; @@ -1772,7 +1803,7 @@ HB_FUNC( HB_INETACCEPT ) HB_FUNC( HB_INETCONNECT ) { const char * szHost = hb_parc( 1 ); - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 3 ); + PHB_SOCKET Socket = HB_PARSOCKET( 3 ); PHB_ITEM pSocket = NULL; struct hostent * Host; int iPort; @@ -1833,7 +1864,7 @@ HB_FUNC( HB_INETCONNECT ) HB_FUNC( HB_INETCONNECTIP ) { const char * szHost = hb_parc( 1 ); - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 3 ); + PHB_SOCKET Socket = HB_PARSOCKET( 3 ); PHB_ITEM pSocket = NULL; int iPort = hb_parni( 2 ); @@ -1891,7 +1922,7 @@ HB_FUNC( HB_INETCONNECTIP ) HB_FUNC( HB_INETDGRAMBIND ) { - HB_SOCKET_STRUCT * Socket; + PHB_SOCKET Socket; PHB_ITEM pSocket = NULL; int iPort = hb_parni( 1 ); int iOpt = 1; @@ -1988,7 +2019,7 @@ HB_FUNC( HB_INETDGRAMBIND ) HB_FUNC( HB_INETDGRAM ) { - HB_SOCKET_STRUCT * Socket; + PHB_SOCKET Socket; PHB_ITEM pSocket = NULL; int iOpt = 1; @@ -2022,7 +2053,7 @@ HB_FUNC( HB_INETDGRAM ) HB_FUNC( HB_INETDGRAMSEND ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); const char * szAddress = hb_parc( 2 ); int iPort = hb_parni( 3 ); PHB_ITEM pBuffer = hb_param( 4, HB_IT_STRING ); @@ -2083,7 +2114,7 @@ HB_FUNC( HB_INETDGRAMSEND ) HB_FUNC( HB_INETDGRAMRECV ) { - HB_SOCKET_STRUCT * Socket = HB_PARSOCKET( 1 ); + PHB_SOCKET Socket = HB_PARSOCKET( 1 ); PHB_ITEM pBuffer = hb_param( 2, HB_IT_STRING ); int iTimeElapsed = 0; int iLen, iMaxLen; diff --git a/harbour/source/vm/itemapi.c b/harbour/source/vm/itemapi.c index 7839b12a8e..16227e9843 100644 --- a/harbour/source/vm/itemapi.c +++ b/harbour/source/vm/itemapi.c @@ -366,11 +366,12 @@ PHB_ITEM hb_itemPutCPtr( PHB_ITEM pItem, char * szText ) pItem->type = HB_IT_STRING; pItem->item.asString.length = ulLen; - if( ulLen == 0 ) + if( ulLen == 0 || szText == NULL ) { pItem->item.asString.allocated = 0; pItem->item.asString.value = ( char * ) ""; - hb_xfree( szText ); + if( szText ) + hb_xfree( szText ); } else if( ulLen == 1 ) { @@ -402,11 +403,12 @@ PHB_ITEM hb_itemPutCLPtr( PHB_ITEM pItem, char * szText, ULONG ulLen ) pItem->type = HB_IT_STRING; pItem->item.asString.length = ulLen; - if( ulLen == 0 ) + if( ulLen == 0 || szText == NULL ) { pItem->item.asString.allocated = 0; pItem->item.asString.value = ( char * ) ""; - hb_xfree( szText ); + if( szText ) + hb_xfree( szText ); } else if( ulLen == 1 ) {