@@ -135,6 +135,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135135
136136
137137static bool sharing_shall_block = true;
138+ static int Protocol_version ;
138139
139140#define COLOR_RED "\33[01;31m"
140141#define COLOR_GREEN "\33[32m"
@@ -568,6 +569,7 @@ static LONG SCardEstablishContextTH(DWORD dwScope,
568569 LONG rv ;
569570 struct establish_struct scEstablishStruct ;
570571 uint32_t dwClientID = 0 ;
572+ struct version_struct veStr ;
571573
572574 (void )pvReserved1 ;
573575 (void )pvReserved2 ;
@@ -586,11 +588,12 @@ static LONG SCardEstablishContextTH(DWORD dwScope,
586588 return SCARD_E_NO_SERVICE ;
587589 }
588590
591+ veStr .major = PROTOCOL_VERSION_MAJOR ;
592+ veStr .minor = PROTOCOL_VERSION_MINOR ;
593+
594+ connect_again :
589595 { /* exchange client/server protocol versions */
590- struct version_struct veStr ;
591596
592- veStr .major = PROTOCOL_VERSION_MAJOR ;
593- veStr .minor = PROTOCOL_VERSION_MINOR ;
594597 veStr .rv = SCARD_S_SUCCESS ;
595598
596599 rv = MessageSendWithHeader (CMD_VERSION , dwClientID , sizeof (veStr ),
@@ -609,6 +612,23 @@ static LONG SCardEstablishContextTH(DWORD dwScope,
609612
610613 Log3 (PCSC_LOG_INFO , "Server is protocol version %d:%d" ,
611614 veStr .major , veStr .minor );
615+ Log3 (PCSC_LOG_INFO , "Client is protocol version %d:%d" ,
616+ PROTOCOL_VERSION_MAJOR , PROTOCOL_VERSION_MINOR );
617+
618+ if (SCARD_E_SERVICE_STOPPED == veStr .rv )
619+ {
620+ /* server complained about our protocol version? */
621+ if (PROTOCOL_VERSION_MAJOR == veStr .major )
622+ {
623+ if (PROTOCOL_VERSION_MINOR_CLIENT_BACKWARD <= veStr .minor )
624+ {
625+ /* try again with the protocol version proposed by
626+ * the server */
627+ Log1 (PCSC_LOG_INFO , "Using backward compatibility" );
628+ goto connect_again ;
629+ }
630+ }
631+ }
612632
613633 if (veStr .rv != SCARD_S_SUCCESS )
614634 {
@@ -617,6 +637,9 @@ static LONG SCardEstablishContextTH(DWORD dwScope,
617637 }
618638 }
619639
640+ /* store protocol version of the server */
641+ Protocol_version = veStr .major * 1000 + veStr .minor ;
642+
620643again :
621644 /*
622645 * Try to establish an Application Context with the server
@@ -3634,6 +3657,10 @@ static LONG getReaderEvents(SCONTEXTMAP * currentContextMap, int *readerEvents)
36343657 LONG rv ;
36353658 struct get_reader_events get_reader_events = {0 };
36363659
3660+ /* CMD_GET_READER_EVENTS was added in protocol 4:5 */
3661+ if (Protocol_version < 4005 )
3662+ return SCARD_E_UNSUPPORTED_FEATURE ;
3663+
36373664 rv = MessageSendWithHeader (CMD_GET_READER_EVENTS , dwClientID , 0 , NULL );
36383665 if (rv != SCARD_S_SUCCESS )
36393666 return rv ;
0 commit comments