source: src/router/services/tools/site_survey_madwifi.c @ 9117

Last change on this file since 9117 was 9117, checked in by BrainSlayer, 5 years ago

block multicast per default to avoid confusion with igmp proxy load

File size: 5.8 KB
Line 
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
44static int
45copy_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
93int write_site_survey (void);
94static int open_site_survey (void);
95int write_site_survey (void);
96
97
98struct 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
113static const char *
114ieee80211_ntoa (const uint8_t mac[IEEE80211_ADDR_LEN])
115{
116  static char a[18];
117  int i;
118
119  i = snprintf (a, sizeof (a), "%02x:%02x:%02x:%02x:%02x:%02x",
120                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
121  return (i < 17 ? NULL : a);
122}
123
124int
125site_survey_main (int argc, char *argv[])
126{
127  char *name = nvram_safe_get ("wl0_ifname");
128  unsigned char mac[20];
129  int i = 0;
130  char *dev = name;
131  unlink (SITE_SURVEY_DB);
132  int ap = 0, oldap = 0;
133
134  unsigned char buf[24 * 1024];
135  char ssid[31];
136  unsigned char *cp;
137  int len;
138  char *sta = nvram_safe_get ("wifi_display");
139  eval ("iwlist", sta, "scan");
140  len = do80211priv (sta, IEEE80211_IOCTL_SCAN_RESULTS, buf, sizeof (buf));
141
142  if (len == -1)
143    fprintf (stderr, "unable to get scan results");
144  if (len < sizeof (struct ieee80211req_scan_result))
145    return;
146  cp = buf;
147  do
148    {
149      struct ieee80211req_scan_result *sr;
150      unsigned char *vp;
151      char ssid[14];
152      sr = (struct ieee80211req_scan_result *) cp;
153      vp = (u_int8_t *) (sr + 1);
154      memset (ssid, 0, sizeof (ssid));
155      strncpy (site_survey_lists[i].SSID, vp, sr->isr_ssid_len);
156      strcpy (site_survey_lists[i].BSSID, ieee80211_ntoa (sr->isr_bssid));
157      site_survey_lists[i].channel = ieee80211_mhz2ieee (sr->isr_freq);
158      int noise = 256;
159      noise -= (int) sr->isr_noise;
160      site_survey_lists[i].phy_noise = -noise;
161      if (sr->isr_noise == 0)
162        {
163          site_survey_lists[i].phy_noise = -95;
164        }
165      site_survey_lists[i].RSSI =
166        (int) site_survey_lists[i].phy_noise + (int) sr->isr_rssi;
167      site_survey_lists[i].capability = sr->isr_capinfo;
168//      site_survey_lists[i].athcaps = sr->isr_athflags;
169      site_survey_lists[i].rate_count = sr->isr_nrates;
170      cp += sr->isr_len, len -= sr->isr_len;
171      i++;
172    }
173  while (len >= sizeof (struct ieee80211req_scan_result));
174
175  write_site_survey ();
176  open_site_survey ();
177  for (i = 0;
178       i < SITE_SURVEY_NUM && site_survey_lists[i].BSSID[0]
179       && site_survey_lists[i].channel != 0; i++)
180    {
181      fprintf (stderr,
182               "[%2d] SSID[%20s] BSSID[%s] channel[%2d] rssi[%d] noise[%d] beacon[%d] cap[%x] dtim[%d] rate[%d]\n",
183               i, site_survey_lists[i].SSID, site_survey_lists[i].BSSID,
184               site_survey_lists[i].channel, site_survey_lists[i].RSSI,
185               site_survey_lists[i].phy_noise,
186               site_survey_lists[i].beacon_period,
187               site_survey_lists[i].capability,
188               site_survey_lists[i].dtim_period,
189               site_survey_lists[i].rate_count);
190    }
191
192  return 0;
193}
194
195int
196write_site_survey (void)
197{
198  FILE *fp;
199
200  if ((fp = fopen (SITE_SURVEY_DB, "w")))
201    {
202      fwrite (&site_survey_lists[0], sizeof (site_survey_lists), 1, fp);
203      fclose (fp);
204      return 0;
205    }
206  return 1;
207}
208
209static int
210open_site_survey (void)
211{
212  FILE *fp;
213
214  bzero (site_survey_lists, sizeof (site_survey_lists));
215
216  if ((fp = fopen (SITE_SURVEY_DB, "r")))
217    {
218      fread (&site_survey_lists[0], sizeof (site_survey_lists), 1, fp);
219      fclose (fp);
220      return 1;
221    }
222  return 0;
223}
Note: See TracBrowser for help on using the repository browser.