"Fossies" - the Fresh Open Source Software Archive

Member "pktstat-1.8.5/tcp_x11.c" (11 Nov 2006, 3307 Bytes) of package /linux/privat/old/pktstat-1.8.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* David Leonard, 2002. Public domain. */
    2 /* $Id: tcp_x11.c 1172 2006-11-11 13:32:14Z d $ */
    3 
    4 #if HAVE_CONFIG_H
    5 # include "config.h"
    6 #endif
    7 
    8 #if STDC_HEADERS
    9 # include <stdio.h>
   10 # include <string.h>
   11 # include <stdlib.h>
   12 #endif
   13 
   14 #if HAVE_SYS_TYPES_H
   15 # include <sys/types.h>
   16 #endif
   17 #if TIME_WITH_SYS_TIME
   18 # include <sys/time.h>
   19 # include <time.h>
   20 #else
   21 # if HAVE_SYS_TIME_H
   22 #  include <sys/time.h>
   23 # else
   24 #  include <time.h>
   25 # endif
   26 #endif
   27 
   28 #include "compat.h"
   29 #include "flow.h"
   30 #include "tcp.h"
   31 
   32 /* From Xproto.h: */
   33 
   34 struct xConnClientPrefix {
   35     u_int8_t    byteOrder;
   36 #define BE  'B'
   37 #define LE  'l'
   38     u_int8_t    pad;
   39     u_int16_t   majorVersion, minorVersion;
   40     u_int16_t   nbytesAuthProto;
   41     u_int16_t   nbytesAuthString;
   42     u_int16_t   pad2;
   43 };
   44 
   45 #define U16(v)  (state->byteorder == BE ? betoh16(v) : letoh16(v))
   46 #define U32(v)  (state->byteorder == BE ? betoh32(v) : letoh32(v))
   47 
   48 struct xReq {
   49     u_int8_t    reqType;
   50 #define X_ChangeProperty 18 
   51     u_int8_t    data0;
   52     u_int16_t   length; /* number of 32-bit words */
   53 };
   54 
   55 struct xChangePropertyReq {
   56     u_int8_t    reqType;
   57     u_int8_t    mode;
   58     u_int16_t   length; /* number of 32-bit words */
   59     u_int32_t   window;
   60     u_int32_t   property;
   61 #define XA_WM_COMMAND   34
   62 #define XA_WM_NAME  39
   63 #define XA_WM_CLASS 67
   64     u_int32_t   type;
   65 #define XA_STRING   31
   66     u_int8_t    format; /* bits per element */
   67     u_int8_t    pad[3];
   68     u_int32_t   nUnits;
   69 };
   70 
   71 struct x11state {
   72     char    byteorder;
   73     int goodness;
   74 };
   75 
   76 /*
   77  * Watch the X11 protocol carefully and look for string atom changes.
   78  * Typically the WM_NAME, WM_CLASS or WM_COMMAND atoms contain a good
   79  * description of the activity that's causing the traffic.
   80  */
   81 void
   82 tcp_x11(f, data, end)
   83     struct flow *f;
   84     const char *data;
   85     const char *end;
   86 {
   87     struct x11state *state;
   88 
   89     if (data == end)
   90         return;
   91 
   92     state = (struct x11state *)f->udata;
   93 
   94     if (state == NULL) {
   95         const struct xConnClientPrefix *p;
   96         state = (struct x11state *)malloc(sizeof (struct x11state));
   97         if (state == NULL)
   98             errx(1, "malloc");
   99         f->udata = state;
  100         f->freeudata = free;
  101         p = (const struct xConnClientPrefix *)data;
  102         state->byteorder = p->byteOrder;
  103         state->goodness = 0;
  104         /* Unlikely to have the WM props in the first pkt */
  105         return;
  106     }
  107 
  108     while (data + sizeof (struct xReq) <= end) {
  109         const struct xReq *xr = (const struct xReq *)data;
  110         const struct xChangePropertyReq *chp =
  111             (const struct xChangePropertyReq *)data;
  112         const char *nextdata = data + 4 * U16(xr->length);
  113 
  114         /* 
  115          * Look for a change to a WM_NAME or WM_COMMAND atom
  116          */
  117         if (xr->reqType == X_ChangeProperty
  118             && data + sizeof *chp < end
  119             && U32(chp->type) == XA_STRING
  120             && chp->format == 8)
  121         {
  122             const char *cmd = data + sizeof *chp;
  123             int i, len = U32(chp->nUnits);
  124             int goodness;
  125             u_int32_t property = U32(chp->property);
  126 
  127             /* We prefer some properties over others */
  128             goodness = 
  129                 property == XA_WM_COMMAND ? 2 :
  130                 property == XA_WM_NAME ? 1 : 
  131                 property == XA_WM_CLASS ? 0 : 
  132                 -1;
  133 
  134             if (goodness > state->goodness)
  135             {
  136                 if (len > 0 && cmd[len-1] == '\0')
  137                 len--;
  138                 for (i = 0; i < sizeof f->desc - 1 && i < len; i++)
  139                 if (cmd[i])
  140                     f->desc[i] = cmd[i];
  141                 else
  142                     f->desc[i] = ' ';
  143                 f->desc[i] = '\0';
  144                 state->goodness = goodness;
  145             }
  146         }
  147 
  148         if (nextdata <= data)
  149             break;          /* hmm, avoid going backwards */
  150         data = nextdata;
  151     }
  152 }