mirror of
https://github.com/berlin-open-wireless-lab/DAWN.git
synced 2025-03-09 15:40:12 +00:00
tcpsocket: ustream_pending_data before ustream_read
according to https://github.com/yousong/osocks/blob/master/main.c#L149 and ustream source code, directly ustream_read would make many packge incomplete.
This commit is contained in:
parent
b49c5b8552
commit
a6d0ea3d1d
1 changed files with 10 additions and 8 deletions
|
@ -86,13 +86,14 @@ static void client_read_cb(struct ustream *s, int bytes) {
|
||||||
str = malloc(final_len);
|
str = malloc(final_len);
|
||||||
if (!str) {
|
if (!str) {
|
||||||
fprintf(stderr,"not enough memory\n");
|
fprintf(stderr,"not enough memory\n");
|
||||||
goto memory_full;
|
goto nofree;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((len = ustream_read(s, str, final_len)) < final_len){//ensure recv sizeof(uint32_t).
|
if ((len = ustream_pending_data(s, false)) < final_len){//ensure recv sizeof(uint32_t).
|
||||||
fprintf(stderr,"not complete msg, len:%d, expected len:%u\n", len, final_len);
|
fprintf(stdout,"not complete msg, len:%d, expected len:%u\n", len, final_len);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
ustream_read(s, str, final_len);
|
||||||
|
|
||||||
final_len = ntohl(*(uint32_t *)str) - sizeof(uint32_t);//the final_len in headder includes header itself
|
final_len = ntohl(*(uint32_t *)str) - sizeof(uint32_t);//the final_len in headder includes header itself
|
||||||
str_tmp = realloc(str, final_len);
|
str_tmp = realloc(str, final_len);
|
||||||
|
@ -102,11 +103,12 @@ static void client_read_cb(struct ustream *s, int bytes) {
|
||||||
//and may need to be deallocated with free() or realloc().
|
//and may need to be deallocated with free() or realloc().
|
||||||
}
|
}
|
||||||
str = str_tmp;
|
str = str_tmp;
|
||||||
if ((len = ustream_read(s, str, final_len)) < final_len) {//ensure recv final_len bytes.
|
|
||||||
fprintf(stderr,"not complete msg, len:%d, expected len:%u\n", len, final_len);
|
if ((len = ustream_pending_data(s, false)) < final_len){//ensure recv final_len bytes.
|
||||||
|
fprintf(stdout,"not complete msg, len:%d, expected len:%u\n", len, final_len);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
ustream_read(s, str, final_len);
|
||||||
if (network_config.use_symm_enc) {
|
if (network_config.use_symm_enc) {
|
||||||
char *dec = gcrypt_decrypt_msg(str, final_len);//len of str is final_len
|
char *dec = gcrypt_decrypt_msg(str, final_len);//len of str is final_len
|
||||||
if (!dec) {
|
if (!dec) {
|
||||||
|
@ -120,7 +122,7 @@ static void client_read_cb(struct ustream *s, int bytes) {
|
||||||
}
|
}
|
||||||
out:
|
out:
|
||||||
free(str);
|
free(str);
|
||||||
memory_full:
|
nofree:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue