📄 sipb_bnftools.cpp
字号:
//sipb_bnftools.cpp//Copyright (C) 2003 Metalink LTD//Author: Rodionov Sergey (seger@metalinkltd.com)//This program is distributed under terms of GPL (see LICENSE)#include "sipb_bnftools.h"#include "sipb_md5.h"#include "sipb_addfun.h"#include <sstream>#include <iomanip>#include <stdexcept>#include <iostream>using namespace std;//------BASE PART--------string sipb_bnftools::hstr(string in){ if (in.size()==0) return ""; ostringstream out; out<<hex<<"%x"; for (unsigned int i=0;i<in.size();i++) { out<<hex<<setw(2)<<setfill('0')<<(unsigned int)((unsigned char)in[i]); if (i<in.size()-1) out<<'.'; } return out.str();}// string sipb_bnftools::str(string in){ return "\""+ in+ "\""; }// string sipb_bnftools::str(int in){ ostringstream out; out<<in; return "\""+out.str()+"\"";}// string sipb_bnftools::rand_nalpha(int l){ if (l<=0) return string(""); int size = 1+random() % l; return nalpha(size);}// string sipb_bnftools::nalpha(int size){ if (size==0) return ""; string rez; char c; for (int i=0;i<size;i++) { c=0; while (!isalpha(c)) c=random()%256; rez.push_back(c); } return rez;}// string sipb_bnftools::nprint(int size){ if (size==0) return ""; string rez; char c; for (int i=0;i<size;i++) { c=0; while (!isprint(c)) c=random()%256; rez.push_back(c); } return rez;}// void sipb_bnftools::create_topacket(bnf_parser&p,string rule,sipb_stpacket*pk, int rand_param){ p.reparse(rule, pk->pack, rand_param);}// bool sipb_bnftools::create_badrequest(string& rez,bnf_parser&pcr,bnf_parser&ppc, int rand_param,int max_clen,int c_quan, int ntry){ for (int i=0;i<ntry;i++) { pcr.reparse("Request",rez,rand_param); make_change(rez,max_clen,c_quan); bnf_parsval tmp; if (!ppc.try_parse(tmp,"Request",rez)) //Ok bad packet return true; } return false;}// void sipb_bnftools::make_change(string& rez,int max_clen,int c_quan){ if (max_clen<=0 || c_quan<=0) throw logic_error("Error in sipb_bnftools::make_change"); if (rez.size()==0) throw logic_error("Error in sipb_bnftools::make_change rez.size()==0"); for (int i=0;i<c_quan;i++) { int pos =random() % rez.size(); int clen=random() % (max_clen + 1); if (random() % 2) //instart garbage { for (int j=0;j<clen;j++) rez.insert(pos,1,random() % 255); } else { for (int j=0;j<clen && ((j+pos) < (int)rez.size());j++) rez.at( j + pos )=random()%255; } }}// //-----READ PART-------// string sipb_bnftools::read_qstr(bnf_parsval* val){ vector<bnf_parsval*> v; val->get("quoted-string-data",v); if (v.size()!=1) logic_error("Error in sipb_bnftools::read_qstr"); return v[0]->get_data();}// //-------FULLSET PART-------// void sipb_bnftools ::fullset_optval(bnf_parser&pcr, string servhost_name,int sport, string username,string userhost_name, int max_contlen,bool rand_set_cont){ sipb_bnftools::set_rulebydef(pcr); sipb_bnftools::set_request(pcr,random() % (max_contlen + 1),"", "Contact Expires",rand_set_cont); if (random()%2) //may be to host and maxforwards>0 { sipb_bnftools::set_reqline(pcr, "OPTIONS", servhost_name,"", sport); sipb_bnftools::set_maxfor(pcr,70); } else //may be to any and maxforwards=0 { sipb_bnftools::set_randhost_reqline(pcr,"OPTIONS"); sipb_bnftools::set_maxfor(pcr,0); } sipb_bnftools::set_to_nottag(pcr, servhost_name,"",sport); sipb_bnftools::set_cseq(pcr,"OPTIONS"); sipb_bnftools::set_from(pcr,userhost_name,username,1 + random()%40000);}// //-----SET PART----------// void sipb_bnftools::set_resetrules(bnf_parser&p, string rulesl,char sep){ vector<string> rl; vec_add(rulesl,rl,sep); for (unsigned int i=0;i<rl.size();i++) p.reset_rule(rl[i]);}// void sipb_bnftools::set_rulebydef(bnf_parser&p){ //TODO: make it more GENERAL //Accept p.reset_rule("Accept","\"Accept\" HCOLON " + hstr("application/sdp") + " [ SEMI "+hstr("level = 1")+"]" ); p.reset_rule("Accept-Encoding","\"Accept-Encoding\" HCOLON "+hstr("gzip")); p.reset_rule("Accept-Language","\"Accept-Language\" HCOLON "+hstr("en")); p.reset_rule("Alert-Info",string("\"Alert-Info\" HCOLON ") + hstr("<http://www.example.com/sound/moo.wav>")); p.reset_rule("Authentication-Info", string("\"Authentication-Info\"")+ "HCOLON nextnonce [COMMA nonce-count]"); p.reset_rule("Call-Info",string("\"Call-Info\" HCOLON ")+ hstr("<http://www.example.com/alice/photo.jpg> ;purpose=icon,")+ hstr("<http://www.example.com/alice/> ;purpose=info")); p.reset_rule("Allow",string("\"Allow\" HCOLON INVITEm COMMA ") + " ACKm COMMA OPTIONSm COMMA BYEm COMMA CANCELm COMMA "+ " REGISTERm [ COMMA " + hstr("SOME")+" ]" ); //TODO: set random contact param p.reset_rule("Contact",string("(\"Contact\" / \"m\" ) HCOLON ")+ hstr("<sip:seger@127.0.0.1>;q=60")); p.reset_rule("Error-Info","\"Error-Info\" HCOLON " + hstr("<sip:oblom@oblom.ru>")); p.reset_rule("Content-Encoding",string("( \"Content-Encoding \" / \"e\" )")+ " HCOLON "+ hstr("gzip")); p.reset_rule("Content-Language","\"Content-Language\" HCOLON " + hstr("ru")); p.reset_rule("Content-Length","( \"Content-Length\" / \"l\" ) HCOLON \"0\""); p.reset_rule("Content-Type","( \"Content-Type\" / \"c\" ) HCOLON "+ hstr("application/sdp")); //List of good headers (not need to change look to sipb_bnfrules_forcreate) //Good - Expires Min-Expires MIME-Version Organization //Good - Priority Server Subject Timestamp //Good - Unsupported p.reset_rule("Retry-After",string("\"Retry-After\" ")+ " HCOLON delta-seconds [ comment ] [ SEMI retry-param ]"); p.reset_rule("retry-param","\"duration\" EQUAL delta-seconds"); p.reset_rule("comment","LPAREN *( ctext / quoted-pair ) RPAREN"); //set light comment p.reset_rule("Warning",string("\"Warning\" ")+ "HCOLON warning-value [COMMA warning-value]"); p.reset_rule("warn-agent"," ( hostname / IPv4address ) [ \":\" port ] / pseudonym "); //for extension-header p.reset_rule("header-name",hstr("SOMEONE"));}// void sipb_bnftools::set_message(bnf_parser&p ,string rule_name, string first_rule,vector<string>& nec, vector<string>&opt,int c_line){ //Remove all nec from opt vector<string>::iterator it; for (unsigned int i=0;i<nec.size();i++) { it=find(opt.begin(),opt.end(),nec[i]); if (it!=opt.end()) opt.erase(it); } //Remove duplicate nec-nec and opt-opt vector<string> tmp=nec; sort(tmp.begin(),tmp.end()); nec.resize(0); unique_copy(tmp.begin(),tmp.end(),back_inserter(nec)); tmp=opt; sort(tmp.begin(),tmp.end()); opt.resize(0); unique_copy(tmp.begin(),tmp.end(),back_inserter(opt)); //Check Duplicat //in nec (nec-nec nec-opt) //TODO: remove this part (we remove all duplicat before). for (unsigned int i=0;i<nec.size();i++) { it=find(nec.begin() + i + 1,nec.end(),nec[i]); if (it!=nec.end()) throw logic_error("Error in sipb_bnftools::set_message duplicat nec-nec"); it=find(opt.begin(),opt.end(),nec[i]); if (it!=opt.end()) throw logic_error("Error in sipb_bnftools::set_message duplicat nec-opt"); } //in opt (opt-opt) for (unsigned int i=0;i<opt.size();i++) { it=find(opt.begin() + i + 1,opt.end(),opt[i]); if (it!=opt.end()) throw logic_error("Error in sipb_bnftools::set_message duplicat opt-opt"); } //general part string rul; int n; rul+=first_rule+" "; while (nec.size() > 0 || opt.size() > 0) { if (random() % 2) { if (nec.size()>0) { n=random() % nec.size(); rul+=nec.at(n) + " CRLF "; nec.erase(nec.begin() + n); } } else { if (opt.size()>0) { n=random() % opt.size(); rul+="[ "+opt.at(n) + " CRLF ] "; opt.erase(opt.begin() + n); } } } rul+=" CRLF "; rul+=hstr(nalpha(c_line)); p.reset_rule(rule_name,rul);}// void sipb_bnftools::set_message(bnf_parser&p,string rule_name, string first_rule,int c_len,string nec_add, string opt_add,bool rand_cont){ vector<string> nec; vector<string> opt; vec_add(nec_add,nec); vec_add(opt_add,opt); vec_add("Via To From CSeq Call-ID Max-Forwards",nec); vec_add("Accept Alert-Info Allow Authentication-Info Call-Info",opt); vec_add("Date Error-Info Min-Expires MIME-Version",opt); vec_add("Organization Priority Retry-After Server Subject Timestamp",opt); vec_add("Unsupported User-Agent Warning extension-header",opt); //content vec_add("Content-Disposition Content-Encoding",opt); vec_add("Content-Language Content-Type",opt); if ( random() % 2 || !rand_cont) //may add content/ { vec_add("Content-Length",nec); set_message(p,rule_name,first_rule,nec,opt,c_len); } else { c_len=0; vec_add("Content-Length",opt); set_message(p,rule_name,first_rule,nec,opt,0); } p.reset_rule("Content-Length","( \"Content-Length \" / \"l\" )" + string(" HCOLON ") + str(c_len));}void sipb_bnftools::set_request(bnf_parser&p,int c_len,string nec_add, string opt_add,bool rand_cont){ set_message(p,"Request","Request-Line",c_len,nec_add,opt_add,rand_cont);}// void sipb_bnftools::set_reqline(bnf_parser& p,string method,string host, string username, int to_port){ p.reset_rule("Request-Line",hstr(method)+" SP Request-URI SP" + "\"SIP/2.0\"" + " CRLF "); //Create rule SIP-URI-Request set_sip_pr(p,"Request",host,username,to_port); p.reset_rule("Request-URI"," SIP-URI-Request ");}// void sipb_bnftools::set_randhost_reqline(bnf_parser&p,string method){ p.reset_rule("Request-Line",hstr(method)+" SP Request-URI SP " + "\"SIP/2.0\"" + " CRLF "); p.add_rule("SIP-URI-Request",str("sip:")+ "[user \"@\"] (hostname / IPv4address) [ \":\" port ]"); p.reset_rule("Request-URI","SIP-URI-Request");}// void sipb_bnftools::set_via(bnf_parser& p ,string host, string tr,int port){ p.reset_rule("Via","( \"Via\" / \"v\" ) HCOLON via-parm"); p.reset_rule("via-parm",string("sent-protocol")+" LWS sent-by " + " [ SEMI via-ttl ] [ SEMI via-branch ]"); p.reset_rule("sent-protocol",str("SIP") + " SLASH \"2.0\" SLASH " + str(tr)); p.reset_rule("sent-by",hstr(host) + " COLON " + str(port));}// void sipb_bnftools::set_to_nottag(bnf_parser&p, string host,string username, int to_port){ //create new rules p.add_rule("display-name-To"," 1*(token LWS) / quoted-string "); set_sip_pr(p, "To", host, username, to_port); p.add_rule("addr-spec-To","SIP-URI-To"); p.add_rule("name-addr-To","[ display-name-To ] LAQUOT addr-spec-To RAQUOT"); //create general rule p.reset_rule("To", string("( \"To\" / \"t\" )") +" HCOLON "+ " ( name-addr-To/ addr-spec-To ) ");}// void sipb_bnftools::set_to_withtag(bnf_parser&p, string host,string username,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -