HW12/server.c

472 lines
12 KiB
C
Raw Normal View History

2020-12-04 08:06:26 +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 08:21:37 +09:00
#include <signal.h>
2020-12-04 08:06:26 +09:00
#include <assert.h>
#include <fcntl.h>
2020-12-05 10:25:54 +09:00
#include <pthread.h>
2020-12-04 08:06:26 +09:00
#include "socket_wrapper.h"
2020-12-05 10:25:54 +09:00
#include "simple_circular_buffer.h"
2020-12-07 00:26:59 +09:00
#include "timerhelper.h"
2020-12-04 08:06:26 +09:00
2020-12-05 10:25:54 +09:00
#ifndef DEFAULT_MAX_LISTEN_SOCKET
static const int MAX_LISTEN_SOCKET = 16;
#else
static const int MAX_LISTEN_SOCKET = DEFAULT_MAX_LISTEN_SOCKET;
#endif
2020-12-04 17:33:41 +09:00
#ifdef USE_SENDFILE
#include <sys/sendfile.h>
#ifndef DEFAULT_SEND_FILE_CHUNK_SIZE
const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/
#else
const size_t SEND_FILE_CHUNK_SIZE = DEFAULT_SEND_FILE_CHUNK_SIZE; /*1MB*/
#endif
#endif
#ifndef DEFAULT_SERVER_PORT
static const in_port_t SERVER_PORT = 9091;
#else
static const in_port_t SERVER_PORT = DEFAULT_SERVER_PORT;
#endif
#ifndef DEFAULT_MAX_PATH_SIZE
/*0 < x < MAX_PATH_SIZE*/
static const uint16_t MAX_PATH_SIZE = 256;
#else
static const uint16_t MAX_PATH_SIZE = DEFAULT_MAX_PATH_SIZE;
#endif
#ifndef DEFAULT_TIMEOUT
static const int TIMEOUT = 5;
#else
static const int TIMEOUT = DEFAULT_TIMEOUT;
#endif
2020-12-05 10:25:54 +09:00
enum{
#ifndef DEFAULT_WORK_QUEUE_SIZE
WORK_QUEUE_SIZE = 10,
#else
WORK_QUEUE_SIZE = DEFAULT_WORK_QUEUE_SIZE,
#endif
#ifndef DEFAULT_MAX_THREAD_NUMBER
MAX_THREAD_NUMBER = 10
#else
MAX_THREAD_NUMBER = DEFAULT_MAX_THREAD_NUMBER
#endif
};
2020-12-07 02:19:14 +09:00
#define USE_TRACE
#define USE_NO_QUEUE
2020-12-05 10:25:54 +09:00
2020-12-04 08:06:26 +09:00
/*========
*Operation
*========*/
2020-12-04 14:25:10 +09:00
/**
* send user error message
* 80 character limit
* thread safe
2020-12-04 08:21:37 +09:00
*/
int send_fail(int sock,const char * msg){
struct TransferResult res;
res.res = RES_USR_ERR;
res.file_size = 0;
res.err_number = 0;
res.error_msg_size = strlen(msg);
//os will be combining if tcp_autocorking emabled.
if(send(sock,&res,sizeof(res),0) < 0){
perror("error msg send");
2020-12-04 08:06:26 +09:00
return -1;
}
2020-12-04 08:21:37 +09:00
if (send(sock,msg,res.error_msg_size,0) < 0){
perror("error msg send");
2020-12-04 08:06:26 +09:00
return -1;
}
return 0;
}
2020-12-04 14:25:10 +09:00
/**
* send errno to client
* thread safe
*/
2020-12-04 08:21:37 +09:00
int send_errno(int sock){
struct TransferResult r;
r.res = RES_ERR;
r.err_number = errno;
r.file_size = 0;
r.error_msg_size = 0;
if(send(sock,&r,sizeof(r),0)){
perror("errno send");
return -1;
}
return 0;
}
2020-12-04 14:25:10 +09:00
/**
* return fd, if success. otherwise, return -1.
* thread safe
2020-12-04 08:21:37 +09:00
*/
int read_request(int sock,uint8_t * buf,size_t bufsize){
struct ReadOp p;
2020-12-04 08:06:26 +09:00
int fd;
2020-12-04 08:21:37 +09:00
ssize_t n = recv_until_byte(sock,&p,sizeof(p),TIMEOUT);
if (n < 0){
if (n == -2) fprintf(stderr,"timeout!");
else perror("receive fail");
2020-12-04 08:06:26 +09:00
return -1;
}
2020-12-04 14:25:10 +09:00
if(bufsize <= ((size_t)p.file_url_size) + sizeof(p) + 1){
2020-12-04 08:21:37 +09:00
send_fail(sock,"buffer overflow");
2020-12-04 08:06:26 +09:00
return -1;
}
2020-12-04 08:21:37 +09:00
else if(p.file_url_size + 1 > MAX_PATH_SIZE){
send_fail(sock,"max path fail");
return -1;
2020-12-04 08:06:26 +09:00
}
2020-12-04 08:21:37 +09:00
else if(p.file_url_size == 0){
send_fail(sock,"filename zero fail");
return -1;
}
n = recv_until_byte(sock,buf,p.file_url_size,TIMEOUT);
2020-12-04 14:25:10 +09:00
buf[p.file_url_size] = '\0'; //truncate
fprintf(stdout,"str size: %d, request %s\n",p.file_url_size,buf);
2020-12-04 08:21:37 +09:00
if(strchr((char *)buf,'/') != NULL){
send_fail(sock,"Illegal character /");
return -1;
}
fd = open((char *)buf,O_RDONLY);
if(fd < 0){
send_errno(sock);
close(fd);
return -1;
}
return fd;
2020-12-04 08:06:26 +09:00
}
2020-12-04 14:25:10 +09:00
/**
* send response to client
* thread safe
*/
2020-12-04 08:21:37 +09:00
int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
struct TransferResult r;
struct stat st;
off_t offset = 0;
ssize_t readed = 0;
r.res = RES_OK;
r.err_number = 0;
r.error_msg_size = 0;
if(fstat(fd,&st) < 0){
return send_errno(sock);
}
if(S_ISDIR(st.st_mode)){
return send_fail(sock,"is a directory");
}
r.file_size = st.st_size;
if(send(sock,&r,sizeof(r),0)<0){
perror("send fail");
2020-12-04 08:06:26 +09:00
return -1;
}
2020-12-04 08:21:37 +09:00
#ifdef USE_SENDFILE
while (r.file_size != offset)
{
size_t count = SEND_FILE_CHUNK_SIZE < (r.file_size - offset) ? SEND_FILE_CHUNK_SIZE : (r.file_size - offset);
if((readed = sendfile(sock,fd,&offset,count)) < 0){
perror("send file fail");
2020-12-04 08:06:26 +09:00
return -1;
2020-12-04 08:21:37 +09:00
}
}
#else
while (offset < r.file_size)
{
readed = bufsize < (r.file_size - offset) ? bufsize : r.file_size - offset;
if(read(fd,buf,readed)<0){
perror("send response read fail");
2020-12-04 08:06:26 +09:00
return -1;
2020-12-04 08:21:37 +09:00
}
if(send(sock,buf,readed,0)<0){
perror("send response send fail");
2020-12-04 08:06:26 +09:00
return -1;
2020-12-04 08:21:37 +09:00
}
offset += readed;
2020-12-04 08:06:26 +09:00
}
2020-12-04 08:21:37 +09:00
#endif
2020-12-04 08:06:26 +09:00
return 0;
}
2020-12-04 18:04:46 +09:00
const char * help(const char * n){
const char * msg = "USASE : %s [Option] ...\n"
"Options and arguments: \n"
"-p port\t:set to port binding. couldn't set to 0\n"
"-h\t:print help message.\n";
printf(msg,n);
return msg;
}
/** return 0 ok. otherwise invalid format*/
int parse_args(int argc,const char * argv[] , in_port_t * port){
int pos = 1;
const char * opt;
while (pos < argc)
{
opt = argv[pos++];
if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0)
{
help(argv[0]);
2020-12-06 03:48:49 +09:00
return 0;
2020-12-04 18:04:46 +09:00
}
else if(strcmp(opt,"-p") == 0 || strcmp(opt,"--port") == 0){
if (pos < argc){
const char * value = argv[pos++];
*port = atoi(value);
if (port == 0){ // either not number or zero
2020-12-06 03:48:49 +09:00
fprintf(stderr,"argument is either not number or zero\n");
2020-12-04 18:04:46 +09:00
return 2;
}
}
2020-12-06 03:48:49 +09:00
else{
fprintf(stderr,"need argument\n");
return 2; //failed to find argument.
}
2020-12-04 18:04:46 +09:00
}
}
return 0;
}
2020-12-05 10:25:54 +09:00
//============
//Simple Thread Pool
//============
2020-12-07 02:19:14 +09:00
#ifdef USE_TRACE
enum{
2020-12-07 19:05:19 +09:00
Trace_Timer_ID = CLOCK_REALTIME
2020-12-07 02:19:14 +09:00
};
#endif
#ifndef USE_NO_QUEUE
2020-12-05 10:25:54 +09:00
typedef struct SharedState{
//empty if less than 0
queue_struct(int,WORK_QUEUE_SIZE) socks;
2020-12-07 00:26:59 +09:00
#ifdef USE_TRACE
queue_struct(struct timespec,WORK_QUEUE_SIZE) trace_timer;
#endif
2020-12-05 10:25:54 +09:00
pthread_mutex_t sock_mutex;
pthread_cond_t ready;
//int progress[MAX_THREAD_NUMBER];
} shared_state_t;
void init_shared_state(shared_state_t * state) {
queue_init(&state->socks);
2020-12-07 00:26:59 +09:00
#ifdef USE_TRACE
queue_init(&state->trace_timer);
#endif
2020-12-05 10:25:54 +09:00
pthread_mutex_init(&state->sock_mutex,NULL);
pthread_cond_init(&state->ready,NULL);
}
2020-12-07 02:19:14 +09:00
#endif
2020-12-05 10:25:54 +09:00
//
typedef struct WorkerArgument
{
int id;
int bufsize;
uint8_t * buf;
2020-12-07 02:19:14 +09:00
#ifdef USE_NO_QUEUE
int csock;
#ifdef USE_TRACE
struct timespec ts;
#endif
#endif
2020-12-05 10:25:54 +09:00
} worker_argument_t;
2020-12-07 02:19:14 +09:00
__attribute_malloc__ worker_argument_t * create_worker_argument(int id, int bufsize
#ifdef USE_NO_QUEUE
, int csock
#endif
){
2020-12-05 10:25:54 +09:00
worker_argument_t * ret = (worker_argument_t *)malloc(sizeof(worker_argument_t));
if (ret == NULL) return ret;
ret->id = id;
2020-12-07 02:19:14 +09:00
#ifdef USE_NO_QUEUE
ret->csock = csock;
#endif
2020-12-05 10:25:54 +09:00
ret->bufsize = bufsize;
ret->buf = (uint8_t *)malloc(sizeof(*ret->buf)*bufsize);
if(ret->buf == NULL){
free(ret);
ret = NULL;
}
return ret;
}
2020-12-07 02:19:14 +09:00
void destory_worker_argument(worker_argument_t * arg){
free(arg->buf);
free(arg);
}
#ifndef USE_NO_QUEUE
2020-12-05 10:25:54 +09:00
static shared_state_t globalState;
void * worker_proc(void * data){
worker_argument_t * args = (worker_argument_t *)data;
int fd, csock;
2020-12-07 02:19:14 +09:00
#ifdef USE_TRACE
2020-12-07 00:26:59 +09:00
struct timespec ts,ts_middle,ts_end;
2020-12-07 02:19:14 +09:00
#endif
2020-12-05 10:25:54 +09:00
for(;;){
2020-12-07 02:19:14 +09:00
2020-12-05 10:25:54 +09:00
pthread_mutex_lock(&globalState.sock_mutex);
while (queue_isempty(&globalState.socks)){
pthread_cond_wait(&globalState.ready,&globalState.sock_mutex);
}
csock = dequeue(&globalState.socks);
2020-12-07 02:19:14 +09:00
#ifdef USE_TRACE
2020-12-07 00:26:59 +09:00
ts = dequeue(&globalState.trace_timer);
2020-12-07 02:19:14 +09:00
#endif
2020-12-05 10:25:54 +09:00
pthread_mutex_unlock(&globalState.sock_mutex);
2020-12-07 02:19:14 +09:00
#ifdef USE_TRACE
2020-12-07 00:26:59 +09:00
clock_gettime(Trace_Timer_ID,&ts_middle);
2020-12-07 02:19:14 +09:00
#endif
2020-12-05 10:25:54 +09:00
if((fd = read_request(csock,args->buf,args->bufsize)) > 0){
send_response(csock,fd,args->buf,args->bufsize);
close(fd);
}
2020-12-07 02:19:14 +09:00
#ifdef USE_TRACE
2020-12-07 00:26:59 +09:00
clock_gettime(Trace_Timer_ID,&ts_end);
struct timespec tophalf = timespec_sub(ts_middle,ts);
struct timespec bottomhelf = timespec_sub(ts_end,ts_middle);
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
2020-12-07 02:19:14 +09:00
#endif
2020-12-05 10:25:54 +09:00
if(close(csock) < 0)
perror("csock close error");
}
2020-12-07 02:19:14 +09:00
destory_worker_argument(args);
return NULL;
2020-12-05 10:25:54 +09:00
}
static pthread_t worker_threads[MAX_THREAD_NUMBER];
2020-12-07 02:19:14 +09:00
#else
void * worker_proc(void * data){
worker_argument_t * args = (worker_argument_t *)data;
int fd, csock;
csock = args->csock;
#ifdef USE_TRACE
struct timespec ts,ts_middle,ts_end;
ts = args->ts;
clock_gettime(Trace_Timer_ID,&ts_middle);
#endif
if((fd = read_request(csock,args->buf,args->bufsize)) > 0){
send_response(csock,fd,args->buf,args->bufsize);
close(fd);
}
#ifdef USE_TRACE
clock_gettime(Trace_Timer_ID,&ts_end);
struct timespec tophalf = timespec_sub(ts_middle,ts);
struct timespec bottomhelf = timespec_sub(ts_end,ts_middle);
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
#endif
if(close(csock) < 0)
perror("csock close error");
destory_worker_argument(args);
return NULL;
}
#endif
2020-12-04 17:33:41 +09:00
2020-12-04 08:21:37 +09:00
static int sock;
void safe_exit(){
close(sock);
}
2020-12-04 08:06:26 +09:00
int main(int argc, const char *argv[]){
struct sockaddr_in addr;
2020-12-04 08:21:37 +09:00
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int csock;
int bufsize;
2020-12-07 02:19:14 +09:00
int i = 0;
2020-12-04 18:04:46 +09:00
in_port_t binding_port_number = SERVER_PORT;
if (argc > 1){
int d = parse_args(argc,argv,&binding_port_number);
if(d != 0 ) return d;
}
2020-12-04 08:06:26 +09:00
sock = socket(AF_INET,SOCK_STREAM,0);
2020-12-04 08:21:37 +09:00
atexit(safe_exit);
2020-12-04 08:06:26 +09:00
if(sock < 0){
perror("sock create fail");
return 1;
}
2020-12-04 15:56:21 +09:00
else {
int option = 1;
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option)) < 0){
perror("setsockopt");
}
}
2020-12-04 08:21:37 +09:00
bufsize = getBufferSizeFrom(sock);
2020-12-07 02:19:14 +09:00
#ifndef USE_NO_QUEUE
init_shared_state(&globalState);
2020-12-05 10:25:54 +09:00
for (i = 0; i < MAX_THREAD_NUMBER; i++) {
worker_argument_t * args = create_worker_argument(i,bufsize);
if (args == NULL) {
fprintf(stderr,"malloc: lack of memory");
return 1;
}
pthread_create(&worker_threads[i],NULL,worker_proc,args);
2020-12-04 08:16:51 +09:00
}
2020-12-07 02:19:14 +09:00
#endif
2020-12-04 08:21:37 +09:00
addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/
2020-12-04 08:06:26 +09:00
addr.sin_family = AF_INET;
2020-12-04 18:04:46 +09:00
addr.sin_port = htons(binding_port_number);
2020-12-04 08:21:37 +09:00
if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){
perror("bind failed");
2020-12-04 08:06:26 +09:00
return 1;
2020-12-04 14:52:25 +09:00
} else {
char ip_buf[INET_ADDRSTRLEN];
2020-12-04 16:48:03 +09:00
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
2020-12-04 14:52:25 +09:00
assert(msg != NULL);
2020-12-06 21:26:38 +09:00
fprintf(stdout,"server bind on %s:%d\n",msg ,binding_port_number);
2020-12-04 08:06:26 +09:00
}
2020-12-04 08:21:37 +09:00
2020-12-05 10:25:54 +09:00
if(listen(sock,MAX_LISTEN_SOCKET) < 0){
2020-12-04 08:21:37 +09:00
perror("listen failed");
return 1;
2020-12-04 08:06:26 +09:00
}
2020-12-04 08:21:37 +09:00
while ((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) >= 0)
{
2020-12-04 16:23:13 +09:00
char ip_buf[INET_ADDRSTRLEN];
2020-12-05 10:25:54 +09:00
const char * msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf));
2020-12-06 21:26:38 +09:00
fprintf(stdout,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
2020-12-07 00:26:59 +09:00
#ifdef USE_TRACE
struct timespec ts;
clock_gettime(Trace_Timer_ID, &ts);
#endif
2020-12-07 02:19:14 +09:00
#ifndef USE_NO_QUEUE
2020-12-05 10:25:54 +09:00
for(;;){
pthread_mutex_lock(&globalState.sock_mutex);
if (queue_isfull(&globalState.socks)){
pthread_mutex_unlock(&globalState.sock_mutex);
2020-12-07 02:19:14 +09:00
#ifdef _GNU_SOURCE
2020-12-05 10:25:54 +09:00
pthread_yield();
2020-12-07 02:19:14 +09:00
#else
2020-12-05 10:25:54 +09:00
usleep(400);
2020-12-07 02:19:14 +09:00
#endif
2020-12-05 10:25:54 +09:00
continue;
}
2020-12-07 00:26:59 +09:00
else {
enqueue(&globalState.socks,csock);
2020-12-07 02:19:14 +09:00
#ifdef USE_TRACE
2020-12-07 00:26:59 +09:00
enqueue(&globalState.trace_timer,ts);
2020-12-07 02:19:14 +09:00
#endif
2020-12-07 00:26:59 +09:00
}
2020-12-05 10:25:54 +09:00
break;
2020-12-04 08:21:37 +09:00
}
2020-12-05 10:25:54 +09:00
pthread_mutex_unlock(&globalState.sock_mutex);
pthread_cond_signal(&globalState.ready);
2020-12-07 02:19:14 +09:00
#else
pthread_t thread_a;
worker_argument_t * args = create_worker_argument(i++,bufsize,csock);
#ifdef USE_TRACE
args->ts = ts;
#endif
pthread_create(&thread_a,NULL,worker_proc,args);
pthread_detach(thread_a);
#endif
2020-12-04 08:21:37 +09:00
}
2020-12-05 10:25:54 +09:00
2020-12-04 08:21:37 +09:00
return 1;
2020-12-04 08:06:26 +09:00
}