summaryrefslogtreecommitdiffstats
path: root/src/util.c
blob: 9b6061f5a8887614ffa7a30ca330c0fb9d2b130c (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* $Id$ */

/*
 * This file is part of waproamd.
 *
 * waproamd is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * waproamd is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with waproamd; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include <unistd.h>
#include <sys/socket.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <libdaemon/dlog.h>

#include "util.h"

struct hw_addr null_ap = { { 0, 0, 0, 0, 0, 0 } };

void print_hex(FILE *f, uint8_t *w, int l) {
    while (l > 0) {
        fprintf(f, "%02x", *(w++));
        l--;
    }
}

int hw_addr_equal(struct hw_addr *a, struct hw_addr *b) {
    return memcmp(a->addr, b->addr, ETH_ALEN) == 0;
}

int is_assoc_ap(struct hw_addr *ap) {
    int b, j;
    b = 1;
    assert(ap);
    
    for (j = 1; j < ETH_ALEN; j++)
        if (ap->addr[j] != ap->addr[0]) {
            b = 0;
            break;
        }

    return !b || (ap->addr[0] != 0xFF && ap->addr[0] != 0x44 && ap->addr[0] != 0x00);
}


void print_hw_addr(FILE*f, struct hw_addr *a) {
    fprintf(f, "%02x:%02x:%02x:%02x:%02x:%02x",
           a->addr[0], a->addr[1], a->addr[2],
           a->addr[3], a->addr[4], a->addr[5]);
}

void snprint_hw_addr(char *c, int l, struct hw_addr *a) {
    snprintf(c, l, "%02x:%02x:%02x:%02x:%02x:%02x",
             a->addr[0], a->addr[1], a->addr[2],
             a->addr[3], a->addr[4], a->addr[5]);
}


int parse_hex(char *s, uint8_t *b, int l) {
    int n = 0;
    char *p;
    int nibble = 0;
    
    for (p = s; *p && l >= 0; p++) {
        int c;

        if (*p >= '0' && *p <= '9')
            c = *p - '0';
        else if (*p >= 'A' && *p <= 'F')
            c = *p - 'A' + 10;
        else if (*p >= 'a' && *p <= 'f')
            c = *p - 'a' + 10;
        else if (*p == ':' || *p == '.')
            continue;
        else
            return -(p-s)-1;

        if (!nibble)
            *b = c << 4;
        else {
            *b |= c & 15;
            b++;
            l--;
            n++;
        }

        nibble = !nibble;
    }

    if (*p)
        return -(p-s)-1;
    
    return n;
}


int get_ifname(int idx, char *p, int l) {
    struct ifreq req;
    int s;

    if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
        daemon_log(LOG_ERR, "socket() failed: %s\n", strerror(errno));
        return -1;
    }
    
    memset(&req, 0, sizeof(req));
    req.ifr_ifindex = idx;
    
    if(ioctl(s, SIOCGIFNAME, &req) < 0) {
        close(s);
        daemon_log(LOG_ERR,  "SIOCGIFNAME failed: %s\n", strerror(errno));
        return -1;
    }

    close(s);

    strncpy(p, req.ifr_name, l-1);
    p[l-1] = 0;

    return 0;
}

int is_iface_available(char *p) {
    struct ifreq req;
    int s, r;

    if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
        daemon_log(LOG_ERR, "socket() failed: %s\n", strerror(errno));
        return -1;
    }
    
    memset(&req, 0, sizeof(req));
    strncpy(req.ifr_name, p, IFNAMSIZ);
    
    if ((r = ioctl(s, SIOCGIFINDEX, &req)) < 0 && errno != ENODEV) {
        daemon_log(LOG_ERR, "SIOCGIFINDEX failed: %s\n", strerror(errno));
        return -1;
    }

    close(s);
    return r >= 0 && req.ifr_ifindex >= 0;
}