From 29193b6c4c13d885681c42a87632cf65fbf5b70d Mon Sep 17 00:00:00 2001 From: Laurent Ghigonis Date: Wed, 5 Jun 2013 18:14:30 +0200 Subject: convert to tabs, and fix some indent manualy no functionnal change --- gg_map/gg_map.c | 752 ++++++++++++++--------------- gg_map/glouglou_localsniff.sh | 16 +- gg_sniff/gg_sniff.c | 70 +-- gg_sniff/gg_sniff.h | 2 +- gg_sniff/pcap.c | 968 +++++++++++++++++++------------------- gg_trackproc/gg_trackproc.c | 491 +++++++++---------- glougloud/glougloud.c | 40 +- libglouglou/Makefile | 2 +- libglouglou/examples/dnsreverse.c | 48 +- libglouglou/libggnet.c | 452 +++++++++--------- libglouglou/libggnet.h | 132 +++--- libglouglou/libggnet_dns.c | 150 +++--- libglouglou/libggnet_dns.h | 36 +- libglouglou/libglouglou.c | 945 ++++++++++++++++++------------------- libglouglou/libglouglou.h | 175 +++---- libglouglou/sendbuf.c | 140 +++--- libglouglou/sendbuf.h | 36 +- libglouglou/tests/connect.c | 52 +- libglouglou/tests/sendrecv.c | 81 ++-- libglouglou/utils.c | 114 ++--- 20 files changed, 2359 insertions(+), 2343 deletions(-) diff --git a/gg_map/gg_map.c b/gg_map/gg_map.c index 48c68f0..a73042f 100644 --- a/gg_map/gg_map.c +++ b/gg_map/gg_map.c @@ -22,480 +22,480 @@ usage(void) { extern char *__progname; - fprintf(stderr, "usage: %s [-hv] [ip [port]]\n", __progname); - exit(1); + fprintf(stderr, "usage: %s [-hv] [ip [port]]\n", __progname); + exit(1); } /* 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; + 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; + 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 void _cb_ggnet_addgroup(struct ggnet *net, struct ggnet_nodegroup *group, - struct ggnet_nodegroup *parent) + struct ggnet_nodegroup *parent) { - Egraph_Vertice *vgroup; + Egraph_Vertice *vgroup; - vgroup = egraph_group_add(_egraph, ip_to_str(group->addr.s_addr), group); - ggnet_nodegroup_usrdata_set(group, vgroup); - if (parent) - egraph_group_vertice_attach(_egraph, vgroup, - ggnet_nodegroup_usrdata_get(parent)); + vgroup = egraph_group_add(_egraph, ip_to_str(group->addr.s_addr), group); + ggnet_nodegroup_usrdata_set(group, vgroup); + if (parent) + egraph_group_vertice_attach(_egraph, vgroup, + ggnet_nodegroup_usrdata_get(parent)); } static void _cb_ggnet_delgroup(struct ggnet *net, struct ggnet_nodegroup *group) { - Egraph_Vertice *vgroup; + Egraph_Vertice *vgroup; - vgroup = ggnet_nodegroup_usrdata_get(group); - egraph_vertice_del(_egraph, vgroup); + vgroup = ggnet_nodegroup_usrdata_get(group); + egraph_vertice_del(_egraph, vgroup); } static Egraph_Vertice * _node_to_vertice(struct ggnet_node *n) { - Egraph_Vertice *v; - struct ggnet_nodegroup *group; - - 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); - group = ggnet_node_group_get(n); - if (group) - egraph_group_vertice_attach(_egraph, v, - ggnet_nodegroup_usrdata_get(group)); - } - return v; + Egraph_Vertice *v; + struct ggnet_nodegroup *group; + + 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); + group = ggnet_node_group_get(n); + if (group) + egraph_group_vertice_attach(_egraph, v, + ggnet_nodegroup_usrdata_get(group)); + } + return v; } static void _node_attach_parentgroup(struct ggnet_node *n) { - struct ggnet_nodegroup *g; - Egraph_Vertice *vg, *vparentg; - - g = ggnet_node_group_get(n); - if (!g || !g->parent) - return; - vg = ggnet_nodegroup_usrdata_get(g); - vparentg = ggnet_nodegroup_usrdata_get(g->parent); - egraph_group_vertice_attach(_egraph, vg, vparentg); + struct ggnet_nodegroup *g; + Egraph_Vertice *vg, *vparentg; + + g = ggnet_node_group_get(n); + if (!g || !g->parent) + return; + vg = ggnet_nodegroup_usrdata_get(g); + vparentg = ggnet_nodegroup_usrdata_get(g->parent); + egraph_group_vertice_attach(_egraph, vg, vparentg); } static void _node_detach_parentgroup(struct ggnet_node *n) { - struct ggnet_nodegroup *g; - Egraph_Vertice *vg, *vparentg; - - g = ggnet_node_group_get(n); - if (!g || !g->parent) - return; - vg = ggnet_nodegroup_usrdata_get(g); - vparentg = ggnet_nodegroup_usrdata_get(g->parent); - egraph_group_vertice_detach(_egraph, vg, vparentg); + struct ggnet_nodegroup *g; + Egraph_Vertice *vg, *vparentg; + + g = ggnet_node_group_get(n); + if (!g || !g->parent) + return; + vg = ggnet_nodegroup_usrdata_get(g); + vparentg = ggnet_nodegroup_usrdata_get(g->parent); + egraph_group_vertice_detach(_egraph, vg, vparentg); } 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_Edge *e; - struct ggnet_node *a, *b; - Egraph_Vertice *va, *vb; - struct in_addr srcaddr, dstaddr; - int size, response; - - GG_PKTDATA_SIZE_DECODE(pktsize, size, response); - if (_loglevel >= 2) - printf("_conn_add\n"); - 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 = ggnet_conn_src_get(conn); - b = ggnet_conn_dst_get(conn); - va = _node_to_vertice(a); - vb = _node_to_vertice(b); - e = egraph_edge_find(_egraph, va, vb); - if (_loglevel >= 2) - printf("_conn_add: a %d b %d e %x id %d\n", va->id, vb->id, e, id); - if (!e) { - if (a->group && a->group->conn_count == 1) - _node_detach_parentgroup(a); - if (b->group && b->group->conn_count == 1) - _node_detach_parentgroup(b); - e = egraph_edge_add(_egraph, va, vb, conn); - if (_loglevel >= 2) - printf("_conn_add: egraph edge added %x\n", e); - } - ggnet_conn_usrdata_set(conn, e); + struct ggnet_conn *conn; + Egraph_Edge *e; + struct ggnet_node *a, *b; + Egraph_Vertice *va, *vb; + struct in_addr srcaddr, dstaddr; + int size, response; + + GG_PKTDATA_SIZE_DECODE(pktsize, size, response); + if (_loglevel >= 2) + printf("_conn_add\n"); + 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 = ggnet_conn_src_get(conn); + b = ggnet_conn_dst_get(conn); + va = _node_to_vertice(a); + vb = _node_to_vertice(b); + e = egraph_edge_find(_egraph, va, vb); + if (_loglevel >= 2) + printf("_conn_add: a %d b %d e %x id %d\n", va->id, vb->id, e, id); + if (!e) { + if (a->group && a->group->conn_count == 1) + _node_detach_parentgroup(a); + if (b->group && b->group->conn_count == 1) + _node_detach_parentgroup(b); + e = egraph_edge_add(_egraph, va, vb, conn); + if (_loglevel >= 2) + printf("_conn_add: egraph edge added %x\n", e); + } + 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); - if (_loglevel >= 2) - printf("_conn_del: conn id %d\n", id); // XXX - 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 ... - if (_loglevel >= 2) - printf("_conn_del: edge del %x\n", e); // XXX - egraph_edge_del(_egraph, e); - if (a->group && a->group->conn_count == 0) - _node_attach_parentgroup(a); - if (b->group && b->group->conn_count == 0) - _node_attach_parentgroup(b); - } else { - if (_loglevel >= 2) - printf("_conn_del: not last one, edge %x *not* deleted\n", e); - } - } else { - if (_loglevel >= 2) - printf("_conn_del: does not exist !\n"); - } + 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); + if (_loglevel >= 2) + printf("_conn_del: conn id %d\n", id); // XXX + 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 ... + if (_loglevel >= 2) + printf("_conn_del: edge del %x\n", e); // XXX + egraph_edge_del(_egraph, e); + if (a->group && a->group->conn_count == 0) + _node_attach_parentgroup(a); + if (b->group && b->group->conn_count == 0) + _node_attach_parentgroup(b); + } else { + if (_loglevel >= 2) + printf("_conn_del: not last one, edge %x *not* deleted\n", e); + } + } else { + if (_loglevel >= 2) + printf("_conn_del: 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); + 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); } static void _conn_name(u_int32_t addr, u_int8_t pktsize, u_char *fqdn) { - struct ggnet_node *n; - struct in_addr ip; + struct ggnet_node *n; + struct in_addr ip; - ip.s_addr = addr; - n = ggnet_node_find(_ggnet, &ip); - if (!n) - return; + ip.s_addr = addr; + n = ggnet_node_find(_ggnet, &ip); + if (!n) + return; - egraph_vertice_rename(_egraph, ggnet_node_usrdata_get(n), (char *)fqdn); + egraph_vertice_rename(_egraph, ggnet_node_usrdata_get(n), (char *)fqdn); } -int + int _cb_packet(struct gg_client *cli, struct gg_packet *pkt) { - switch(pkt->type) { - case PACKET_NEWCONN: - if (_loglevel >= 1) { - 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: - if (_loglevel >= 1) { - printf(" type PACKET_DELCONN\n"); - printf(" delconn_id %d\n", pkt->delconn_id); - } - - _conn_del(pkt->delconn_id); - break; - - case PACKET_DATA: - if (_loglevel >= 1) { - //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: - if (_loglevel >= 1) { - 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); - } - - _conn_name(pkt->name_addr, pkt->name_len, pkt->name_fqdn); - break; - } - - return 0; + switch(pkt->type) { + case PACKET_NEWCONN: + if (_loglevel >= 1) { + 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: + if (_loglevel >= 1) { + printf(" type PACKET_DELCONN\n"); + printf(" delconn_id %d\n", pkt->delconn_id); + } + + _conn_del(pkt->delconn_id); + break; + + case PACKET_DATA: + if (_loglevel >= 1) { + //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: + if (_loglevel >= 1) { + 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); + } + + _conn_name(pkt->name_addr, pkt->name_len, pkt->name_fqdn); + break; + } + + return 0; } /* static void -_cb_del_edges(void *data, Evas_Object *obj, void *event_info) -{ - if (!_egraph) - return; + _cb_del_edges(void *data, Evas_Object *obj, void *event_info) + { + if (!_egraph) + return; - // XXX ggnet_clear(_ggnet); - egraph_clear(_egraph); +// XXX ggnet_clear(_ggnet); +egraph_clear(_egraph); } */ static void _cb_show_nodes(void *data, Evas_Object *obj, void *event_info) { - egraph_display_vertices_set(_egraph, elm_check_state_get(obj)); + egraph_display_vertices_set(_egraph, elm_check_state_get(obj)); } static void _cb_show_labels(void *data, Evas_Object *obj, void *event_info) { - if (!_egraph) - return; + if (!_egraph) + return; - egraph_display_names_set(_egraph, elm_check_state_get(obj)); + egraph_display_names_set(_egraph, elm_check_state_get(obj)); } static void _cb_show_edges(void *data, Evas_Object *obj, void *event_info) { - if (!_egraph) - return; + if (!_egraph) + return; - egraph_display_edges_set(_egraph, elm_check_state_get(obj)); + egraph_display_edges_set(_egraph, elm_check_state_get(obj)); } static void _cb_layout_changed(void *data, Evas_Object *obj, void *event_info) { - Elm_Object_Item *it; - char *selected; - int layout; - - if (!_egraph) - return; - - it = event_info; - selected = elm_object_item_text_get(it); - layout = EGRAPH_LAYOUT_DEFAULT; - if (!strcmp(selected, "Kamada K.")) - layout = EGRAPH_LAYOUT_KAMADAKAWAI; - else if (!strcmp(selected, "GraphOpt")) - layout = EGRAPH_LAYOUT_GRAPHOPT; - else if (!strcmp(selected, "Fruchterman R.")) - layout = EGRAPH_LAYOUT_FRUCHTERMANREINGOLD; - - egraph_layout_set(_egraph, layout); + Elm_Object_Item *it; + char *selected; + int layout; + + if (!_egraph) + return; + + it = event_info; + selected = elm_object_item_text_get(it); + layout = EGRAPH_LAYOUT_DEFAULT; + if (!strcmp(selected, "Kamada K.")) + layout = EGRAPH_LAYOUT_KAMADAKAWAI; + else if (!strcmp(selected, "GraphOpt")) + layout = EGRAPH_LAYOUT_GRAPHOPT; + else if (!strcmp(selected, "Fruchterman R.")) + layout = EGRAPH_LAYOUT_FRUCHTERMANREINGOLD; + + egraph_layout_set(_egraph, layout); } static void _cb_on_done(void *data, Evas_Object *obj, void *event_info) { - elm_exit(); + elm_exit(); } EAPI_MAIN int elm_main(int argc, char **argv) { - Evas_Object *win, *bg, *egraph, *panes; - Evas_Object *bx, *bx2, *ck, *sc, *seg_it, *lb; - Evas *evas; - struct gg_client *ggcli; - char gg_serv_ip[30] = "127.0.0.1"; - int gg_serv_port = GLOUGLOU_ANALY_DEFAULT_PORT; - int retval = -1; - int op; - - while ((op = getopt(argc, argv, "hv")) != -1) { - switch (op) { - case 'h': - usage(); - /* NOTREACHED */ - case 'v': - _loglevel++; - break; - default: - usage(); - /* NOTREACHED */ - } - } - switch (argc - optind) { - case 2: gg_serv_port = atoi(argv[3]); - case 1: strncpy(gg_serv_ip, argv[2], sizeof(gg_serv_ip)); - case 0: - break; - default: - usage(); - /* NOTREACHED */ - } - - win = elm_win_add(NULL, "panes", ELM_WIN_BASIC); - evas = evas_object_evas_get(win); - elm_win_title_set(win, "Glouglou Network Map"); - 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); - - bx = elm_box_add(win); - elm_box_horizontal_set(bx, EINA_TRUE); - evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); - elm_win_resize_object_add(win, bx); - evas_object_show(bx); - - panes = elm_panes_add(win); - 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); - elm_panes_content_right_size_set(panes, 0.0); - elm_box_pack_end(bx, panes); - evas_object_show(panes); - - egraph = egraph_new(evas, 1); - egraph_layout_set(egraph, EGRAPH_LAYOUT_KAMADAKAWAI); - if (!egraph) - goto quit; - 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, "left", egraph); - - bx2 = elm_box_add(win); - //elm_box_horizontal_set(bx, EINA_FALSE); - evas_object_size_hint_weight_set(bx2, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); - evas_object_size_hint_align_set(bx2, EVAS_HINT_FILL, EVAS_HINT_FILL); - evas_object_color_set(bx2, 255, 255, 255, 100); // XXX broken - evas_object_show(bx2); - - lb = elm_label_add(win); - elm_object_style_set(lb, "marker"); - evas_object_color_set(lb, 255, 255, 255, 255); - elm_object_text_set(lb, - "Glouglou Network Mapper
" - "
" - "Enjoy !
"); - evas_object_show(lb); - elm_box_pack_end(bx2, lb); - - lb = elm_label_add(win); - elm_object_text_set(lb, "Layout"); - evas_object_size_hint_weight_set(lb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); - evas_object_size_hint_align_set(lb, EVAS_HINT_FILL, EVAS_HINT_FILL); - elm_box_pack_end(bx2, lb); - sc = elm_segment_control_add(win); - //evas_object_size_hint_weight_set(sc, em->weight.w, em->weight.h); - //evas_object_size_hint_align_set(sc, em->align.x, em->align.y); - seg_it = elm_segment_control_item_add(sc, NULL, "GraphOpt"); - elm_segment_control_item_selected_set(seg_it, EINA_FALSE); - seg_it = elm_segment_control_item_add(sc, NULL, "Kamada K."); - elm_segment_control_item_selected_set(seg_it, EINA_TRUE); - seg_it = elm_segment_control_item_add(sc, NULL, "Fruchterman R."); - elm_segment_control_item_selected_set(seg_it, EINA_FALSE); - evas_object_smart_callback_add(sc, "changed", _cb_layout_changed, NULL); - evas_object_show(sc); - elm_box_pack_end(bx2, sc); - ck = elm_check_add(win); - elm_object_text_set(ck, "Show Node"); - elm_check_state_set(ck, EINA_TRUE); - evas_object_show(ck); - evas_object_smart_callback_add(ck, "changed", _cb_show_nodes, NULL); - elm_box_pack_end(bx2, ck); - ck = elm_check_add(win); - elm_object_text_set(ck, "Show Label"); - elm_check_state_set(ck, EINA_TRUE); - evas_object_show(ck); - evas_object_smart_callback_add(ck, "changed", _cb_show_labels, NULL); - elm_box_pack_end(bx2, ck); - ck = elm_check_add(win); - elm_object_text_set(ck, "Show Edges"); - elm_check_state_set(ck, EINA_TRUE); - evas_object_show(ck); - evas_object_smart_callback_add(ck, "changed", _cb_show_edges, NULL); - elm_box_pack_end(bx2, ck); - - elm_object_part_content_set(panes, "right", bx2); - - evas_object_resize(win, 150, 150); // XXX workaround elm sizing issue - evas_object_show(win); - evas_object_resize(win, 1300, 715); - evas_object_show(win); - _egraph = egraph; - _mainwin = win; - - _ggnet = ggnet_new(GGNET_MANAGE_CONNID_FALSE); - if (!_ggnet) - goto quit; - ggnet_set_grouping(_ggnet, GGNET_GROUPING_TRUE, - _cb_ggnet_addgroup, _cb_ggnet_delgroup); - _ev_base = event_base_new(); - - ggcli = gg_client_connect(_ev_base, gg_serv_ip, gg_serv_port, - NULL, _cb_packet, NULL); - if (!ggcli) - goto quit; - - ecore_timer_add(0.1, _cb_ecore_libevent, NULL); - - elm_run(); - retval = 0; + Evas_Object *win, *bg, *egraph, *panes; + Evas_Object *bx, *bx2, *ck, *sc, *seg_it, *lb; + Evas *evas; + struct gg_client *ggcli; + char gg_serv_ip[30] = "127.0.0.1"; + int gg_serv_port = GLOUGLOU_ANALY_DEFAULT_PORT; + int retval = -1; + int op; + + while ((op = getopt(argc, argv, "hv")) != -1) { + switch (op) { + case 'h': + usage(); + /* NOTREACHED */ + case 'v': + _loglevel++; + break; + default: + usage(); + /* NOTREACHED */ + } + } + switch (argc - optind) { + case 2: gg_serv_port = atoi(argv[3]); + case 1: strncpy(gg_serv_ip, argv[2], sizeof(gg_serv_ip)); + case 0: + break; + default: + usage(); + /* NOTREACHED */ + } + + win = elm_win_add(NULL, "panes", ELM_WIN_BASIC); + evas = evas_object_evas_get(win); + elm_win_title_set(win, "Glouglou Network Map"); + 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); + + bx = elm_box_add(win); + elm_box_horizontal_set(bx, EINA_TRUE); + evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + elm_win_resize_object_add(win, bx); + evas_object_show(bx); + + panes = elm_panes_add(win); + 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); + elm_panes_content_right_size_set(panes, 0.0); + elm_box_pack_end(bx, panes); + evas_object_show(panes); + + egraph = egraph_new(evas, 1); + egraph_layout_set(egraph, EGRAPH_LAYOUT_KAMADAKAWAI); + if (!egraph) + goto quit; + 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, "left", egraph); + + bx2 = elm_box_add(win); + //elm_box_horizontal_set(bx, EINA_FALSE); + evas_object_size_hint_weight_set(bx2, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + evas_object_size_hint_align_set(bx2, EVAS_HINT_FILL, EVAS_HINT_FILL); + evas_object_color_set(bx2, 255, 255, 255, 100); // XXX broken + evas_object_show(bx2); + + lb = elm_label_add(win); + elm_object_style_set(lb, "marker"); + evas_object_color_set(lb, 255, 255, 255, 255); + elm_object_text_set(lb, + "Glouglou Network Mapper
" + "
" + "Enjoy !
"); + evas_object_show(lb); + elm_box_pack_end(bx2, lb); + + lb = elm_label_add(win); + elm_object_text_set(lb, "Layout"); + evas_object_size_hint_weight_set(lb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + evas_object_size_hint_align_set(lb, EVAS_HINT_FILL, EVAS_HINT_FILL); + elm_box_pack_end(bx2, lb); + sc = elm_segment_control_add(win); + //evas_object_size_hint_weight_set(sc, em->weight.w, em->weight.h); + //evas_object_size_hint_align_set(sc, em->align.x, em->align.y); + seg_it = elm_segment_control_item_add(sc, NULL, "GraphOpt"); + elm_segment_control_item_selected_set(seg_it, EINA_FALSE); + seg_it = elm_segment_control_item_add(sc, NULL, "Kamada K."); + elm_segment_control_item_selected_set(seg_it, EINA_TRUE); + seg_it = elm_segment_control_item_add(sc, NULL, "Fruchterman R."); + elm_segment_control_item_selected_set(seg_it, EINA_FALSE); + evas_object_smart_callback_add(sc, "changed", _cb_layout_changed, NULL); + evas_object_show(sc); + elm_box_pack_end(bx2, sc); + ck = elm_check_add(win); + elm_object_text_set(ck, "Show Node"); + elm_check_state_set(ck, EINA_TRUE); + evas_object_show(ck); + evas_object_smart_callback_add(ck, "changed", _cb_show_nodes, NULL); + elm_box_pack_end(bx2, ck); + ck = elm_check_add(win); + elm_object_text_set(ck, "Show Label"); + elm_check_state_set(ck, EINA_TRUE); + evas_object_show(ck); + evas_object_smart_callback_add(ck, "changed", _cb_show_labels, NULL); + elm_box_pack_end(bx2, ck); + ck = elm_check_add(win); + elm_object_text_set(ck, "Show Edges"); + elm_check_state_set(ck, EINA_TRUE); + evas_object_show(ck); + evas_object_smart_callback_add(ck, "changed", _cb_show_edges, NULL); + elm_box_pack_end(bx2, ck); + + elm_object_part_content_set(panes, "right", bx2); + + evas_object_resize(win, 150, 150); // XXX workaround elm sizing issue + evas_object_show(win); + evas_object_resize(win, 1300, 715); + evas_object_show(win); + _egraph = egraph; + _mainwin = win; + + _ggnet = ggnet_new(GGNET_MANAGE_CONNID_FALSE); + if (!_ggnet) + goto quit; + ggnet_set_grouping(_ggnet, GGNET_GROUPING_TRUE, + _cb_ggnet_addgroup, _cb_ggnet_delgroup); + _ev_base = event_base_new(); + + ggcli = gg_client_connect(_ev_base, gg_serv_ip, gg_serv_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_shutdown(); + return retval; } ELM_MAIN() diff --git a/gg_map/glouglou_localsniff.sh b/gg_map/glouglou_localsniff.sh index 75d9cdd..f4c1928 100755 --- a/gg_map/glouglou_localsniff.sh +++ b/gg_map/glouglou_localsniff.sh @@ -10,14 +10,14 @@ gg_map=`which gg_map` iface=${1-"eth0"} if [ -z "$glougloud" -o -z "$gg_sniff" -o -z "$gg_map" ]; then - echo "instalation incomplete (glougloud, gg_sniff, gg_map)" - exit 1 + echo "instalation incomplete (glougloud, gg_sniff, gg_map)" + exit 1 fi -tmux start \;\ - new-session -d -s glouglou \;\ - neww -n "glougloud" "sudo $glougloud -v; bash" \;\ - neww -n "gg_sniff" "sudo $gg_sniff -v -i $iface" \;\ - neww -n "gg_map" "gg_map" \;\ - attach-session -t glouglou +tmux start \;\ + new-session -d -s glouglou \;\ + neww -n "glougloud" "sudo $glougloud -v; bash" \;\ + neww -n "gg_sniff" "sudo $gg_sniff -v -i $iface" \;\ + neww -n "gg_map" "gg_map" \;\ + attach-session -t glouglou diff --git a/gg_sniff/gg_sniff.c b/gg_sniff/gg_sniff.c index 8ee6de1..f703615 100644 --- a/gg_sniff/gg_sniff.c +++ b/gg_sniff/gg_sniff.c @@ -39,8 +39,8 @@ usage(void) { extern char *__progname; - fprintf(stderr, "usage: %s [-hv] [-f filter] [-i interface] [ip [port]]\n", __progname); - exit(1); + fprintf(stderr, "usage: %s [-hv] [-f filter] [-i interface] [ip [port]]\n", __progname); + exit(1); } static void @@ -48,15 +48,15 @@ sig_handler(int sig, short why, void *data) { gg_log_info("got signal %d", sig); if (sig == SIGINT || sig == SIGTERM) - if (_ev_base) - event_base_loopexit(_ev_base, NULL); + if (_ev_base) + event_base_loopexit(_ev_base, NULL); } int main(int argc, char **argv) { - struct gg_client *ggcli = NULL; - struct ggnet *net = NULL; + struct gg_client *ggcli = NULL; + struct ggnet *net = NULL; struct event *ev_sigint, *ev_sigterm, *ev_sigchld, *ev_sighup; char ggserv_ip[30] = "127.0.0.1"; char *iface = NULL; @@ -73,18 +73,18 @@ main(int argc, char **argv) while ((op = getopt(argc, argv, "af:hi:v")) != -1) { switch (op) { - case 'a': - active = 1; - break; + case 'a': + active = 1; + break; case 'f': - filter = strndup(optarg, 256); - break; + filter = strndup(optarg, 256); + break; case 'h': usage(); /* NOTREACHED */ case 'i': - iface = strndup(optarg, 30); - break; + iface = strndup(optarg, 30); + break; case 'v': loglevel++; break; @@ -94,29 +94,29 @@ main(int argc, char **argv) } } switch (argc - optind) { - case 2: ggserv_port = atoi(argv[optind+1]); - case 1: strncpy(ggserv_ip, argv[optind], sizeof(ggserv_ip)); - case 0: - break; - default: - usage(); - /* NOTREACHED */ - } + case 2: ggserv_port = atoi(argv[optind+1]); + case 1: strncpy(ggserv_ip, argv[optind], sizeof(ggserv_ip)); + case 0: + break; + default: + usage(); + /* NOTREACHED */ + } gg_log_init(GG_SNIFF_LOGFILE, loglevel); - gg_log_warn("Starting gg_sniff"); + gg_log_warn("Starting gg_sniff"); _ev_base = event_base_new(); - net = ggnet_new(GGNET_MANAGE_CONNID_TRUE); - if (!net) - goto quit; + net = ggnet_new(GGNET_MANAGE_CONNID_TRUE); + if (!net) + goto quit; ggcli = gg_client_connect(_ev_base, ggserv_ip, ggserv_port, NULL, NULL, NULL); if (!ggcli) - goto quit; - pcap_init = ggsniff_pcap_init(_ev_base, ggcli, net, iface, active, filter); + goto quit; + pcap_init = ggsniff_pcap_init(_ev_base, ggcli, net, iface, active, filter); if (!pcap_init) - goto quit; + goto quit; ev_sigint = evsignal_new(_ev_base, SIGINT, sig_handler, NULL); ev_sigterm = evsignal_new(_ev_base, SIGTERM, sig_handler, NULL); @@ -135,13 +135,13 @@ main(int argc, char **argv) retval = 0; quit: - if (pcap_init) - ggsniff_pcap_shutdown(); - if (ggcli) - gg_client_disconnect(ggcli); - if (net) - ggnet_free(net); - + if (pcap_init) + ggsniff_pcap_shutdown(); + if (ggcli) + gg_client_disconnect(ggcli); + if (net) + ggnet_free(net); + gg_log_warn("exiting"); gg_log_shutdown(); diff --git a/gg_sniff/gg_sniff.h b/gg_sniff/gg_sniff.h index 5cbd1fd..0b8d85c 100644 --- a/gg_sniff/gg_sniff.h +++ b/gg_sniff/gg_sniff.h @@ -1,5 +1,5 @@ /* pcap.c */ int ggsniff_pcap_init(struct event_base *, struct gg_client *, - struct ggnet *, char *, int, char *); + struct ggnet *, char *, int, char *); void ggsniff_pcap_shutdown(void); diff --git a/gg_sniff/pcap.c b/gg_sniff/pcap.c index 6ec710b..2e31769 100644 --- a/gg_sniff/pcap.c +++ b/gg_sniff/pcap.c @@ -46,113 +46,113 @@ #define CONNTIMER 5 // XXX conf ? struct phandler { - pcap_handler f; - int type; + pcap_handler f; + int type; }; struct _cap_t { - pcap_t *pcap; - pcap_handler handler; - struct event *ev; - struct event *conntimer_ev; - struct timeval conntimer_tv; - struct gg_client *ggcli; - struct ggnet *net; - int pinvalid; - int ptruncated; + pcap_t *pcap; + pcap_handler handler; + struct event *ev; + struct event *conntimer_ev; + struct timeval conntimer_tv; + struct gg_client *ggcli; + struct ggnet *net; + int pinvalid; + int ptruncated; }; static pcap_t *my_pcap_open_live(const char *, int, int, int, - char *, u_int, u_int); + char *, u_int, u_int); static void ip_handle(struct ip *, const u_char *, u_int); static void conn_del(struct ggnet_conn *); static void ether_handle(struct ether_header *, const u_char *, u_int); static pcap_handler lookup_phandler(int); static void phandler_ether(u_char *, - const struct pcap_pkthdr *, const u_char *); + const struct pcap_pkthdr *, const u_char *); #if defined(__OpenBSD__) static void phandler_loop(u_char *, - const struct pcap_pkthdr *, const u_char *); + const struct pcap_pkthdr *, const u_char *); #endif #if defined(__linux__) static void phandler_sll(u_char *, - const struct pcap_pkthdr *, const u_char *); + const struct pcap_pkthdr *, const u_char *); #endif static void cb_pcap(int, short, void *); static void cb_conntimer(int, short, void *); static void cb_nodename(struct ggnet *, struct ggnet_node *); static struct phandler phandlers[] = { - { phandler_ether, DLT_EN10MB }, - { phandler_ether, DLT_IEEE802 }, + { phandler_ether, DLT_EN10MB }, + { phandler_ether, DLT_IEEE802 }, #if defined(__OpenBSD__) - { phandler_loop, DLT_LOOP }, + { phandler_loop, DLT_LOOP }, #endif #if defined(__linux__) - { phandler_sll, DLT_LINUX_SLL }, + { phandler_sll, DLT_LINUX_SLL }, #endif - { NULL, 0 }, + { NULL, 0 }, }; static struct _cap_t _cap; - int +int ggsniff_pcap_init(struct event_base *ev_base, struct gg_client *ggcli, - struct ggnet *net, char *iface, int active, char *filter) + struct ggnet *net, char *iface, int active, char *filter) { - char errbuf[PCAP_ERRBUF_SIZE]; - struct bpf_program bprog; - pcap_t *pcap; + char errbuf[PCAP_ERRBUF_SIZE]; + struct bpf_program bprog; + pcap_t *pcap; #if defined(__OpenBSD__) - if (!iface) - err(1, "On OpenBSD you cannot listen on ANY interface"); + if (!iface) + err(1, "On OpenBSD you cannot listen on ANY interface"); #endif - pcap = my_pcap_open_live(iface, PCAP_SNAPLEN, 1, PCAP_TO, - errbuf, -1, 0); - if (pcap == NULL) - err(1, "capture: pcap_open_live failed on interface %s\n" - "with snaplen %d : %s", - iface, PCAP_SNAPLEN, errbuf); - if (!filter) - filter = strndup(PCAP_FILTER, 256); - if (pcap_compile(pcap, &bprog, filter, 0, 0) < 0) - err(1, "capture: pcap_compile failed with filter %s : %s", - filter, pcap_geterr(pcap)); - if (pcap_setfilter(pcap, &bprog) < 0) - err(1, "capture: pcap_setfilter failed : %s", - pcap_geterr(pcap)); - - _cap.pcap = pcap; - _cap.handler = lookup_phandler(pcap_datalink(pcap)); - //_cap.tv.tv_sec = 0; - //_cap.tv.tv_usec = PCAP_TO; - fd_nonblock(pcap_fileno(pcap)); - _cap.ev = event_new(ev_base, pcap_fileno(pcap), EV_READ|EV_PERSIST, - cb_pcap, NULL); - //event_add(_cap.ev, &_cap->tv); - event_add(_cap.ev, NULL); - - _cap.conntimer_tv.tv_sec = CONNTIMER; - _cap.conntimer_tv.tv_usec = 0; - _cap.conntimer_ev = evtimer_new(ev_base, cb_conntimer, NULL); - if (evtimer_add(_cap.conntimer_ev, &_cap.conntimer_tv) == -1) - gg_log_fatal("user: event_add conntimer failed: %s", strerror(errno)); - - if (active) - ggnet_set_dns(net, 1, ev_base, cb_nodename); - - _cap.ggcli = ggcli; - _cap.net = net; - - return 1; + pcap = my_pcap_open_live(iface, PCAP_SNAPLEN, 1, PCAP_TO, + errbuf, -1, 0); + if (pcap == NULL) + err(1, "capture: pcap_open_live failed on interface %s\n" + "with snaplen %d : %s", + iface, PCAP_SNAPLEN, errbuf); + if (!filter) + filter = strndup(PCAP_FILTER, 256); + if (pcap_compile(pcap, &bprog, filter, 0, 0) < 0) + err(1, "capture: pcap_compile failed with filter %s : %s", + filter, pcap_geterr(pcap)); + if (pcap_setfilter(pcap, &bprog) < 0) + err(1, "capture: pcap_setfilter failed : %s", + pcap_geterr(pcap)); + + _cap.pcap = pcap; + _cap.handler = lookup_phandler(pcap_datalink(pcap)); + //_cap.tv.tv_sec = 0; + //_cap.tv.tv_usec = PCAP_TO; + fd_nonblock(pcap_fileno(pcap)); + _cap.ev = event_new(ev_base, pcap_fileno(pcap), EV_READ|EV_PERSIST, + cb_pcap, NULL); + //event_add(_cap.ev, &_cap->tv); + event_add(_cap.ev, NULL); + + _cap.conntimer_tv.tv_sec = CONNTIMER; + _cap.conntimer_tv.tv_usec = 0; + _cap.conntimer_ev = evtimer_new(ev_base, cb_conntimer, NULL); + if (evtimer_add(_cap.conntimer_ev, &_cap.conntimer_tv) == -1) + gg_log_fatal("user: event_add conntimer failed: %s", strerror(errno)); + + if (active) + ggnet_set_dns(net, 1, ev_base, cb_nodename); + + _cap.ggcli = ggcli; + _cap.net = net; + + return 1; } - void +void ggsniff_pcap_shutdown(void) { - event_del(_cap.ev); - pcap_close(_cap.pcap); + event_del(_cap.ev); + pcap_close(_cap.pcap); } /* @@ -162,192 +162,192 @@ ggsniff_pcap_shutdown(void) * based on OpenBSD tcpdump, privsep_pcap.c v1.16 */ - static pcap_t * +static pcap_t * my_pcap_open_live(const char *dev, int slen, int promisc, int to_ms, - char *ebuf, u_int dlt, u_int dirfilt) + char *ebuf, u_int dlt, u_int dirfilt) { #if defined(__OpenBSD__) - struct bpf_version bv; - u_int v; - pcap_t *p; - char bpf[sizeof "/dev/bpf0000000000"]; - int fd, n = 0; - struct ifreq ifr; - - p = xmalloc(sizeof(*p)); - bzero(p, sizeof(*p)); - - /* priv part */ - - do { - snprintf(bpf, sizeof(bpf), "/dev/bpf%d", n++); - fd = open(bpf, O_RDONLY); - } while (fd < 0 && errno == EBUSY); - if (fd < 0) - return NULL; - - v = 32768; /* XXX this should be a user-accessible hook */ - ioctl(fd, BIOCSBLEN, &v); - - strlcpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); - if (ioctl(fd, BIOCSETIF, &ifr) < 0) - return NULL; - - if (dlt != (u_int) -1 && ioctl(fd, BIOCSDLT, &dlt)) - return NULL; - - if (promisc) - /* this is allowed to fail */ - ioctl(fd, BIOCPROMISC, NULL); - if (ioctl(fd, BIOCSDIRFILT, &dirfilt) < 0) - return NULL; - - /* lock the descriptor */ - if (ioctl(fd, BIOCLOCK, NULL) < 0) - return NULL; - - /* end of priv part */ - - /* fd is locked, can only use 'safe' ioctls */ - if (ioctl(fd, BIOCVERSION, &bv) < 0) { - snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s", - pcap_strerror(errno)); - return NULL; - } - - if (bv.bv_major != BPF_MAJOR_VERSION || - bv.bv_minor < BPF_MINOR_VERSION) { - snprintf(ebuf, PCAP_ERRBUF_SIZE, - "kernel bpf filter out of date"); - return NULL; - } - - p->fd = fd; - p->snapshot = slen; - - /* Get the data link layer type. */ - if (ioctl(fd, BIOCGDLT, &v) < 0) { - snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s", - pcap_strerror(errno)); - return NULL; - } + struct bpf_version bv; + u_int v; + pcap_t *p; + char bpf[sizeof "/dev/bpf0000000000"]; + int fd, n = 0; + struct ifreq ifr; + + p = xmalloc(sizeof(*p)); + bzero(p, sizeof(*p)); + + /* priv part */ + + do { + snprintf(bpf, sizeof(bpf), "/dev/bpf%d", n++); + fd = open(bpf, O_RDONLY); + } while (fd < 0 && errno == EBUSY); + if (fd < 0) + return NULL; + + v = 32768; /* XXX this should be a user-accessible hook */ + ioctl(fd, BIOCSBLEN, &v); + + strlcpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); + if (ioctl(fd, BIOCSETIF, &ifr) < 0) + return NULL; + + if (dlt != (u_int) -1 && ioctl(fd, BIOCSDLT, &dlt)) + return NULL; + + if (promisc) + /* this is allowed to fail */ + ioctl(fd, BIOCPROMISC, NULL); + if (ioctl(fd, BIOCSDIRFILT, &dirfilt) < 0) + return NULL; + + /* lock the descriptor */ + if (ioctl(fd, BIOCLOCK, NULL) < 0) + return NULL; + + /* end of priv part */ + + /* fd is locked, can only use 'safe' ioctls */ + if (ioctl(fd, BIOCVERSION, &bv) < 0) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s", + pcap_strerror(errno)); + return NULL; + } + + if (bv.bv_major != BPF_MAJOR_VERSION || + bv.bv_minor < BPF_MINOR_VERSION) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, + "kernel bpf filter out of date"); + return NULL; + } + + p->fd = fd; + p->snapshot = slen; + + /* Get the data link layer type. */ + if (ioctl(fd, BIOCGDLT, &v) < 0) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s", + pcap_strerror(errno)); + return NULL; + } #if _BSDI_VERSION - 0 >= 199510 - /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */ - switch (v) { + /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */ + switch (v) { - case DLT_SLIP: - v = DLT_SLIP_BSDOS; - break; + case DLT_SLIP: + v = DLT_SLIP_BSDOS; + break; - case DLT_PPP: - v = DLT_PPP_BSDOS; - break; - } + case DLT_PPP: + v = DLT_PPP_BSDOS; + break; + } #endif - p->linktype = v; - - /* XXX hack from tcpdump */ - if (p->linktype == DLT_PFLOG && p->snapshot < 160) - p->snapshot = 160; - - /* set timeout */ - if (to_ms != 0) { - struct timeval to; - to.tv_sec = to_ms / 1000; - to.tv_usec = (to_ms * 1000) % 1000000; - if (ioctl(p->fd, BIOCSRTIMEOUT, &to) < 0) { - snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT: %s", - pcap_strerror(errno)); - return NULL; - } - } - - if (ioctl(fd, BIOCGBLEN, &v) < 0) { - snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s", - pcap_strerror(errno)); - return NULL; - } - p->bufsize = v; - p->buffer = (u_char *)malloc(p->bufsize); - if (p->buffer == NULL) { - snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); - return NULL; - } - return p; + p->linktype = v; + + /* XXX hack from tcpdump */ + if (p->linktype == DLT_PFLOG && p->snapshot < 160) + p->snapshot = 160; + + /* set timeout */ + if (to_ms != 0) { + struct timeval to; + to.tv_sec = to_ms / 1000; + to.tv_usec = (to_ms * 1000) % 1000000; + if (ioctl(p->fd, BIOCSRTIMEOUT, &to) < 0) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT: %s", + pcap_strerror(errno)); + return NULL; + } + } + + if (ioctl(fd, BIOCGBLEN, &v) < 0) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s", + pcap_strerror(errno)); + return NULL; + } + p->bufsize = v; + p->buffer = (u_char *)malloc(p->bufsize); + if (p->buffer == NULL) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", + pcap_strerror(errno)); + return NULL; + } + return p; #else /* defined(__OpenBSD__) */ - return pcap_open_live(dev, slen, promisc, to_ms, ebuf); + return pcap_open_live(dev, slen, promisc, to_ms, ebuf); #endif } - static void +static void cb_pcap(int fd, short why, void *data) { - //gg_log_tmp("cb_pcap"); - pcap_dispatch(_cap.pcap, PCAP_COUNT, _cap.handler, NULL); + //gg_log_tmp("cb_pcap"); + pcap_dispatch(_cap.pcap, PCAP_COUNT, _cap.handler, NULL); - /* reschedule */ - //if (event_add(&_cap->ev, &_cap->tv) == -1) - // gg_log_fatal("user: event_add pcap failed : %s", strerror(errno)); + /* reschedule */ + //if (event_add(&_cap->ev, &_cap->tv) == -1) + // gg_log_fatal("user: event_add pcap failed : %s", strerror(errno)); } - static void +static void cb_conntimer(int fd, short why, void *data) { - struct ggnet_conn *c, *ctmp; - struct ggnet_node *n, *ntmp; - int i, to; - - gg_log_debug("ev_timer"); - ggnet_time_update(_cap.net, time(NULL)); - - i = 0; - LIST_FOREACH_SAFE(c, &_cap.net->conn_list, entry, ctmp) { - switch (c->proto) { - case IPPROTO_UDP: - to = CONN_TIMEOUT_UDP; - break; - case IPPROTO_ICMP: - to = CONN_TIMEOUT_ICMP; - break; - default: - to = CONN_TIMEOUT; - break; - } - if (_cap.net->time > c->lastseen + to) - conn_del(c); - else - i++; - } - - if (_cap.net->node_count > NODE_MAX_WITHOUT_TIMEOUT) { - LIST_FOREACH_SAFE(n, &_cap.net->node_list, entry, ntmp) { - if (n->used == 0 && - _cap.net->time > n->lastseen + NODE_TIMEOUT) - ggnet_node_del(_cap.net, n); - } - } - - gg_log_debug("user: ev_timer leaving with %d active connections and %d active nodes", i, _cap.net->node_count); - if (evtimer_add(_cap.conntimer_ev, &_cap.conntimer_tv) == -1) - gg_log_fatal("user: event_add conntimer failed : %s", strerror(errno)); + struct ggnet_conn *c, *ctmp; + struct ggnet_node *n, *ntmp; + int i, to; + + gg_log_debug("ev_timer"); + ggnet_time_update(_cap.net, time(NULL)); + + i = 0; + LIST_FOREACH_SAFE(c, &_cap.net->conn_list, entry, ctmp) { + switch (c->proto) { + case IPPROTO_UDP: + to = CONN_TIMEOUT_UDP; + break; + case IPPROTO_ICMP: + to = CONN_TIMEOUT_ICMP; + break; + default: + to = CONN_TIMEOUT; + break; + } + if (_cap.net->time > c->lastseen + to) + conn_del(c); + else + i++; + } + + if (_cap.net->node_count > NODE_MAX_WITHOUT_TIMEOUT) { + LIST_FOREACH_SAFE(n, &_cap.net->node_list, entry, ntmp) { + if (n->used == 0 && + _cap.net->time > n->lastseen + NODE_TIMEOUT) + ggnet_node_del(_cap.net, n); + } + } + + gg_log_debug("user: ev_timer leaving with %d active connections and %d active nodes", i, _cap.net->node_count); + if (evtimer_add(_cap.conntimer_ev, &_cap.conntimer_tv) == -1) + gg_log_fatal("user: event_add conntimer failed : %s", strerror(errno)); } - static void +static void cb_nodename(struct ggnet *net, struct ggnet_node *n) { - struct gg_packet pkt; - int len; - - len = strnlen(n->fqdn, GGNET_DNSNAME_MAX); - if (len > 0) { - pkt.ver = PACKET_VERSION; - pkt.type = PACKET_NAME; - pkt.name_addr = n->addr.s_addr; - pkt.name_len = len; - strncpy((char *)pkt.name_fqdn, n->fqdn, sizeof(pkt.name_fqdn)); - gg_client_send(_cap.ggcli, &pkt); - } + struct gg_packet pkt; + int len; + + len = strnlen(n->fqdn, GGNET_DNSNAME_MAX); + if (len > 0) { + pkt.ver = PACKET_VERSION; + pkt.type = PACKET_NAME; + pkt.name_addr = n->addr.s_addr; + pkt.name_len = len; + strncpy((char *)pkt.name_fqdn, n->fqdn, sizeof(pkt.name_fqdn)); + gg_client_send(_cap.ggcli, &pkt); + } } /* @@ -359,221 +359,221 @@ cb_nodename(struct ggnet *net, struct ggnet_node *n) #define NOTCAPTURED(v) ((u_char *)v > (u_char *)pend - sizeof(*v)) #define NOTRECEIVED(v) (wirelen < sizeof(v)) #define log_pinvalid(fmt, ...) \ - gg_log_info("ggsniff pinvalid: " fmt, ##__VA_ARGS__) - static void + gg_log_info("ggsniff pinvalid: " fmt, ##__VA_ARGS__) +static void ip_handle(struct ip *ip, const u_char *pend, u_int wirelen) { - u_int len, ip_hlen, off; - const u_char *cp; - struct tcphdr *tcph; - struct udphdr *udph; - struct icmp *icmp; - struct in_addr src, dst; - u_int src_port, dst_port; - u_int proto, close; - int response; - struct ggnet_conn *conn; - struct gg_packet pkt; - - if (NOTCAPTURED(ip)) { - log_pinvalid("user: ip truncated (ip %x pend %x sizeof(ip) %d", - ip, pend, sizeof(ip)); - _cap.ptruncated++; - return; - } - - if (ip->ip_v != IPVERSION) { - log_pinvalid("user: invalid ip version"); - _cap.pinvalid++; - return; - } - - len = ntohs(ip->ip_len); - if (wirelen < len) { - log_pinvalid("user: ip too small"); - _cap.pinvalid++; - len = wirelen; - } - - ip_hlen = ip->ip_hl * 4; - if (ip_hlen < sizeof(struct ip) || ip_hlen > len) { - log_pinvalid("user: ip_hlen invalid, %d", ip_hlen); - _cap.pinvalid++; - return; - } - len -= ip_hlen; - - src.s_addr = ntohl(ip->ip_src.s_addr); - dst.s_addr = ntohl(ip->ip_dst.s_addr); - src_port = 0; - dst_port = 0; - proto = IPPROTO_IP; - close = 0; - - off = ntohs(ip->ip_off); - if ((off & IP_OFFMASK) == 0) { - cp = (const u_char *)ip + ip_hlen; - switch (ip->ip_p) { - - case IPPROTO_TCP: - tcph = (struct tcphdr *)cp; - if (NOTCAPTURED(&tcph->th_flags)) { - log_pinvalid("user: tcp truncated"); - _cap.ptruncated++; - return; - } - if (NOTRECEIVED(*tcph)) { - log_pinvalid("user: tcp too small"); - _cap.pinvalid++; - return; - } - src_port = ntohs(tcph->th_sport); - dst_port = ntohs(tcph->th_dport); - proto = IPPROTO_TCP; - if ((tcph->th_flags & TH_FIN) && - (tcph->th_flags & TH_ACK)) - close = 1; - break; - - case IPPROTO_UDP: - udph = (struct udphdr *)cp; - if (NOTCAPTURED(&udph->uh_dport)) { - log_pinvalid("user: udp truncated, " - "ip %x, udph %x, uh_port %x, pend %x, ip_hlen %d", - ip, udph, &udph->uh_dport, pend, ip_hlen); - _cap.ptruncated++; - return; - } - if (NOTRECEIVED(*udph)) { - log_pinvalid("user: udp too small"); - _cap.pinvalid++; - return; - } - src_port = ntohs(udph->uh_sport); - dst_port = ntohs(udph->uh_dport); - proto = IPPROTO_UDP; - break; - - case IPPROTO_ICMP: - icmp = (struct icmp *)cp; - if (NOTRECEIVED(*icmp)) { - log_pinvalid("user: icmp too small"); - _cap.pinvalid++; - return; - } - proto = IPPROTO_ICMP; - break; - - default: - gg_log_warn("user: unknown ip protocol !"); - break; - } - } else { - /* - * if this isn't the first frag, we're missing the - * next level protocol header. - */ - gg_log_debug("user: got a fragmented ip packet !"); - } - - pkt.ver = PACKET_VERSION; - conn = ggnet_conn_find(_cap.net, &src, src_port, &dst, dst_port, - proto, &response); - if (conn) { - if (!close) { - pkt.type = PACKET_DATA; - pkt.data_connid = conn->id; - GG_PKTDATA_SIZE_ENCODE(pkt.data_size, ip->ip_len, response); - gg_client_send(_cap.ggcli, &pkt); - ggnet_conn_data(_cap.net, conn, ip->ip_len, response); - } else { - conn_del(conn); - } - } else { - if (!close) { - conn = ggnet_conn_add(_cap.net, &src, src_port, &dst, dst_port, proto, - ip->ip_len, -1); - pkt.type = PACKET_NEWCONN; - pkt.newconn_id = conn->id; - pkt.newconn_src = src.s_addr; - pkt.newconn_dst = dst.s_addr; - pkt.newconn_proto = proto; - GG_PKTDATA_SIZE_ENCODE(pkt.newconn_size, ip->ip_len, 0); - gg_client_send(_cap.ggcli, &pkt); - } else { - gg_log_warn("user: captured connection close w/o open !"); - } - } + u_int len, ip_hlen, off; + const u_char *cp; + struct tcphdr *tcph; + struct udphdr *udph; + struct icmp *icmp; + struct in_addr src, dst; + u_int src_port, dst_port; + u_int proto, close; + int response; + struct ggnet_conn *conn; + struct gg_packet pkt; + + if (NOTCAPTURED(ip)) { + log_pinvalid("user: ip truncated (ip %x pend %x sizeof(ip) %d", + ip, pend, sizeof(ip)); + _cap.ptruncated++; + return; + } + + if (ip->ip_v != IPVERSION) { + log_pinvalid("user: invalid ip version"); + _cap.pinvalid++; + return; + } + + len = ntohs(ip->ip_len); + if (wirelen < len) { + log_pinvalid("user: ip too small"); + _cap.pinvalid++; + len = wirelen; + } + + ip_hlen = ip->ip_hl * 4; + if (ip_hlen < sizeof(struct ip) || ip_hlen > len) { + log_pinvalid("user: ip_hlen invalid, %d", ip_hlen); + _cap.pinvalid++; + return; + } + len -= ip_hlen; + + src.s_addr = ntohl(ip->ip_src.s_addr); + dst.s_addr = ntohl(ip->ip_dst.s_addr); + src_port = 0; + dst_port = 0; + proto = IPPROTO_IP; + close = 0; + + off = ntohs(ip->ip_off); + if ((off & IP_OFFMASK) == 0) { + cp = (const u_char *)ip + ip_hlen; + switch (ip->ip_p) { + + case IPPROTO_TCP: + tcph = (struct tcphdr *)cp; + if (NOTCAPTURED(&tcph->th_flags)) { + log_pinvalid("user: tcp truncated"); + _cap.ptruncated++; + return; + } + if (NOTRECEIVED(*tcph)) { + log_pinvalid("user: tcp too small"); + _cap.pinvalid++; + return; + } + src_port = ntohs(tcph->th_sport); + dst_port = ntohs(tcph->th_dport); + proto = IPPROTO_TCP; + if ((tcph->th_flags & TH_FIN) && + (tcph->th_flags & TH_ACK)) + close = 1; + break; + + case IPPROTO_UDP: + udph = (struct udphdr *)cp; + if (NOTCAPTURED(&udph->uh_dport)) { + log_pinvalid("user: udp truncated, " + "ip %x, udph %x, uh_port %x, pend %x, ip_hlen %d", + ip, udph, &udph->uh_dport, pend, ip_hlen); + _cap.ptruncated++; + return; + } + if (NOTRECEIVED(*udph)) { + log_pinvalid("user: udp too small"); + _cap.pinvalid++; + return; + } + src_port = ntohs(udph->uh_sport); + dst_port = ntohs(udph->uh_dport); + proto = IPPROTO_UDP; + break; + + case IPPROTO_ICMP: + icmp = (struct icmp *)cp; + if (NOTRECEIVED(*icmp)) { + log_pinvalid("user: icmp too small"); + _cap.pinvalid++; + return; + } + proto = IPPROTO_ICMP; + break; + + default: + gg_log_warn("user: unknown ip protocol !"); + break; + } + } else { + /* + * if this isn't the first frag, we're missing the + * next level protocol header. + */ + gg_log_debug("user: got a fragmented ip packet !"); + } + + pkt.ver = PACKET_VERSION; + conn = ggnet_conn_find(_cap.net, &src, src_port, &dst, dst_port, + proto, &response); + if (conn) { + if (!close) { + pkt.type = PACKET_DATA; + pkt.data_connid = conn->id; + GG_PKTDATA_SIZE_ENCODE(pkt.data_size, ip->ip_len, response); + gg_client_send(_cap.ggcli, &pkt); + ggnet_conn_data(_cap.net, conn, ip->ip_len, response); + } else { + conn_del(conn); + } + } else { + if (!close) { + conn = ggnet_conn_add(_cap.net, &src, src_port, &dst, dst_port, proto, + ip->ip_len, -1); + pkt.type = PACKET_NEWCONN; + pkt.newconn_id = conn->id; + pkt.newconn_src = src.s_addr; + pkt.newconn_dst = dst.s_addr; + pkt.newconn_proto = proto; + GG_PKTDATA_SIZE_ENCODE(pkt.newconn_size, ip->ip_len, 0); + gg_client_send(_cap.ggcli, &pkt); + } else { + gg_log_warn("user: captured connection close w/o open !"); + } + } } - static void +static void conn_del(struct ggnet_conn *c) { - struct gg_packet pkt; + struct gg_packet pkt; - pkt.ver = PACKET_VERSION; - pkt.type = PACKET_DELCONN; - pkt.delconn_id = c->id; - gg_client_send(_cap.ggcli, &pkt); - ggnet_conn_del(_cap.net, c); + pkt.ver = PACKET_VERSION; + pkt.type = PACKET_DELCONN; + pkt.delconn_id = c->id; + gg_client_send(_cap.ggcli, &pkt); + ggnet_conn_del(_cap.net, c); } - static void +static void ether_handle(struct ether_header *ether, const u_char *pend, u_int wirelen) { - struct ip *ip; - u_short ether_type; - - wirelen -= sizeof(struct ether_header); - - ether_type = ntohs(ether->ether_type); - if (ether_type <= ETHERMTU) - gg_log_debug("llc packet !"); - else { - switch (ether_type) { - case ETHERTYPE_IP: - gg_log_debug("loop family AF_LINK IP"); - ip = (struct ip *)((u_char *)ether + sizeof(struct ether_header)); - ip_handle(ip, pend, wirelen); - break; - default: - gg_log_debug("loop non ip packet !"); - break; - } - } + struct ip *ip; + u_short ether_type; + + wirelen -= sizeof(struct ether_header); + + ether_type = ntohs(ether->ether_type); + if (ether_type <= ETHERMTU) + gg_log_debug("llc packet !"); + else { + switch (ether_type) { + case ETHERTYPE_IP: + gg_log_debug("loop family AF_LINK IP"); + ip = (struct ip *)((u_char *)ether + sizeof(struct ether_header)); + ip_handle(ip, pend, wirelen); + break; + default: + gg_log_debug("loop non ip packet !"); + break; + } + } } - static pcap_handler +static pcap_handler lookup_phandler(int type) { - struct phandler *p; - - for (p = phandlers; p->f; ++p) { - if (type == p->type) - return p->f; - } - err(1, "user: unknown data link type 0x%x", type); - /* NOTREACHED */ - return NULL; + struct phandler *p; + + for (p = phandlers; p->f; ++p) { + if (type == p->type) + return p->f; + } + err(1, "user: unknown data link type 0x%x", type); + /* NOTREACHED */ + return NULL; } - static void +static void phandler_ether(u_char *user, const struct pcap_pkthdr *h, const u_char *p) { - struct ether_header *ep; - const u_char *pend; - u_int len; + struct ether_header *ep; + const u_char *pend; + u_int len; - gg_log_debug("user: pcap handler ethernet !"); + gg_log_debug("user: pcap handler ethernet !"); - /* XXX here i assume that packets are alligned, which might not - * be the case when using dump files, says tcpdump sources */ + /* XXX here i assume that packets are alligned, which might not + * be the case when using dump files, says tcpdump sources */ - ep = (struct ether_header *)p; - pend = p + h->caplen; - len = h->len - sizeof(struct ether_header); + ep = (struct ether_header *)p; + pend = p + h->caplen; + len = h->len - sizeof(struct ether_header); - ether_handle(ep, pend, len); + ether_handle(ep, pend, len); } /* @@ -587,37 +587,37 @@ phandler_ether(u_char *user, const struct pcap_pkthdr *h, const u_char *p) #define LINUX_SLL_P_CAN 0x000C /* Controller Area Network */ #define LINUX_SLL_P_IRDA_LAP 0x0017 /* IrDA Link Access Protocol */ - static void +static void phandler_sll(u_char *user, const struct pcap_pkthdr *h, const u_char *p) { - struct ip *ip; - struct ether_header *ep; - u_int family; - const u_char *pend; - u_int len; - - gg_log_debug("user: phandler_sll !"); - - /* XXX here i assume that packets are alligned, which might not - * be the case when using dump files, says tcpdump sources */ - - pend = p + h->caplen; - len = h->len - SLL_HDR_LEN; - - family = ntohs(p[14]); - if (family < 1536) { /* linux and wireshark are good for you */ - switch (family) { - case LINUX_SLL_P_ETHERNET: - ep = (struct ether_header *)((u_char *)p + SLL_HDR_LEN); - ether_handle(ep, pend, len); - default: - gg_log_debug("unknown family %x !", family); - break; - } - } else { - ip = (struct ip *)(p + SLL_HDR_LEN); - ip_handle(ip, pend, len); - } + struct ip *ip; + struct ether_header *ep; + u_int family; + const u_char *pend; + u_int len; + + gg_log_debug("user: phandler_sll !"); + + /* XXX here i assume that packets are alligned, which might not + * be the case when using dump files, says tcpdump sources */ + + pend = p + h->caplen; + len = h->len - SLL_HDR_LEN; + + family = ntohs(p[14]); + if (family < 1536) { /* linux and wireshark are good for you */ + switch (family) { + case LINUX_SLL_P_ETHERNET: + ep = (struct ether_header *)((u_char *)p + SLL_HDR_LEN); + ether_handle(ep, pend, len); + default: + gg_log_debug("unknown family %x !", family); + break; + } + } else { + ip = (struct ip *)(p + SLL_HDR_LEN); + ip_handle(ip, pend, len); + } } #endif /* __linux__ */ @@ -627,38 +627,38 @@ phandler_sll(u_char *user, const struct pcap_pkthdr *h, const u_char *p) #if defined(__OpenBSD__) #define NULL_HDRLEN 4 - static void +static void phandler_loop(u_char *user, const struct pcap_pkthdr *h, const u_char *p) { - struct ip *ip; - struct ether_header *ep; - u_int family; - const u_char *pend; - u_int len; - - gg_log_debug("user: pcap handler loop !"); - - /* XXX here i assume that packets are alligned, which might not - * be the case when using dump files, says tcpdump sources */ - - pend = p + h->caplen; - len = h->len - NULL_HDRLEN; - - memcpy((char *)&family, (char *)p, sizeof(family)); - family = ntohl(family); - switch (family) { - case AF_INET: - gg_log_debug("loop family AF_INET"); - ip = (struct ip *)(p + NULL_HDRLEN); - ip_handle(ip, pend, len); - break; - case AF_LINK: - ep = (struct ether_header *)((u_char *)p + NULL_HDRLEN); - ether_handle(ep, pend, len); - break; - default: - gg_log_debug("unknown family %x !", family); - break; - } + struct ip *ip; + struct ether_header *ep; + u_int family; + const u_char *pend; + u_int len; + + gg_log_debug("user: pcap handler loop !"); + + /* XXX here i assume that packets are alligned, which might not + * be the case when using dump files, says tcpdump sources */ + + pend = p + h->caplen; + len = h->len - NULL_HDRLEN; + + memcpy((char *)&family, (char *)p, sizeof(family)); + family = ntohl(family); + switch (family) { + case AF_INET: + gg_log_debug("loop family AF_INET"); + ip = (struct ip *)(p + NULL_HDRLEN); + ip_handle(ip, pend, len); + break; + case AF_LINK: + ep = (struct ether_header *)((u_char *)p + NULL_HDRLEN); + ether_handle(ep, pend, len); + break; + default: + gg_log_debug("unknown family %x !", family); + break; + } } #endif /* __OpenBSD__ */ diff --git a/gg_trackproc/gg_trackproc.c b/gg_trackproc/gg_trackproc.c index 8706849..7bb995f 100644 --- a/gg_trackproc/gg_trackproc.c +++ b/gg_trackproc/gg_trackproc.c @@ -34,242 +34,246 @@ int loglevel = 0; static char* get_current_date() { - time_t ts; - struct tm *tm; - static char date_buf[64]; - - time(&ts); - tm = localtime(&ts); - strftime(date_buf, 64, "%b %d %X", tm); - return date_buf; + time_t ts; + struct tm *tm; + static char date_buf[64]; + + time(&ts); + tm = localtime(&ts); + strftime(date_buf, 64, "%b %d %X", tm); + return date_buf; } static int get_proc_infos(int pid, char **cmd) { - char path[128]; - char readbuf[1024]; - int size; - FILE *file; - char *comm, *tmpptr; - - //if ((pid = (pid_t) atoi(de->d_name)) == 0) { - snprintf(path, sizeof(path), "%s/%d/stat", PROC_BASE, pid); - if ((file = fopen(path, "r")) == NULL) { - warn("Error: PID %d: cannot open %s\n", pid, path); - return -1; - } - size = fread(readbuf, 1, sizeof(readbuf), file); - if (ferror(file) != 0) { - warn("Error: PID %d: error reading %s", pid, path); - return -1; - } - readbuf[size] = 0; - /* commands may have spaces or ) in them. - * so don't trust anything from the ( to the last ) */ - if (!(comm = strchr(readbuf, '(')) - || !(tmpptr = strrchr(comm, ')'))) { - warn("Error: PID %d: could not parse %s", pid, path); - return -1; - } - ++comm; - *tmpptr = 0; - /* We now have readbuf with pid and cmd, and tmpptr+2 - * with the rest */ - fclose(file); - - *cmd = comm; - return 0; + char path[128]; + char readbuf[1024]; + int size; + FILE *file; + char *comm, *tmpptr; + + //if ((pid = (pid_t) atoi(de->d_name)) == 0) { + snprintf(path, sizeof(path), "%s/%d/stat", PROC_BASE, pid); + if ((file = fopen(path, "r")) == NULL) { + warn("Error: PID %d: cannot open %s\n", pid, path); + return -1; + } + size = fread(readbuf, 1, sizeof(readbuf), file); + if (ferror(file) != 0) { + warn("Error: PID %d: error reading %s", pid, path); + return -1; + } + readbuf[size] = 0; + /* commands may have spaces or ) in them. + * so don't trust anything from the ( to the last ) */ + if (!(comm = strchr(readbuf, '(')) + || !(tmpptr = strrchr(comm, ')'))) { + warn("Error: PID %d: could not parse %s", pid, path); + return -1; + } + ++comm; + *tmpptr = 0; + /* We now have readbuf with pid and cmd, and tmpptr+2 + * with the rest */ + fclose(file); + + *cmd = comm; + return 0; } /* * connect to netlink * returns netlink socket, or -1 on error */ -static int nl_connect() +static int +nl_connect() { - int rc; - int nl_sock; - struct sockaddr_nl sa_nl; - - nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); - if (nl_sock == -1) { - perror("socket"); - return -1; - } - - sa_nl.nl_family = AF_NETLINK; - sa_nl.nl_groups = CN_IDX_PROC; - sa_nl.nl_pid = getpid(); - - rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl)); - if (rc == -1) { - perror("bind"); - close(nl_sock); - return -1; - } - - return nl_sock; + int rc; + int nl_sock; + struct sockaddr_nl sa_nl; + + nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); + if (nl_sock == -1) { + perror("socket"); + return -1; + } + + sa_nl.nl_family = AF_NETLINK; + sa_nl.nl_groups = CN_IDX_PROC; + sa_nl.nl_pid = getpid(); + + rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl)); + if (rc == -1) { + perror("bind"); + close(nl_sock); + return -1; + } + + return nl_sock; } /* * subscribe on proc events (process notifications) */ -static int set_proc_ev_listen(int nl_sock, bool enable) +static int +set_proc_ev_listen(int nl_sock, bool enable) { - int rc; - struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { - struct nlmsghdr nl_hdr; - struct __attribute__ ((__packed__)) { - struct cn_msg cn_msg; - enum proc_cn_mcast_op cn_mcast; - }; - } nlcn_msg; - - memset(&nlcn_msg, 0, sizeof(nlcn_msg)); - nlcn_msg.nl_hdr.nlmsg_len = sizeof(nlcn_msg); - nlcn_msg.nl_hdr.nlmsg_pid = getpid(); - nlcn_msg.nl_hdr.nlmsg_type = NLMSG_DONE; - - nlcn_msg.cn_msg.id.idx = CN_IDX_PROC; - nlcn_msg.cn_msg.id.val = CN_VAL_PROC; - nlcn_msg.cn_msg.len = sizeof(enum proc_cn_mcast_op); - - nlcn_msg.cn_mcast = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE; - - rc = send(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0); - if (rc == -1) { - perror("netlink send"); - return -1; - } - - return 0; + int rc; + struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { + struct nlmsghdr nl_hdr; + struct __attribute__ ((__packed__)) { + struct cn_msg cn_msg; + enum proc_cn_mcast_op cn_mcast; + }; + } nlcn_msg; + + memset(&nlcn_msg, 0, sizeof(nlcn_msg)); + nlcn_msg.nl_hdr.nlmsg_len = sizeof(nlcn_msg); + nlcn_msg.nl_hdr.nlmsg_pid = getpid(); + nlcn_msg.nl_hdr.nlmsg_type = NLMSG_DONE; + + nlcn_msg.cn_msg.id.idx = CN_IDX_PROC; + nlcn_msg.cn_msg.id.val = CN_VAL_PROC; + nlcn_msg.cn_msg.len = sizeof(enum proc_cn_mcast_op); + + nlcn_msg.cn_mcast = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE; + + rc = send(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0); + if (rc == -1) { + perror("netlink send"); + return -1; + } + + return 0; } -void cb_nl(evutil_socket_t fd, short what, void *arg) +void +cb_nl(evutil_socket_t fd, short what, void *arg) { - int rc; - struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { - struct nlmsghdr nl_hdr; - struct __attribute__ ((__packed__)) { - struct cn_msg cn_msg; - struct proc_event proc_ev; - }; - } nlcn_msg; - static char *cmd; - struct gg_packet pkt; - - rc = recv(fd, &nlcn_msg, sizeof(nlcn_msg), 0); - if (rc == 0) { - /* shutdown? */ - if (loglevel) - printf("%s: stop listening to netlink events\n", - get_current_date()); - event_base_loopexit(ev_base, NULL); - return; - } else if (rc == -1) { - if (errno == EINTR) - return; - perror("netlink recv"); - event_base_loopexit(ev_base, NULL); - return; - } - - pkt.type = 0; - pkt.ver = PACKET_VERSION; - /* see /usr/include/linux/cn_proc.h for struct proc_event */ - switch (nlcn_msg.proc_ev.what) { - case PROC_EVENT_NONE: - /* XXX what is this event for ? - * if (loglevel) - * printf("%s: start listening to netlink events...\n", - * get_current_date()); */ - break; - - case PROC_EVENT_FORK: - get_proc_infos(nlcn_msg.proc_ev.event_data.fork.parent_pid, - &cmd); - if ((nlcn_msg.proc_ev.event_data.fork.child_tgid - != nlcn_msg.proc_ev.event_data.fork.child_pid)) { - if (loglevel) - printf("%s: fork %s %d -> %d tid %d\n", - get_current_date(), - cmd, - nlcn_msg.proc_ev.event_data.fork.parent_pid, - nlcn_msg.proc_ev.event_data.fork.child_pid, - nlcn_msg.proc_ev.event_data.fork.child_tgid); - } else { - if (loglevel) - printf("%s: fork %s %d -> %d\n", - get_current_date(), - cmd, - nlcn_msg.proc_ev.event_data.fork.parent_pid, - nlcn_msg.proc_ev.event_data.fork.child_pid); - } - pkt.type = PACKET_FORK; - pkt.fork_pid = nlcn_msg.proc_ev.event_data.fork.parent_pid; - pkt.fork_ppid = nlcn_msg.proc_ev.event_data.fork.parent_pid; - pkt.fork_cpid = nlcn_msg.proc_ev.event_data.fork.child_pid; - pkt.fork_tgid = nlcn_msg.proc_ev.event_data.fork.child_tgid; - break; - - case PROC_EVENT_EXEC: - get_proc_infos(nlcn_msg.proc_ev.event_data.exec.process_pid, - &cmd); - if (loglevel) - printf("%s: exec %d -> %s\n", - get_current_date(), - nlcn_msg.proc_ev.event_data.exec.process_pid, - cmd); - pkt.type = PACKET_EXEC; - pkt.exec_pid = nlcn_msg.proc_ev.event_data.fork.parent_pid; - pkt.exec_cmdlen = strnlen(cmd, GG_PKTARG_MAX); - strncpy((char *)pkt.exec_cmd, cmd, GG_PKTARG_MAX); - break; + int rc; + struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { + struct nlmsghdr nl_hdr; + struct __attribute__ ((__packed__)) { + struct cn_msg cn_msg; + struct proc_event proc_ev; + }; + } nlcn_msg; + static char *cmd; + struct gg_packet pkt; + + rc = recv(fd, &nlcn_msg, sizeof(nlcn_msg), 0); + if (rc == 0) { + /* shutdown? */ + if (loglevel) + printf("%s: stop listening to netlink events\n", + get_current_date()); + event_base_loopexit(ev_base, NULL); + return; + } else if (rc == -1) { + if (errno == EINTR) + return; + perror("netlink recv"); + event_base_loopexit(ev_base, NULL); + return; + } + + pkt.type = 0; + pkt.ver = PACKET_VERSION; + /* see /usr/include/linux/cn_proc.h for struct proc_event */ + switch (nlcn_msg.proc_ev.what) { + case PROC_EVENT_NONE: + /* XXX what is this event for ? + * if (loglevel) + * printf("%s: start listening to netlink events...\n", + * get_current_date()); */ + break; + + case PROC_EVENT_FORK: + get_proc_infos(nlcn_msg.proc_ev.event_data.fork.parent_pid, + &cmd); + if ((nlcn_msg.proc_ev.event_data.fork.child_tgid + != nlcn_msg.proc_ev.event_data.fork.child_pid)) { + if (loglevel) + printf("%s: fork %s %d -> %d tid %d\n", + get_current_date(), + cmd, + nlcn_msg.proc_ev.event_data.fork.parent_pid, + nlcn_msg.proc_ev.event_data.fork.child_pid, + nlcn_msg.proc_ev.event_data.fork.child_tgid); + } else { + if (loglevel) + printf("%s: fork %s %d -> %d\n", + get_current_date(), + cmd, + nlcn_msg.proc_ev.event_data.fork.parent_pid, + nlcn_msg.proc_ev.event_data.fork.child_pid); + } + pkt.type = PACKET_FORK; + pkt.fork_pid = nlcn_msg.proc_ev.event_data.fork.parent_pid; + pkt.fork_ppid = nlcn_msg.proc_ev.event_data.fork.parent_pid; + pkt.fork_cpid = nlcn_msg.proc_ev.event_data.fork.child_pid; + pkt.fork_tgid = nlcn_msg.proc_ev.event_data.fork.child_tgid; + break; + + case PROC_EVENT_EXEC: + get_proc_infos(nlcn_msg.proc_ev.event_data.exec.process_pid, + &cmd); + if (loglevel) + printf("%s: exec %d -> %s\n", + get_current_date(), + nlcn_msg.proc_ev.event_data.exec.process_pid, + cmd); + pkt.type = PACKET_EXEC; + pkt.exec_pid = nlcn_msg.proc_ev.event_data.fork.parent_pid; + pkt.exec_cmdlen = strnlen(cmd, GG_PKTARG_MAX); + strncpy((char *)pkt.exec_cmd, cmd, GG_PKTARG_MAX); + break; #if 0 - case PROC_EVENT_UID: - if (loglevel) - printf("uid change: tid=%d pid=%d from %d to %d\n", - nlcn_msg.proc_ev.event_data.id.process_pid, - nlcn_msg.proc_ev.event_data.id.process_tgid, - nlcn_msg.proc_ev.event_data.id.r.ruid, - nlcn_msg.proc_ev.event_data.id.e.euid); - break; - - case PROC_EVENT_GID: - if (loglevel) - printf("gid change: tid=%d pid=%d from %d to %d\n", - nlcn_msg.proc_ev.event_data.id.process_pid, - nlcn_msg.proc_ev.event_data.id.process_tgid, - nlcn_msg.proc_ev.event_data.id.r.rgid, - nlcn_msg.proc_ev.event_data.id.e.egid); - break; + case PROC_EVENT_UID: + if (loglevel) + printf("uid change: tid=%d pid=%d from %d to %d\n", + nlcn_msg.proc_ev.event_data.id.process_pid, + nlcn_msg.proc_ev.event_data.id.process_tgid, + nlcn_msg.proc_ev.event_data.id.r.ruid, + nlcn_msg.proc_ev.event_data.id.e.euid); + break; + + case PROC_EVENT_GID: + if (loglevel) + printf("gid change: tid=%d pid=%d from %d to %d\n", + nlcn_msg.proc_ev.event_data.id.process_pid, + nlcn_msg.proc_ev.event_data.id.process_tgid, + nlcn_msg.proc_ev.event_data.id.r.rgid, + nlcn_msg.proc_ev.event_data.id.e.egid); + break; #endif - case PROC_EVENT_EXIT: - if (loglevel) - printf("exit: tid=%d pid=%d exit_code=%d\n", - nlcn_msg.proc_ev.event_data.exit.process_pid, - nlcn_msg.proc_ev.event_data.exit.process_tgid, - nlcn_msg.proc_ev.event_data.exit.exit_code); - pkt.type = PACKET_EXIT; - pkt.exit_pid = nlcn_msg.proc_ev.event_data.exit.process_pid; - pkt.exit_tgid = nlcn_msg.proc_ev.event_data.exit.process_tgid; - pkt.exit_ecode = nlcn_msg.proc_ev.event_data.exit.exit_code; - break; - - default: - if (loglevel) - printf("unhandled proc event\n"); - break; - } - - if (pkt.type != 0) - gg_client_send(ggcli, &pkt); + case PROC_EVENT_EXIT: + if (loglevel) + printf("exit: tid=%d pid=%d exit_code=%d\n", + nlcn_msg.proc_ev.event_data.exit.process_pid, + nlcn_msg.proc_ev.event_data.exit.process_tgid, + nlcn_msg.proc_ev.event_data.exit.exit_code); + pkt.type = PACKET_EXIT; + pkt.exit_pid = nlcn_msg.proc_ev.event_data.exit.process_pid; + pkt.exit_tgid = nlcn_msg.proc_ev.event_data.exit.process_tgid; + pkt.exit_ecode = nlcn_msg.proc_ev.event_data.exit.exit_code; + break; + + default: + if (loglevel) + printf("unhandled proc event\n"); + break; + } + + if (pkt.type != 0) + gg_client_send(ggcli, &pkt); } -static void on_sigint(int unused) +static void +on_sigint(int unused) { - event_base_loopexit(ev_base, NULL); + event_base_loopexit(ev_base, NULL); } #if defined(__OPENBSD__) @@ -279,17 +283,18 @@ void #endif usage(void) { - extern char *__progname; + extern char *__progname; - fprintf(stderr, "usage: %s [-vi]", __progname); - exit(1); + fprintf(stderr, "usage: %s [-vi]", __progname); + exit(1); } -int main(int argc, char **argv) +int +main(int argc, char **argv) { - struct event *ev_nl; - int nl_sock; - int rc = EXIT_SUCCESS; + struct event *ev_nl; + int nl_sock; + int rc = EXIT_SUCCESS; int op; while ((op = getopt(argc, argv, "hv")) != -1) { @@ -309,38 +314,38 @@ int main(int argc, char **argv) if (geteuid() != 0) errx(1, "must be root"); - signal(SIGINT, &on_sigint); - siginterrupt(SIGINT, true); + signal(SIGINT, &on_sigint); + siginterrupt(SIGINT, true); - gg_verbosity_set(loglevel); + gg_verbosity_set(loglevel); - ev_base = event_base_new(); - ggcli = gg_client_connect(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT, - NULL, NULL, NULL); + ev_base = event_base_new(); + ggcli = gg_client_connect(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT, + NULL, NULL, NULL); - nl_sock = nl_connect(); - if (nl_sock == -1) - exit(EXIT_FAILURE); - rc = set_proc_ev_listen(nl_sock, true); - if (rc == -1) { - rc = EXIT_FAILURE; - goto out; - } - rc = set_proc_ev_listen(nl_sock, true); - if (rc == -1) { - rc = EXIT_FAILURE; - goto out; - } - ev_nl = event_new(ev_base, nl_sock, EV_READ|EV_PERSIST, cb_nl, NULL); - event_add(ev_nl, NULL); + nl_sock = nl_connect(); + if (nl_sock == -1) + exit(EXIT_FAILURE); + rc = set_proc_ev_listen(nl_sock, true); + if (rc == -1) { + rc = EXIT_FAILURE; + goto out; + } + rc = set_proc_ev_listen(nl_sock, true); + if (rc == -1) { + rc = EXIT_FAILURE; + goto out; + } + ev_nl = event_new(ev_base, nl_sock, EV_READ|EV_PERSIST, cb_nl, NULL); + event_add(ev_nl, NULL); - droppriv(GG_TRACKPROC_USER, 1, CHROOT_PATH); + droppriv(GG_TRACKPROC_USER, 1, CHROOT_PATH); - event_base_dispatch(ev_base); + event_base_dispatch(ev_base); - set_proc_ev_listen(nl_sock, false); + set_proc_ev_listen(nl_sock, false); out: - close(nl_sock); - exit(rc); + close(nl_sock); + exit(rc); } diff --git a/glougloud/glougloud.c b/glougloud/glougloud.c index e868e29..86d6290 100644 --- a/glougloud/glougloud.c +++ b/glougloud/glougloud.c @@ -35,11 +35,11 @@ void #endif usage(void) { - extern char *__progname; + extern char *__progname; - fprintf(stderr, "usage: %s [-hv] [-l probes_ip] [-L analysers_ip]\n" - "\t\t[-p probes_port] [-P analysers_port]\n", __progname); - exit(1); + fprintf(stderr, "usage: %s [-hv] [-l probes_ip] [-L analysers_ip]\n" + "\t\t[-p probes_port] [-P analysers_port]\n", __progname); + exit(1); } static void @@ -67,16 +67,16 @@ main(int argc, char **argv) usage(); /* NOTREACHED */ case 'l': - strncpy(probes_ip, optarg, sizeof(probes_ip)); + strncpy(probes_ip, optarg, sizeof(probes_ip)); break; case 'L': - strncpy(analysers_ip, optarg, sizeof(analysers_ip)); + strncpy(analysers_ip, optarg, sizeof(analysers_ip)); break; case 'p': - probes_port = atoi(optarg); + probes_port = atoi(optarg); break; case 'P': - analysers_port = atoi(optarg); + analysers_port = atoi(optarg); break; case 'v': loglevel++; @@ -92,7 +92,7 @@ main(int argc, char **argv) droppriv(GLOUGLOUD_USER, 1, NULL); - gg_verbosity_set(loglevel); + gg_verbosity_set(loglevel); ev_base = event_base_new(); @@ -107,41 +107,41 @@ main(int argc, char **argv) signal(SIGPIPE, SIG_IGN); ggserv_probes = gg_server_start(ev_base, probes_ip, probes_port, - prb_handle_conn, prb_handle_packet, NULL); + prb_handle_conn, prb_handle_packet, NULL); ggserv_analysers = gg_server_start(ev_base, analysers_ip, analysers_port, - cli_handle_conn, cli_handle_packet, NULL); + cli_handle_conn, cli_handle_packet, NULL); event_base_dispatch(ev_base); - gg_server_stop(ggserv_probes); - gg_server_stop(ggserv_analysers); + gg_server_stop(ggserv_probes); + gg_server_stop(ggserv_analysers); - return 0; + return 0; } int prb_handle_conn(struct gg_server *srv, struct gg_user *usr) { - return 0; + return 0; } int cli_handle_conn(struct gg_server *srv, struct gg_user *usr) { - return 0; + return 0; } int prb_handle_packet(struct gg_server *srv, struct gg_user *usr, struct gg_packet *pkt) { - gg_server_send(ggserv_analysers, pkt, NULL); - return 0; + gg_server_send(ggserv_analysers, pkt, NULL); + return 0; } int cli_handle_packet(struct gg_server *srv, struct gg_user *usr, struct gg_packet *pkt) { - gg_server_send(ggserv_probes, pkt, NULL); - return 0; + gg_server_send(ggserv_probes, pkt, NULL); + return 0; } diff --git a/libglouglou/Makefile b/libglouglou/Makefile index 0343e6f..aae8f0b 100644 --- a/libglouglou/Makefile +++ b/libglouglou/Makefile @@ -13,7 +13,7 @@ OBJECTS = $(SOURCES:.c=.o) all: $(TARGET) $(TARGET): $(OBJECTS) - $(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS) + $(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS) install: @echo "installation of $(LIBNAME)" diff --git a/libglouglou/examples/dnsreverse.c b/libglouglou/examples/dnsreverse.c index 5111391..e6504b0 100644 --- a/libglouglou/examples/dnsreverse.c +++ b/libglouglou/examples/dnsreverse.c @@ -15,39 +15,39 @@ int _answer = 0; int main(int argc, char *argv[]) { - struct ggnet_dns *ggdns; - struct in_addr ip; - struct timeval tv; + struct ggnet_dns *ggdns; + struct in_addr ip; + struct timeval tv; - if (argc < 2) { - printf("usage: dnsreverse \n"); - exit(1); - } - inet_aton(argv[1], &ip); - ip.s_addr = ntohl(ip.s_addr); + if (argc < 2) { + printf("usage: dnsreverse \n"); + exit(1); + } + inet_aton(argv[1], &ip); + ip.s_addr = ntohl(ip.s_addr); - bzero(&tv, sizeof(struct timeval)); - tv.tv_sec = 5; + bzero(&tv, sizeof(struct timeval)); + tv.tv_sec = 5; - _ev_base = event_base_new(); + _ev_base = event_base_new(); - ggdns = ggnet_dns_new(_ev_base); - ggnet_dns_reverse(ggdns, &ip, _cb_dns, NULL); + ggdns = ggnet_dns_new(_ev_base); + ggnet_dns_reverse(ggdns, &ip, _cb_dns, NULL); - event_base_loopexit(_ev_base, &tv); - event_base_dispatch(_ev_base); + event_base_loopexit(_ev_base, &tv); + event_base_dispatch(_ev_base); - if (_answer) - printf("%x: %s\n", ip.s_addr, _name); - else - printf("no answer\n"); - return 0; + if (_answer) + printf("%x: %s\n", ip.s_addr, _name); + else + printf("no answer\n"); + return 0; } static void _cb_dns(struct in_addr *ip, char *name, void *data) { - _name = name; - _answer = 1; - event_base_loopexit(_ev_base, NULL); + _name = name; + _answer = 1; + event_base_loopexit(_ev_base, NULL); } diff --git a/libglouglou/libggnet.c b/libglouglou/libggnet.c index 10c2cf5..ab44d9f 100644 --- a/libglouglou/libggnet.c +++ b/libglouglou/libggnet.c @@ -6,12 +6,12 @@ #include "libggnet_dns.h" static struct ggnet_nodegroup *nodegroup_add(struct ggnet *, - enum ggnet_grouptype, void *, - struct ggnet_nodegroup *); + enum ggnet_grouptype, void *, + struct ggnet_nodegroup *); static void nodegroup_del(struct ggnet *, struct ggnet_nodegroup *); static struct ggnet_nodegroup *nodegroup_find(struct ggnet *, - enum ggnet_grouptype type, - void *param); + enum ggnet_grouptype type, + void *param); static void nodegroup_set(struct ggnet *, struct ggnet_node *); static void nodegroup_unset(struct ggnet *, struct ggnet_node *); static void _cb_dns_reverse(struct in_addr *, char *, void *); @@ -19,20 +19,20 @@ static void _cb_dns_reverse(struct in_addr *, char *, void *); struct ggnet * ggnet_new(int manage_connid) { - struct ggnet *net; - int i; - - net = calloc(1, sizeof(struct ggnet)); - if (!net) { - printf("could not allocate ggnet\n"); - exit(1); - } - net->manage_connid = manage_connid; - if (manage_connid) - for (i=0; iconn_freeids[i] = i; - - return net; + struct ggnet *net; + int i; + + net = calloc(1, sizeof(struct ggnet)); + if (!net) { + printf("could not allocate ggnet\n"); + exit(1); + } + net->manage_connid = manage_connid; + if (manage_connid) + for (i=0; iconn_freeids[i] = i; + + return net; } /** @@ -42,48 +42,48 @@ ggnet_new(int manage_connid) */ void ggnet_set_grouping(struct ggnet *net, int set, - void (*cb_addgroup)(struct ggnet *, struct ggnet_nodegroup *, struct ggnet_nodegroup *), - void (*cb_delgroup)(struct ggnet *, struct ggnet_nodegroup *)) + void (*cb_addgroup)(struct ggnet *, struct ggnet_nodegroup *, struct ggnet_nodegroup *), + void (*cb_delgroup)(struct ggnet *, struct ggnet_nodegroup *)) { - net->use_grouping = set; - net->cb_addgroup = cb_addgroup; - net->cb_delgroup = cb_delgroup; + net->use_grouping = set; + net->cb_addgroup = cb_addgroup; + net->cb_delgroup = cb_delgroup; } void ggnet_set_dns(struct ggnet *net, int set, - struct event_base *ev_base, - void (*cb_nodename)(struct ggnet *, struct ggnet_node *)) + struct event_base *ev_base, + void (*cb_nodename)(struct ggnet *, struct ggnet_node *)) { - net->use_dns = set; - if (set) { - net->ggdns = ggnet_dns_new(ev_base); - net->cb_nodename = cb_nodename; - } - else if (net->use_dns) - ggnet_dns_free(net->ggdns); + net->use_dns = set; + if (set) { + net->ggdns = ggnet_dns_new(ev_base); + net->cb_nodename = cb_nodename; + } + else if (net->use_dns) + ggnet_dns_free(net->ggdns); } void ggnet_debug_set(struct ggnet *net, int set) { - net->debug = set; + net->debug = set; } void ggnet_free(struct ggnet *net) { - struct ggnet_conn *c, *ctmp; - struct ggnet_node *n, *ntmp; - struct ggnet_nodegroup *g, *gtmp; - - LIST_FOREACH_SAFE(c, &net->conn_list, entry, ctmp) - ggnet_conn_del(net, c); - LIST_FOREACH_SAFE(n, &net->node_list, entry, ntmp) - ggnet_node_del(net, n); - LIST_FOREACH_SAFE(g, &net->group_list, entry, gtmp) - nodegroup_del(net, g); - free(net); + struct ggnet_conn *c, *ctmp; + struct ggnet_node *n, *ntmp; + struct ggnet_nodegroup *g, *gtmp; + + LIST_FOREACH_SAFE(c, &net->conn_list, entry, ctmp) + ggnet_conn_del(net, c); + LIST_FOREACH_SAFE(n, &net->node_list, entry, ntmp) + ggnet_node_del(net, n); + LIST_FOREACH_SAFE(g, &net->group_list, entry, gtmp) + nodegroup_del(net, g); + free(net); } struct ggnet_node * @@ -92,22 +92,22 @@ ggnet_node_add(struct ggnet *net, struct in_addr *addr) struct ggnet_node *n; if (net->debug) - printf("ggnet_node_add\n"); + printf("ggnet_node_add\n"); n = calloc(1, sizeof(struct ggnet_node)); - if (!n) { - printf("could not allocate node\n"); - exit(1); - } - n->net = net; + if (!n) { + printf("could not allocate node\n"); + exit(1); + } + n->net = net; n->addr.s_addr = addr->s_addr; n->lastseen = net->time; LIST_INSERT_HEAD(&net->node_list, n, entry); net->node_count++; if (net->use_grouping) - nodegroup_set(net, n); - if (net->use_dns) - n->dns_req = ggnet_dns_reverse(net->ggdns, &n->addr, _cb_dns_reverse, n); + nodegroup_set(net, n); + if (net->use_dns) + n->dns_req = ggnet_dns_reverse(net->ggdns, &n->addr, _cb_dns_reverse, n); return n; } @@ -118,14 +118,14 @@ ggnet_node_del(struct ggnet *net, struct ggnet_node *n) if (n->used) { printf("FATAL: ggnet_node_del: trying to remove a used node !\n"); exit(1); - } + } if (net->debug) - printf("ggnet_node_del: ggnet_node_del\n"); + printf("ggnet_node_del: ggnet_node_del\n"); if (net->use_grouping) - nodegroup_unset(net, n); - if (n->dns_req) - ggnet_dns_cancel(net->ggdns, n->dns_req); + nodegroup_unset(net, n); + if (n->dns_req) + ggnet_dns_cancel(net->ggdns, n->dns_req); LIST_REMOVE(n, entry); free(n); @@ -146,25 +146,25 @@ ggnet_node_find(struct ggnet *net, struct in_addr *remote) void * ggnet_node_usrdata_get(struct ggnet_node *n) { - return n->usrdata; + return n->usrdata; } void ggnet_node_usrdata_set(struct ggnet_node *n, void *usrdata) { - n->usrdata = usrdata; + n->usrdata = usrdata; } struct ggnet_nodegroup * ggnet_node_group_get(struct ggnet_node *n) { - return n->group; + return n->group; } struct ggnet_conn * ggnet_conn_add(struct ggnet *net, struct in_addr *src, int src_port, - struct in_addr *dst, int dst_port, int proto, int size, - int given_id) + struct in_addr *dst, int dst_port, int proto, int size, + int given_id) { struct ggnet_conn *c; struct ggnet_node *srcnode; @@ -172,38 +172,38 @@ ggnet_conn_add(struct ggnet *net, struct in_addr *src, int src_port, int id; if (net->debug) - printf("ggnet_conn_add, %x:%d->%x:%d %d [%d]\n", - src->s_addr, src_port, dst->s_addr, dst_port, proto, size); - - if (net->manage_connid) { - if (net->conn_freeids_ptr == GGNET_CONN_FREEIDS_COUNT) { - printf("ggnet_conn_add: ERROR: out of connection identifiers !"); - return NULL; - } - id = net->conn_freeids[net->conn_freeids_ptr]; - net->conn_freeids_ptr++; - } else { - id = given_id; - } + printf("ggnet_conn_add, %x:%d->%x:%d %d [%d]\n", + src->s_addr, src_port, dst->s_addr, dst_port, proto, size); + + if (net->manage_connid) { + if (net->conn_freeids_ptr == GGNET_CONN_FREEIDS_COUNT) { + printf("ggnet_conn_add: ERROR: out of connection identifiers !"); + return NULL; + } + id = net->conn_freeids[net->conn_freeids_ptr]; + net->conn_freeids_ptr++; + } else { + id = given_id; + } srcnode = ggnet_node_find(net, src); if (!srcnode) srcnode = ggnet_node_add(net, src); srcnode->used++; if (srcnode->group) - srcnode->group->conn_count++; + srcnode->group->conn_count++; dstnode = ggnet_node_find(net, dst); if (!dstnode) dstnode = ggnet_node_add(net, dst); dstnode->used++; if (dstnode->group) - dstnode->group->conn_count++; + dstnode->group->conn_count++; c = malloc(sizeof(struct ggnet_conn)); - if (!c) { - printf("could not allocate conn\n"); - exit(1); - } + if (!c) { + printf("could not allocate conn\n"); + exit(1); + } c->id = id; c->state = CONNSTATE_ESTABLISHED; c->src = srcnode; @@ -224,12 +224,12 @@ void ggnet_conn_data(struct ggnet *net, struct ggnet_conn *c, int size, int response) { if (net->debug) - printf("ggnet_conn_data\n"); + printf("ggnet_conn_data\n"); - if (!response) - c->size = c->size + size; - else - c->size_response = c->size_response + size; + if (!response) + c->size = c->size + size; + else + c->size_response = c->size_response + size; c->lastseen = net->time; c->src->lastseen = net->time; c->dst->lastseen = net->time; @@ -239,38 +239,38 @@ void ggnet_conn_del(struct ggnet *net, struct ggnet_conn *c) { if (net->debug) - printf("ggnet_conn_del\n"); - - /* UNSUPPORTED - if (c->proto == IPPROTO_TCP) { - switch (c->state) { - case CONNSTATE_ESTABLISHED: - c->state = CONNSTATE_TCPFIN; - return; - case CONNSTATE_TCPFIN: - c->state = CONNSTATE_TCPFIN2; - return; - case CONNSTATE_TCPFIN2: - break; + printf("ggnet_conn_del\n"); + + /* UNSUPPORTED + if (c->proto == IPPROTO_TCP) { + switch (c->state) { + case CONNSTATE_ESTABLISHED: + c->state = CONNSTATE_TCPFIN; + return; + case CONNSTATE_TCPFIN: + c->state = CONNSTATE_TCPFIN2; + return; + case CONNSTATE_TCPFIN2: + break; + } + } + */ + + if (net->manage_connid) { + if (net->conn_freeids_ptr == 0) { + printf("FATAL: net->conn_freeids_ptr == 0\n"); + exit(1); } + net->conn_freeids_ptr--; + net->conn_freeids[net->conn_freeids_ptr] = c->id; } - */ - - if (net->manage_connid) { - if (net->conn_freeids_ptr == 0) { - printf("FATAL: net->conn_freeids_ptr == 0\n"); - exit(1); - } - net->conn_freeids_ptr--; - net->conn_freeids[net->conn_freeids_ptr] = c->id; - } c->src->used--; if (c->src->group) - c->src->group->conn_count--; + c->src->group->conn_count--; c->dst->used--; if (c->dst->group) - c->dst->group->conn_count--; + c->dst->group->conn_count--; LIST_REMOVE(c, entry); free(c); @@ -278,20 +278,20 @@ ggnet_conn_del(struct ggnet *net, struct ggnet_conn *c) struct ggnet_conn * ggnet_conn_find(struct ggnet *net, struct in_addr *src, int src_port, - struct in_addr *dst, int dst_port, int proto, int *response) + struct in_addr *dst, int dst_port, int proto, int *response) { - struct ggnet_conn *c; + struct ggnet_conn *c; LIST_FOREACH(c, &net->conn_list, entry) { if (((c->src->addr.s_addr == src->s_addr && - c->src_port == src_port && - c->dst->addr.s_addr == dst->s_addr && - c->dst_port == dst_port) || - (c->src->addr.s_addr == dst->s_addr && - c->src_port == dst_port && - c->dst->addr.s_addr == src->s_addr && - c->dst_port == src_port)) && - c->proto == proto) { + c->src_port == src_port && + c->dst->addr.s_addr == dst->s_addr && + c->dst_port == dst_port) || + (c->src->addr.s_addr == dst->s_addr && + c->src_port == dst_port && + c->dst->addr.s_addr == src->s_addr && + c->dst_port == src_port)) && + c->proto == proto) { if (c->src->addr.s_addr == src->s_addr) *response = 0; else @@ -305,122 +305,122 @@ ggnet_conn_find(struct ggnet *net, struct in_addr *src, int src_port, struct ggnet_conn * ggnet_conn_find_by_id(struct ggnet *net, int id) { - struct ggnet_conn *c; + struct ggnet_conn *c; LIST_FOREACH(c, &net->conn_list, entry) - if (c->id == id) - return c; - return NULL; + if (c->id == id) + return c; + return NULL; } struct ggnet_conn * ggnet_conn_find_by_node(struct ggnet *net, - struct ggnet_node *a, struct ggnet_node *b) + struct ggnet_node *a, struct ggnet_node *b) { - struct ggnet_conn *c; + struct ggnet_conn *c; LIST_FOREACH(c, &net->conn_list, entry) - if ((c->src == a && c->dst == b) || - (c->src == b && c->dst == a)) - return c; - return NULL; + if ((c->src == a && c->dst == b) || + (c->src == b && c->dst == a)) + return c; + return NULL; } void * ggnet_conn_usrdata_get(struct ggnet_conn *c) { - return c->usrdata; + return c->usrdata; } void ggnet_conn_usrdata_set(struct ggnet_conn *c, void *usrdata) { - c->usrdata = usrdata; + c->usrdata = usrdata; } void * ggnet_conn_src_get(struct ggnet_conn *c) { - return c->src; + return c->src; } void * ggnet_conn_dst_get(struct ggnet_conn *c) { - return c->dst; + return c->dst; } void * ggnet_nodegroup_usrdata_get(struct ggnet_nodegroup *g) { - return g->usrdata; + return g->usrdata; } void ggnet_nodegroup_usrdata_set(struct ggnet_nodegroup *g, void *usrdata) { - g->usrdata = usrdata; + g->usrdata = usrdata; } void ggnet_time_update(struct ggnet *net, time_t time) { - net->time = time; + net->time = time; } static struct ggnet_nodegroup * nodegroup_add(struct ggnet *net, enum ggnet_grouptype type, void *param, - struct ggnet_nodegroup *parent) + struct ggnet_nodegroup *parent) { - struct ggnet_nodegroup *group; - - group = calloc(1, sizeof(struct ggnet_nodegroup)); - if (!group) { - printf("could not allocate nodegroup\n"); - exit(1); - } - group->type = type; - if (parent) { - group->parent = parent; - parent->child_groups_count++; - } - - switch (type) { - case GROUP_ADDRESS: - group->addr.s_addr = *(u_int *)param; - break; - case GROUP_WHOIS: - case GROUP_DNS: - case GROUP_ROUTE: - /* UNSUPPORTED */ - free(group); - return NULL; - } - - LIST_INSERT_HEAD(&net->group_list, group, entry); - net->cb_addgroup(net, group, parent); - - return group; + struct ggnet_nodegroup *group; + + group = calloc(1, sizeof(struct ggnet_nodegroup)); + if (!group) { + printf("could not allocate nodegroup\n"); + exit(1); + } + group->type = type; + if (parent) { + group->parent = parent; + parent->child_groups_count++; + } + + switch (type) { + case GROUP_ADDRESS: + group->addr.s_addr = *(u_int *)param; + break; + case GROUP_WHOIS: + case GROUP_DNS: + case GROUP_ROUTE: + /* UNSUPPORTED */ + free(group); + return NULL; + } + + LIST_INSERT_HEAD(&net->group_list, group, entry); + net->cb_addgroup(net, group, parent); + + return group; } static void nodegroup_del(struct ggnet *net, struct ggnet_nodegroup *group) { - if (group->node_count || group->conn_count) { + if (group->node_count || group->conn_count) { printf("FATAL: ggnet_nodegroup_del: trying to remove a used group !\n"); exit(1); - } + } if (net->debug) - printf("ggnet_nodegroup_del\n"); - - net->cb_delgroup(net, group); - if (group->parent) { - group->parent->child_groups_count--; - if (group->parent->child_groups_count == 0) - nodegroup_del(net, group->parent); - } - LIST_REMOVE(group, entry); - free(group); + printf("ggnet_nodegroup_del\n"); + + net->cb_delgroup(net, group); + if (group->parent) { + group->parent->child_groups_count--; + if (group->parent->child_groups_count == 0) + nodegroup_del(net, group->parent); + } + LIST_REMOVE(group, entry); + free(group); } static struct ggnet_nodegroup * @@ -428,19 +428,19 @@ nodegroup_find(struct ggnet *net, enum ggnet_grouptype type, void *param) { struct ggnet_nodegroup *g; - LIST_FOREACH(g, &net->group_list, entry) { - switch (g->type) { - case GROUP_ADDRESS: - if (g->addr.s_addr == *(u_int *)param) - return g; - case GROUP_WHOIS: - case GROUP_DNS: - case GROUP_ROUTE: - /* UNSUPPORTED */ - break; - } - } - return NULL; + LIST_FOREACH(g, &net->group_list, entry) { + switch (g->type) { + case GROUP_ADDRESS: + if (g->addr.s_addr == *(u_int *)param) + return g; + case GROUP_WHOIS: + case GROUP_DNS: + case GROUP_ROUTE: + /* UNSUPPORTED */ + break; + } + } + return NULL; } static void @@ -449,48 +449,48 @@ nodegroup_set(struct ggnet *net, struct ggnet_node *n) struct ggnet_nodegroup *group, *groot, *gclassb; u_int addr, addr2; - addr = n->addr.s_addr & 0xffff0000; - - group = nodegroup_find(net, GROUP_ADDRESS, &addr); - if (!group) { - addr2 = 0x00000000; - groot = nodegroup_find(net, GROUP_ADDRESS, &addr2); - if (!groot) - groot = nodegroup_add(net, GROUP_ADDRESS, &addr2, NULL); - addr2 = addr & 0xff000000; - gclassb = nodegroup_find(net, GROUP_ADDRESS, &addr2); - if (!gclassb) - gclassb = nodegroup_add(net, GROUP_ADDRESS, &addr2, groot); - group = nodegroup_add(net, GROUP_ADDRESS, &addr, gclassb); - } - - n->group = group; - group->node_count++; + addr = n->addr.s_addr & 0xffff0000; + + group = nodegroup_find(net, GROUP_ADDRESS, &addr); + if (!group) { + addr2 = 0x00000000; + groot = nodegroup_find(net, GROUP_ADDRESS, &addr2); + if (!groot) + groot = nodegroup_add(net, GROUP_ADDRESS, &addr2, NULL); + addr2 = addr & 0xff000000; + gclassb = nodegroup_find(net, GROUP_ADDRESS, &addr2); + if (!gclassb) + gclassb = nodegroup_add(net, GROUP_ADDRESS, &addr2, groot); + group = nodegroup_add(net, GROUP_ADDRESS, &addr, gclassb); + } + + n->group = group; + group->node_count++; } static void nodegroup_unset(struct ggnet *net, struct ggnet_node *n) { - if (!n->group) - return; + if (!n->group) + return; - n->group->node_count--; - if (n->group->node_count == 0) - nodegroup_del(net, n->group); + n->group->node_count--; + if (n->group->node_count == 0) + nodegroup_del(net, n->group); } static void _cb_dns_reverse(struct in_addr *ip, char *name, void *data) { - struct ggnet *net; - struct ggnet_node *n; - - n = data; - net = n->net; - n->dns_req = NULL; - if (name) { - snprintf(n->fqdn, sizeof(n->fqdn), "%s", name); - net->cb_nodename(net, n); - } + struct ggnet *net; + struct ggnet_node *n; + + n = data; + net = n->net; + n->dns_req = NULL; + if (name) { + snprintf(n->fqdn, sizeof(n->fqdn), "%s", name); + net->cb_nodename(net, n); + } } diff --git a/libglouglou/libggnet.h b/libglouglou/libggnet.h index 1b17a32..3caea7c 100644 --- a/libglouglou/libggnet.h +++ b/libglouglou/libggnet.h @@ -23,10 +23,10 @@ #define GGNET_GROUPING_FALSE 1 enum ggnet_grouptype { - GROUP_ADDRESS, - GROUP_WHOIS, - GROUP_DNS, - GROUP_ROUTE + GROUP_ADDRESS, + GROUP_WHOIS, + GROUP_DNS, + GROUP_ROUTE }; struct ggnet_nodegroup { @@ -34,11 +34,11 @@ struct ggnet_nodegroup { struct ggnet_nodegroup *parent; enum ggnet_grouptype type; struct in_addr addr; - char name[GGNET_DNSNAME_MAX]; - int node_count; - int conn_count; - int child_groups_count; - void *usrdata; + char name[GGNET_DNSNAME_MAX]; + int node_count; + int conn_count; + int child_groups_count; + void *usrdata; }; struct ggnet_node { @@ -61,72 +61,76 @@ enum ggnet_connstate { struct ggnet_conn { LIST_ENTRY(ggnet_conn) entry; - u_int id; + u_int id; enum ggnet_connstate state; - struct ggnet_node *src; - u_int src_port; - struct ggnet_node *dst; - u_int dst_port; - u_int proto; - u_int size; - u_int size_response; - time_t lastseen; - void *usrdata; + struct ggnet_node *src; + u_int src_port; + struct ggnet_node *dst; + u_int dst_port; + u_int proto; + u_int size; + u_int size_response; + time_t lastseen; + void *usrdata; }; struct ggnet { LIST_HEAD(, ggnet_conn) conn_list; LIST_HEAD(, ggnet_node) node_list; LIST_HEAD(, ggnet_nodegroup) group_list; - int conn_count; - int node_count; - u_int16_t conn_freeids[GGNET_CONN_FREEIDS_COUNT]; - int conn_freeids_ptr; - int manage_connid; - time_t time; - int debug; + int conn_count; + int node_count; + u_int16_t conn_freeids[GGNET_CONN_FREEIDS_COUNT]; + int conn_freeids_ptr; + int manage_connid; + time_t time; + int debug; /* grouping */ - int use_grouping; - void (*cb_addgroup)(struct ggnet *, struct ggnet_nodegroup *, struct ggnet_nodegroup *); - void (*cb_delgroup)(struct ggnet *, struct ggnet_nodegroup *); - /* dns */ - int use_dns; + int use_grouping; + void (*cb_addgroup)(struct ggnet *, struct ggnet_nodegroup *, struct ggnet_nodegroup *); + void (*cb_delgroup)(struct ggnet *, struct ggnet_nodegroup *); + /* dns */ + int use_dns; struct ggnet_dns *ggdns; - void (*cb_nodename)(struct ggnet *, struct ggnet_node *); + void (*cb_nodename)(struct ggnet *, struct ggnet_node *); }; -struct ggnet *ggnet_new(int); -void ggnet_set_grouping(struct ggnet *, int, - void (*cb_addgroup)(struct ggnet *, struct ggnet_nodegroup *, struct ggnet_nodegroup *), - void (*cb_delgroup)(struct ggnet *, struct ggnet_nodegroup *)); -void ggnet_set_dns(struct ggnet *net, int set, - struct event_base *ev_base, - void (*cb_nodename)(struct ggnet *, struct ggnet_node *)); -void ggnet_debug_set(struct ggnet *, int); -void ggnet_free(struct ggnet *); -struct ggnet_node *ggnet_node_add(struct ggnet *, struct in_addr *); -void ggnet_node_del(struct ggnet *, struct ggnet_node *); -struct ggnet_node *ggnet_node_find(struct ggnet *, struct in_addr *); -void *ggnet_node_usrdata_get(struct ggnet_node *); -void ggnet_node_usrdata_set(struct ggnet_node *, void *); +struct ggnet *ggnet_new(int); +void ggnet_set_grouping(struct ggnet *, int, + void (*cb_addgroup)(struct ggnet *, + struct ggnet_nodegroup *, + struct ggnet_nodegroup *), + void (*cb_delgroup)(struct ggnet *, + struct ggnet_nodegroup *)); +void ggnet_set_dns(struct ggnet *net, int set, + struct event_base *ev_base, + void (*cb_nodename)(struct ggnet *, + struct ggnet_node *)); +void ggnet_debug_set(struct ggnet *, int); +void ggnet_free(struct ggnet *); +struct ggnet_node *ggnet_node_add(struct ggnet *, struct in_addr *); +void ggnet_node_del(struct ggnet *, struct ggnet_node *); +struct ggnet_node *ggnet_node_find(struct ggnet *, struct in_addr *); +void *ggnet_node_usrdata_get(struct ggnet_node *); +void ggnet_node_usrdata_set(struct ggnet_node *, void *); struct ggnet_nodegroup *ggnet_node_group_get(struct ggnet_node *); -struct ggnet_conn *ggnet_conn_add(struct ggnet *, struct in_addr *, int, - struct in_addr *, int, int, int, int); -void ggnet_conn_data(struct ggnet *, struct ggnet_conn *, - int, int); -void ggnet_conn_del(struct ggnet *, struct ggnet_conn *); -struct ggnet_conn *ggnet_conn_find(struct ggnet *, struct in_addr *, int, - struct in_addr *, int, int, int *); -struct ggnet_conn *ggnet_conn_find_by_id(struct ggnet *, int); -struct ggnet_conn *ggnet_conn_find_by_node(struct ggnet *, struct ggnet_node *, - struct ggnet_node *); -void *ggnet_conn_usrdata_get(struct ggnet_conn *); -void ggnet_conn_usrdata_set(struct ggnet_conn *, void *); -void *ggnet_conn_src_get(struct ggnet_conn *); -void *ggnet_conn_dst_get(struct ggnet_conn *); -void *ggnet_nodegroup_usrdata_get(struct ggnet_nodegroup *); -void ggnet_nodegroup_usrdata_set(struct ggnet_nodegroup *, - void *); -void ggnet_time_update(struct ggnet *, time_t); +struct ggnet_conn *ggnet_conn_add(struct ggnet *, struct in_addr *, int, + struct in_addr *, int, int, int, int); +void ggnet_conn_data(struct ggnet *, struct ggnet_conn *, + int, int); +void ggnet_conn_del(struct ggnet *, struct ggnet_conn *); +struct ggnet_conn *ggnet_conn_find(struct ggnet *, struct in_addr *, int, + struct in_addr *, int, int, int *); +struct ggnet_conn *ggnet_conn_find_by_id(struct ggnet *, int); +struct ggnet_conn *ggnet_conn_find_by_node(struct ggnet *, + struct ggnet_node *, struct ggnet_node *); +void *ggnet_conn_usrdata_get(struct ggnet_conn *); +void ggnet_conn_usrdata_set(struct ggnet_conn *, void *); +void *ggnet_conn_src_get(struct ggnet_conn *); +void *ggnet_conn_dst_get(struct ggnet_conn *); +void *ggnet_nodegroup_usrdata_get(struct ggnet_nodegroup *); +void ggnet_nodegroup_usrdata_set(struct ggnet_nodegroup *, + void *); +void ggnet_time_update(struct ggnet *, time_t); #endif /* _LIBGGNET_H_ */ diff --git a/libglouglou/libggnet_dns.c b/libglouglou/libggnet_dns.c index 25bada3..c59c8a7 100644 --- a/libglouglou/libggnet_dns.c +++ b/libglouglou/libggnet_dns.c @@ -16,101 +16,101 @@ static void _cb_evdns_reverse(int, char, int, int, void *, void *); struct ggnet_dns * ggnet_dns_new(struct event_base *ev_base) { - struct ggnet_dns *ggdns = NULL; - - ggdns = calloc(1, sizeof(struct ggnet_dns)); - if (!ggdns) { - printf("could not allocate ggnet_dns\n"); - exit(1); - } - ggdns->ev_base = ev_base; - ggdns->evdns_base = evdns_base_new(ev_base, 1); - if (!ggdns->evdns_base) - goto err; - return ggdns; + struct ggnet_dns *ggdns = NULL; + + ggdns = calloc(1, sizeof(struct ggnet_dns)); + if (!ggdns) { + printf("could not allocate ggnet_dns\n"); + exit(1); + } + ggdns->ev_base = ev_base; + ggdns->evdns_base = evdns_base_new(ev_base, 1); + if (!ggdns->evdns_base) + goto err; + return ggdns; err: - if (ggdns) - free(ggdns); - return NULL; + if (ggdns) + free(ggdns); + return NULL; } void ggnet_dns_free(struct ggnet_dns *ggdns) { - evdns_base_free(ggdns->evdns_base, 1); - free(ggdns); + evdns_base_free(ggdns->evdns_base, 1); + free(ggdns); } struct ggnet_dns_req * ggnet_dns_reverse(struct ggnet_dns *ggdns, - struct in_addr *ip, - void (*cb_usr)(struct in_addr *, char *, void *), - void *data) + struct in_addr *ip, + void (*cb_usr)(struct in_addr *, char *, void *), + void *data) { - struct evutil_addrinfo hints; - struct ggnet_dns_req *req; - struct evdns_request *ereq; - struct in_addr nip; - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_flags = EVUTIL_AI_CANONNAME; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - - req = malloc(sizeof(struct ggnet_dns_req)); - if (!req) { - printf("could not allocate ggnet_dns_req\n"); - exit(1); - } - req->ggdns = ggdns; - memcpy(&req->ip, ip, sizeof(struct in_addr)); - req->cb_usr = cb_usr; - req->data = data; - - nip.s_addr = htonl(ip->s_addr); /* evdns eats network byte order */ - - LIST_INSERT_HEAD(&ggdns->req_list, req, entry); - ggdns->req_pending++; - ereq = evdns_base_resolve_reverse(ggdns->evdns_base, &nip, 0, - _cb_evdns_reverse, req); - if (ereq == NULL) { - printf("libggnet_dns WARNING: dns request for %d returned immediately\n", - ip->s_addr); - /* remove req from list and free it happened in the callback. */ - return NULL; - } - req->ereq = ereq; - - return req; + struct evutil_addrinfo hints; + struct ggnet_dns_req *req; + struct evdns_request *ereq; + struct in_addr nip; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_flags = EVUTIL_AI_CANONNAME; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + + req = malloc(sizeof(struct ggnet_dns_req)); + if (!req) { + printf("could not allocate ggnet_dns_req\n"); + exit(1); + } + req->ggdns = ggdns; + memcpy(&req->ip, ip, sizeof(struct in_addr)); + req->cb_usr = cb_usr; + req->data = data; + + nip.s_addr = htonl(ip->s_addr); /* evdns eats network byte order */ + + LIST_INSERT_HEAD(&ggdns->req_list, req, entry); + ggdns->req_pending++; + ereq = evdns_base_resolve_reverse(ggdns->evdns_base, &nip, 0, + _cb_evdns_reverse, req); + if (ereq == NULL) { + printf("libggnet_dns WARNING: dns request for %d returned immediately\n", + ip->s_addr); + /* remove req from list and free it happened in the callback. */ + return NULL; + } + req->ereq = ereq; + + return req; } void ggnet_dns_cancel(struct ggnet_dns *ggdns, struct ggnet_dns_req *req) { - evdns_cancel_request(ggdns->evdns_base, req->ereq); - /* remove req from list and free it happened in the callback. */ + evdns_cancel_request(ggdns->evdns_base, req->ereq); + /* remove req from list and free it happened in the callback. */ } static void _cb_evdns_reverse(int result, char type, int count, - int ttl, void *addresses, void *arg) + int ttl, void *addresses, void *arg) { - struct ggnet_dns_req *req; - char **name; - - if (count > 1) - printf("libggnet_dns XXX: has %d PTR records !\n", count); // XXX - - req = arg; - name = addresses; - if (result != DNS_ERR_NONE || count == 0) - req->cb_usr(&req->ip, NULL, req->data); - else - req->cb_usr(&req->ip, *name, req->data); - - LIST_REMOVE(req, entry); - req->ggdns->req_pending--; - free(req); + struct ggnet_dns_req *req; + char **name; + + if (count > 1) + printf("libggnet_dns XXX: has %d PTR records !\n", count); // XXX + + req = arg; + name = addresses; + if (result != DNS_ERR_NONE || count == 0) + req->cb_usr(&req->ip, NULL, req->data); + else + req->cb_usr(&req->ip, *name, req->data); + + LIST_REMOVE(req, entry); + req->ggdns->req_pending--; + free(req); } diff --git a/libglouglou/libggnet_dns.h b/libglouglou/libggnet_dns.h index ae7b634..9fc716d 100644 --- a/libglouglou/libggnet_dns.h +++ b/libglouglou/libggnet_dns.h @@ -9,26 +9,26 @@ #endif struct ggnet_dns_req { - LIST_ENTRY(ggnet_dns_req) entry; - struct ggnet_dns *ggdns; - struct evdns_request *ereq; - struct in_addr ip; - char *name; - void (*cb_usr)(struct in_addr *, char *, void *); - void *data; + LIST_ENTRY(ggnet_dns_req) entry; + struct ggnet_dns *ggdns; + struct evdns_request *ereq; + struct in_addr ip; + char *name; + void (*cb_usr)(struct in_addr *, char *, void *); + void *data; }; struct ggnet_dns { - struct event_base *ev_base; - struct evdns_base *evdns_base; - int req_pending; - LIST_HEAD(, ggnet_dns_req) req_list; /* XXX for now unused. remove ? */ + struct event_base *ev_base; + struct evdns_base *evdns_base; + int req_pending; + LIST_HEAD(, ggnet_dns_req) req_list; /* XXX for now unused. remove ? */ }; -struct ggnet_dns *ggnet_dns_new(struct event_base *); -void ggnet_dns_free(struct ggnet_dns *); -struct ggnet_dns_req *ggnet_dns_reverse(struct ggnet_dns *, struct in_addr *, - void (*cb_usr)(struct in_addr *, char *, void *), - void *); -void ggnet_dns_cancel(struct ggnet_dns *, - struct ggnet_dns_req *); +struct ggnet_dns *ggnet_dns_new(struct event_base *); +void ggnet_dns_free(struct ggnet_dns *); +struct ggnet_dns_req *ggnet_dns_reverse(struct ggnet_dns *, struct in_addr *, + void (*cb_usr)(struct in_addr *, char *, void *), + void *); +void ggnet_dns_cancel(struct ggnet_dns *, + struct ggnet_dns_req *); diff --git a/libglouglou/libglouglou.c b/libglouglou/libglouglou.c index 79f57c6..972bb07 100644 --- a/libglouglou/libglouglou.c +++ b/libglouglou/libglouglou.c @@ -16,46 +16,46 @@ #include "sendbuf.h" #define error(fmt, ...) \ - if (_verbosity >= 0) \ - printf("libgg: %s: ERROR: " fmt "\n", __func__, ##__VA_ARGS__) + if (_verbosity >= 0) \ +printf("libgg: %s: ERROR: " fmt "\n", __func__, ##__VA_ARGS__) #define verbose(fmt, ...) \ - if (_verbosity >= 1) \ - printf("libgg: %s: " fmt "\n", __func__, ##__VA_ARGS__) + if (_verbosity >= 1) \ +printf("libgg: %s: " fmt "\n", __func__, ##__VA_ARGS__) #define debug(fmt, ...) \ - if (_verbosity >= 2) \ - printf("libgg: %s: XXX: " fmt "\n", __func__, ##__VA_ARGS__) - -void cb_srv_receive(evutil_socket_t, short, void *); -struct gg_user *user_add(struct gg_server *, struct sockaddr_in *); -void user_del(struct gg_server *, struct gg_user *); -struct gg_user * user_find(struct gg_server *, struct sockaddr_in *); -int user_send(struct gg_user *, void *, int); -int cb_usr_send(void *, int, void *); -int client_send(struct gg_client *, void *, int); -void cb_cli_receive(evutil_socket_t, short, void *); -void cb_cli_timer(evutil_socket_t, short, void *); -int cb_cli_send(void *, int, void *); + if (_verbosity >= 2) \ +printf("libgg: %s: XXX: " fmt "\n", __func__, ##__VA_ARGS__) + +void cb_srv_receive(evutil_socket_t, short, void *); +struct gg_user *user_add(struct gg_server *, struct sockaddr_in *); +void user_del(struct gg_server *, struct gg_user *); +struct gg_user *user_find(struct gg_server *, struct sockaddr_in *); +int user_send(struct gg_user *, void *, int); +int cb_usr_send(void *, int, void *); +int client_send(struct gg_client *, void *, int); +void cb_cli_receive(evutil_socket_t, short, void *); +void cb_cli_timer(evutil_socket_t, short, void *); +int cb_cli_send(void *, int, void *); struct gg_packet *pkt_decode(char **, int *); -int pkt_getsize(struct gg_packet *); -int pkt_encode(struct gg_packet *, struct gg_packet *); +int pkt_getsize(struct gg_packet *); +int pkt_encode(struct gg_packet *, struct gg_packet *); int _verbosity = 0; gg_packet_props_t gg_packet_props[] = { - [PACKET_NEWCONN] = \ - { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.newconn) }, - [PACKET_DELCONN] = \ - { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.delconn) }, - [PACKET_DATA] = \ - { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.data) }, - [PACKET_NAME] = \ - { ((PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.name) - GG_PKTARG_MAX) }, - [PACKET_FORK] = \ - { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.fork) }, - [PACKET_EXEC] = \ - { ((PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.exec) - GG_PKTARG_MAX) }, - [PACKET_EXIT] = \ - { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.exit) }, + [PACKET_NEWCONN] = \ + { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.newconn) }, + [PACKET_DELCONN] = \ + { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.delconn) }, + [PACKET_DATA] = \ + { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.data) }, + [PACKET_NAME] = \ + { ((PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.name) - GG_PKTARG_MAX) }, + [PACKET_FORK] = \ + { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.fork) }, + [PACKET_EXEC] = \ + { ((PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.exec) - GG_PKTARG_MAX) }, + [PACKET_EXIT] = \ + { (PACKET_HEADER_SIZE + sizeof((struct gg_packet *)0)->pdat.exit) }, }; /* @@ -70,30 +70,30 @@ gg_packet_props_t gg_packet_props[] = { */ struct gg_server * gg_server_start(struct event_base *ev_base, char *ip, int port, - int (*handle_conn)(struct gg_server *, struct gg_user *), - int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *), - void *usrdata) + int (*handle_conn)(struct gg_server *, struct gg_user *), + int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *), + void *usrdata) { - struct gg_server *srv; - struct sockaddr_in sock_addr; - struct event *ev; - int s; - int sock_on = 1; - - srv = xcalloc(1, sizeof(struct gg_server)); - srv->ev_base = ev_base; - srv->ip = ip; - srv->port = port; - srv->handle_conn = handle_conn; - srv->handle_packet = handle_packet; - srv->usrdata = usrdata; - - s = socket(AF_INET, SOCK_DGRAM, 0); - if (s < 0) - goto err; - srv->sock = s; + struct gg_server *srv; + struct sockaddr_in sock_addr; + struct event *ev; + int s; + int sock_on = 1; + + srv = xcalloc(1, sizeof(struct gg_server)); + srv->ev_base = ev_base; + srv->ip = ip; + srv->port = port; + srv->handle_conn = handle_conn; + srv->handle_packet = handle_packet; + srv->usrdata = usrdata; + + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s < 0) + goto err; + srv->sock = s; setsockopt(s, SOL_SOCKET, SO_REUSEADDR, - &sock_on, sizeof(sock_on)); + &sock_on, sizeof(sock_on)); fd_nonblock(s); bzero(&sock_addr, sizeof(sock_addr)); @@ -103,97 +103,98 @@ gg_server_start(struct event_base *ev_base, char *ip, int port, addrcpy(&srv->addr, &sock_addr); if (bind(s, (struct sockaddr *)&sock_addr, - sizeof(sock_addr)) != 0) + sizeof(sock_addr)) != 0) goto err; - ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_srv_receive, srv); - event_add(ev, NULL); - srv->ev = ev; + ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_srv_receive, srv); + event_add(ev, NULL); + srv->ev = ev; - return srv; + return srv; err: - error("%s", strerror(errno)); - gg_server_stop(srv); - return NULL; + error("%s", strerror(errno)); + gg_server_stop(srv); + return NULL; } void gg_server_stop(struct gg_server *srv) { - struct gg_user *usr; - - while ((usr = LIST_FIRST(&srv->user_list))) { - user_del(srv, usr); - } - if (srv->sock) - close(srv->sock); - if (srv->ev) - event_del(srv->ev); - free(srv); + struct gg_user *usr; + + while ((usr = LIST_FIRST(&srv->user_list))) { + user_del(srv, usr); + } + if (srv->sock) + close(srv->sock); + if (srv->ev) + event_del(srv->ev); + free(srv); } int gg_server_send(struct gg_server *srv, struct gg_packet *pkt, struct gg_user *usr) { - static struct gg_packet *newpkt; - static struct gg_packet pktbuf; - struct gg_user *u; - int size; - int res = 0; - - /* optimisation to use sendbuf_gettoken */ - if (!usr && srv->user_count == 1) - usr = LIST_FIRST(&srv->user_list); - - if (usr) { - size = pkt_getsize(pkt); - newpkt = sendbuf_gettoken(usr->sbuf, size); - if (!newpkt) - return -1; - return pkt_encode(pkt, newpkt); - } else { - size = pkt_encode(pkt, &pktbuf); - LIST_FOREACH(u, &srv->user_list, entry) { - res = res + sendbuf_append(u->sbuf, &pktbuf, size); - } - } - - return res; + static struct gg_packet *newpkt; + static struct gg_packet pktbuf; + struct gg_user *u; + int size; + int res = 0; + + /* optimisation to use sendbuf_gettoken */ + if (!usr && srv->user_count == 1) + usr = LIST_FIRST(&srv->user_list); + + if (usr) { + size = pkt_getsize(pkt); + newpkt = sendbuf_gettoken(usr->sbuf, size); + if (!newpkt) + return -1; + return pkt_encode(pkt, newpkt); + } else { + size = pkt_encode(pkt, &pktbuf); + LIST_FOREACH(u, &srv->user_list, entry) { + res = res + sendbuf_append(u->sbuf, &pktbuf, size); + } + } + + return res; } void gg_server_send_flush(struct gg_server *srv, struct gg_user *usr) { - struct gg_user *u; + struct gg_user *u; - if (usr) - sendbuf_flush(usr->sbuf); - else - LIST_FOREACH(u, &srv->user_list, entry) - sendbuf_flush(u->sbuf); + if (usr) + sendbuf_flush(usr->sbuf); + else + LIST_FOREACH(u, &srv->user_list, entry) + sendbuf_flush(u->sbuf); } /* * Server - private */ -void cb_srv_receive(evutil_socket_t fd, short what, void *arg) +void +cb_srv_receive(evutil_socket_t fd, short what, void *arg) { - struct gg_server *srv; - struct gg_user *usr; - struct gg_packet *pkt; + struct gg_server *srv; + struct gg_user *usr; + struct gg_packet *pkt; struct sockaddr_in remote; - socklen_t remote_len; - char buf[PACKET_BUFFER_SIZE]; - char *buf_p; + socklen_t remote_len; + char buf[PACKET_BUFFER_SIZE]; + char *buf_p; int buf_len; int len; - srv = arg; - remote_len = sizeof(struct sockaddr_in); + srv = arg; + remote_len = sizeof(struct sockaddr_in); len = recvfrom(fd, buf, sizeof(buf), 0, - (struct sockaddr *)&remote, &remote_len); + (struct sockaddr *)&remote, &remote_len); if (len < 0) { error("recvfrom failed"); return; @@ -201,67 +202,67 @@ void cb_srv_receive(evutil_socket_t fd, short what, void *arg) usr = user_find(srv, &remote); if (!usr) { - usr = user_add(srv, &remote); - if (srv->handle_conn) - srv->handle_conn(srv, usr); - user_send(usr, "", 0); - } else { - debug("Incoming data from existing user !"); - if (len == 0) { - user_del(srv, usr); - return; - } - if (srv->handle_packet) { - buf_p = buf; - buf_len = len; - while (buf_len > 0 && (pkt = pkt_decode(&buf_p, &buf_len))) - srv->handle_packet(srv, usr, pkt); - if (buf_len > 0) { - /* XXX store incomplete packet for next recv */ - error("incomplete packet, dropped %d bytes !", buf_len); - } - } - } + usr = user_add(srv, &remote); + if (srv->handle_conn) + srv->handle_conn(srv, usr); + user_send(usr, "", 0); + } else { + debug("Incoming data from existing user !"); + if (len == 0) { + user_del(srv, usr); + return; + } + if (srv->handle_packet) { + buf_p = buf; + buf_len = len; + while (buf_len > 0 && (pkt = pkt_decode(&buf_p, &buf_len))) + srv->handle_packet(srv, usr, pkt); + if (buf_len > 0) { + /* XXX store incomplete packet for next recv */ + error("incomplete packet, dropped %d bytes !", buf_len); + } + } + } } struct gg_user * user_add(struct gg_server *srv, struct sockaddr_in *remote) { - struct gg_user *usr; - struct sendbuf *sbuf; + struct gg_user *usr; + struct sendbuf *sbuf; - usr = xcalloc(1, sizeof(struct gg_user)); - usr->id = srv->user_id_count; - srv->user_id_count++; - srv->user_count++; - usr->sock = srv->sock; - addrcpy(&usr->addr, remote); + usr = xcalloc(1, sizeof(struct gg_user)); + usr->id = srv->user_id_count; + srv->user_id_count++; + srv->user_count++; + usr->sock = srv->sock; + addrcpy(&usr->addr, remote); - sbuf = sendbuf_new(srv->ev_base, PACKET_SNDBUF_MAX, 200, cb_usr_send, usr); - if (!sbuf) - goto err; - usr->sbuf = sbuf; + sbuf = sendbuf_new(srv->ev_base, PACKET_SNDBUF_MAX, 200, cb_usr_send, usr); + if (!sbuf) + goto err; + usr->sbuf = sbuf; - LIST_INSERT_HEAD(&srv->user_list, usr, entry); - verbose("Add user %d !", usr->id); + LIST_INSERT_HEAD(&srv->user_list, usr, entry); + verbose("Add user %d !", usr->id); - return usr; + return usr; err: - user_del(srv, usr); - return NULL; + user_del(srv, usr); + return NULL; } void user_del(struct gg_server *srv, struct gg_user *usr) { - verbose("Del user %d !", usr->id); - if (usr->sbuf) - sendbuf_free(usr->sbuf); - user_send(usr, "", 0); - LIST_REMOVE(usr, entry); - srv->user_count--; - free(usr); + verbose("Del user %d !", usr->id); + if (usr->sbuf) + sendbuf_free(usr->sbuf); + user_send(usr, "", 0); + LIST_REMOVE(usr, entry); + srv->user_count--; + free(usr); } struct gg_user * @@ -273,7 +274,7 @@ user_find(struct gg_server *srv, struct sockaddr_in *remote) LIST_FOREACH(usr, &srv->user_list, entry) { u = &usr->addr; if (u->sin_addr.s_addr == remote->sin_addr.s_addr && - u->sin_port == remote->sin_port) + u->sin_port == remote->sin_port) return usr; } return NULL; @@ -282,22 +283,22 @@ user_find(struct gg_server *srv, struct sockaddr_in *remote) int user_send(struct gg_user *usr, void *data, int size) { - int sent; + int sent; - sent = sendto(usr->sock, data, size, 0, (struct sockaddr *)&usr->addr, - sizeof(struct sockaddr_in)); - if (sent == -1) - error("failed: %s", strerror(errno)); - return sent; + sent = sendto(usr->sock, data, size, 0, (struct sockaddr *)&usr->addr, + sizeof(struct sockaddr_in)); + if (sent == -1) + error("failed: %s", strerror(errno)); + return sent; } int cb_usr_send(void *data, int size, void *usrdata) { - struct gg_user *usr; + struct gg_user *usr; - usr = usrdata; - return user_send(usr, data, size); + usr = usrdata; + return user_send(usr, data, size); } /* @@ -312,96 +313,96 @@ cb_usr_send(void *data, int size, void *usrdata) */ struct gg_client * gg_client_connect(struct event_base *ev_base, char *ip, int port, - int (*handle_conn)(struct gg_client *), - int (*handle_packet)(struct gg_client *, struct gg_packet *), - void *usrdata) + int (*handle_conn)(struct gg_client *), + int (*handle_packet)(struct gg_client *, struct gg_packet *), + void *usrdata) { - struct gg_client *cli; - struct sockaddr_in sock_addr; - struct sendbuf *sbuf; - struct event *ev; - struct timeval tv; - int s; - int sock_on = 1; - - cli = xcalloc(1, sizeof(struct gg_client)); - cli->ev_base = ev_base; - cli->ip = ip; - cli->port = port; - cli->handle_conn = handle_conn; - cli->handle_packet = handle_packet; - cli->usrdata = usrdata; - cli->status = GG_CLIENT_STATUS_CONNECTING; - s = socket(AF_INET, SOCK_DGRAM, 0); - if (s < 0) - goto err; - cli->sock = s; + struct gg_client *cli; + struct sockaddr_in sock_addr; + struct sendbuf *sbuf; + struct event *ev; + struct timeval tv; + int s; + int sock_on = 1; + + cli = xcalloc(1, sizeof(struct gg_client)); + cli->ev_base = ev_base; + cli->ip = ip; + cli->port = port; + cli->handle_conn = handle_conn; + cli->handle_packet = handle_packet; + cli->usrdata = usrdata; + cli->status = GG_CLIENT_STATUS_CONNECTING; + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s < 0) + goto err; + cli->sock = s; setsockopt(s, SOL_SOCKET, SO_REUSEADDR, - &sock_on, sizeof(sock_on)); + &sock_on, sizeof(sock_on)); fd_nonblock(s); bzero(&sock_addr, sizeof(sock_addr)); - sock_addr.sin_family = AF_INET; - sock_addr.sin_addr.s_addr=inet_addr(ip); - sock_addr.sin_port=htons(port); - addrcpy(&cli->addr, &sock_addr); - - ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_cli_receive, cli); - event_add(ev, NULL); - cli->ev = ev; - - ev = evtimer_new(ev_base, cb_cli_timer, cli); - cli->ev_timer = ev; - bzero(&tv, sizeof(struct timeval)); - tv.tv_sec = 0; - evtimer_add(ev, &tv); - - sbuf = sendbuf_new(cli->ev_base, PACKET_SNDBUF_MAX, 200, cb_cli_send, cli); - if (!sbuf) - goto err; - cli->sbuf = sbuf; + sock_addr.sin_family = AF_INET; + sock_addr.sin_addr.s_addr=inet_addr(ip); + sock_addr.sin_port=htons(port); + addrcpy(&cli->addr, &sock_addr); + + ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_cli_receive, cli); + event_add(ev, NULL); + cli->ev = ev; + + ev = evtimer_new(ev_base, cb_cli_timer, cli); + cli->ev_timer = ev; + bzero(&tv, sizeof(struct timeval)); + tv.tv_sec = 0; + evtimer_add(ev, &tv); + + sbuf = sendbuf_new(cli->ev_base, PACKET_SNDBUF_MAX, 200, cb_cli_send, cli); + if (!sbuf) + goto err; + cli->sbuf = sbuf; - return cli; + return cli; err: - error("Error ! %s", strerror(errno)); - gg_client_disconnect(cli); - return NULL; + error("Error ! %s", strerror(errno)); + gg_client_disconnect(cli); + return NULL; } void gg_client_disconnect(struct gg_client *cli) { - if (cli->sbuf) - sendbuf_free(cli->sbuf); - if (cli->sock) { - client_send(cli, "", 0); - close(cli->sock); - } - if (cli->ev) - event_del(cli->ev); - if (cli->ev_timer) - event_del(cli->ev_timer); - free(cli); + if (cli->sbuf) + sendbuf_free(cli->sbuf); + if (cli->sock) { + client_send(cli, "", 0); + close(cli->sock); + } + if (cli->ev) + event_del(cli->ev); + if (cli->ev_timer) + event_del(cli->ev_timer); + free(cli); } int gg_client_send(struct gg_client *cli, struct gg_packet *pkt) { - static struct gg_packet *newpkt; - int size; - - size = pkt_getsize(pkt); - newpkt = sendbuf_gettoken(cli->sbuf, size); - if (!newpkt) - return -1; - return pkt_encode(pkt, newpkt); + static struct gg_packet *newpkt; + int size; + + size = pkt_getsize(pkt); + newpkt = sendbuf_gettoken(cli->sbuf, size); + if (!newpkt) + return -1; + return pkt_encode(pkt, newpkt); } void gg_client_send_flush(struct gg_client *cli) { - sendbuf_flush(cli->sbuf); + sendbuf_flush(cli->sbuf); } /* @@ -411,95 +412,97 @@ gg_client_send_flush(struct gg_client *cli) int client_send(struct gg_client *cli, void *data, int size) { - int sent; + int sent; - sent = sendto(cli->sock, data, size, 0, (struct sockaddr *)&cli->addr, - sizeof(struct sockaddr_in)); - if (sent == -1) - error("failed: %s", strerror(errno)); - return sent; + sent = sendto(cli->sock, data, size, 0, (struct sockaddr *)&cli->addr, + sizeof(struct sockaddr_in)); + if (sent == -1) + error("failed: %s", strerror(errno)); + return sent; } -void cb_cli_receive(evutil_socket_t fd, short what, void *arg) +void +cb_cli_receive(evutil_socket_t fd, short what, void *arg) { - struct gg_client *cli; - struct gg_packet *pkt; - struct sockaddr_in remote; + struct gg_client *cli; + struct gg_packet *pkt; + struct sockaddr_in remote; socklen_t remote_len; - char buf[PACKET_BUFFER_SIZE]; - char *buf_p; - int buf_len; - int len; + char buf[PACKET_BUFFER_SIZE]; + char *buf_p; + int buf_len; + int len; - cli = arg; - remote_len = sizeof(struct sockaddr_in); + cli = arg; + remote_len = sizeof(struct sockaddr_in); len = recvfrom(fd, buf, sizeof(buf), 0, - (struct sockaddr *)&remote, &remote_len); + (struct sockaddr *)&remote, &remote_len); if (addrcmp(&cli->addr, &remote)) { - error("receiving from stranger !"); - return; - } + error("receiving from stranger !"); + return; + } if (len < 0) { error("recvfrom failed"); return; } - switch (cli->status) { - case GG_CLIENT_STATUS_CONNECTING: - verbose("Connected !"); - cli->status = GG_CLIENT_STATUS_CONNECTED; - if (cli->handle_conn) - cli->handle_conn(cli); - break; - case GG_CLIENT_STATUS_CONNECTED: - if (len == 0) { - verbose("libglouglou: cb_cli_receive: recvfrom = 0"); - cli->status = GG_CLIENT_STATUS_CONNECTING; - return; - } - debug("Incoming data !"); - if (cli->handle_packet) { - buf_p = buf; - buf_len = len; - while (buf_len > 0 && (pkt = pkt_decode(&buf_p, &buf_len))) - cli->handle_packet(cli, pkt); - if (buf_len > 0) { - /* XXX store incomplete packet for next recv */ - error("incomplete packet, dropped %d bytes !", buf_len); - } - } - break; - } + switch (cli->status) { + case GG_CLIENT_STATUS_CONNECTING: + verbose("Connected !"); + cli->status = GG_CLIENT_STATUS_CONNECTED; + if (cli->handle_conn) + cli->handle_conn(cli); + break; + case GG_CLIENT_STATUS_CONNECTED: + if (len == 0) { + verbose("libglouglou: cb_cli_receive: recvfrom = 0"); + cli->status = GG_CLIENT_STATUS_CONNECTING; + return; + } + debug("Incoming data !"); + if (cli->handle_packet) { + buf_p = buf; + buf_len = len; + while (buf_len > 0 && (pkt = pkt_decode(&buf_p, &buf_len))) + cli->handle_packet(cli, pkt); + if (buf_len > 0) { + /* XXX store incomplete packet for next recv */ + error("incomplete packet, dropped %d bytes !", buf_len); + } + } + break; + } } -void cb_cli_timer(evutil_socket_t fd, short what, void *arg) +void +cb_cli_timer(evutil_socket_t fd, short what, void *arg) { - struct timeval tv; - struct gg_client *cli; - - cli = arg; - - switch (cli->status) { - case GG_CLIENT_STATUS_CONNECTING: - client_send(cli, "", 0); - break; - case GG_CLIENT_STATUS_CONNECTED: - // XXX send keepalive - break; - } - - bzero(&tv, sizeof(struct timeval)); - tv.tv_sec = 2; - event_add(cli->ev_timer, &tv); + struct timeval tv; + struct gg_client *cli; + + cli = arg; + + switch (cli->status) { + case GG_CLIENT_STATUS_CONNECTING: + client_send(cli, "", 0); + break; + case GG_CLIENT_STATUS_CONNECTED: + // XXX send keepalive + break; + } + + bzero(&tv, sizeof(struct timeval)); + tv.tv_sec = 2; + event_add(cli->ev_timer, &tv); } int cb_cli_send(void *data, int size, void *usrdata) { - struct gg_client *cli; + struct gg_client *cli; - cli = usrdata; - return client_send(cli, data, size); + cli = usrdata; + return client_send(cli, data, size); } /* @@ -509,100 +512,100 @@ cb_cli_send(void *data, int size, void *usrdata) struct gg_packet * pkt_decode(char **buf, int *buf_len) { - static struct gg_packet newpkt; - struct gg_packet *pkt; - int len; - int packet_len; + static struct gg_packet newpkt; + struct gg_packet *pkt; + int len; + int packet_len; - len = *buf_len; + len = *buf_len; #define invalid(msg) \ - { verbose(msg); goto invalid; } + { verbose(msg); goto invalid; } #define incomplete(msg) \ - { verbose(msg); goto incomplete; } - - if (len < PACKET_HEADER_SIZE) - invalid("len"); - if (len > PACKET_BUFFER_SIZE) - invalid("len"); - pkt = (struct gg_packet *)*buf; - if (pkt->ver != PACKET_VERSION) - invalid("ver"); - if (pkt->type < PACKET_TYPE_MIN || pkt->type > PACKET_TYPE_MAX) - invalid("type"); - packet_len = gg_packet_props[pkt->type].size; // XXX never overflow ? - debug("type %d: %d %d", pkt->type, len, packet_len); - if (len < packet_len) - invalid("type len"); - - newpkt.ver = pkt->ver; - newpkt.type = pkt->type; - switch(pkt->type) { - case PACKET_NEWCONN: - newpkt.newconn_id = ntohs(pkt->newconn_id); - newpkt.newconn_src = ntohl(pkt->newconn_src); - newpkt.newconn_dst = ntohl(pkt->newconn_dst); - newpkt.newconn_proto = pkt->newconn_proto; - newpkt.newconn_size = pkt->newconn_size; - break; - case PACKET_DELCONN: - newpkt.delconn_id = ntohs(pkt->delconn_id); - break; - case PACKET_DATA: - newpkt.data_connid = ntohs(pkt->data_connid); - newpkt.data_size = pkt->data_size; - break; - case PACKET_NAME: - newpkt.name_addr = ntohl(pkt->name_addr); - newpkt.name_len = pkt->name_len; - if (newpkt.name_len > GG_PKTARG_MAX) - invalid("type name name_len"); - if (len < packet_len + newpkt.name_len) - goto incomplete; - packet_len = packet_len + newpkt.name_len; - strncpy((char *)newpkt.name_fqdn, (char *)pkt->name_fqdn, - newpkt.name_len); - break; - case PACKET_FORK: - newpkt.fork_pid = ntohl(pkt->fork_pid); - newpkt.fork_ppid = ntohl(pkt->fork_ppid); - newpkt.fork_cpid = ntohl(pkt->fork_cpid); - newpkt.fork_tgid = ntohl(pkt->fork_tgid); - break; - case PACKET_EXEC: - newpkt.exec_pid = ntohl(pkt->exec_pid); - newpkt.exec_cmdlen = pkt->exec_cmdlen; - if (newpkt.exec_cmdlen > GG_PKTARG_MAX) - invalid("type exec cmdlen"); - if (len < packet_len + newpkt.exec_cmdlen) - goto incomplete; - packet_len = packet_len + newpkt.exec_cmdlen; - strncpy((char *)newpkt.exec_cmd, (char *)pkt->exec_cmd, - newpkt.exec_cmdlen); - break; - case PACKET_EXIT: - newpkt.exit_pid = ntohl(pkt->exit_pid); - newpkt.exit_tgid = ntohl(pkt->exit_tgid); - newpkt.exit_ecode = pkt->exit_ecode; - break; - default: - goto invalid; - invalid("type switch"); - } - - *buf = *buf + packet_len; - *buf_len = len - packet_len; - return &newpkt; + { verbose(msg); goto incomplete; } + + if (len < PACKET_HEADER_SIZE) + invalid("len"); + if (len > PACKET_BUFFER_SIZE) + invalid("len"); + pkt = (struct gg_packet *)*buf; + if (pkt->ver != PACKET_VERSION) + invalid("ver"); + if (pkt->type < PACKET_TYPE_MIN || pkt->type > PACKET_TYPE_MAX) + invalid("type"); + packet_len = gg_packet_props[pkt->type].size; // XXX never overflow ? + debug("type %d: %d %d", pkt->type, len, packet_len); + if (len < packet_len) + invalid("type len"); + + newpkt.ver = pkt->ver; + newpkt.type = pkt->type; + switch(pkt->type) { + case PACKET_NEWCONN: + newpkt.newconn_id = ntohs(pkt->newconn_id); + newpkt.newconn_src = ntohl(pkt->newconn_src); + newpkt.newconn_dst = ntohl(pkt->newconn_dst); + newpkt.newconn_proto = pkt->newconn_proto; + newpkt.newconn_size = pkt->newconn_size; + break; + case PACKET_DELCONN: + newpkt.delconn_id = ntohs(pkt->delconn_id); + break; + case PACKET_DATA: + newpkt.data_connid = ntohs(pkt->data_connid); + newpkt.data_size = pkt->data_size; + break; + case PACKET_NAME: + newpkt.name_addr = ntohl(pkt->name_addr); + newpkt.name_len = pkt->name_len; + if (newpkt.name_len > GG_PKTARG_MAX) + invalid("type name name_len"); + if (len < packet_len + newpkt.name_len) + goto incomplete; + packet_len = packet_len + newpkt.name_len; + strncpy((char *)newpkt.name_fqdn, (char *)pkt->name_fqdn, + newpkt.name_len); + break; + case PACKET_FORK: + newpkt.fork_pid = ntohl(pkt->fork_pid); + newpkt.fork_ppid = ntohl(pkt->fork_ppid); + newpkt.fork_cpid = ntohl(pkt->fork_cpid); + newpkt.fork_tgid = ntohl(pkt->fork_tgid); + break; + case PACKET_EXEC: + newpkt.exec_pid = ntohl(pkt->exec_pid); + newpkt.exec_cmdlen = pkt->exec_cmdlen; + if (newpkt.exec_cmdlen > GG_PKTARG_MAX) + invalid("type exec cmdlen"); + if (len < packet_len + newpkt.exec_cmdlen) + goto incomplete; + packet_len = packet_len + newpkt.exec_cmdlen; + strncpy((char *)newpkt.exec_cmd, (char *)pkt->exec_cmd, + newpkt.exec_cmdlen); + break; + case PACKET_EXIT: + newpkt.exit_pid = ntohl(pkt->exit_pid); + newpkt.exit_tgid = ntohl(pkt->exit_tgid); + newpkt.exit_ecode = pkt->exit_ecode; + break; + default: + goto invalid; + invalid("type switch"); + } + + *buf = *buf + packet_len; + *buf_len = len - packet_len; + return &newpkt; incomplete: - error("incomplete packet"); - *buf_len = len; - return NULL; + error("incomplete packet"); + *buf_len = len; + return NULL; invalid: - error("invalid packet"); - *buf_len = 0; - return NULL; + error("invalid packet"); + *buf_len = 0; + return NULL; } /* @@ -612,96 +615,96 @@ invalid: int pkt_encode(struct gg_packet *pkt, struct gg_packet *newpkt) { - if (pkt->type < PACKET_TYPE_MIN || pkt->type > PACKET_TYPE_MAX) - invalid("type"); - - newpkt->ver = pkt->ver; - newpkt->type = pkt->type; - switch(pkt->type) { - case PACKET_NEWCONN: - newpkt->newconn_id = htons(pkt->newconn_id); - newpkt->newconn_src = htonl(pkt->newconn_src); - newpkt->newconn_dst = htonl(pkt->newconn_dst); - newpkt->newconn_proto = pkt->newconn_proto; - newpkt->newconn_size = pkt->newconn_size; - break; - case PACKET_DELCONN: - newpkt->delconn_id = htons(pkt->delconn_id); - break; - case PACKET_DATA: - newpkt->data_connid = htons(pkt->data_connid); - newpkt->data_size = pkt->data_size; - break; - case PACKET_NAME: - if (pkt->name_len > GG_PKTARG_MAX) - goto invalid; - newpkt->name_addr = htonl(pkt->name_addr); - newpkt->name_len = pkt->name_len; - strncpy((char *)newpkt->name_fqdn, (char *)pkt->name_fqdn, - pkt->name_len); - break; - case PACKET_FORK: - newpkt->fork_pid = htonl(pkt->fork_pid); - newpkt->fork_ppid = htonl(pkt->fork_ppid); - newpkt->fork_cpid = htonl(pkt->fork_cpid); - newpkt->fork_tgid = htonl(pkt->fork_tgid); - break; - case PACKET_EXEC: - if (pkt->exec_cmdlen > GG_PKTARG_MAX) - goto invalid; - newpkt->exec_pid = htonl(pkt->exec_pid); - newpkt->exec_cmdlen = pkt->exec_cmdlen; - strncpy((char *)newpkt->exec_cmd, (char *)pkt->exec_cmd, - pkt->exec_cmdlen); - break; - case PACKET_EXIT: - newpkt->exit_pid = htonl(pkt->exit_pid); - newpkt->exit_tgid = htonl(pkt->exit_tgid); - newpkt->exit_ecode = pkt->exit_ecode; - break; - default: - error("Unsupported packet type"); - return -1; - } - return pkt_getsize(newpkt); + if (pkt->type < PACKET_TYPE_MIN || pkt->type > PACKET_TYPE_MAX) + invalid("type"); + + newpkt->ver = pkt->ver; + newpkt->type = pkt->type; + switch(pkt->type) { + case PACKET_NEWCONN: + newpkt->newconn_id = htons(pkt->newconn_id); + newpkt->newconn_src = htonl(pkt->newconn_src); + newpkt->newconn_dst = htonl(pkt->newconn_dst); + newpkt->newconn_proto = pkt->newconn_proto; + newpkt->newconn_size = pkt->newconn_size; + break; + case PACKET_DELCONN: + newpkt->delconn_id = htons(pkt->delconn_id); + break; + case PACKET_DATA: + newpkt->data_connid = htons(pkt->data_connid); + newpkt->data_size = pkt->data_size; + break; + case PACKET_NAME: + if (pkt->name_len > GG_PKTARG_MAX) + goto invalid; + newpkt->name_addr = htonl(pkt->name_addr); + newpkt->name_len = pkt->name_len; + strncpy((char *)newpkt->name_fqdn, (char *)pkt->name_fqdn, + pkt->name_len); + break; + case PACKET_FORK: + newpkt->fork_pid = htonl(pkt->fork_pid); + newpkt->fork_ppid = htonl(pkt->fork_ppid); + newpkt->fork_cpid = htonl(pkt->fork_cpid); + newpkt->fork_tgid = htonl(pkt->fork_tgid); + break; + case PACKET_EXEC: + if (pkt->exec_cmdlen > GG_PKTARG_MAX) + goto invalid; + newpkt->exec_pid = htonl(pkt->exec_pid); + newpkt->exec_cmdlen = pkt->exec_cmdlen; + strncpy((char *)newpkt->exec_cmd, (char *)pkt->exec_cmd, + pkt->exec_cmdlen); + break; + case PACKET_EXIT: + newpkt->exit_pid = htonl(pkt->exit_pid); + newpkt->exit_tgid = htonl(pkt->exit_tgid); + newpkt->exit_ecode = pkt->exit_ecode; + break; + default: + error("Unsupported packet type"); + return -1; + } + return pkt_getsize(newpkt); invalid: - error("invalid packet"); - return -1; + error("invalid packet"); + return -1; } int pkt_getsize(struct gg_packet *pkt) { - int packet_len; - - packet_len = gg_packet_props[pkt->type].size; // XXX never overflow ? - switch(pkt->type) { - case PACKET_NEWCONN: - case PACKET_DELCONN: - case PACKET_DATA: - case PACKET_FORK: - case PACKET_EXIT: - break; - case PACKET_NAME: - if (pkt->name_len > GG_PKTARG_MAX) - goto invalid; - packet_len = packet_len + pkt->name_len; - break; - case PACKET_EXEC: - if (pkt->exec_cmdlen > GG_PKTARG_MAX) - goto invalid; - packet_len = packet_len + pkt->exec_cmdlen; - break; - default: - error("Unsupported packet type"); - return -1; - } - return packet_len; + int packet_len; + + packet_len = gg_packet_props[pkt->type].size; // XXX never overflow ? + switch(pkt->type) { + case PACKET_NEWCONN: + case PACKET_DELCONN: + case PACKET_DATA: + case PACKET_FORK: + case PACKET_EXIT: + break; + case PACKET_NAME: + if (pkt->name_len > GG_PKTARG_MAX) + goto invalid; + packet_len = packet_len + pkt->name_len; + break; + case PACKET_EXEC: + if (pkt->exec_cmdlen > GG_PKTARG_MAX) + goto invalid; + packet_len = packet_len + pkt->exec_cmdlen; + break; + default: + error("Unsupported packet type"); + return -1; + } + return packet_len; invalid: - error("invalid packet"); - return -1; + error("invalid packet"); + return -1; } /* @@ -711,11 +714,11 @@ invalid: int gg_verbosity_get(void) { - return _verbosity; + return _verbosity; } void gg_verbosity_set(int verb) { - _verbosity = verb; + _verbosity = verb; } diff --git a/libglouglou/libglouglou.h b/libglouglou/libglouglou.h index b081685..cdb29e7 100644 --- a/libglouglou/libglouglou.h +++ b/libglouglou/libglouglou.h @@ -22,22 +22,22 @@ #define GG_PKTDATA_SIZE_FACTOR 20 #define GG_PKTDATA_SIZE_ENCODE(pktsize, size, response) \ - (pktsize = ((size / GG_PKTDATA_SIZE_FACTOR) & 0x7f) | (response << 7)) + (pktsize = ((size / GG_PKTDATA_SIZE_FACTOR) & 0x7f) | (response << 7)) #define GG_PKTDATA_SIZE_DECODE(pktsize, size, response) { \ - response = pktsize >> 7; size = (pktsize & 0x7f) * GG_PKTDATA_SIZE_FACTOR; \ + response = pktsize >> 7; size = (pktsize & 0x7f) * GG_PKTDATA_SIZE_FACTOR; \ } #define PACKET_HEADER_SIZE 2 #define PACKET_TYPE_MIN 0x00 #define PACKET_TYPE_MAX 0x12 enum gg_packet_type { /* u_int8_t */ - PACKET_NEWCONN = 0x00, - PACKET_DELCONN = 0x01, - PACKET_DATA = 0x02, - PACKET_NAME = 0x03, - PACKET_FORK = 0x10, - PACKET_EXEC = 0x11, - PACKET_EXIT = 0x12, + PACKET_NEWCONN = 0x00, + PACKET_DELCONN = 0x01, + PACKET_DATA = 0x02, + PACKET_NAME = 0x03, + PACKET_FORK = 0x10, + PACKET_EXEC = 0x11, + PACKET_EXIT = 0x12, }; /* XXX is packed needed everywhere ? */ @@ -64,22 +64,22 @@ struct __attribute__((packed)) gg_packet { u_int8_t len; u_char fqdn[GG_PKTARG_MAX]; } name; - struct __attribute__((packed)) fork { - u_int32_t pid; - u_int32_t ppid; - u_int32_t cpid; - u_int32_t tgid; - } fork; - struct __attribute__((packed)) exec { - u_int32_t pid; - u_int8_t cmdlen; - u_char cmd[GG_PKTARG_MAX]; - } exec; - struct __attribute__((packed)) exit { - u_int32_t pid; - u_int32_t tgid; - u_int8_t ecode; - } exit; + struct __attribute__((packed)) fork { + u_int32_t pid; + u_int32_t ppid; + u_int32_t cpid; + u_int32_t tgid; + } fork; + struct __attribute__((packed)) exec { + u_int32_t pid; + u_int8_t cmdlen; + u_char cmd[GG_PKTARG_MAX]; + } exec; + struct __attribute__((packed)) exit { + u_int32_t pid; + u_int32_t tgid; + u_int8_t ecode; + } exit; } pdat; #define newconn_id pdat.newconn.id #define newconn_src pdat.newconn.src @@ -105,72 +105,73 @@ struct __attribute__((packed)) gg_packet { }; typedef struct gg_packet_props_t { - int size; + int size; } gg_packet_props_t; extern gg_packet_props_t gg_packet_props[]; struct gg_user { LIST_ENTRY(gg_user) entry; - int id; - int sock; - struct sockaddr_in addr; - struct sendbuf *sbuf; + int id; + int sock; + struct sockaddr_in addr; + struct sendbuf *sbuf; }; struct gg_server { - struct event_base *ev_base; - const char *ip; - int port; - struct sockaddr_in addr; - struct event *ev; - int sock; - int (*handle_conn)(struct gg_server *, struct gg_user *); - int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *); - void *usrdata; - LIST_HEAD(, gg_user) user_list; - int user_count; - int user_id_count; + struct event_base *ev_base; + const char *ip; + int port; + struct sockaddr_in addr; + struct event *ev; + int sock; + int (*handle_conn)(struct gg_server *, struct gg_user *); + int (*handle_packet)(struct gg_server *, + struct gg_user *, struct gg_packet *); + void *usrdata; + LIST_HEAD(, gg_user) user_list; + int user_count; + int user_id_count; }; enum client_status { - GG_CLIENT_STATUS_CONNECTING = 0, - GG_CLIENT_STATUS_CONNECTED = 1 + GG_CLIENT_STATUS_CONNECTING = 0, + GG_CLIENT_STATUS_CONNECTED = 1 }; struct gg_client { - struct event_base *ev_base; - const char *ip; - int port; - struct sockaddr_in addr; - struct event *ev; - struct event *ev_timer; - int sock; - enum client_status status; - int (*handle_conn)(struct gg_client *); - int (*handle_packet)(struct gg_client *, struct gg_packet *); - void *usrdata; - struct sendbuf *sbuf; + struct event_base *ev_base; + const char *ip; + int port; + struct sockaddr_in addr; + struct event *ev; + struct event *ev_timer; + int sock; + enum client_status status; + int (*handle_conn)(struct gg_client *); + int (*handle_packet)(struct gg_client *, struct gg_packet *); + void *usrdata; + struct sendbuf *sbuf; }; -struct gg_server *gg_server_start(struct event_base *, char *, int, - int (*handle_conn)(struct gg_server *, struct gg_user *), - int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *), - void *); -void gg_server_stop(struct gg_server *); -int gg_server_send(struct gg_server *, struct gg_packet *, struct gg_user *); -void gg_server_send_flush(struct gg_server *, struct gg_user *); - -struct gg_client *gg_client_connect(struct event_base *, char *, int, - int (*handle_conn)(struct gg_client *), - int (*handle_packet)(struct gg_client *, struct gg_packet *), - void *); -void gg_client_disconnect(struct gg_client *); -int gg_client_send(struct gg_client *, struct gg_packet *); -void gg_client_send_flush(struct gg_client *); - -int gg_verbosity_get(void); -void gg_verbosity_set(int); +struct gg_server *gg_server_start(struct event_base *, char *, int, + int (*handle_conn)(struct gg_server *, struct gg_user *), + int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *), + void *); +void gg_server_stop(struct gg_server *); +int gg_server_send(struct gg_server *, struct gg_packet *, struct gg_user *); +void gg_server_send_flush(struct gg_server *, struct gg_user *); + +struct gg_client *gg_client_connect(struct event_base *, char *, int, + int (*handle_conn)(struct gg_client *), + int (*handle_packet)(struct gg_client *, struct gg_packet *), + void *); +void gg_client_disconnect(struct gg_client *); +int gg_client_send(struct gg_client *, struct gg_packet *); +void gg_client_send_flush(struct gg_client *); + +int gg_verbosity_get(void); +void gg_verbosity_set(int); /* utils.c */ @@ -180,23 +181,23 @@ void gg_verbosity_set(int); #define GGLOG_INFO 1 #define GGLOG_DEBUG 2 -int gg_log_init(char *, int); -void gg_log_shutdown(void); -void gg_log_tmp(const char *, ...); -void gg_log_debug(const char *, ...); -void gg_log_info(const char *, ...); -void gg_log_warn(const char *, ...); +int gg_log_init(char *, int); +void gg_log_shutdown(void); +void gg_log_tmp(const char *, ...); +void gg_log_debug(const char *, ...); +void gg_log_info(const char *, ...); +void gg_log_warn(const char *, ...); #if defined(__OpenBSD__) void __dead gg_log_fatal(const char *, ...); #else -void gg_log_fatal(const char *, ...); +void gg_log_fatal(const char *, ...); #endif -void *xmalloc(size_t); -void *xcalloc(size_t, size_t); -void fd_nonblock(int); -void addrcpy(struct sockaddr_in *, struct sockaddr_in *); -int addrcmp(struct sockaddr_in *, struct sockaddr_in *); -void droppriv(char *, int, char *); +void *xmalloc(size_t); +void *xcalloc(size_t, size_t); +void fd_nonblock(int); +void addrcpy(struct sockaddr_in *, struct sockaddr_in *); +int addrcmp(struct sockaddr_in *, struct sockaddr_in *); +void droppriv(char *, int, char *); #endif /* _LIBGLOUGLOU_H_ */ diff --git a/libglouglou/sendbuf.c b/libglouglou/sendbuf.c index 4e83294..47c4fd1 100644 --- a/libglouglou/sendbuf.c +++ b/libglouglou/sendbuf.c @@ -14,45 +14,45 @@ static void cb_timer(evutil_socket_t, short, void *); */ struct sendbuf * sendbuf_new(struct event_base *ev_base, int buffer_size, int msec_max, - int (*send_func)(void *, int, void *), void *usrdata) + int (*send_func)(void *, int, void *), void *usrdata) { - struct sendbuf *sbuf = NULL; - struct event *ev_timer; - - sbuf = calloc(1, sizeof(struct sendbuf)); - if (!sbuf) - return NULL; - sbuf->ev_base = ev_base; - sbuf->msec_max = msec_max; - sbuf->buffer_size = buffer_size; - sbuf->send_func = send_func; - sbuf->usrdata = usrdata; - sbuf->buffer = malloc(sbuf->buffer_size); - if (!sbuf->buffer) - goto err; - - ev_timer = evtimer_new(ev_base, cb_timer, sbuf); - sbuf->ev_timer = ev_timer; - sbuf->ev_timer_tv.tv_usec = msec_max * 1000; - evtimer_add(ev_timer, &sbuf->ev_timer_tv); - - return sbuf; + struct sendbuf *sbuf = NULL; + struct event *ev_timer; + + sbuf = calloc(1, sizeof(struct sendbuf)); + if (!sbuf) + return NULL; + sbuf->ev_base = ev_base; + sbuf->msec_max = msec_max; + sbuf->buffer_size = buffer_size; + sbuf->send_func = send_func; + sbuf->usrdata = usrdata; + sbuf->buffer = malloc(sbuf->buffer_size); + if (!sbuf->buffer) + goto err; + + ev_timer = evtimer_new(ev_base, cb_timer, sbuf); + sbuf->ev_timer = ev_timer; + sbuf->ev_timer_tv.tv_usec = msec_max * 1000; + evtimer_add(ev_timer, &sbuf->ev_timer_tv); + + return sbuf; err: - sendbuf_free(sbuf); - return NULL; + sendbuf_free(sbuf); + return NULL; } void sendbuf_free(struct sendbuf *sbuf) { - if (sbuf->ev_timer) - event_del(sbuf->ev_timer); - if (sbuf->buffer && sbuf->send_func) - sendbuf_flush(sbuf); - if (sbuf->buffer) - free(sbuf->buffer); - free(sbuf); + if (sbuf->ev_timer) + event_del(sbuf->ev_timer); + if (sbuf->buffer && sbuf->send_func) + sendbuf_flush(sbuf); + if (sbuf->buffer) + free(sbuf->buffer); + free(sbuf); } /* @@ -63,14 +63,14 @@ sendbuf_free(struct sendbuf *sbuf) int sendbuf_append(struct sendbuf *sbuf, void *token, int size) { - if (sbuf->buffer_pos + size >= sbuf->buffer_size) - if (sendbuf_flush(sbuf) == -1) - return -1; + if (sbuf->buffer_pos + size >= sbuf->buffer_size) + if (sendbuf_flush(sbuf) == -1) + return -1; - memcpy(sbuf->buffer + sbuf->buffer_pos, token, size); - sbuf->buffer_pos = sbuf->buffer_pos + size; + memcpy(sbuf->buffer + sbuf->buffer_pos, token, size); + sbuf->buffer_pos = sbuf->buffer_pos + size; - return size; + return size; } /* @@ -81,16 +81,16 @@ sendbuf_append(struct sendbuf *sbuf, void *token, int size) void * sendbuf_gettoken(struct sendbuf *sbuf, int size) { - void *token; + void *token; - if (sbuf->buffer_pos + size >= sbuf->buffer_size) - if (sendbuf_flush(sbuf) == -1) - return NULL; + if (sbuf->buffer_pos + size >= sbuf->buffer_size) + if (sendbuf_flush(sbuf) == -1) + return NULL; - token = sbuf->buffer + sbuf->buffer_pos; - sbuf->buffer_pos = sbuf->buffer_pos + size; + token = sbuf->buffer + sbuf->buffer_pos; + sbuf->buffer_pos = sbuf->buffer_pos + size; - return token; + return token; } /* @@ -102,28 +102,28 @@ sendbuf_gettoken(struct sendbuf *sbuf, int size) int sendbuf_flush(struct sendbuf *sbuf) { - int tosend, sent; - - if (sbuf->buffer_pos == 0) - return 0; - - sbuf->flushing = 1; - - tosend = sbuf->buffer_pos - sbuf->flushing_pos; - sent = sbuf->send_func(sbuf->buffer + sbuf->flushing_pos, - tosend, sbuf->usrdata); - if (sent == -1) { - // XXX handle error - return -1; - } else if (sent < tosend) { - sbuf->flushing_pos = sbuf->flushing_pos + sent; - return -1; - } - sbuf->buffer_pos = 0; - - sbuf->flushing = 0; - sbuf->flushing_pos = 0; - return 0; + int tosend, sent; + + if (sbuf->buffer_pos == 0) + return 0; + + sbuf->flushing = 1; + + tosend = sbuf->buffer_pos - sbuf->flushing_pos; + sent = sbuf->send_func(sbuf->buffer + sbuf->flushing_pos, + tosend, sbuf->usrdata); + if (sent == -1) { + // XXX handle error + return -1; + } else if (sent < tosend) { + sbuf->flushing_pos = sbuf->flushing_pos + sent; + return -1; + } + sbuf->buffer_pos = 0; + + sbuf->flushing = 0; + sbuf->flushing_pos = 0; + return 0; } @@ -134,9 +134,9 @@ sendbuf_flush(struct sendbuf *sbuf) static void cb_timer(evutil_socket_t fd, short what, void *arg) { - struct sendbuf *sbuf; + struct sendbuf *sbuf; - sbuf = arg; - sendbuf_flush(sbuf); - evtimer_add(sbuf->ev_timer, &sbuf->ev_timer_tv); + sbuf = arg; + sendbuf_flush(sbuf); + evtimer_add(sbuf->ev_timer, &sbuf->ev_timer_tv); } diff --git a/libglouglou/sendbuf.h b/libglouglou/sendbuf.h index 6d163d4..7a7bd05 100644 --- a/libglouglou/sendbuf.h +++ b/libglouglou/sendbuf.h @@ -2,23 +2,23 @@ #include struct sendbuf { - struct event_base *ev_base; - struct event *ev_timer; - struct timeval ev_timer_tv; - int msec_max; - int buffer_size; - void *buffer; - int buffer_pos; /* next to use in buffer */ - int flushing; - int flushing_pos; /* next to send in buffer */ - int (*send_func)(void *, int, void *); - void *usrdata; + struct event_base *ev_base; + struct event *ev_timer; + struct timeval ev_timer_tv; + int msec_max; + int buffer_size; + void *buffer; + int buffer_pos; /* next to use in buffer */ + int flushing; + int flushing_pos; /* next to send in buffer */ + int (*send_func)(void *, int, void *); + void *usrdata; }; -struct sendbuf *sendbuf_new(struct event_base *, int, int, - int (*send_func)(void *, int, void *), - void *); -void sendbuf_free(struct sendbuf *); -int sendbuf_append(struct sendbuf *, void *, int); -void *sendbuf_gettoken(struct sendbuf *, int); -int sendbuf_flush(struct sendbuf *); +struct sendbuf *sendbuf_new(struct event_base *, int, int, + int (*send_func)(void *, int, void *), + void *); +void sendbuf_free(struct sendbuf *); +int sendbuf_append(struct sendbuf *, void *, int); +void *sendbuf_gettoken(struct sendbuf *, int); +int sendbuf_flush(struct sendbuf *); diff --git a/libglouglou/tests/connect.c b/libglouglou/tests/connect.c index 3e46667..2a203c8 100644 --- a/libglouglou/tests/connect.c +++ b/libglouglou/tests/connect.c @@ -8,39 +8,41 @@ int cli_connect_ok = 0; int srv_handle_conn(struct gg_server *srv, struct gg_user *usr) { - if (usr->id == 0) - srv_connect_ok = 1; - return 0; + if (usr->id == 0) + srv_connect_ok = 1; + return 0; } int cli_handle_conn(struct gg_client *cli) { - cli_connect_ok = 1; - return 0; + cli_connect_ok = 1; + return 0; } int main(void) { - struct event_base *ev_base; - - ev_base = event_base_new(); - if (!gg_server_start(ev_base, "127.0.0.1", 12345, srv_handle_conn, NULL, NULL)) { - printf("error: gg_server_start returned NULL\n"); - return 1; - } - if (!gg_client_connect(ev_base, "127.0.0.1", 12345, cli_handle_conn, NULL, NULL)) { - printf("error: gg_client_connect returned NULL\n"); - return 1; - } - event_base_loop(ev_base, EVLOOP_NONBLOCK); - event_base_loop(ev_base, EVLOOP_NONBLOCK); - - if (srv_connect_ok == 0) - printf("error: srv_connect_ok == 0\n"); - if (cli_connect_ok == 0) - printf("error: cli_connect_ok == 0\n"); - - return (!srv_connect_ok || !cli_connect_ok); + struct event_base *ev_base; + + ev_base = event_base_new(); + if (!gg_server_start(ev_base, "127.0.0.1", 12345, + srv_handle_conn,NULL, NULL)) { + printf("error: gg_server_start returned NULL\n"); + return 1; + } + if (!gg_client_connect(ev_base, "127.0.0.1", 12345, + cli_handle_conn, NULL, NULL)) { + printf("error: gg_client_connect returned NULL\n"); + return 1; + } + event_base_loop(ev_base, EVLOOP_NONBLOCK); + event_base_loop(ev_base, EVLOOP_NONBLOCK); + + if (srv_connect_ok == 0) + printf("error: srv_connect_ok == 0\n"); + if (cli_connect_ok == 0) + printf("error: cli_connect_ok == 0\n"); + + return (!srv_connect_ok || !cli_connect_ok); } diff --git a/libglouglou/tests/sendrecv.c b/libglouglou/tests/sendrecv.c index cbfb402..2df9e01 100644 --- a/libglouglou/tests/sendrecv.c +++ b/libglouglou/tests/sendrecv.c @@ -6,62 +6,63 @@ int srv_recv_ok = 0; int cli_recv_ok = 0; int -srv_handle_packet(struct gg_server *srv, struct gg_user *usr, struct gg_packet *pkt) +srv_handle_packet(struct gg_server *srv, + struct gg_user *usr, struct gg_packet *pkt) { - if (pkt->type == PACKET_NEWCONN) - srv_recv_ok = 1; - return 0; + if (pkt->type == PACKET_NEWCONN) + srv_recv_ok = 1; + return 0; } int cli_handle_packet(struct gg_client *cli, struct gg_packet *pkt) { - if (pkt->type == PACKET_NEWCONN) - cli_recv_ok = 1; - return 0; + if (pkt->type == PACKET_NEWCONN) + cli_recv_ok = 1; + return 0; } int main(void) { - struct event_base *ev_base; - struct gg_server *srv; - struct gg_client *cli; - struct gg_packet pkt; + struct event_base *ev_base; + struct gg_server *srv; + struct gg_client *cli; + struct gg_packet pkt; - ev_base = event_base_new(); - srv = gg_server_start(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT, - NULL, srv_handle_packet, NULL); - if (!srv) { - printf("error: gg_server_start returned NULL\n"); - return 1; - } - cli = gg_client_connect(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT, - NULL, cli_handle_packet, NULL); - if (!cli) { - printf("error: gg_client_connect returned NULL\n"); - return 1; - } - event_base_loop(ev_base, EVLOOP_NONBLOCK); - event_base_loop(ev_base, EVLOOP_NONBLOCK); + ev_base = event_base_new(); + srv = gg_server_start(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT, + NULL, srv_handle_packet, NULL); + if (!srv) { + printf("error: gg_server_start returned NULL\n"); + return 1; + } + cli = gg_client_connect(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT, + NULL, cli_handle_packet, NULL); + if (!cli) { + printf("error: gg_client_connect returned NULL\n"); + return 1; + } + event_base_loop(ev_base, EVLOOP_NONBLOCK); + event_base_loop(ev_base, EVLOOP_NONBLOCK); - pkt.ver = PACKET_VERSION; - pkt.type = PACKET_NEWCONN; + pkt.ver = PACKET_VERSION; + pkt.type = PACKET_NEWCONN; - gg_server_send(srv, &pkt, NULL); - gg_server_send_flush(srv, NULL); - gg_client_send(cli, &pkt); - gg_client_send_flush(cli); + gg_server_send(srv, &pkt, NULL); + gg_server_send_flush(srv, NULL); + gg_client_send(cli, &pkt); + gg_client_send_flush(cli); - event_base_loop(ev_base, EVLOOP_NONBLOCK); + event_base_loop(ev_base, EVLOOP_NONBLOCK); - gg_client_disconnect(cli); - gg_server_stop(srv); + gg_client_disconnect(cli); + gg_server_stop(srv); - if (srv_recv_ok == 0) - printf("error: srv_recv_ok == 0\n"); - if (cli_recv_ok == 0) - printf("error: cli_recv_ok == 0\n"); + if (srv_recv_ok == 0) + printf("error: srv_recv_ok == 0\n"); + if (cli_recv_ok == 0) + printf("error: cli_recv_ok == 0\n"); - return (!srv_recv_ok || !cli_recv_ok); + return (!srv_recv_ok || !cli_recv_ok); } diff --git a/libglouglou/utils.c b/libglouglou/utils.c index 8ca3660..2c17eb0 100644 --- a/libglouglou/utils.c +++ b/libglouglou/utils.c @@ -43,47 +43,47 @@ gg_log_init(char *filename, int level) void gg_log_shutdown(void) { - fclose(_logfile); + fclose(_logfile); } void gg_log_tmp(const char *msg, ...) { - va_list ap; + va_list ap; - va_start(ap, msg); - logit(GGLOG_FORCED, "XXX ", msg, ap); - va_end(ap); + va_start(ap, msg); + logit(GGLOG_FORCED, "XXX ", msg, ap); + va_end(ap); } void gg_log_debug(const char *msg, ...) { - va_list ap; + va_list ap; - va_start(ap, msg); - logit(GGLOG_DEBUG, "", msg, ap); - va_end(ap); + va_start(ap, msg); + logit(GGLOG_DEBUG, "", msg, ap); + va_end(ap); } void gg_log_info(const char *msg, ...) { - va_list ap; + va_list ap; - va_start(ap, msg); - logit(GGLOG_INFO, "", msg, ap); - va_end(ap); + va_start(ap, msg); + logit(GGLOG_INFO, "", msg, ap); + va_end(ap); } void gg_log_warn(const char *msg, ...) { - va_list ap; + va_list ap; - va_start(ap, msg); - logit(GGLOG_WARN, "", msg, ap); - va_end(ap); + va_start(ap, msg); + logit(GGLOG_WARN, "", msg, ap); + va_end(ap); } #if defined(__OpenBSD__) @@ -93,29 +93,29 @@ void #endif gg_log_fatal(const char *msg, ...) { - va_list ap; + va_list ap; - va_start(ap, msg); - logit(GGLOG_FATAL, "fatal: ", msg, ap); - va_end(ap); + va_start(ap, msg); + logit(GGLOG_FATAL, "fatal: ", msg, ap); + va_end(ap); - exit(1); + exit(1); } /* XXX mpsafe */ static void logit(int level, const char *prefix, const char *msg, va_list ap) { - time_t clock; - - if (level <= _loglevel) { - time(&clock); - fprintf(_logfile, "%d ", (int)clock); - vfprintf(_logfile, prefix, ap); - vfprintf(_logfile, msg, ap); - fprintf(_logfile, "\n"); - fflush(_logfile); - } + time_t clock; + + if (level <= _loglevel) { + time(&clock); + fprintf(_logfile, "%d ", (int)clock); + vfprintf(_logfile, prefix, ap); + vfprintf(_logfile, msg, ap); + fprintf(_logfile, "\n"); + fflush(_logfile); + } } /* @@ -125,23 +125,23 @@ logit(int level, const char *prefix, const char *msg, va_list ap) void * xmalloc(size_t size) { - void *data; + void *data; - data = malloc(size); - if (!data) - err(1, "could not malloc %d", (int)size); - return data; + data = malloc(size); + if (!data) + err(1, "could not malloc %d", (int)size); + return data; } void * xcalloc(size_t nmemb, size_t size) { - void *data; + void *data; - data = calloc(nmemb, size); - if (!data) - err(1, "could not calloc %d", (int)size); - return data; + data = calloc(nmemb, size); + if (!data) + err(1, "could not calloc %d", (int)size); + return data; } void @@ -156,21 +156,21 @@ fd_nonblock(int fd) void addrcpy(struct sockaddr_in *dst, struct sockaddr_in *src) { - dst->sin_addr.s_addr = src->sin_addr.s_addr; - dst->sin_port = src->sin_port; - dst->sin_family = src->sin_family; + dst->sin_addr.s_addr = src->sin_addr.s_addr; + dst->sin_port = src->sin_port; + dst->sin_family = src->sin_family; } int addrcmp(struct sockaddr_in *a, struct sockaddr_in *b) { - if (a->sin_addr.s_addr != b->sin_addr.s_addr) - return -1; - if (a->sin_port != b->sin_port) - return -2; - if (a->sin_family != b->sin_family) - return -3; - return 0; + if (a->sin_addr.s_addr != b->sin_addr.s_addr) + return -1; + if (a->sin_port != b->sin_port) + return -2; + if (a->sin_family != b->sin_family) + return -3; + return 0; } void @@ -182,11 +182,11 @@ droppriv(char *user, int do_chroot, char *chroot_path) if (!pw) err(1, "unknown user %s", user); if (do_chroot) { - if (!chroot_path) - chroot_path = pw->pw_dir; - if (chroot(chroot_path) != 0) - err(1, "unable to chroot"); - } + if (!chroot_path) + chroot_path = pw->pw_dir; + if (chroot(chroot_path) != 0) + err(1, "unable to chroot"); + } if (chdir("/") != 0) err(1, "unable to chdir"); if (setgroups(1, &pw->pw_gid) == -1) -- cgit v1.2.3-59-g8ed1b