#include #include #include #include #include #include "fdpass.h" #include #include using namespace std; static void test() { REQUEST req; { req.add ("name","jacques"); req.add ("age",25); req.add ("text","allo\ncomment\nca\nva"); vector vals={"v1","v2","v3"}; req.add ("v",vals); req.complete(); } req.dump(); REQUEST req2; unsigned len = req.getlength(); const char *buf = req.getbuf(); unsigned offset = 0; while (offset < len){ unsigned size = 10; if (size > len - offset){ size = len - offset; } char tmp[size+1]; memcpy (tmp,buf+offset,size); tmp[size] = '\0'; offset += size; int ok = req2.addpart(tmp); if (ok == -1){ printf ("addpart ok=-1\n"); break; }else if (ok == 1){ printf ("addpart ok=1 offset=%u len=%u\n",offset,len); break; } } printf ("----dump\n"); req2.dump(); const char *val; int age; const char *text; vector vals; if (req2.getarg("name",val)!=-1 && req2.getarg("age",age)!=-1 && req2.getarg("text",text)!=-1 && req2.getarg("v",vals)!=-1){ printf ("----getarg\n"); printf ("name=%s\n",val); printf ("age=%d\n",age); printf ("text=%s\n",text); printf ("v="); for (auto &x:vals) printf ("%s,",x.c_str()); printf ("\n"); } } struct CLIENTINFO: public ARRAY_OBJ{ REQUEST_INFO req; CLIENTINFO(){ } }; static void test_send (int handle, bool verbose, const char *secret) { REQUEST req; bool dosign = strcmp(secret,"none")!=0; if (dosign) req.addsign(); req.add ("name","jacques"); req.add ("age",25); req.add ("text","ceci est\n" "un long texte\n\n" "qui ne fini\n" "pas"); vector vals; for (int i=0; i<10; i++){ char tmp[100]; snprintf (tmp,sizeof(tmp),"value-%d",i); vals.push_back(tmp); } req.add ("vals",vals); req.complete(); if (dosign) req.sign(secret); req.send(handle); if (verbose) printf ("send len=%lu\n",strlen(req.getbuf())); } #define _TLMP_trli_sessiond_client_getsessioninfo struct _F_trli_sessiond_client_getsessioninfo{ #define _F_trli_sessiond_client_getsessioninfo_ok(x) void x ok( bool success, const char * email, unsigned lang, bool admin,const vector &interest) virtual _F_trli_sessiond_client_getsessioninfo_ok( )=0; }; static void trli_sessiond_client_getsessioninfo (_F_trli_sessiond_client_getsessioninfo &c, CONNECT_INFO &con, const char * sessionid) { REQUEST req; con.reset(req); req.add("_func_","getsessioninfo"); req.add("sessionid",sessionid); if (con.send(req) != -1){ if (con.receive(req) != -1){ bool success=bool(); const char *email=NULL; unsigned lang=unsigned(); bool admin=bool(); vector interest; if (req.getarg("success",success)!=-1 && req.getarg("email",email)!=-1 && req.getarg("lang",lang)!=-1 && req.getarg("admin",admin)!=-1 && req.getarg("interest",interest)!=-1){ c.ok(success,email,lang,admin,interest); }else{ tlmp_error ("Protocol error\n"); } } } } #define _TLMP_trli_sessiond_client struct _F_trli_sessiond_client{ _F_TCPSERVER_V1 *cc; #define _F_trli_sessiond_client_getsessioninfo(x) void x getsessioninfo(bool &endserver, bool &endclient, const char *line,const char * sessionid,int no,const char *host) virtual _F_trli_sessiond_client_getsessioninfo( )=0; void rep_getsessioninfo (bool success, const char * email, unsigned lang, bool admin, const vector &interest); #define _F_trli_sessiond_client_invalid(x) void x invalid(bool &endserver, bool &endclient, const char *line,int no,const char *host) virtual _F_trli_sessiond_client_invalid( )=0; }; void _F_trli_sessiond_client::rep_getsessioninfo (bool success, const char * email, unsigned lang, bool admin, const vector &interest) { REQUEST req; req.add("success",success); req.add("email",email); req.add("lang",lang); req.add("admin",admin); req.add("interest",interest); req.send (cc); } static void trli_sessiond_client(_F_trli_sessiond_client &_c, _F_TCPSERVER_V1 *_cc, REQUEST_INFO &req, const char *_line, bool &_endserver, bool &_endclient,int no,const char *host) { int ok = req.addpart(_line,strlen(_line)); if (ok == -1){ _c.invalid (_endserver,_endclient,"",no,host); _endclient = true; }else if (ok == 1){ _c.cc = _cc; const char *_func; if (req.intruder){ // Ignore everything from an intruder }else if (req.getarg("_func_",_func)==-1){ _c.invalid (_endserver,_endclient,"no func in protocol",no,host); _endclient = true; }else{ if(strcmp(_func,"getsessioninfo")==0){ const char *sessionid; if (req.getarg("sessionid",sessionid)==-1){ _c.invalid (_endserver,_endclient,"no sessionid",no,host); }else{ _c.getsessioninfo(_endserver,_endclient,_line,sessionid,no,host); } }else{ _c.invalid (_endserver,_endclient,"unknown function",no,host); } } req.reset(); } } int main (int argc, char *argv[]) { glocal int ret = -1; glocal bool test = false; glocal bool client = false; glocal bool clientreq = false; glocal bool server = false; glocal bool serverreq = false; glocal bool verbose = false; glocal int nbreq = 100000; glocal const char *secret = "secret"; glocal.ret = (argc,argv); setarg (' ',"test","static test",glocal.test,false); setarg (' ',"client","network client",glocal.client,false); setarg (' ',"clientreq","network client using REQUEST",glocal.clientreq,false); setarg (' ',"server","network server",glocal.server,false); setarg (' ',"serverreq","network server using REQUEST",glocal.serverreq,false); setarg ('n',"nbreq","nb request for client",glocal.nbreq,false); setarg ('v',"verbose","show network messages",glocal.verbose,false); setarg ('s',"secret","secret",glocal.secret,false); if (glocal.test){ test(); char out1[SHA256_DIGEST_LENGTH*2+1],out2[SHA256_DIGEST_LENGTH*2+1]; fdpass_shasum ("allototo",out1); fdpass_shasum ("allo",4,"toto",4,out2); printf ("SHAL=%d\nout1=%s\nout2=%s\n",SHA256_DIGEST_LENGTH*2,out1,out2); }else if (glocal.client){ glocal int nb=0; ("localhost","9900",10); settcpnodelay(true); setrawmode (true); test_send (info.handle,glocal.verbose,glocal.secret); if (glocal.verbose) printf ("len=%d nb=%d\n",info.linelen,glocal.nb); glocal.nb++; if (glocal.nb > glocal.nbreq){ end = true; }else{ test_send (info.handle,glocal.verbose,glocal.secret); } printf ("nb=%d\n",glocal.nb); }else if (glocal.server){ (9900,5); settcpnodelay(true); info.data = new CLIENTINFO(); if (glocal.verbose) printf ("receive len=%d strlen %lu\n",info.linelen,strlen(line)); CLIENTINFO *c = (CLIENTINFO*)info.data; int ok = c->req.addpart(line,strlen(line)); if (ok == -1){ printf ("Invalid\n"); endclient = true; }else if (ok == 1){ if (strcmp(glocal.secret,"none")!=0 && c->req.req.checksign(glocal.secret)==-1){ tlmp_error ("Sign fail\n"); endclient = true; }else{ if (glocal.verbose) c->req.req.dump(); send ("OK\n"); } c->req.reset(); }else{ printf ("ok=%d\n",ok); } if (o.is_ok()){ o.setrawmode(true); o.loop(); } }else if (glocal.clientreq){ ("localhost","9900",10); settcpnodelay(true); setrawmode (true); CONNECT_INFO con; con.fd = info.handle; for (int i=0; i (con,tmp); if (glocal.verbose){ printf ("success=%d email=%s lang=%u admin=%d\n",success,email,lang,admin); for (auto x:interest) printf ("interest %s\n",x); } } end = true; }else if (glocal.serverreq){ glocal vector interest; for (int i=0; i<20; i++){ char tmp[100]; snprintf (tmp,sizeof(tmp)-1,"this is interest number %d",i); glocal.interest.push_back (tmp); } (9900,5); settcpnodelay(true); CLIENTINFO *c = new CLIENTINFO(); info.data = c; if (glocal.verbose) printf ("receive len=%d strlen %lu\n",info.linelen,strlen(line)); CLIENTINFO *c = (CLIENTINFO*)info.data; (this, c->req,line,endserver,endclient,no,"host"); if (glocal.verbose) printf ("sessionid=%s\n",sessionid); char tmp[100]; snprintf (tmp,sizeof(tmp)-1,"This is the email\n%s@foo.com\nand the telephone\n111-2222\n",sessionid); rep_getsessioninfo (true,tmp,0,true,glocal.interest); printf ("invalid %s\n",line); if (o.is_ok()){ o.setrawmode(true); o.loop(); } }else{ usage(); } return 0; return glocal.ret; }