WPS UFD: Use private data structure for oob_device_data
authorJouni Malinen <j@w1.fi>
Thu, 26 Feb 2009 20:04:49 +0000 (22:04 +0200)
committerJouni Malinen <j@w1.fi>
Thu, 26 Feb 2009 20:04:49 +0000 (22:04 +0200)
This gets rid of global variable use and may be needed to allow
concurrent access in some case if the interface is extended.

src/wps/wps.h
src/wps/wps_common.c
src/wps/wps_ufd.c

index e255e43..2be93f3 100644 (file)
@@ -532,10 +532,10 @@ struct wps_context {
 
 struct oob_device_data {
        char *device_path;
-       int (*init_func)(struct wps_context *, int);
-       struct wpabuf * (*read_func)(void);
-       int (*write_func)(struct wpabuf *);
-       int (*deinit_func)(void);
+       void * (*init_func)(struct wps_context *, int);
+       struct wpabuf * (*read_func)(void *);
+       int (*write_func)(void *, struct wpabuf *);
+       void (*deinit_func)(void *);
 };
 
 struct wps_registrar *
index a2c88ad..622a0a3 100644 (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 wpabuf *data;
        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;
 
-       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");
                return -1;
        }
@@ -488,12 +490,15 @@ int wps_process_oob(struct wps_context *wps, int registrar)
                        data = wps_get_oob_dev_pwd(wps);
 
                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;
        } else {
-               data = oob_dev->read_func();
-               if (data == NULL)
+               data = oob_dev->read_func(oob_priv);
+               if (data == NULL) {
+                       oob_dev->deinit_func(oob_priv);
                        return -1;
+               }
 
                if (oob_method == OOB_METHOD_CRED)
                        ret = wps_parse_oob_cred(wps, data);
@@ -503,14 +508,11 @@ int wps_process_oob(struct wps_context *wps, int registrar)
        wpabuf_free(data);
        if (ret < 0) {
                wpa_printf(MSG_ERROR, "WPS: Failed to process OOB data");
+               oob_dev->deinit_func(oob_priv);
                return -1;
        }
 
-       if (oob_dev->deinit_func() < 0) {
-               wpa_printf(MSG_ERROR, "WPS: Failed to deinitialize OOB "
-                          "device");
-               return -1;
-       }
+       oob_dev->deinit_func(oob_priv);
 
        return 0;
 }
index cec8d90..d6b01ac 100644 (file)
@@ -22,7 +22,9 @@
 
 #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)
@@ -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;
        char temp[128];
        char *path = wps->oob_dev->device_path;
        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 ?
                !registrar : registrar;
 
        if (get_file_name(wps, registrar, filename) < 0) {
                wpa_printf(MSG_ERROR, "WPS (UFD): Failed to get file name");
-               return -1;
+               return NULL;
        }
 
        if (write_f) {
                os_snprintf(temp, sizeof(temp), "%s/SMRTNTKY", path);
                if (ufd_mkdir(temp))
-                       return -1;
+                       return NULL;
                os_snprintf(temp, sizeof(temp), "%s/SMRTNTKY/WFAWSC", path);
                if (ufd_mkdir(temp))
-                       return -1;
+                       return NULL;
        }
 
        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) {
                wpa_printf(MSG_ERROR, "WPS (UFD): Failed to open %s: %s",
                           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 stat s;
        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");
                return NULL;
        }
@@ -165,7 +177,8 @@ static struct wpabuf * read_ufd(void)
                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);
                wpa_printf(MSG_ERROR, "WPS (UFD): Failed to read");
                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)) {
                wpa_printf(MSG_ERROR, "WPS (UFD): Failed to write");
                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);
-       ufd_fd = -1;
-       return 0;
+       struct wps_ufd_data *data = priv;
+       close(data->ufd_fd);
+       os_free(data);
 }