- driver data is now allocated and destroyed by drivers
- new type driver_data_t, driver data is not stored in struct rf_driver. - added some comments and better debug messages git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@357 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
@@ -99,7 +99,7 @@ static size_t get_historical_bytes(unsigned char *atr, size_t atrlen,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int lnfc_connect(void **driver_data)
|
static int lnfc_connect(driver_data_t **driver_data)
|
||||||
{
|
{
|
||||||
struct lnfc_data *data;
|
struct lnfc_data *data;
|
||||||
/* data derived from German (test) identity card issued 2010 */
|
/* data derived from German (test) identity card issued 2010 */
|
||||||
@@ -131,10 +131,11 @@ static int lnfc_connect(void **driver_data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (verbose >= 0)
|
if (verbose >= 0)
|
||||||
printf("Connected to %s\n", data->pndTarget->acName);
|
printf("Connected to %s\n", nfc_device_name(data->pndTarget));
|
||||||
|
|
||||||
|
DEBUG("Waiting for a command that is not part of the anti-collision...\n");
|
||||||
if (!nfc_target_init (data->pndTarget, &ntEmulatedTarget, data->abtCapdu, &data->szCapduLen)) {
|
if (!nfc_target_init (data->pndTarget, &ntEmulatedTarget, data->abtCapdu, &data->szCapduLen)) {
|
||||||
ERROR("Initialization of NFC emulator failed");
|
ERROR("nfc_target_init: %s\n", nfc_strerror(data->pndTarget));
|
||||||
nfc_disconnect (data->pndTarget);
|
nfc_disconnect (data->pndTarget);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -144,18 +145,21 @@ static int lnfc_connect(void **driver_data)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lnfc_disconnect(void *driver_data)
|
static int lnfc_disconnect(driver_data_t *driver_data)
|
||||||
{
|
{
|
||||||
struct lnfc_data *data = driver_data;
|
struct lnfc_data *data = driver_data;
|
||||||
|
|
||||||
|
|
||||||
|
if (data) {
|
||||||
nfc_disconnect (data->pndTarget);
|
nfc_disconnect (data->pndTarget);
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lnfc_receive_capdu(void *driver_data,
|
static int lnfc_receive_capdu(driver_data_t *driver_data,
|
||||||
unsigned char **capdu, size_t *len)
|
unsigned char **capdu, size_t *len)
|
||||||
{
|
{
|
||||||
struct lnfc_data *data = driver_data;
|
struct lnfc_data *data = driver_data;
|
||||||
@@ -185,7 +189,7 @@ static int lnfc_receive_capdu(void *driver_data,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lnfc_send_rapdu(void *driver_data,
|
static int lnfc_send_rapdu(driver_data_t *driver_data,
|
||||||
const unsigned char *rapdu, size_t len)
|
const unsigned char *rapdu, size_t len)
|
||||||
{
|
{
|
||||||
struct lnfc_data *data = driver_data;
|
struct lnfc_data *data = driver_data;
|
||||||
@@ -205,7 +209,6 @@ static int lnfc_send_rapdu(void *driver_data,
|
|||||||
|
|
||||||
|
|
||||||
struct rf_driver driver_libnfc = {
|
struct rf_driver driver_libnfc = {
|
||||||
.data = NULL,
|
|
||||||
.connect = lnfc_connect,
|
.connect = lnfc_connect,
|
||||||
.disconnect = lnfc_disconnect,
|
.disconnect = lnfc_disconnect,
|
||||||
.receive_capdu = lnfc_receive_capdu,
|
.receive_capdu = lnfc_receive_capdu,
|
||||||
|
|||||||
@@ -52,11 +52,14 @@ int picc_encode_rapdu(const unsigned char *inbuf, size_t inlen,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
*outbuf = p;
|
*outbuf = p;
|
||||||
|
*outlen = length;
|
||||||
|
|
||||||
sprintf(*outbuf, "%04X:", inlen);
|
/* write length of R-APDU */
|
||||||
|
sprintf(p, "%04X:", inlen);
|
||||||
|
|
||||||
|
/* next points to the next byte to encode */
|
||||||
next = inbuf;
|
next = inbuf;
|
||||||
/* let p point behind ':' */
|
/* let p point behind ':' to write hex encoded bytes */
|
||||||
p += 5;
|
p += 5;
|
||||||
while (inbuf+inlen > next) {
|
while (inbuf+inlen > next) {
|
||||||
sprintf(p," %02X",*next);
|
sprintf(p," %02X",*next);
|
||||||
@@ -64,7 +67,6 @@ int picc_encode_rapdu(const unsigned char *inbuf, size_t inlen,
|
|||||||
p += 3;
|
p += 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
*outlen = length;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +117,7 @@ int picc_decode_apdu(const char *inbuf, size_t inlen,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int picc_connect(void **driver_data)
|
static int picc_connect(driver_data_t **driver_data)
|
||||||
{
|
{
|
||||||
struct picc_data *data;
|
struct picc_data *data;
|
||||||
|
|
||||||
@@ -128,15 +130,15 @@ static int picc_connect(void **driver_data)
|
|||||||
return 0;
|
return 0;
|
||||||
*driver_data = data;
|
*driver_data = data;
|
||||||
|
|
||||||
|
data->buf = NULL;
|
||||||
|
data->bufmax = 0;
|
||||||
|
|
||||||
data->fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
|
data->fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
|
||||||
if (!data->fd) {
|
if (!data->fd) {
|
||||||
ERROR("Error opening %s: %s\n", PICCDEV, strerror(errno));
|
ERROR("Error opening %s: %s\n", PICCDEV, strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
data->buf = NULL;
|
|
||||||
data->bufmax = 0;
|
|
||||||
|
|
||||||
|
|
||||||
if (verbose >= 0)
|
if (verbose >= 0)
|
||||||
printf("Connected to %s\n", PICCDEV);
|
printf("Connected to %s\n", PICCDEV);
|
||||||
@@ -144,7 +146,7 @@ static int picc_connect(void **driver_data)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int picc_disconnect(void *driver_data)
|
static int picc_disconnect(driver_data_t *driver_data)
|
||||||
{
|
{
|
||||||
struct picc_data *data = driver_data;
|
struct picc_data *data = driver_data;
|
||||||
|
|
||||||
@@ -152,17 +154,15 @@ static int picc_disconnect(void *driver_data)
|
|||||||
if (data) {
|
if (data) {
|
||||||
if (data->fd)
|
if (data->fd)
|
||||||
fclose(data->fd);
|
fclose(data->fd);
|
||||||
data->fd = NULL;
|
|
||||||
free(data->buf);
|
free(data->buf);
|
||||||
data->buf = NULL;
|
free(data);
|
||||||
data->bufmax = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int picc_receive_capdu(void *driver_data,
|
static int picc_receive_capdu(driver_data_t *driver_data,
|
||||||
unsigned char **capdu, size_t *len)
|
unsigned char **capdu, size_t *len)
|
||||||
{
|
{
|
||||||
ssize_t linelen;
|
ssize_t linelen;
|
||||||
@@ -184,7 +184,8 @@ static int picc_receive_capdu(void *driver_data,
|
|||||||
*len = 0;
|
*len = 0;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
fflush(data->fd);
|
if (fflush(data->fd) != 0)
|
||||||
|
ERROR("Warning, fflush failed: %s\n", strerror(errno));
|
||||||
|
|
||||||
DEBUG("%s\n", data->buf);
|
DEBUG("%s\n", data->buf);
|
||||||
|
|
||||||
@@ -193,7 +194,7 @@ static int picc_receive_capdu(void *driver_data,
|
|||||||
return picc_decode_apdu(data->buf, linelen, capdu, len);
|
return picc_decode_apdu(data->buf, linelen, capdu, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int picc_send_rapdu(void *driver_data,
|
static int picc_send_rapdu(driver_data_t *driver_data,
|
||||||
const unsigned char *rapdu, size_t len)
|
const unsigned char *rapdu, size_t len)
|
||||||
{
|
{
|
||||||
struct picc_data *data = driver_data;
|
struct picc_data *data = driver_data;
|
||||||
@@ -213,14 +214,12 @@ static int picc_send_rapdu(void *driver_data,
|
|||||||
/* write R-APDU */
|
/* write R-APDU */
|
||||||
DEBUG("INF: Writing R-APDU\r\n%s\r\n", data->buf);
|
DEBUG("INF: Writing R-APDU\r\n%s\r\n", data->buf);
|
||||||
|
|
||||||
if (fprintf(data->fd,"%s\r\n", (char *) data->buf) < 0) {
|
if (fprintf(data->fd,"%s\r\n", data->buf) < 0) {
|
||||||
ERROR("fprintf: %s\n", strerror(errno));
|
ERROR("Error writing to %s: %s\n", PICCDEV, strerror(errno));
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (fflush(data->fd) != 0) {
|
|
||||||
ERROR("fflush: %s\n", strerror(errno));
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if (fflush(data->fd) != 0)
|
||||||
|
ERROR("Warning, fflush failed: %s\n", strerror(errno));
|
||||||
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@@ -228,7 +227,6 @@ static int picc_send_rapdu(void *driver_data,
|
|||||||
|
|
||||||
|
|
||||||
struct rf_driver driver_openpicc = {
|
struct rf_driver driver_openpicc = {
|
||||||
.data = NULL,
|
|
||||||
.connect = picc_connect,
|
.connect = picc_connect,
|
||||||
.disconnect = picc_disconnect,
|
.disconnect = picc_disconnect,
|
||||||
.receive_capdu = picc_receive_capdu,
|
.receive_capdu = picc_receive_capdu,
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ static int dodaemon = 1;
|
|||||||
int verbose = 0;
|
int verbose = 0;
|
||||||
static unsigned int readernum = 0;
|
static unsigned int readernum = 0;
|
||||||
static struct rf_driver *driver = &driver_openpicc;
|
static struct rf_driver *driver = &driver_openpicc;
|
||||||
|
static driver_data_t *driver_data = NULL;
|
||||||
|
|
||||||
|
|
||||||
static LPSTR readers = NULL;
|
static LPSTR readers = NULL;
|
||||||
@@ -66,12 +67,12 @@ static SCARDCONTEXT hContext = 0;
|
|||||||
static SCARDHANDLE hCard = 0;
|
static SCARDHANDLE hCard = 0;
|
||||||
|
|
||||||
/* Forward declaration */
|
/* Forward declaration */
|
||||||
static void daemonize();
|
static void daemonize(void);
|
||||||
static void cleanup_exit(int signo);
|
static void cleanup_exit(int signo);
|
||||||
static void cleanup(void);
|
static void cleanup(void);
|
||||||
|
|
||||||
|
|
||||||
void daemonize() {
|
void daemonize(void) {
|
||||||
pid_t pid, sid;
|
pid_t pid, sid;
|
||||||
|
|
||||||
/* Fork and continue as child process */
|
/* Fork and continue as child process */
|
||||||
@@ -117,7 +118,8 @@ void cleanup_exit(int signo){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void cleanup(void) {
|
void cleanup(void) {
|
||||||
driver->disconnect(driver->data);
|
driver->disconnect(driver_data);
|
||||||
|
driver_data = NULL;
|
||||||
pcsc_disconnect(hContext, hCard, readers);
|
pcsc_disconnect(hContext, hCard, readers);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,12 +197,6 @@ int main (int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (dodaemon) {
|
|
||||||
verbose = -1;
|
|
||||||
daemonize();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Register signal handlers */
|
/* Register signal handlers */
|
||||||
new_sig.sa_handler = cleanup_exit;
|
new_sig.sa_handler = cleanup_exit;
|
||||||
sigemptyset(&new_sig.sa_mask);
|
sigemptyset(&new_sig.sa_mask);
|
||||||
@@ -213,7 +209,7 @@ int main (int argc, char **argv)
|
|||||||
|
|
||||||
|
|
||||||
/* Open the device */
|
/* Open the device */
|
||||||
if (!driver->connect(&driver->data))
|
if (!driver->connect(&driver_data))
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
|
|
||||||
@@ -224,9 +220,16 @@ int main (int argc, char **argv)
|
|||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
|
|
||||||
|
if (dodaemon) {
|
||||||
|
INFO("Forking to background...\n");
|
||||||
|
verbose = -1;
|
||||||
|
daemonize();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
/* get C-APDU */
|
/* get C-APDU */
|
||||||
if (!driver->receive_capdu(driver->data, &buf, &buflen))
|
if (!driver->receive_capdu(driver_data, &buf, &buflen))
|
||||||
goto err;
|
goto err;
|
||||||
if (!buflen || !buf)
|
if (!buflen || !buf)
|
||||||
continue;
|
continue;
|
||||||
@@ -246,10 +249,11 @@ int main (int argc, char **argv)
|
|||||||
if (verbose >= 0)
|
if (verbose >= 0)
|
||||||
printb("R-APDU:\n", outputBuffer, outputLength);
|
printb("R-APDU:\n", outputBuffer, outputLength);
|
||||||
|
|
||||||
if (!driver->send_rapdu(driver->data, outputBuffer, outputLength))
|
if (!driver->send_rapdu(driver_data, outputBuffer, outputLength))
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
err:
|
err:
|
||||||
cleanup();
|
cleanup();
|
||||||
|
|
||||||
|
|||||||
@@ -27,13 +27,13 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef void driver_data_t;
|
||||||
struct rf_driver {
|
struct rf_driver {
|
||||||
void *data;
|
int (*connect) (driver_data_t **driver_data);
|
||||||
int (*connect) (void **driver_data);
|
int (*disconnect) (driver_data_t *driver_data);
|
||||||
int (*disconnect) (void *driver_data);
|
int (*receive_capdu) (driver_data_t *driver_data,
|
||||||
int (*receive_capdu) (void *driver_data,
|
|
||||||
unsigned char **capdu, size_t *len);
|
unsigned char **capdu, size_t *len);
|
||||||
int (*send_rapdu) (void *driver_data,
|
int (*send_rapdu) (driver_data_t *driver_data,
|
||||||
const unsigned char *rapdu, size_t len);
|
const unsigned char *rapdu, size_t len);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user