dbus: Split wpas_dbus_handler_wps_start() into helper functions

This commit is contained in:
Jouni Malinen 2010-01-01 22:33:47 +02:00
parent d89530f776
commit d8d8c6d02a

View file

@ -24,6 +24,154 @@
#include "dbus_new_handlers.h"
#include "dbus_dict_helpers.h"
struct wps_start_params {
int role; /* 0 - not set, 1 - enrollee, 2 - registrar */
int type; /* 0 - not set, 1 - pin, 2 - pbc */
u8 *bssid;
char *pin;
};
static int wpas_dbus_handler_wps_role(DBusMessage *message,
DBusMessageIter *entry_iter,
struct wps_start_params *params,
DBusMessage **reply)
{
DBusMessageIter variant_iter;
char *val;
dbus_message_iter_recurse(entry_iter, &variant_iter);
if (dbus_message_iter_get_arg_type(&variant_iter) !=
DBUS_TYPE_STRING) {
wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Wrong Role type, "
"string required");
*reply = wpas_dbus_error_invalid_args(message,
"Role must be a string");
return -1;
}
dbus_message_iter_get_basic(&variant_iter, &val);
if (os_strcmp(val, "enrollee") == 0)
params->role = 1;
else if (os_strcmp(val, "registrar") == 0)
params->role = 2;
else {
wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Uknown role %s", val);
*reply = wpas_dbus_error_invalid_args(message, val);
return -1;
}
return 0;
}
static int wpas_dbus_handler_wps_type(DBusMessage *message,
DBusMessageIter *entry_iter,
struct wps_start_params *params,
DBusMessage **reply)
{
DBusMessageIter variant_iter;
char *val;
dbus_message_iter_recurse(entry_iter, &variant_iter);
if (dbus_message_iter_get_arg_type(&variant_iter) !=
DBUS_TYPE_STRING) {
wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Wrong Type type, "
"string required");
*reply = wpas_dbus_error_invalid_args(message,
"Type must be a string");
return -1;
}
dbus_message_iter_get_basic(&variant_iter, &val);
if (os_strcmp(val, "pin") == 0)
params->type = 1;
else if (os_strcmp(val, "pbc") == 0)
params->type = 2;
else {
wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Unknown type %s",
val);
*reply = wpas_dbus_error_invalid_args(message, val);
return -1;
}
return 0;
}
static int wpas_dbus_handler_wps_bssid(DBusMessage *message,
DBusMessageIter *entry_iter,
struct wps_start_params *params,
DBusMessage **reply)
{
DBusMessageIter variant_iter, array_iter;
int len;
dbus_message_iter_recurse(entry_iter, &variant_iter);
if (dbus_message_iter_get_arg_type(&variant_iter) != DBUS_TYPE_ARRAY ||
dbus_message_iter_get_element_type(&variant_iter) !=
DBUS_TYPE_ARRAY) {
wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Wrong Bssid type, "
"byte array required");
*reply = wpas_dbus_error_invalid_args(
message, "Bssid must be a byte array");
return -1;
}
dbus_message_iter_recurse(&variant_iter, &array_iter);
dbus_message_iter_get_fixed_array(&array_iter, &params->bssid, &len);
if (len != ETH_ALEN) {
wpa_printf(MSG_DEBUG, "dbus: WPS.Stsrt - Wrong Bssid length "
"%d", len);
*reply = wpas_dbus_error_invalid_args(message,
"Bssid is wrong length");
return -1;
}
return 0;
}
static int wpas_dbus_handler_wps_pin(DBusMessage *message,
DBusMessageIter *entry_iter,
struct wps_start_params *params,
DBusMessage **reply)
{
DBusMessageIter variant_iter;
dbus_message_iter_recurse(entry_iter, &variant_iter);
if (dbus_message_iter_get_arg_type(&variant_iter) !=
DBUS_TYPE_STRING) {
wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Wrong Pin type, "
"string required");
*reply = wpas_dbus_error_invalid_args(message,
"Pin must be a string");
return -1;
}
dbus_message_iter_get_basic(&variant_iter, &params->pin);
return 0;
}
static int wpas_dbus_handler_wps_start_entry(DBusMessage *message, char *key,
DBusMessageIter *entry_iter,
struct wps_start_params *params,
DBusMessage **reply)
{
if (os_strcmp(key, "Role") == 0)
return wpas_dbus_handler_wps_role(message, entry_iter,
params, reply);
else if (os_strcmp(key, "Type") == 0)
return wpas_dbus_handler_wps_type(message, entry_iter,
params, reply);
else if (os_strcmp(key, "Bssid") == 0)
return wpas_dbus_handler_wps_bssid(message, entry_iter,
params, reply);
else if (os_strcmp(key, "Pin") == 0)
return wpas_dbus_handler_wps_pin(message, entry_iter,
params, reply);
wpa_printf(MSG_DEBUG, "dbus: WPS.Start - unknown key %s", key);
*reply = wpas_dbus_error_invalid_args(message, key);
return -1;
}
/**
* wpas_dbus_handler_wps_start - Start WPS configuration
* @message: Pointer to incoming dbus message
@ -38,17 +186,14 @@
DBusMessage * wpas_dbus_handler_wps_start(DBusMessage *message,
struct wpa_supplicant *wpa_s)
{
DBusMessage * reply = NULL;
DBusMessageIter iter, dict_iter, entry_iter, variant_iter, array_iter;
char *key, *val;
int role = 0; /* 0 - not set, 1 - enrollee, 2 - registrar */
int type = 0; /* 0 - not set, 1 - pin, 2 - pbc */
u8 *bssid = NULL;
char *pin = NULL, npin[9] = { '\0' };
int len, ret;
DBusMessage *reply = NULL;
DBusMessageIter iter, dict_iter, entry_iter;
struct wps_start_params params;
char *key;
char npin[9] = { '\0' };
int ret;
os_memset(&params, 0, sizeof(params));
dbus_message_iter_init(message, &iter);
dbus_message_iter_recurse(&iter, &dict_iter);
@ -59,130 +204,47 @@ DBusMessage * wpas_dbus_handler_wps_start(DBusMessage *message,
dbus_message_iter_get_basic(&entry_iter, &key);
dbus_message_iter_next(&entry_iter);
if (os_strcmp(key, "Role") == 0) {
dbus_message_iter_recurse(&entry_iter, &variant_iter);
if (dbus_message_iter_get_arg_type(&variant_iter) !=
DBUS_TYPE_STRING) {
wpa_printf(MSG_DEBUG,
"wpas_dbus_handler_wps_start"
"[dbus]: "
"wrong Role type. string required");
return wpas_dbus_error_invalid_args(
message, "Role must be a string");
}
dbus_message_iter_get_basic(&variant_iter, &val);
if (os_strcmp(val, "enrollee") == 0)
role = 1;
else if (os_strcmp(val, "registrar") == 0)
role = 2;
else {
wpa_printf(MSG_DEBUG,
"wpas_dbus_handler_wps_start[dbus]: "
"unknown role %s", val);
return wpas_dbus_error_invalid_args(message,
val);
}
} else if (strcmp(key, "Type") == 0) {
dbus_message_iter_recurse(&entry_iter, &variant_iter);
if (dbus_message_iter_get_arg_type(&variant_iter) !=
DBUS_TYPE_STRING) {
wpa_printf(MSG_DEBUG,
"wpas_dbus_handler_wps_start[dbus]: "
"wrong Type type. string required");
return wpas_dbus_error_invalid_args(
message, "Type must be a string");
}
dbus_message_iter_get_basic(&variant_iter, &val);
if (os_strcmp(val, "pin") == 0)
type = 1;
else if (os_strcmp(val, "pbc") == 0)
type = 2;
else {
wpa_printf(MSG_DEBUG,
"wpas_dbus_handler_wps_start[dbus]: "
"unknown type %s", val);
return wpas_dbus_error_invalid_args(message,
val);
}
} else if (strcmp(key, "Bssid") == 0) {
dbus_message_iter_recurse(&entry_iter, &variant_iter);
if (dbus_message_iter_get_arg_type(&variant_iter) !=
DBUS_TYPE_ARRAY ||
dbus_message_iter_get_element_type(&variant_iter) !=
DBUS_TYPE_ARRAY) {
wpa_printf(MSG_DEBUG,
"wpas_dbus_handler_wps_start[dbus]: "
"wrong Bssid type. byte array required");
return wpas_dbus_error_invalid_args(
message, "Bssid must be a byte array");
}
dbus_message_iter_recurse(&variant_iter, &array_iter);
dbus_message_iter_get_fixed_array(&array_iter, &bssid,
&len);
if (len != ETH_ALEN) {
wpa_printf(MSG_DEBUG,
"wpas_dbus_handler_wps_start[dbus]: "
"wrong Bssid length %d", len);
return wpas_dbus_error_invalid_args(
message, "Bssid is wrong length");
}
}
else if (os_strcmp(key, "Pin") == 0) {
dbus_message_iter_recurse(&entry_iter, &variant_iter);
if (dbus_message_iter_get_arg_type(&variant_iter) !=
DBUS_TYPE_STRING) {
wpa_printf(MSG_DEBUG,
"wpas_dbus_handler_wps_start[dbus]: "
"wrong Pin type. string required");
return wpas_dbus_error_invalid_args(
message, "Pin must be a string");
}
dbus_message_iter_get_basic(&variant_iter, &pin);
} else {
wpa_printf(MSG_DEBUG,
"wpas_dbus_handler_wps_start[dbus]: "
"unknown key %s", key);
return wpas_dbus_error_invalid_args(message, key);
}
if (wpas_dbus_handler_wps_start_entry(message, key,
&entry_iter,
&params, &reply))
return reply;
dbus_message_iter_next(&dict_iter);
}
if (role == 0) {
wpa_printf(MSG_DEBUG, "wpas_dbus_handler_wps_start[dbus]: "
"Role not specified");
if (params.role == 0) {
wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Role not specified");
return wpas_dbus_error_invalid_args(message,
"Role not specified");
}
else if (role == 1 && type == 0) {
wpa_printf(MSG_DEBUG, "wpas_dbus_handler_wps_start[dbus]: "
"Type not specified");
} else if (params.role == 1 && params.type == 0) {
wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Type not specified");
return wpas_dbus_error_invalid_args(message,
"Type not specified");
}
else if (role == 2 && pin == NULL) {
wpa_printf(MSG_DEBUG, "wpas_dbus_handler_wps_start[dbus]: "
"Pin required for registrar role.");
} else if (params.role == 2 && params.pin == NULL) {
wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Pin required for "
"registrar role");
return wpas_dbus_error_invalid_args(
message, "Pin required for registrar role.");
}
if (role == 2)
ret = wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
else if (type == 1) {
ret = wpas_wps_start_pin(wpa_s, bssid, pin);
if (params.role == 2)
ret = wpas_wps_start_reg(wpa_s, params.bssid, params.pin,
NULL);
else if (params.type == 1) {
ret = wpas_wps_start_pin(wpa_s, params.bssid, params.pin);
if (ret > 0)
os_snprintf(npin, sizeof(npin), "%08d", ret);
} else
ret = wpas_wps_start_pbc(wpa_s, bssid);
ret = wpas_wps_start_pbc(wpa_s, params.bssid);
if (ret < 0) {
wpa_printf(MSG_DEBUG, "wpas_dbus_handler_wps_start[dbus]: "
"wpas_wps_failed in role %s and key %s.",
(role == 1 ? "enrollee" : "registrar"),
(type == 0 ? "" : (type == 1 ? "pin" : "pbc")));
wpa_printf(MSG_DEBUG, "dbus: WPS.Start wpas_wps_failed in "
"role %s and key %s",
(params.role == 1 ? "enrollee" : "registrar"),
(params.type == 0 ? "" :
(params.type == 1 ? "pin" : "pbc")));
return wpas_dbus_error_unknown_error(message,
"wps start failed");
"WPS start failed");
}
reply = dbus_message_new_method_return(message);