Error conditions in utils & examples: fix leaks, unify style (see details)

* in main():
** errx()/err()/return -> exit()
** return values -> EXIT_SUCCESS & EXIT_FAILURE

* out of main:
** err()/errx()/exit() -> return
** change retval from size_t to int to allow returning errors
** don't use EXIT_SUCCESS / EXIT_FAILURE as retvals

* add nfc_close() & nfc_exit() to exit() on errors
* add missing fclose() on errors
* add missing test if (pnd == NULL)
* unify style if (pnd == / != NULL)
* remove goto's
* few related fixes
* remove if(pnd!=NULL) test on nfc_close() calls
This commit is contained in:
Philippe Teuwen 2013-03-05 19:44:59 +01:00
parent 232930c3d5
commit bece73faaf
21 changed files with 433 additions and 298 deletions

View file

@ -50,7 +50,7 @@ main(int argc, const char *argv[])
if (pnd == NULL) { if (pnd == NULL) {
warnx("ERROR: %s", "Unable to open NFC device."); warnx("ERROR: %s", "Unable to open NFC device.");
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
// Set opened NFC device to initiator mode // Set opened NFC device to initiator mode
if (nfc_initiator_init(pnd) < 0) { if (nfc_initiator_init(pnd) < 0) {
@ -82,5 +82,5 @@ main(int argc, const char *argv[])
nfc_close(pnd); nfc_close(pnd);
// Release the context // Release the context
nfc_exit(context); nfc_exit(context);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} }

View file

@ -42,7 +42,7 @@ main(int argc, const char *argv[])
if (pnd == NULL) { if (pnd == NULL) {
ERR("%s", "Unable to open NFC device."); ERR("%s", "Unable to open NFC device.");
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
// Set opened NFC device to initiator mode // Set opened NFC device to initiator mode
if (nfc_initiator_init(pnd) < 0) { if (nfc_initiator_init(pnd) < 0) {
@ -74,5 +74,5 @@ main(int argc, const char *argv[])
nfc_close(pnd); nfc_close(pnd);
// Release the context // Release the context
nfc_exit(context); nfc_exit(context);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} }

View file

@ -155,30 +155,39 @@ main(int argc, char *argv[])
// Try to open the NFC reader // Try to open the NFC reader
pnd = nfc_open(context, NULL); pnd = nfc_open(context, NULL);
if (!pnd) { if (pnd == NULL) {
printf("Error opening NFC reader\n"); printf("Error opening NFC reader\n");
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Initialise NFC device as "initiator" // Initialise NFC device as "initiator"
if (nfc_initiator_init(pnd) < 0) { if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init"); nfc_perror(pnd, "nfc_initiator_init");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Configure the CRC // Configure the CRC
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Use raw send/receive methods // Use raw send/receive methods
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Disable 14443-4 autoswitching // Disable 14443-4 autoswitching
if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -189,7 +198,7 @@ main(int argc, char *argv[])
printf("Error: No tag available\n"); printf("Error: No tag available\n");
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
memcpy(abtAtqa, abtRx, 2); memcpy(abtAtqa, abtRx, 2);
@ -319,5 +328,5 @@ main(int argc, char *argv[])
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} }

View file

@ -53,7 +53,7 @@ static nfc_device *pnd;
static void stop_dep_communication(int sig) static void stop_dep_communication(int sig)
{ {
(void) sig; (void) sig;
if (pnd) if (pnd != NULL)
nfc_abort_command(pnd); nfc_abort_command(pnd);
else else
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -68,16 +68,17 @@ main(int argc, const char *argv[])
if (argc > 1) { if (argc > 1) {
printf("Usage: %s\n", argv[0]); printf("Usage: %s\n", argv[0]);
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
nfc_context *context; nfc_context *context;
nfc_init(&context); nfc_init(&context);
pnd = nfc_open(context, NULL); pnd = nfc_open(context, NULL);
if (!pnd) { if (pnd == NULL) {
printf("Unable to open NFC device.\n"); printf("Unable to open NFC device.\n");
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("NFC device: %s\n opened", nfc_device_get_name(pnd)); printf("NFC device: %s\n opened", nfc_device_get_name(pnd));
@ -85,12 +86,16 @@ main(int argc, const char *argv[])
if (nfc_initiator_init(pnd) < 0) { if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init"); nfc_perror(pnd, "nfc_initiator_init");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
if (nfc_initiator_select_dep_target(pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) { if (nfc_initiator_select_dep_target(pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) {
nfc_perror(pnd, "nfc_initiator_select_dep_target"); nfc_perror(pnd, "nfc_initiator_select_dep_target");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
print_nfc_target(nt, false); print_nfc_target(nt, false);
@ -98,7 +103,9 @@ main(int argc, const char *argv[])
int res; int res;
if ((res = nfc_initiator_transceive_bytes(pnd, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 0)) < 0) { if ((res = nfc_initiator_transceive_bytes(pnd, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 0)) < 0) {
nfc_perror(pnd, "nfc_initiator_transceive_bytes"); nfc_perror(pnd, "nfc_initiator_transceive_bytes");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
abtRx[res] = 0; abtRx[res] = 0;
@ -106,11 +113,12 @@ main(int argc, const char *argv[])
if (nfc_initiator_deselect_target(pnd) < 0) { if (nfc_initiator_deselect_target(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_deselect_target"); nfc_perror(pnd, "nfc_initiator_deselect_target");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
error:
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} }

View file

@ -52,7 +52,7 @@ static nfc_device *pnd;
static void stop_dep_communication(int sig) static void stop_dep_communication(int sig)
{ {
(void) sig; (void) sig;
if (pnd) if (pnd != NULL)
nfc_abort_command(pnd); nfc_abort_command(pnd);
else else
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -65,6 +65,11 @@ main(int argc, const char *argv[])
int szRx; int szRx;
uint8_t abtTx[] = "Hello Mars!"; uint8_t abtTx[] = "Hello Mars!";
if (argc > 1) {
printf("Usage: %s\n", argv[0]);
exit(EXIT_FAILURE);
}
nfc_context *context; nfc_context *context;
nfc_init(&context); nfc_init(&context);
#define MAX_DEVICE_COUNT 2 #define MAX_DEVICE_COUNT 2
@ -80,12 +85,8 @@ main(int argc, const char *argv[])
pnd = nfc_open(context, connstrings[1]); pnd = nfc_open(context, connstrings[1]);
} else { } else {
printf("No device found.\n"); printf("No device found.\n");
return EXIT_FAILURE; nfc_exit(context);
} exit(EXIT_FAILURE);
if (argc > 1) {
printf("Usage: %s\n", argv[0]);
return EXIT_FAILURE;
} }
nfc_target nt = { nfc_target nt = {
@ -109,9 +110,10 @@ main(int argc, const char *argv[])
}, },
}; };
if (!pnd) { if (pnd == NULL) {
printf("Unable to open NFC device.\n"); printf("Unable to open NFC device.\n");
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
@ -123,13 +125,17 @@ main(int argc, const char *argv[])
printf("Waiting for initiator request...\n"); printf("Waiting for initiator request...\n");
if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) { if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) {
nfc_perror(pnd, "nfc_target_init"); nfc_perror(pnd, "nfc_target_init");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("Initiator request received. Waiting for data...\n"); printf("Initiator request received. Waiting for data...\n");
if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), 0)) < 0) { if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), 0)) < 0) {
nfc_perror(pnd, "nfc_target_receive_bytes"); nfc_perror(pnd, "nfc_target_receive_bytes");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
abtRx[(size_t) szRx] = '\0'; abtRx[(size_t) szRx] = '\0';
printf("Received: %s\n", abtRx); printf("Received: %s\n", abtRx);
@ -137,12 +143,13 @@ main(int argc, const char *argv[])
printf("Sending: %s\n", abtTx); printf("Sending: %s\n", abtTx);
if (nfc_target_send_bytes(pnd, abtTx, sizeof(abtTx), 0) < 0) { if (nfc_target_send_bytes(pnd, abtTx, sizeof(abtTx), 0) < 0) {
nfc_perror(pnd, "nfc_target_send_bytes"); nfc_perror(pnd, "nfc_target_send_bytes");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("Data sent.\n"); printf("Data sent.\n");
error:
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} }

View file

@ -79,7 +79,7 @@ static void
stop_emulation(int sig) stop_emulation(int sig)
{ {
(void)sig; (void)sig;
if (pnd) { if (pnd != NULL) {
nfc_abort_command(pnd); nfc_abort_command(pnd);
} else { } else {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -191,6 +191,7 @@ main(int argc, char *argv[])
if (pnd == NULL) { if (pnd == NULL) {
ERR("Unable to open NFC device"); ERR("Unable to open NFC device");
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -198,18 +199,13 @@ main(int argc, char *argv[])
printf("Emulating NDEF tag now, please touch it with a second NFC device\n"); printf("Emulating NDEF tag now, please touch it with a second NFC device\n");
if (nfc_emulate_target(pnd, &emulator, 0) < 0) { if (nfc_emulate_target(pnd, &emulator, 0) < 0) {
goto error; nfc_perror(pnd, argv[0]);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
error:
if (pnd) {
nfc_perror(pnd, argv[0]);
nfc_close(pnd);
nfc_exit(context);
}
} }

View file

@ -66,9 +66,7 @@ intr_hdlr(int sig)
{ {
(void) sig; (void) sig;
printf("\nQuitting...\n"); printf("\nQuitting...\n");
if (pnd != NULL) { nfc_close(pnd);
nfc_close(pnd);
}
nfc_exit(context); nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -184,15 +182,16 @@ main(int argc, char *argv[])
nfc_init(&context); nfc_init(&context);
// Try to open the NFC reader
pnd = nfc_open(context, NULL);
// Display libnfc version // Display libnfc version
acLibnfcVersion = nfc_version(); acLibnfcVersion = nfc_version();
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
// Try to open the NFC reader
pnd = nfc_open(context, NULL);
if (pnd == NULL) { if (pnd == NULL) {
ERR("Unable to open NFC device"); ERR("Unable to open NFC device");
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -267,6 +266,8 @@ main(int argc, char *argv[])
printf("NFC device (configured as target) is now emulating the tag, please touch it with a second NFC device (initiator)\n"); printf("NFC device (configured as target) is now emulating the tag, please touch it with a second NFC device (initiator)\n");
if (!nfc_target_emulate_tag(pnd, &nt)) { if (!nfc_target_emulate_tag(pnd, &nt)) {
nfc_perror(pnd, "nfc_target_emulate_tag"); nfc_perror(pnd, "nfc_target_emulate_tag");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View file

@ -134,6 +134,7 @@ main(int argc, char *argv[])
if (pnd == NULL) { if (pnd == NULL) {
printf("Unable to open NFC device\n"); printf("Unable to open NFC device\n");
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -162,13 +163,17 @@ main(int argc, char *argv[])
if ((szRecvBits = nfc_target_init(pnd, &nt, abtRecv, sizeof(abtRecv), 0)) < 0) { if ((szRecvBits = nfc_target_init(pnd, &nt, abtRecv, sizeof(abtRecv), 0)) < 0) {
nfc_perror(pnd, "nfc_target_init"); nfc_perror(pnd, "nfc_target_init");
ERR("Could not come out of auto-emulation, no command was received"); ERR("Could not come out of auto-emulation, no command was received");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("[+] Received initiator command: "); printf("[+] Received initiator command: ");
print_hex_bits(abtRecv, (size_t) szRecvBits); print_hex_bits(abtRecv, (size_t) szRecvBits);
printf("[+] Configuring communication\n"); printf("[+] Configuring communication\n");
if ((nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, true) < 0)) { if ((nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, true) < 0)) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n", abtUidBcc[0], abtUidBcc[1], printf("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n", abtUidBcc[0], abtUidBcc[1],
@ -211,7 +216,9 @@ main(int argc, char *argv[])
// Send and print the command to the screen // Send and print the command to the screen
if (nfc_target_send_bits(pnd, pbtTx, szTxBits, NULL) < 0) { if (nfc_target_send_bits(pnd, pbtTx, szTxBits, NULL) < 0) {
nfc_perror(pnd, "nfc_target_send_bits"); nfc_perror(pnd, "nfc_target_send_bits");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
if (!quiet_output) { if (!quiet_output) {
printf("T: "); printf("T: ");
@ -223,9 +230,4 @@ main(int argc, char *argv[])
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
error:
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }

View file

@ -183,30 +183,39 @@ main(int argc, char *argv[])
// Try to open the NFC reader // Try to open the NFC reader
pnd = nfc_open(context, NULL); pnd = nfc_open(context, NULL);
if (!pnd) { if (pnd == NULL) {
printf("Error opening NFC reader\n"); printf("Error opening NFC reader\n");
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Initialise NFC device as "initiator" // Initialise NFC device as "initiator"
if (nfc_initiator_init(pnd) < 0) { if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init"); nfc_perror(pnd, "nfc_initiator_init");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Configure the CRC // Configure the CRC
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Use raw send/receive methods // Use raw send/receive methods
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Disable 14443-4 autoswitching // Disable 14443-4 autoswitching
if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -217,7 +226,7 @@ main(int argc, char *argv[])
printf("Error: No tag available\n"); printf("Error: No tag available\n");
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
return 1; exit(EXIT_FAILURE);
} }
memcpy(abtAtqa, abtRx, 2); memcpy(abtAtqa, abtRx, 2);
@ -353,8 +362,7 @@ main(int argc, char *argv[])
} }
} }
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} }

View file

@ -56,7 +56,7 @@ static nfc_device *pnd = NULL;
static void stop_polling(int sig) static void stop_polling(int sig)
{ {
(void) sig; (void) sig;
if (pnd) if (pnd != NULL)
nfc_abort_command(pnd); nfc_abort_command(pnd);
else else
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -110,11 +110,14 @@ main(int argc, const char *argv[])
if (pnd == NULL) { if (pnd == NULL) {
ERR("%s", "Unable to open NFC device."); ERR("%s", "Unable to open NFC device.");
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (nfc_initiator_init(pnd) < 0) { if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init"); nfc_perror(pnd, "nfc_initiator_init");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View file

@ -89,13 +89,13 @@ main(int argc, char *argv[])
for (arg = 1; arg < argc; arg++) { for (arg = 1; arg < argc; arg++) {
if (0 == strcmp(argv[arg], "-h")) { if (0 == strcmp(argv[arg], "-h")) {
print_usage(argv); print_usage(argv);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} else if (0 == strcmp(argv[arg], "-q")) { } else if (0 == strcmp(argv[arg], "-q")) {
quiet_output = true; quiet_output = true;
} else { } else {
ERR("%s is not supported option.", argv[arg]); ERR("%s is not supported option.", argv[arg]);
print_usage(argv); print_usage(argv);
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
} }
@ -116,14 +116,16 @@ main(int argc, char *argv[])
if (szFound < 2) { if (szFound < 2) {
ERR("%zd device found but two opened devices are needed to relay NFC.", szFound); ERR("%zd device found but two opened devices are needed to relay NFC.", szFound);
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
// Try to open the NFC emulator device // Try to open the NFC emulator device
pndTag = nfc_open(context, connstrings[0]); pndTag = nfc_open(context, connstrings[0]);
if (pndTag == NULL) { if (pndTag == NULL) {
printf("Error opening NFC emulator device\n"); printf("Error opening NFC emulator device\n");
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n"); printf("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n");
@ -153,30 +155,44 @@ main(int argc, char *argv[])
ERR("%s", "Initialization of NFC emulator failed"); ERR("%s", "Initialization of NFC emulator failed");
nfc_close(pndTag); nfc_close(pndTag);
nfc_exit(context); nfc_exit(context);
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
printf("%s", "Configuring emulator settings..."); printf("%s", "Configuring emulator settings...");
if ((nfc_device_set_property_bool(pndTag, NP_HANDLE_CRC, false) < 0) || if ((nfc_device_set_property_bool(pndTag, NP_HANDLE_CRC, false) < 0) ||
(nfc_device_set_property_bool(pndTag, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool(pndTag, NP_ACCEPT_INVALID_FRAMES, true)) < 0) { (nfc_device_set_property_bool(pndTag, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool(pndTag, NP_ACCEPT_INVALID_FRAMES, true)) < 0) {
nfc_perror(pndTag, "nfc_device_set_property_bool"); nfc_perror(pndTag, "nfc_device_set_property_bool");
nfc_close(pndTag);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("%s", "Done, emulated tag is initialized"); printf("%s", "Done, emulated tag is initialized");
// Try to open the NFC reader // Try to open the NFC reader
pndReader = nfc_open(context, connstrings[1]); pndReader = nfc_open(context, connstrings[1]);
if (pndReader == NULL) {
printf("Error opening NFC reader device\n");
nfc_close(pndTag);
nfc_exit(context);
exit(EXIT_FAILURE);
}
printf("NFC reader device: %s opened", nfc_device_get_name(pndReader)); printf("NFC reader device: %s opened", nfc_device_get_name(pndReader));
printf("%s", "Configuring NFC reader settings..."); printf("%s", "Configuring NFC reader settings...");
if (nfc_initiator_init(pndReader) < 0) { if (nfc_initiator_init(pndReader) < 0) {
nfc_perror(pndReader, "nfc_initiator_init"); nfc_perror(pndReader, "nfc_initiator_init");
nfc_close(pndTag);
nfc_close(pndReader);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if ((nfc_device_set_property_bool(pndReader, NP_HANDLE_CRC, false) < 0) || if ((nfc_device_set_property_bool(pndReader, NP_HANDLE_CRC, false) < 0) ||
(nfc_device_set_property_bool(pndReader, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool(pndReader, NP_HANDLE_PARITY, false) < 0) ||
(nfc_device_set_property_bool(pndReader, NP_ACCEPT_INVALID_FRAMES, true)) < 0) { (nfc_device_set_property_bool(pndReader, NP_ACCEPT_INVALID_FRAMES, true)) < 0) {
nfc_perror(pndReader, "nfc_device_set_property_bool"); nfc_perror(pndReader, "nfc_device_set_property_bool");
nfc_close(pndTag);
nfc_close(pndReader);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("%s", "Done, relaying frames now!"); printf("%s", "Done, relaying frames now!");
@ -189,12 +205,18 @@ main(int argc, char *argv[])
// Drop down field for a very short time (original tag will reboot) // Drop down field for a very short time (original tag will reboot)
if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, false) < 0) { if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, false) < 0) {
nfc_perror(pndReader, "nfc_device_set_property_bool"); nfc_perror(pndReader, "nfc_device_set_property_bool");
nfc_close(pndTag);
nfc_close(pndReader);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!quiet_output) if (!quiet_output)
printf("\n"); printf("\n");
if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, true) < 0) { if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, true) < 0) {
nfc_perror(pndReader, "nfc_device_set_property_bool"); nfc_perror(pndReader, "nfc_device_set_property_bool");
nfc_close(pndTag);
nfc_close(pndReader);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -209,6 +231,9 @@ main(int argc, char *argv[])
// Redirect the answer back to the reader // Redirect the answer back to the reader
if (nfc_target_send_bits(pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) { if (nfc_target_send_bits(pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) {
nfc_perror(pndTag, "nfc_target_send_bits"); nfc_perror(pndTag, "nfc_target_send_bits");
nfc_close(pndTag);
nfc_close(pndReader);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Print the tag frame to the screen // Print the tag frame to the screen

View file

@ -51,7 +51,7 @@ int
main(int argc, const char *argv[]) main(int argc, const char *argv[])
{ {
size_t i; size_t i;
nfc_device *pnd; nfc_device *pnd = NULL;
const char *acLibnfcVersion; const char *acLibnfcVersion;
bool result; bool result;
int res = 0; int res = 0;
@ -63,7 +63,8 @@ main(int argc, const char *argv[])
const uint8_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 }; const uint8_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 };
if (argc > 1) { if (argc > 1) {
errx(1, "usage: %s", argv[0]); printf("Usage: %s", argv[0]);
exit(EXIT_FAILURE);
} }
nfc_context *context; nfc_context *context;
@ -85,7 +86,8 @@ main(int argc, const char *argv[])
if (pnd == NULL) { if (pnd == NULL) {
ERR("%s", "Unable to open NFC device."); ERR("%s", "Unable to open NFC device.");
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("NFC device [%s] opened.\n", nfc_device_get_name(pnd)); printf("NFC device [%s] opened.\n", nfc_device_get_name(pnd));
@ -119,4 +121,7 @@ main(int argc, const char *argv[])
nfc_perror(pnd, "pn53x_transceive: cannot diagnose RAM"); nfc_perror(pnd, "pn53x_transceive: cannot diagnose RAM");
} }
} }
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_SUCCESS);
} }

View file

@ -75,8 +75,6 @@ main(int argc, const char *argv[])
(void) argc; (void) argc;
(void) argv; (void) argv;
int ret = EXIT_FAILURE;
nfc_context *context; nfc_context *context;
nfc_init(&context); nfc_init(&context);
@ -90,7 +88,8 @@ main(int argc, const char *argv[])
if (pnd == NULL) { if (pnd == NULL) {
ERR("%s", "Unable to open NFC device."); ERR("%s", "Unable to open NFC device.");
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
@ -107,7 +106,9 @@ main(int argc, const char *argv[])
printf("\n"); printf("\n");
if ((input < '1') || (input > '3')) { if ((input < '1') || (input > '3')) {
ERR("%s", "Invalid selection."); ERR("%s", "Invalid selection.");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
/* /*
@ -125,7 +126,9 @@ main(int argc, const char *argv[])
// FIXME Its a private pn53x function // FIXME Its a private pn53x function
if (pn532_SAMConfiguration(pnd, mode, 0) < 0) { if (pn532_SAMConfiguration(pnd, mode, 0) < 0) {
nfc_perror(pnd, "pn53x_SAMConfiguration"); nfc_perror(pnd, "pn53x_SAMConfiguration");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("Now the SAM is readable for 1 minute from an external reader.\n"); printf("Now the SAM is readable for 1 minute from an external reader.\n");
wait_one_minute(); wait_one_minute();
@ -136,13 +139,17 @@ main(int argc, const char *argv[])
// Set opened NFC device to initiator mode // Set opened NFC device to initiator mode
if (nfc_initiator_init_secure_element(pnd) < 0) { if (nfc_initiator_init_secure_element(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init_secure_element"); nfc_perror(pnd, "nfc_initiator_init_secure_element");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
// Let the reader only try once to find a tag // Let the reader only try once to find a tag
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
// Read the SAM's info // Read the SAM's info
const nfc_modulation nmSAM = { const nfc_modulation nmSAM = {
@ -154,16 +161,22 @@ main(int argc, const char *argv[])
int res; int res;
if ((res = nfc_initiator_select_passive_target(pnd, nmSAM, NULL, 0, &nt)) < 0) { if ((res = nfc_initiator_select_passive_target(pnd, nmSAM, NULL, 0, &nt)) < 0) {
nfc_perror(pnd, "nfc_initiator_select_passive_target"); nfc_perror(pnd, "nfc_initiator_select_passive_target");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} else if (res == 0) { } else if (res == 0) {
ERR("No SAM found."); ERR("No SAM found.");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} else if (res == 1) { } else if (res == 1) {
printf("The following ISO14443A tag (SAM) was found:\n"); printf("The following ISO14443A tag (SAM) was found:\n");
print_nfc_target(nt, true); print_nfc_target(nt, true);
} else { } else {
ERR("%s", "More than one ISO14442 tag found as SAM."); ERR("%s", "More than one ISO14442 tag found as SAM.");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
} }
break; break;
@ -172,7 +185,9 @@ main(int argc, const char *argv[])
// FIXME Its a private pn53x function // FIXME Its a private pn53x function
if (pn532_SAMConfiguration(pnd, mode, 0) < 0) { if (pn532_SAMConfiguration(pnd, mode, 0) < 0) {
nfc_perror(pnd, "pn53x_SAMConfiguration"); nfc_perror(pnd, "pn53x_SAMConfiguration");
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
uint8_t abtRx[MAX_FRAME_LEN]; uint8_t abtRx[MAX_FRAME_LEN];
@ -195,7 +210,9 @@ main(int argc, const char *argv[])
printf("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n"); printf("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n");
if (nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0) < 0) { if (nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0) < 0) {
nfc_perror(pnd, "nfc_target_init"); nfc_perror(pnd, "nfc_target_init");
return EXIT_FAILURE; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
// wait_one_minute (); // wait_one_minute ();
} }
@ -204,15 +221,12 @@ main(int argc, const char *argv[])
// This should not happend... nothing to do. // This should not happend... nothing to do.
break; break;
} }
ret = EXIT_SUCCESS;
error:
// Disconnect from the SAM // Disconnect from the SAM
pn532_SAMConfiguration(pnd, PSM_NORMAL, -1); pn532_SAMConfiguration(pnd, PSM_NORMAL, -1);
// Close NFC device // Close NFC device
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
exit(EXIT_SUCCESS);
exit(ret);
} }

View file

@ -79,7 +79,7 @@ int main(int argc, const char *argv[])
if (argc >= 2) { if (argc >= 2) {
if ((input = fopen(argv[1], "r")) == NULL) { if ((input = fopen(argv[1], "r")) == NULL) {
ERR("%s", "Cannot open file."); ERR("%s", "Cannot open file.");
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
} }
@ -94,7 +94,8 @@ int main(int argc, const char *argv[])
if (input != NULL) { if (input != NULL) {
fclose(input); fclose(input);
} }
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
@ -103,6 +104,8 @@ int main(int argc, const char *argv[])
if (input != NULL) { if (input != NULL) {
fclose(input); fclose(input);
} }
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -205,5 +208,5 @@ int main(int argc, const char *argv[])
} }
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} }

View file

@ -237,22 +237,25 @@ nfcforum_tag4_io(struct nfc_emulator *emulator, const uint8_t *data_in, const si
static void stop_emulation(int sig) static void stop_emulation(int sig)
{ {
(void) sig; (void) sig;
if (pnd) if (pnd != NULL)
nfc_abort_command(pnd); nfc_abort_command(pnd);
else else
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
static size_t static int
ndef_message_load(char *filename, struct nfcforum_tag4_ndef_data *tag_data) ndef_message_load(char *filename, struct nfcforum_tag4_ndef_data *tag_data)
{ {
struct stat sb; struct stat sb;
if (stat(filename, &sb) < 0) if (stat(filename, &sb) < 0) {
return 0; printf("file not found or not accessible '%s'", filename);
return -1;
}
/* Check file size */ /* Check file size */
if (sb.st_size > 0xFFFF) { if (sb.st_size > 0xFFFF) {
errx(EXIT_FAILURE, "file size too large '%s'", filename); printf("file size too large '%s'", filename);
return -1;
} }
tag_data->ndef_file_len = sb.st_size + 2; tag_data->ndef_file_len = sb.st_size + 2;
@ -261,29 +264,37 @@ ndef_message_load(char *filename, struct nfcforum_tag4_ndef_data *tag_data)
tag_data->ndef_file[1] = (uint8_t)(sb.st_size); tag_data->ndef_file[1] = (uint8_t)(sb.st_size);
FILE *F; FILE *F;
if (!(F = fopen(filename, "r"))) if (!(F = fopen(filename, "r"))) {
err(EXIT_FAILURE, "fopen (%s, \"r\")", filename); printf("fopen (%s, \"r\")", filename);
return -1;
}
if (1 != fread(tag_data->ndef_file + 2, sb.st_size, 1, F)) if (1 != fread(tag_data->ndef_file + 2, sb.st_size, 1, F)) {
err(EXIT_FAILURE, "Can't read from %s", filename); printf("Can't read from %s", filename);
fclose(F);
return -1;
}
fclose(F); fclose(F);
return sb.st_size; return sb.st_size;
} }
static size_t static int
ndef_message_save(char *filename, struct nfcforum_tag4_ndef_data *tag_data) ndef_message_save(char *filename, struct nfcforum_tag4_ndef_data *tag_data)
{ {
FILE *F; FILE *F;
if (!(F = fopen(filename, "w"))) if (!(F = fopen(filename, "w"))) {
err(EXIT_FAILURE, "fopen (%s, w)", filename); printf("fopen (%s, w)", filename);
return -1;
}
if (1 != fwrite(tag_data->ndef_file + 2, tag_data->ndef_file_len - 2, 1, F)) { if (1 != fwrite(tag_data->ndef_file + 2, tag_data->ndef_file_len - 2, 1, F)) {
err(EXIT_FAILURE, "fwrite (%d)", (int) tag_data->ndef_file_len - 2); printf("fwrite (%d)", (int) tag_data->ndef_file_len - 2);
fclose(F);
return -1;
} }
fclose(F); fclose(F);
return tag_data->ndef_file_len - 2; return tag_data->ndef_file_len - 2;
} }
@ -361,8 +372,9 @@ main(int argc, char *argv[])
// If some file is provided load it // If some file is provided load it
if (argc >= (2 + options)) { if (argc >= (2 + options)) {
if (!ndef_message_load(argv[1 + options], &nfcforum_tag4_data)) { if (ndef_message_load(argv[1 + options], &nfcforum_tag4_data) < 0) {
err(EXIT_FAILURE, "Can't load NDEF file '%s'", argv[1 + options]); printf("Can't load NDEF file '%s'", argv[1 + options]);
exit(EXIT_FAILURE);
} }
} }
@ -374,6 +386,7 @@ main(int argc, char *argv[])
if (pnd == NULL) { if (pnd == NULL) {
ERR("Unable to open NFC device"); ERR("Unable to open NFC device");
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -384,16 +397,21 @@ main(int argc, char *argv[])
if (0 != nfc_emulate_target(pnd, &emulator, 0)) { // contains already nfc_target_init() call if (0 != nfc_emulate_target(pnd, &emulator, 0)) { // contains already nfc_target_init() call
nfc_perror(pnd, "nfc_emulate_target"); nfc_perror(pnd, "nfc_emulate_target");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
nfc_close(pnd);
if (argc == (3 + options)) { if (argc == (3 + options)) {
if (!(ndef_message_save(argv[2 + options], &nfcforum_tag4_data))) { if (ndef_message_save(argv[2 + options], &nfcforum_tag4_data) < 0) {
err(EXIT_FAILURE, "Can't save NDEF file '%s'", argv[2 + options]); printf("Can't save NDEF file '%s'", argv[2 + options]);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
} }
nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

View file

@ -122,6 +122,7 @@ main(int argc, const char *argv[])
} }
if (nfc_initiator_init(pnd) < 0) { if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init"); nfc_perror(pnd, "nfc_initiator_init");
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -242,5 +243,5 @@ main(int argc, const char *argv[])
} }
nfc_exit(context); nfc_exit(context);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} }

View file

@ -224,12 +224,12 @@ unlock_card(void)
// Configure the CRC // Configure the CRC
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
nfc_perror(pnd, "nfc_configure"); nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE); return false;
} }
// Use raw send/receive methods // Use raw send/receive methods
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
nfc_perror(pnd, "nfc_configure"); nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE); return false;
} }
iso14443a_crc_append(abtHalt, 2); iso14443a_crc_append(abtHalt, 2);
@ -248,12 +248,12 @@ unlock_card(void)
// Configure the CRC // Configure the CRC
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) { if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
exit(EXIT_FAILURE); return false;
} }
// Switch off raw send/receive methods // Switch off raw send/receive methods
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) { if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
exit(EXIT_FAILURE); return false;
} }
return true; return true;
} }
@ -465,134 +465,138 @@ main(int argc, const char *argv[])
bUseKeyFile = (argc > 4); bUseKeyFile = (argc > 4);
} }
switch (atAction) { if (atAction == ACTION_USAGE) {
case ACTION_USAGE: print_usage(argv[0]);
print_usage(argv[0]); exit(EXIT_FAILURE);
}
if (bUseKeyFile) {
pfKeys = fopen(argv[4], "rb");
if (pfKeys == NULL) {
printf("Could not open keys file: %s\n", argv[4]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; }
case ACTION_READ: if (fread(&mtKeys, 1, sizeof(mtKeys), pfKeys) != sizeof(mtKeys)) {
case ACTION_WRITE: printf("Could not read keys file: %s\n", argv[4]);
if (bUseKeyFile) { fclose(pfKeys);
pfKeys = fopen(argv[4], "rb"); exit(EXIT_FAILURE);
if (pfKeys == NULL) { }
printf("Could not open keys file: %s\n", argv[4]); fclose(pfKeys);
exit(EXIT_FAILURE); }
}
if (fread(&mtKeys, 1, sizeof(mtKeys), pfKeys) != sizeof(mtKeys)) {
printf("Could not read keys file: %s\n", argv[4]);
fclose(pfKeys);
exit(EXIT_FAILURE);
}
fclose(pfKeys);
}
if (atAction == ACTION_READ) { if (atAction == ACTION_READ) {
memset(&mtDump, 0x00, sizeof(mtDump)); memset(&mtDump, 0x00, sizeof(mtDump));
} else { } else {
pfDump = fopen(argv[3], "rb"); pfDump = fopen(argv[3], "rb");
if (pfDump == NULL) { if (pfDump == NULL) {
printf("Could not open dump file: %s\n", argv[3]); printf("Could not open dump file: %s\n", argv[3]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
printf("Could not read dump file: %s\n", argv[3]); printf("Could not read dump file: %s\n", argv[3]);
fclose(pfDump); fclose(pfDump);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
fclose(pfDump); fclose(pfDump);
} }
// printf("Successfully opened required files\n"); // printf("Successfully opened required files\n");
nfc_init(&context); nfc_init(&context);
// Try to open the NFC reader // Try to open the NFC reader
pnd = nfc_open(context, NULL); pnd = nfc_open(context, NULL);
if (pnd == NULL) { if (pnd == NULL) {
printf("Error opening NFC reader\n"); printf("Error opening NFC reader\n");
exit(EXIT_FAILURE); nfc_exit(context);
} exit(EXIT_FAILURE);
}
if (nfc_initiator_init(pnd) < 0) { if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init"); nfc_perror(pnd, "nfc_initiator_init");
exit(EXIT_FAILURE); nfc_close(pnd);
}; nfc_exit(context);
exit(EXIT_FAILURE);
};
// Let the reader only try once to find a tag // Let the reader only try once to find a tag
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
exit(EXIT_FAILURE); nfc_close(pnd);
} nfc_exit(context);
// Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance. exit(EXIT_FAILURE);
nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false); }
// Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance.
nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false);
printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
// Try to find a MIFARE Classic tag // Try to find a MIFARE Classic tag
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) {
printf("Error: no tag was found\n"); printf("Error: no tag was found\n");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
// Test if we are dealing with a MIFARE compatible tag
if ((nt.nti.nai.btSak & 0x08) == 0) {
printf("Warning: tag is probably not a MFC!\n");
}
// Get the info from the current tag
pbtUID = nt.nti.nai.abtUid;
if (bUseKeyFile) {
uint8_t fileUid[4];
memcpy(fileUid, mtKeys.amb[0].mbm.abtUID, 4);
// Compare if key dump UID is the same as the current tag UID, at least for the first 4 bytes
if (memcmp(pbtUID, fileUid, 4) != 0) {
printf("Expected MIFARE Classic card with UID starting as: %02x%02x%02x%02x\n",
fileUid[0], fileUid[1], fileUid[2], fileUid[3]);
}
}
printf("Found MIFARE Classic card:\n");
print_nfc_target(nt, false);
// Guessing size
if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02)
// 4K
uiBlocks = 0xff;
else if ((nt.nti.nai.btSak & 0x01) == 0x01)
// 320b
uiBlocks = 0x13;
else
// 1K
// TODO: for MFP it is 0x7f (2K) but how to be sure it's a MFP? Try to get RATS?
uiBlocks = 0x3f;
printf("Guessing size: seems to be a %i-byte card\n", (uiBlocks + 1) * 16);
if (atAction == ACTION_READ) {
if (read_card(unlock)) {
printf("Writing data to file: %s ...", argv[3]);
fflush(stdout);
pfDump = fopen(argv[3], "wb");
if (pfDump == NULL) {
printf("Could not open dump file: %s\n", argv[3]);
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Test if we are dealing with a MIFARE compatible tag if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
if ((nt.nti.nai.btSak & 0x08) == 0) { printf("\nCould not write to file: %s\n", argv[3]);
printf("Warning: tag is probably not a MFC!\n"); fclose(pfDump);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("Done.\n");
fclose(pfDump);
}
} else if (atAction == ACTION_WRITE) {
write_card(unlock);
}
// Get the info from the current tag nfc_close(pnd);
pbtUID = nt.nti.nai.abtUid;
if (bUseKeyFile) {
uint8_t fileUid[4];
memcpy(fileUid, mtKeys.amb[0].mbm.abtUID, 4);
// Compare if key dump UID is the same as the current tag UID, at least for the first 4 bytes
if (memcmp(pbtUID, fileUid, 4) != 0) {
printf("Expected MIFARE Classic card with UID starting as: %02x%02x%02x%02x\n",
fileUid[0], fileUid[1], fileUid[2], fileUid[3]);
}
}
printf("Found MIFARE Classic card:\n");
print_nfc_target(nt, false);
// Guessing size
if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02)
// 4K
uiBlocks = 0xff;
else if ((nt.nti.nai.btSak & 0x01) == 0x01)
// 320b
uiBlocks = 0x13;
else
// 1K
// TODO: for MFP it is 0x7f (2K) but how to be sure it's a MFP? Try to get RATS?
uiBlocks = 0x3f;
printf("Guessing size: seems to be a %i-byte card\n", (uiBlocks + 1) * 16);
if (atAction == ACTION_READ) {
if (read_card(unlock)) {
printf("Writing data to file: %s ...", argv[3]);
fflush(stdout);
pfDump = fopen(argv[3], "wb");
if (pfDump == NULL) {
printf("Could not open dump file: %s\n", argv[3]);
exit(EXIT_FAILURE);
}
if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
printf("\nCould not write to file: %s\n", argv[3]);
exit(EXIT_FAILURE);
}
printf("Done.\n");
fclose(pfDump);
}
} else if (atAction == ACTION_WRITE) {
write_card(unlock);
}
nfc_close(pnd);
break;
};
nfc_exit(context); nfc_exit(context);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

View file

@ -178,7 +178,7 @@ main(int argc, const char *argv[])
printf("r|w - Perform read from or write to card\n"); printf("r|w - Perform read from or write to card\n");
printf("<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n"); printf("<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
printf("\n"); printf("\n");
return 1; exit(EXIT_FAILURE);
} }
DBG("\nChecking arguments and settings\n"); DBG("\nChecking arguments and settings\n");
@ -192,13 +192,13 @@ main(int argc, const char *argv[])
if (pfDump == NULL) { if (pfDump == NULL) {
ERR("Could not open dump file: %s\n", argv[2]); ERR("Could not open dump file: %s\n", argv[2]);
return 1; exit(EXIT_FAILURE);
} }
if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
ERR("Could not read from dump file: %s\n", argv[2]); ERR("Could not read from dump file: %s\n", argv[2]);
fclose(pfDump); fclose(pfDump);
return 1; exit(EXIT_FAILURE);
} }
fclose(pfDump); fclose(pfDump);
} }
@ -211,17 +211,22 @@ main(int argc, const char *argv[])
pnd = nfc_open(context, NULL); pnd = nfc_open(context, NULL);
if (pnd == NULL) { if (pnd == NULL) {
ERR("Error opening NFC device\n"); ERR("Error opening NFC device\n");
return 1; nfc_exit(context);
exit(EXIT_FAILURE);
} }
if (nfc_initiator_init(pnd) < 0) { if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init"); nfc_perror(pnd, "nfc_initiator_init");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Let the device only try once to find a tag // Let the device only try once to find a tag
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -232,7 +237,7 @@ main(int argc, const char *argv[])
ERR("no tag was found\n"); ERR("no tag was found\n");
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
// Test if we are dealing with a MIFARE compatible tag // Test if we are dealing with a MIFARE compatible tag
@ -240,7 +245,7 @@ main(int argc, const char *argv[])
ERR("tag is not a MIFARE Ultralight card\n"); ERR("tag is not a MIFARE Ultralight card\n");
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
// Get the info from the current tag // Get the info from the current tag
printf("Found MIFARE Ultralight card with UID: "); printf("Found MIFARE Ultralight card with UID: ");
@ -257,12 +262,16 @@ main(int argc, const char *argv[])
pfDump = fopen(argv[2], "wb"); pfDump = fopen(argv[2], "wb");
if (pfDump == NULL) { if (pfDump == NULL) {
printf("Could not open file: %s\n", argv[2]); printf("Could not open file: %s\n", argv[2]);
return EXIT_FAILURE; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
printf("Could not write to file: %s\n", argv[2]); printf("Could not write to file: %s\n", argv[2]);
fclose(pfDump); fclose(pfDump);
return EXIT_FAILURE; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
fclose(pfDump); fclose(pfDump);
printf("Done.\n"); printf("Done.\n");
@ -273,5 +282,5 @@ main(int argc, const char *argv[])
nfc_close(pnd); nfc_close(pnd);
nfc_exit(context); nfc_exit(context);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} }

View file

@ -72,7 +72,7 @@ print_usage(char *progname)
static void stop_select(int sig) static void stop_select(int sig)
{ {
(void) sig; (void) sig;
if (pnd) if (pnd != NULL)
nfc_abort_command(pnd); nfc_abort_command(pnd);
else else
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -203,6 +203,8 @@ main(int argc, char *argv[])
if (pnd == NULL) { if (pnd == NULL) {
ERR("Unable to open NFC device"); ERR("Unable to open NFC device");
fclose(ndef_stream);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -219,17 +221,21 @@ main(int argc, char *argv[])
if (nfc_initiator_init(pnd) < 0) { if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init"); nfc_perror(pnd, "nfc_initiator_init");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
fprintf(message_stream, "Place your NFC Forum Tag Type 3 in the field...\n"); fprintf(message_stream, "Place your NFC Forum Tag Type 3 in the field...\n");
int error = EXIT_SUCCESS;
// Polling payload (SENSF_REQ) must be present (see NFC Digital Protol) // Polling payload (SENSF_REQ) must be present (see NFC Digital Protol)
const uint8_t *pbtSensfReq = (uint8_t *)"\x00\xff\xff\x01\x00"; const uint8_t *pbtSensfReq = (uint8_t *)"\x00\xff\xff\x01\x00";
if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReq, 5, &nt) <= 0) { if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReq, 5, &nt) <= 0) {
nfc_perror(pnd, "nfc_initiator_select_passive_target"); nfc_perror(pnd, "nfc_initiator_select_passive_target");
error = EXIT_FAILURE; fclose(ndef_stream);
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
// Check if System Code equals 0x12fc // Check if System Code equals 0x12fc
@ -239,14 +245,18 @@ main(int argc, char *argv[])
const uint8_t *pbtSensfReqNfcForum = (uint8_t *)"\x00\x12\xfc\x01\x00"; const uint8_t *pbtSensfReqNfcForum = (uint8_t *)"\x00\x12\xfc\x01\x00";
if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReqNfcForum, 5, &nt) <= 0) { if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReqNfcForum, 5, &nt) <= 0) {
nfc_perror(pnd, "nfc_initiator_select_passive_target"); nfc_perror(pnd, "nfc_initiator_select_passive_target");
error = EXIT_FAILURE; fclose(ndef_stream);
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
// Check again if System Code equals 0x12fc // Check again if System Code equals 0x12fc
if (0 != memcmp(nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) { if (0 != memcmp(nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) {
fprintf(stderr, "Tag is not NFC Forum Tag Type 3 compliant.\n"); fprintf(stderr, "Tag is not NFC Forum Tag Type 3 compliant.\n");
error = EXIT_FAILURE; fclose(ndef_stream);
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
} }
@ -254,8 +264,10 @@ main(int argc, char *argv[])
if ((nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) || (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0)) { if ((nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) || (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0)) {
nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_perror(pnd, "nfc_device_set_property_bool");
error = EXIT_FAILURE; fclose(ndef_stream);
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
uint8_t data[1024]; uint8_t data[1024];
@ -264,8 +276,10 @@ main(int argc, char *argv[])
if (0 >= (len = nfc_forum_tag_type3_check(pnd, nt, 0, 1, data, &data_len))) { if (0 >= (len = nfc_forum_tag_type3_check(pnd, nt, 0, 1, data, &data_len))) {
nfc_perror(pnd, "nfc_forum_tag_type3_check"); nfc_perror(pnd, "nfc_forum_tag_type3_check");
error = EXIT_FAILURE; fclose(ndef_stream);
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
const int ndef_major_version = (data[0] & 0xf0) >> 4; const int ndef_major_version = (data[0] & 0xf0) >> 4;
@ -285,14 +299,18 @@ main(int argc, char *argv[])
const uint16_t ndef_checksum = (data[14] << 8) + data[15]; const uint16_t ndef_checksum = (data[14] << 8) + data[15];
if (ndef_calculated_checksum != ndef_checksum) { if (ndef_calculated_checksum != ndef_checksum) {
fprintf(stderr, "NDEF CRC does not match with calculated one\n"); fprintf(stderr, "NDEF CRC does not match with calculated one\n");
error = EXIT_FAILURE; fclose(ndef_stream);
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
if (!ndef_data_len) { if (!ndef_data_len) {
fprintf(stderr, "Empty NFC Forum Tag Type 3\n"); fprintf(stderr, "Empty NFC Forum Tag Type 3\n");
error = EXIT_FAILURE; fclose(ndef_stream);
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
const uint8_t block_max_per_check = data[1]; const uint8_t block_max_per_check = data[1];
@ -303,22 +321,23 @@ main(int argc, char *argv[])
size_t size = sizeof(data) - data_len; size_t size = sizeof(data) - data_len;
if (!nfc_forum_tag_type3_check(pnd, nt, 1 + b, MIN(block_max_per_check, (block_count_to_check - (b * block_max_per_check))), data + data_len, &size)) { if (!nfc_forum_tag_type3_check(pnd, nt, 1 + b, MIN(block_max_per_check, (block_count_to_check - (b * block_max_per_check))), data + data_len, &size)) {
nfc_perror(pnd, "nfc_forum_tag_type3_check"); nfc_perror(pnd, "nfc_forum_tag_type3_check");
error = EXIT_FAILURE; fclose(ndef_stream);
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
data_len += size; data_len += size;
} }
if (fwrite(data, 1, data_len, ndef_stream) != data_len) { if (fwrite(data, 1, data_len, ndef_stream) != data_len) {
fprintf(stderr, "Could not write to file.\n"); fprintf(stderr, "Could not write to file.\n");
error = EXIT_FAILURE; fclose(ndef_stream);
goto error; nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} }
error:
fclose(ndef_stream); fclose(ndef_stream);
if (pnd) { nfc_close(pnd);
nfc_close(pnd);
}
nfc_exit(context); nfc_exit(context);
exit(error); exit(EXIT_SUCCESS);
} }

View file

@ -94,29 +94,29 @@ print_usage(char *argv[])
printf("\t-n N\tAdds a waiting time of N seconds (integer) in the relay to mimic long distance.\n"); printf("\t-n N\tAdds a waiting time of N seconds (integer) in the relay to mimic long distance.\n");
} }
static bool print_hex_fd4(const uint8_t *pbtData, const size_t szBytes, const char *pchPrefix) static int print_hex_fd4(const uint8_t *pbtData, const size_t szBytes, const char *pchPrefix)
{ {
size_t szPos; size_t szPos;
if (szBytes > MAX_FRAME_LEN) { if (szBytes > MAX_FRAME_LEN) {
return EXIT_FAILURE; return -1;
} }
if (fprintf(fd4, "#%s %04zx: ", pchPrefix, szBytes) < 0) { if (fprintf(fd4, "#%s %04zx: ", pchPrefix, szBytes) < 0) {
return EXIT_FAILURE; return -1;
} }
for (szPos = 0; szPos < szBytes; szPos++) { for (szPos = 0; szPos < szBytes; szPos++) {
if (fprintf(fd4, "%02x ", pbtData[szPos]) < 0) { if (fprintf(fd4, "%02x ", pbtData[szPos]) < 0) {
return EXIT_FAILURE; return -1;
} }
} }
if (fprintf(fd4, "\n") < 0) { if (fprintf(fd4, "\n") < 0) {
return EXIT_FAILURE; return -1;
} }
fflush(fd4); fflush(fd4);
return EXIT_SUCCESS; return 0;
} }
static bool scan_hex_fd3(uint8_t *pbtData, size_t *pszBytes, const char *pchPrefix) static int scan_hex_fd3(uint8_t *pbtData, size_t *pszBytes, const char *pchPrefix)
{ {
size_t szPos; size_t szPos;
unsigned int uiBytes; unsigned int uiBytes;
@ -126,25 +126,25 @@ static bool scan_hex_fd3(uint8_t *pbtData, size_t *pszBytes, const char *pchPref
// Look for our next sync marker // Look for our next sync marker
while ((c = fgetc(fd3)) != '#') { while ((c = fgetc(fd3)) != '#') {
if (c == EOF) { if (c == EOF) {
return EXIT_FAILURE; return -1;
} }
} }
strncpy(pchScan, pchPrefix, 250); strncpy(pchScan, pchPrefix, 250);
strcat(pchScan, " %04x:"); strcat(pchScan, " %04x:");
if (fscanf(fd3, pchScan, &uiBytes) < 1) { if (fscanf(fd3, pchScan, &uiBytes) < 1) {
return EXIT_FAILURE; return -1;
} }
*pszBytes = uiBytes; *pszBytes = uiBytes;
if (*pszBytes > MAX_FRAME_LEN) { if (*pszBytes > MAX_FRAME_LEN) {
return EXIT_FAILURE; return -1;
} }
for (szPos = 0; szPos < *pszBytes; szPos++) { for (szPos = 0; szPos < *pszBytes; szPos++) {
if (fscanf(fd3, "%02x", &uiData) < 1) { if (fscanf(fd3, "%02x", &uiData) < 1) {
return EXIT_FAILURE; return -1;
} }
pbtData[szPos] = uiData; pbtData[szPos] = uiData;
} }
return EXIT_SUCCESS; return 0;
} }
int int
@ -158,7 +158,7 @@ main(int argc, char *argv[])
for (arg = 1; arg < argc; arg++) { for (arg = 1; arg < argc; arg++) {
if (0 == strcmp(argv[arg], "-h")) { if (0 == strcmp(argv[arg], "-h")) {
print_usage(argv); print_usage(argv);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} else if (0 == strcmp(argv[arg], "-q")) { } else if (0 == strcmp(argv[arg], "-q")) {
quiet_output = true; quiet_output = true;
} else if (0 == strcmp(argv[arg], "-t")) { } else if (0 == strcmp(argv[arg], "-t")) {
@ -176,13 +176,13 @@ main(int argc, char *argv[])
if (++arg == argc || (sscanf(argv[arg], "%i", &waiting_time) < 1)) { if (++arg == argc || (sscanf(argv[arg], "%i", &waiting_time) < 1)) {
ERR("Missing or wrong waiting time value: %s.", argv[arg]); ERR("Missing or wrong waiting time value: %s.", argv[arg]);
print_usage(argv); print_usage(argv);
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
printf("Waiting time: %i secs.\n", waiting_time); printf("Waiting time: %i secs.\n", waiting_time);
} else { } else {
ERR("%s is not supported option.", argv[arg]); ERR("%s is not supported option.", argv[arg]);
print_usage(argv); print_usage(argv);
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
} }
@ -205,20 +205,24 @@ main(int argc, char *argv[])
if (initiator_only_mode || target_only_mode) { if (initiator_only_mode || target_only_mode) {
if (szFound < 1) { if (szFound < 1) {
ERR("No device found"); ERR("No device found");
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
if ((fd3 = fdopen(3, "r")) == NULL) { if ((fd3 = fdopen(3, "r")) == NULL) {
ERR("Could not open file descriptor 3"); ERR("Could not open file descriptor 3");
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
if ((fd4 = fdopen(4, "r")) == NULL) { if ((fd4 = fdopen(4, "r")) == NULL) {
ERR("Could not open file descriptor 4"); ERR("Could not open file descriptor 4");
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
} else { } else {
if (szFound < 2) { if (szFound < 2) {
ERR("%zd device found but two opened devices are needed to relay NFC.", szFound); ERR("%zd device found but two opened devices are needed to relay NFC.", szFound);
return EXIT_FAILURE; nfc_exit(context);
exit(EXIT_FAILURE);
} }
} }
@ -234,8 +238,9 @@ main(int argc, char *argv[])
pndInitiator = nfc_open(context, connstrings[1]); pndInitiator = nfc_open(context, connstrings[1]);
} }
if (!pndInitiator) { if (pndInitiator == NULL) {
printf("Error opening NFC reader\n"); printf("Error opening NFC reader\n");
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -263,25 +268,25 @@ main(int argc, char *argv[])
printf("Found tag:\n"); printf("Found tag:\n");
print_nfc_target(ntRealTarget, false); print_nfc_target(ntRealTarget, false);
if (initiator_only_mode) { if (initiator_only_mode) {
if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") != EXIT_SUCCESS) { if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") < 0) {
fprintf(stderr, "Error while printing UID to FD4\n"); fprintf(stderr, "Error while printing UID to FD4\n");
nfc_close(pndInitiator); nfc_close(pndInitiator);
nfc_exit(context); nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (print_hex_fd4(ntRealTarget.nti.nai.abtAtqa, 2, "ATQA") != EXIT_SUCCESS) { if (print_hex_fd4(ntRealTarget.nti.nai.abtAtqa, 2, "ATQA") < 0) {
fprintf(stderr, "Error while printing ATQA to FD4\n"); fprintf(stderr, "Error while printing ATQA to FD4\n");
nfc_close(pndInitiator); nfc_close(pndInitiator);
nfc_exit(context); nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (print_hex_fd4(&(ntRealTarget.nti.nai.btSak), 1, "SAK") != EXIT_SUCCESS) { if (print_hex_fd4(&(ntRealTarget.nti.nai.btSak), 1, "SAK") < 0) {
fprintf(stderr, "Error while printing SAK to FD4\n"); fprintf(stderr, "Error while printing SAK to FD4\n");
nfc_close(pndInitiator); nfc_close(pndInitiator);
nfc_exit(context); nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (print_hex_fd4(ntRealTarget.nti.nai.abtAts, ntRealTarget.nti.nai.szAtsLen, "ATS") != EXIT_SUCCESS) { if (print_hex_fd4(ntRealTarget.nti.nai.abtAts, ntRealTarget.nti.nai.szAtsLen, "ATS") < 0) {
fprintf(stderr, "Error while printing ATS to FD4\n"); fprintf(stderr, "Error while printing ATS to FD4\n");
nfc_close(pndInitiator); nfc_close(pndInitiator);
nfc_exit(context); nfc_exit(context);
@ -305,24 +310,25 @@ main(int argc, char *argv[])
}; };
if (target_only_mode) { if (target_only_mode) {
size_t foo; size_t foo;
if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtUid, &(ntEmulatedTarget.nti.nai.szUidLen), "UID") != EXIT_SUCCESS) { if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtUid, &(ntEmulatedTarget.nti.nai.szUidLen), "UID") < 0) {
fprintf(stderr, "Error while scanning UID from FD3\n"); fprintf(stderr, "Error while scanning UID from FD3\n");
nfc_close(pndInitiator); nfc_close(pndInitiator);
nfc_exit(context); nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAtqa, &foo, "ATQA") != EXIT_SUCCESS) { if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAtqa, &foo, "ATQA") < 0) {
fprintf(stderr, "Error while scanning ATQA from FD3\n"); fprintf(stderr, "Error while scanning ATQA from FD3\n");
nfc_close(pndInitiator); nfc_close(pndInitiator);
nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (scan_hex_fd3(&(ntEmulatedTarget.nti.nai.btSak), &foo, "SAK") != EXIT_SUCCESS) { if (scan_hex_fd3(&(ntEmulatedTarget.nti.nai.btSak), &foo, "SAK") < 0) {
fprintf(stderr, "Error while scanning SAK from FD3\n"); fprintf(stderr, "Error while scanning SAK from FD3\n");
nfc_close(pndInitiator); nfc_close(pndInitiator);
nfc_exit(context); nfc_exit(context);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAts, &(ntEmulatedTarget.nti.nai.szAtsLen), "ATS") != EXIT_SUCCESS) { if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAts, &(ntEmulatedTarget.nti.nai.szAtsLen), "ATS") < 0) {
fprintf(stderr, "Error while scanning ATS from FD3\n"); fprintf(stderr, "Error while scanning ATS from FD3\n");
nfc_close(pndInitiator); nfc_close(pndInitiator);
nfc_exit(context); nfc_exit(context);
@ -375,7 +381,7 @@ main(int argc, char *argv[])
nfc_close(pndInitiator); nfc_close(pndInitiator);
} }
nfc_exit(context); nfc_exit(context);
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
printf("NFC emulator device: %s opened\n", nfc_device_get_name(pndTarget)); printf("NFC emulator device: %s opened\n", nfc_device_get_name(pndTarget));
@ -408,7 +414,7 @@ main(int argc, char *argv[])
} }
szCapduLen = (size_t) res; szCapduLen = (size_t) res;
if (target_only_mode) { if (target_only_mode) {
if (print_hex_fd4(abtCapdu, szCapduLen, "C-APDU") != EXIT_SUCCESS) { if (print_hex_fd4(abtCapdu, szCapduLen, "C-APDU") < 0) {
fprintf(stderr, "Error while printing C-APDU to FD4\n"); fprintf(stderr, "Error while printing C-APDU to FD4\n");
nfc_close(pndTarget); nfc_close(pndTarget);
nfc_exit(context); nfc_exit(context);
@ -416,7 +422,7 @@ main(int argc, char *argv[])
} }
} }
} else { } else {
if (scan_hex_fd3(abtCapdu, &szCapduLen, "C-APDU") != EXIT_SUCCESS) { if (scan_hex_fd3(abtCapdu, &szCapduLen, "C-APDU") < 0) {
fprintf(stderr, "Error while scanning C-APDU from FD3\n"); fprintf(stderr, "Error while scanning C-APDU from FD3\n");
nfc_close(pndInitiator); nfc_close(pndInitiator);
nfc_exit(context); nfc_exit(context);
@ -438,7 +444,7 @@ main(int argc, char *argv[])
ret = true; ret = true;
} }
} else { } else {
if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) { if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") < 0) {
fprintf(stderr, "Error while scanning R-APDU from FD3\n"); fprintf(stderr, "Error while scanning R-APDU from FD3\n");
nfc_close(pndTarget); nfc_close(pndTarget);
nfc_exit(context); nfc_exit(context);
@ -474,7 +480,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else { } else {
if (print_hex_fd4(abtRapdu, szRapduLen, "R-APDU") != EXIT_SUCCESS) { if (print_hex_fd4(abtRapdu, szRapduLen, "R-APDU") < 0) {
fprintf(stderr, "Error while printing R-APDU to FD4\n"); fprintf(stderr, "Error while printing R-APDU to FD4\n");
nfc_close(pndInitiator); nfc_close(pndInitiator);
nfc_exit(context); nfc_exit(context);

View file

@ -76,7 +76,7 @@ main(int argc, const char *argv[])
for (int arg = 1; arg < argc; arg++) { for (int arg = 1; arg < argc; arg++) {
if (0 == strcmp(argv[arg], "-h")) { if (0 == strcmp(argv[arg], "-h")) {
print_usage(argv); print_usage(argv);
return EXIT_SUCCESS; exit(EXIT_SUCCESS);
} else if (0 == strcmp(argv[arg], "-v")) { } else if (0 == strcmp(argv[arg], "-v")) {
verbose = true; verbose = true;
} else if (0 == strcmp(argv[arg], "-i")) { } else if (0 == strcmp(argv[arg], "-i")) {
@ -85,7 +85,7 @@ main(int argc, const char *argv[])
} else { } else {
ERR("%s is not supported option.", argv[arg]); ERR("%s is not supported option.", argv[arg]);
print_usage(argv); print_usage(argv);
return EXIT_FAILURE; exit(EXIT_FAILURE);
} }
} }
@ -98,10 +98,10 @@ main(int argc, const char *argv[])
nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_connstring connstrings[MAX_DEVICE_COUNT];
size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
int res = EXIT_FAILURE;
if (szDeviceFound == 0) { if (szDeviceFound == 0) {
printf("No NFC device found.\n"); printf("No NFC device found.\n");
goto bye; nfc_exit(context);
exit(EXIT_FAILURE);
} }
printf("%d NFC device(s) found:\n", (int)szDeviceFound); printf("%d NFC device(s) found:\n", (int)szDeviceFound);
@ -121,9 +121,6 @@ main(int argc, const char *argv[])
printf("nfc_open failed for %s\n", connstrings[i]); printf("nfc_open failed for %s\n", connstrings[i]);
} }
} }
res = EXIT_SUCCESS;
bye:
nfc_exit(context); nfc_exit(context);
return res; exit(EXIT_SUCCESS);
} }