1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/* arch-tag: c421c4ab-1bca-452b-8fe7-24fe1b98d895 */
#ifndef __DASHBOARD_FRONTEND_H__
#define __DASHBOARD_FRONTEND_H__
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <glib.h>
#include <strings.h>
#include <unistd.h>
#if GLIB_CHECK_VERSION (2,0,0)
#include <glib/giochannel.h>
#endif
#define DASHBOARD_PORT 5913
#define NATMIN(a,b) ((a) < (b) ? (a) : (b))
/*
* Open a connection to the dashboard. We never block and at
* the first sign of a problem we bail.
*/
static int
dashboard_connect_with_timeout (int *fd,
long timeout_usecs)
{
struct sockaddr_in sock;
struct timeval timeout;
fd_set write_fds;
*fd = socket (PF_INET, SOCK_STREAM, 0);
if (*fd < 0) {
perror ("Dashboard: socket");
return 0;
}
/*
* Set the socket to be non-blocking so that connect ()
* doesn't block.
*/
if (fcntl (*fd, F_SETFL, O_NONBLOCK) < 0) {
perror ("Dashboard: setting O_NONBLOCK");
return 0;
}
bzero ((char *) &sock, sizeof (sock));
sock.sin_family = AF_INET;
sock.sin_port = htons (DASHBOARD_PORT);
sock.sin_addr.s_addr = inet_addr ("127.0.0.1");
timeout.tv_sec = 0;
timeout.tv_usec = timeout_usecs;
while (1) {
/*
* Try to connect.
*/
if (connect (*fd, (struct sockaddr *) &sock,
sizeof (struct sockaddr_in)) < 0) {
if (errno != EAGAIN &&
errno != EINPROGRESS) {
return 0;
}
} else
return 1;
/*
* We couldn't connect, so we select on the fd and
* wait for the timer to run out, or for the fd to be
* ready.
*/
FD_ZERO (&write_fds);
FD_SET (*fd, &write_fds);
while (select (getdtablesize (), NULL, &write_fds, NULL, &timeout) < 0) {
if (errno != EINTR) {
perror ("Dashboard: select");
return 0;
}
}
if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
fprintf (stderr, "Dashboard: Connection timed out.\n");
return 0;
}
}
return 1;
}
typedef struct {
char *rawcluepacket;
int bytes_written;
} CluepacketInfo;
#if GLIB_CHECK_VERSION(2,0,0)
static gboolean
cluepacket_write_cb (GIOChannel *channel,
GIOCondition cond,
gpointer user_data)
{
CluepacketInfo *info = user_data;
GIOError err;
int total_bytes;
total_bytes = strlen (info->rawcluepacket);
do {
int b;
err = g_io_channel_write (channel,
info->rawcluepacket + info->bytes_written,
total_bytes - info->bytes_written,
&b);
info->bytes_written += b;
} while (info->bytes_written < total_bytes && err == G_IO_ERROR_NONE);
if (err == G_IO_ERROR_NONE) {
/* We're all done sending */
fprintf (stderr, "Dashboard: Sent.\n");
goto cleanup;
}
if (err == G_IO_ERROR_AGAIN) {
/* Hand control back to the main loop */
return TRUE;
}
/* Otherwise... */
fprintf (stderr, "Dashboard: Error trying to send cluepacket.\n");
cleanup:
g_io_channel_close (channel);
g_free (info->rawcluepacket);
g_free (info);
return FALSE;
}
static void
dashboard_send_raw_cluepacket (const char *rawcluepacket)
{
int fd;
GIOChannel *channel;
CluepacketInfo *info;
/* Connect. */
if (! dashboard_connect_with_timeout (&fd, 200000))
return;
channel = g_io_channel_unix_new (fd);
info = g_new0 (CluepacketInfo, 1);
info->rawcluepacket = g_strdup (rawcluepacket);
g_io_add_watch (channel,
G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
cluepacket_write_cb,
info);
g_io_channel_unref (channel);
}
#endif /* GLIB_CHECK_VERSION (2,0,0) */
#if !GLIB_CHECK_VERSION (2,0,0)
static char *
lame_xml_quote (const char *str)
{
char *retval;
const char *p;
char *q;
if (str == NULL || strlen (str) == 0)
return g_strdup ("");
retval = g_new (char, strlen (str) * 3);
q = retval;
for (p = str; *p != '\0'; p ++) {
switch (*p) {
case '<':
*q ++ = '&';
*q ++ = 'l';
*q ++ = 't';
*q ++ = ';';
break;
case '>':
*q ++ = '&';
*q ++ = 'g';
*q ++ = 't';
*q ++ = ';';
break;
case '&':
*q ++ = '&';
*q ++ = 'a';
*q ++ = 'm';
*q ++ = 'p';
*q ++ = ';';
break;
default:
*q ++ = *p;
break;
}
}
*q = '\0';
return retval;
}
#endif
static char *
dashboard_xml_quote (const char *str)
{
#if GLIB_CHECK_VERSION (2,0,0)
return g_markup_escape_text (str, strlen (str));
#else
return lame_xml_quote (str);
#endif
}
static char *
dashboard_build_clue (const char *text,
const char *type,
int relevance)
{
char *text_xml;
char *clue;
if (text == NULL || strlen (text) == 0)
return g_strdup ("");
text_xml = dashboard_xml_quote (text);
clue = g_strdup_printf (" <Clue Type=\"%s\" Relevance=\"%d\">%s</Clue>\n",
type, relevance, text_xml);
g_free (text_xml);
return clue;
}
static char *
dashboard_build_cluepacket_from_cluelist (const char *frontend,
gboolean focused,
const char *context,
GList *clues)
{
char *cluepacket;
char *new_cluepacket;
GList *l;
g_return_val_if_fail (frontend != NULL, NULL);
g_return_val_if_fail (clues != NULL, NULL);
cluepacket = g_strdup_printf (
"<CluePacket>\n"
" <Frontend>%s</Frontend>\n"
" <Context>%s</Context>\n"
" <Focused>%s</Focused>\n",
frontend,
context,
focused ? "true" : "false");
for (l = clues; l != NULL; l = l->next) {
const char *clue = (const char *) l->data;
new_cluepacket = g_strconcat (cluepacket, clue, NULL);
g_free (cluepacket);
cluepacket = new_cluepacket;
}
new_cluepacket = g_strconcat (cluepacket, "</CluePacket>\n", NULL);
g_free (cluepacket);
cluepacket = new_cluepacket;
return cluepacket;
}
static char *
dashboard_build_cluepacket_v (const char *frontend,
gboolean focused,
const char *context,
va_list args)
{
char *cluep;
GList *clue_list;
char *retval;
g_return_val_if_fail (frontend != NULL, NULL);
cluep = va_arg (args, char *);
clue_list = NULL;
while (cluep) {
clue_list = g_list_append (clue_list, cluep);
cluep = va_arg (args, char *);
}
retval = dashboard_build_cluepacket_from_cluelist (frontend, focused, context, clue_list);
g_list_free (clue_list);
return retval;
}
static char *
dashboard_build_cluepacket_then_free_clues (const char *frontend,
gboolean focused,
const char *context,
...)
{
char *retval;
char *cluep;
va_list args;
g_return_val_if_fail (frontend != NULL, NULL);
/* Build the cluepacket */
va_start (args, context);
retval = dashboard_build_cluepacket_v (frontend, focused, context, args);
va_end (args);
/* Free the clues */
va_start (args, context);
cluep = va_arg (args, char *);
while (cluep) {
g_free (cluep);
cluep = va_arg (args, char *);
}
va_end (args);
return retval;
}
#endif /* ! __DASHBOARD_FRONTEND_H__ */
|