libnfc 1.8.0
nfc-relay.c
Go to the documentation of this file.
1/*-
2 * Free/Libre Near Field Communication (NFC) library
3 *
4 * Libnfc historical contributors:
5 * Copyright (C) 2009 Roel Verdult
6 * Copyright (C) 2009-2013 Romuald Conty
7 * Copyright (C) 2010-2012 Romain Tartière
8 * Copyright (C) 2010-2013 Philippe Teuwen
9 * Copyright (C) 2012-2013 Ludovic Rousseau
10 * See AUTHORS file for a more comprehensive list of contributors.
11 * Additional contributors of this file:
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 * 1) Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2 )Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Note that this license only applies on the examples, NFC library itself is under LGPL
34 *
35 */
36
42#ifdef HAVE_CONFIG_H
43# include "config.h"
44#endif /* HAVE_CONFIG_H */
45
46#include <inttypes.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <stdint.h>
50#include <string.h>
51#include <signal.h>
52
53#include <nfc/nfc.h>
54
55#include "utils/nfc-utils.h"
56
57#define MAX_FRAME_LEN 264
58#define MAX_DEVICE_COUNT 2
59
60static uint8_t abtReaderRx[MAX_FRAME_LEN];
61static uint8_t abtReaderRxPar[MAX_FRAME_LEN];
62static int szReaderRxBits;
63static uint8_t abtTagRx[MAX_FRAME_LEN];
64static uint8_t abtTagRxPar[MAX_FRAME_LEN];
65static int szTagRxBits;
66static nfc_device *pndReader;
67static nfc_device *pndTag;
68static bool quitting = false;
69
70static void
71intr_hdlr(int sig)
72{
73 (void) sig;
74 printf("\nQuitting...\n");
75 quitting = true;
76 return;
77}
78
79static void
80print_usage(char *argv[])
81{
82 printf("Usage: %s [OPTIONS]\n", argv[0]);
83 printf("Options:\n");
84 printf("\t-h\tHelp. Print this message.\n");
85 printf("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
86}
87
88int
89main(int argc, char *argv[])
90{
91 int arg;
92 bool quiet_output = false;
93 const char *acLibnfcVersion = nfc_version();
94
95 // Get commandline options
96 for (arg = 1; arg < argc; arg++) {
97 if (0 == strcmp(argv[arg], "-h")) {
98 print_usage(argv);
99 exit(EXIT_SUCCESS);
100 } else if (0 == strcmp(argv[arg], "-q")) {
101 quiet_output = true;
102 } else {
103 ERR("%s is not supported option.", argv[arg]);
104 print_usage(argv);
105 exit(EXIT_FAILURE);
106 }
107 }
108
109 // Display libnfc version
110 printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
111
112#ifdef WIN32
113 signal(SIGINT, (void (__cdecl *)(int)) intr_hdlr);
114#else
115 signal(SIGINT, intr_hdlr);
116#endif
117
118 nfc_context *context;
119 nfc_init(&context);
120 if (context == NULL) {
121 ERR("Unable to init libnfc (malloc)");
122 exit(EXIT_FAILURE);
123 }
124 nfc_connstring connstrings[MAX_DEVICE_COUNT];
125 // List available devices
126 size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
127
128 if (szFound < 2) {
129 ERR("%" PRIdPTR " device found but two opened devices are needed to relay NFC.", szFound);
130 nfc_exit(context);
131 exit(EXIT_FAILURE);
132 }
133
134 // Try to open the NFC emulator device
135 pndTag = nfc_open(context, connstrings[0]);
136 if (pndTag == NULL) {
137 ERR("Error opening NFC emulator device");
138 nfc_exit(context);
139 exit(EXIT_FAILURE);
140 }
141
142 printf("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n");
143
144 printf("NFC emulator device: %s opened\n", nfc_device_get_name(pndTag));
145 printf("[+] Try to break out the auto-emulation, this requires a second reader!\n");
146 printf("[+] To do this, please send any command after the anti-collision\n");
147 printf("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
148
149 nfc_target nt = {
150 .nm = {
151 .nmt = NMT_ISO14443A,
152 .nbr = NBR_UNDEFINED,
153 },
154 .nti = {
155 .nai = {
156 .abtAtqa = { 0x04, 0x00 },
157 .abtUid = { 0x08, 0xad, 0xbe, 0xef },
158 .btSak = 0x20,
159 .szUidLen = 4,
160 .szAtsLen = 0,
161 },
162 },
163 };
164
165 if ((szReaderRxBits = nfc_target_init(pndTag, &nt, abtReaderRx, sizeof(abtReaderRx), 0)) < 0) {
166 ERR("%s", "Initialization of NFC emulator failed");
167 nfc_close(pndTag);
168 nfc_exit(context);
169 exit(EXIT_FAILURE);
170 }
171 printf("%s", "Configuring emulator settings...");
172 if ((nfc_device_set_property_bool(pndTag, NP_HANDLE_CRC, false) < 0) ||
174 nfc_perror(pndTag, "nfc_device_set_property_bool");
175 nfc_close(pndTag);
176 nfc_exit(context);
177 exit(EXIT_FAILURE);
178 }
179 printf("%s", "Done, emulated tag is initialized");
180
181 // Try to open the NFC reader
182 pndReader = nfc_open(context, connstrings[1]);
183 if (pndReader == NULL) {
184 printf("Error opening NFC reader device\n");
185 nfc_close(pndTag);
186 nfc_exit(context);
187 exit(EXIT_FAILURE);
188 }
189
190 printf("NFC reader device: %s opened", nfc_device_get_name(pndReader));
191 printf("%s", "Configuring NFC reader settings...");
192
193 if (nfc_initiator_init(pndReader) < 0) {
194 nfc_perror(pndReader, "nfc_initiator_init");
195 nfc_close(pndTag);
196 nfc_close(pndReader);
197 nfc_exit(context);
198 exit(EXIT_FAILURE);
199 }
200 if ((nfc_device_set_property_bool(pndReader, NP_HANDLE_CRC, false) < 0) ||
201 (nfc_device_set_property_bool(pndReader, NP_HANDLE_PARITY, false) < 0) ||
203 nfc_perror(pndReader, "nfc_device_set_property_bool");
204 nfc_close(pndTag);
205 nfc_close(pndReader);
206 nfc_exit(context);
207 exit(EXIT_FAILURE);
208 }
209 printf("%s", "Done, relaying frames now!");
210
211 while (!quitting) {
212 // Test if we received a frame from the reader
213 if ((szReaderRxBits = nfc_target_receive_bits(pndTag, abtReaderRx, sizeof(abtReaderRx), abtReaderRxPar)) > 0) {
214 // Drop down the field before sending a REQA command and start a new session
215 if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26) {
216 // Drop down field for a very short time (original tag will reboot)
217 if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, false) < 0) {
218 nfc_perror(pndReader, "nfc_device_set_property_bool");
219 nfc_close(pndTag);
220 nfc_close(pndReader);
221 nfc_exit(context);
222 exit(EXIT_FAILURE);
223 }
224 if (!quiet_output)
225 printf("\n");
226 if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, true) < 0) {
227 nfc_perror(pndReader, "nfc_device_set_property_bool");
228 nfc_close(pndTag);
229 nfc_close(pndReader);
230 nfc_exit(context);
231 exit(EXIT_FAILURE);
232 }
233 }
234 // Print the reader frame to the screen
235 if (!quiet_output) {
236 printf("R: ");
237 print_hex_par(abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar);
238 }
239 // Forward the frame to the original tag
240 if ((szTagRxBits = nfc_initiator_transceive_bits
241 (pndReader, abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar, abtTagRx, sizeof(abtTagRx), abtTagRxPar)) > 0) {
242 // Redirect the answer back to the reader
243 if (nfc_target_send_bits(pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) {
244 nfc_perror(pndTag, "nfc_target_send_bits");
245 nfc_close(pndTag);
246 nfc_close(pndReader);
247 nfc_exit(context);
248 exit(EXIT_FAILURE);
249 }
250 // Print the tag frame to the screen
251 if (!quiet_output) {
252 printf("T: ");
253 print_hex_par(abtTagRx, szTagRxBits, abtTagRxPar);
254 }
255 }
256 }
257 }
258
259 nfc_close(pndTag);
260 nfc_close(pndReader);
261 nfc_exit(context);
262 exit(EXIT_SUCCESS);
263}
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition nfc.c:1209
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition nfc.c:339
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition nfc.c:277
size_t nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
Scan for discoverable supported devices (ie. only available for some drivers)
Definition nfc.c:356
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
Definition nfc.c:1183
int nfc_initiator_transceive_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
Transceive raw bit-frames to a target.
Definition nfc.c:852
int nfc_initiator_init(nfc_device *pnd)
Initialize NFC device as initiator (reader)
Definition nfc.c:493
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
Definition nfc.c:248
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition nfc.c:231
const char * nfc_version(void)
Returns the library version.
Definition nfc.c:1319
int nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
Set a device's boolean-property value.
Definition nfc.c:466
int nfc_target_send_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar)
Send raw bit-frames.
Definition nfc.c:1094
int nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout)
Initialize NFC device as an emulated tag.
Definition nfc.c:978
int nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
Receive bit-frames.
Definition nfc.c:1116
@ NP_HANDLE_CRC
Definition nfc-types.h:94
@ NP_ACCEPT_INVALID_FRAMES
Definition nfc-types.h:119
@ NP_ACTIVATE_FIELD
Definition nfc-types.h:105
@ NP_HANDLE_PARITY
Definition nfc-types.h:102
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
Definition nfc-types.h:63
Provide some examples shared functions like print, parity calculation, options parsing.
#define ERR(...)
Print a error message.
Definition nfc-utils.h:85
libnfc interface
NFC library context Struct which contains internal options, references, pointers, etc....
NFC device information.
NFC target structure.
Definition nfc-types.h:351