2020-12-04 16:48:03 +09:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
2020-12-04 20:11:35 +09:00
|
|
|
#include <stdbool.h>
|
2020-12-04 16:48:03 +09:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
2020-12-04 20:11:35 +09:00
|
|
|
|
2020-12-04 16:48:03 +09:00
|
|
|
#include "socket_wrapper.h"
|
|
|
|
|
|
|
|
#ifdef USE_SENDFILE
|
|
|
|
#include <sys/sendfile.h>
|
2020-12-04 17:33:41 +09:00
|
|
|
#ifndef DEFAULT_SEND_FILE_CHUNK_SIZE
|
2020-12-04 16:48:03 +09:00
|
|
|
const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/
|
2020-12-04 17:33:41 +09:00
|
|
|
#else
|
|
|
|
const size_t SEND_FILE_CHUNK_SIZE = DEFAULT_SEND_FILE_CHUNK_SIZE; /*1MB*/
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifndef DEFAULT_TIMEOUT
|
|
|
|
static const int TIMEOUT = 5;
|
|
|
|
#else
|
|
|
|
static const int TIMEOUT = DEFAULT_TIMEOUT;
|
2020-12-04 16:48:03 +09:00
|
|
|
#endif
|
2020-12-05 10:25:54 +09:00
|
|
|
#ifndef DEFAULT_PROGRESS_BAR_WIDTH
|
2020-12-04 20:11:35 +09:00
|
|
|
static const int PROGRESS_BAR_WIDTH = 30;
|
2020-12-05 10:25:54 +09:00
|
|
|
#else
|
|
|
|
static const int PROGRESS_BAR_WIDTH = DEFAULT_PROGRESS_BAR_WIDTH;
|
|
|
|
#endif
|
2020-12-04 16:48:03 +09:00
|
|
|
/*========
|
|
|
|
*Operation
|
|
|
|
*========*/
|
|
|
|
|
|
|
|
int sendReadOp(int sock,const char * filename){
|
|
|
|
struct ReadOp op;
|
|
|
|
op.file_url_size = strlen(filename);
|
|
|
|
op.padding0 = 0;
|
|
|
|
if(send(sock,&op,sizeof(op),0)<0){
|
|
|
|
perror("readop send fail");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-12-04 19:10:40 +09:00
|
|
|
#ifdef SLOW_CLIENT
|
|
|
|
sleep(SLOW_CLIENT);
|
|
|
|
#endif
|
2020-12-04 16:48:03 +09:00
|
|
|
if(send(sock,filename,op.file_url_size,0)<0){
|
|
|
|
perror("readop filename send fail");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-04 20:11:35 +09:00
|
|
|
/**
|
|
|
|
* @arg cur_progress : it is percentage.
|
|
|
|
*/
|
|
|
|
bool isProgressBarNeedUpdate(size_t offset,size_t total,double cur_progress){
|
|
|
|
return ((double)offset / (double)total) > (cur_progress / 100.0);
|
|
|
|
}
|
|
|
|
void DisplayProgressBar(size_t offset,size_t total,double cur_progress){
|
|
|
|
char buf[PROGRESS_BAR_WIDTH];
|
|
|
|
size_t i;
|
|
|
|
size_t cur_pos = (cur_progress / 100.0 * PROGRESS_BAR_WIDTH); //must be less than SIZE_MAX. other value is undefined behavior.
|
|
|
|
for (i = 0; i < PROGRESS_BAR_WIDTH; i++)
|
|
|
|
{
|
|
|
|
if (i < cur_pos)
|
|
|
|
buf[i] = '=';
|
|
|
|
else if(i == cur_pos)
|
|
|
|
buf[i] = '>';
|
2020-12-05 10:25:54 +09:00
|
|
|
else buf[i] = '.';
|
2020-12-04 20:11:35 +09:00
|
|
|
}
|
|
|
|
printf("\r[%s]: %.2f%% bytes: %ld/%ld bytes",buf,cur_progress,total,offset);
|
|
|
|
}
|
|
|
|
void DisplayProgressBar100Percent(size_t total){
|
|
|
|
size_t i;
|
|
|
|
char buf[PROGRESS_BAR_WIDTH];
|
|
|
|
for (i = 0; i < PROGRESS_BAR_WIDTH; i++){
|
|
|
|
buf[i] = '=';
|
|
|
|
}
|
|
|
|
printf("\r[%s]: 100%% bytes: %ld/%ld bytes\n",buf,total,total);
|
|
|
|
}
|
2020-12-04 16:48:03 +09:00
|
|
|
int recvFile(int sock, const char * filename,size_t file_size){
|
|
|
|
int fd;
|
|
|
|
size_t count = 0;
|
|
|
|
int i;
|
2020-12-04 20:11:35 +09:00
|
|
|
double cur_progress = 1;
|
2020-12-04 16:48:03 +09:00
|
|
|
int return_value = 0;
|
|
|
|
int buf_sz = getBufferSizeFrom(sock);
|
|
|
|
uint8_t * buf = malloc(buf_sz*sizeof(*buf));
|
|
|
|
if (buf == NULL){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRWXG|S_IROTH);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
perror("file open fail");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (file_size - count > 0)
|
|
|
|
{
|
|
|
|
int readed = buf_sz < file_size - count ? buf_sz : file_size - count;
|
|
|
|
if((i = recv_until_byte(sock,buf,readed,TIMEOUT)) < 0){
|
|
|
|
if(i == -2)
|
|
|
|
fprintf(stderr,"recv file failed : timeout connetion lost\n");
|
|
|
|
else perror("recv file failed");
|
|
|
|
return_value = -1;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
if(write(fd,buf,readed)<0){
|
|
|
|
perror("file write failed");
|
|
|
|
return_value = -1;
|
|
|
|
goto END;
|
|
|
|
}
|
2020-12-04 20:11:35 +09:00
|
|
|
if( isProgressBarNeedUpdate(count,file_size,cur_progress) ){
|
|
|
|
DisplayProgressBar(count,file_size,cur_progress);
|
2020-12-04 16:48:03 +09:00
|
|
|
cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
count += readed;
|
|
|
|
}
|
2020-12-04 20:11:35 +09:00
|
|
|
DisplayProgressBar100Percent(file_size);
|
2020-12-04 16:48:03 +09:00
|
|
|
END:
|
|
|
|
free(buf);
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int recvData(int sock,const char * filename){
|
|
|
|
struct TransferResult res;
|
|
|
|
int i=0;
|
|
|
|
if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){
|
|
|
|
if (i == -2) fprintf(stderr,"timeout");
|
|
|
|
else perror("recv fail");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
static char error_meesage_buf[80] = "";
|
|
|
|
switch(res.res){
|
|
|
|
case RES_ERR:
|
|
|
|
fprintf(stderr,"Server Fail: %s", strerror(res.err_number));
|
|
|
|
return -1;
|
|
|
|
case RES_USR_ERR:
|
|
|
|
assert(res.error_msg_size < 80);/*todo : fix*/
|
|
|
|
if((i=recv_until_byte(sock,error_meesage_buf,res.error_msg_size,TIMEOUT)) < 0){
|
|
|
|
if (i == -2) fprintf(stderr,"timeout");
|
|
|
|
else perror("recv fail");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fprintf(stderr,"Error Message From Server: %s",error_meesage_buf);
|
|
|
|
return -1;
|
|
|
|
case RES_OK:
|
|
|
|
return recvFile(sock,filename,res.file_size);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr,"unknown value!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char *argv[]){
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
const char * filename;
|
|
|
|
const char * server_name;
|
2020-12-04 17:33:41 +09:00
|
|
|
in_port_t server_port = 0;
|
2020-12-04 16:48:03 +09:00
|
|
|
int sock, err;
|
|
|
|
if (argc != 4){
|
2020-12-04 19:24:26 +09:00
|
|
|
fprintf(stderr,"USAUE: %s SERVERNAME PORT FILENAME\n",argv[0]);
|
2020-12-04 16:48:03 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
server_name = argv[1];
|
|
|
|
server_port = atoi(argv[2]);
|
|
|
|
filename = argv[3];
|
|
|
|
if (server_port == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"port invalid");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
sock = socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
if(sock < 0){
|
|
|
|
perror("sock create fail");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,server_name,(struct sockaddr *)&addr);
|
|
|
|
if (err != 0){
|
|
|
|
int check;
|
|
|
|
fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
|
|
|
|
//assume that sernmae is *.*.*.* and try to parse addr
|
|
|
|
check = inet_pton(AF_INET,server_name,&addr.sin_addr);
|
|
|
|
assert(check != -1);
|
|
|
|
if (check == 0){
|
|
|
|
fprintf(stderr,"parsing fail : invaild format\n");
|
|
|
|
close(sock);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(server_port);
|
|
|
|
if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)) < 0){
|
|
|
|
perror("connect failed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if(sendReadOp(sock,filename) == 0){
|
|
|
|
int ret = recvData(sock,filename);
|
|
|
|
close(sock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
close(sock);
|
|
|
|
return 0;
|
|
|
|
}
|