ndef.c
Go to the documentation of this file.
00001 /*
00002  * NDEF(NFC Data Exchange Format) routines for Wi-Fi Protected Setup
00003  *   Reference is "NFCForum-TS-NDEF_1.0 2006-07-24".
00004  * Copyright (c) 2009, Masashi Honma <honma@ictec.co.jp>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License version 2 as
00008  * published by the Free Software Foundation.
00009  *
00010  * Alternatively, this software may be distributed under the terms of BSD
00011  * license.
00012  *
00013  * See README and COPYING for more details.
00014  */
00015 
00016 #include "includes.h"
00017 #include "common.h"
00018 #include "wps/wps.h"
00019 #include "wps/wps_i.h"
00020 
00021 #define FLAG_MESSAGE_BEGIN (1 << 7)
00022 #define FLAG_MESSAGE_END (1 << 6)
00023 #define FLAG_CHUNK (1 << 5)
00024 #define FLAG_SHORT_RECORD (1 << 4)
00025 #define FLAG_ID_LENGTH_PRESENT (1 << 3)
00026 #define FLAG_TNF_RFC2046 (0x02)
00027 
00028 struct ndef_record {
00029         u8 *type;
00030         u8 *id;
00031         u8 *payload;
00032         u8 type_length;
00033         u8 id_length;
00034         u32 payload_length;
00035         u32 total_length;
00036 };
00037 
00038 static char wifi_handover_type[] = "application/vnd.wfa.wsc";
00039 
00040 static int ndef_parse_record(u8 *data, u32 size, struct ndef_record *record)
00041 {
00042         u8 *pos = data + 1;
00043 
00044         if (size < 2)
00045                 return -1;
00046         record->type_length = *pos++;
00047         if (data[0] & FLAG_SHORT_RECORD) {
00048                 if (size < 3)
00049                         return -1;
00050                 record->payload_length = *pos++;
00051         } else {
00052                 if (size < 6)
00053                         return -1;
00054                 record->payload_length = ntohl(*(u32 *)pos);
00055                 pos += sizeof(u32);
00056         }
00057 
00058         if (data[0] & FLAG_ID_LENGTH_PRESENT) {
00059                 if ((int) size < pos - data + 1)
00060                         return -1;
00061                 record->id_length = *pos++;
00062         } else
00063                 record->id_length = 0;
00064 
00065         record->type = record->type_length == 0 ? NULL : pos;
00066         pos += record->type_length;
00067 
00068         record->id = record->id_length == 0 ? NULL : pos;
00069         pos += record->id_length;
00070 
00071         record->payload = record->payload_length == 0 ? NULL : pos;
00072         pos += record->payload_length;
00073 
00074         record->total_length = pos - data;
00075         if (record->total_length > size)
00076                 return -1;
00077         return 0;
00078 }
00079 
00080 
00081 static struct wpabuf * ndef_parse_records(struct wpabuf *buf,
00082                                           int (*filter)(struct ndef_record *))
00083 {
00084         struct ndef_record record;
00085         int len = wpabuf_len(buf);
00086         u8 *data = wpabuf_mhead(buf);
00087 
00088         while (len > 0) {
00089                 if (ndef_parse_record(data, len, &record) < 0) {
00090                         wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
00091                         return NULL;
00092                 }
00093                 if (filter == NULL || filter(&record))
00094                         return wpabuf_alloc_copy(record.payload,
00095                                                  record.payload_length);
00096                 data += record.total_length;
00097                 len -= record.total_length;
00098         }
00099         wpa_printf(MSG_ERROR, "NDEF : Record not found");
00100         return NULL;
00101 }
00102 
00103 
00104 static struct wpabuf * ndef_build_record(u8 flags, void *type,
00105                                          u8 type_length, void *id,
00106                                          u8 id_length, void *payload,
00107                                          u32 payload_length)
00108 {
00109         struct wpabuf *record;
00110         size_t total_len;
00111         int short_record;
00112         u8 local_flag;
00113 
00114         short_record = payload_length < 256 ? 1 : 0;
00115 
00116         total_len = 2; /* flag + type length */
00117         /* payload length */
00118         total_len += short_record ? sizeof(u8) : sizeof(u32);
00119         if (id_length > 0)
00120                 total_len += 1;
00121         total_len += type_length + id_length + payload_length;
00122         record = wpabuf_alloc(total_len);
00123         if (record == NULL) {
00124                 wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
00125                            "record for build");
00126                 return NULL;
00127         }
00128 
00129         local_flag = flags;
00130         if (id_length > 0)
00131                 local_flag |= FLAG_ID_LENGTH_PRESENT;
00132         if (short_record)
00133                 local_flag |= FLAG_SHORT_RECORD;
00134         wpabuf_put_u8(record, local_flag);
00135 
00136         wpabuf_put_u8(record, type_length);
00137 
00138         if (short_record)
00139                 wpabuf_put_u8(record, payload_length);
00140         else
00141                 wpabuf_put_be32(record, payload_length);
00142 
00143         if (id_length > 0)
00144                 wpabuf_put_u8(record, id_length);
00145         wpabuf_put_data(record, type, type_length);
00146         wpabuf_put_data(record, id, id_length);
00147         wpabuf_put_data(record, payload, payload_length);
00148         return record;
00149 }
00150 
00151 
00152 static int wifi_filter(struct ndef_record *record)
00153 {
00154         if (record->type_length != os_strlen(wifi_handover_type))
00155                 return 0;
00156         if (os_memcmp(record->type, wifi_handover_type,
00157                       os_strlen(wifi_handover_type)) != 0)
00158                 return 0;
00159         return 1;
00160 }
00161 
00162 
00163 struct wpabuf * ndef_parse_wifi(struct wpabuf *buf)
00164 {
00165         return ndef_parse_records(buf, wifi_filter);
00166 }
00167 
00168 
00169 struct wpabuf * ndef_build_wifi(struct wpabuf *buf)
00170 {
00171         return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
00172                                  FLAG_TNF_RFC2046, wifi_handover_type,
00173                                  os_strlen(wifi_handover_type), NULL, 0,
00174                                  wpabuf_mhead(buf), wpabuf_len(buf));
00175 }


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:35