Merging upstream version 0.8.2 (Closes: #786485, #784725, #801319).
[debian/lxpanel.git] / plugins / netstat / lxnm_client.c
CommitLineData
38c4c1ba
AG
1/*
2 * Copyright (C) 2008 Fred Chien <fred@lxde.org>
3 * 2012 Henry Gebhardt <hsggebhardt@gmail.com>
4 *
5 * This file is a part of LXPanel project.
f8c25730
DB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21#if HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <glib.h>
26#include <sys/time.h>
27#include <sys/socket.h>
28#include <sys/un.h>
29#include <unistd.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <stdio.h>
33#include <string.h>
34#include <glib.h>
35#include <glib/gi18n.h>
36#include <iwlib.h>
37#include "netstat.h"
38#include "lxnm_client.h"
39
40static char*
41asc2hex(char const *src)
42{
43 char *buf, *tmp;
44 char c[3];
45
46 buf = g_malloc(sizeof(char)+strlen(src)*2);
47 tmp = buf;
48
49 for (;*src!='\0';src++) {
50 sprintf(c, "%X", *src);
51 *tmp = c[0];
52 *(tmp+1) = c[1];
53 tmp += 2;
54 }
55
56 *tmp = '\0';
57 return buf;
58}
59
60static gboolean
61lxnm_read_channel(GIOChannel *gio, GIOCondition condition, gpointer data)
62{
63/*
64 GIOStatus ret;
65 GError *err = NULL;
66 gchar *msg;
67 gsize len;
68
69 if (condition & G_IO_HUP)
70 g_error ("Read end of pipe died!\n");
71
72 ret = g_io_channel_read_line (gio, &msg, &len, NULL, &err);
73 if (ret == G_IO_STATUS_ERROR)
74 g_error ("Error reading: %s\n", err->message);
75
76 printf ("Read %u bytes: %s\n", len, msg);
77
78 g_free (msg);
79*/
80 return TRUE;
81}
82
83GIOChannel *
84lxnm_socket(void)
85{
86 GIOChannel *gio;
87 int sockfd;
88 struct sockaddr_un sa_un;
89
90 /* create socket */
91 sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
92 if (sockfd < 0) {
93 return NULL;
94 }
95
96 /* Initiate socket */
97 bzero(&sa_un, sizeof(sa_un));
98
99 /* setting UNIX socket */
100 sa_un.sun_family = AF_UNIX;
101 snprintf(sa_un.sun_path, sizeof(sa_un.sun_path), LXNM_SOCKET);
102
103 if (connect(sockfd, (struct sockaddr *) &sa_un, sizeof (sa_un)) < 0) {
104 return NULL;
105 }
106
107 gio = g_io_channel_unix_new(sockfd);
108 g_io_channel_set_encoding(gio, NULL, NULL);
64ea8d44 109 /* FIXME: g_source_remove() on the watch or we get crash sometime! */
f8c25730
DB
110 g_io_add_watch(gio, G_IO_IN | G_IO_HUP, lxnm_read_channel, NULL);
111
112 return gio;
113}
114
115void lxnm_close(GIOChannel *gio)
116{
117 if (gio)
118 close(g_io_channel_unix_get_fd(gio));
119}
120
121void
122lxnm_send_command(GIOChannel *gio, int command, const char* cmdargs)
123{
124 char *msg;
125 gsize len;
126
127 if (gio==NULL)
128 return;
129
130 msg = g_strdup_printf("%d %s\n", command, cmdargs);
131 g_io_channel_write_chars(gio, msg, -1, &len, NULL);
132 g_io_channel_flush(gio, NULL);
133 g_free(msg);
134}
135
136char *lxnm_wireless_command_make(const char *ifname, const char *essid,
00916e98
AG
137 const char *apaddr, const char *key,
138 int protocol, int key_mgmt,
139 int group, int pairwise)
f8c25730
DB
140{
141 char *cmd_essid;
142 char *cmd_key;
143 char *cmd;
144
145 if (essid==NULL || strlen(essid)==0) {
146 cmd_essid = g_strdup("NULL");
147 } else {
00916e98 148 cmd_essid = asc2hex(essid);
f8c25730
DB
149 }
150
151 if (key==NULL || strlen(key)==0) {
152 cmd_key = g_strdup("OFF");
153 } else {
00916e98 154 cmd_key = asc2hex(key);
f8c25730
DB
155 }
156
157 cmd = g_strdup_printf("%s %s %s %s %d %d %d %d", ifname, cmd_essid, apaddr,
158 cmd_key, protocol, key_mgmt, group, pairwise);
159
160 g_free(cmd_essid);
161 g_free(cmd_key);
162
163 return cmd;
164}