WPS UFD: Use private data structure for oob_device_data

This gets rid of global variable use and may be needed to allow
concurrent access in some case if the interface is extended.
This commit is contained in:
Jouni Malinen 2009-02-26 22:04:49 +02:00
parent 390cd3105d
commit 70e070461d
3 changed files with 46 additions and 29 deletions

View file

@ -532,10 +532,10 @@ struct wps_context {
struct oob_device_data { struct oob_device_data {
char *device_path; char *device_path;
int (*init_func)(struct wps_context *, int); void * (*init_func)(struct wps_context *, int);
struct wpabuf * (*read_func)(void); struct wpabuf * (*read_func)(void *);
int (*write_func)(struct wpabuf *); int (*write_func)(void *, struct wpabuf *);
int (*deinit_func)(void); void (*deinit_func)(void *);
}; };
struct wps_registrar * struct wps_registrar *

View file

@ -473,10 +473,12 @@ int wps_process_oob(struct wps_context *wps, int registrar)
struct oob_device_data *oob_dev = wps->oob_dev; struct oob_device_data *oob_dev = wps->oob_dev;
struct wpabuf *data; struct wpabuf *data;
int ret, write_f, oob_method = wps->oob_conf.oob_method; int ret, write_f, oob_method = wps->oob_conf.oob_method;
void *oob_priv;
write_f = oob_method == OOB_METHOD_DEV_PWD_E ? !registrar : registrar; write_f = oob_method == OOB_METHOD_DEV_PWD_E ? !registrar : registrar;
if (oob_dev->init_func(wps, registrar) < 0) { oob_priv = oob_dev->init_func(wps, registrar);
if (oob_priv == NULL) {
wpa_printf(MSG_ERROR, "WPS: Failed to initialize OOB device"); wpa_printf(MSG_ERROR, "WPS: Failed to initialize OOB device");
return -1; return -1;
} }
@ -488,12 +490,15 @@ int wps_process_oob(struct wps_context *wps, int registrar)
data = wps_get_oob_dev_pwd(wps); data = wps_get_oob_dev_pwd(wps);
ret = 0; ret = 0;
if (data == NULL || wps->oob_dev->write_func(data) < 0) if (data == NULL ||
wps->oob_dev->write_func(oob_priv, data) < 0)
ret = -1; ret = -1;
} else { } else {
data = oob_dev->read_func(); data = oob_dev->read_func(oob_priv);
if (data == NULL) if (data == NULL) {
oob_dev->deinit_func(oob_priv);
return -1; return -1;
}
if (oob_method == OOB_METHOD_CRED) if (oob_method == OOB_METHOD_CRED)
ret = wps_parse_oob_cred(wps, data); ret = wps_parse_oob_cred(wps, data);
@ -503,14 +508,11 @@ int wps_process_oob(struct wps_context *wps, int registrar)
wpabuf_free(data); wpabuf_free(data);
if (ret < 0) { if (ret < 0) {
wpa_printf(MSG_ERROR, "WPS: Failed to process OOB data"); wpa_printf(MSG_ERROR, "WPS: Failed to process OOB data");
oob_dev->deinit_func(oob_priv);
return -1; return -1;
} }
if (oob_dev->deinit_func() < 0) { oob_dev->deinit_func(oob_priv);
wpa_printf(MSG_ERROR, "WPS: Failed to deinitialize OOB "
"device");
return -1;
}
return 0; return 0;
} }

View file

@ -22,7 +22,9 @@
#include "wps/wps.h" #include "wps/wps.h"
static int ufd_fd = -1; struct wps_ufd_data {
int ufd_fd;
};
static int dev_pwd_e_file_filter(const struct dirent *entry) static int dev_pwd_e_file_filter(const struct dirent *entry)
@ -105,28 +107,33 @@ static int ufd_mkdir(const char *path)
} }
static int init_ufd(struct wps_context *wps, int registrar) static void * init_ufd(struct wps_context *wps, int registrar)
{ {
int write_f; int write_f;
char temp[128]; char temp[128];
char *path = wps->oob_dev->device_path; char *path = wps->oob_dev->device_path;
char filename[13]; char filename[13];
struct wps_ufd_data *data;
int ufd_fd;
if (path == NULL)
return NULL;
write_f = wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ? write_f = wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ?
!registrar : registrar; !registrar : registrar;
if (get_file_name(wps, registrar, filename) < 0) { if (get_file_name(wps, registrar, filename) < 0) {
wpa_printf(MSG_ERROR, "WPS (UFD): Failed to get file name"); wpa_printf(MSG_ERROR, "WPS (UFD): Failed to get file name");
return -1; return NULL;
} }
if (write_f) { if (write_f) {
os_snprintf(temp, sizeof(temp), "%s/SMRTNTKY", path); os_snprintf(temp, sizeof(temp), "%s/SMRTNTKY", path);
if (ufd_mkdir(temp)) if (ufd_mkdir(temp))
return -1; return NULL;
os_snprintf(temp, sizeof(temp), "%s/SMRTNTKY/WFAWSC", path); os_snprintf(temp, sizeof(temp), "%s/SMRTNTKY/WFAWSC", path);
if (ufd_mkdir(temp)) if (ufd_mkdir(temp))
return -1; return NULL;
} }
os_snprintf(temp, sizeof(temp), "%s/SMRTNTKY/WFAWSC/%s", path, os_snprintf(temp, sizeof(temp), "%s/SMRTNTKY/WFAWSC/%s", path,
@ -139,20 +146,25 @@ static int init_ufd(struct wps_context *wps, int registrar)
if (ufd_fd < 0) { if (ufd_fd < 0) {
wpa_printf(MSG_ERROR, "WPS (UFD): Failed to open %s: %s", wpa_printf(MSG_ERROR, "WPS (UFD): Failed to open %s: %s",
temp, strerror(errno)); temp, strerror(errno));
return -1; return NULL;
} }
return 0; data = os_zalloc(sizeof(*data));
if (data == NULL)
return NULL;
data->ufd_fd = ufd_fd;
return data;
} }
static struct wpabuf * read_ufd(void) static struct wpabuf * read_ufd(void *priv)
{ {
struct wps_ufd_data *data = priv;
struct wpabuf *buf; struct wpabuf *buf;
struct stat s; struct stat s;
size_t file_size; size_t file_size;
if (fstat(ufd_fd, &s) < 0) { if (fstat(data->ufd_fd, &s) < 0) {
wpa_printf(MSG_ERROR, "WPS (UFD): Failed to get file size"); wpa_printf(MSG_ERROR, "WPS (UFD): Failed to get file size");
return NULL; return NULL;
} }
@ -165,7 +177,8 @@ static struct wpabuf * read_ufd(void)
return NULL; return NULL;
} }
if (read(ufd_fd, wpabuf_mhead(buf), file_size) != (int) file_size) { if (read(data->ufd_fd, wpabuf_mhead(buf), file_size) !=
(int) file_size) {
wpabuf_free(buf); wpabuf_free(buf);
wpa_printf(MSG_ERROR, "WPS (UFD): Failed to read"); wpa_printf(MSG_ERROR, "WPS (UFD): Failed to read");
return NULL; return NULL;
@ -175,9 +188,11 @@ static struct wpabuf * read_ufd(void)
} }
static int write_ufd(struct wpabuf *buf) static int write_ufd(void *priv, struct wpabuf *buf)
{ {
if (write(ufd_fd, wpabuf_mhead(buf), wpabuf_len(buf)) != struct wps_ufd_data *data = priv;
if (write(data->ufd_fd, wpabuf_mhead(buf), wpabuf_len(buf)) !=
(int) wpabuf_len(buf)) { (int) wpabuf_len(buf)) {
wpa_printf(MSG_ERROR, "WPS (UFD): Failed to write"); wpa_printf(MSG_ERROR, "WPS (UFD): Failed to write");
return -1; return -1;
@ -186,11 +201,11 @@ static int write_ufd(struct wpabuf *buf)
} }
static int deinit_ufd(void) static void deinit_ufd(void *priv)
{ {
close(ufd_fd); struct wps_ufd_data *data = priv;
ufd_fd = -1; close(data->ufd_fd);
return 0; os_free(data);
} }