From a7457281e51e37f08fd586be2b9f0caabacef2b4 Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Fri, 4 Dec 2020 05:25:10 +0000 Subject: [PATCH] add comment thread safe --- server.c | 28 +++++++++++++++++++--------- socket_wrapper.c | 7 +++++-- socket_wrapper.h | 16 +++++++++++----- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/server.c b/server.c index f0dddb9..1f9e165 100644 --- a/server.c +++ b/server.c @@ -17,8 +17,11 @@ /*======== *Operation *========*/ -/*send user error message -80 character limit + +/** + * send user error message + * 80 character limit + * thread safe */ int send_fail(int sock,const char * msg){ struct TransferResult res; @@ -37,7 +40,10 @@ int send_fail(int sock,const char * msg){ } return 0; } - +/** + * send errno to client + * thread safe + */ int send_errno(int sock){ struct TransferResult r; r.res = RES_ERR; @@ -50,8 +56,9 @@ int send_errno(int sock){ } return 0; } -/* return fd, if success. -otherwise, return -1. +/** + * return fd, if success. otherwise, return -1. + * thread safe */ int read_request(int sock,uint8_t * buf,size_t bufsize){ struct ReadOp p; @@ -62,7 +69,7 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){ else perror("receive fail"); return -1; } - if(bufsize <= p.file_url_size + sizeof(p) + 1){ + if(bufsize <= ((size_t)p.file_url_size) + sizeof(p) + 1){ send_fail(sock,"buffer overflow"); return -1; } @@ -75,8 +82,8 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){ return -1; } n = recv_until_byte(sock,buf,p.file_url_size,TIMEOUT); - buf[p.file_url_size] = '\0'; - printf("str size: %d, request %s\n",p.file_url_size,buf); + buf[p.file_url_size] = '\0'; //truncate + fprintf(stdout,"str size: %d, request %s\n",p.file_url_size,buf); if(strchr((char *)buf,'/') != NULL){ send_fail(sock,"Illegal character /"); return -1; @@ -89,7 +96,10 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){ } return fd; } - +/** + * send response to client + * thread safe +*/ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){ struct TransferResult r; struct stat st; diff --git a/socket_wrapper.c b/socket_wrapper.c index fc7d19c..dab2d4f 100644 --- a/socket_wrapper.c +++ b/socket_wrapper.c @@ -11,6 +11,7 @@ int getBufferSizeFrom(int sock){ int buffer_sz; socklen_t len = sizeof(buffer_sz); + //getsockopt is thread-safe if(getsockopt(sock,SOL_SOCKET,SO_SNDBUF,&buffer_sz,&len) < 0){ perror("failed to get sock buffer size: getsockopt"); buffer_sz = DEFAULT_BUF_SIZE;/*set to default*/ @@ -37,11 +38,13 @@ ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout) case POLLNVAL: return -1; case POLL_IN: - return recv(fd,buf,n,0); + ret = recv(fd,buf,n,0); + assert(ret != 0); + return ret; default: assert(0 && "unreachable"); } - return ret; + assert(0 && "unreachable"); } ssize_t recv_until_byte(int fd,void * buf, size_t n,int timeout){ diff --git a/socket_wrapper.h b/socket_wrapper.h index 9a2fdac..7b41c2b 100644 --- a/socket_wrapper.h +++ b/socket_wrapper.h @@ -41,16 +41,22 @@ struct TransferResult{ #endif +/** + * find buffer size from sock + * thread safe +*/ int getBufferSizeFrom(int sock); -/* -return -2 if timeout occur. -otherwise, implement equal to 'recv'. -`timeout` unit is second. +/** + * return -2 if timeout occur. + * otherwise, implement equal to 'recv'. + * `timeout` unit is second. + * thread safe */ ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout); /* -recieve data to buf until all bytes. + * recieve data to buf until all bytes. + * thread safe */ ssize_t recv_until_byte(int fd,void * buf, size_t n,int timeout);