summaryrefslogtreecommitdiffstats
path: root/src/iwkey.c
blob: 387559492dad47729dfd6b07afed95905e1f9885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
}