From 987960ea03c59a2ef9164e4f7309031ef4e0242d Mon Sep 17 00:00:00 2001 Message-Id: <987960ea03c59a2ef9164e4f7309031ef4e0242d.1410189314.git.daniel@totalueberwachung.de> From: Daniel Willmann Date: Mon, 8 Sep 2014 15:53:54 +0200 Subject: [PATCH 1/1] Add wifi2udp program --- Makefile | 4 +- wifi2udp.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 wifi2udp.c Index: wireless_tools.30/Makefile =================================================================== --- wireless_tools.30.orig/Makefile +++ wireless_tools.30/Makefile @@ -50,7 +50,7 @@ WEXT_HEADER = wireless.$(WE_VERSION).h # Targets to build STATIC=libiw.a DYNAMIC=libiw.so.$(WT_VERSION) -PROGS= iwconfig iwlist iwpriv iwspy iwgetid iwevent ifrename +PROGS= iwconfig iwlist iwpriv iwspy iwgetid iwevent ifrename wifi2udp MANPAGES8=iwconfig.8 iwlist.8 iwpriv.8 iwspy.8 iwgetid.8 iwevent.8 ifrename.8 MANPAGES7=wireless.7 MANPAGES5=iftab.5 @@ -105,7 +105,7 @@ endif # Other flags CFLAGS=-Os -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Wshadow \ - -Wpointer-arith -Wcast-qual -Winline -I. + -Wpointer-arith -Wcast-qual -Winline -I. -ggdb3 #CFLAGS=-O2 -W -Wall -Wstrict-prototypes -I. DEPFLAGS=-MMD XCFLAGS=$(CFLAGS) $(DEPFLAGS) $(WARN) $(HEADERS) $(WELIB_FLAG) $(WEDEF_FLAG) @@ -137,6 +137,8 @@ ifrename: ifrename.o $(IWLIB) macaddr: macaddr.o $(IWLIB) +wifi2udp: wifi2udp.o $(IWLIB) + iwmulticall: iwmulticall.o $(CC) $(LDFLAGS) $(STRIPFLAGS) $(XCFLAGS) -o $@ $^ $(LIBS) Index: wireless_tools.30/wifi2udp.c =================================================================== --- /dev/null +++ wireless_tools.30/wifi2udp.c @@ -0,0 +1,149 @@ + +#include +#include +#include +#include + +#include + +static int verbose = 0; +static int delay = 10; + +static void escape_essid(const char *src, char *dst, ssize_t len) +{ + int i, j; + + for (i = 0, j = 0; j < len; i++, j++) { + if (src[i] == '"') { + dst[j] = '\\'; + j++; + if (j == len) + break; + } + + dst[j] = src[i]; + + if (!src[i]) + return; + } +} + +static void scan_wifi(char *iface, const char *oduiface, const char *server, int port) +{ + int sock, rc; + iwrange range; + struct ifreq buf; + char mac[20]; + + struct sockaddr_in remote; + struct hostent *hp; + + hp = gethostbyname(server); + if (!hp) { + printf("Could not resolve %s\n", server); + exit(-1); + } + + remote.sin_family = AF_INET; + memcpy(&remote.sin_addr, hp->h_addr, hp->h_length); + remote.sin_port = htons(port); + + /* Retrieve MAC address of Wifi card */ + sock = socket(PF_INET, SOCK_DGRAM, 0); + memset(&buf, 0, sizeof(buf)); + + strcpy(buf.ifr_name, oduiface); + ioctl(sock, SIOCGIFHWADDR, &buf); + iw_sawap_ntop(&buf.ifr_hwaddr, mac); + + sock = iw_sockets_open(); + + rc = iw_get_range_info(sock, iface, &range); + if (rc < 0) { + printf("iw_get_range_info() returned %i\n", rc); + exit(2); + } + + while (1) { + time_t now; + wireless_scan_head head; + wireless_scan *result; + /* Scan for wireless networks */ + rc = iw_scan(sock, iface, range.we_version_compiled, &head); + if (rc < 0) { + printf("iw_scan() returned %i\n", rc); + exit(2); + } + + now = time(NULL); + + /* Print the results */ + result = head.result; + while (NULL != result) { + char bssid[20]; + char line[1024]; + char essid_escaped[50]; + wireless_scan *old_result; + iw_sawap_ntop(&result->ap_addr, bssid); + + escape_essid(result->b.essid, essid_escaped, 50); + snprintf(line, 1024, "WIFI,%li,%s,\"%s\",%s,%0.0f,%i\r\n", now, mac, essid_escaped, bssid, result->b.freq/1000000, (int8_t)result->stats.qual.level); + if (verbose) + printf("%s", line); + rc = sendto(sock, line, strlen(line), 0, (struct sockaddr*)&remote, sizeof(remote)); + if (rc == -1) + printf("Failed to send message: %s\n", strerror(errno)); + old_result = result; + result = result->next; + free(old_result); + } + sleep(delay); + } +} + +static void usage(void) +{ + printf("Usage: prog [options] host [port]\n"); +} + +int main(int argc, char *argv[]) +{ + int option; + char *wifiiface = NULL, *ifacename = NULL, *server; + int port = 20001; + + while ((option = getopt(argc, argv, "?hvw:i:d:")) != -1) { + switch (option) { + case 'v': + verbose = 1; + break; + case 'w': + wifiiface = optarg; + break; + case 'i': + ifacename = optarg; + break; + case 'd': + delay = atoi(optarg); + break; + case '?': + case 'h': + usage(); + exit(1); + } + } + + if (optind < argc) { + server = argv[optind]; + optind++; + } else { + usage(); + exit(-1); + } + if (optind < argc) + port = atoi(argv[optind]); + + scan_wifi(wifiiface, ifacename, server, port); + + return 0; +}