static void handle_class(struct nl_object *obj, void *arg) { struct rtnl_tc *tc = (struct rtnl_tc *) obj; struct element *e; struct rdata *rdata = arg; struct rdata ndata = { .level = rdata->level + 1, }; if (!(e = handle_tc_obj(tc, "class", rdata))) return; ndata.parent = e; if (!strcmp(rtnl_tc_get_kind(tc), "htb")) element_set_txmax(e, rtnl_htb_get_rate((struct rtnl_class *) tc)); find_classes(rtnl_tc_get_handle(tc), &ndata); find_qdiscs(rtnl_tc_get_handle(tc), &ndata); } static void find_qdiscs(uint32_t parent, struct rdata *rdata) { struct rtnl_qdisc *filter; if (!(filter = rtnl_qdisc_alloc())) return; rtnl_tc_set_parent((struct rtnl_tc *) filter, parent); nl_cache_foreach_filter(qdisc_cache, OBJ_CAST(filter), handle_qdisc, rdata); rtnl_qdisc_put(filter); } static void find_cls(int ifindex, uint32_t parent, struct rdata *rdata) { struct nl_cache *cls_cache; if (rtnl_cls_alloc_cache(sock, ifindex, parent, &cls_cache) < 0) return; nl_cache_foreach(cls_cache, handle_cls, rdata); nl_cache_free(cls_cache); } static void find_classes(uint32_t parent, struct rdata *rdata) { struct rtnl_class *filter; if (!(filter = rtnl_class_alloc())) return; rtnl_tc_set_parent((struct rtnl_tc *) filter, parent); nl_cache_foreach_filter(class_cache, OBJ_CAST(filter), handle_class, rdata); rtnl_class_put(filter); } static void handle_qdisc(struct nl_object *obj, void *arg) { struct rtnl_tc *tc = (struct rtnl_tc *) obj; struct element *e; struct rdata *rdata = arg; struct rdata ndata = { .level = rdata->level + 1, }; if (!(e = handle_tc_obj(tc, "qdisc", rdata))) return; ndata.parent = e; find_cls(rtnl_tc_get_ifindex(tc), rtnl_tc_get_handle(tc), &ndata); if (rtnl_tc_get_parent(tc) == TC_H_ROOT) { find_cls(rtnl_tc_get_ifindex(tc), TC_H_ROOT, &ndata); find_classes(TC_H_ROOT, &ndata); } find_classes(rtnl_tc_get_handle(tc), &ndata); } static void handle_tc(struct element *e, struct rtnl_link *link) { struct rtnl_qdisc *qdisc; int ifindex = rtnl_link_get_ifindex(link); struct rdata rdata = { .level = 1, .parent = e, }; if (rtnl_class_alloc_cache(sock, ifindex, &class_cache) < 0) return; qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_ROOT); if (qdisc) { handle_qdisc(OBJ_CAST(qdisc), &rdata); rtnl_qdisc_put(qdisc); } qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, 0); if (qdisc) { handle_qdisc(OBJ_CAST(qdisc), &rdata); rtnl_qdisc_put(qdisc); } qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_INGRESS); if (qdisc) { handle_qdisc(OBJ_CAST(qdisc), &rdata); rtnl_qdisc_put(qdisc); } nl_cache_free(class_cache); } static void update_link_infos(struct element *e, struct rtnl_link *link) { char buf[64]; snprintf(buf, sizeof(buf), "%u", rtnl_link_get_mtu(link)); element_update_info(e, "MTU", buf); rtnl_link_flags2str(rtnl_link_get_flags(link), buf, sizeof(buf)); element_update_info(e, "Flags", buf); rtnl_link_operstate2str(rtnl_link_get_operstate(link), buf, sizeof(buf)); element_update_info(e, "Operstate", buf); snprintf(buf, sizeof(buf), "%u", rtnl_link_get_ifindex(link)); element_update_info(e, "IfIndex", buf); nl_addr2str(rtnl_link_get_addr(link), buf, sizeof(buf)); element_update_info(e, "Address", buf); nl_addr2str(rtnl_link_get_broadcast(link), buf, sizeof(buf)); element_update_info(e, "Broadcast", buf); rtnl_link_mode2str(rtnl_link_get_linkmode(link), buf, sizeof(buf)); element_update_info(e, "Mode", buf); snprintf(buf, sizeof(buf), "%u", rtnl_link_get_txqlen(link)); element_update_info(e, "TXQlen", buf); nl_af2str(rtnl_link_get_family(link), buf, sizeof(buf)); element_update_info(e, "Family", buf); element_update_info(e, "Alias", rtnl_link_get_ifalias(link) ? : ""); element_update_info(e, "Qdisc", rtnl_link_get_qdisc(link) ? : ""); if (rtnl_link_get_link(link)) { snprintf(buf, sizeof(buf), "%u", rtnl_link_get_link(link)); element_update_info(e, "SlaveOfIndex", buf); } } static void do_link(struct nl_object *obj, void *arg) { struct rtnl_link *link = (struct rtnl_link *) obj; struct element *e, *e_parent = NULL; int i, master_ifindex; if (!cfg_show_all && !(rtnl_link_get_flags(link) & IFF_UP)) { /* FIXME: delete element */ return; } /* Check if the interface is a slave of another interface */ if ((master_ifindex = rtnl_link_get_link(link))) { char parent[IFNAMSIZ+1]; rtnl_link_i2name(link_cache, master_ifindex, parent, sizeof(parent)); e_parent = element_lookup(grp, parent, master_ifindex, NULL, 0); } if (!(e = element_lookup(grp, rtnl_link_get_name(link), rtnl_link_get_ifindex(link), e_parent, ELEMENT_CREAT))) return; if (e->e_flags & ELEMENT_FLAG_CREATED) { if (e->e_parent) e->e_level = e->e_parent->e_level + 1; if (element_set_key_attr(e, "bytes", "packets") || element_set_usage_attr(e, "bytes")) BUG(); /* FIXME: Update link infos every 1s or so */ update_link_infos(e, link); e->e_flags &= ~ELEMENT_FLAG_CREATED; } for (i = 0; i < ARRAY_SIZE(link_attrs); i++) { struct attr_map *m = &link_attrs[i]; uint64_t c_rx = 0, c_tx = 0; int flags = 0; if (m->rxid >= 0) { c_rx = rtnl_link_get_stat(link, m->rxid); flags |= UPDATE_FLAG_RX; } if (m->txid >= 0) { c_tx = rtnl_link_get_stat(link, m->txid); flags |= UPDATE_FLAG_TX; } attr_update(e, m->attrid, c_rx, c_tx, flags); } if (!c_notc) handle_tc(e, link); element_notify_update(e, NULL); element_lifesign(e, 1); } static void netlink_read(void) { int err; if ((err = nl_cache_resync(sock, link_cache, NULL, NULL)) < 0) { fprintf(stderr, "Unable to resync link cache: %s\n", nl_geterror(err)); goto disable; } if ((err = nl_cache_resync(sock, qdisc_cache, NULL, NULL)) < 0) { fprintf(stderr, "Unable to resync qdisc cache: %s\n", nl_geterror(err)); goto disable; } nl_cache_foreach(link_cache, do_link, NULL); return; disable: netlink_ops.m_flags &= ~BMON_MODULE_ENABLED; } static void netlink_shutdown(void) { nl_cache_free(link_cache); nl_cache_free(qdisc_cache); nl_socket_free(sock); }
void generate_code_python(batch *b) { umlclasslist tmplist, parents; umlattrlist umla, tmpa; umloplist umlo; char *tmpname; char outfilename[90]; FILE * outfile, *dummyfile, *licensefile = NULL; namelist used_classes, tmpnamelist; int tmpdirlgth, tmpfilelgth; int interface, abstract; if (b->outdir == NULL) { b->outdir = "."; } tmpdirlgth = strlen(b->outdir); tmplist = b->classlist; /* open license file */ if ( b->license != NULL ) { licensefile = fopen(b->license, "r"); if(!licensefile) { fprintf(stderr, "Can't open the license file.\n"); exit(2); } } while ( tmplist != NULL ) { if ( ! ( is_present(b->classes, tmplist->key->name) ^ b->mask ) ) { tmpname = tmplist->key->name; /* This prevents buffer overflows */ tmpfilelgth = strlen(tmpname); if (tmpfilelgth + tmpdirlgth > sizeof(*outfilename) - 2) { fprintf(stderr, "Sorry, name of file too long ...\nTry a smaller dir name\n"); exit(4); } sprintf(outfilename, "%s/%s.py", b->outdir, tmplist->key->name); dummyfile = fopen(outfilename, "r"); if ( b->clobber || ! dummyfile ) { outfile = fopen(outfilename, "w"); if ( outfile == NULL ) { fprintf(stderr, "Can't open file %s for writing\n", outfilename); exit(3); } /* add license to the header */ if(b->license != NULL){ char lc; rewind(licensefile); while((lc = fgetc(licensefile)) != EOF){ fprintf(outfile,"%c",lc); } } used_classes = find_classes(tmplist, b); tmpnamelist = used_classes; while (tmpnamelist != NULL) { fprintf(outfile, "from %s import %s\n", tmpnamelist->name, tmpnamelist->name); tmpnamelist = tmpnamelist->next; } fprintf(outfile, "\n"); tmpname = strtolower(tmplist->key->stereotype); interface = !strcmp("interface", tmpname); abstract = tmplist->key->isabstract; free(tmpname); fprintf(outfile, "class %s", tmplist->key->name); parents = tmplist->parents; if (parents != NULL) { fprintf(outfile, "("); while ( parents != NULL ) { fprintf(outfile, "%s", parents->key->name); parents = parents->next; if (parents != NULL) fprintf(outfile, ", "); } fprintf(outfile, ")"); } fprintf(outfile, ":\n"); if (abstract) { fprintf(outfile, " \"\"\"Abstract class %s\n \"\"\"\n", tmplist->key->name); } else if (interface) { fprintf(outfile, " \"\"\"Interface %s\n \"\"\"\n", tmplist->key->name); } else { fprintf(outfile, " \"\"\"Class %s\n \"\"\"\n", tmplist->key->name); } fprintf(outfile, " # Attributes:\n"); umla = tmplist->key->attributes; while ( umla != NULL) { switch (umla->key.visibility) { case '0': fprintf (outfile, " "); break; case '1': fprintf (outfile, " __"); break; case '2': fprintf (outfile, " _"); break; } fprintf(outfile, "%s", umla->key.name); if ( umla->key.value[0] != 0 ) { fprintf(outfile, " = %s", umla->key.value); } else { fprintf(outfile, " = None"); } fprintf(outfile, " # (%s) \n", umla->key.type); umla = umla->next; } fprintf(outfile, " \n"); umlo = tmplist->key->operations; fprintf(outfile, " # Operations\n"); while ( umlo != NULL) { switch (umlo->key.attr.visibility) { case '0': fprintf (outfile, " def %s(self", umlo->key.attr.name); break; case '1': fprintf (outfile, " def __%s(self", umlo->key.attr.name); break; case '2': fprintf (outfile, " def _%s(self", umlo->key.attr.name); break; } tmpa = umlo->key.parameters; while (tmpa != NULL) { fprintf(outfile, ", %s", tmpa->key.name); if ( tmpa->key.value[0] != 0 ) { fprintf(outfile, " = %s", tmpa->key.value); } tmpa = tmpa->next; } fprintf(outfile, "):\n"); fprintf(outfile, " \"\"\"function %s\n", umlo->key.attr.name); tmpa = umlo->key.parameters; if (tmpa) fprintf(outfile, " \n"); while (tmpa != NULL) { fprintf(outfile, " %s: %s\n", tmpa->key.name, tmpa->key.type); tmpa = tmpa->next; } fprintf(outfile, " \n"); fprintf(outfile, " returns %s\n", umlo->key.attr.type); fprintf(outfile, " \"\"\"\n"); if (abstract || interface) { fprintf(outfile, " raise NotImplementedError()\n \n"); } else { fprintf(outfile, " return None # should raise NotImplementedError()\n \n"); } umlo = umlo->next; } fprintf(outfile, "\n"); fclose(outfile); } } tmplist = tmplist->next; } }
void generate_code_c(batch *b) { umlclasslist tmplist, parents; umlassoclist associations; int tmpv; umlattrlist umla, tmpa; umloplist umlo; char *tmpname; char outfilename[BIG_BUFFER]; FILE * outfileh, *outfilecpp, *dummyfile, *licensefile = NULL; namelist classes_used, tmpclass; int tmpdirlgth, tmpfilelgth; if (b->outdir == NULL) { b->outdir = "."; } tmpdirlgth = strlen(b->outdir); tmplist = b->classlist; /* open license file */ if ( b->license != NULL ) { licensefile = fopen(b->license, "r"); if(!licensefile) { fprintf(stderr, "Can't open the license file.\n"); exit(2); } } while ( tmplist != NULL ) { if ( ! ( is_present(b->classes, tmplist->key->name) ^ b->mask ) ) { tmpname = strtolower(tmplist->key->name); /* This prevents buffer overflows */ tmpfilelgth = strlen(tmpname); if (tmpfilelgth + tmpdirlgth > sizeof(*outfilename) - 2) { fprintf(stderr, "Sorry, name of file too long ...\nTry a smaller dir name\n"); exit(4); } sprintf(outfilename, "%s/%s.h", b->outdir, tmpname); dummyfile = fopen(outfilename, "r"); if ( b->clobber || ! dummyfile ) { outfileh = fopen(outfilename, "w"); if ( outfileh == NULL ) { fprintf(stderr, "Can't open file %s for writing\n", outfilename); exit(3); } /* This prevents buffer overflows */ tmpfilelgth = strlen(tmpname); if (tmpfilelgth + tmpdirlgth > sizeof(*outfilename) - 2) { fprintf(stderr, "Sorry, name of file too long ...\nTry a smaller dir name\n"); exit(4); } sprintf(outfilename, "%s/%s.c", b->outdir, tmpname); outfilecpp = fopen(outfilename, "w"); if ( outfilecpp == NULL ) { fprintf(stderr, "Can't open file %s for writing\n", outfilename); exit(3); } /* add license to the header */ if(b->license != NULL){ char lc; rewind(licensefile); while((lc = fgetc(licensefile)) != EOF){ fprintf(outfileh,"%c",lc); fprintf(outfilecpp,"%c",lc); } } fprintf(outfilecpp, "#include \"%s.h\"\n\n", tmpname); free(tmpname); tmpname = strtoupper(tmplist->key->name); fprintf(outfileh, "#ifndef __%s_H__\n", tmpname); fprintf(outfileh, "#define __%s_H__\n\n", tmpname); fprintf(outfileh, "#define %s(OBJ) ((%s*)OBJ)\n\n", tmpname, tmplist->key->name); free(tmpname); fprintf(outfileh, "#ifndef String\n#define String char*\n#endif\n\n"); classes_used = find_classes(tmplist, b); tmpclass = classes_used; while (tmpclass != NULL) { tmpname = strtolower(tmpclass->name); fprintf(outfileh, "#include \"%s.h\"\n", tmpname); tmpclass = tmpclass->next; free(tmpname); } fprintf(outfileh, "\n"); if ( strlen(tmplist->key->stereotype) > 0 ) { fprintf(outfileh, "// %s\n", tmplist->key->stereotype); } fprintf(outfileh, "typedef struct _%s %s;\n\n", tmplist->key->name, tmplist->key->name); fprintf(outfileh, "struct _%s", tmplist->key->name); fprintf(outfileh, " {\n"); parents = tmplist->parents; if (parents != NULL) { fprintf(outfileh, "%s super;\n", parents->key->name); } fprintf(outfileh, " /** Attributes **/\n"); tmpv = -1; umla = tmplist->key->attributes; while ( umla != NULL) { if (!umla->key.isstatic) { fprintf(outfileh, " "); if ( tmpv != umla->key.visibility ) { switch (umla->key.visibility) { case '0': fprintf (outfileh, "/*public*/\n "); break; case '1': fprintf (outfileh, "/*private*/\n "); break; case '2': fprintf (outfileh, "/*protected*/\n "); break; } tmpv = umla->key.visibility; } else { fprintf (outfileh, " "); } fprintf(outfileh, "%s %s", umla->key.type, umla->key.name); /*if ( umla->key.value[0] != 0 && umla->key.isstatic) { fprintf(outfilecpp,"%s %s::%s",umla->key.type,tmplist->key->name,umla->key.name); fprintf(outfilecpp," = %s",umla->key.value); fprintf(outfilecpp,";\n"); }*/ fprintf(outfileh, ";\n"); } else { fprintf(outfilecpp, "static %s %s", umla->key.type, umla->key.name); fprintf(outfilecpp, " = %s", umla->key.value); fprintf(outfilecpp, ";\n"); } umla = umla->next; } fprintf(outfileh, " /** Associations **/\n"); associations = tmplist->associations; while ( associations != NULL) { fprintf(outfileh, " %s ", associations->key->name); if (associations->composite == 0) { fprintf(outfileh, "* "); } fprintf(outfileh, "%s;\n", associations->name); associations = associations->next; } /***** VIRTUAL METHODS *****/ /* Virtuald methods should be in the structure */ umlo = tmplist->key->operations; fprintf(outfileh, "/** Operations **/\n"); tmpv = -1; while ( umlo != NULL) { if ( umlo->key.attr.isabstract ) { if ( tmpv != umlo->key.attr.visibility ) { switch (umlo->key.attr.visibility) { case '0': fprintf(outfileh, "/*public*/\n"); break; case '1': fprintf(outfileh, "/*private*/\n"); break; case '2': fprintf(outfileh, "/*protected*/\n"); break; } tmpv = umlo->key.attr.visibility; } if ( umlo->key.attr.isstatic ) { /*static virtual methods are not supported yet */ } else { fprintf(outfileh, "%s (*%s) ( %s *this", umlo->key.attr.type, umlo->key.attr.name, tmplist->key->name); if ( ! umlo->key.attr.isabstract ) { fprintf(outfilecpp, "%s %s_%s ( %s *this", umlo->key.attr.type, tmplist->key->name, umlo->key.attr.name, tmplist->key->name); } } tmpa = umlo->key.parameters; if (tmpa != NULL) { fprintf(outfileh, ", "); } while (tmpa != NULL) { fprintf(outfileh, "%s %s", tmpa->key.type, tmpa->key.name); if ( ! umlo->key.attr.isabstract ) { fprintf(outfilecpp, "%s %s", tmpa->key.type, tmpa->key.name); } /*if ( tmpa->key.value[0] != 0 ) { fprintf(outfileh," = %s",tmpa->key.value); if ( ! umlo->key.attr.isabstract ) { fprintf(outfilecpp," = %s",tmpa->key.value); } }*/ tmpa = tmpa->next; if (tmpa != NULL) { fprintf(outfileh, ", "); } } fprintf(outfileh, " )"); if ( umlo->key.attr.value[0] != 0 ) { fprintf(outfileh, " = %s", umlo->key.attr.value); } fprintf(outfileh, ";\n"); } umlo = umlo->next; } fprintf(outfileh, "};\n\n"); /***** ALL METHODS ******/ umlo = tmplist->key->operations; fprintf(outfileh, "/** Operations **/\n"); tmpv = -1; while ( umlo != NULL) { if ( tmpv != umlo->key.attr.visibility ) { switch (umlo->key.attr.visibility) { case '0': fprintf(outfileh, "/*public*/\n"); break; case '1': fprintf(outfileh, "/*private*/\n"); break; case '2': fprintf(outfileh, "/*protected*/\n"); break; } tmpv = umlo->key.attr.visibility; } if ( umlo->key.attr.isstatic ) { /*static methods doesn't receive the instance */ fprintf(outfileh, "%s %s_%s ( ", umlo->key.attr.type, tmplist->key->name, umlo->key.attr.name); fprintf(outfilecpp, "%s %s_%s ( ", umlo->key.attr.type, tmplist->key->name, umlo->key.attr.name); } else { fprintf(outfileh, "%s %s_%s ( %s *this", umlo->key.attr.type, tmplist->key->name, umlo->key.attr.name, tmplist->key->name); fprintf(outfilecpp, "%s %s_%s ( %s *this", umlo->key.attr.type, tmplist->key->name, umlo->key.attr.name, tmplist->key->name); } tmpa = umlo->key.parameters; if (tmpa != NULL) { fprintf(outfileh, ", "); fprintf(outfilecpp, ", "); } while (tmpa != NULL) { fprintf(outfileh, "%s %s", tmpa->key.type, tmpa->key.name); fprintf(outfilecpp, "%s %s", tmpa->key.type, tmpa->key.name); /*if ( tmpa->key.value[0] != 0 ) { fprintf(outfileh," = %s",tmpa->key.value); if ( ! umlo->key.attr.isabstract ) { fprintf(outfilecpp," = %s",tmpa->key.value); } }*/ tmpa = tmpa->next; if (tmpa != NULL) { fprintf(outfileh, ", "); if ( ! umlo->key.attr.isabstract ) { fprintf(outfilecpp, ", "); } } } fprintf(outfileh, " )"); fprintf(outfilecpp, " )"); if ( umlo->key.attr.value[0] != 0 ) { fprintf(outfileh, " = %s", umlo->key.attr.value); } fprintf(outfileh, ";\n"); if ( umlo->key.attr.isabstract ) { fprintf(outfilecpp, "{\n this->%s(", umlo->key.attr.name); tmpa = umlo->key.parameters; if (tmpa != NULL) { fprintf(outfilecpp, "this, "); } else { fprintf(outfilecpp, "this"); } while (tmpa != NULL) { fprintf(outfilecpp, "%s", tmpa->key.name); tmpa = tmpa->next; if (tmpa != NULL) { fprintf(outfilecpp, ", "); } } fprintf(outfilecpp, ");\n}\n\n"); } else { fprintf(outfilecpp, "{\n}\n\n"); } umlo = umlo->next; } fprintf(outfileh, "#endif\n"); fclose(outfileh); fclose(outfilecpp); } } tmplist = tmplist->next; } }