Пример #1
0
void text_connections()
{   
   void *list;
   char *desc;
   
   SAFE_CALLOC(desc, 100, sizeof(char));

   /* retrieve the first element */
   list = conntrack_print(0, NULL, NULL, 0);
   
   fprintf(stdout, "\nConnections list:\n\n");
  
   /* walk the connection list */
   while(list) {
      /* get the next element */
      list = conntrack_print(+1, list, &desc, 99);
      fprintf(stdout, "%s\n", desc);
   }

   fprintf(stdout, "\n");

   SAFE_FREE(desc);
}
Пример #2
0
/* for keeping the connection list in sync with the conntrack list */
static gboolean refresh_connections(gpointer data)
{
   struct row_pairs *lastconn = NULL, *cache = NULL;
   GtkTreeModel *model = GTK_TREE_MODEL (ls_conns);
   void *list, *next, *listend;
   char *desc;                  /* holds line from conntrack_print */
   GtkTreeIter iter;            /* points to a specific row */
   char flags[2], status[8];
   unsigned int xferred = 0;
   struct row_pairs *row = NULL, *nextrow = NULL, top, bottom;

   /* null terminate strings */
   flags[1] = 0;
   status[7] = 0;

   /* make sure the list has been created and window is visible */
   if(ls_conns) {
      if (!GTK_WIDGET_VISIBLE(conns_window))
         return(FALSE);
   } else {
      /* Columns:   Flags, Host, Port, "-",   Host, Port,
                    Proto, State, Bytes, (hidden) pointer */
      ls_conns = gtk_list_store_new (10, 
                    G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, 
                    G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, 
                    G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, 
                    G_TYPE_POINTER);
      connections = NULL;
   }

   /* remove old connections */
   for(row = connections; row; row = nextrow) {
       nextrow = row->next;
       if(conntrack_print(0, row->conn, NULL, 0) == NULL) {
          /* remove row from the GTK list */
          gtk_list_store_remove(GTK_LIST_STORE(ls_conns), &row->iter);

          /* remove pointers from the linked-list and free */
          if(row->next)
              row->next->prev = row->prev;

          if(row->prev)
              row->prev->next = row->next;
          else
              connections = row->next;
          free(row);
          row = NULL;
       }
       if(row)
           lastconn = row;
   }

   /* make sure we have a place to start searching for new rows */
   if(!lastconn) {
      listend = conntrack_print(0, NULL, NULL, 0);
      if(listend == NULL)
         return(TRUE);
   } else {
      listend = lastconn->conn;
   }

   /* allocate space for conntrack_print to pass connection data */
   SAFE_CALLOC(desc, 100, sizeof(char));

   /* add new connections */
   for(list = conntrack_print(+1, listend, NULL, 0); list; list = next) {
      next = conntrack_print(+1, list, &desc, 99);
      cache = gtkui_connections_add(desc, list, &connections);
      if(cache)
         lastconn = cache;
   }

   /* find the first and last visible rows */
   gtkui_connection_list_row(1, &top);
   gtkui_connection_list_row(0, &bottom);

   if(top.conn == NULL) 
      return(TRUE);

   iter = top.iter; /* copy iter by value */

   /* update visible part of list */
   do {
      /* get the conntrack pointer for this row */
      gtk_tree_model_get (model, &iter, 9, &list, -1);
      conntrack_print(0, list, &desc, 99);

      /* extract changing values from conntrack_print string */
      flags[0] = desc[0];
      strncpy(status, desc+50, 7);
      int i =sscanf(desc+62, "%u", &xferred);
      BUG_IF(i!=1);  

      gtk_list_store_set (ls_conns, &iter, 0, flags, 7, status, 8, xferred, -1);

      /* when we reach the bottom of the visible part, stop updating */
      if(bottom.conn == list)
         break;
   } while(gtk_tree_model_iter_next(model, &iter));
  
   SAFE_FREE(desc); 
   return(TRUE);
}