| 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 | static int |
|---|
| 44 | copy_essid( char buf[], size_t bufsize, const u_int8_t * essid, |
|---|
| 45 | size_t essid_len ) |
|---|
| 46 | { |
|---|
| 47 | const u_int8_t *p; |
|---|
| 48 | int maxlen; |
|---|
| 49 | int i; |
|---|
| 50 | |
|---|
| 51 | if( essid_len > bufsize ) |
|---|
| 52 | maxlen = bufsize; |
|---|
| 53 | else |
|---|
| 54 | maxlen = essid_len; |
|---|
| 55 | /* |
|---|
| 56 | * determine printable or not |
|---|
| 57 | */ |
|---|
| 58 | for( i = 0, p = essid; i < maxlen; i++, p++ ) |
|---|
| 59 | { |
|---|
| 60 | if( *p < ' ' || *p > 0x7e ) |
|---|
| 61 | break; |
|---|
| 62 | } |
|---|
| 63 | if( i != maxlen ) |
|---|
| 64 | { /* not printable, print as hex */ |
|---|
| 65 | if( bufsize < 3 ) |
|---|
| 66 | return 0; |
|---|
| 67 | #if 0 |
|---|
| 68 | strlcpy( buf, "0x", bufsize ); |
|---|
| 69 | #else |
|---|
| 70 | strncpy( buf, "0x", bufsize ); |
|---|
| 71 | #endif |
|---|
| 72 | bufsize -= 2; |
|---|
| 73 | p = essid; |
|---|
| 74 | for( i = 0; i < maxlen && bufsize >= 2; i++ ) |
|---|
| 75 | { |
|---|
| 76 | sprintf( &buf[2 + 2 * i], "%02x", *p++ ); |
|---|
| 77 | bufsize -= 2; |
|---|
| 78 | } |
|---|
| 79 | maxlen = 2 + 2 * i; |
|---|
| 80 | } |
|---|
| 81 | else |
|---|
| 82 | { /* printable, truncate as needed */ |
|---|
| 83 | memcpy( buf, essid, maxlen ); |
|---|
| 84 | } |
|---|
| 85 | if( maxlen != essid_len ) |
|---|
| 86 | memcpy( buf + maxlen - 3, "...", 3 ); |
|---|
| 87 | return maxlen; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | #define sys_restart() kill(1, SIGHUP) |
|---|
| 91 | #define SITE_SURVEY_DB "/tmp/site_survey" |
|---|
| 92 | #define SITE_SURVEY_NUM 256 |
|---|
| 93 | |
|---|
| 94 | int write_site_survey( void ); |
|---|
| 95 | static int open_site_survey( void ); |
|---|
| 96 | int write_site_survey( void ); |
|---|
| 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 athcaps; |
|---|
| 108 | unsigned char ENCINFO[32]; /* encryption info */ |
|---|
| 109 | int rate_count; /* # rates in this set */ |
|---|
| 110 | unsigned char dtim_period; /* DTIM period */ |
|---|
| 111 | } site_survey_lists[SITE_SURVEY_NUM]; |
|---|
| 112 | |
|---|
| 113 | static const char *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 site_survey_main( int argc, char *argv[] ) |
|---|
| 124 | { |
|---|
| 125 | char *name = nvram_safe_get( "wl0_ifname" ); |
|---|
| 126 | unsigned char mac[20]; |
|---|
| 127 | int i = 0; |
|---|
| 128 | char *dev = name; |
|---|
| 129 | |
|---|
| 130 | unlink( SITE_SURVEY_DB ); |
|---|
| 131 | int ap = 0, oldap = 0; |
|---|
| 132 | |
|---|
| 133 | unsigned char buf[sizeof( struct ieee80211req_scan_result )*256]; |
|---|
| 134 | char ssid[31]; |
|---|
| 135 | unsigned char *cp; |
|---|
| 136 | int len; |
|---|
| 137 | char *sta = nvram_safe_get( "wifi_display" ); |
|---|
| 138 | |
|---|
| 139 | eval( "iwlist", sta, "scan" ); |
|---|
| 140 | len = |
|---|
| 141 | do80211priv( sta, IEEE80211_IOCTL_SCAN_RESULTS, buf, sizeof( buf ) ); |
|---|
| 142 | |
|---|
| 143 | if( len == -1 ) |
|---|
| 144 | fprintf( stderr, "unable to get scan results" ); |
|---|
| 145 | if( len < sizeof( struct ieee80211req_scan_result ) ) |
|---|
| 146 | return; |
|---|
| 147 | cp = buf; |
|---|
| 148 | do |
|---|
| 149 | { |
|---|
| 150 | struct ieee80211req_scan_result *sr; |
|---|
| 151 | unsigned char *vp; |
|---|
| 152 | char ssid[14]; |
|---|
| 153 | |
|---|
| 154 | sr = ( struct ieee80211req_scan_result * )cp; |
|---|
| 155 | vp = ( u_int8_t * ) ( sr + 1 ); |
|---|
| 156 | memset( ssid, 0, sizeof( ssid ) ); |
|---|
| 157 | strncpy( site_survey_lists[i].SSID, vp, sr->isr_ssid_len ); |
|---|
| 158 | strcpy( site_survey_lists[i].BSSID, ieee80211_ntoa( sr->isr_bssid ) ); |
|---|
| 159 | site_survey_lists[i].channel = ieee80211_mhz2ieee( sr->isr_freq ); |
|---|
| 160 | int noise = 256; |
|---|
| 161 | |
|---|
| 162 | noise -= ( int )sr->isr_noise; |
|---|
| 163 | site_survey_lists[i].phy_noise = -noise; |
|---|
| 164 | if( sr->isr_noise == 0 ) |
|---|
| 165 | { |
|---|
| 166 | site_survey_lists[i].phy_noise = -95; |
|---|
| 167 | } |
|---|
| 168 | site_survey_lists[i].RSSI = |
|---|
| 169 | ( int )site_survey_lists[i].phy_noise + ( int )sr->isr_rssi; |
|---|
| 170 | site_survey_lists[i].capability = sr->isr_capinfo; |
|---|
| 171 | // site_survey_lists[i].athcaps = sr->isr_athflags; |
|---|
| 172 | site_survey_lists[i].rate_count = sr->isr_nrates; |
|---|
| 173 | cp += sr->isr_len, len -= sr->isr_len; |
|---|
| 174 | i++; |
|---|
| 175 | } |
|---|
| 176 | while( len >= sizeof( struct ieee80211req_scan_result ) ); |
|---|
| 177 | |
|---|
| 178 | write_site_survey( ); |
|---|
| 179 | open_site_survey( ); |
|---|
| 180 | for( i = 0; |
|---|
| 181 | i < SITE_SURVEY_NUM && site_survey_lists[i].BSSID[0] |
|---|
| 182 | && site_survey_lists[i].channel != 0; i++ ) |
|---|
| 183 | { |
|---|
| 184 | fprintf( stderr, |
|---|
| 185 | "[%2d] SSID[%20s] BSSID[%s] channel[%2d] rssi[%d] noise[%d] beacon[%d] cap[%x] dtim[%d] rate[%d]\n", |
|---|
| 186 | i, site_survey_lists[i].SSID, site_survey_lists[i].BSSID, |
|---|
| 187 | site_survey_lists[i].channel, site_survey_lists[i].RSSI, |
|---|
| 188 | site_survey_lists[i].phy_noise, |
|---|
| 189 | site_survey_lists[i].beacon_period, |
|---|
| 190 | site_survey_lists[i].capability, |
|---|
| 191 | site_survey_lists[i].dtim_period, |
|---|
| 192 | site_survey_lists[i].rate_count ); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | return 0; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | int write_site_survey( void ) |
|---|
| 199 | { |
|---|
| 200 | FILE *fp; |
|---|
| 201 | |
|---|
| 202 | if( ( fp = fopen( SITE_SURVEY_DB, "w" ) ) ) |
|---|
| 203 | { |
|---|
| 204 | fwrite( &site_survey_lists[0], sizeof( site_survey_lists ), 1, fp ); |
|---|
| 205 | fclose( fp ); |
|---|
| 206 | return 0; |
|---|
| 207 | } |
|---|
| 208 | return 1; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | static int open_site_survey( void ) |
|---|
| 212 | { |
|---|
| 213 | FILE *fp; |
|---|
| 214 | |
|---|
| 215 | bzero( site_survey_lists, sizeof( site_survey_lists ) ); |
|---|
| 216 | |
|---|
| 217 | if( ( fp = fopen( SITE_SURVEY_DB, "r" ) ) ) |
|---|
| 218 | { |
|---|
| 219 | fread( &site_survey_lists[0], sizeof( site_survey_lists ), 1, fp ); |
|---|
| 220 | fclose( fp ); |
|---|
| 221 | return 1; |
|---|
| 222 | } |
|---|
| 223 | return 0; |
|---|
| 224 | } |
|---|