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