Annotation of parser3/src/main/pa_socks.C, revision 1.27

1.3       paf         1: /**    @file
1.1       paf         2:        Parser: socks initialization/finalization.
                      3: 
1.26      moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.13      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.15      paf         6: */
1.1       paf         7: 
1.27    ! moko        8: volatile const char * IDENT_PA_SOCKS_C="$Id: pa_socks.C,v 1.26 2012-03-16 09:24:14 moko Exp $";
1.2       paf         9: 
                     10: #define NO_UNISTD_H
                     11: #include "pa_config_includes.h"
1.1       paf        12: 
1.19      paf        13: #ifdef WIN32
1.9       paf        14: #include "pa_exception.h"
                     15: #include "pa_socks.h"
1.19      paf        16: #include "pa_string.h"
1.11      paf        17: 
1.19      paf        18: // in cygwin: for pascal
                     19: // in MSVC: for everything
                     20: #include <windows.h>
                     21: 
                     22: #ifdef CYGWIN
                     23: // WSADATA
                     24: #define WSADESCRIPTION_LEN     256
                     25: #define WSASYS_STATUS_LEN      128
                     26: typedef struct WSAData {
                     27:        WORD    wVersion;
                     28:        WORD    wHighVersion;
                     29:        char    szDescription[WSADESCRIPTION_LEN+1];
                     30:        char    szSystemStatus[WSASYS_STATUS_LEN+1];
                     31:        unsigned short  iMaxSockets;
                     32:        unsigned short  iMaxUdpDg;
                     33:        char *  lpVendorInfo;
                     34: } WSADATA;
                     35: typedef WSADATA *LPWSADATA;
                     36: 
1.27    ! moko       37: extern "C" int PASCAL FAR WSAStartup(WORD,LPWSADATA);
        !            38: extern "C" int PASCAL FAR WSACleanup(void);
        !            39: extern "C" int PASCAL FAR WSAGetLastError();
        !            40: 
        !            41: #endif // CYGWIN
1.9       paf        42: 
1.1       paf        43: WSADATA wsaData;
                     44: 
1.22      paf        45: void pa_socks_init() {
1.1       paf        46:        WORD wVersionRequested;
                     47:        int err; 
                     48:        wVersionRequested = MAKEWORD( 1, 1 ); 
                     49:        //wVersionRequested = MAKEWORD( 2, 2 ); 
                     50:        err = WSAStartup( wVersionRequested, &wsaData );
                     51:        if ( err != 0 ) {
                     52:            /* Tell the user that we could not find a usable */
                     53:                /* WinSock DLL.                                  */    
1.14      paf        54:                throw Exception(0,
1.1       paf        55:                        0,
                     56:                        "can not WSAStartup, err=%d", 
                     57:                                err);
                     58:        } 
                     59: }
                     60: 
1.22      paf        61: void pa_socks_done() {
1.1       paf        62:        /* Confirm that the WinSock DLL supports 2.2.*/
                     63:        /* Note that if the DLL supports versions greater    */
                     64:        /* than 2.2 in addition to 2.2, it will still return */
                     65:        /* 2.2 in wVersion since that is the version we      */
                     66:        /* requested.                                        */ 
                     67:        if ( LOBYTE( wsaData.wVersion ) == 2 ||
                     68:         HIBYTE( wsaData.wVersion ) == 2 ) {
                     69:                WSACleanup( );
                     70:                return; 
                     71:        } 
                     72: }
                     73: 
1.23      paf        74: const char* pa_socks_strerr(int no) {
                     75:        char buf[MAX_STRING];
                     76:        buf[0]=0;
                     77:        size_t error_size=FormatMessage( 
                     78:                FORMAT_MESSAGE_FROM_SYSTEM | 
                     79:                FORMAT_MESSAGE_IGNORE_INSERTS,
                     80:                NULL,
                     81:                no,
                     82:                0, // Default language
                     83:                (LPTSTR) &buf,
                     84:                sizeof(buf),
                     85:                NULL 
                     86:        );
                     87:        if(error_size>3) // ".\r\n"
                     88:                buf[error_size-3]=0;
                     89:        return buf[0]? pa_strdup(buf): "unknown error";
                     90: }
                     91: int pa_socks_errno() {
                     92:        return WSAGetLastError();
                     93: }
                     94: 
1.1       paf        95: #else
1.10      paf        96: 
1.22      paf        97: void pa_socks_init() {}
                     98: void pa_socks_done() {}
1.23      paf        99: 
                    100: const char* pa_socks_strerr(int no) {
                    101:        return strerror(no);
                    102: }
                    103: int pa_socks_errno() {
                    104:        return errno;
                    105: }
1.1       paf       106: 
                    107: #endif

E-mail: