⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ospfd_mon.c

📁 BCAST Implementation for NS2
💻 C
📖 第 1 页 / 共 2 页
字号:
void get_database(byte lstype){    MonMsg req;    int mlen;    uns32 a_id;    uns32 ls_id;    uns32 adv_rtr;    byte new_lstype;    int n_lsas = 0;    uns32 xsum = 0;    if (lstype != LST_ASL) {	char *ptr;	ptr = buffer;	if (!strsep(&ptr, " ")) {	    syntax();	    return;	}	a_id = ntoh32(inet_addr(ptr));    }    else	a_id = 0;    ls_id = 0;    adv_rtr = 0;    new_lstype = lstype;    printf("%4s %15s %15s %10s %6s %4s\r\n",	   "Type", "LS_ID", "ADV_RTR", "Seqno", "Xsum", "Age");    while (1) {	MonHdr *mhdr;	MonMsg *m;	LShdr *lshdr;	in_addr in;	uns16 type;	uns16 subtype;	age_t age;	req.hdr.version = OSPF_MON_VERSION;	req.hdr.retcode = 0;	req.hdr.exact = 0;	req.body.lsarq.area_id = hton32(a_id);	req.body.lsarq.ls_type = hton32(new_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)) {            printf("Send failed");	    exit(1);	}	if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) {	    perror("recv");	    exit(1);	}	m = (MonMsg *) mhdr;	if (m->hdr.retcode != 0)	    break;	if (a_id != ntoh32(m->body.lsarq.area_id))	    break;	new_lstype = ntoh32(m->body.lsarq.ls_type);	if (new_lstype != lstype) {	    if (lstype == LST_ASL || new_lstype == LST_ASL)		break;	}	ls_id = ntoh32(m->body.lsarq.ls_id);	adv_rtr = ntoh32(m->body.lsarq.adv_rtr);	n_lsas++;	lshdr = (LShdr *) (((char *) m) + mlen);	xsum += ntoh16(lshdr->ls_xsum);	// Print out Link state header	printf("%4d ", lshdr->ls_type);	in = *((in_addr *) &lshdr->ls_id);	printf("%15s ", inet_ntoa(in));	in = *((in_addr *) &lshdr->ls_org);	printf("%15s ", inet_ntoa(in));	printf("0x%08x ", ntoh32(lshdr->ls_seqno));	printf("0x%04x ", ntoh16(lshdr->ls_xsum));	age = ntoh16(lshdr->ls_age);	if ((age & DoNotAge) != 0) {	    age &= ~DoNotAge;	    printf("DNA+%d\r\n", age);	}	else	    printf("%4d\r\n", age);    }    printf("\t\t# LSAs: %d\r\n", n_lsas);    printf("\t\tDatabase xsum: 0x%x\r\n", xsum);}/* 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;    printf("%-15s %-15s %-15s %-8s %-8s %-4s %-4s %-5s\r\n",	   "Phy", "Addr", "Area", "Type", "State", "#Nbr", "#Adj", "Cost");    // 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)) {            printf("Send failed");	    exit(1);	}	if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) {	    perror("recv");	    exit(1);	}	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)) {            printf("Send failed");	    exit(1);	}	if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) {	    perror("recv");	    exit(1);	}	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);   }}/* 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    printf("%-16s ", ifcrsp->phyname);    in = *((in_addr *) &ifcrsp->if_addr);    printf("%-15s ", inet_ntoa(in));    in = *((in_addr *) &ifcrsp->area_id);    printf("%-15s ", inet_ntoa(in));    printf("%-8s ", ifcrsp->type);    printf("%-8s ", ifcrsp->if_state);    printf("%-4d ", ifcrsp->if_nnbrs);    printf("%-4d ", ifcrsp->if_nfull);    printf("%-5d\r\n", ntoh16(ifcrsp->if_cost));}/* 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;    printf("%-15s %-15s %-15s %-8s %-4s %-4s %-5s\r\n",	   "Phy", "Addr", "ID", "State", "#DD", "#Rq", "#Rxmt");    // 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)) {            printf("Send failed");	    exit(1);	}	if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) {	    perror("recv");	    exit(1);	}	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)) {            printf("Send failed");	    exit(1);	}	if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) {	    perror("recv");	    exit(1);	}	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);    }}/* 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    printf("%-16s ", nbrsp->phyname);    in = *((in_addr *) &nbrsp->n_addr);    printf("%-15s ", inet_ntoa(in));    in = *((in_addr *) &nbrsp->n_id);    printf("%-15s ", inet_ntoa(in));    memset(buffer, 0, sizeof(buffer));    memcpy(buffer, nbrsp->n_state, MON_STATELEN);    printf("%-8s ", buffer);    printf("%-4d ", ntoh32(nbrsp->n_ddlst));    printf("%-4d ", ntoh32(nbrsp->n_rqlst));    printf("%-5d\r\n", ntoh32(nbrsp->rxmt_count));}/* Print out a line for each prefix in the routing table. */void get_rttbl(){    MonMsg req;    int mlen;    uns32 net;    uns32 mask;    int i;    printf("%-18s %-8s %-8s %-8s %-15s %s\r\n",	   "Prefix", "Type", "Cost", "Ifc", "Next-hop", "Mpaths");    for (net = 0, mask = 0, i = 0; ; i++) {	MonHdr *mhdr;	MonMsg *m;	in_addr in;	char str[20];	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)) {            printf("Send failed");	    exit(1);	}	if (monpkt->rcv_suspend((void **)&mhdr, type, subtype) == -1) {	    perror("recv");	    exit(1);	}	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(str, "%s/%d", inet_ntoa(in), prefix_length);	printf("%-18s ", str);	printf("%-8s ", rtersp->type);	printf("%-8d ", ntoh32(rtersp->cost));	n_paths = ntoh32(rtersp->npaths);	if (n_paths == 0)	    printf("%-8s %-15s", "n/a", "n/a");	else {	    printf("%-8s ", rtersp->hops[0].phyname);	    in = *((in_addr *) &rtersp->hops[0].gw);	    if (rtersp->hops[0].gw == 0)	        printf("%-15s ", "n/a");	    else	        printf("%-15s ", inet_ntoa(in));	    if (n_paths > 1)	        printf("%d", n_paths);	}	printf("\r\n");    }}/* Print a pair of numbers. The second is printed only * if it is different from the first, and then in * parenthesis. */void print_pair(char *s, int val1, int val2){    s[0] = '\0';    sprintf(s, "%d", val1);    if (val2 != val2) {	char *ptr;	ptr = s + strlen(s);	sprintf(ptr, "(%d)", val2);    }}const char *yesorno(byte val){    return((val != 0) ? "yes" : "no");}void syntax(){    printf("Command syntax:\n");    printf("adv %%type %%ls_id %%adv_rtr %%area_id\n");    printf("areas\n");    printf("as-externals\n");    printf("interfaces\n");    printf("neighbors\n");    printf("database %%area_id\n");    printf("routes\n");    printf("statistics\n");    printf("exit\n");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -