From 206e1f422f42e5dec7cfaa27fd7c098703143c0b Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Thu, 28 Apr 2011 16:14:35 +0300 Subject: [PATCH] P2P: Provide mechanism for figuring out p2p_scan_ie() buffer need The new function, p2p_scan_ie_buf_len(), can be used to figure out how large a buffer needs to be allocated for p2p_scan_ie() use. This makes it easier to add new data into the buffer without forcing all callers to be updated to use a larger buffer. --- src/drivers/driver_test.c | 4 +++- src/p2p/p2p.c | 6 ++++++ src/p2p/p2p.h | 7 +++++++ wpa_supplicant/p2p_supplicant.c | 8 ++++++-- wpa_supplicant/scan.c | 3 ++- 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/drivers/driver_test.c b/src/drivers/driver_test.c index cdae739e4..a34cc5469 100644 --- a/src/drivers/driver_test.c +++ b/src/drivers/driver_test.c @@ -2983,6 +2983,7 @@ static int test_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, int ret; struct wpabuf *wps_ie, *ies; int social_channels[] = { 2412, 2437, 2462, 0, 0 }; + size_t ielen; wpa_printf(MSG_DEBUG, "%s(type=%d freq=%d)", __func__, type, freq); @@ -3004,7 +3005,8 @@ static int test_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, if (wps_ie == NULL) return -1; - ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100); + ielen = p2p_scan_ie_buf_len(drv->p2p); + ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); if (ies == NULL) { wpabuf_free(wps_ie); return -1; diff --git a/src/p2p/p2p.c b/src/p2p/p2p.c index 816196af2..3cb8f4dc7 100644 --- a/src/p2p/p2p.c +++ b/src/p2p/p2p.c @@ -2462,6 +2462,12 @@ void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies) } +size_t p2p_scan_ie_buf_len(struct p2p_data *p2p) +{ + return 100; +} + + int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end) { return p2p_attr_text(p2p_ie, buf, end); diff --git a/src/p2p/p2p.h b/src/p2p/p2p.h index 4cab3da9a..31e83d5ff 100644 --- a/src/p2p/p2p.h +++ b/src/p2p/p2p.h @@ -1343,6 +1343,13 @@ int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf, */ void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies); +/** + * p2p_scan_ie_buf_len - Get maximum buffer length needed for p2p_scan_ie + * @p2p: P2P module context from p2p_init() + * Returns: Number of octets that p2p_scan_ie() may add to the buffer + */ +size_t p2p_scan_ie_buf_len(struct p2p_data *p2p); + /** * p2p_go_params - Generate random P2P group parameters * @p2p: P2P module context from p2p_init() diff --git a/wpa_supplicant/p2p_supplicant.c b/wpa_supplicant/p2p_supplicant.c index f1912f3e9..7d3bac181 100644 --- a/wpa_supplicant/p2p_supplicant.c +++ b/wpa_supplicant/p2p_supplicant.c @@ -92,6 +92,7 @@ static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, int ret; struct wpabuf *wps_ie, *ies; int social_channels[] = { 2412, 2437, 2462, 0, 0 }; + size_t ielen; if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) return -1; @@ -110,7 +111,8 @@ static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, if (wps_ie == NULL) return -1; - ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100); + ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); + ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); if (ies == NULL) { wpabuf_free(wps_ie); return -1; @@ -2766,6 +2768,7 @@ static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) int ret; struct wpa_driver_scan_params params; struct wpabuf *wps_ie, *ies; + size_t ielen; os_memset(¶ms, 0, sizeof(params)); @@ -2782,7 +2785,8 @@ static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) return; } - ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100); + ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); + ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); if (ies == NULL) { wpabuf_free(wps_ie); wpas_p2p_scan_res_join(wpa_s, NULL); diff --git a/wpa_supplicant/scan.c b/wpa_supplicant/scan.c index 5be661a2e..9159d31b9 100644 --- a/wpa_supplicant/scan.c +++ b/wpa_supplicant/scan.c @@ -471,7 +471,8 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx) #ifdef CONFIG_P2P if (wps_ie) { - if (wpabuf_resize(&wps_ie, 100) == 0) { + size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); + if (wpabuf_resize(&wps_ie, ielen) == 0) { wpas_p2p_scan_ie(wpa_s, wps_ie); params.extra_ies = wpabuf_head(wps_ie); params.extra_ies_len = wpabuf_len(wps_ie);