~ubuntu-branches/ubuntu/utopic/modem-manager-gui/utopic

« back to all changes in this revision

Viewing changes to src/netlink.c

  • Committer: Package Import Robot
  • Author(s): Graham Inggs
  • Date: 2013-07-30 12:51:59 UTC
  • Revision ID: package-import@ubuntu.com-20130730125159-flzv882fhuzhmfmi
Tags: upstream-0.0.16
ImportĀ upstreamĀ versionĀ 0.0.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      netlink.c
 
3
 *      
 
4
 *      Copyright 2012-2013 Alex <alex@linuxonly.ru>
 
5
 *      
 
6
 *      This program is free software: you can redistribute it and/or modify
 
7
 *      it under the terms of the GNU General Public License as published by
 
8
 *      the Free Software Foundation; either version 3 of the License, or
 
9
 *      (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, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include <glib.h>
 
21
#include <stdio.h>
 
22
#include <string.h>
 
23
#include <errno.h>
 
24
#include <unistd.h>
 
25
#include <time.h>
 
26
#include <sys/types.h>
 
27
#include <sys/socket.h>
 
28
#include <netdb.h>
 
29
#include <arpa/inet.h>
 
30
#include <netinet/tcp.h>
 
31
#include <netinet/in.h>
 
32
#include <linux/netlink.h>
 
33
#include <linux/inet_diag.h>
 
34
#include <dirent.h>
 
35
#include <ctype.h>
 
36
#include <sys/stat.h>
 
37
#include <fcntl.h>
 
38
#include <net/if.h>
 
39
 
 
40
#include "netlink.h"
 
41
 
 
42
#define MMGUI_NETLINK_INTERNAL_SEQUENCE_NUMBER 100000
 
43
 
 
44
static gboolean mmgui_netlink_numeric_name(gchar *dirname)
 
45
{
 
46
        if ((dirname == NULL) || (!*dirname)) return FALSE;
 
47
        
 
48
        while (*dirname) {
 
49
                if (!isdigit(*dirname)) {
 
50
                        return FALSE;
 
51
                } else {
 
52
                        *dirname++;
 
53
                }
 
54
        }
 
55
        
 
56
        return TRUE;
 
57
}
 
58
 
 
59
static gboolean mmgui_netlink_process_access(gchar *dirname, uid_t uid)
 
60
{
 
61
        gchar fullpath[128];
 
62
        struct stat pathstat;
 
63
        
 
64
        if (!mmgui_netlink_numeric_name(dirname)) return FALSE;
 
65
        
 
66
        memset(fullpath, 0, sizeof(fullpath));
 
67
        
 
68
        sprintf(fullpath, "/proc/%s", dirname);
 
69
        
 
70
        if (stat(fullpath, &pathstat) == -1) {
 
71
                return FALSE;
 
72
        }
 
73
        
 
74
        if (pathstat.st_uid != uid) {
 
75
                return FALSE;
 
76
        }
 
77
        
 
78
        return TRUE;
 
79
}
 
80
 
 
81
static gboolean mmgui_netlink_socket_access(gchar *dirname, gchar *sockname, guint inode)
 
82
{
 
83
        gchar fullpath[128];
 
84
        struct stat fdstat;
 
85
        
 
86
        if (!mmgui_netlink_numeric_name(sockname)) return FALSE;
 
87
                
 
88
        memset(fullpath, 0, sizeof(fullpath));
 
89
        
 
90
        sprintf(fullpath, "/proc/%s/fd/%s", dirname, sockname);
 
91
        
 
92
        if (stat(fullpath, &fdstat) == -1) {
 
93
                return FALSE;
 
94
        }
 
95
        
 
96
        if (((fdstat.st_mode & S_IFMT) == S_IFSOCK) && (fdstat.st_ino == inode)) {
 
97
                return TRUE;
 
98
        }
 
99
        
 
100
        return FALSE;
 
101
}
 
102
 
 
103
static gchar *mmgui_netlink_process_name(gchar *dirname, gchar *appname, gsize appsize)
 
104
{
 
105
        gint fd, i;
 
106
        gchar fpath[128];
 
107
        ssize_t linkchars;
 
108
                
 
109
        if ((dirname == NULL) || (dirname[0] == '\0')) return NULL;
 
110
        if ((appname == NULL) || (appsize == 0)) return NULL;
 
111
        
 
112
        memset(fpath, 0, sizeof(fpath));
 
113
        sprintf(fpath, "/proc/%s/exe", dirname);
 
114
        
 
115
        linkchars = readlink(fpath, appname, appsize-1);
 
116
        
 
117
        if (linkchars == 0) {
 
118
                memset(fpath, 0, sizeof(fpath));
 
119
                sprintf(fpath, "/proc/%s/comm", dirname);
 
120
                
 
121
                fd = open(fpath, O_RDONLY);
 
122
                if (fd != -1) {
 
123
                        linkchars = read(fd, appname, appsize-1);
 
124
                        close(fd);
 
125
                } else {
 
126
                        return NULL;
 
127
                }
 
128
        }
 
129
                
 
130
        appname[linkchars] = '\0';
 
131
        
 
132
        for (i=linkchars; i>=0; i--) {
 
133
                if (appname[i] == '/') {
 
134
                        memmove(appname+0, appname+i+1, linkchars-i-1);
 
135
                        linkchars -= i+1;
 
136
                        break;
 
137
                }
 
138
        }
 
139
        
 
140
        appname[linkchars] = '\0';
 
141
        
 
142
        return appname;
 
143
}
 
144
 
 
145
static gboolean mmgui_netlink_get_process(guint inode, gchar *appname, gsize namesize, pid_t *apppid)
 
146
{
 
147
        DIR *procdir, *fddir;
 
148
        struct dirent *procde, *fdde;
 
149
        char fdirpath[128], fdpath[128];
 
150
        
 
151
        if ((appname == NULL) || (namesize == 0) || (apppid == NULL)) return FALSE;
 
152
        
 
153
        procdir = opendir("/proc");
 
154
        if (procdir != NULL) {
 
155
                while (procde = readdir(procdir)) {
 
156
                        if (mmgui_netlink_process_access(procde->d_name, getuid())) {
 
157
                                memset(fdirpath, 0, sizeof(fdirpath));
 
158
                                sprintf(fdirpath, "/proc/%s/fd", procde->d_name);
 
159
                                //enumerate file descriptors
 
160
                                fddir = opendir(fdirpath);
 
161
                                if (fddir != NULL) {
 
162
                                        while (fdde = readdir(fddir)) {
 
163
                                                if (mmgui_netlink_socket_access(procde->d_name, fdde->d_name, inode)) {
 
164
                                                        //printf("%s:%s (%s)\n", procde->d_name, fdde->d_name, nlconnections_process_name(procde->d_name, appname, sizeof(appname)));
 
165
                                                        *apppid = atoi(procde->d_name);
 
166
                                                        mmgui_netlink_process_name(procde->d_name, appname, namesize);
 
167
                                                        closedir(fddir);
 
168
                                                        closedir(procdir);
 
169
                                                        return TRUE;
 
170
                                                }
 
171
                                        }
 
172
                                        closedir(fddir);
 
173
                                }
 
174
                        }
 
175
                }
 
176
                closedir(procdir);
 
177
        }
 
178
        
 
179
        return FALSE;
 
180
}
 
181
 
 
182
gboolean mmgui_netlink_terminate_application(pid_t pid)
 
183
{
 
184
        if (kill(pid, 0) == 0) {
 
185
                if (kill(pid, SIGTERM) == 0) {
 
186
                        return TRUE;
 
187
                }
 
188
        }
 
189
        
 
190
        return FALSE;
 
191
}
 
192
 
 
193
gchar *mmgui_netlink_socket_state(guchar state)
 
194
{
 
195
        switch (state) {
 
196
                case TCP_ESTABLISHED:
 
197
                        return "Established";
 
198
                case TCP_SYN_SENT:
 
199
                        return "SYN sent";
 
200
                case TCP_SYN_RECV:
 
201
                        return "SYN recv";
 
202
                case TCP_FIN_WAIT1:
 
203
                        return "FIN wait";
 
204
                case TCP_FIN_WAIT2:
 
205
                        return "FIN wait";
 
206
                case TCP_TIME_WAIT:
 
207
                        return "Time wait";
 
208
                case TCP_CLOSE:
 
209
                        return "Close";
 
210
                case TCP_CLOSE_WAIT:
 
211
                        return "Close wait";
 
212
                case TCP_LAST_ACK:
 
213
                        return "Last ACK";
 
214
                case TCP_LISTEN:
 
215
                        return "Listen";
 
216
                case TCP_CLOSING:
 
217
                        return "Closing";
 
218
                default:
 
219
                        return "Unknown";
 
220
        }
 
221
}
 
222
 
 
223
void mmgui_netlink_hash_destroy(gpointer data)
 
224
{
 
225
        mmgui_netlink_connection_t connection;
 
226
        
 
227
        connection = (mmgui_netlink_connection_t)data;
 
228
        
 
229
        if (connection == NULL) return;
 
230
        
 
231
        if (connection->appname != NULL) g_free(connection->appname);
 
232
        if (connection->dsthostname != NULL) g_free(connection->dsthostname);
 
233
        
 
234
        g_free(connection);
 
235
}
 
236
 
 
237
static gboolean mmgui_netlink_hash_clear_foreach(gpointer key, gpointer value, gpointer user_data)
 
238
{
 
239
        mmgui_netlink_connection_t connection;
 
240
        time_t currenttime;
 
241
        
 
242
        connection = (mmgui_netlink_connection_t)value;
 
243
        currenttime = *(time_t *)user_data;
 
244
        
 
245
        if (connection->updatetime == currenttime) {
 
246
                return FALSE;
 
247
        } else {
 
248
                //printf("Remove: %u\n", socket->inode);
 
249
                return TRUE;
 
250
        }
 
251
}
 
252
 
 
253
gboolean mmgui_netlink_request_connections_list(mmgui_netlink_t netlink, guint family)
 
254
{
 
255
        struct _mmgui_netlink_connection_info_request request;
 
256
        gint status;
 
257
        
 
258
        if ((netlink == NULL) || ((!(family & AF_INET)) && (!(family & AF_INET6)))) return FALSE;
 
259
        
 
260
        memset(&request.msgheader, 0, sizeof(struct nlmsghdr));
 
261
        
 
262
        request.msgheader.nlmsg_len = sizeof(struct _mmgui_netlink_connection_info_request);
 
263
        request.msgheader.nlmsg_type = TCPDIAG_GETSOCK;
 
264
        request.msgheader.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
 
265
        request.msgheader.nlmsg_pid = getpid();
 
266
        request.msgheader.nlmsg_seq = 0;
 
267
        
 
268
        memset(&request.nlreq, 0, sizeof(struct inet_diag_req));
 
269
        request.nlreq.idiag_family = family;
 
270
        request.nlreq.idiag_states = ((1 << TCP_CLOSING + 1) - 1);
 
271
        
 
272
        request.msgheader.nlmsg_len = NLMSG_ALIGN(request.msgheader.nlmsg_len);
 
273
        
 
274
        status = send(netlink->connsocketfd, &request, request.msgheader.nlmsg_len, 0);
 
275
        
 
276
        if (status != -1) {
 
277
                return TRUE;
 
278
        } else {
 
279
                return FALSE;
 
280
        }
 
281
}
 
282
 
 
283
gboolean mmgui_netlink_read_connections_list(mmgui_netlink_t netlink, gchar *data, gsize datasize)
 
284
{
 
285
        time_t currenttime;
 
286
        struct nlmsghdr *msgheader;
 
287
        struct inet_diag_msg *entry;
 
288
        mmgui_netlink_connection_t connection;
 
289
        struct hostent *dsthost;
 
290
        gchar srcbuf[INET6_ADDRSTRLEN];
 
291
        gchar dstbuf[INET6_ADDRSTRLEN];
 
292
        gchar appname[1024];
 
293
        pid_t apppid;
 
294
                        
 
295
        if ((netlink == NULL) || (data == NULL) || (datasize == 0)) return FALSE;
 
296
        
 
297
        //Get current time
 
298
        currenttime = time(NULL);
 
299
        
 
300
        //Work with data
 
301
        for (msgheader = (struct nlmsghdr *)data; NLMSG_OK(msgheader, (unsigned int)datasize); msgheader = NLMSG_NEXT(msgheader, datasize)) {
 
302
                if ((msgheader->nlmsg_type == NLMSG_ERROR) || (msgheader->nlmsg_type == NLMSG_DONE)) {
 
303
                        break;
 
304
                }
 
305
                //New connections list
 
306
                if (msgheader->nlmsg_type == TCPDIAG_GETSOCK) {
 
307
                        entry = (struct inet_diag_msg *)NLMSG_DATA(msgheader);
 
308
                        if (entry != NULL) {
 
309
                                if ((entry->idiag_uid == netlink->userid) || (netlink->userid == 0)) {
 
310
                                        if (!g_hash_table_contains(netlink->connections, (gconstpointer)&entry->idiag_inode)) {
 
311
                                                //Add new connection
 
312
                                                if (mmgui_netlink_get_process(entry->idiag_inode, appname, sizeof(appname), &apppid)) {
 
313
                                                        connection = g_new(struct _mmgui_netlink_connection, 1);
 
314
                                                        connection->inode = entry->idiag_inode;
 
315
                                                        connection->family = entry->idiag_family;
 
316
                                                        connection->userid = entry->idiag_uid;
 
317
                                                        connection->updatetime = currenttime;
 
318
                                                        connection->dqueue = entry->idiag_rqueue + entry->idiag_wqueue;
 
319
                                                        connection->state = entry->idiag_state;
 
320
                                                        connection->srcport = ntohs(entry->id.idiag_sport);
 
321
                                                        g_snprintf(connection->srcaddr, sizeof(connection->srcaddr), "%s:%u", inet_ntop(entry->idiag_family, entry->id.idiag_src, srcbuf, INET6_ADDRSTRLEN), ntohs(entry->id.idiag_sport));
 
322
                                                        g_snprintf(connection->dstaddr, sizeof(connection->dstaddr), "%s:%u", inet_ntop(entry->idiag_family, entry->id.idiag_dst, dstbuf, INET6_ADDRSTRLEN), ntohs(entry->id.idiag_dport));
 
323
                                                        connection->appname = g_strdup(appname);
 
324
                                                        connection->apppid = apppid;
 
325
                                                        connection->dsthostname = NULL;
 
326
                                                        /*dsthost = gethostbyaddr(entry->id.idiag_dst, sizeof(entry->id.idiag_dst), entry->idiag_family);
 
327
                                                        if (dsthost != NULL) {
 
328
                                                                connection->dsthostname = g_strdup(dsthost->h_name);
 
329
                                                        } else {*/
 
330
                                                                connection->dsthostname = g_strdup(connection->dstaddr);
 
331
                                                        /*}*/
 
332
                                                        g_hash_table_insert(netlink->connections, (gpointer)&connection->inode, connection);
 
333
                                                        g_debug("Connection added: inode %u\n", entry->idiag_inode);
 
334
                                                }
 
335
                                        } else {
 
336
                                                //Update connection information (state, buffers fill, time)
 
337
                                                connection = g_hash_table_lookup(netlink->connections, (gconstpointer)&entry->idiag_inode);
 
338
                                                if (connection != NULL) {
 
339
                                                        connection->updatetime = currenttime;
 
340
                                                        connection->dqueue = entry->idiag_rqueue + entry->idiag_wqueue;
 
341
                                                        connection->state = entry->idiag_state;
 
342
                                                        g_debug("Connection updated: inode %u\n", entry->idiag_inode);
 
343
                                                }       
 
344
                                        }
 
345
                                }
 
346
                        }
 
347
                }
 
348
        }
 
349
        
 
350
        //Remove connections that disappear
 
351
        g_hash_table_foreach_remove(netlink->connections, mmgui_netlink_hash_clear_foreach, &currenttime);
 
352
        
 
353
        return TRUE;
 
354
}
 
355
 
 
356
gboolean mmgui_netlink_request_interface_statistics(mmgui_netlink_t netlink, gchar *interface)
 
357
{
 
358
        struct _mmgui_netlink_interface_info_request request;
 
359
        guint ifindex;
 
360
        gint status;
 
361
                
 
362
        if ((netlink == NULL) || (interface == NULL)) return FALSE;
 
363
        if ((netlink->intsocketfd == -1) || (interface[0] == '\0')) return FALSE;
 
364
        
 
365
        ifindex = if_nametoindex(interface);
 
366
        
 
367
        if ((ifindex == 0) && (errno == ENXIO)) return FALSE;
 
368
        
 
369
        memset(&request, 0, sizeof(request));
 
370
        
 
371
        request.msgheader.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
 
372
        request.msgheader.nlmsg_type = RTM_GETLINK;
 
373
        request.msgheader.nlmsg_flags = NLM_F_REQUEST;
 
374
        request.msgheader.nlmsg_seq = MMGUI_NETLINK_INTERNAL_SEQUENCE_NUMBER;
 
375
        request.msgheader.nlmsg_pid = getpid();
 
376
        
 
377
        request.ifinfo.ifi_family = AF_UNSPEC;
 
378
        request.ifinfo.ifi_index = ifindex;
 
379
        request.ifinfo.ifi_type = 0;
 
380
        request.ifinfo.ifi_flags = 0;
 
381
        request.ifinfo.ifi_change = 0xFFFFFFFF;
 
382
        
 
383
        request.msgheader.nlmsg_len = NLMSG_ALIGN(request.msgheader.nlmsg_len);
 
384
        
 
385
        status = send(netlink->intsocketfd, &request, request.msgheader.nlmsg_len, 0);
 
386
        
 
387
        if (status != -1) {
 
388
                return TRUE;
 
389
        } else {
 
390
                return FALSE;
 
391
        }
 
392
}
 
393
 
 
394
gboolean mmgui_netlink_read_interface_event(mmgui_netlink_t netlink, gchar *data, gsize datasize, mmgui_netlink_interface_event_t event)
 
395
{
 
396
        struct nlmsghdr *msgheader;
 
397
        struct ifinfomsg *ifi;
 
398
        struct rtattr *rta;
 
399
        struct rtnl_link_stats *ifstats;
 
400
        struct rtnl_link_stats64 *ifstats64;
 
401
        gchar ifname[IFNAMSIZ];
 
402
        gboolean have64bitstats;
 
403
        
 
404
        if ((netlink == NULL) || (data == NULL) || (datasize == 0) || (event == NULL)) return FALSE;
 
405
        
 
406
        //Work with data
 
407
        for (msgheader = (struct nlmsghdr *)data; NLMSG_OK(msgheader, (unsigned int)datasize); msgheader = NLMSG_NEXT(msgheader, datasize)) {
 
408
                if ((msgheader->nlmsg_type == NLMSG_ERROR) || (msgheader->nlmsg_type == NLMSG_DONE)) {
 
409
                        break;
 
410
                }
 
411
                //New event
 
412
                if ((msgheader->nlmsg_type == RTM_NEWLINK) || (msgheader->nlmsg_type == RTM_DELLINK) || (msgheader->nlmsg_type == RTM_GETLINK)) {
 
413
                        ifi = NLMSG_DATA(msgheader);
 
414
                        rta = IFLA_RTA(ifi);
 
415
                        //Copy valuable data first
 
416
                        event->running = (ifi->ifi_flags & IFF_RUNNING);
 
417
                        event->up = (ifi->ifi_flags & IFF_UP);
 
418
                        if (if_indextoname(ifi->ifi_index, ifname) != NULL) {
 
419
                                strncpy(event->ifname, ifname, sizeof(event->ifname));
 
420
                        }
 
421
                        event->type = MMGUI_NETLINK_INTERFACE_EVENT_TYPE_UNKNOWN;
 
422
                        //Detrmine type of event
 
423
                        if (msgheader->nlmsg_seq == MMGUI_NETLINK_INTERNAL_SEQUENCE_NUMBER) {
 
424
                                event->type = MMGUI_NETLINK_INTERFACE_EVENT_TYPE_STATS;
 
425
                        } else {
 
426
                                if (msgheader->nlmsg_type == RTM_NEWLINK) {
 
427
                                        event->type = MMGUI_NETLINK_INTERFACE_EVENT_TYPE_ADD;
 
428
                                        g_debug("[%u] New interface state: Running: %s, Up: %s, Name: %s\n", msgheader->nlmsg_seq, (ifi->ifi_flags & IFF_RUNNING) ? "Yes" : "No", (ifi->ifi_flags & IFF_UP) ? "Yes" : "No",  if_indextoname(ifi->ifi_index, ifname));
 
429
                                } else if (msgheader->nlmsg_type == RTM_DELLINK) {
 
430
                                        event->type = MMGUI_NETLINK_INTERFACE_EVENT_TYPE_REMOVE;
 
431
                                        g_debug("[%u] Deleted interface state: Running: %s, Up: %s, Name: %s\n", msgheader->nlmsg_seq, (ifi->ifi_flags & IFF_RUNNING) ? "Yes" : "No", (ifi->ifi_flags & IFF_UP) ? "Yes" : "No",  if_indextoname(ifi->ifi_index, ifname));
 
432
                                } else if (msgheader->nlmsg_type == RTM_GETLINK) {
 
433
                                        event->type = MMGUI_NETLINK_INTERFACE_EVENT_TYPE_STATS;
 
434
                                        g_debug("[%u] Requested interface state: Running: %s, Up: %s, Name: %s\n", msgheader->nlmsg_seq, (ifi->ifi_flags & IFF_RUNNING) ? "Yes" : "No", (ifi->ifi_flags & IFF_UP) ? "Yes" : "No",  if_indextoname(ifi->ifi_index, ifname));
 
435
                                }
 
436
                        } 
 
437
                        //If 64bit traffic statistics values are not available, 32bit values will be used anyway
 
438
                        have64bitstats = FALSE;
 
439
                        //Use tags to get additional data
 
440
                        while (RTA_OK(rta, msgheader->nlmsg_len)) {
 
441
                                if (rta->rta_type == IFLA_IFNAME) {
 
442
                                        strncpy(event->ifname, (char *)RTA_DATA(rta), sizeof(event->ifname));
 
443
                                        g_debug("Tag: Device name: %s\n", (char *)RTA_DATA(rta));
 
444
                                } else if (rta->rta_type == IFLA_STATS) {
 
445
                                        ifstats = (struct rtnl_link_stats *)RTA_DATA(rta);
 
446
                                        if (!have64bitstats) {
 
447
                                                event->rxbytes = ifstats->rx_bytes;
 
448
                                                event->txbytes = ifstats->tx_bytes;
 
449
                                                if (!(event->type & MMGUI_NETLINK_INTERFACE_EVENT_TYPE_STATS)) {
 
450
                                                        event->type |= MMGUI_NETLINK_INTERFACE_EVENT_TYPE_STATS;
 
451
                                                }
 
452
                                        }
 
453
                                        g_debug("Tag: Interface Statistics (32bit): RX: %u, TX: %u\n", ifstats->rx_bytes, ifstats->tx_bytes);
 
454
                                } else if (rta->rta_type == IFLA_STATS64) {
 
455
                                        ifstats64 = (struct rtnl_link_stats64 *)RTA_DATA(rta);
 
456
                                        event->rxbytes = ifstats64->rx_bytes;
 
457
                                        event->txbytes = ifstats64->tx_bytes;
 
458
                                        have64bitstats = TRUE;
 
459
                                        if (!(event->type & MMGUI_NETLINK_INTERFACE_EVENT_TYPE_STATS)) {
 
460
                                                event->type |= MMGUI_NETLINK_INTERFACE_EVENT_TYPE_STATS;
 
461
                                        }
 
462
                                        g_debug("Tag: Interface Statistics (64bit): RX: %llu, TX: %llu\n", ifstats64->rx_bytes, ifstats64->tx_bytes);
 
463
                                } else if (rta->rta_type == IFLA_LINK) {
 
464
                                        g_debug("Tag: Link type\n");
 
465
                                } else if (rta->rta_type == IFLA_ADDRESS) {
 
466
                                        g_debug("Tag: interface L2 address\n");
 
467
                                } else if (rta->rta_type == IFLA_UNSPEC) {
 
468
                                        g_debug("Tag: unspecified\n");
 
469
                                } else {
 
470
                                        g_debug("Tag: %u\n", rta->rta_type);
 
471
                                }
 
472
                                rta = RTA_NEXT(rta, msgheader->nlmsg_len);
 
473
                        }
 
474
                }
 
475
        }
 
476
        
 
477
        return TRUE;
 
478
}
 
479
 
 
480
gint mmgui_netlink_get_connections_monitoring_socket_fd(mmgui_netlink_t netlink)
 
481
{
 
482
        if (netlink == NULL) return -1;
 
483
        
 
484
        return netlink->connsocketfd;
 
485
}
 
486
 
 
487
gint mmgui_netlink_get_interfaces_monitoring_socket_fd(mmgui_netlink_t netlink)
 
488
{
 
489
        if (netlink == NULL) return -1;
 
490
        
 
491
        return netlink->intsocketfd;
 
492
}
 
493
 
 
494
struct sockaddr_nl *mmgui_netlink_get_connections_monitoring_socket_address(mmgui_netlink_t netlink)
 
495
{
 
496
        if (netlink == NULL) return NULL;
 
497
        
 
498
        return &(netlink->connaddr);
 
499
}
 
500
 
 
501
struct sockaddr_nl *mmgui_netlink_get_interfaces_monitoring_socket_address(mmgui_netlink_t netlink)
 
502
{
 
503
        if (netlink == NULL) return NULL;
 
504
        
 
505
        return &(netlink->intaddr);
 
506
}
 
507
 
 
508
GHashTable *mmgui_netlink_get_connections_list(mmgui_netlink_t netlink)
 
509
{
 
510
        if (netlink == NULL) return NULL;
 
511
        
 
512
        return netlink->connections;
 
513
}
 
514
 
 
515
void mmgui_netlink_close(mmgui_netlink_t netlink)
 
516
{
 
517
        if (netlink == NULL) return;
 
518
        
 
519
        if (netlink->connsocketfd != -1) {
 
520
                close(netlink->connsocketfd);
 
521
                g_hash_table_destroy(netlink->connections);
 
522
        }
 
523
        
 
524
        if (netlink->intsocketfd != -1) {
 
525
                close(netlink->intsocketfd);
 
526
        }
 
527
        
 
528
        g_free(netlink);
 
529
}
 
530
 
 
531
mmgui_netlink_t mmgui_netlink_open(void)
 
532
{
 
533
        mmgui_netlink_t netlink;
 
534
                
 
535
        netlink = g_new(struct _mmgui_netlink, 1);
 
536
        
 
537
        #ifndef NETLINK_SOCK_DIAG
 
538
                netlink->connsocketfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG);
 
539
        #else
 
540
                netlink->connsocketfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
 
541
        #endif
 
542
        
 
543
        if (netlink->connsocketfd != -1) {
 
544
                memset(&netlink->connaddr, 0, sizeof(struct sockaddr_nl));
 
545
                netlink->connaddr.nl_family = AF_NETLINK;
 
546
                netlink->connaddr.nl_pid = getpid();
 
547
                netlink->connaddr.nl_groups = 0;
 
548
                
 
549
                netlink->userid = getuid();
 
550
                
 
551
                netlink->connections = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, (GDestroyNotify)mmgui_netlink_hash_destroy);
 
552
        } else {
 
553
                netlink->connections = NULL;
 
554
                g_debug("Failed to open connections monitoring netlink socket\n");
 
555
        }
 
556
        
 
557
        netlink->intsocketfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
 
558
        
 
559
        if (netlink->intsocketfd != -1) {
 
560
                memset(&netlink->intaddr, 0, sizeof(netlink->intaddr));
 
561
                netlink->intaddr.nl_family = AF_NETLINK;
 
562
                netlink->intaddr.nl_groups = RTMGRP_LINK;
 
563
                netlink->intaddr.nl_pid = getpid();
 
564
                
 
565
                if (bind(netlink->intsocketfd, (struct sockaddr *)&(netlink->intaddr), sizeof(netlink->intaddr)) == -1) {
 
566
                        g_debug("Failed to bind network interfaces monitoring netlink socket\n");
 
567
                        close(netlink->intsocketfd);
 
568
                        netlink->intsocketfd = -1;
 
569
                }
 
570
        } else {
 
571
                g_debug("Failed to open network interfaces monitoring netlink socket\n");
 
572
        }
 
573
        
 
574
        return netlink;
 
575
}