1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/*
*
* neard - Near Field Communication manager
*
* Copyright (C) 2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/socket.h>
#include <near/nfc_copy.h>
#include <near/plugin.h>
#include <near/log.h>
#include <near/types.h>
#include <near/adapter.h>
#include <near/device.h>
#include <near/ndef.h>
#include <near/tlv.h>
#include <near/snep.h>
#include "p2p.h"
/* Would store incoming ndefs per client */
static GHashTable *snep_validation_hash = NULL;
/* Callback: free validation data */
static void free_snep_validation_client(gpointer data)
{
GList *old_ndefs = data;
if (old_ndefs != NULL)
g_list_free(old_ndefs);
}
/* Validation Server REQ_PUT function
* The validation server shall accept PUT and GET requests. A PUT request shall
* cause the server to store the ndef message transmitted with the request.
* */
static near_bool_t snep_validation_server_req_put(int client_fd, void *data)
{
struct p2p_snep_data *snep_data = data;
GList *records;
struct near_ndef_record *recd;
GList *incoming_ndefs;
DBG("");
if (snep_data->nfc_data == NULL)
goto error;
/*
* We received a ndef, parse it, check if there's only
* 1 record (a mime type !) with an ID
*/
records = near_ndef_parse_msg(snep_data->nfc_data,
snep_data->nfc_data_length, NULL);
if (g_list_length(records) != 1) {
DBG("records number mismatch");
goto error;
}
recd = records->data;
if (recd == NULL) {
g_list_free(records);
goto error;
}
/* Save the record but look if there are some incoming ndef stored */
incoming_ndefs = g_hash_table_lookup(snep_validation_hash,
GINT_TO_POINTER(client_fd));
incoming_ndefs = g_list_append(incoming_ndefs, recd);
/* remove existing one silently */
g_hash_table_steal(snep_validation_hash, GINT_TO_POINTER(client_fd));
/* push the new one */
g_hash_table_insert(snep_validation_hash, GINT_TO_POINTER(client_fd),
incoming_ndefs);
near_snep_core_response_noinfo(client_fd, NEAR_SNEP_RESP_SUCCESS);
return TRUE;
error:
near_snep_core_response_noinfo(client_fd, NEAR_SNEP_RESP_REJECT);
return TRUE;
}
/*
* Validation Server REQ_GET function
* The validation server shall accept PUT and GET requests. A GET request shall
* cause the server to return a previously stored NDEF message of the same NDEF
* message type and identifier as transmitted with the request.
* */
static near_bool_t snep_validation_server_req_get(int client_fd, void *data)
{
struct p2p_snep_data *snep_data = data;
struct near_ndef_record *recd, *rec_store;
uint32_t acceptable_length;
GList *records;
GList *iter;
GList *incoming_ndefs;
DBG("");
/*
* We received a ndef, parse it, check if there's only
* 1 record (a mime type !) with an ID
*/
records = near_ndef_parse_msg(snep_data->nfc_data +
NEAR_SNEP_ACC_LENGTH_SIZE,
snep_data->nfc_data_length - NEAR_SNEP_ACC_LENGTH_SIZE,
NULL);
if (g_list_length(records) != 1) {
DBG("records number mismatch");
goto error;
}
recd = records->data;
if (recd == NULL) {
g_list_free(records);
goto error;
}
/* check if the acceptable length is higher than the data_len
* otherwise returns a NEAR_SNEP_RESP_EXCESS
*/
acceptable_length = GUINT32_FROM_BE(*(uint32_t *)snep_data->nfc_data);
/* Look if there are some incoming ndef stored */
incoming_ndefs = g_hash_table_lookup(snep_validation_hash,
GINT_TO_POINTER(client_fd));
if (incoming_ndefs == NULL)
goto done;
/* Now, loop to find the the associated record */
for (iter = incoming_ndefs; iter; iter = iter->next) {
rec_store = iter->data;
/* Same mime type and same id ?*/
if (near_ndef_record_cmp_id(recd, rec_store) == FALSE)
continue;
if (near_ndef_record_cmp_mime(recd, rec_store) == FALSE)
continue;
/* Found a record, check the length */
if (acceptable_length >= near_ndef_data_length(rec_store)) {
near_snep_core_response_with_info(client_fd,
NEAR_SNEP_RESP_SUCCESS,
near_ndef_data_ptr(rec_store),
near_ndef_data_length(rec_store));
incoming_ndefs = g_list_remove(incoming_ndefs,
iter->data);
/* remove existing one silently */
g_hash_table_steal(snep_validation_hash,
GINT_TO_POINTER(client_fd));
/* push the new one */
g_hash_table_insert(snep_validation_hash,
GINT_TO_POINTER(client_fd),
incoming_ndefs);
} else
near_snep_core_response_noinfo(client_fd, NEAR_SNEP_RESP_EXCESS);
return TRUE;
}
done:
/* If not found */
near_snep_core_response_noinfo(client_fd, NEAR_SNEP_RESP_NOT_FOUND);
return TRUE;
error:
/* Not found */
near_snep_core_response_noinfo(client_fd, NEAR_SNEP_RESP_REJECT);
return FALSE;
}
/* This function is a wrapper to push post processing read functions */
static near_bool_t snep_validation_read(int client_fd, uint32_t adapter_idx,
uint32_t target_idx,
near_tag_io_cb cb)
{
DBG("");
return near_snep_core_read(client_fd, adapter_idx, target_idx, cb,
snep_validation_server_req_get,
snep_validation_server_req_put);
}
static void snep_validation_close(int client_fd, int err)
{
DBG("");
g_hash_table_remove(snep_validation_hash, GINT_TO_POINTER(client_fd));
/* Call core server close */
near_snep_core_close(client_fd, err);
}
struct near_p2p_driver validation_snep_driver = {
.name = "VALIDATION_SNEP",
.service_name = "urn:nfc:xsn:nfc-forum.org:snep-validation",
.fallback_service_name = NULL,
.read = snep_validation_read,
.push = near_snep_core_push,
.close = snep_validation_close,
};
int snep_validation_init(void)
{
/* Would store incoming ndefs per client */
snep_validation_hash = g_hash_table_new_full(g_direct_hash,
g_direct_equal, NULL,
free_snep_validation_client);
return near_p2p_register(&validation_snep_driver);
}
void snep_validation_exit(void)
{
near_p2p_unregister(&validation_snep_driver);
g_hash_table_destroy(snep_validation_hash);
snep_validation_hash = NULL;
}
|