wifi2udp: Initial version and as part of a patch to wireless-tools
This is using UDP similiar to gps2udp. Holger has pushed for a simple solution like this to be replaced by a proper application that will use AMQP. Fixes: ORT#586
This commit is contained in:
parent
930665f4b1
commit
1d29e4db37
|
@ -0,0 +1,185 @@
|
|||
From 987960ea03c59a2ef9164e4f7309031ef4e0242d Mon Sep 17 00:00:00 2001
|
||||
Message-Id: <987960ea03c59a2ef9164e4f7309031ef4e0242d.1410189314.git.daniel@totalueberwachung.de>
|
||||
From: Daniel Willmann <dwillmann@sysmocom.de>
|
||||
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.29/Makefile
|
||||
===================================================================
|
||||
--- wireless_tools.29.orig/Makefile 2014-09-08 17:27:45.000000000 +0200
|
||||
+++ wireless_tools.29/Makefile 2014-09-08 17:32:52.065211007 +0200
|
||||
@@ -48,7 +48,7 @@
|
||||
# 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
|
||||
@@ -135,6 +135,8 @@
|
||||
|
||||
macaddr: macaddr.o $(IWLIB)
|
||||
|
||||
+wifi2udp: wifi2udp.o $(IWLIB)
|
||||
+
|
||||
iwmulticall: iwmulticall.o
|
||||
$(CC) $(LDFLAGS) $(STRIPFLAGS) $(XCFLAGS) -o $@ $^ $(LIBS)
|
||||
|
||||
Index: wireless_tools.29/wifi2udp.c
|
||||
===================================================================
|
||||
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
|
||||
+++ wireless_tools.29/wifi2udp.c 2014-09-08 17:30:08.217211001 +0200
|
||||
@@ -0,0 +1,146 @@
|
||||
+
|
||||
+#include <unistd.h>
|
||||
+#include <stdio.h>
|
||||
+#include <time.h>
|
||||
+#include <iwlib.h>
|
||||
+
|
||||
+#include <sys/socket.h>
|
||||
+
|
||||
+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];
|
||||
+ 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));
|
||||
+ result = result->next;
|
||||
+ }
|
||||
+ 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;
|
||||
+}
|
|
@ -0,0 +1,7 @@
|
|||
[Unit]
|
||||
Description=WIFI scan service
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/sbin/wifi2udp -w wlan0 -i eth0 10.3.0.2
|
||||
Restart=always
|
||||
RestartSec=2
|
|
@ -0,0 +1,21 @@
|
|||
THISDIR := "${@os.path.dirname(bb.data.getVar('FILE', d, True))}"
|
||||
FILESPATH =. "${@base_set_filespath(["${THISDIR}/files"], d)}:"
|
||||
|
||||
PRINC="3"
|
||||
|
||||
SRC_URI += "file://0001-Add-wifi2udp-program.patch \
|
||||
file://wifi2udp.service"
|
||||
|
||||
PACKAGES =+ "wireless-tools-wifi2udp"
|
||||
|
||||
FILES_wireless-tools-wifi2udp = "${sbindir}/wifi2udp \
|
||||
${systemd_unitdir}/system/wifi2udp.service \
|
||||
${systemd_unitdir}/system/multi-user.target.wants/wifi2udp.service"
|
||||
|
||||
do_install_append() {
|
||||
install -m 0755 wifi2udp ${D}${sbindir}/wifi2udp
|
||||
install -d ${D}${systemd_unitdir}/system/
|
||||
install -m 0644 ${WORKDIR}/wifi2udp.service ${D}${systemd_unitdir}/system/
|
||||
install -d ${D}${systemd_unitdir}/system/multi-user.target.wants/
|
||||
ln -sf ../wifi2udp.service ${D}${systemd_unitdir}/system/multi-user.target.wants/
|
||||
}
|
Loading…
Reference in New Issue