c++ - WSASend Buffer to String -


how can winsock2 wsasend() buffer string?

this code have, , write lot of icharacters.

int winapi hook_wsasend(socket a0, lpwsabuf a1, dword a2, lpdword a3, dword a4, lpwsaoverlapped a5, lpwsaoverlapped_completion_routine a6) {     int rv = 0;     char * buf = "";     wsabuf * wb = a1;      for(int = 0; == a2; i++){         strcpy_s(buf, wb[i].len, wb[i].buf);     }      fopen_s(&pwsasendlogfile, "c:\\wsasendlog.txt", "a+");     fprintf(pwsasendlogfile, "%s\n", buf);     fclose(pwsasendlogfile);     rv = real_wsasend(a0,a1,a2,a3,a4,a5,a6);     return rv; } 

as remy lebeau asked, i'm adding more info on need achieve.

i need have buffer inside string because:

  • i have search specific string inside buffer, before doing string must start <talkmsg.

  • then, have send buffer trough namedpipe, have functions handling that.

just explain better i'm doing, code have winsock send(). have same thing wsasend().

int winapi hook_send(socket s, const char* buf, int len, int flags) {     /*     fopen_s(&psendlogfile, "c:\\sendlog.txt", "a+");     fprintf(psendlogfile, "%s\n", buf);     fclose(psendlogfile);     */     cursocket = s;     if(filtering){         pipeheader ph;         string p(buf);         if(p.find("<talkmsg") == 0){             ph.command = 5;             ph.sockid = s;             ph.datasize = len;             if(sendpipeheader((char*)&ph, sizeof(ph))){                 if(sendpipedata(buf, len)){                     return len;                 }             }         }     }      return real_send(s, buf, len, flags); } 

as @enhzflep said in comments, not managing buf variable correctly. nor need @ all. write source buffers as-is directly file:

int winapi hook_wsasend(socket a0, lpwsabuf a1, dword a2, lpdword a3, dword a4, lpwsaoverlapped a5, lpwsaoverlapped_completion_routine a6) {     fopen_s(&pwsasendlogfile, "c:\\wsasendlog.txt", "a+");     for(dword = 0; < a2; i++)         fwrite(a1[i].buf, 1, a1[i].len, pwsasendlogfile);     fprintf(pwsasendlogfile, "\n");     fclose(pwsasendlogfile);      int rv = real_wsasend(a0,a1,a2,a3,a4,a5,a6);     return rv; } 

update: send() hook assuming buf null-terminated, not case. have use provided len when copying buf data `string:

int winapi hook_send(socket s, const char* buf, int len, int flags) {     /*     fopen_s(&psendlogfile, "c:\\sendlog.txt", "a+");     fwrite(buf, 1, len, psendlogfile);     fprintf(psendlogfile, "\n");     fclose(psendlogfile);     */     cursocket = s;     if(filtering){         pipeheader ph;         string p(buf, len); // <-- here         if(p.find("<talkmsg") == 0){             ph.command = 5;             ph.sockid = s;             ph.datasize = len;             if(sendpipeheader((char*)&ph, sizeof(ph))){                 if(sendpipedata(buf, len)){                     return len;                 }             }         }     }      return real_send(s, buf, len, flags); } 

you have similar in wsasend() hook, taking total length of wsabuf buffers account:

int winapi hook_wsasend(socket s, lpwsabuf lpbuffers, dword dwbuffercount, lpdword lpnumberofbytessent, dword dwflags, lpwsaoverlapped                    lpoverlapped, lpwsaoverlapped_completion_routine lpcompletionroutine) {     /*     fopen_s(&pwsasendlogfile, "c:\\wsasendlog.txt", "a+");     for(dword = 0; < dwbuffercount; i++)         fwrite(lpbuffers[i].buf, 1, lpbuffers[i].len, pwsasendlogfile);     fprintf(pwsasendlogfile, "\n");     fclose(pwsasendlogfile);     */     cursocket = s;     if(filtering){         pipeheader ph;         string p;         size_t len = 0;         for(dword = 0; < dwbuffercount; i++) {             len += lpbuffers[i].len;         }         p.reserve(len);         for(dword = 0; < dwbuffercount; i++) {             p.append(lpbuffers[i].buf, lpbuffers[i].len);         }         if(p.find("<talkmsg") == 0){             ph.command = 5;             ph.sockid = s;             ph.datasize = len;             if(sendpipeheader((char*)&ph, sizeof(ph))){                 if(sendpipedata(p.c_str(), len)){                     if (lpnumberofbytessent){                         *lpnumberofbytessent = len;                     }                     if (lpcompletionroutine) {                         lpcompletionroutine(0, len, lpoverlapped, 0);                     }                     return 0;                 }             }         }     }      return real_wsasend(s, lpbuffers, dwbuffercount, lpnumberofbytessent, dwflags, lpoverlapped, lpcompletionroutine); } 

that being said, kind of filtering code not reliable due streaming nature of tcp. there no guarantee input data represent complete message in single send when "<talkmsg" present, or "<talkmsg" complete in single send. need instead save outbound data as-is per-socket buffer, , parse buffer looking complete messages, passing complete messages pipe , removing them buffer, leaving partial data in buffer completed subsequent sends.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -