0

I'm sniffing WiFi packets for RSSI (my interest is probe request and data null, timestamp and source mac address), and I need to save the results in pcap files for further processing.

The problem is that I need to divide the info to multiple different files, creating one file per hour.
I don't have an idea how to check the elapsed time in my program, except multithreading (while(1), sleep() and check with clock() function). I'm using Ubuntu, but the target platform is a router with Linux OpenWrt installed.

My first concert are the problems with cross compilation + pthread.h, thread.h or something else, or maybe there should be no issues in OpenWrt?

And the other side of the question - isn't it too complicated to use multithreading for this silly problem?
Maybe there are other solutions? (i.e. a small pseudo-multithreading solution, or some simpler tricks)?

Ralor
  • 371
  • 1
  • 3
  • 10

1 Answers1

2

You should use Cron:

Cron is a system daemon used to execute desired tasks (in the background) at designated times.

Basically, you define a script to be executed every hour with this syntax:

0 */1 * * * /path/to/script.sh

Check here for more info: https://help.ubuntu.com/community/CronHowto

For OpenWrt specific setup, check here: http://martybugs.net/wireless/openwrt/cron.cgi

For your specific case, you can use the following design:

  • Cron jobs triggers every full hour
  • The custom script gets executed and uses an IPC mechanism to inform the main app.
  • The main app has a dedicated function in the writter handler that does what is necessary to reset the writter (other function calls, file deletion, creating new file, etc.)

enter image description here

For an overview of IPC methods to get you started, check this Wikipedia article.

For specific implementations you can use Boost.Interprocess, Sockets, Pipes, and others.

bosnjak
  • 8,424
  • 2
  • 21
  • 47
  • As I understood, cron can run sh scripts with some shedule, but how can I make my program start writing in next file (file0.pcap -> file1.pcap)? – Ralor Mar 24 '14 at 15:24
  • This is a logic you must implement. The simplest approach I can think of would be to have a file, for example `/var/tmp/startnew`, and make the cron script write `1` to it every hour. On every write from your main program you check if this file contains the value `1`. If it does, you write `0` to it and start a new file. But of course, this is not the ideal solution, just a minimum effort solution that could get you started. If you have many writes to a file, reading an additional file will be a big overhead. In such cases you should use other techniques.. – bosnjak Mar 24 '14 at 15:26
  • This is the main problem for me, I need to stop writing to file directly after one hour, not by checking before writing. – Ralor Mar 24 '14 at 15:30
  • I don't see any difference between those two. If you stop the next time after the hour has passed, effectively you have started instantly. No data was written, right? Please elaborate if I am wrong. – bosnjak Mar 24 '14 at 15:31
  • To stop writing to file I need to call pcap_dump_close(). File will be corrupted without it. And if there wasn't any packet during one hour (and no write actions of course), it also must be saved separately. – Ralor Mar 24 '14 at 15:37
  • No problem. In such case you need to implement some other mechanism, like FIFO's, some of the IPC mechanisms, and other, so you can actively communicate with your app and trigger events, etc... – bosnjak Mar 24 '14 at 15:39
  • Hmm, I think the cron + IPC is working solution. Thanks, now I have a start point) – Ralor Mar 24 '14 at 15:53
  • No problem, i have further update the answer with relevant info. – bosnjak Mar 24 '14 at 16:01