49#include <sys/socket.h>
80#define member_size(type, member) sizeof(((type *)0)->member)
82static char SocketName[member_size(
struct sockaddr_un, sun_path)];
83static pthread_once_t SocketName_init_control = PTHREAD_ONCE_INIT;
84static void SocketName_init(
void)
87 const char *socketNameEnv;
89 socketNameEnv =
SYS_GetEnv(
"PCSCLITE_CSOCK_NAME");
91 strncpy(SocketName, socketNameEnv,
sizeof SocketName);
93 strncpy(SocketName, PCSCLITE_CSOCK_NAME,
sizeof SocketName);
96 SocketName[
sizeof SocketName -1] =
'\0';
99char *getSocketName(
void)
101 pthread_once(&SocketName_init_control, SocketName_init);
121 struct sockaddr_un svc_addr;
125 ret = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
128 Log2(PCSC_LOG_CRITICAL,
"Error: create on client socket: %s",
134 socketName = getSocketName();
135 svc_addr.sun_family = AF_UNIX;
136 strncpy(svc_addr.sun_path, socketName,
sizeof(svc_addr.sun_path));
138 if (connect(*pdwClientID, (
struct sockaddr *) &svc_addr,
139 sizeof(svc_addr.sun_family) + strlen(svc_addr.sun_path) + 1) < 0)
141 Log3(PCSC_LOG_CRITICAL,
"Error: connect to client socket %s: %s",
142 socketName, strerror(errno));
143 (void)close(*pdwClientID);
147 ret = fcntl(*pdwClientID, F_GETFL, 0);
150 Log3(PCSC_LOG_CRITICAL,
"Error: cannot retrieve socket %s flags: %s",
151 socketName, strerror(errno));
152 (void)close(*pdwClientID);
156 if (fcntl(*pdwClientID, F_SETFL, ret | O_NONBLOCK) < 0)
158 Log3(PCSC_LOG_CRITICAL,
"Error: cannot set socket %s nonblocking: %s",
159 socketName, strerror(errno));
160 (void)close(*pdwClientID);
196 uint64_t buffer_size, int32_t filedes,
long timeOut)
198 char *buffer = buffer_void;
204 struct timeval start;
207 size_t remaining = buffer_size;
209 gettimeofday(&start, NULL);
212 while (remaining > 0)
214 struct pollfd read_fd;
219 gettimeofday(&now, NULL);
220 delta =
time_sub(&now, &start) / 1000;
230 delta = timeOut - delta;
232 read_fd.fd = filedes;
233 read_fd.events = POLLIN;
236 pollret = poll(&read_fd, 1, delta);
243 if (!(read_fd.revents & POLLIN))
249 bytes_read = read(filedes, buffer, remaining);
254 buffer += bytes_read;
255 remaining -= bytes_read;
256 }
else if (bytes_read == 0)
265 if (errno != EINTR && errno != EAGAIN)
271 }
else if (pollret == 0)
274 retval = SCardCheckDaemonAvailability();
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);
358 char *buffer = buffer_void;
364 size_t remaining = buffer_size;
367 while (remaining > 0)
369 struct pollfd write_fd;
372 write_fd.fd = filedes;
373 write_fd.events = POLLOUT;
374 write_fd.revents = 0;
376 pollret = poll(&write_fd, 1, -1);
383 if (!(write_fd.revents & POLLOUT))
394 written = send(filedes, buffer, remaining, MSG_NOSIGNAL);
397 written = write(filedes, buffer, remaining);
404 remaining -= written;
405 }
else if (written == 0)
414 if (errno != EINTR && errno != EAGAIN)
420 }
else if (pollret == 0)
430 Log2(PCSC_LOG_ERROR,
"select returns with failure: %s",
458 char *buffer = buffer_void;
464 size_t remaining = buffer_size;
467 while (remaining > 0)
469 struct pollfd read_fd;
472 read_fd.fd = filedes;
473 read_fd.events = POLLIN;
476 pollret = poll(&read_fd, 1 , -1);
483 if (!(read_fd.revents & POLLIN))
489 bytes_read = read(filedes, buffer, remaining);
494 buffer += bytes_read;
495 remaining -= bytes_read;
496 }
else if (bytes_read == 0)
505 if (errno != EINTR && errno != EAGAIN)
508 if (ECONNRESET == errno)
521 Log2(PCSC_LOG_ERROR,
"select returns with failure: %s",
#define SCARD_W_SECURITY_VIOLATION
Access was denied because of a security violation.
#define SCARD_S_SUCCESS
No error was encountered.
#define SCARD_F_COMM_ERROR
An internal communications error has been detected.
#define SCARD_E_TIMEOUT
The user-specified timeout value has expired.
#define SCARD_E_NO_SERVICE
The Smart card resource manager is not running.
This handles abstract system level calls.
const char * SYS_GetEnv(const char *name)
(More) secure version of getenv(3)
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
This handles smart card reader communications.
INTERNAL int ClientSetupSession(uint32_t *pdwClientID)
Prepares a communication channel for the client to talk to the server.
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 response from the server or vice-versa.
INTERNAL LONG MessageSendWithHeader(uint32_t command, uint32_t dwClientID, uint64_t size, void *data_void)
Wrapper for the MessageSend() function.
INTERNAL LONG MessageSend(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Sends a menssage from client to server or vice-versa.
INTERNAL void ClientCloseSession(uint32_t dwClientID)
Closes the socket used by the client to communicate with the server.
INTERNAL LONG MessageReceive(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Called by the Client to get the response from the server or vice-versa.
This defines some structures and #defines to be used over the transport layer.