Hi,
I'll try to document how the backend of simple-netaid works. First of
all, please take in mind that this project
was started by another person (Edward Bartolo) with a very different
point of view, but helding out an inspiration
for mine. So, take this thread as a positive feedback for the project.
This said, let's start doing a list of all the different
items of the project:
1- The shared library:
This library contains all the functions required for getting the
information about the network status,
that is, the list of the network devices, if we are connected or
disconnected, the ip address, etc, etc. All the resulting
information will be shown in the command line. It's important to
understand the fact that all these functions included in
the shared library can't require root privileges. The backbone of the
shared library is a small part of the iproute project
remaining the rest of the project in order to enable the accessibility
to the "iproute show" command without root privileges.
The code'll say if we are connected or disconnected. There are other
methods for that, but i found this one the most lightweight
of them. Take in mind that this function will be run at every second.
Only the shared library is written in C, the rest is written
in C++ and Gtkmm-2.4.
2- The server:
That is the server of an unix socket. A binary containing a while(1){}
loop (forever) which reads the outputs
in the command line of those functions included in the shared library,
writing this information afterwards in a file descriptor
accessible from the worker thread of the frontend.
3- The backend:
A SUID binary that involves those functions requiring superuser
privileges. For example, the wifi scanning,the connection attempts...
4- The frontend:
A multithreaded process with two threads, one GUI thread, and one worker
thread. A Glib::Dispatcher is used
for sending notifications from the second one to the first one, because
only the GUI thread can update the GUI. The data sent
is protected by a mutex. Depending on the automatically connect option's
setup, the worker thread will call or not to the backend
for the connection attempts.
In addition to this documentation, there will be a simple example
consisting of systray icon that will blink when the ethernet cable is
plugged.
But first things first, and i'll start it giving an example about the
use of an unix socket for the communication between two different processes.
Recently I put the following example for a member of the gtkmm mailing
list, asking for a working example of a socket:
http://www.gnuinos.org/examples/socket/
Build the server and the client:
$ gcc server.c -o server
$ gcc client.c -o client
Run the server in a secondary plane:
$ ./server &
And then run the client as often as you wish:
$ ./client
You'll get the messages each time, as follows:
$ ./server &
[1] 2993
$ ./client
This is the first message
This is the second message
$ ./client
This is the first message
This is the second message
$ pkill server
[1]+ Terminado ./server
Cheers,
Aitor.