📄 analyzer.cpp
字号:
(struct sockaddr *) &sin, /* socket addr, just like in */ sizeof (sin)) < 0) /* a normal send() */ { close(s); exit(1); } else return recvfd;}int Analyzer::FragSender(string ipv6In, string ipv6Out, string ipv4In, string ipv4Out, int hoplimit) { RawSender s1; int recvfd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6); if(recvfd < 0) { perror("socket"); close(recvfd); return -1; } char datagram[4096]; /* this buffer will contain ip header, ipv6 header, icmpv6 header and payload. we'll point an ip header structure at its beginning, ipv6 header structure and icmpv6 header structure after that to write the header values into it */ char icmp6PL[8]; struct ip *iph = (struct ip *) datagram; memset (datagram, 0, 4096); /* zero out the buffer */ /* we'll now fill in the ip header values, see above for explanations */ iph->ip_hl = 5; iph->ip_v = 4; iph->ip_tos = 0; iph->ip_len = mtu; iph->ip_id = 12756; /* the value doesn't matter here */ iph->ip_off = 0; iph->ip_ttl = 255; iph->ip_p = 41; //6 - tcp, 1 - icmp iph->ip_sum = 0; /* set it to 0 before computing the actual checksum later */ iph->ip_src.s_addr = inet_addr (ipv4In.c_str()); iph->ip_dst.s_addr = inet_addr (ipv4Out.c_str()); iph->ip_sum = csum ((unsigned short *) datagram, (iph->ip_hl*2) >> 1); struct ip6_hdr *ip6 = (struct ip6_hdr *) (datagram + sizeof(struct ip)); in6_addr in; in6_addr *p_in=∈ int res=inet_pton(AF_INET6, ipv6In.c_str() , p_in); in6_addr in2; in6_addr *p_in2=&in2; int res2=inet_pton(AF_INET6, ipv6Out.c_str() , p_in2); uint32_t pippo; pippo = 6<<28 |0 << 8|0; ip6->ip6_flow = htonl(pippo); //for setting version = 6 ip6->ip6_plen = htons(mtu - 40); ip6->ip6_nxt = 58; //58 = IPPROTO_ICMP6 ip6->ip6_hlim = hoplimit; ip6->ip6_src = in; ip6->ip6_dst = in2; struct icmp6_hdr *icmp; icmp = (struct icmp6_hdr *) (datagram + sizeof(struct ip) + sizeof(struct ip6_hdr)); icmp->icmp6_type = 128; icmp->icmp6_code = 0; icmp->icmp6_cksum = 0; icmp->icmp6_id =pid; icmp->icmp6_seq=sequence; memset(&icmp6PL, 0, sizeof(icmp6PL)); struct icmp6_hdr *icmp2; icmp2 = (struct icmp6_hdr *) icmp6PL; icmp2->icmp6_type = icmp->icmp6_type; icmp2->icmp6_code = icmp->icmp6_code; icmp2->icmp6_cksum = 0; icmp2->icmp6_id=icmp->icmp6_id; icmp2->icmp6_seq=icmp->icmp6_seq; icmp->icmp6_cksum = pseudo_check(ip6->ip6_src,ip6->ip6_dst, &icmp6PL, sizeof(icmp6PL), 58); //input to sender /* for(int i=0; i<iph->ip_len; i++) { printf("%02x ", (int)*(datagram + i)); if (!((i+1)%16)) printf("\n"); }; printf("\n");*/ //To call the RawSender, fragmentation and sending module, where I give in the //whole packet and the size of each fragment, that has to be multiple of 8 if (s1.SendPacket(datagram,68)==-1) return -1; return recvfd; }void Analyzer::setIpRef(string ipApp) { ipRef = ipApp;}void Analyzer::setIpRefPath(string pathApp) { ipRefPath = pathApp;}void Analyzer::setOption(string opApp) { option = opApp;}//checks if a tunnel is a vantage point.. and stores it into vp, in future to implement a list instead of vpint Analyzer::setVanPoint(Segment s) { if (s.getIsTunnel()==true) { vp=s; return 1; } else return 0;}//for now just resolves into ipv4 addressesstring Analyzer::resolve(string hostn) { IfAna ia; return ia.resolve(hostn);}Segment Analyzer::analyse(string a,string b) { SegmentAna sa; sa.setIpRefPath(ipRefPath); sa.setIpRef(ipRef); sa.setOption(option); Segment s; int addA; //if a is an ipv4 address (1) or not (0) int addB; in_addr in; in_addr *p_in=∈ const char* s_addr=a.c_str(); in_addr in2; in_addr *p_in2=&in2; const char* s_addr2=b.c_str(); int res=inet_pton(AF_INET, s_addr, p_in); int res2=inet_pton(AF_INET, s_addr2, p_in2); if(res==1){ addA = 1; if(res2==1){ addB = 1; } else addB=0; } else { addA=0; if(res2==1){ addB = 1; } else addB=0; } switch(addA){ case(1): if (addB==1) s=sa.create("","",a,b); else { if (resolve(b)!="") s=sa.create("",b,a,resolve(b)); else { if (option == "-v") { printf("UNKNOWN2 "); printf("%s -> %s\n",a.c_str(),b.c_str()); } else cerr <<"error resolving interfaces"<<endl; exit(2); } } break; case(0): if (addB==1) { if (resolve(a)!="") s=sa.create(a,"",resolve(a),b); else { if (option == "-v") { printf("UNKNOWN1 "); printf("%s -> %s\n",a.c_str(),b.c_str()); } else cerr <<"error resolving interfaces"<<endl; exit(2); } } else if ((resolve(b)!="")&&(resolve(a)!="")) s=sa.create(a,b,resolve(a),resolve(b)); else { if (option == "-v") { if ((resolve(a)=="")&&(resolve(b)=="")) printf("UNKNOWN12 "); else if (resolve(a)=="") printf("UNKNOWN1 "); else printf("UNKNOWN2 "); printf("%s -> %s\n",a.c_str(),b.c_str()); } else cerr <<"error resolving interfaces"<<endl; exit(2); } break; } // cout <<s.getHostnamein()<<endl; s=sa.analyse(s); setVanPoint(s); return s;}string Analyzer::resolve6(string hostn) { IfAna ia; return ia.resolve6(hostn);}void Analyzer::analyse(string a) { //if we are in the case of tunneltrace B with B = IPv6 string ipDest; SegmentAna sa; sa.setIpRefPath(ipRefPath); sa.setIpRef(ipRef); sa.setOption(option); Segment s1; in6_addr in; in6_addr *p_in=∈ int res=inet_pton(AF_INET6, a.c_str() , p_in); if(res==1) { ipDest = a; // a = IPv6 address } else { if (resolve6(a)!="") { ipDest = resolve6(a); //a = hostname } else { cerr <<"error resolving interfaces"<<endl; exit(2); } } printf("30 Hops MAX\n"); NameIpAna n1; string result = ""; string previous = ""; string ownIp = getOwnIp(ipDest); //to find the IP of the machine where Tunneltrace is running while ((result != "ok")&&(sequence<30)) { sequence++; relsequence++; previous = result; //to remember the previous node, for finding segments if (vp4dest=="") result = Receiver(Sender(ownIp,ipDest,sequence)); else result = Receiver(FragSender(ownIp, ipDest, vp4source, vp4dest,relsequence)); if (result!="ok") { if (result=="") { int tentative = 0; if (vp4dest!="") { //we tryed once fragmented traceroute6 but it didnt' work, so let's mtu = appmtu; //put everything back as it was before appmtu = 0; if (vp4destapp=="") { vp4source = ""; vp4dest = ""; relsequence = 0; } else { vp4source = vp4sourceapp; vp4dest = vp4destapp; relsequence = relsequenceapp; vp4sourceapp = ""; vp4destapp = ""; relsequenceapp = 0; } } else tentative = 1; while ((tentative<3)&&(result=="")) { if (vp4dest=="") result = Receiver(Sender(ownIp,ipDest,sequence)); else result = Receiver(FragSender(ownIp, ipDest, vp4source, vp4dest,relsequence)); tentative++; } if (result=="") { //node unreachable printf("%i *\n",sequence); } else { if ((result!="ok")&&(result!=ipDest)){ //node found printf("%i %s(%s) %i\n",sequence,n1.resolvehost6(result).c_str(),result.c_str(),mtu); if (previous!="") { //we have a valid segment to analyze s1 = sa.create(previous,result); Segment s2; s2=sa.analyse(s1); string ipv6in,ipv6out; if (s2.getIsTunnel()==true) { //let's set the outern endpoint as a vantagePoint for fragmented Traceroute6: if (vp4source != "") { vp4sourceapp = vp4source; //we already used a functional vantagepoint vp4destapp = vp4dest; //so let's back it up in case this new one doesn't work relsequenceapp = relsequence; } vp4source = s2.getIpv4in(); vp4dest = s2.getIpv4out(); relsequence = 0; appmtu = mtu; mtu = 1500; printf("%s(%s) -> %s(%s)\n",s2.getHostnamein().c_str(),s2.getIpv4in().c_str(), s2.getHostnameout().c_str(),s2.getIpv4out().c_str()); if (s2.getIpv6in()=="") ipv6in = s1.getIpv6in(); else ipv6in = s2.getIpv6in(); if (s2.getIpv6out()=="") ipv6out = s1.getIpv6in(); else ipv6out = s2.getIpv6out(); printf("%s -> %s ; CONFIDENCE %i/10\n",ipv6in.c_str(),ipv6out.c_str(),s2.getConfidence()); } } } else if (result==ipDest) result = "ok"; } } else { //node found if (result!=ipDest) { printf("%i %s(%s) %i\n",sequence,n1.resolvehost6(result).c_str(),result.c_str(),mtu); if (previous!="") { //we have a valid segment to analyze s1 = sa.create(previous,result); Segment s2; s2=sa.analyse(s1); string ipv6in,ipv6out; if (s2.getIsTunnel()==true) { //let's set the outern endpoint as a vantagePoint for fragmented Traceroute6: if (vp4source != "") { vp4sourceapp = vp4source; //we already used a functional vantagepoint vp4destapp = vp4dest; //so let's back it up in case this new one doesn't work relsequenceapp = relsequence; } vp4source = s2.getIpv4in(); vp4dest = s2.getIpv4out(); relsequence = 0; appmtu = mtu; mtu = 1500; printf("%s(%s) -> %s(%s)\n",s2.getHostnamein().c_str(),s2.getIpv4in().c_str(), s2.getHostnameout().c_str(),s2.getIpv4out().c_str()); if (s2.getIpv6in()=="") ipv6in = s1.getIpv6in(); else ipv6in = s2.getIpv6in(); if (s2.getIpv6out()=="") ipv6out = s1.getIpv6in(); else ipv6out = s2.getIpv6out(); printf("%s -> %s ; CONFIDENCE %i/10\n",ipv6in.c_str(),ipv6out.c_str(),s2.getConfidence()); } } } else result = "ok"; } } } if (result=="ok") { printf("%i %s(%s) %i\n",sequence,n1.resolvehost6(ipDest).c_str(),ipDest.c_str(),mtu); if (previous!="") { //we have a valid segment to analyze s1 = sa.create(previous,result); Segment s2; s2=sa.analyse(s1); string ipv6in,ipv6out; if (s2.getIsTunnel()==true) { printf("%s(%s) -> %s(%s)\n",s2.getHostnamein().c_str(),s2.getIpv4in().c_str(), s2.getHostnameout().c_str(),s2.getIpv4out().c_str()); if (s2.getIpv6in()=="") ipv6in = s1.getIpv6in(); else ipv6in = s2.getIpv6in(); if (s2.getIpv6out()=="") ipv6out = s1.getIpv6in(); else ipv6out = s2.getIpv6out(); printf("%s -> %s ; CONFIDENCE %i/10\n",ipv6in.c_str(),ipv6out.c_str(),s2.getConfidence()); } } }}//returns the vantage point.. in the future the list of vp of a pathSegment Analyzer::getVanPoint() { return vp;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -