Added WirelessManager, a port of wpa_supplicant.
[AROS.git] / workbench / network / WirelessManager / src / wps / wps_dev_attr.c
blob090bfa2bfc2b585bc0d963d9e2bfca1a06f62594
1 /*
2 * Wi-Fi Protected Setup - device attributes
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "wps_i.h"
19 #include "wps_dev_attr.h"
22 static int wps_build_manufacturer(struct wps_device_data *dev,
23 struct wpabuf *msg)
25 size_t len;
26 wpa_printf(MSG_DEBUG, "WPS: * Manufacturer");
27 wpabuf_put_be16(msg, ATTR_MANUFACTURER);
28 len = dev->manufacturer ? os_strlen(dev->manufacturer) : 0;
29 if (len == 0) {
31 * Some deployed WPS implementations fail to parse zero-length
32 * attributes. As a workaround, send a null character if the
33 * device attribute string is empty.
35 wpabuf_put_be16(msg, 1);
36 wpabuf_put_u8(msg, '\0');
37 } else {
38 wpabuf_put_be16(msg, len);
39 wpabuf_put_data(msg, dev->manufacturer, len);
41 return 0;
45 static int wps_build_model_name(struct wps_device_data *dev,
46 struct wpabuf *msg)
48 size_t len;
49 wpa_printf(MSG_DEBUG, "WPS: * Model Name");
50 wpabuf_put_be16(msg, ATTR_MODEL_NAME);
51 len = dev->model_name ? os_strlen(dev->model_name) : 0;
52 if (len == 0) {
54 * Some deployed WPS implementations fail to parse zero-length
55 * attributes. As a workaround, send a null character if the
56 * device attribute string is empty.
58 wpabuf_put_be16(msg, 1);
59 wpabuf_put_u8(msg, '\0');
60 } else {
61 wpabuf_put_be16(msg, len);
62 wpabuf_put_data(msg, dev->model_name, len);
64 return 0;
68 static int wps_build_model_number(struct wps_device_data *dev,
69 struct wpabuf *msg)
71 size_t len;
72 wpa_printf(MSG_DEBUG, "WPS: * Model Number");
73 wpabuf_put_be16(msg, ATTR_MODEL_NUMBER);
74 len = dev->model_number ? os_strlen(dev->model_number) : 0;
75 if (len == 0) {
77 * Some deployed WPS implementations fail to parse zero-length
78 * attributes. As a workaround, send a null character if the
79 * device attribute string is empty.
81 wpabuf_put_be16(msg, 1);
82 wpabuf_put_u8(msg, '\0');
83 } else {
84 wpabuf_put_be16(msg, len);
85 wpabuf_put_data(msg, dev->model_number, len);
87 return 0;
91 static int wps_build_serial_number(struct wps_device_data *dev,
92 struct wpabuf *msg)
94 size_t len;
95 wpa_printf(MSG_DEBUG, "WPS: * Serial Number");
96 wpabuf_put_be16(msg, ATTR_SERIAL_NUMBER);
97 len = dev->serial_number ? os_strlen(dev->serial_number) : 0;
98 if (len == 0) {
100 * Some deployed WPS implementations fail to parse zero-length
101 * attributes. As a workaround, send a null character if the
102 * device attribute string is empty.
104 wpabuf_put_be16(msg, 1);
105 wpabuf_put_u8(msg, '\0');
106 } else {
107 wpabuf_put_be16(msg, len);
108 wpabuf_put_data(msg, dev->serial_number, len);
110 return 0;
114 int wps_build_primary_dev_type(struct wps_device_data *dev, struct wpabuf *msg)
116 wpa_printf(MSG_DEBUG, "WPS: * Primary Device Type");
117 wpabuf_put_be16(msg, ATTR_PRIMARY_DEV_TYPE);
118 wpabuf_put_be16(msg, WPS_DEV_TYPE_LEN);
119 wpabuf_put_data(msg, dev->pri_dev_type, WPS_DEV_TYPE_LEN);
120 return 0;
124 static int wps_build_dev_name(struct wps_device_data *dev, struct wpabuf *msg)
126 size_t len;
127 wpa_printf(MSG_DEBUG, "WPS: * Device Name");
128 wpabuf_put_be16(msg, ATTR_DEV_NAME);
129 len = dev->device_name ? os_strlen(dev->device_name) : 0;
130 if (len == 0) {
132 * Some deployed WPS implementations fail to parse zero-length
133 * attributes. As a workaround, send a null character if the
134 * device attribute string is empty.
136 wpabuf_put_be16(msg, 1);
137 wpabuf_put_u8(msg, '\0');
138 } else {
139 wpabuf_put_be16(msg, len);
140 wpabuf_put_data(msg, dev->device_name, len);
142 return 0;
146 int wps_build_device_attrs(struct wps_device_data *dev, struct wpabuf *msg)
148 if (wps_build_manufacturer(dev, msg) ||
149 wps_build_model_name(dev, msg) ||
150 wps_build_model_number(dev, msg) ||
151 wps_build_serial_number(dev, msg) ||
152 wps_build_primary_dev_type(dev, msg) ||
153 wps_build_dev_name(dev, msg))
154 return -1;
155 return 0;
159 int wps_build_os_version(struct wps_device_data *dev, struct wpabuf *msg)
161 wpa_printf(MSG_DEBUG, "WPS: * OS Version");
162 wpabuf_put_be16(msg, ATTR_OS_VERSION);
163 wpabuf_put_be16(msg, 4);
164 wpabuf_put_be32(msg, 0x80000000 | dev->os_version);
165 return 0;
169 int wps_build_rf_bands(struct wps_device_data *dev, struct wpabuf *msg)
171 wpa_printf(MSG_DEBUG, "WPS: * RF Bands (%x)", dev->rf_bands);
172 wpabuf_put_be16(msg, ATTR_RF_BANDS);
173 wpabuf_put_be16(msg, 1);
174 wpabuf_put_u8(msg, dev->rf_bands);
175 return 0;
179 static int wps_process_manufacturer(struct wps_device_data *dev, const u8 *str,
180 size_t str_len)
182 if (str == NULL) {
183 wpa_printf(MSG_DEBUG, "WPS: No Manufacturer received");
184 return -1;
187 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer", str, str_len);
189 os_free(dev->manufacturer);
190 dev->manufacturer = os_malloc(str_len + 1);
191 if (dev->manufacturer == NULL)
192 return -1;
193 os_memcpy(dev->manufacturer, str, str_len);
194 dev->manufacturer[str_len] = '\0';
196 return 0;
200 static int wps_process_model_name(struct wps_device_data *dev, const u8 *str,
201 size_t str_len)
203 if (str == NULL) {
204 wpa_printf(MSG_DEBUG, "WPS: No Model Name received");
205 return -1;
208 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name", str, str_len);
210 os_free(dev->model_name);
211 dev->model_name = os_malloc(str_len + 1);
212 if (dev->model_name == NULL)
213 return -1;
214 os_memcpy(dev->model_name, str, str_len);
215 dev->model_name[str_len] = '\0';
217 return 0;
221 static int wps_process_model_number(struct wps_device_data *dev, const u8 *str,
222 size_t str_len)
224 if (str == NULL) {
225 wpa_printf(MSG_DEBUG, "WPS: No Model Number received");
226 return -1;
229 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number", str, str_len);
231 os_free(dev->model_number);
232 dev->model_number = os_malloc(str_len + 1);
233 if (dev->model_number == NULL)
234 return -1;
235 os_memcpy(dev->model_number, str, str_len);
236 dev->model_number[str_len] = '\0';
238 return 0;
242 static int wps_process_serial_number(struct wps_device_data *dev,
243 const u8 *str, size_t str_len)
245 if (str == NULL) {
246 wpa_printf(MSG_DEBUG, "WPS: No Serial Number received");
247 return -1;
250 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number", str, str_len);
252 os_free(dev->serial_number);
253 dev->serial_number = os_malloc(str_len + 1);
254 if (dev->serial_number == NULL)
255 return -1;
256 os_memcpy(dev->serial_number, str, str_len);
257 dev->serial_number[str_len] = '\0';
259 return 0;
263 static int wps_process_dev_name(struct wps_device_data *dev, const u8 *str,
264 size_t str_len)
266 if (str == NULL) {
267 wpa_printf(MSG_DEBUG, "WPS: No Device Name received");
268 return -1;
271 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name", str, str_len);
273 os_free(dev->device_name);
274 dev->device_name = os_malloc(str_len + 1);
275 if (dev->device_name == NULL)
276 return -1;
277 os_memcpy(dev->device_name, str, str_len);
278 dev->device_name[str_len] = '\0';
280 return 0;
284 static int wps_process_primary_dev_type(struct wps_device_data *dev,
285 const u8 *dev_type)
287 #ifndef CONFIG_NO_STDOUT_DEBUG
288 char devtype[WPS_DEV_TYPE_BUFSIZE];
289 #endif /* CONFIG_NO_STDOUT_DEBUG */
291 if (dev_type == NULL) {
292 wpa_printf(MSG_DEBUG, "WPS: No Primary Device Type received");
293 return -1;
296 os_memcpy(dev->pri_dev_type, dev_type, WPS_DEV_TYPE_LEN);
297 wpa_printf(MSG_DEBUG, "WPS: Primary Device Type: %s",
298 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
299 sizeof(devtype)));
301 return 0;
305 int wps_process_device_attrs(struct wps_device_data *dev,
306 struct wps_parse_attr *attr)
308 if (wps_process_manufacturer(dev, attr->manufacturer,
309 attr->manufacturer_len) ||
310 wps_process_model_name(dev, attr->model_name,
311 attr->model_name_len) ||
312 wps_process_model_number(dev, attr->model_number,
313 attr->model_number_len) ||
314 wps_process_serial_number(dev, attr->serial_number,
315 attr->serial_number_len) ||
316 wps_process_primary_dev_type(dev, attr->primary_dev_type) ||
317 wps_process_dev_name(dev, attr->dev_name, attr->dev_name_len))
318 return -1;
319 return 0;
323 int wps_process_os_version(struct wps_device_data *dev, const u8 *ver)
325 if (ver == NULL) {
326 wpa_printf(MSG_DEBUG, "WPS: No OS Version received");
327 return -1;
330 dev->os_version = WPA_GET_BE32(ver);
331 wpa_printf(MSG_DEBUG, "WPS: OS Version %08x", dev->os_version);
333 return 0;
337 int wps_process_rf_bands(struct wps_device_data *dev, const u8 *bands)
339 if (bands == NULL) {
340 wpa_printf(MSG_DEBUG, "WPS: No RF Bands received");
341 return -1;
344 dev->rf_bands = *bands;
345 wpa_printf(MSG_DEBUG, "WPS: Enrollee RF Bands 0x%x", dev->rf_bands);
347 return 0;
351 void wps_device_data_dup(struct wps_device_data *dst,
352 const struct wps_device_data *src)
354 if (src->device_name)
355 dst->device_name = os_strdup(src->device_name);
356 if (src->manufacturer)
357 dst->manufacturer = os_strdup(src->manufacturer);
358 if (src->model_name)
359 dst->model_name = os_strdup(src->model_name);
360 if (src->model_number)
361 dst->model_number = os_strdup(src->model_number);
362 if (src->serial_number)
363 dst->serial_number = os_strdup(src->serial_number);
364 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
365 dst->os_version = src->os_version;
366 dst->rf_bands = src->rf_bands;
370 void wps_device_data_free(struct wps_device_data *dev)
372 os_free(dev->device_name);
373 dev->device_name = NULL;
374 os_free(dev->manufacturer);
375 dev->manufacturer = NULL;
376 os_free(dev->model_name);
377 dev->model_name = NULL;
378 os_free(dev->model_number);
379 dev->model_number = NULL;
380 os_free(dev->serial_number);
381 dev->serial_number = NULL;