aboutsummaryrefslogblamecommitdiffstats
path: root/gg_map/gg_net.c
blob: 646cfacc63490531ed8978c945bf6b042c70a6ec (plain) (tree)
1
2
3
4
5
6

                



                        





















































































































































                                                                                            
 


                                                                    


                                           
                   
             

        
                             

                                              
                                              




                   
                        





                                    
                                        








                                                       


                        

                         
                                        





                                      
            
                     


                                 
                                     





                                       
            
                     



                                  
                                     







                                            
            
   




                              
                                                           
 





                               
                                    

                         

                                                                                
 
                            
                                                                           
                                          

                       
                              
                                         
                                           

                        
                                



                                                      
                                 
                                   

                             
                                                                    
 

                                   
 
                             











                                                                                 
#include <err.h>

#include <Elementary.h>

#include <libglouglou.h>

struct conn {
  int id;
  struct node *nsrc;
  struct node *ndst;
  int proto;
  int size;
  char name[DNSNAME_MAX];
  Evas_Object *line;
};

struct node {
  char name[DNSNAME_MAX];
  u_int ip;
  char fqdn[DNSNAME_MAX];
  Evas_Object *btn;
};

Evas_Object *_mainwin, *_connbox;
Eina_List *_conns = NULL;
Eina_List *_nodes = NULL;

static struct conn *conn_add(u_int, struct node *, struct node *, u_int, u_int);
static void         conn_del(struct conn *);
static struct conn *conn_find(u_int);
static struct conn *conn_find_or_create(u_int, struct node *, struct node *, u_int, u_int);
static struct node *node_add(u_int);
static void         node_del(struct node *);
static struct node *node_find(u_int);
static struct node *node_find_or_create(u_int);
static void         node_update_name(struct node *, char *, int);

static struct conn *
conn_add(u_int id, struct node *nsrc, struct node *ndst, u_int proto, u_int size)
{
  struct conn *c;

  c = calloc(sizeof(struct conn), 1);
  if (!c)
    err(1, "calloc failed in conn_add\n");
  c->id = id;
  c->nsrc = nsrc;
  c->ndst = ndst;
  c->proto = proto;
  c->size = size;
  snprintf(c->name, sizeof(c->name), "%d: %x -> %x - %d",
      id, nsrc->ip, ndst->ip, proto);

  _conns = eina_list_append(_conns, c);

  // XXX c->line evas

  return c;
}

static void
conn_del(struct conn *c)
{
  if (c->line)
    evas_object_del(c->line);
  _conns = eina_list_prepend(_conns, c);
  free(c);
}

static struct conn *
conn_find(u_int id)
{
  struct conn *c;
  Eina_List *l;

  EINA_LIST_FOREACH(_conns, l, c)
    if (c->id == id)
      return c;
  return NULL;
}

static struct conn *
conn_find_or_create(u_int id, struct node *nsrc, struct node *ndst, u_int proto, u_int size)
{
  struct conn *c;

  c = conn_find(id);
  if (!c)
    c = conn_add(id, nsrc, ndst, proto, size);
  return c;
}

static struct node *
node_add(u_int ip)
{
  struct node *n;

  n = calloc(sizeof(struct node), 1);
  if (!n)
    err(1, "calloc failed in node_add\n");
  n->ip = ip;

  _nodes = eina_list_append(_nodes, n);

  n->btn = elm_button_add(_mainwin);
  node_update_name(n, NULL, 0);
  elm_box_pack_end(_connbox, n->btn);
  evas_object_show(n->btn);

  return n;
}

static void
node_del(struct node *n)
{
  if (n->btn)
    evas_object_del(n->btn);
  _nodes = eina_list_prepend(_nodes, n);
  free(n);
}

static struct node *
node_find(u_int ip)
{
  struct node *n;
  Eina_List *l;

  EINA_LIST_FOREACH(_nodes, l, n)
    if (n->ip == ip)
      return n;
  return NULL;
}

static struct node *
node_find_or_create(u_int ip)
{
  struct node *n;

  n = node_find(ip);
  if (!n)
    n = node_add(ip);
  return n;
}

static void
node_update_name(struct node *n, char *name, int len)
{
  if (!name) {
    snprintf(n->name, sizeof(n->name), "%x", n->ip);
  } else {
    if (len > sizeof(n->name))
      len = sizeof(n->name);
    strncpy(n->name, name, len);
  }
  elm_object_text_set(n->btn, n->name);
}

static Eina_Bool
_cb_conn_data(void *data, int type, Ecore_Con_Event_Server_Data *ev)
{
  u_int id, ip, src, dst, proto, size, len;
  struct node *n, *nsrc, *ndst;
  struct conn *c;
  struct packet *p;
  char *fqdn;
  int i;

  printf("data received:\n");
  for (i=0; i < ev->size; i++){
    printf("%2.2x ", ((u_char *)ev->data)[i]);
    if( (i%15 == 0 && i!=0) || i==ev->size-1 )
      printf("\n");
  }

  p = ev->data;
  switch(p->type) {
    case PACKET_NEWCONN:
      id = p->newconn_id;
      src = ntohl(p->newconn_src);
      dst = ntohl(p->newconn_dst);
      proto = p->newconn_proto;
      size = ntohs(p->newconn_size);

      printf("  type PACKET_NEWCONN\n");
      printf("  newconn_id %d\n", id);
      printf("  newconn_src %4x\n", src);
      printf("  newconn_dst %4x\n", dst);
      printf("  newconn_proto %d\n", proto);
      printf("  newconn_size %d\n", size);

      nsrc = node_find_or_create(src);
      ndst = node_find_or_create(dst);
      conn_find_or_create(id, nsrc, ndst, proto, size);

      break;
    case PACKET_DELCONN:
      id = p->delconn_id;

      printf("  type PACKET_DELCONN\n");
      printf("  delconn_id %d\n", id);

      n = node_find(id);
      if (n)
        node_del(n);

      break;
    case PACKET_DATA:
      id = p->data_connid;
      size = ntohs(p->data_size);

      printf("  type PACKET_DATA\n");
      printf("  data_connid %d\n", id);
      printf("  data_size %d\n", size);

      c = conn_find(id);
      // XXX evas_point on conn line

      break;
    case PACKET_NAME:
      ip = ntohl(p->name_addr);
      len = p->name_len;
      fqdn = (char *)p->name_fqdn;

      printf("  type PACKET_NAME\n");
      printf("  name_addr %4x\n", ip);
      printf("  name_len %d\n", len);
      printf("  name_name_fqdn %s\n", fqdn);

      n = node_find(ip);
      if (n)
        node_update_name(n, fqdn, len);

      break;
  }

  return ECORE_CALLBACK_RENEW;
}

static void
_cb_on_done(void *data, Evas_Object *obj, void *event_info)
{
  elm_exit();
}

EAPI_MAIN int
elm_main(int argc, char **argv)
{
  Evas_Object *box, *lab, *btn, *bg;
  Ecore_Con_Server *conn;

  _mainwin = elm_win_util_standard_add("hello", "Hello");
  evas_object_smart_callback_add(_mainwin, "delete,request", _cb_on_done, NULL);

  bg = elm_bg_add(_mainwin);
  evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  elm_win_resize_object_add(_mainwin, bg);
  evas_object_show(bg);

  box = elm_box_add(_mainwin);
  elm_box_horizontal_set(box, EINA_TRUE);
  elm_win_resize_object_add(_mainwin, box);
  evas_object_show(box);

  lab = elm_label_add(_mainwin);
  elm_object_text_set(lab, "Welcome to glouglou_efl");
  elm_box_pack_end(box, lab);
  evas_object_show(lab);

  btn = elm_button_add(_mainwin);
  elm_object_text_set(btn, "Quit");
  elm_box_pack_end(box, btn);
  evas_object_show(btn);
  evas_object_smart_callback_add(btn, "clicked", _cb_on_done, NULL);

  _connbox = elm_box_add(_mainwin);
  evas_object_show(_connbox);

  evas_object_show(_mainwin);

  ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA,
    (Ecore_Event_Handler_Cb)_cb_conn_data, NULL);
  conn = ecore_con_server_connect(ECORE_CON_REMOTE_UDP, "127.0.0.1", 4430, NULL);
  ecore_con_server_send(conn, "hello", 5);
  ecore_con_server_flush(conn);

  elm_run();
  elm_shutdown();
  return 0;
}
ELM_MAIN()