📄 ospfd_browser.c
字号:
sprintf(buffer, "0x%08x", ntoh32(lshdr->ls_seqno)); addVP(&pairs, "seqno", buffer); sprintf(buffer, "0x%04x", ntoh16(lshdr->ls_xsum)); addVP(&pairs, "lsa_xsum", buffer); sprintf(buffer, "%d", ntoh16(lshdr->ls_length)); addVP(&pairs, "lsa_len", buffer); ptr = buffer; age = ntoh16(lshdr->ls_age); if ((age & DoNotAge) != 0) { age &= ~DoNotAge; strcpy(buffer, "DNA+"); ptr += strlen(buffer); } sprintf(ptr, "%d", age); addVP(&pairs, "ls_age", buffer); display_html(database_row); } sprintf(buffer, "%d", n_lsas); addVP(&pairs, "n_lsas", buffer); sprintf(buffer, "0x%x", xsum); addVP(&pairs, "area_xsum", buffer); display_html(database_page_bottom);}/* Get the Opaque-LSAs, through the registration interface. */void get_opaques(){ MonMsg req; int mlen; int n_lsas = 0; uns32 xsum = 0; display_html(opaque_page_top); req.hdr.version = OSPF_MON_VERSION; req.hdr.retcode = 0; req.hdr.exact = 0; mlen = sizeof(MonHdr); req.hdr.id = hton16(id++); if (!monpkt->sendpkt_suspend(&req, MonReq_OpqReg, 0, mlen)) { display_error("Send failed"); exit(0); } while (1) { MonHdr *mhdr; MonMsg *m; LShdr *lshdr; in_addr in; uns16 type; uns16 subtype; age_t age; char *ptr; req.hdr.version = OSPF_MON_VERSION; req.hdr.retcode = 0; req.hdr.exact = 0; mlen = sizeof(MonHdr); req.hdr.id = hton16(id++); if (!monpkt->sendpkt_suspend(&req, MonReq_OpqNext, 0, mlen)) { display_error("Send failed"); exit(0); } if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) { display_error("Receive failed"); exit(0); } m = (MonMsg *) mhdr; if (m->hdr.retcode != 0) break; addVP(&pairs, "phyint", "n/a"); addVP(&pairs, "if_addr", "n/a"); addVP(&pairs, "area_id", "n/a"); n_lsas++; lshdr = (LShdr *) (((char *) m) + sizeof(MonHdr) + sizeof(OpqRsp)); xsum += ntoh16(lshdr->ls_xsum); sprintf(buffer, "%d", lshdr->ls_type); addVP(&pairs, "ls_typeno", buffer); // Print out Link state header switch (lshdr->ls_type) { case LST_LINK_OPQ: addVP(&pairs, "ls_type", "Link Opaque"); sprintf(buffer, "%d", ntoh32(m->body.opqrsp.phyint)); addVP(&pairs, "phyint", buffer); in = *((in_addr *) &m->body.opqrsp.if_addr); addVP(&pairs, "if_addr", inet_ntoa(in)); break; case LST_AREA_OPQ: addVP(&pairs, "ls_type", "Area Opaque"); in = *((in_addr *) &m->body.opqrsp.a_id); addVP(&pairs, "area_id", inet_ntoa(in)); break; case LST_AS_OPQ: addVP(&pairs, "ls_type", "AS Opaque"); break; } in = *((in_addr *) &lshdr->ls_id); addVP(&pairs, "ls_id", inet_ntoa(in)); in = *((in_addr *) &lshdr->ls_org); addVP(&pairs, "adv_rtr", inet_ntoa(in)); sprintf(buffer, "0x%08x", ntoh32(lshdr->ls_seqno)); addVP(&pairs, "seqno", buffer); sprintf(buffer, "0x%04x", ntoh16(lshdr->ls_xsum)); addVP(&pairs, "lsa_xsum", buffer); sprintf(buffer, "%d", ntoh16(lshdr->ls_length)); addVP(&pairs, "lsa_len", buffer); ptr = buffer; age = ntoh16(lshdr->ls_age); if ((age & DoNotAge) != 0) { age &= ~DoNotAge; strcpy(buffer, "DNA+"); ptr += strlen(buffer); } sprintf(ptr, "%d", age); addVP(&pairs, "ls_age", buffer); display_html(opaque_row); } sprintf(buffer, "%d", n_lsas); addVP(&pairs, "n_lsas", buffer); sprintf(buffer, "0x%x", xsum); addVP(&pairs, "area_xsum", buffer); display_html(database_page_bottom);}/* Ask the user to select a particular LSA to * expand. *//* Get the link-state database of a given area, printing * one line for each LSA. If called with LS type of 5 * dumps AS-external-LSAs instead. */void select_lsa(){ ValuePair *entry; get_areas(false); display_html(select_lsa_top); for (int i=0; ; i++) { sprintf(buffer, "area_id%d", i); if (!(entry = (ValuePair *)pairs.find(buffer))) break; printf("<option>%s\n", entry->value); } display_html(select_lsa_bottom);}/* Get a given LSA, and print it out in detail. * Area must be specified for all advertisements except * the AS-external-LSAs. */void get_lsa(){ MonMsg req; int mlen; uns32 a_id=0; uns32 ls_id=0; uns32 adv_rtr=0; byte lstype=0; MonHdr *mhdr; MonMsg *m; LShdr *lshdr; uns16 type; uns16 subtype; ValuePair *entry; if ((entry = (ValuePair *)pairs.find("ls_type"))) lstype = atoi(entry->value); if ((entry = (ValuePair *)pairs.find("ls_id"))) ls_id = ntoh32(inet_addr(entry->value)); if ((entry = (ValuePair *)pairs.find("adv_rtr"))) adv_rtr = ntoh32(inet_addr(entry->value)); if ((entry = (ValuePair *)pairs.find("area_id"))) a_id = ntoh32(inet_addr(entry->value)); req.hdr.version = OSPF_MON_VERSION; req.hdr.retcode = 0; req.hdr.exact = 1; req.body.lsarq.area_id = hton32(a_id); req.body.lsarq.ls_type = hton32(lstype); req.body.lsarq.ls_id = hton32(ls_id); req.body.lsarq.adv_rtr = hton32(adv_rtr); mlen = sizeof(MonHdr) + sizeof(MonRqLsa); req.hdr.id = hton16(id++); if (!monpkt->sendpkt_suspend(&req, MonReq_LSA, 0, mlen)) { display_error("Send failed"); exit(0); } if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) { display_error("Receive failed"); exit(0); } m = (MonMsg *) mhdr; if (m->hdr.retcode != 0) { printf("LSA not found\n"); return; } // Print out LSA lshdr = (LShdr *) (((char *) m) + mlen); display_html(expand_lsa_top); print_lsa(lshdr); display_html(expand_lsa_bottom);}/* Print out a line for each interface. */void get_interfaces(){ MonMsg req; int mlen; int phyint; InAddr addr; InAddr taid; InAddr endpt; MonHdr *mhdr; MonMsg *m; uns16 type; uns16 subtype; get_statistics(false); display_html(interface_page_top); // First real interfaces for (addr = 0, phyint = 0; ; ) { req.hdr.version = OSPF_MON_VERSION; req.hdr.retcode = 0; req.hdr.exact = 0; req.body.ifcrq.phyint = hton32(phyint); req.body.ifcrq.if_addr = hton32(addr); mlen = sizeof(MonHdr) + sizeof(MonRqIfc); req.hdr.id = hton16(id++); if (!monpkt->sendpkt_suspend(&req, MonReq_Ifc, 0, mlen)) { display_error("Send failed"); exit(0); } if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) { display_error("Receive failed"); exit(0); } m = (MonMsg *) mhdr; if (m->hdr.retcode != 0) break; phyint = ntoh32(m->body.ifcrsp.if_phyint); addr = ntoh32(m->body.ifcrsp.if_addr); print_interface(m); } // Then virtual links for (taid = 0, endpt = 0; ; ) { req.hdr.version = OSPF_MON_VERSION; req.hdr.retcode = 0; req.hdr.exact = 0; req.body.vlrq.transit_area = hton32(taid); req.body.vlrq.endpoint_id = hton32(endpt); mlen = sizeof(MonHdr) + sizeof(MonRqVL); req.hdr.id = hton16(id++); if (!monpkt->sendpkt_suspend(&req, MonReq_VL, 0, mlen)) { display_error("Send failed"); exit(0); } if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) { display_error("Receive failed"); exit(0); } m = (MonMsg *) mhdr; if (m->hdr.retcode != 0) break; taid = ntoh32(m->body.ifcrsp.transit_id); endpt = ntoh32(m->body.ifcrsp.endpt_id); print_interface(m); } display_html(interface_page_bottom);}/* Print the interface information. Used for both real interfaces * and virtual links. */void print_interface(MonMsg *m){ IfcRsp *ifcrsp; in_addr in; ifcrsp = &m->body.ifcrsp; // Print out Interface info addVP(&pairs, "phyname", ifcrsp->phyname); in = *((in_addr *) &ifcrsp->if_addr); addVP(&pairs, "if_addr", inet_ntoa(in)); in = *((in_addr *) &ifcrsp->area_id); addVP(&pairs, "if_area", inet_ntoa(in)); addVP(&pairs, "if_type", ifcrsp->type); addVP(&pairs, "if_state", ifcrsp->if_state); sprintf(buffer, "%d", ifcrsp->if_nnbrs); addVP(&pairs, "if_nnbrs", buffer); sprintf(buffer, "%d", ifcrsp->if_nfull); addVP(&pairs, "if_nfull", buffer); sprintf(buffer, "%d", ntoh16(ifcrsp->if_cost)); addVP(&pairs, "if_cost", buffer); display_html(interface_row);}/* Print out a line for each neighbor */void get_neighbors(){ MonMsg req; int mlen; int phyint; InAddr addr; InAddr taid; InAddr endpt; MonHdr *mhdr; MonMsg *m; uns16 type; uns16 subtype; get_statistics(false); display_html(neighbor_page_top); // First the real neighbors for (addr = 0, phyint = 0; ; ) { req.hdr.version = OSPF_MON_VERSION; req.hdr.retcode = 0; req.hdr.exact = 0; req.body.nbrrq.phyint = hton32(phyint); req.body.nbrrq.nbr_addr = hton32(addr); mlen = sizeof(MonHdr) + sizeof(MonRqNbr); req.hdr.id = hton16(id++); if (!monpkt->sendpkt_suspend(&req, MonReq_Nbr, 0, mlen)) { display_error("Send failed"); exit(0); } if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) { display_error("Receive failed"); exit(0); } m = (MonMsg *) mhdr; if (m->hdr.retcode != 0) break; phyint = ntoh32(m->body.nbrsp.phyint); addr = ntoh32(m->body.nbrsp.n_addr); print_neighbor(m); } // Then virtual links for (taid = 0, endpt = 0; ; ) { req.hdr.version = OSPF_MON_VERSION; req.hdr.retcode = 0; req.hdr.exact = 0; req.body.vlrq.transit_area = hton32(taid); req.body.vlrq.endpoint_id = hton32(endpt); mlen = sizeof(MonHdr) + sizeof(MonRqVL); req.hdr.id = hton16(id++); if (!monpkt->sendpkt_suspend(&req, MonReq_VLNbr, 0, mlen)) { display_error("Send failed"); exit(0); } if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) { display_error("Receive failed"); exit(0); } m = (MonMsg *) mhdr; if (m->hdr.retcode != 0) break; taid = ntoh32(m->body.nbrsp.transit_id); endpt = ntoh32(m->body.nbrsp.endpt_id); print_neighbor(m); } display_html(neighbor_page_bottom);}/* Print out single line for neighbor. Used by both * real neighbors and virtual neighbors. */void print_neighbor(MonMsg *m){ in_addr in; NbrRsp *nbrsp; char buffer[20]; nbrsp = &m->body.nbrsp; // Print out Neighbor info addVP(&pairs, "phyname", nbrsp->phyname); in = *((in_addr *) &nbrsp->n_addr); addVP(&pairs, "n_addr", inet_ntoa(in)); in = *((in_addr *) &nbrsp->n_id); addVP(&pairs, "n_id", inet_ntoa(in)); memset(buffer, 0, sizeof(buffer)); memcpy(buffer, nbrsp->n_state, MON_STATELEN); addVP(&pairs, "n_state", buffer); sprintf(buffer, "%d", ntoh32(nbrsp->n_ddlst)); addVP(&pairs, "n_ddlst", buffer); sprintf(buffer, "%d", ntoh32(nbrsp->n_rqlst)); addVP(&pairs, "n_rqlst", buffer); sprintf(buffer, "%d", ntoh32(nbrsp->rxmt_count)); addVP(&pairs, "rxmt_count", buffer); display_html(neighbor_row);}/* Print out a line for each prefix in the routing table. */void get_rttbl(){ MonMsg req; int mlen; uns32 net; uns32 mask; int i; get_statistics(false); display_html(rttbl_page_top); for (net = 0, mask = 0, i = 0; ; i++) { MonHdr *mhdr; MonMsg *m; in_addr in; RteRsp *rtersp; int prefix_length; int n_paths; uns16 type; uns16 subtype; req.hdr.version = OSPF_MON_VERSION; req.hdr.retcode = 0; req.hdr.exact = (i == 0) ? 1 : 0; req.body.rtrq.net = hton32(net); req.body.rtrq.mask = hton32(mask); mlen = sizeof(MonHdr) + sizeof(MonRqRte); req.hdr.id = hton16(id++); if (!monpkt->sendpkt_suspend(&req, MonReq_Rte, 0, mlen)) { display_error("Send failed"); exit(0); } if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) { display_error("Receive failed"); exit(0); } m = (MonMsg *) mhdr; if (m->hdr.retcode != 0) { if (i != 0) break; continue; } net = ntoh32(m->body.rtersp.net); mask = ntoh32(m->body.rtersp.mask); rtersp = &m->body.rtersp; // Print out Area info in = *((in_addr *) &m->body.rtersp.net); for (prefix_length = 32; prefix_length > 0; prefix_length--) { if ((mask & (1 << (32-prefix_length))) != 0) break; } sprintf(buffer, "%s/%d", inet_ntoa(in), prefix_length); addVP(&pairs, "prefix", buffer); addVP(&pairs, "rt_type", rtersp->type); sprintf(buffer, "%d", ntoh32(rtersp->cost)); addVP(&pairs, "rt_cost", buffer); n_paths = ntoh32(rtersp->npaths); sprintf(buffer, "%d", n_paths); addVP(&pairs, "rt_paths", buffer); if (n_paths == 0) { addVP(&pairs, "rt_ifc", "n/a"); addVP(&pairs, "rt_nh", "n/a"); } else { addVP(&pairs, "rt_ifc", rtersp->hops[0].phyname); in = *((in_addr *) &rtersp->hops[0].gw); if (rtersp->hops[0].gw == 0) addVP(&pairs, "rt_nh", "n/a"); else addVP(&pairs, "rt_nh", inet_ntoa(in)); } display_html(rttbl_row); } display_html(rttbl_page_bottom);}/* Display HTML, substituting for $variable$ * in the string argument. */void display_html(char *s){ char *ptr; char *next; char *end; end = s + strlen(s); for (ptr = s; (next = strchr(ptr, '$')); ptr = next+1) { int size; ValuePair *entry; size = next - ptr; fwrite(ptr, size, 1, stdout); ptr = next+1; next = strchr(ptr, '$'); size = next - ptr; entry = (ValuePair *) pairs.find((byte *)ptr, size); if (entry) fwrite(entry->value, entry->vlen, 1, stdout); } if (end > ptr) fwrite(ptr, end-ptr, 1, stdout);}/* HTML pages. *//* The header that is printed with every * page. */char *page_header = "\<html>\n\<head>\n\<title>OSPFD browser: $cstate$ $command$</title>\n\<meta name=description content=\"A Web based monitor of the ospfd\n\routing protocol daemon\">\n\</head>\n\\n\<body bgcolor=white text=black>\n\\n\<center>\n\<table>\n\<tr>\n\<td align=\"center\">\n\\n\<table>\n\<tr align=\"center\">\n\\n\<!-- Left hand column -->\n\<td>\n\<table cellpadding=0 cellspacing=0 border=0 width=130>\n\<tr bgcolor=\"#efefef\">\n\<td>\n\This is the statistics browser for the OSPFD routing daemon.\n\Definitions of the various OSPF statistics can be\n\found in the\n\<a href=\"http://www.ietf.org/rfc/rfc1850.txt\">OSPF MIB.</a>\n\
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -