47 #include <sys/types.h> 49 #include <sys/socket.h> 52 #include <sys/ioctl.h> 58 #ifdef HAVE_SYS_FILIO_H 59 #include <sys/filio.h> 79 #define SOCK_CLOEXEC 0 82 char *getSocketName(
void)
84 static char socketName[
sizeof(
struct sockaddr_un)];
86 if (
'\0' == socketName[0])
91 socketNameEnv = getenv(
"PCSCLITE_CSOCK_NAME");
93 strncpy(socketName, socketNameEnv,
sizeof(socketName));
95 strncpy(socketName, PCSCLITE_CSOCK_NAME,
sizeof(socketName));
98 socketName[
sizeof socketName -1] =
'\0';
119 struct sockaddr_un svc_addr;
123 ret = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
126 Log2(PCSC_LOG_CRITICAL,
"Error: create on client socket: %s",
132 socketName = getSocketName();
133 svc_addr.sun_family = AF_UNIX;
134 strncpy(svc_addr.sun_path, socketName,
sizeof(svc_addr.sun_path));
136 if (connect(*pdwClientID, (
struct sockaddr *) &svc_addr,
137 sizeof(svc_addr.sun_family) + strlen(svc_addr.sun_path) + 1) < 0)
139 Log3(PCSC_LOG_CRITICAL,
"Error: connect to client socket %s: %s",
140 socketName, strerror(errno));
141 (void)close(*pdwClientID);
145 ret = fcntl(*pdwClientID, F_GETFL, 0);
148 Log3(PCSC_LOG_CRITICAL,
"Error: cannot retrieve socket %s flags: %s",
149 socketName, strerror(errno));
150 (void)close(*pdwClientID);
154 if (fcntl(*pdwClientID, F_SETFL, ret | O_NONBLOCK) < 0)
156 Log3(PCSC_LOG_CRITICAL,
"Error: cannot set socket %s nonblocking: %s",
157 socketName, strerror(errno));
158 (void)close(*pdwClientID);
194 uint64_t buffer_size, int32_t filedes,
long timeOut)
196 char *buffer = buffer_void;
202 struct timeval start;
205 size_t remaining = buffer_size;
207 gettimeofday(&start, NULL);
210 while (remaining > 0)
213 struct timeval timeout, now;
217 gettimeofday(&now, NULL);
220 if (delta > timeOut*1000)
228 delta = timeOut*1000 - delta;
231 FD_SET(filedes, &read_fd);
233 timeout.tv_sec = delta/1000000;
234 timeout.tv_usec = delta - timeout.tv_sec*1000000;
236 selret = select(filedes + 1, &read_fd, NULL, NULL, &timeout);
243 if (!FD_ISSET(filedes, &read_fd))
249 readed = read(filedes, buffer, remaining);
256 }
else if (readed == 0)
265 if (errno != EINTR && errno != EAGAIN)
271 }
else if (selret == 0)
287 Log2(PCSC_LOG_INFO,
"Command 0x%X not yet finished", command);
293 Log2(PCSC_LOG_ERROR,
"select returns with failure: %s",
319 uint64_t size,
void *data_void)
327 ret =
MessageSend(&header,
sizeof(header), dwClientID);
354 INTERNAL LONG
MessageSend(
void *buffer_void, uint64_t buffer_size,
357 char *buffer = buffer_void;
363 size_t remaining = buffer_size;
366 while (remaining > 0)
372 FD_SET(filedes, &write_fd);
374 selret = select(filedes + 1, NULL, &write_fd, NULL, NULL);
381 if (!FD_ISSET(filedes, &write_fd))
392 written = send(filedes, buffer, remaining, MSG_NOSIGNAL);
395 written = write(filedes, buffer, remaining);
402 remaining -= written;
403 }
else if (written == 0)
412 if (errno != EINTR && errno != EAGAIN)
418 }
else if (selret == 0)
428 Log2(PCSC_LOG_ERROR,
"select returns with failure: %s",
455 char *buffer = buffer_void;
461 size_t remaining = buffer_size;
464 while (remaining > 0)
470 FD_SET(filedes, &read_fd);
472 selret = select(filedes + 1, &read_fd, NULL, NULL, NULL);
479 if (!FD_ISSET(filedes, &read_fd))
485 readed = read(filedes, buffer, remaining);
492 }
else if (readed == 0)
501 if (errno != EINTR && errno != EAGAIN)
513 Log2(PCSC_LOG_ERROR,
"select returns with failure: %s",
#define SCARD_S_SUCCESS
No error was encountered.
#define SCARD_E_TIMEOUT
The user-specified timeout value has expired.
#define SCARD_E_NO_SERVICE
The Smart card resource manager is not running.
INTERNAL void 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.
This handles abstract system level calls.
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.
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
#define SCARD_F_COMM_ERROR
An internal communications error has been detected.
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.
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.