aboutsummaryrefslogtreecommitdiffstats
path: root/gg_map/gg_map.c
blob: 24f353621375fc2826fb2fbc076e7b8de0fc0d92 (plain) (blame)
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
#include <err.h>

#include <Elementary.h>
#include <Egraph.h>

#include <libglouglou.h>
#include <libggnet.h>

Evas_Object  *_mainwin;
Evas_Object  *_egraph;
struct ggnet *_ggnet;
struct event_base *_ev_base;

/* link between ecore loop and libevent loop */
static Eina_Bool
_cb_ecore_libevent(void *data) {
  if (event_base_got_exit(_ev_base))
    return EINA_FALSE;
  else
    event_base_loop(_ev_base, EVLOOP_NONBLOCK);
  return EINA_TRUE;
}

const char *ip_to_str(u_int ip)
{
  unsigned char bytes[4];
  static char buf[16];

  bytes[0] = ip & 0xFF;
  bytes[1] = (ip >> 8) & 0xFF;
  bytes[2] = (ip >> 16) & 0xFF;
  bytes[3] = (ip >> 24) & 0xFF;	
  snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
           bytes[3], bytes[2], bytes[1], bytes[0]);        
  return buf;
}

static Egraph_Vertice *
_node_to_vertice(struct ggnet_node *n)
{
  Egraph_Vertice *v;

  v = ggnet_node_usrdata_get(n);
  if (!v) {
    v = egraph_vertice_add(_egraph, ip_to_str(n->addr.s_addr), n);
    ggnet_node_usrdata_set(n, v);
  }
  return v;
}

static void
_conn_add(u_int id, u_int src, u_int dst, u_int proto, u_int8_t pktsize)
{
  struct ggnet_conn *conn;
  Egraph_Vertice *a, *b;
  Egraph_Edge *e;
  struct in_addr srcaddr, dstaddr;
  int size, response;

  GG_PKTDATA_SIZE_DECODE(pktsize, size, response);
  if (response > 0) /* cannot have a new connection that is a response */
    return;

  srcaddr.s_addr = src;
  dstaddr.s_addr = dst;

  conn = ggnet_conn_add(_ggnet, &srcaddr, -1, &dstaddr, -1, proto, size, id);
  a = _node_to_vertice(ggnet_conn_src_get(conn));
  b = _node_to_vertice(ggnet_conn_dst_get(conn));
  e = egraph_edge_find(_egraph, a, b);
  if (!e)
    e = egraph_edge_add(_egraph, a, b, conn);
  ggnet_conn_usrdata_set(conn, e);
}

static void
_conn_del(int id) {
  struct ggnet_conn *conn, *otherconn;
  struct ggnet_node *a, *b;
  Egraph_Edge *e;

  conn = ggnet_conn_find_by_id(_ggnet, id);
  if (conn) {
    a = ggnet_conn_src_get(conn);
    b = ggnet_conn_dst_get(conn);
    e = ggnet_conn_usrdata_get(conn);
    ggnet_conn_del(_ggnet, conn);
    /* is there other connections between these peers ? */
    otherconn = ggnet_conn_find_by_node(_ggnet, a, b);
    if (!otherconn) {
      // XXX lets keep the edges, igraph layouting behaves badly when you have
      // a vertice without edge ...
      egraph_edge_del(_egraph, e);
      //if (ggnet_node_is_connected(
    } else {
      printf("not last one\n");
    }
  } else {
    printf("does not exist !\n");
  }
}

static void
_conn_data(int id, u_int8_t pktsize) {
  struct ggnet_conn *conn;
  Egraph_Vertice *a, *b, *tmp;
  int size, response;
  u_int32_t color;

  conn = ggnet_conn_find_by_id(_ggnet, id);
  if (!conn)
    return;

  a = ggnet_node_usrdata_get(ggnet_conn_src_get(conn));
  b = ggnet_node_usrdata_get(ggnet_conn_dst_get(conn));

  GG_PKTDATA_SIZE_DECODE(pktsize, size, response);
  if (response) {
    tmp = a; a = b; b = tmp;
  }
  size = sqrt(size) / 2;
  color = ((id+1) * 0x98765400) % 0xFFFFFF00;
  egraph_vertice_send_blob(_egraph, a, b, size, color);
}

int
_cb_packet(struct gg_client *cli, struct gg_packet *pkt)
{
  switch(pkt->type) {
    case PACKET_NEWCONN:
      printf("  type PACKET_NEWCONN\n");
      printf("  newconn_id %d\n", pkt->newconn_id);
      printf("  newconn_src %4x\n", pkt->newconn_src);
      printf("  newconn_dst %4x\n", pkt->newconn_dst);
      printf("  newconn_proto %d\n", pkt->newconn_proto);
      printf("  newconn_size %d\n", pkt->newconn_size);

      _conn_del(pkt->newconn_id); /* in case we missed a previous del */
      _conn_add(pkt->newconn_id, pkt->newconn_src, pkt->newconn_dst,
                pkt->newconn_proto, pkt->newconn_size);
      break;

    case PACKET_DELCONN:
      printf("  type PACKET_DELCONN\n");
      printf("  delconn_id %d\n", pkt->delconn_id);
      
      _conn_del(pkt->delconn_id);
      break;

    case PACKET_DATA:
      //printf("  type PACKET_DATA\n");
      //printf("  data_connid %d\n", pkt->data_connid);
      //printf("  data_size %d\n", pkt->data_size);

      _conn_data(pkt->data_connid, pkt->data_size);
      break;

    case PACKET_NAME:
      printf("  type PACKET_NAME\n");
      printf("  name_addr %4x\n", pkt->name_addr);
      printf("  name_len %d\n", pkt->name_len);
      printf("  name_name_fqdn %s\n", pkt->name_fqdn);

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

  return 0;
}

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 *win, *bg, *panes, *bt, *egraph;
  Evas *evas;
  struct gg_client *ggcli;
  int retval = -1;

  win = elm_win_add(NULL, "panes", ELM_WIN_BASIC);
  evas = evas_object_evas_get(win);
  elm_win_title_set(win, "Glouglou Network Mapper");
  evas_object_smart_callback_add(win, "delete,request", _cb_on_done, NULL);

  bg = elm_bg_add(win);
  elm_win_resize_object_add(win, bg);
  evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  evas_object_color_set(bg, 0, 0, 0, 255);
  evas_object_show(bg);

  panes = elm_panes_add(win);
  elm_win_resize_object_add(win, panes);
  evas_object_size_hint_weight_set(panes, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  evas_object_size_hint_align_set(panes, EVAS_HINT_FILL, EVAS_HINT_FILL);
  evas_object_show(panes);

  bt = elm_button_add(win);
  elm_object_text_set(bt, "Quit");
  //evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  //evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
  evas_object_smart_callback_add(bt, "clicked", _cb_on_done, bt);
  evas_object_show(bt);
  elm_object_part_content_set(panes, "left", bt);

  egraph = egraph_new(evas, 1);
  if (!egraph)
    goto quit;
  egraph_layout_set(egraph, EGRAPH_LAYOUT_FRUCHTERMANREINGOLD); // XXX
  evas_object_size_hint_weight_set(egraph, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  evas_object_size_hint_align_set(egraph, EVAS_HINT_FILL, EVAS_HINT_FILL);
  evas_object_show(egraph);
  elm_object_part_content_set(panes, "right", egraph);

  evas_object_resize(win, 320, 400);
  evas_object_show(win);
  evas_object_show(win);

  _egraph = egraph;
  _mainwin = win;

  _ggnet = ggnet_new(GGNET_MANAGE_CONNID_FALSE);
  _ev_base = event_base_new();

  ggcli = gg_client_connect(_ev_base, "127.0.0.1", GLOUGLOU_ANALY_DEFAULT_PORT,
                            NULL, _cb_packet, NULL);
  if (!ggcli)
    goto quit;

  ecore_timer_add(0.1, _cb_ecore_libevent, NULL);

  elm_run();
  retval = 0;

quit:
  elm_shutdown();
  return retval;
}
ELM_MAIN()