26 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include <sys/ioctl.h>
37 #ifdef HAVE_SYS_FILIO_H
38 #include <sys/filio.h>
58 char *getSocketName(
void)
60 static char socketName[
sizeof(
struct sockaddr_un)];
62 if (
'\0' == socketName[0])
67 socketNameEnv = getenv(
"PCSCLITE_CSOCK_NAME");
69 strlcpy(socketName, socketNameEnv,
sizeof(socketName));
71 strlcpy(socketName, PCSCLITE_CSOCK_NAME,
sizeof(socketName));
92 struct sockaddr_un svc_addr;
96 ret = socket(PF_UNIX, SOCK_STREAM, 0);
99 Log2(PCSC_LOG_CRITICAL,
"Error: create on client socket: %s",
105 socketName = getSocketName();
106 svc_addr.sun_family = AF_UNIX;
107 strncpy(svc_addr.sun_path, socketName,
sizeof(svc_addr.sun_path));
109 if (connect(*pdwClientID, (
struct sockaddr *) &svc_addr,
110 sizeof(svc_addr.sun_family) + strlen(svc_addr.sun_path) + 1) < 0)
112 Log3(PCSC_LOG_CRITICAL,
"Error: connect to client socket %s: %s",
113 socketName, strerror(errno));
114 (void)close(*pdwClientID);
118 ret = fcntl(*pdwClientID, F_GETFL, 0);
121 Log3(PCSC_LOG_CRITICAL,
"Error: cannot retrieve socket %s flags: %s",
122 socketName, strerror(errno));
123 (void)close(*pdwClientID);
127 if (fcntl(*pdwClientID, F_SETFL, ret | O_NONBLOCK) < 0)
129 Log3(PCSC_LOG_CRITICAL,
"Error: cannot set socket %s nonblocking: %s",
130 socketName, strerror(errno));
131 (void)close(*pdwClientID);
147 return close(dwClientID);
167 uint64_t buffer_size, int32_t filedes,
long timeOut)
169 char *buffer = buffer_void;
175 struct timeval start;
178 size_t remaining = buffer_size;
180 gettimeofday(&start, NULL);
183 while (remaining > 0)
186 struct timeval timeout, now;
190 gettimeofday(&now, NULL);
193 if (delta > timeOut*1000)
201 delta = timeOut*1000 - delta;
204 FD_SET(filedes, &read_fd);
206 timeout.tv_sec = delta/1000000;
207 timeout.tv_usec = delta - timeout.tv_sec*1000000;
209 selret = select(filedes + 1, &read_fd, NULL, NULL, &timeout);
216 if (!FD_ISSET(filedes, &read_fd))
222 readed = read(filedes, buffer, remaining);
229 }
else if (readed == 0)
238 if (errno != EINTR && errno != EAGAIN)
244 }
else if (selret == 0)
257 Log2(PCSC_LOG_INFO,
"Command 0x%X not yet finished", command);
263 Log2(PCSC_LOG_ERROR,
"select returns with failure: %s",
289 uint64_t size,
void *data_void)
297 ret =
MessageSend(&header,
sizeof(header), dwClientID);
324 INTERNAL LONG
MessageSend(
void *buffer_void, uint64_t buffer_size,
327 char *buffer = buffer_void;
333 size_t remaining = buffer_size;
336 while (remaining > 0)
342 FD_SET(filedes, &write_fd);
344 selret = select(filedes + 1, NULL, &write_fd, NULL, NULL);
351 if (!FD_ISSET(filedes, &write_fd))
362 written = send(filedes, buffer, remaining, MSG_NOSIGNAL);
365 written = write(filedes, buffer, remaining);
372 remaining -= written;
373 }
else if (written == 0)
382 if (errno != EINTR && errno != EAGAIN)
388 }
else if (selret == 0)
398 Log2(PCSC_LOG_ERROR,
"select returns with failure: %s",
425 char *buffer = buffer_void;
431 size_t remaining = buffer_size;
434 while (remaining > 0)
440 FD_SET(filedes, &read_fd);
442 selret = select(filedes + 1, &read_fd, NULL, NULL, NULL);
449 if (!FD_ISSET(filedes, &read_fd))
455 readed = read(filedes, buffer, remaining);
462 }
else if (readed == 0)
471 if (errno != EINTR && errno != EAGAIN)
483 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.