summaryrefslogtreecommitdiffstats
path: root/src/iwkey.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/iwkey.c')
-rw-r--r--src/iwkey.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/iwkey.c b/src/iwkey.c
new file mode 100644
index 0000000..3875594
--- /dev/null
+++ b/src/iwkey.c
@@ -0,0 +1,87 @@
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+
+#include "iwkey.h"
+#include "wireless.h"
+#include "util.h"
+
+static int n_used_keys = 0;
+
+int wep_key_add(struct interface *i, uint8_t w[WEP_KEY_LEN]) {
+ struct iwreq req;
+ assert(i);
+
+ if (n_used_keys >= n_max_keys) {
+ fprintf(stderr, "Too many keys added!\n");
+ return -1;
+ }
+
+ memset(&req, 0, sizeof(req));
+ strncpy(req.ifr_ifrn.ifrn_name, i->name, IFNAMSIZ);
+
+ req.u.encoding.pointer = w;
+ req.u.encoding.length = WEP_KEY_LEN;
+ req.u.encoding.flags = key_map[n_used_keys++]+1;
+
+ if (ioctl(i->fd, SIOCSIWENCODE, &req) < 0) {
+ fprintf(stderr, "ioctl(SIOCSIWENCODE): %s\n", strerror(errno));
+ return -1;
+ }
+
+ return 0;
+}
+
+int wep_key_finish(struct interface *i) {
+ struct iwreq req;
+ assert(i);
+
+ if (n_used_keys) {
+ uint8_t tmp[WEP_KEY_LEN];
+ int j;
+
+ memset(&req, 0, sizeof(req));
+ strncpy(req.ifr_ifrn.ifrn_name, i->name, IFNAMSIZ);
+
+ req.u.encoding.pointer = tmp;
+ req.u.encoding.length = WEP_KEY_LEN;
+ req.u.encoding.flags = (key_map[n_used_keys-1]+1);
+
+ if (ioctl(i->fd, SIOCGIWENCODE, &req) < 0) {
+ fprintf(stderr, "ioctl(SIOCGIWENCODE): %s\n", strerror(errno));
+ return -1;
+ }
+
+ for (j = n_used_keys; j < n_max_keys; j++) {
+ memset(&req, 0, sizeof(req));
+ strncpy(req.ifr_ifrn.ifrn_name, i->name, IFNAMSIZ);
+
+ req.u.encoding.pointer = tmp;
+ req.u.encoding.length = WEP_KEY_LEN;
+ req.u.encoding.flags = (key_map[j]+1);
+
+ if (ioctl(i->fd, SIOCSIWENCODE, &req) < 0) {
+ fprintf(stderr, "ioctl(SIOCSIWENCODE): %s\n", strerror(errno));
+ return -1;
+ }
+ }
+ }
+
+ memset(&req, 0, sizeof(req));
+ strncpy(req.ifr_ifrn.ifrn_name, i->name, IFNAMSIZ);
+
+ req.u.encoding.pointer = NULL;
+ req.u.encoding.length = 0;
+ req.u.encoding.flags = IW_ENCODE_RESTRICTED;
+
+ if (ioctl(i->fd, SIOCSIWENCODE, &req) < 0) {
+ fprintf(stderr, "ioctl(SIOCSIWENCODE): %s\n", strerror(errno));
+ return -1;
+ }
+
+ n_used_keys = 0;
+
+ return 0;
+}