52 #include <sys/types.h>
54 #include <sys/socket.h>
57 #include <sys/ioctl.h>
63 #ifdef HAVE_SYS_FILIO_H
64 #include <sys/filio.h>
84 char *getSocketName(
void)
86 static char socketName[
sizeof(
struct sockaddr_un)];
88 if (
'\0' == socketName[0])
93 socketNameEnv = getenv(
"PCSCLITE_CSOCK_NAME");
95 strlcpy(socketName, socketNameEnv,
sizeof(socketName));
97 strlcpy(socketName, PCSCLITE_CSOCK_NAME,
sizeof(socketName));
118 struct sockaddr_un svc_addr;
122 ret = socket(PF_UNIX, SOCK_STREAM, 0);
125 Log2(PCSC_LOG_CRITICAL,
"Error: create on client socket: %s",
131 socketName = getSocketName();
132 svc_addr.sun_family = AF_UNIX;
133 strncpy(svc_addr.sun_path, socketName,
sizeof(svc_addr.sun_path));
135 if (connect(*pdwClientID, (
struct sockaddr *) &svc_addr,
136 sizeof(svc_addr.sun_family) + strlen(svc_addr.sun_path) + 1) < 0)
138 Log3(PCSC_LOG_CRITICAL,
"Error: connect to client socket %s: %s",
139 socketName, strerror(errno));
140 (void)close(*pdwClientID);
144 ret = fcntl(*pdwClientID, F_GETFL, 0);
147 Log3(PCSC_LOG_CRITICAL,
"Error: cannot retrieve socket %s flags: %s",
148 socketName, strerror(errno));
149 (void)close(*pdwClientID);
153 if (fcntl(*pdwClientID, F_SETFL, ret | O_NONBLOCK) < 0)
155 Log3(PCSC_LOG_CRITICAL,
"Error: cannot set socket %s nonblocking: %s",
156 socketName, strerror(errno));
157 (void)close(*pdwClientID);
173 return close(dwClientID);
193 uint64_t buffer_size, int32_t filedes,
long timeOut)
195 char *buffer = buffer_void;
201 struct timeval start;
204 size_t remaining = buffer_size;
206 gettimeofday(&start, NULL);
209 while (remaining > 0)
212 struct timeval timeout, now;
216 gettimeofday(&now, NULL);
219 if (delta > timeOut*1000)
227 delta = timeOut*1000 - delta;
230 FD_SET(filedes, &read_fd);
232 timeout.tv_sec = delta/1000000;
233 timeout.tv_usec = delta - timeout.tv_sec*1000000;
235 selret = select(filedes + 1, &read_fd, NULL, NULL, &timeout);
242 if (!FD_ISSET(filedes, &read_fd))
248 readed = read(filedes, buffer, remaining);
255 }
else if (readed == 0)
264 if (errno != EINTR && errno != EAGAIN)
270 }
else if (selret == 0)
283 Log2(PCSC_LOG_INFO,
"Command 0x%X not yet finished", command);
289 Log2(PCSC_LOG_ERROR,
"select returns with failure: %s",
315 uint64_t size,
void *data_void)
323 ret =
MessageSend(&header,
sizeof(header), dwClientID);
350 INTERNAL LONG
MessageSend(
void *buffer_void, uint64_t buffer_size,
353 char *buffer = buffer_void;
359 size_t remaining = buffer_size;
362 while (remaining > 0)
368 FD_SET(filedes, &write_fd);
370 selret = select(filedes + 1, NULL, &write_fd, NULL, NULL);
377 if (!FD_ISSET(filedes, &write_fd))
388 written = send(filedes, buffer, remaining, MSG_NOSIGNAL);
391 written = write(filedes, buffer, remaining);
398 remaining -= written;
399 }
else if (written == 0)
408 if (errno != EINTR && errno != EAGAIN)
414 }
else if (selret == 0)
424 Log2(PCSC_LOG_ERROR,
"select returns with failure: %s",
451 char *buffer = buffer_void;
457 size_t remaining = buffer_size;
460 while (remaining > 0)
466 FD_SET(filedes, &read_fd);
468 selret = select(filedes + 1, &read_fd, NULL, NULL, NULL);
475 if (!FD_ISSET(filedes, &read_fd))
481 readed = read(filedes, buffer, remaining);
488 }
else if (readed == 0)
497 if (errno != EINTR && errno != EAGAIN)
509 Log2(PCSC_LOG_ERROR,
"select returns with failure: %s",
INTERNAL int ClientCloseSession(uint32_t dwClientID)
Closes the socket used by the client to communicate with the server.
INTERNAL LONG MessageSendWithHeader(uint32_t command, uint32_t dwClientID, uint64_t size, void *data_void)
Wrapper for the MessageSend() function.
#define SCARD_F_COMM_ERROR
An internal communications error has been detected.
This handles abstract system level calls.
#define SCARD_E_NO_SERVICE
The Smart card resource manager is not running.
INTERNAL LONG MessageReceive(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Called by the Client to get the reponse from the server or vice-versa.
prototypes of strlcpy()/strlcat() imported from OpenBSD
This defines some structures and #defines to be used over the transport layer.
long int time_sub(struct timeval *a, struct timeval *b)
return the difference (as long int) in µs between 2 struct timeval r = a - b
LONG SCardCheckDaemonAvailability(void)
Checks if the server is running.
INTERNAL int ClientSetupSession(uint32_t *pdwClientID)
Prepares a communication channel for the client to talk to the server.
This keeps a list of defines for pcsc-lite.
INTERNAL LONG MessageSend(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Sends a menssage from client to server or vice-versa.
#define SCARD_S_SUCCESS
error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx
This handles smart card reader communications.
INTERNAL LONG MessageReceiveTimeout(uint32_t command, void *buffer_void, uint64_t buffer_size, int32_t filedes, long timeOut)
Called by the Client to get the reponse from the server or vice-versa.
#define SCARD_E_TIMEOUT
The user-specified timeout value has expired.