[PATCH 04/26] utils: Add helper function to get IE by ID

Ilan Peer ilan.peer at intel.com
Mon Feb 15 06:53:20 PST 2016


From: Avraham Stern <avraham.stern at intel.com>

Add helper function to find a certain IE inside IEs buffer by ID.

Use this function in several places that implemented similar
functionality locally.

Signed-off-by: Avraham Stern <avraham.stern at intel.com>
---
 src/common/ieee802_11_common.c    | 33 +++++++++++++++++++++++++++++++++
 src/common/ieee802_11_common.h    |  3 +++
 src/drivers/driver_nl80211_scan.c | 19 ++-----------------
 wpa_supplicant/bss.c              | 15 +--------------
 wpa_supplicant/scan.c             | 15 +--------------
 5 files changed, 40 insertions(+), 45 deletions(-)

diff --git a/src/common/ieee802_11_common.c b/src/common/ieee802_11_common.c
index 8dee883..2d5927c 100644
--- a/src/common/ieee802_11_common.c
+++ b/src/common/ieee802_11_common.c
@@ -1172,3 +1172,36 @@ struct wpabuf * mb_ies_by_info(struct mb_ies_info *info)
 
 	return mb_ies;
 }
+
+
+/**
+ * get_ie - Fetch a specified information element from IEs buffer
+ * @ies: Information elements buffer
+ * @len: Information elements buffer length
+ * @eid: Information element identitifier (WLAN_EID_*)
+ * Returns: Pointer to the information element (id field) or %NULL if not found
+ *
+ * This function returns the first matching information element in the IEs
+ * buffer or NULL in case the eid is not found.
+ */
+const u8 *get_ie(const u8 *ies, size_t len, u8 eid)
+{
+	const u8 *end;
+
+	if (!ies)
+		return NULL;
+
+	end = ies + len;
+
+	while (end - ies > 1) {
+		if (2 + ies[1] > end - ies)
+			break;
+
+		if (ies[0] == eid)
+			return ies;
+
+		ies += 2 + ies[1];
+	}
+
+	return NULL;
+}
diff --git a/src/common/ieee802_11_common.h b/src/common/ieee802_11_common.h
index 55ce022..cfd949e 100644
--- a/src/common/ieee802_11_common.h
+++ b/src/common/ieee802_11_common.h
@@ -125,4 +125,7 @@ int mb_ies_info_by_ies(struct mb_ies_info *info, const u8 *ies_buf,
 struct wpabuf * mb_ies_by_info(struct mb_ies_info *info);
 
 const char * fc2str(u16 fc);
+
+const u8 *get_ie(const u8 *ies, size_t len, u8 eid);
+
 #endif /* IEEE802_11_COMMON_H */
diff --git a/src/drivers/driver_nl80211_scan.c b/src/drivers/driver_nl80211_scan.c
index 2ff254e..b3542d6 100644
--- a/src/drivers/driver_nl80211_scan.c
+++ b/src/drivers/driver_nl80211_scan.c
@@ -17,6 +17,7 @@
 #include "common/ieee802_11_defs.h"
 #include "common/qca-vendor.h"
 #include "driver_nl80211.h"
+#include "common/ieee802_11_common.h"
 
 
 static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
@@ -524,23 +525,7 @@ int wpa_driver_nl80211_stop_sched_scan(void *priv)
 
 const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
 {
-	const u8 *end, *pos;
-
-	if (ies == NULL)
-		return NULL;
-
-	pos = ies;
-	end = ies + ies_len;
-
-	while (end - pos > 1) {
-		if (2 + pos[1] > end - pos)
-			break;
-		if (pos[0] == ie)
-			return pos;
-		pos += 2 + pos[1];
-	}
-
-	return NULL;
+	return get_ie(ies, ies_len, ie);
 }
 
 
diff --git a/wpa_supplicant/bss.c b/wpa_supplicant/bss.c
index dcfa12f..4b1d232 100644
--- a/wpa_supplicant/bss.c
+++ b/wpa_supplicant/bss.c
@@ -1019,20 +1019,7 @@ struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
  */
 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
 {
-	const u8 *end, *pos;
-
-	pos = (const u8 *) (bss + 1);
-	end = pos + bss->ie_len;
-
-	while (end - pos > 1) {
-		if (2 + pos[1] > end - pos)
-			break;
-		if (pos[0] == ie)
-			return pos;
-		pos += 2 + pos[1];
-	}
-
-	return NULL;
+	return get_ie((const u8 *) (bss + 1), bss->ie_len, ie);
 }
 
 
diff --git a/wpa_supplicant/scan.c b/wpa_supplicant/scan.c
index f4f2c20..30bec2c 100644
--- a/wpa_supplicant/scan.c
+++ b/wpa_supplicant/scan.c
@@ -1535,20 +1535,7 @@ static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  */
 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
 {
-	const u8 *end, *pos;
-
-	pos = (const u8 *) (res + 1);
-	end = pos + res->ie_len;
-
-	while (end - pos > 1) {
-		if (2 + pos[1] > end - pos)
-			break;
-		if (pos[0] == ie)
-			return pos;
-		pos += 2 + pos[1];
-	}
-
-	return NULL;
+	return get_ie((const u8 *) (res + 1), res->ie_len, ie);
 }
 
 
-- 
1.9.1




More information about the Hostap mailing list