<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>pcsc-lite: winscard_msg.c Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.7.4 --> <div id="top"> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td style="padding-left: 0.5em;"> <div id="projectname">pcsc-lite <span id="projectnumber">1.7.2</span></div> </td> </tr> </tbody> </table> </div> <div id="navrow1" class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div id="navrow2" class="tabs2"> <ul class="tablist"> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul> </div> <div id="nav-path" class="navpath"> <ul> <li class="navelem"><a class="el" href="dir_7a4553ee0c3b13d0e595a5d1659110a6.html">src</a> </li> </ul> </div> </div> <div class="header"> <div class="headertitle"> <div class="title">winscard_msg.c</div> </div> </div> <div class="contents"> <a href="winscard__msg_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> * MUSCLE SmartCard Development ( http://www.linuxnet.com )</span> <a name="l00003"></a>00003 <span class="comment"> *</span> <a name="l00004"></a>00004 <span class="comment"> * Copyright (C) 2001-2004</span> <a name="l00005"></a>00005 <span class="comment"> * David Corcoran <corcoran@linuxnet.com></span> <a name="l00006"></a>00006 <span class="comment"> * Copyright (C) 2003-2004</span> <a name="l00007"></a>00007 <span class="comment"> * Damien Sauveron <damien.sauveron@labri.fr></span> <a name="l00008"></a>00008 <span class="comment"> * Copyright (C) 2002-2010</span> <a name="l00009"></a>00009 <span class="comment"> * Ludovic Rousseau <ludovic.rousseau@free.fr></span> <a name="l00010"></a>00010 <span class="comment"> *</span> <a name="l00011"></a>00011 <span class="comment"> * $Id: winscard_msg.c 5440 2010-12-11 15:41:35Z rousseau $</span> <a name="l00012"></a>00012 <span class="comment"> */</span> <a name="l00013"></a>00013 <a name="l00023"></a>00023 <span class="preprocessor">#include "config.h"</span> <a name="l00024"></a>00024 <span class="preprocessor">#include <fcntl.h></span> <a name="l00025"></a>00025 <span class="preprocessor">#include <unistd.h></span> <a name="l00026"></a>00026 <span class="preprocessor">#include <sys/types.h></span> <a name="l00027"></a>00027 <span class="preprocessor">#include <sys/stat.h></span> <a name="l00028"></a>00028 <span class="preprocessor">#include <sys/socket.h></span> <a name="l00029"></a>00029 <span class="preprocessor">#include <sys/time.h></span> <a name="l00030"></a>00030 <span class="preprocessor">#include <sys/un.h></span> <a name="l00031"></a>00031 <span class="preprocessor">#include <sys/ioctl.h></span> <a name="l00032"></a>00032 <span class="preprocessor">#include <errno.h></span> <a name="l00033"></a>00033 <span class="preprocessor">#include <stdio.h></span> <a name="l00034"></a>00034 <span class="preprocessor">#include <time.h></span> <a name="l00035"></a>00035 <span class="preprocessor">#include <string.h></span> <a name="l00036"></a>00036 <span class="preprocessor">#include <stdlib.h></span> <a name="l00037"></a>00037 <span class="preprocessor">#ifdef HAVE_SYS_FILIO_H</span> <a name="l00038"></a>00038 <span class="preprocessor"></span><span class="preprocessor">#include <sys/filio.h></span> <a name="l00039"></a>00039 <span class="preprocessor">#endif</span> <a name="l00040"></a>00040 <span class="preprocessor"></span> <a name="l00041"></a>00041 <span class="preprocessor">#include "misc.h"</span> <a name="l00042"></a>00042 <span class="preprocessor">#include "<a class="code" href="pcscd_8h.html" title="This keeps a list of defines for pcsc-lite.">pcscd.h</a>"</span> <a name="l00043"></a>00043 <span class="preprocessor">#include "<a class="code" href="winscard_8h.html" title="This handles smartcard reader communications.">winscard.h</a>"</span> <a name="l00044"></a>00044 <span class="preprocessor">#include "<a class="code" href="debuglog_8h.html" title="This handles debugging.">debuglog.h</a>"</span> <a name="l00045"></a>00045 <span class="preprocessor">#include "<a class="code" href="winscard__msg_8h.html" title="This defines some structures and #defines to be used over the transport layer.">winscard_msg.h</a>"</span> <a name="l00046"></a>00046 <span class="preprocessor">#include "<a class="code" href="sys__generic_8h.html" title="This handles abstract system level calls.">sys_generic.h</a>"</span> <a name="l00047"></a>00047 <span class="preprocessor">#include "utils.h"</span> <a name="l00048"></a>00048 <span class="preprocessor">#include "<a class="code" href="strlcpycat_8h.html" title="prototypes of strlcpy()/strlcat() imported from OpenBSD">strlcpycat.h</a>"</span> <a name="l00049"></a>00049 <a name="l00050"></a>00050 <span class="preprocessor">#ifdef PCSCD</span> <a name="l00051"></a>00051 <span class="preprocessor"></span> <a name="l00052"></a>00052 <span class="comment">/* functions used by pcscd only */</span> <a name="l00053"></a>00053 <a name="l00054"></a>00054 <span class="preprocessor">#else</span> <a name="l00055"></a>00055 <span class="preprocessor"></span> <a name="l00056"></a>00056 <span class="comment">/* functions used by libpcsclite only */</span> <a name="l00057"></a>00057 <a name="l00058"></a>00058 <span class="keywordtype">char</span> *getSocketName(<span class="keywordtype">void</span>) <a name="l00059"></a>00059 { <a name="l00060"></a>00060 <span class="keyword">static</span> <span class="keywordtype">char</span> socketName[<span class="keyword">sizeof</span>(<span class="keyword">struct </span>sockaddr_un)]; <a name="l00061"></a>00061 <a name="l00062"></a>00062 <span class="keywordflow">if</span> (<span class="charliteral">'\0'</span> == socketName[0]) <a name="l00063"></a>00063 { <a name="l00064"></a>00064 <span class="comment">/* socket name not yet initialized */</span> <a name="l00065"></a>00065 <span class="keywordtype">char</span> *socketNameEnv; <a name="l00066"></a>00066 <a name="l00067"></a>00067 socketNameEnv = getenv(<span class="stringliteral">"PCSCLITE_CSOCK_NAME"</span>); <a name="l00068"></a>00068 <span class="keywordflow">if</span> (socketNameEnv) <a name="l00069"></a>00069 strlcpy(socketName, socketNameEnv, <span class="keyword">sizeof</span>(socketName)); <a name="l00070"></a>00070 <span class="keywordflow">else</span> <a name="l00071"></a>00071 strlcpy(socketName, PCSCLITE_CSOCK_NAME, <span class="keyword">sizeof</span>(socketName)); <a name="l00072"></a>00072 } <a name="l00073"></a>00073 <a name="l00074"></a>00074 <span class="keywordflow">return</span> socketName; <a name="l00075"></a>00075 } <a name="l00076"></a>00076 <a name="l00090"></a><a class="code" href="winscard__msg_8h.html#ab19f0d98aa90725ca558be816dab3b59">00090</a> INTERNAL <span class="keywordtype">int</span> <a class="code" href="winscard__msg_8c.html#a616a7d779449d1dc6a8a5d925d8212b1" title="Prepares a communication channel for the client to talk to the server.">ClientSetupSession</a>(uint32_t *pdwClientID) <a name="l00091"></a>00091 { <a name="l00092"></a>00092 <span class="keyword">struct </span>sockaddr_un svc_addr; <a name="l00093"></a>00093 <span class="keywordtype">int</span> one; <a name="l00094"></a>00094 <span class="keywordtype">int</span> ret; <a name="l00095"></a>00095 <span class="keywordtype">char</span> *socketName; <a name="l00096"></a>00096 <a name="l00097"></a>00097 ret = socket(PF_UNIX, SOCK_STREAM, 0); <a name="l00098"></a>00098 <span class="keywordflow">if</span> (ret < 0) <a name="l00099"></a>00099 { <a name="l00100"></a>00100 Log2(PCSC_LOG_CRITICAL, <span class="stringliteral">"Error: create on client socket: %s"</span>, <a name="l00101"></a>00101 strerror(errno)); <a name="l00102"></a>00102 <span class="keywordflow">return</span> -1; <a name="l00103"></a>00103 } <a name="l00104"></a>00104 *pdwClientID = ret; <a name="l00105"></a>00105 <a name="l00106"></a>00106 socketName = getSocketName(); <a name="l00107"></a>00107 svc_addr.sun_family = AF_UNIX; <a name="l00108"></a>00108 strncpy(svc_addr.sun_path, socketName, <span class="keyword">sizeof</span>(svc_addr.sun_path)); <a name="l00109"></a>00109 <a name="l00110"></a>00110 <span class="keywordflow">if</span> (connect(*pdwClientID, (<span class="keyword">struct</span> sockaddr *) &svc_addr, <a name="l00111"></a>00111 <span class="keyword">sizeof</span>(svc_addr.sun_family) + strlen(svc_addr.sun_path) + 1) < 0) <a name="l00112"></a>00112 { <a name="l00113"></a>00113 Log3(PCSC_LOG_CRITICAL, <span class="stringliteral">"Error: connect to client socket %s: %s"</span>, <a name="l00114"></a>00114 socketName, strerror(errno)); <a name="l00115"></a>00115 (void)close(*pdwClientID); <a name="l00116"></a>00116 <span class="keywordflow">return</span> -1; <a name="l00117"></a>00117 } <a name="l00118"></a>00118 <a name="l00119"></a>00119 one = 1; <a name="l00120"></a>00120 <span class="keywordflow">if</span> (ioctl(*pdwClientID, FIONBIO, &one) < 0) <a name="l00121"></a>00121 { <a name="l00122"></a>00122 Log3(PCSC_LOG_CRITICAL, <span class="stringliteral">"Error: cannot set socket %s nonblocking: %s"</span>, <a name="l00123"></a>00123 socketName, strerror(errno)); <a name="l00124"></a>00124 (void)close(*pdwClientID); <a name="l00125"></a>00125 <span class="keywordflow">return</span> -1; <a name="l00126"></a>00126 } <a name="l00127"></a>00127 <a name="l00128"></a>00128 <span class="keywordflow">return</span> 0; <a name="l00129"></a>00129 } <a name="l00130"></a>00130 <a name="l00138"></a><a class="code" href="winscard__msg_8h.html#a3ac9199c5e346cd4d9729e190d9ae0e8">00138</a> INTERNAL <span class="keywordtype">int</span> <a class="code" href="winscard__msg_8c.html#a43b3c0b77aeac885864df13a39b5d44f" title="Closes the socket used by the client to communicate with the server.">ClientCloseSession</a>(uint32_t dwClientID) <a name="l00139"></a>00139 { <a name="l00140"></a>00140 <span class="keywordflow">return</span> close(dwClientID); <a name="l00141"></a>00141 } <a name="l00142"></a>00142 <a name="l00159"></a><a class="code" href="winscard__msg_8h.html#a3c7f8cd86b5717adc9a32546159585ce">00159</a> INTERNAL LONG <a class="code" href="winscard__msg_8c.html#a882edc5b2e3fe0d8a70a4ffd00d8455c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceiveTimeout</a>(uint32_t command, <span class="keywordtype">void</span> *buffer_void, <a name="l00160"></a>00160 uint64_t buffer_size, int32_t filedes, int32_t timeOut) <a name="l00161"></a>00161 { <a name="l00162"></a>00162 <span class="keywordtype">char</span> *buffer = buffer_void; <a name="l00163"></a>00163 <a name="l00164"></a>00164 <span class="comment">/* default is success */</span> <a name="l00165"></a>00165 LONG retval = <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>; <a name="l00166"></a>00166 <a name="l00167"></a>00167 <span class="comment">/* record the time when we started */</span> <a name="l00168"></a>00168 <span class="keyword">struct </span>timeval start; <a name="l00169"></a>00169 <a name="l00170"></a>00170 <span class="comment">/* how many bytes we must read */</span> <a name="l00171"></a>00171 <span class="keywordtype">size_t</span> remaining = buffer_size; <a name="l00172"></a>00172 <a name="l00173"></a>00173 gettimeofday(&start, NULL); <a name="l00174"></a>00174 <a name="l00175"></a>00175 <span class="comment">/* repeat until we get the whole message */</span> <a name="l00176"></a>00176 <span class="keywordflow">while</span> (remaining > 0) <a name="l00177"></a>00177 { <a name="l00178"></a>00178 fd_set read_fd; <a name="l00179"></a>00179 <span class="keyword">struct </span>timeval timeout, now; <a name="l00180"></a>00180 <span class="keywordtype">int</span> selret; <a name="l00181"></a>00181 <span class="keywordtype">long</span> delta; <a name="l00182"></a>00182 <a name="l00183"></a>00183 gettimeofday(&now, NULL); <a name="l00184"></a>00184 delta = <a class="code" href="utils_8c.html#a470f4ac4abe231a7b3346be4fe455912" title="return the difference (as long int) in µs between 2 struct timeval r = a - b">time_sub</a>(&now, &start); <a name="l00185"></a>00185 <a name="l00186"></a>00186 <span class="keywordflow">if</span> (delta > timeOut*1000) <a name="l00187"></a>00187 { <a name="l00188"></a>00188 <span class="comment">/* we already timed out */</span> <a name="l00189"></a>00189 retval = <a class="code" href="pcsclite_8h.html#aa01265b3f55d4f18cdeb4f9cd736b508" title="The user-specified timeout value has expired.">SCARD_E_TIMEOUT</a>; <a name="l00190"></a>00190 <span class="keywordflow">break</span>; <a name="l00191"></a>00191 } <a name="l00192"></a>00192 <a name="l00193"></a>00193 <span class="comment">/* remaining time to wait */</span> <a name="l00194"></a>00194 delta = timeOut*1000 - delta; <a name="l00195"></a>00195 <a name="l00196"></a>00196 FD_ZERO(&read_fd); <a name="l00197"></a>00197 FD_SET(filedes, &read_fd); <a name="l00198"></a>00198 <a name="l00199"></a>00199 timeout.tv_sec = delta/1000000; <a name="l00200"></a>00200 timeout.tv_usec = delta - timeout.tv_sec*1000000; <a name="l00201"></a>00201 <a name="l00202"></a>00202 selret = select(filedes + 1, &read_fd, NULL, NULL, &timeout); <a name="l00203"></a>00203 <a name="l00204"></a>00204 <span class="comment">/* try to read only when socket is readable */</span> <a name="l00205"></a>00205 <span class="keywordflow">if</span> (selret > 0) <a name="l00206"></a>00206 { <a name="l00207"></a>00207 <span class="keywordtype">int</span> readed; <a name="l00208"></a>00208 <a name="l00209"></a>00209 <span class="keywordflow">if</span> (!FD_ISSET(filedes, &read_fd)) <a name="l00210"></a>00210 { <a name="l00211"></a>00211 <span class="comment">/* very strange situation. it should be an assert really */</span> <a name="l00212"></a>00212 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00213"></a>00213 <span class="keywordflow">break</span>; <a name="l00214"></a>00214 } <a name="l00215"></a>00215 readed = read(filedes, buffer, remaining); <a name="l00216"></a>00216 <a name="l00217"></a>00217 <span class="keywordflow">if</span> (readed > 0) <a name="l00218"></a>00218 { <a name="l00219"></a>00219 <span class="comment">/* we got something */</span> <a name="l00220"></a>00220 buffer += readed; <a name="l00221"></a>00221 remaining -= readed; <a name="l00222"></a>00222 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (readed == 0) <a name="l00223"></a>00223 { <a name="l00224"></a>00224 <span class="comment">/* peer closed the socket */</span> <a name="l00225"></a>00225 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00226"></a>00226 <span class="keywordflow">break</span>; <a name="l00227"></a>00227 } <span class="keywordflow">else</span> <a name="l00228"></a>00228 { <a name="l00229"></a>00229 <span class="comment">/* we ignore the signals and empty socket situations, all</span> <a name="l00230"></a>00230 <span class="comment"> * other errors are fatal */</span> <a name="l00231"></a>00231 <span class="keywordflow">if</span> (errno != EINTR && errno != EAGAIN) <a name="l00232"></a>00232 { <a name="l00233"></a>00233 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00234"></a>00234 <span class="keywordflow">break</span>; <a name="l00235"></a>00235 } <a name="l00236"></a>00236 } <a name="l00237"></a>00237 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (selret == 0) <a name="l00238"></a>00238 { <a name="l00239"></a>00239 <span class="comment">/* is the daemon still there? */</span> <a name="l00240"></a>00240 retval = <a class="code" href="winscard__clnt_8c.html#aaa02e1f70d68a502cd92ed066c44542f" title="Checks if the server is running.">SCardCheckDaemonAvailability</a>(); <a name="l00241"></a>00241 <span class="keywordflow">if</span> (retval != <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>) <a name="l00242"></a>00242 { <a name="l00243"></a>00243 <span class="comment">/* timeout */</span> <a name="l00244"></a>00244 <span class="keywordflow">break</span>; <a name="l00245"></a>00245 } <a name="l00246"></a>00246 <a name="l00247"></a>00247 <span class="comment">/* you need to set the env variable PCSCLITE_DEBUG=0 since</span> <a name="l00248"></a>00248 <span class="comment"> * this is logged on the client side and not on the pcscd</span> <a name="l00249"></a>00249 <span class="comment"> * side*/</span> <a name="l00250"></a>00250 Log2(PCSC_LOG_INFO, <span class="stringliteral">"Command 0x%X not yet finished"</span>, command); <a name="l00251"></a>00251 } <span class="keywordflow">else</span> <a name="l00252"></a>00252 { <a name="l00253"></a>00253 <span class="comment">/* we ignore signals, all other errors are fatal */</span> <a name="l00254"></a>00254 <span class="keywordflow">if</span> (errno != EINTR) <a name="l00255"></a>00255 { <a name="l00256"></a>00256 Log2(PCSC_LOG_ERROR, <span class="stringliteral">"select returns with failure: %s"</span>, <a name="l00257"></a>00257 strerror(errno)); <a name="l00258"></a>00258 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00259"></a>00259 <span class="keywordflow">break</span>; <a name="l00260"></a>00260 } <a name="l00261"></a>00261 } <a name="l00262"></a>00262 } <a name="l00263"></a>00263 <a name="l00264"></a>00264 <span class="keywordflow">return</span> retval; <a name="l00265"></a>00265 } <a name="l00266"></a>00266 <a name="l00281"></a><a class="code" href="winscard__msg_8h.html#a5bb74102a7d2387f14d7341f1894ee3e">00281</a> INTERNAL LONG <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(uint32_t command, uint32_t dwClientID, <a name="l00282"></a>00282 uint64_t size, <span class="keywordtype">void</span> *data_void) <a name="l00283"></a>00283 { <a name="l00284"></a>00284 <span class="keyword">struct </span><a class="code" href="structrxHeader.html" title="header structure for client/server message data exchange.">rxHeader</a> header; <a name="l00285"></a>00285 LONG ret; <a name="l00286"></a>00286 <a name="l00287"></a>00287 <span class="comment">/* header */</span> <a name="l00288"></a>00288 header.<a class="code" href="structrxHeader.html#af7e471411e95448316f756e3905fdcce" title="one of the pcsc_msg_commands ">command</a> = <a class="code" href="structrxHeader.html#af7e471411e95448316f756e3905fdcce" title="one of the pcsc_msg_commands ">command</a>; <a name="l00289"></a>00289 header.<a class="code" href="structrxHeader.html#ab2c6b258f02add8fdf4cfc7c371dd772" title="size of the message excluding this header">size</a> = <a class="code" href="structrxHeader.html#ab2c6b258f02add8fdf4cfc7c371dd772" title="size of the message excluding this header">size</a>; <a name="l00290"></a>00290 ret = <a class="code" href="winscard__msg_8c.html#aacd09139ce5667d3b651de29c0d62057" title="Sends a menssage from client to server or vice-versa.">MessageSend</a>(&header, <span class="keyword">sizeof</span>(header), dwClientID); <a name="l00291"></a>00291 <a name="l00292"></a>00292 <span class="comment">/* command */</span> <a name="l00293"></a>00293 <span class="keywordflow">if</span> (size > 0) <a name="l00294"></a>00294 ret = <a class="code" href="winscard__msg_8c.html#aacd09139ce5667d3b651de29c0d62057" title="Sends a menssage from client to server or vice-versa.">MessageSend</a>(data_void, size, dwClientID); <a name="l00295"></a>00295 <a name="l00296"></a>00296 <span class="keywordflow">return</span> ret; <a name="l00297"></a>00297 } <a name="l00298"></a>00298 <a name="l00299"></a>00299 <span class="preprocessor">#endif</span> <a name="l00300"></a>00300 <span class="preprocessor"></span> <a name="l00301"></a>00301 <span class="comment">/* functions used by pcscd and libpcsclite */</span> <a name="l00302"></a>00302 <a name="l00317"></a><a class="code" href="winscard__msg_8h.html#a05d22dbf58a6d7ab9b0c1ae77049f533">00317</a> INTERNAL LONG <a class="code" href="winscard__msg_8c.html#aacd09139ce5667d3b651de29c0d62057" title="Sends a menssage from client to server or vice-versa.">MessageSend</a>(<span class="keywordtype">void</span> *buffer_void, uint64_t buffer_size, <a name="l00318"></a>00318 int32_t filedes) <a name="l00319"></a>00319 { <a name="l00320"></a>00320 <span class="keywordtype">char</span> *buffer = buffer_void; <a name="l00321"></a>00321 <a name="l00322"></a>00322 <span class="comment">/* default is success */</span> <a name="l00323"></a>00323 LONG retval = <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>; <a name="l00324"></a>00324 <a name="l00325"></a>00325 <span class="comment">/* how many bytes remains to be written */</span> <a name="l00326"></a>00326 <span class="keywordtype">size_t</span> remaining = buffer_size; <a name="l00327"></a>00327 <a name="l00328"></a>00328 <span class="comment">/* repeat until all data is written */</span> <a name="l00329"></a>00329 <span class="keywordflow">while</span> (remaining > 0) <a name="l00330"></a>00330 { <a name="l00331"></a>00331 fd_set write_fd; <a name="l00332"></a>00332 <span class="keywordtype">int</span> selret; <a name="l00333"></a>00333 <a name="l00334"></a>00334 FD_ZERO(&write_fd); <a name="l00335"></a>00335 FD_SET(filedes, &write_fd); <a name="l00336"></a>00336 <a name="l00337"></a>00337 selret = select(filedes + 1, NULL, &write_fd, NULL, NULL); <a name="l00338"></a>00338 <a name="l00339"></a>00339 <span class="comment">/* try to write only when the file descriptor is writable */</span> <a name="l00340"></a>00340 <span class="keywordflow">if</span> (selret > 0) <a name="l00341"></a>00341 { <a name="l00342"></a>00342 <span class="keywordtype">int</span> written; <a name="l00343"></a>00343 <a name="l00344"></a>00344 <span class="keywordflow">if</span> (!FD_ISSET(filedes, &write_fd)) <a name="l00345"></a>00345 { <a name="l00346"></a>00346 <span class="comment">/* very strange situation. it should be an assert really */</span> <a name="l00347"></a>00347 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00348"></a>00348 <span class="keywordflow">break</span>; <a name="l00349"></a>00349 } <a name="l00350"></a>00350 <span class="comment">/* since we are a user library we can't play with signals</span> <a name="l00351"></a>00351 <span class="comment"> * The signals may already be used by the application */</span> <a name="l00352"></a>00352 <span class="preprocessor">#ifdef MSG_NOSIGNAL</span> <a name="l00353"></a>00353 <span class="preprocessor"></span> <span class="comment">/* Get EPIPE return code instead of SIGPIPE signal</span> <a name="l00354"></a>00354 <span class="comment"> * Works on Linux */</span> <a name="l00355"></a>00355 written = send(filedes, buffer, remaining, MSG_NOSIGNAL); <a name="l00356"></a>00356 <span class="preprocessor">#else</span> <a name="l00357"></a>00357 <span class="preprocessor"></span> <span class="comment">/* we may get a SIGPIPE signal if the other side has closed */</span> <a name="l00358"></a>00358 written = write(filedes, buffer, remaining); <a name="l00359"></a>00359 <span class="preprocessor">#endif</span> <a name="l00360"></a>00360 <span class="preprocessor"></span> <a name="l00361"></a>00361 <span class="keywordflow">if</span> (written > 0) <a name="l00362"></a>00362 { <a name="l00363"></a>00363 <span class="comment">/* we wrote something */</span> <a name="l00364"></a>00364 buffer += written; <a name="l00365"></a>00365 remaining -= written; <a name="l00366"></a>00366 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (written == 0) <a name="l00367"></a>00367 { <a name="l00368"></a>00368 <span class="comment">/* peer closed the socket */</span> <a name="l00369"></a>00369 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00370"></a>00370 <span class="keywordflow">break</span>; <a name="l00371"></a>00371 } <span class="keywordflow">else</span> <a name="l00372"></a>00372 { <a name="l00373"></a>00373 <span class="comment">/* we ignore the signals and socket full situations, all</span> <a name="l00374"></a>00374 <span class="comment"> * other errors are fatal */</span> <a name="l00375"></a>00375 <span class="keywordflow">if</span> (errno != EINTR && errno != EAGAIN) <a name="l00376"></a>00376 { <a name="l00377"></a>00377 retval = <a class="code" href="pcsclite_8h.html#ad4729ab109ff490285d2ad881c04bee8" title="The Smart card resource manager is not running.">SCARD_E_NO_SERVICE</a>; <a name="l00378"></a>00378 <span class="keywordflow">break</span>; <a name="l00379"></a>00379 } <a name="l00380"></a>00380 } <a name="l00381"></a>00381 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (selret == 0) <a name="l00382"></a>00382 { <a name="l00383"></a>00383 <span class="comment">/* timeout */</span> <a name="l00384"></a>00384 retval = <a class="code" href="pcsclite_8h.html#aa01265b3f55d4f18cdeb4f9cd736b508" title="The user-specified timeout value has expired.">SCARD_E_TIMEOUT</a>; <a name="l00385"></a>00385 <span class="keywordflow">break</span>; <a name="l00386"></a>00386 } <span class="keywordflow">else</span> <a name="l00387"></a>00387 { <a name="l00388"></a>00388 <span class="comment">/* ignore signals */</span> <a name="l00389"></a>00389 <span class="keywordflow">if</span> (errno != EINTR) <a name="l00390"></a>00390 { <a name="l00391"></a>00391 Log2(PCSC_LOG_ERROR, <span class="stringliteral">"select returns with failure: %s"</span>, <a name="l00392"></a>00392 strerror(errno)); <a name="l00393"></a>00393 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00394"></a>00394 <span class="keywordflow">break</span>; <a name="l00395"></a>00395 } <a name="l00396"></a>00396 } <a name="l00397"></a>00397 } <a name="l00398"></a>00398 <a name="l00399"></a>00399 <span class="keywordflow">return</span> retval; <a name="l00400"></a>00400 } <a name="l00401"></a>00401 <a name="l00415"></a><a class="code" href="winscard__msg_8h.html#a081a9b923dff2a0a831b604dd7d1267b">00415</a> INTERNAL LONG <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(<span class="keywordtype">void</span> *buffer_void, uint64_t buffer_size, <a name="l00416"></a>00416 int32_t filedes) <a name="l00417"></a>00417 { <a name="l00418"></a>00418 <span class="keywordtype">char</span> *buffer = buffer_void; <a name="l00419"></a>00419 <a name="l00420"></a>00420 <span class="comment">/* default is success */</span> <a name="l00421"></a>00421 LONG retval = <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>; <a name="l00422"></a>00422 <a name="l00423"></a>00423 <span class="comment">/* how many bytes we must read */</span> <a name="l00424"></a>00424 <span class="keywordtype">size_t</span> remaining = buffer_size; <a name="l00425"></a>00425 <a name="l00426"></a>00426 <span class="comment">/* repeat until we get the whole message */</span> <a name="l00427"></a>00427 <span class="keywordflow">while</span> (remaining > 0) <a name="l00428"></a>00428 { <a name="l00429"></a>00429 fd_set read_fd; <a name="l00430"></a>00430 <span class="keywordtype">int</span> selret; <a name="l00431"></a>00431 <a name="l00432"></a>00432 FD_ZERO(&read_fd); <a name="l00433"></a>00433 FD_SET(filedes, &read_fd); <a name="l00434"></a>00434 <a name="l00435"></a>00435 selret = select(filedes + 1, &read_fd, NULL, NULL, NULL); <a name="l00436"></a>00436 <a name="l00437"></a>00437 <span class="comment">/* try to read only when socket is readable */</span> <a name="l00438"></a>00438 <span class="keywordflow">if</span> (selret > 0) <a name="l00439"></a>00439 { <a name="l00440"></a>00440 <span class="keywordtype">int</span> readed; <a name="l00441"></a>00441 <a name="l00442"></a>00442 <span class="keywordflow">if</span> (!FD_ISSET(filedes, &read_fd)) <a name="l00443"></a>00443 { <a name="l00444"></a>00444 <span class="comment">/* very strange situation. it should be an assert really */</span> <a name="l00445"></a>00445 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00446"></a>00446 <span class="keywordflow">break</span>; <a name="l00447"></a>00447 } <a name="l00448"></a>00448 readed = read(filedes, buffer, remaining); <a name="l00449"></a>00449 <a name="l00450"></a>00450 <span class="keywordflow">if</span> (readed > 0) <a name="l00451"></a>00451 { <a name="l00452"></a>00452 <span class="comment">/* we got something */</span> <a name="l00453"></a>00453 buffer += readed; <a name="l00454"></a>00454 remaining -= readed; <a name="l00455"></a>00455 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (readed == 0) <a name="l00456"></a>00456 { <a name="l00457"></a>00457 <span class="comment">/* peer closed the socket */</span> <a name="l00458"></a>00458 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00459"></a>00459 <span class="keywordflow">break</span>; <a name="l00460"></a>00460 } <span class="keywordflow">else</span> <a name="l00461"></a>00461 { <a name="l00462"></a>00462 <span class="comment">/* we ignore the signals and empty socket situations, all</span> <a name="l00463"></a>00463 <span class="comment"> * other errors are fatal */</span> <a name="l00464"></a>00464 <span class="keywordflow">if</span> (errno != EINTR && errno != EAGAIN) <a name="l00465"></a>00465 { <a name="l00466"></a>00466 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00467"></a>00467 <span class="keywordflow">break</span>; <a name="l00468"></a>00468 } <a name="l00469"></a>00469 } <a name="l00470"></a>00470 } <a name="l00471"></a>00471 <span class="keywordflow">else</span> <a name="l00472"></a>00472 { <a name="l00473"></a>00473 <span class="comment">/* we ignore signals, all other errors are fatal */</span> <a name="l00474"></a>00474 <span class="keywordflow">if</span> (errno != EINTR) <a name="l00475"></a>00475 { <a name="l00476"></a>00476 Log2(PCSC_LOG_ERROR, <span class="stringliteral">"select returns with failure: %s"</span>, <a name="l00477"></a>00477 strerror(errno)); <a name="l00478"></a>00478 retval = <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00479"></a>00479 <span class="keywordflow">break</span>; <a name="l00480"></a>00480 } <a name="l00481"></a>00481 } <a name="l00482"></a>00482 } <a name="l00483"></a>00483 <a name="l00484"></a>00484 <span class="keywordflow">return</span> retval; <a name="l00485"></a>00485 } <a name="l00486"></a>00486 </pre></div></div> </div> <hr class="footer"/><address class="footer"><small>Generated on Thu Sep 15 2011 for pcsc-lite by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address> </body> </html>