diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 90290919e4..9696209805 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,27 @@ The license applies to all entries newer than 2009-04-28. */ +2010-06-09 23:38 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) + * include/hbsocket.ch + + Added address info array positions. + + * src/rtl/hbsockhb.c + - Deleted spaces @ EOL. + + * contrib/hbtpathy/telepath.prg + * contrib/hbcomm/tests/test.prg + * contrib/hbcomm/hbcomm.prg + * contrib/hbsms/hbsms.prg + ! Fixed HB_COMRECV() which requires a preallocated string + to be passed. + (no testing done, please review me, I'm almost sure I've + made mistakes here) + + * examples/httpsrv/uhttpd.hbp + * examples/httpsrv/uhttpd.prg + - examples/httpsrv/socket.c + + Changed to use new natic SOCKET API. + 2010-06-09 22:38 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) * utils/hbmk2/hbmk2.prg ! Tuned watcom filename escaping behavior: diff --git a/harbour/contrib/hbcomm/hbcomm.prg b/harbour/contrib/hbcomm/hbcomm.prg index b2ad96ac3d..54a5c0319b 100644 --- a/harbour/contrib/hbcomm/hbcomm.prg +++ b/harbour/contrib/hbcomm/hbcomm.prg @@ -127,6 +127,7 @@ FUNCTION ISWORKING( nPort ) [vszakats] */ /* Fetch chars into */ FUNCTION INCHR( nPort, nCount, /* @ */ cString ) + cString := iif( ISNUMBER( nCount ), Space( nCount ), "" ) RETURN hb_comRecv( nPort, @cString, nCount ) /* Send out characters. Returns .t. if successful. */ diff --git a/harbour/contrib/hbcomm/tests/test.prg b/harbour/contrib/hbcomm/tests/test.prg index f5aaa86878..9e3d58b893 100644 --- a/harbour/contrib/hbcomm/tests/test.prg +++ b/harbour/contrib/hbcomm/tests/test.prg @@ -87,6 +87,7 @@ STATIC PROCEDURE FReceive() nSize := INBUFSIZE( s_nHandle ) IF nSize > 0 + cReceive := Space( nSize ) INCHR( s_nHandle, nSize, @cReceive ) ? ">>", Left( cReceive, nSize ) ENDIF diff --git a/harbour/contrib/hbsms/hbsms.prg b/harbour/contrib/hbsms/hbsms.prg index 2449115d01..1dfd9ae43b 100644 --- a/harbour/contrib/hbsms/hbsms.prg +++ b/harbour/contrib/hbsms/hbsms.prg @@ -64,9 +64,9 @@ STATIC FUNCTION port_send( h, s ) RETURN hb_comSend( h, s ) STATIC FUNCTION port_rece( h, n, t ) - LOCAL cString := "" + LOCAL cString := iif( ISNUMBER( n ), Space( n ), "" ) - hb_comRecv( h, @cString, n, t ) + hb_comRecv( h, @cString,, t ) RETURN cString diff --git a/harbour/contrib/hbtpathy/telepath.prg b/harbour/contrib/hbtpathy/telepath.prg index 6dc8486af1..09a4b127b6 100644 --- a/harbour/contrib/hbtpathy/telepath.prg +++ b/harbour/contrib/hbtpathy/telepath.prg @@ -695,7 +695,7 @@ STATIC FUNCTION FetchChars( nPort ) RETURN 0 ENDIF - cStr := "" + cStr := Space( hb_comInputCount( t_aPorts[ nPort, TPFP_HANDLE ] ) ) hb_comRecv( t_aPorts[ nPort, TPFP_HANDLE ], @cStr ) t_aPorts[ nPort, TPFP_INBUF ] += cStr diff --git a/harbour/examples/httpsrv/socket.c b/harbour/examples/httpsrv/socket.c deleted file mode 100644 index 9102eff881..0000000000 --- a/harbour/examples/httpsrv/socket.c +++ /dev/null @@ -1,281 +0,0 @@ -/* - * $Id$ - */ - -/* - * Harbour Project source code: - * socket API - * - * Copyright 2009 Przemyslaw Czerpak - * www - http://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. - * - */ - -/* - - Function naming: - The intention of this library is to be as close as possible to the original - socket implementation. This supposed to be valid for function names also, - but some of the names are very platform dependent, ex., WSA*() functions. - select() function name is reserved for standard Harbour's function, so, - socket_*() prefix was used: - socket_init() - WSAStartup() - socket_exit() - WSACleanup() - socket_error() - WSALastError() - socket_select() - select() - Finally I renamed all functions to have socket_*() prefix to be more "prefix - compatible" and not to occupy a general function names like send(), bind(), - accept(), listen(), etc.: - socket_create() - socket() - socket_close() - closesocket() - socket_shutdown() - shutdown() - socket_bind() - bind() - socket_listen() - listen() - socket_accept() - accept() - socket_send() - send() - socket_recv() - recv() - socket_recv() - recv() - socket_getsockname() - getsockname() - socket_getpeername() - getpeername() - - - Types mapping: - SOCKET - UINT_PTR in Windows, let's map it to pointer type, and INVALID_SOCKET value to NIL - - struct sockaddr - It is not only IP addresses, also can be IPX, etc. All network-host byte order - conversion should be hidden from Harbour API. So, let's map to: - { adress_familly, ... } - AF_INET: { AF_INET, cAddr, nPort } - other: { AF_?, cAddressDump } -*/ - -#include "hbsocket.h" -#include "hbapiitm.h" - -static HB_SOCKET hb_parsocket( int iParam ) -{ - return HB_ISPOINTER( iParam ) ? ( HB_SOCKET ) ( HB_PTRDIFF ) - hb_parptr( iParam ) : HB_NO_SOCKET; -} - -static void hb_retsocket( HB_SOCKET hSocket ) -{ - if( hSocket != HB_NO_SOCKET ) - hb_retptr( ( void * ) ( HB_PTRDIFF ) hSocket ); -} - -static HB_SOCKET hb_itemGetSocket( PHB_ITEM pSocket ) -{ - if( pSocket && HB_IS_POINTER( pSocket ) ) - return ( HB_SOCKET ) ( HB_PTRDIFF ) hb_itemGetPtr( pSocket ); - else - return HB_NO_SOCKET; -} - -HB_FUNC( SOCKET_INIT ) -{ - hb_retni( hb_socketInit() ); -} - -HB_FUNC( SOCKET_EXIT ) -{ - hb_socketCleanup(); -} - -HB_FUNC( SOCKET_ERROR ) -{ - hb_retni( hb_socketGetError() ); -} - -HB_FUNC( SOCKET_CREATE ) -{ - hb_retsocket( hb_socketOpen( hb_parnidef( 1, HB_SOCKET_PF_INET ), - hb_parnidef( 2, HB_SOCKET_PT_STREAM ), - hb_parnidef( 3, HB_SOCKET_IPPROTO_TCP ) ) ); -} - -HB_FUNC( SOCKET_CLOSE ) -{ - hb_retni( hb_socketClose( hb_parsocket( 1 ) ) ); -} - -HB_FUNC( SOCKET_BIND ) -{ - void * sa; - unsigned len; - - if( hb_socketAddrFromItem( &sa, &len, hb_param( 2, HB_IT_ANY ) ) ) - { - hb_retni( hb_socketBind( hb_parsocket( 1 ), sa, len ) ); - hb_xfree( sa ); - } -} - -HB_FUNC( SOCKET_LISTEN ) -{ - hb_retni( hb_socketListen( hb_parsocket( 1 ), hb_parnidef( 2, 10 ) ) ); -} - -HB_FUNC( SOCKET_ACCEPT ) -{ - if( HB_ISBYREF( 2 ) ) - { - void * sa; - unsigned len; - PHB_ITEM pItem; - - hb_retsocket( hb_socketAccept( hb_parsocket( 1 ), &sa, &len, - HB_ISNUM( 3 ) ? hb_parnint( 3 ) : -1 ) ); - pItem = hb_socketAddrToItem( sa, len ); - if( pItem ) - { - hb_itemParamStoreForward( 2, pItem ); - hb_itemRelease( pItem ); - } - else - hb_stor( 2 ); - - if( sa ) - hb_xfree( sa ); - } - else - hb_retsocket( hb_socketAccept( hb_parsocket( 1 ), NULL, 0, - HB_ISNUM( 3 ) ? hb_parnint( 3 ) : -1 ) ); -} - -HB_FUNC( SOCKET_SHUTDOWN ) -{ - hb_retni( hb_socketShutdown( hb_parsocket( 1 ), - hb_parnidef( 2, HB_SOCKET_SHUT_RDWR ) ) ); -} - -HB_FUNC( SOCKET_RECV ) -{ - char * pBuf; - long len; - - len = hb_parni( 3 ); - if( len <= 0 ) - len = 4096; - pBuf = ( char * ) hb_xgrab( len + 1 ); - len = hb_socketRecv( hb_parsocket( 1 ), pBuf, len, hb_parni( 4 ), - HB_ISNUM( 5 ) ? hb_parnint( 5 ) : -1 ); - hb_retni( len ); - hb_storclen( pBuf, len > 0 ? len : 0, 2 ); - hb_xfree( pBuf ); -} - -HB_FUNC( SOCKET_SEND ) -{ - hb_retni( hb_socketSend( hb_parsocket( 1 ), hb_parc( 2 ), hb_parclen( 2 ), - hb_parni( 4 ), HB_ISNUM( 5 ) ? hb_parnint( 5 ) : -1 ) ); -} - -HB_FUNC( SOCKET_GETSOCKNAME ) -{ - void * sa; - unsigned len; - int iRet; - - iRet = hb_socketGetSockName( hb_parsocket( 1 ), &sa, &len ); - hb_retni( iRet ); - if( HB_ISBYREF( 2 ) ) - { - PHB_ITEM pItem = hb_socketAddrToItem( sa, len ); - if( pItem ) - { - hb_itemParamStoreForward( 2, pItem ); - hb_itemRelease( pItem ); - } - else - hb_stor( 2 ); - } - if( sa ) - hb_xfree( sa ); -} - -HB_FUNC( SOCKET_GETPEERNAME ) -{ - void * sa; - unsigned len; - int iRet; - - iRet = hb_socketGetPeerName( hb_parsocket( 1 ), &sa, &len ); - hb_retni( iRet ); - if( HB_ISBYREF( 2 ) ) - { - PHB_ITEM pItem = hb_socketAddrToItem( sa, len ); - if( pItem ) - { - hb_itemParamStoreForward( 2, pItem ); - hb_itemRelease( pItem ); - } - else - hb_stor( 2 ); - } - if( sa ) - hb_xfree( sa ); -} - -HB_FUNC( SOCKET_CONNECT ) -{ - void * sa; - unsigned len; - - if( hb_socketAddrFromItem( &sa, &len, hb_param( 2, HB_IT_ANY ) ) ) - { - hb_retni( hb_socketConnect( hb_parsocket( 1 ), sa, len, - HB_ISNUM( 3 ) ? hb_parnint( 3 ) : -1 ) ); - hb_xfree( sa ); - } -} - -HB_FUNC( SOCKET_SELECT ) -{ - hb_retni( hb_socketSelect( hb_param( 1, HB_IT_ARRAY ), HB_ISBYREF( 1 ), - hb_param( 2, HB_IT_ARRAY ), HB_ISBYREF( 2 ), - hb_param( 3, HB_IT_ARRAY ), HB_ISBYREF( 3 ), - HB_ISNUM( 4 ) ? hb_parnint( 4 ) : -1, - hb_itemGetSocket ) ); -} diff --git a/harbour/examples/httpsrv/uhttpd.hbp b/harbour/examples/httpsrv/uhttpd.hbp index 16e55a17ee..9f4c7f21f0 100644 --- a/harbour/examples/httpsrv/uhttpd.hbp +++ b/harbour/examples/httpsrv/uhttpd.hbp @@ -2,6 +2,6 @@ # $Id$ # --mt -gui uhttpd.prg cgifunc.prg cookie.prg session.prg socket.c +-mt -gui uhttpd.prg cgifunc.prg cookie.prg session.prg -l{win}hbwin diff --git a/harbour/examples/httpsrv/uhttpd.prg b/harbour/examples/httpsrv/uhttpd.prg index c86a05ac5f..b3d2260624 100644 --- a/harbour/examples/httpsrv/uhttpd.prg +++ b/harbour/examples/httpsrv/uhttpd.prg @@ -93,6 +93,8 @@ #include "hbextern.ch" // need this to use with HRB +#include "hbsocket.ch" + #ifdef GD_SUPPORT // adding GD support REQUEST GDIMAGE, GDIMAGECHAR, GDCHART @@ -103,14 +105,6 @@ #stdout "Lib GD support disabled" #endif -#ifdef USE_HB_INET - #define APP_INET_SUPPORT "_INET" - #stdout "inet socket API" -#else - #define APP_INET_SUPPORT "" - #stdout "custom socket API" -#endif - #ifdef FIXED_THREADS #define APP_DT_SUPPORT "_FIXED_THREADS" #stdout "Fixed # of threads" @@ -121,7 +115,7 @@ #define APP_NAME "uhttpd" #define APP_VER_NUM "0.4.4" -#define APP_VERSION APP_VER_NUM + APP_GD_SUPPORT + APP_INET_SUPPORT + APP_DT_SUPPORT +#define APP_VERSION APP_VER_NUM + APP_GD_SUPPORT + APP_DT_SUPPORT #define AF_INET 2 @@ -180,10 +174,6 @@ STATIC s_aServiceThreads := {} STATIC s_hHRBModules := {=>} STATIC s_aDirectoryIndex -#ifdef USE_HB_INET -STATIC s_cLocalAddress, s_nLocalPort -#endif - STATIC s_hActions := { ; /*"default-handler" => @Handler_Default() ,*/; // default handler /*"send-as-is" => @Handler_SendAsIs() ,*/; @@ -542,27 +532,12 @@ FUNCTION MAIN( ... ) // SOCKET CREATION // -------------------------------------------------------------------------- -#ifdef USE_HB_INET - hListen := hb_InetServer( nPort ) - - IF hb_InetErrorCode( hListen ) != 0 - ? "Bind Error" - WAIT + hListen := hb_socketOpen() + IF ! hb_socketBind( hListen, { AF_INET, "0.0.0.0", nPort } ) + ? "bind() error", hb_socketGetError() + ELSEIF ! hb_socketListen( hListen ) + ? "listen() error", hb_socketGetError() ELSE - - s_nLocalPort := hb_InetPort( hListen ) - s_cLocalAddress := hb_InetAddress( hListen ) - - hb_InetTimeOut( hListen, 3000 ) - -#else - hListen := socket_create() - IF socket_bind( hListen, { AF_INET, "0.0.0.0", nPort } ) == -1 - ? "bind() error", socket_error() - ELSEIF socket_listen( hListen ) == -1 - ? "listen() error", socket_error() - ELSE -#endif // --------------------------------------------------------------------------------- // // Starting Accept connection thread // --------------------------------------------------------------------------------- // @@ -626,27 +601,16 @@ FUNCTION MAIN( ... ) ENDIF // Wait a connection -#ifdef USE_HB_INET - IF HB_InetDataReady( hListen, 100 ) > 0 -#else - IF socket_select( { hListen },,, 50 ) > 0 -#endif + IF hb_socketSelect( { hListen },,,,,, 50 ) > 0 + // reset remote values aRemote := NIL // Accept a remote connection -#ifdef USE_HB_INET - hSocket := hb_InetAccept( hListen ) -#else - hSocket := socket_accept( hListen, @aRemote ) -#endif + hSocket := hb_socketAccept( hListen, @aRemote ) IF hSocket == NIL -#ifdef USE_HB_INET - WriteToConsole( hb_StrFormat( "accept() error" ) ) -#else - WriteToConsole( hb_StrFormat( "accept() error: %s", socket_error() ) ) -#endif + WriteToConsole( hb_StrFormat( "accept() error: %s", hb_socketGetError() ) ) ELSE @@ -681,11 +645,7 @@ FUNCTION MAIN( ... ) WriteToConsole( "--- Quitting " + APP_NAME + " ---" ) // Close socket -#ifdef USE_HB_INET - hb_InetClose( hListen ) -#else - socket_close( hListen ) -#endif + hb_socketClose( hListen ) // Close log files FCLOSE( s_hfileLogAccess ) @@ -802,12 +762,8 @@ STATIC FUNCTION AcceptConnections() // If I have no more of service threads to use ... (DOS attack ?) IF nServiceConnections > nMaxServiceThreads // DROP connection -#ifdef USE_HB_INET - hb_InetClose( hSocket ) -#else - socket_shutdown( hSocket ) - socket_close( hSocket ) -#endif + hb_socketShutdown( hSocket ) + hb_socketClose( hSocket ) // If I have no service threads in use ... ELSEIF nServiceConnections >= nServiceThreads @@ -949,11 +905,7 @@ STATIC FUNCTION ProcessConnection() #endif IF nLen == -1 -#ifdef USE_HB_INET - ? "recv() error:", hb_InetErrorCode( hSocket ), hb_InetErrorDesc( hSocket ) -#else - ? "recv() error:", socket_error() -#endif + ? "recv() error:", hb_socketGetError() ELSEIF nLen == 0 /* connection closed */ ELSE @@ -996,13 +948,8 @@ STATIC FUNCTION ProcessConnection() nParseTime := hb_milliseconds() - nMsecs WriteToConsole( "Page served in : " + Str( nParseTime/1000, 7, 4 ) + " seconds" ) -#ifdef USE_HB_INET - hb_InetClose( hSocket ) - hSocket := NIL -#else - socket_shutdown( hSocket ) - socket_close( hSocket ) -#endif + hb_socketShutdown( hSocket ) + hb_socketClose( hSocket ) IF hb_mutexLock( s_hmtxBusy ) s_nConnections-- @@ -1107,11 +1054,7 @@ STATIC FUNCTION ServiceConnection() nLen := readRequest( hSocket, @cRequest ) IF nLen == -1 -#ifdef USE_HB_INET - ? "recv() error:", hb_InetErrorCode( hSocket ), hb_InetErrorDesc( hSocket ) -#else - ? "recv() error:", socket_error() -#endif + ? "recv() error:", hb_socketGetError() ELSEIF nLen == 0 /* connection closed */ ELSE @@ -1149,13 +1092,8 @@ STATIC FUNCTION ServiceConnection() nParseTime := hb_milliseconds() - nMsecs WriteToConsole( "Page served in : " + Str( nParseTime/1000, 7, 4 ) + " seconds" ) -#ifdef USE_HB_INET - hb_InetClose( hSocket ) - hSocket := NIL -#else - socket_shutdown( hSocket ) - socket_close( hSocket ) -#endif + hb_socketShutdown( hSocket ) + hb_socketClose( hSocket ) IF hb_mutexLock( s_hmtxBusy ) s_nServiceConnections-- @@ -1690,25 +1628,6 @@ STATIC FUNCTION CGIKill( hProc, hmtxCGIKill ) RETURN nErrorLevel -INIT PROCEDURE SocketInit() -#ifdef USE_HB_INET - hb_InetInit() -#else - IF socket_init() != 0 - ? "socket_init() error" - ENDIF -#endif - RETURN - - -EXIT PROCEDURE SocketExit() -#ifdef USE_HB_INET - hb_InetCleanup() -#else - socket_exit() -#endif - RETURN - /******************************************************************** Public helper functions @@ -1781,15 +1700,10 @@ STATIC FUNCTION readRequest( hSocket, /* @ */ cRequest ) LOCAL cBuf, nLen, nPos /* receive query */ -#ifdef USE_HB_INET - cRequest := hb_InetRecvEndBlock( hSocket, CR_LF + CR_LF, @nLen ) - IF nLen > 0 - cRequest += CR_LF + CR_LF - ENDIF -#else cRequest := "" DO WHILE .T. - nLen := socket_recv( hSocket, @cBuf ) + cBuf := Space( 1 ) + nLen := hb_socketRecv( hSocket, @cBuf ) IF nLen <= 0 EXIT ENDIF @@ -1798,7 +1712,6 @@ STATIC FUNCTION readRequest( hSocket, /* @ */ cRequest ) EXIT ENDIF ENDDO -#endif /* receive CONTENT-LENGTH data */ IF nLen > 0 @@ -1806,28 +1719,19 @@ STATIC FUNCTION readRequest( hSocket, /* @ */ cRequest ) IF nPos > 0 nPos := Val( Substr( cRequest, nPos + 17, 10 ) ) IF nPos > 0 -#ifdef USE_HB_INET - cBuf := Space( nPos ) - nLen := hb_InetRecvAll( hSocket, @cBuf, nPos ) - IF nLen < 0 - nLen := -1 - ELSE - cRequest += Left( cBuf, nLen ) - ENDIF -#else /* we have to decrease number of bytes to read by already read * data after CR_LF + CR_LF */ nPos -= Len( cRequest ) - At( CR_LF + CR_LF, cRequest ) - 3 WHILE nPos > 0 - nLen := socket_recv( hSocket, @cBuf, nPos ) + cBuf := Space( 1 ) + nLen := hb_socketRecv( hSocket, @cBuf, nPos ) IF nLen <= 0 EXIT ENDIF cRequest += cBuf nPos -= nLen ENDDO -#endif ENDIF ENDIF ENDIF @@ -1842,55 +1746,32 @@ STATIC FUNCTION sendReply( hSocket, cSend ) LOCAL nError := 0 LOCAL nLen -#ifdef USE_HB_INET DO WHILE LEN( cSend ) > 0 - IF ( nLen := hb_InetSendAll( hSocket, cSend ) ) == -1 - ? "send() error:", hb_InetErrorCode( hSocket ), HB_InetErrorDesc( hSocket ) - WriteToConsole( hb_StrFormat( "ProcessConnection() - send() error: %s, cSend = %s, hSocket = %s", hb_InetErrorDesc( hSocket ), cSend, hSocket ) ) + IF ( nLen := hb_socketSend( hSocket, cSend ) ) == -1 + ? "send() error:", hb_socketGetError() + WriteToConsole( hb_StrFormat( "ServiceConnection() - send() error: %s, cSend = %s, hSocket = %s", hb_socketGetError(), cSend, hSocket ) ) EXIT ELSEIF nLen > 0 cSend := SUBSTR( cSend, nLen + 1 ) ENDIF ENDDO -#else - DO WHILE LEN( cSend ) > 0 - IF ( nLen := socket_send( hSocket, cSend ) ) == -1 - ? "send() error:", socket_error() - WriteToConsole( hb_StrFormat( "ServiceConnection() - send() error: %s, cSend = %s, hSocket = %s", socket_error(), cSend, hSocket ) ) - EXIT - ELSEIF nLen > 0 - cSend := SUBSTR( cSend, nLen + 1 ) - ENDIF - ENDDO -#endif RETURN nError STATIC PROCEDURE defineServer( hSocket ) -#ifndef USE_HB_INET LOCAL aI -#endif // define _SERVER vars (address part) -#ifdef USE_HB_INET - _SERVER[ "REMOTE_ADDR" ] := hb_InetAddress( hSocket ) - _SERVER[ "REMOTE_HOST" ] := _SERVER[ "REMOTE_ADDR" ] // no reverse DNS - _SERVER[ "REMOTE_PORT" ] := hb_InetPort( hSocket ) - - _SERVER[ "SERVER_ADDR" ] := s_cLocalAddress - _SERVER[ "SERVER_PORT" ] := LTrim( Str( s_nLocalPort ) ) -#else - IF socket_getpeername( hSocket, @aI ) != -1 - _SERVER[ "REMOTE_ADDR" ] := aI[2] + IF ! Empty( aI := hb_socketGetPeerName( hSocket ) ) + _SERVER[ "REMOTE_ADDR" ] := aI[ HB_SOCKET_ADINFO_ADDRESS ] _SERVER[ "REMOTE_HOST" ] := _SERVER[ "REMOTE_ADDR" ] // no reverse DNS - _SERVER[ "REMOTE_PORT" ] := aI[3] + _SERVER[ "REMOTE_PORT" ] := aI[ HB_SOCKET_ADINFO_PORT ] ENDIF - IF socket_getsockname( hSocket, @aI ) != -1 - _SERVER[ "SERVER_ADDR" ] := aI[2] - _SERVER[ "SERVER_PORT" ] := LTrim( Str( aI[3] ) ) + IF ! Empty( aI := hb_socketGetSockName( hSocket ) ) + _SERVER[ "SERVER_ADDR" ] := aI[ HB_SOCKET_ADINFO_ADDRESS ] + _SERVER[ "SERVER_PORT" ] := LTrim( Str( aI[ HB_SOCKET_ADINFO_PORT ] ) ) ENDIF -#endif // add other _SERVER vars _SERVER[ "REQUEST_METHOD" ] := NIL diff --git a/harbour/include/hbsocket.ch b/harbour/include/hbsocket.ch index 3d6894aa07..b76f947f95 100644 --- a/harbour/include/hbsocket.ch +++ b/harbour/include/hbsocket.ch @@ -125,7 +125,7 @@ #define HB_SOCKET_ERR_TYPENOTFOUND 69 #define HB_SOCKET_ERR_OTHER 70 -/* address familly */ +/* address family */ #define HB_SOCKET_AF_LOCAL 1 #define HB_SOCKET_AF_INET 2 #define HB_SOCKET_AF_IPX 4 @@ -204,4 +204,10 @@ #define HB_SOCKET_IFINFO_HWADDR 8 /* hardware address */ #define HB_SOCKET_IFINFO_LEN 8 +/* HB_SOCKETGETSOCKNAME(), HB_SOCKETGETPEERNAME() return value indexes */ +#define HB_SOCKET_ADINFO_FAMILY 1 +#define HB_SOCKET_ADINFO_ADDRESS 2 /* HB_SOCKET_AF_INET, HB_SOCKET_AF_INET6 */ +#define HB_SOCKET_ADINFO_PATH 2 /* HB_SOCKET_AF_LOCAL */ +#define HB_SOCKET_ADINFO_PORT 3 /* HB_SOCKET_AF_INET, HB_SOCKET_AF_INET6 */ + #endif /* HB_SOCKET_CH_ */ diff --git a/harbour/src/rtl/hbsockhb.c b/harbour/src/rtl/hbsockhb.c index 50b96abaa1..f04fa43aa6 100644 --- a/harbour/src/rtl/hbsockhb.c +++ b/harbour/src/rtl/hbsockhb.c @@ -52,7 +52,7 @@ /* * HB_SOCKETGETERROR() --> nSocketError - * HB_SOCKETGETOSERROR() --> nOSError + * HB_SOCKETGETOSERROR() --> nOSError * HB_SOCKETERRORSTRING( [ nSocketErrror = hb_socketGetError() ] ) --> cError * HB_SOCKETGETSOCKNAME( hSocket ) --> aAddr | NIL * HB_SOCKETGETPEERNAME( hSocket ) --> aAddr | NIL @@ -191,7 +191,7 @@ HB_FUNC( HB_SOCKETGETSOCKNAME ) { PHB_ITEM pItem = hb_socketAddrToItem( addr, len ); - if( addr ) + if( addr ) hb_xfree( addr ); if( pItem ) @@ -239,7 +239,7 @@ HB_FUNC( HB_SOCKETOPEN ) socket_init(); if( ( socket = hb_socketOpen( iDomain, iType, iProtocol ) ) != HB_NO_SOCKET ) { - PHB_PRG_SOCKET pSocket = ( PHB_PRG_SOCKET ) hb_gcAllocate( sizeof( HB_PRG_SOCKET ), + PHB_PRG_SOCKET pSocket = ( PHB_PRG_SOCKET ) hb_gcAllocate( sizeof( HB_PRG_SOCKET ), &s_gcSocketFuncs ); pSocket->socket = socket; hb_retptrGC( pSocket ); @@ -303,14 +303,14 @@ HB_FUNC( HB_SOCKETACCEPT ) if( socket != HB_NO_SOCKET ) { - PHB_PRG_SOCKET pSocket = ( PHB_PRG_SOCKET ) hb_gcAllocate( sizeof( HB_PRG_SOCKET ), + PHB_PRG_SOCKET pSocket = ( PHB_PRG_SOCKET ) hb_gcAllocate( sizeof( HB_PRG_SOCKET ), &s_gcSocketFuncs ); pSocket->socket = socket; hb_retptrGC( pSocket ); } else hb_retptr( NULL ); - + if( HB_ISBYREF( 2 ) ) { @@ -341,7 +341,7 @@ HB_FUNC( HB_SOCKETCONNECT ) hb_xfree( addr ); } } - + HB_FUNC( HB_SOCKETSEND ) { PHB_PRG_SOCKET pSocket = socketParam( 1 ); @@ -356,7 +356,7 @@ HB_FUNC( HB_SOCKETSEND ) if( lParam >= 0 && lParam < lLen ) lLen = lParam; } - hb_retnl( hb_socketSend( pSocket->socket, hb_parc( 2 ), lLen, hb_parni( 4 ), + hb_retnl( hb_socketSend( pSocket->socket, hb_parc( 2 ), lLen, hb_parni( 4 ), hb_parnintdef( 5, -1 ) ) ); } } @@ -378,7 +378,7 @@ HB_FUNC( HB_SOCKETSENDTO ) if( lParam >= 0 && lParam < lLen ) lLen = lParam; } - hb_retnl( hb_socketSendTo( pSocket->socket, hb_parc( 2 ), lLen, hb_parni( 4 ), + hb_retnl( hb_socketSendTo( pSocket->socket, hb_parc( 2 ), lLen, hb_parni( 4 ), addr, len, hb_parnintdef( 6, -1 ) ) ); hb_xfree( addr ); } @@ -401,7 +401,7 @@ HB_FUNC( HB_SOCKETRECV ) if( lRead >= 0 && lRead < ( long ) iLen ) iLen = lRead; } - hb_retnl( hb_socketRecv( pSocket->socket, pBuffer, ( long ) iLen, + hb_retnl( hb_socketRecv( pSocket->socket, pBuffer, ( long ) iLen, hb_parni( 4 ), hb_parnintdef( 5, -1 ) ) ); return; } @@ -430,12 +430,12 @@ HB_FUNC( HB_SOCKETRECVFROM ) if( lRead >= 0 && lRead < ( long ) iLen ) iLen = lRead; } - hb_retnl( lRet = hb_socketRecvFrom( pSocket->socket, pBuffer, ( long ) iLen, - hb_parni( 4 ), &addr, &len, + hb_retnl( lRet = hb_socketRecvFrom( pSocket->socket, pBuffer, ( long ) iLen, + hb_parni( 4 ), &addr, &len, hb_parnintdef( 6, -1 ) ) ); if( HB_ISBYREF( 5 ) ) { - PHB_ITEM pAddr; + PHB_ITEM pAddr; if( lRet != -1 && ( pAddr = hb_socketAddrToItem( addr, len ) ) != NULL ) { @@ -444,7 +444,7 @@ HB_FUNC( HB_SOCKETRECVFROM ) } else hb_stor( 5 ); - } + } if( addr ) hb_xfree( addr ); @@ -589,9 +589,9 @@ static HB_SOCKET socketSelectCallback( PHB_ITEM pItem ) HB_FUNC( HB_SOCKETSELECT ) { socket_init(); - hb_retni( hb_socketSelect( hb_param( 1, HB_IT_ARRAY ), hb_parl( 2 ), - hb_param( 3, HB_IT_ARRAY ), hb_parl( 4 ), - hb_param( 5, HB_IT_ARRAY ), hb_parl( 6 ), + hb_retni( hb_socketSelect( hb_param( 1, HB_IT_ARRAY ), hb_parl( 2 ), + hb_param( 3, HB_IT_ARRAY ), hb_parl( 4 ), + hb_param( 5, HB_IT_ARRAY ), hb_parl( 6 ), hb_parnintdef( 7, -1 ), socketSelectCallback ) ); }