Compare commits
5 Commits
a7457281e5
...
9ff0082313
Author | SHA1 | Date | |
---|---|---|---|
|
9ff0082313 | ||
|
e6801d11b2 | ||
|
9b991b13b2 | ||
|
3e4a5ac1db | ||
|
eae3af86fb |
6
.gitignore
vendored
6
.gitignore
vendored
@ -54,4 +54,10 @@ dkms.conf
|
||||
|
||||
#Bin
|
||||
bin/*
|
||||
client_test/*
|
||||
server_test/*
|
||||
|
||||
p-client
|
||||
p-server
|
||||
server
|
||||
client
|
||||
|
7
.vscode/settings.json
vendored
7
.vscode/settings.json
vendored
@ -1,3 +1,8 @@
|
||||
{
|
||||
"C_Cpp.default.defines": ["_GNU_SOURCE"]
|
||||
"C_Cpp.default.defines": [
|
||||
"_GNU_SOURCE"
|
||||
],
|
||||
"files.associations": {
|
||||
"socket_wrapper.h": "c"
|
||||
}
|
||||
}
|
9
Makefile
9
Makefile
@ -1,7 +1,6 @@
|
||||
CC = gcc
|
||||
CFLAGS = -lm -Wall -O2
|
||||
|
||||
Bin = server client
|
||||
Bin = server client p-server p-client
|
||||
|
||||
all:
|
||||
make $(Bin)
|
||||
@ -11,8 +10,12 @@ socket_wrapper.o: socket_wrapper.c socket_wrapper.h
|
||||
|
||||
client: socket_wrapper.o client.c
|
||||
$(CC) -o client client.c socket_wrapper.o $(CFLAGS)
|
||||
server:
|
||||
server: socket_wrapper.o server.c
|
||||
$(CC) -o server server.c socket_wrapper.o $(CFLAGS)
|
||||
p-server: socket_wrapper.o p-server.c
|
||||
$(CC) -o p-server p-server.c socket_wrapper.o $(CFLAGS)
|
||||
p-client: socket_wrapper.o p-client.c
|
||||
$(CC) -o p-client p-client.c socket_wrapper.o $(CFLAGS)
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
|
17
README.md
17
README.md
@ -1,3 +1,18 @@
|
||||
# HW12
|
||||
|
||||
Homework
|
||||
Homework
|
||||
|
||||
To build source, you should install `build-essential`.
|
||||
|
||||
Usage:
|
||||
```bash
|
||||
./server [OPTION]...
|
||||
./client SERVERNAME PORT FILENAME
|
||||
```
|
||||
|
||||
available macro:
|
||||
- DEFAULT_SERVER_PORT : 9091
|
||||
- DEFAULT_MAX_PATH_SIZE : 256(must be less than 1000)
|
||||
- TIMEOUT : 5(second unit)
|
||||
- USE_SENDFILE
|
||||
- DEFAULT_SEND_FILE_CHUNK_SIZE : 0x100000(1MB)
|
30
client.c
30
client.c
@ -16,7 +16,16 @@
|
||||
|
||||
#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_TIMEOUT
|
||||
static const int TIMEOUT = 5;
|
||||
#else
|
||||
static const int TIMEOUT = DEFAULT_TIMEOUT;
|
||||
#endif
|
||||
|
||||
/*========
|
||||
@ -121,8 +130,8 @@ int main(int argc, const char *argv[]){
|
||||
struct sockaddr_in addr;
|
||||
const char * filename;
|
||||
const char * server_name;
|
||||
in_port_t server_port = SERVER_PORT;
|
||||
int sock;
|
||||
in_port_t server_port;
|
||||
int sock, err;
|
||||
if (argc != 4){
|
||||
fprintf(stderr,"invaild arguments number.");
|
||||
return 1;
|
||||
@ -140,17 +149,24 @@ int main(int argc, const char *argv[]){
|
||||
perror("sock create fail");
|
||||
return 1;
|
||||
}
|
||||
{
|
||||
int option = 1;
|
||||
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option)) < 0){
|
||||
perror("setsockopt");
|
||||
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_addr.s_addr = inet_addr(server_name);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(server_port);
|
||||
if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)) < 0){
|
||||
perror("connect failed");
|
||||
close(sock);
|
||||
return 1;
|
||||
}
|
||||
if(sendReadOp(sock,filename) == 0){
|
||||
|
177
p-client.c
Normal file
177
p-client.c
Normal file
@ -0,0 +1,177 @@
|
||||
#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>
|
||||
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include "socket_wrapper.h"
|
||||
|
||||
#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_TIMEOUT
|
||||
static const int TIMEOUT = 5;
|
||||
#else
|
||||
static const int TIMEOUT = DEFAULT_TIMEOUT;
|
||||
#endif
|
||||
/*========
|
||||
*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;
|
||||
}
|
||||
if(send(sock,filename,op.file_url_size,0)<0){
|
||||
perror("readop filename send fail");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int recvFile(int sock, const char * filename,size_t file_size){
|
||||
int fd;
|
||||
size_t count = 0;
|
||||
int i;
|
||||
int cur_progress = 1;
|
||||
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;
|
||||
}
|
||||
if( ((double)count / (double)file_size) * 100.0 > ((double)cur_progress) ){
|
||||
printf("\rprogress : %d%% current bytes: %ld bytes",cur_progress,count);
|
||||
cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0);
|
||||
fflush(stdout);
|
||||
}
|
||||
count += readed;
|
||||
}
|
||||
printf("\rprogress : 100.00%% current bytes: %ld bytes\n",count);
|
||||
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;
|
||||
in_port_t server_port = 0;
|
||||
int sock, err;
|
||||
if (argc != 4){
|
||||
fprintf(stderr,"invaild arguments number.");
|
||||
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;
|
||||
}
|
251
p-server.c
Normal file
251
p-server.c
Normal file
@ -0,0 +1,251 @@
|
||||
#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>
|
||||
#include <signal.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include "socket_wrapper.h"
|
||||
|
||||
static const int MAX_LISTEN_SOCKET = 16;
|
||||
|
||||
#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
|
||||
|
||||
/*========
|
||||
*Operation
|
||||
*========*/
|
||||
|
||||
/**
|
||||
* send user error message
|
||||
* 80 character limit
|
||||
* thread safe
|
||||
*/
|
||||
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");
|
||||
return -1;
|
||||
}
|
||||
if (send(sock,msg,res.error_msg_size,0) < 0){
|
||||
perror("error msg send");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* send errno to client
|
||||
* thread safe
|
||||
*/
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* return fd, if success. otherwise, return -1.
|
||||
* thread safe
|
||||
*/
|
||||
int read_request(int sock,uint8_t * buf,size_t bufsize){
|
||||
struct ReadOp p;
|
||||
int fd;
|
||||
ssize_t n = recv_until_byte(sock,&p,sizeof(p),TIMEOUT);
|
||||
if (n < 0){
|
||||
if (n == -2) fprintf(stderr,"timeout!");
|
||||
else perror("receive fail");
|
||||
return -1;
|
||||
}
|
||||
if(bufsize <= ((size_t)p.file_url_size) + sizeof(p) + 1){
|
||||
send_fail(sock,"buffer overflow");
|
||||
return -1;
|
||||
}
|
||||
else if(p.file_url_size + 1 > MAX_PATH_SIZE){
|
||||
send_fail(sock,"max path fail");
|
||||
return -1;
|
||||
}
|
||||
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);
|
||||
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;
|
||||
}
|
||||
fd = open((char *)buf,O_RDONLY);
|
||||
if(fd < 0){
|
||||
send_errno(sock);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
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;
|
||||
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");
|
||||
return -1;
|
||||
}
|
||||
#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");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#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");
|
||||
return -1;
|
||||
}
|
||||
if(send(sock,buf,readed,0)<0){
|
||||
perror("send response send fail");
|
||||
return -1;
|
||||
}
|
||||
offset += readed;
|
||||
}
|
||||
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
static int sock;
|
||||
void safe_exit(){
|
||||
close(sock);
|
||||
}
|
||||
int main(int argc, const char *argv[]){
|
||||
uint8_t * buf;
|
||||
struct sockaddr_in addr;
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addr_len = sizeof(client_addr);
|
||||
int csock;
|
||||
int bufsize;
|
||||
int i;
|
||||
sock = socket(AF_INET,SOCK_STREAM,0);
|
||||
atexit(safe_exit);
|
||||
if(sock < 0){
|
||||
perror("sock create fail");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int option = 1;
|
||||
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option)) < 0){
|
||||
perror("setsockopt");
|
||||
}
|
||||
}
|
||||
bufsize = getBufferSizeFrom(sock);
|
||||
buf = malloc(bufsize * sizeof(*buf));
|
||||
if (buf == NULL){
|
||||
fprintf(stderr,"lack of memory");
|
||||
return 1;
|
||||
}
|
||||
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(SERVER_PORT);
|
||||
|
||||
if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){
|
||||
perror("bind failed");
|
||||
return 1;
|
||||
} else {
|
||||
char ip_buf[INET_ADDRSTRLEN];
|
||||
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
|
||||
assert(msg != NULL);
|
||||
fprintf(stderr,"server bind on %s:%d\n",msg ,SERVER_PORT);
|
||||
}
|
||||
if(listen(sock,MAX_LISTEN_SOCKET) < 0){
|
||||
perror("listen failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
int fd, pid;
|
||||
char ip_buf[INET_ADDRSTRLEN];
|
||||
const char * msg;
|
||||
if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){
|
||||
free(buf);
|
||||
perror("accept error");
|
||||
return 1;
|
||||
}
|
||||
msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
|
||||
fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
|
||||
pid = fork();
|
||||
if(pid == 0){
|
||||
if((fd = read_request(csock,buf,bufsize)) > 0){
|
||||
send_response(csock,fd,buf,bufsize);
|
||||
close(fd);
|
||||
}
|
||||
if(close(csock) < 0)
|
||||
perror("csock close error");
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
43
server.c
43
server.c
@ -14,6 +14,31 @@
|
||||
#include <fcntl.h>
|
||||
#include "socket_wrapper.h"
|
||||
|
||||
#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
|
||||
|
||||
/*========
|
||||
*Operation
|
||||
*========*/
|
||||
@ -146,6 +171,7 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sock;
|
||||
void safe_exit(){
|
||||
close(sock);
|
||||
@ -163,6 +189,12 @@ int main(int argc, const char *argv[]){
|
||||
perror("sock create fail");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int option = 1;
|
||||
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option)) < 0){
|
||||
perror("setsockopt");
|
||||
}
|
||||
}
|
||||
bufsize = getBufferSizeFrom(sock);
|
||||
buf = malloc(bufsize * sizeof(*buf));
|
||||
if (buf == NULL){
|
||||
@ -177,8 +209,12 @@ int main(int argc, const char *argv[]){
|
||||
if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){
|
||||
perror("bind failed");
|
||||
return 1;
|
||||
} else {
|
||||
char ip_buf[INET_ADDRSTRLEN];
|
||||
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
|
||||
assert(msg != NULL);
|
||||
fprintf(stderr,"server bind on %s:%d\n",msg ,SERVER_PORT);
|
||||
}
|
||||
fprintf(stderr,"server bind on %s:%d\n",inet_ntoa(addr.sin_addr),SERVER_PORT);
|
||||
|
||||
if(listen(sock,1) < 0){
|
||||
perror("listen failed");
|
||||
@ -188,8 +224,9 @@ int main(int argc, const char *argv[]){
|
||||
while ((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) >= 0)
|
||||
{
|
||||
int fd;
|
||||
/*inet_ntoa is not reentrant function. do not use it in signal handler.*/
|
||||
printf("Connect : %s\n",(inet_ntoa(client_addr.sin_addr)));
|
||||
char ip_buf[INET_ADDRSTRLEN];
|
||||
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
|
||||
fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
|
||||
if((fd = read_request(csock,buf,bufsize)) > 0){
|
||||
send_response(csock,fd,buf,bufsize);
|
||||
close(fd);
|
||||
|
@ -5,7 +5,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <netdb.h>
|
||||
#include <string.h>
|
||||
#include "socket_wrapper.h"
|
||||
|
||||
int getBufferSizeFrom(int sock){
|
||||
@ -60,4 +61,31 @@ ssize_t recv_until_byte(int fd,void * buf, size_t n,int timeout){
|
||||
}
|
||||
assert(cur == n);
|
||||
return cur;
|
||||
}
|
||||
|
||||
int getsockaddrbyname(int domain,int type, int protocol, const char * hostname_str, struct sockaddr * retaddr){
|
||||
struct addrinfo hints;
|
||||
struct addrinfo * result, *entry;
|
||||
int ret, i;
|
||||
memset(&hints,0,sizeof(hints));
|
||||
hints.ai_family = domain;
|
||||
hints.ai_socktype = type;
|
||||
hints.ai_flags = 0;
|
||||
hints.ai_protocol = protocol;
|
||||
|
||||
//try 5 times
|
||||
for (i = 0; i < 5; i++)
|
||||
{
|
||||
ret = getaddrinfo(hostname_str,NULL,&hints,&result);
|
||||
if(ret == EAI_AGAIN) continue;
|
||||
else if (ret != 0) return ret;
|
||||
break;
|
||||
}
|
||||
if (ret != 0) return ret; //maybe name server doesn't work properly.
|
||||
|
||||
for (entry = result; entry != NULL; entry = entry->ai_next){
|
||||
memcpy(retaddr,entry->ai_addr,sizeof(*retaddr));
|
||||
}
|
||||
freeaddrinfo(result);
|
||||
return ret;
|
||||
}
|
@ -5,18 +5,11 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#ifdef USE_SENDFILE
|
||||
#include <sys/sendfile.h>
|
||||
const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/
|
||||
#endif
|
||||
|
||||
static const in_port_t SERVER_PORT = 9091;
|
||||
static const size_t DEFAULT_BUF_SIZE = 4096;
|
||||
static const size_t MINIMUM_BUF_SIZE = 1024;
|
||||
/*0 < x < MAX_PATH_SIZE*/
|
||||
static const uint16_t MAX_PATH_SIZE = 256;
|
||||
static const int TIMEOUT = 5;
|
||||
|
||||
enum{
|
||||
RES_OK = 0,
|
||||
@ -54,10 +47,18 @@ int getBufferSizeFrom(int sock);
|
||||
* thread safe
|
||||
*/
|
||||
ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout);
|
||||
/*
|
||||
/**
|
||||
* recieve data to buf until all bytes.
|
||||
* thread safe
|
||||
*/
|
||||
ssize_t recv_until_byte(int fd,void * buf, size_t n,int timeout);
|
||||
|
||||
/**
|
||||
* find sockaddr by hostname
|
||||
* it perform like `gethostbyname`
|
||||
* hostname could not be greater than _SC_HOST_NAME_MAX.
|
||||
* return 0 on success, otherwise return ecode.
|
||||
* ecode can be converted to string by `gai_strerror`.
|
||||
*/
|
||||
int getsockaddrbyname(int domain,int type, int protocol, const char * hostname_str, struct sockaddr * retaddr);
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user