| 1 | /* |
|---|
| 2 | * site_survey_madwifi.c |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2006 Sebastian Gottschall <gottschall@dd-wrt.com> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or |
|---|
| 7 | * modify it under the terms of the GNU General Public License |
|---|
| 8 | * as published by the Free Software Foundation; either version 2 |
|---|
| 9 | * of the License, or (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | * |
|---|
| 20 | * $Id: |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <sys/types.h> |
|---|
| 24 | #include <sys/file.h> |
|---|
| 25 | #include <sys/ioctl.h> |
|---|
| 26 | #include <sys/socket.h> |
|---|
| 27 | |
|---|
| 28 | #include <unistd.h> |
|---|
| 29 | #include <stdio.h> |
|---|
| 30 | #include <stdlib.h> |
|---|
| 31 | #include <string.h> |
|---|
| 32 | #include <stdint.h> |
|---|
| 33 | #include <ctype.h> |
|---|
| 34 | #include <getopt.h> |
|---|
| 35 | #include <err.h> |
|---|
| 36 | #include <shutils.h> |
|---|
| 37 | |
|---|
| 38 | #include "wireless_copy.h" |
|---|
| 39 | #include "net80211/ieee80211.h" |
|---|
| 40 | #include "net80211/ieee80211_crypto.h" |
|---|
| 41 | #include "net80211/ieee80211_ioctl.h" |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | static int |
|---|
| 45 | copy_essid (char buf[], size_t bufsize, const u_int8_t * essid, |
|---|
| 46 | size_t essid_len) |
|---|
| 47 | { |
|---|
| 48 | const u_int8_t *p; |
|---|
| 49 | int maxlen; |
|---|
| 50 | int i; |
|---|
| 51 | |
|---|
| 52 | if (essid_len > bufsize) |
|---|
| 53 | maxlen = bufsize; |
|---|
| 54 | else |
|---|
| 55 | maxlen = essid_len; |
|---|
| 56 | /* determine printable or not */ |
|---|
| 57 | for (i = 0, p = essid; i < maxlen; i++, p++) |
|---|
| 58 | { |
|---|
| 59 | if (*p < ' ' || *p > 0x7e) |
|---|
| 60 | break; |
|---|
| 61 | } |
|---|
| 62 | if (i != maxlen) |
|---|
| 63 | { /* not printable, print as hex */ |
|---|
| 64 | if (bufsize < 3) |
|---|
| 65 | return 0; |
|---|
| 66 | #if 0 |
|---|
| 67 | strlcpy (buf, "0x", bufsize); |
|---|
| 68 | #else |
|---|
| 69 | strncpy (buf, "0x", bufsize); |
|---|
| 70 | #endif |
|---|
| 71 | bufsize -= 2; |
|---|
| 72 | p = essid; |
|---|
| 73 | for (i = 0; i < maxlen && bufsize >= 2; i++) |
|---|
| 74 | { |
|---|
| 75 | sprintf (&buf[2 + 2 * i], "%02x", *p++); |
|---|
| 76 | bufsize -= 2; |
|---|
| 77 | } |
|---|
| 78 | maxlen = 2 + 2 * i; |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | { /* printable, truncate as needed */ |
|---|
| 82 | memcpy (buf, essid, maxlen); |
|---|
| 83 | } |
|---|
| 84 | if (maxlen != essid_len) |
|---|
| 85 | memcpy (buf + maxlen - 3, "...", 3); |
|---|
| 86 | return maxlen; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | #define sys_restart() kill(1, SIGHUP) |
|---|
| 90 | #define SITE_SURVEY_DB "/tmp/site_survey" |
|---|
| 91 | #define SITE_SURVEY_NUM 50 |
|---|
| 92 | |
|---|
| 93 | int write_site_survey (void); |
|---|
| 94 | static int open_site_survey (void); |
|---|
| 95 | int write_site_survey (void); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | struct site_survey_list |
|---|
| 99 | { |
|---|
| 100 | unsigned char SSID[33]; |
|---|
| 101 | unsigned char BSSID[18]; |
|---|
| 102 | unsigned char channel; /* Channel no. */ |
|---|
| 103 | short RSSI; /* receive signal strength (in dBm) */ |
|---|
| 104 | short phy_noise; /* noise (in dBm) */ |
|---|
| 105 | unsigned short beacon_period; /* units are Kusec */ |
|---|
| 106 | unsigned short capability; /* Capability information */ |
|---|
| 107 | unsigned char ENCINFO[32]; /* encryption info */ |
|---|
| 108 | int rate_count; /* # rates in this set */ |
|---|
| 109 | unsigned char dtim_period; /* DTIM period */ |
|---|
| 110 | } site_survey_lists[SITE_SURVEY_NUM]; |
|---|
| 111 | |
|---|
| 112 | static const char * |
|---|
| 113 | ieee80211_ntoa (const uint8_t mac[IEEE80211_ADDR_LEN]) |
|---|
| 114 | { |
|---|
| 115 | static char a[18]; |
|---|
| 116 | int i; |
|---|
| 117 | |
|---|
| 118 | i = snprintf (a, sizeof (a), "%02x:%02x:%02x:%02x:%02x:%02x", |
|---|
| 119 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
|---|
| 120 | return (i < 17 ? NULL : a); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | int |
|---|
| 124 | site_survey_main (int argc, char *argv[]) |
|---|
| 125 | { |
|---|
| 126 | char *name = nvram_safe_get ("wl0_ifname"); |
|---|
| 127 | unsigned char mac[20]; |
|---|
| 128 | int i = 0; |
|---|
| 129 | char *dev = name; |
|---|
| 130 | unlink (SITE_SURVEY_DB); |
|---|
| 131 | int ap = 0, oldap = 0; |
|---|
| 132 | |
|---|
| 133 | unsigned char buf[24 * 1024]; |
|---|
| 134 | char ssid[31]; |
|---|
| 135 | unsigned char *cp; |
|---|
| 136 | int len; |
|---|
| 137 | char *sta = nvram_safe_get ("wifi_display"); |
|---|
| 138 | eval ("iwlist", sta, "scan"); |
|---|
| 139 | len = do80211priv (sta, IEEE80211_IOCTL_SCAN_RESULTS, buf, sizeof (buf)); |
|---|
| 140 | |
|---|
| 141 | if (len == -1) |
|---|
| 142 | fprintf (stderr, "unable to get scan results"); |
|---|
| 143 | if (len < sizeof (struct ieee80211req_scan_result)) |
|---|
| 144 | return; |
|---|
| 145 | cp = buf; |
|---|
| 146 | do |
|---|
| 147 | { |
|---|
| 148 | struct ieee80211req_scan_result *sr; |
|---|
| 149 | unsigned char *vp; |
|---|
| 150 | char ssid[14]; |
|---|
| 151 | sr = (struct ieee80211req_scan_result *) cp; |
|---|
| 152 | vp = (u_int8_t *) (sr + 1); |
|---|
| 153 | memset (ssid, 0, sizeof (ssid)); |
|---|
| 154 | strncpy (site_survey_lists[i].SSID, vp, sr->isr_ssid_len); |
|---|
| 155 | strcpy (site_survey_lists[i].BSSID, ieee80211_ntoa (sr->isr_bssid)); |
|---|
| 156 | site_survey_lists[i].channel = ieee80211_mhz2ieee (sr->isr_freq); |
|---|
| 157 | int noise = 256; |
|---|
| 158 | noise -= (int) sr->isr_noise; |
|---|
| 159 | site_survey_lists[i].phy_noise = -noise; |
|---|
| 160 | if (sr->isr_noise == 0) |
|---|
| 161 | { |
|---|
| 162 | site_survey_lists[i].phy_noise = -95; |
|---|
| 163 | } |
|---|
| 164 | site_survey_lists[i].RSSI = |
|---|
| 165 | (int) site_survey_lists[i].phy_noise + (int) sr->isr_rssi; |
|---|
| 166 | site_survey_lists[i].capability = sr->isr_capinfo; |
|---|
| 167 | site_survey_lists[i].rate_count = sr->isr_nrates; |
|---|
| 168 | cp += sr->isr_len, len -= sr->isr_len; |
|---|
| 169 | i++; |
|---|
| 170 | } |
|---|
| 171 | while (len >= sizeof (struct ieee80211req_scan_result)); |
|---|
| 172 | |
|---|
| 173 | write_site_survey (); |
|---|
| 174 | open_site_survey (); |
|---|
| 175 | for (i = 0; |
|---|
| 176 | i < SITE_SURVEY_NUM && site_survey_lists[i].BSSID[0] |
|---|
| 177 | && site_survey_lists[i].channel != 0; i++) |
|---|
| 178 | { |
|---|
| 179 | fprintf (stderr, |
|---|
| 180 | "[%2d] SSID[%20s] BSSID[%s] channel[%2d] rssi[%d] noise[%d] beacon[%d] cap[%x] dtim[%d] rate[%d]\n", |
|---|
| 181 | i, site_survey_lists[i].SSID, site_survey_lists[i].BSSID, |
|---|
| 182 | site_survey_lists[i].channel, site_survey_lists[i].RSSI, |
|---|
| 183 | site_survey_lists[i].phy_noise, |
|---|
| 184 | site_survey_lists[i].beacon_period, |
|---|
| 185 | site_survey_lists[i].capability, |
|---|
| 186 | site_survey_lists[i].dtim_period, |
|---|
| 187 | site_survey_lists[i].rate_count); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | return 0; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | int |
|---|
| 194 | write_site_survey (void) |
|---|
| 195 | { |
|---|
| 196 | FILE *fp; |
|---|
| 197 | |
|---|
| 198 | if ((fp = fopen (SITE_SURVEY_DB, "w"))) |
|---|
| 199 | { |
|---|
| 200 | fwrite (&site_survey_lists[0], sizeof (site_survey_lists), 1, fp); |
|---|
| 201 | fclose (fp); |
|---|
| 202 | return 0; |
|---|
| 203 | } |
|---|
| 204 | return 1; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | static int |
|---|
| 208 | open_site_survey (void) |
|---|
| 209 | { |
|---|
| 210 | FILE *fp; |
|---|
| 211 | |
|---|
| 212 | bzero (site_survey_lists, sizeof (site_survey_lists)); |
|---|
| 213 | |
|---|
| 214 | if ((fp = fopen (SITE_SURVEY_DB, "r"))) |
|---|
| 215 | { |
|---|
| 216 | fread (&site_survey_lists[0], sizeof (site_survey_lists), 1, fp); |
|---|
| 217 | fclose (fp); |
|---|
| 218 | return 1; |
|---|
| 219 | } |
|---|
| 220 | return 0; |
|---|
| 221 | } |
|---|