PATCH: send_mgmt_frame in driver.c

Gunter Burchardt gbur
Fri Jun 18 00:17:38 PDT 2004


Hello Jouni

Next patch is ready. Here is a short changelog

 * moved struct hostapd_data->sock to stuct hostap_driver_data->sock
 * new function pointer send_mgmt_frame in struct driver_info
 * wraped all calls to send(hapd->sock, ...) to send_mgmt_frame
 * moved init/deinitialisation of ->sock to driver.c
 * moved call to hostapd_init_sockets() to driver.c
 * moved extern declaration of hostapd_init_sockets from hostapd.h to
   driver.h

We should rename receive.c and driver.c to hostap_receive.c and
hostap_driver.c . I wrote wired_receive.c and wired_driver.c, but at
this stage its no good idea to bring it into hostapd now. Perhaps a
driver directory is a solution to seperate drivers from hostapd.

Next step is to bring successively all functions of driver.c to
function pointers. Perhaps it is possible to make the interface to the
driver more clear. A function pointer for example to hostapd_ioctl() 
makes no sense.

regards
gunter 
-------------- next part --------------
diff -Nur hostap/hostapd/driver.c hostap.n/hostapd/driver.c
--- hostap/hostapd/driver.c	2004-06-18 06:22:05.000000000 +0200
+++ hostap.n/hostapd/driver.c	2004-06-18 07:55:33.000000000 +0200
@@ -38,6 +38,15 @@
 #include "ieee802_11.h"
 
 
+static int hostapd_send_mgmt_frame(void *priv, const void *msg, size_t len,
+				   int flags)
+{
+	struct hostap_driver_data *drv = priv;
+	
+	return send(drv->sock, msg, len, flags);
+}
+
+
 static int hostapd_send_eapol(void *priv, u8 *addr,
 			      u8 *data, size_t data_len, int encrypt)
 {
@@ -73,7 +82,7 @@
 	pos += 2;
 	memcpy(pos, data, data_len);
 
-	res = send(drv->hapd->sock, (u8 *) hdr, len, 0);
+	res = hostapd_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
 	free(hdr);
 
 	if (res < 0) {
@@ -579,7 +588,7 @@
 	hapd->driver.data = drv;
 
 	drv->hapd = hapd;
-	drv->ioctl_sock = -1;
+	drv->ioctl_sock = drv->sock = -1;
 	memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
 
 	drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
@@ -601,9 +610,13 @@
 		return -1;
 	}
 
+    if (hostapd_init_sockets(drv))
+		return -1;
+	
 	/* register callback functions */
 	hapd->driver.set_sta_authorized = hostapd_set_sta_authorized;
 	hapd->driver.send_eapol = hostapd_send_eapol;
+	hapd->driver.send_mgmt_frame = hostapd_send_mgmt_frame;
 
 	return 0;
 }
@@ -615,6 +628,7 @@
 
 	hapd->driver.set_sta_authorized = NULL;
 	hapd->driver.send_eapol = NULL;
+	hapd->driver.send_mgmt_frame = NULL;
 
 	(void) hostapd_set_iface_flags(drv, 0);
 	(void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
@@ -623,5 +637,8 @@
 	if (drv->ioctl_sock >= 0)
 		close(drv->ioctl_sock);
 
+	if (drv->sock >= 0)
+		close(drv->sock);
+	
 	free(hapd->driver.data);
 }
diff -Nur hostap/hostapd/driver.h hostap.n/hostapd/driver.h
--- hostap/hostapd/driver.h	2004-06-17 06:37:40.000000000 +0200
+++ hostap.n/hostapd/driver.h	2004-06-18 07:55:33.000000000 +0200
@@ -9,6 +9,7 @@
 	struct hostapd_data *hapd;
 
 	char iface[IFNAMSIZ + 1];
+	int sock; /* raw packet socket for driver access */
 	int ioctl_sock; /* socket for ioctl() use */
 	int wext_sock; /* socket for wireless events */
 };
@@ -33,4 +34,7 @@
 int hostapd_wireless_event_init(void *priv);
 void hostapd_wireless_event_deinit(void *priv);
 
+/* receive.c */
+int hostapd_init_sockets(struct hostap_driver_data *drv);
+
 #endif /* DRIVER_H */
diff -Nur hostap/hostapd/hostapd.c hostap.n/hostapd/hostapd.c
--- hostap/hostapd/hostapd.c	2004-06-17 06:28:44.000000000 +0200
+++ hostap.n/hostapd/hostapd.c	2004-06-18 07:55:33.000000000 +0200
@@ -349,8 +349,6 @@
 	hostapd_acl_deinit(hapd);
 	radius_client_deinit(hapd);
 
-	if (hapd->sock >= 0)
-		close(hapd->sock);
 	hostapd_wireless_event_deinit(hapd->driver.data);
 
 	hostapd_driver_deinit(hapd);
@@ -414,9 +412,6 @@
 		return -1;
 	}
 
-	if (hostapd_init_sockets(hapd))
-		return -1;
-
 	printf("Using interface %sap with hwaddr " MACSTR " and ssid '%s'\n",
 	       hapd->conf->iface, MAC2STR(hapd->own_addr), hapd->conf->ssid);
 
@@ -499,7 +494,6 @@
 		exit(1);
 	}
 	memset(hapd, 0, sizeof(*hapd));
-	hapd->sock = -1;
 
 	hapd->config_fname = strdup(config_file);
 	if (hapd->config_fname == NULL) {
diff -Nur hostap/hostapd/hostapd.h hostap.n/hostapd/hostapd.h
--- hostap/hostapd/hostapd.h	2004-06-18 06:22:05.000000000 +0200
+++ hostap.n/hostapd/hostapd.h	2004-06-18 07:55:33.000000000 +0200
@@ -49,6 +49,8 @@
 struct driver_info {
 	void *data; /* driver specific data - each driver can store data for
 		     * its own use in this pointer */
+	int (*send_mgmt_frame)(void *priv, const void *msg, size_t len,
+			       int flags);
 	int (*send_eapol)(void *driver, u8 *addr, u8 *data, size_t data_len,
 			  int encrypt);
 	void (*set_sta_authorized)(void *driver, u8 *addr, int authorized);
@@ -58,7 +60,6 @@
 	struct hostapd_config *conf;
 	char *config_fname;
 
-	int sock; /* raw packet socket for driver access */
 	u8 own_addr[6];
 
 	int num_sta; /* number of entries in sta_list */
@@ -123,7 +124,4 @@
 
 #define HOSTAPD_DEBUG_COND(level) (hapd->conf->debug >= (level))
 
-/* receive.c */
-int hostapd_init_sockets(hostapd *hapd);
-
 #endif /* HOSTAPD_H */
diff -Nur hostap/hostapd/ieee802_11.c hostap.n/hostapd/ieee802_11.c
--- hostap/hostapd/ieee802_11.c	2004-06-17 06:45:25.000000000 +0200
+++ hostap.n/hostapd/ieee802_11.c	2004-06-18 07:55:33.000000000 +0200
@@ -182,9 +182,12 @@
 	memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
 	memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
 	mgmt.u.deauth.reason_code = host_to_le16(reason);
-	if (send(hapd->sock, &mgmt, IEEE80211_HDRLEN + sizeof(mgmt.u.deauth),
-		 0) < 0)
-		perror("ieee802_11_send_deauth: send");
+	if (hapd->driver.send_mgmt_frame)
+		if (hapd->driver.send_mgmt_frame(hapd->driver.data, &mgmt, 
+					         IEEE80211_HDRLEN + 
+						 sizeof(mgmt.u.deauth), 0)
+		    < 0)
+			perror("ieee802_11_send_deauth: send");
 }
 
 
@@ -199,9 +202,12 @@
 	memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
 	memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
 	mgmt.u.disassoc.reason_code = host_to_le16(reason);
-	if (send(hapd->sock, &mgmt, IEEE80211_HDRLEN +
-		 sizeof(mgmt.u.disassoc), 0) < 0)
-		perror("ieee802_11_send_disassoc: send");
+	if (hapd->driver.send_mgmt_frame)
+	        if (hapd->driver.send_mgmt_frame(hapd->driver.data, &mgmt, 
+						 IEEE80211_HDRLEN + 
+						sizeof(mgmt.u.disassoc), 0)
+		    < 0)
+			perror("ieee802_11_send_disassoc: send");
 }
 
 
@@ -231,9 +237,11 @@
 	mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
 	mgmt.u.auth.auth_transaction = host_to_le16(1);
 	mgmt.u.auth.status_code = host_to_le16(0);
-	if (send(hapd->sock, &mgmt, IEEE80211_HDRLEN + sizeof(mgmt.u.auth),
-		 0) < 0)
-		perror("ieee802_11_sta_authenticate: send");
+	if (hapd->driver.send_mgmt_frame)
+		if (hapd->driver.send_mgmt_frame(hapd->driver.data, &mgmt, 
+						 IEEE80211_HDRLEN + 
+						 sizeof(mgmt.u.auth), 0) < 0)
+			perror("ieee802_11_sta_authenticate: send");
 
 	/* Try to authenticate again, if this attempt fails or times out. */
 	eloop_register_timeout(5, 0, ieee802_11_sta_authenticate, hapd, NULL);
@@ -276,8 +284,11 @@
 
 	p = hostapd_eid_supp_rates(hapd, p);
 
-	if (send(hapd->sock, mgmt, p - (u8 *) mgmt, 0) < 0)
-		perror("ieee802_11_sta_associate: send");
+	if (hapd->driver.send_mgmt_frame)
+		if (hapd->driver.send_mgmt_frame(hapd->driver.send_mgmt_frame,
+						 mgmt, p - (u8 *) mgmt, 0)
+		    < 0)
+			perror("ieee802_11_sta_associate: send");
 
 	/* Try to authenticate again, if this attempt fails or times out. */
 	eloop_register_timeout(5, 0, ieee802_11_sta_associate, hapd, NULL);
@@ -418,8 +429,10 @@
 		      "auth_transaction=%d resp=%d%s\n",
 		      MAC2STR(mgmt->sa), auth_alg, auth_transaction,
 		      resp, challenge ? " challenge" : "");
-	if (send(hapd->sock, reply, rlen, 0) < 0)
-		perror("send_auth_reply: send");
+	if (hapd->driver.send_mgmt_frame)
+        	if (hapd->driver.send_mgmt_frame(hapd->driver.data, reply,
+						 rlen, 0) < 0)
+			perror("send_auth_reply: send");
 }
 
 
@@ -776,8 +789,10 @@
 		mgmt->frame_control |= host_to_le16(BIT(1));
 	}
 
-	if (send(hapd->sock, mgmt, send_len, 0) < 0)
-		perror("handle_assoc: send");
+	if (hapd->driver.send_mgmt_frame)
+	        if (hapd->driver.send_mgmt_frame(hapd->driver.data, mgmt, 
+						 send_len, 0) < 0)
+			perror("handle_assoc: send");
 }
 
 
diff -Nur hostap/hostapd/receive.c hostap.n/hostapd/receive.c
--- hostap/hostapd/receive.c	2004-06-17 06:28:44.000000000 +0200
+++ hostap.n/hostapd/receive.c	2004-06-18 07:55:34.000000000 +0200
@@ -241,31 +241,32 @@
 }
 
 
-int hostapd_init_sockets(hostapd *hapd)
+int hostapd_init_sockets(struct hostap_driver_data *drv)
 {
-        struct ifreq ifr;
+	struct hostapd_data *hapd = drv->hapd;
+	struct ifreq ifr;
 	struct sockaddr_ll addr;
 
-	hapd->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
-	if (hapd->sock < 0) {
+	drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+	if (drv->sock < 0) {
 		perror("socket[PF_PACKET,SOCK_RAW]");
 		return -1;
 	}
 
-	if (eloop_register_read_sock(hapd->sock, handle_read, hapd, NULL)) {
+	if (eloop_register_read_sock(drv->sock, handle_read, drv->hapd, NULL)) {
 		printf("Could not register read socket\n");
 		return -1;
 	}
 
         memset(&ifr, 0, sizeof(ifr));
         snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap",
-		 hapd->conf->iface);
-        if (ioctl(hapd->sock, SIOCGIFINDEX, &ifr) != 0) {
+		 drv->iface);
+        if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
 		perror("ioctl(SIOCGIFINDEX)");
 		return -1;
         }
 
-	if (hostapd_set_iface_flags(hapd->driver.data, 1)) {
+	if (hostapd_set_iface_flags(drv, 1)) {
 		return -1;
 	}
 
@@ -276,14 +277,14 @@
 		      "Opening raw packet socket for ifindex %d\n",
 		      addr.sll_ifindex);
 
-	if (bind(hapd->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+	if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
 		perror("bind");
 		return -1;
 	}
 
         memset(&ifr, 0, sizeof(ifr));
-        snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", hapd->conf->iface);
-        if (ioctl(hapd->sock, SIOCGIFHWADDR, &ifr) != 0) {
+        snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", drv->iface);
+        if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
 		perror("ioctl(SIOCGIFHWADDR)");
 		return -1;
         }
@@ -293,7 +294,7 @@
 		       ifr.ifr_hwaddr.sa_family);
 		return -1;
 	}
-	memcpy(hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
+	memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
 
 	return 0;
 }
diff -Nur hostap/hostapd/sta_info.c hostap.n/hostapd/sta_info.c
--- hostap/hostapd/sta_info.c	2004-06-17 06:37:40.000000000 +0200
+++ hostap.n/hostapd/sta_info.c	2004-06-18 07:55:34.000000000 +0200
@@ -206,8 +206,10 @@
 		memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr, ETH_ALEN);
 		memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
 
-		if (send(hapd->sock, &hdr, sizeof(hdr), 0) < 0)
-			perror("ap_handle_timer: send");
+		if (hapd->driver.send_mgmt_frame)
+			if (hapd->driver.send_mgmt_frame(hapd->driver.data, 
+			    &hdr, sizeof(hdr), 0) < 0)
+				perror("ap_handle_timer: send");
 	} else {
 		int deauth = sta->timeout_next == STA_DEAUTH;
 



More information about the Hostap mailing list