From 99dea168104e9091698e77500a0fe773a6a7fe9b Mon Sep 17 00:00:00 2001 From: Nishi Date: Fri, 11 Oct 2024 14:53:43 +0000 Subject: [PATCH] server can be launched git-svn-id: file:///raid/svn-personal/rbuild/trunk@5 c68d3453-7f82-0740-9748-1d72386a946b --- Common/cm_bool.h | 11 ++++ Platform/generic.mk | 2 +- Platform/win32.mk | 8 +++ Platform/win64.mk | 8 +++ Server/Makefile | 2 +- Server/main.c | 37 ++++++++++++- Server/rbs_server.h | 11 ++++ Server/server.c | 126 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 Common/cm_bool.h create mode 100644 Platform/win32.mk create mode 100644 Platform/win64.mk create mode 100644 Server/rbs_server.h create mode 100644 Server/server.c diff --git a/Common/cm_bool.h b/Common/cm_bool.h new file mode 100644 index 0000000..f5ee229 --- /dev/null +++ b/Common/cm_bool.h @@ -0,0 +1,11 @@ +/* $Id$ */ + +#ifndef __CM_BOOL_H__ +#define __CM_BOOL_H__ + +typedef unsigned char CMBOOL; + +#define CMTRUE 1 +#define CMFALSE 0 + +#endif diff --git a/Platform/generic.mk b/Platform/generic.mk index 3dfe007..d4470ef 100644 --- a/Platform/generic.mk +++ b/Platform/generic.mk @@ -2,7 +2,7 @@ CC = cc AR = ar -CFLAGS = -I $(PWD)/Common -fPIC +CFLAGS = -I $(PWD)/Common LDFLAGS = LIBS = EXEC = diff --git a/Platform/win32.mk b/Platform/win32.mk new file mode 100644 index 0000000..629662a --- /dev/null +++ b/Platform/win32.mk @@ -0,0 +1,8 @@ +# $Id$ + +CC = i686-w64-mingw32-gcc +AR = i686-w64-mingw32-ar +CFLAGS = -I $(PWD)/Common +LDFLAGS = +LIBS = -lws2_32 +EXEC = .exe diff --git a/Platform/win64.mk b/Platform/win64.mk new file mode 100644 index 0000000..2e9a91d --- /dev/null +++ b/Platform/win64.mk @@ -0,0 +1,8 @@ +# $Id$ + +CC = x86_64-w64-mingw32-gcc +AR = x86_64-w64-mingw32-ar +CFLAGS = -I $(PWD)/Common +LDFLAGS = +LIBS = -lws2_32 +EXEC = .exe diff --git a/Server/Makefile b/Server/Makefile index 92cc9cb..10de383 100644 --- a/Server/Makefile +++ b/Server/Makefile @@ -5,7 +5,7 @@ include $(PWD)/Platform/$(PLATFORM).mk .PHONY: all clean .SUFFIXES: .c .o -OBJS = main.o +OBJS = main.o server.o OBJS += ../Common/common.a all: rbuild-server$(EXEC) diff --git a/Server/main.c b/Server/main.c index 989daf6..e01dc2e 100644 --- a/Server/main.c +++ b/Server/main.c @@ -1,4 +1,39 @@ /* $Id$ */ -int main(){ +#include +#include + +#include "rbs_server.h" + +#include + +extern CMBOOL run_inetd; + +int main(int argc, char** argv){ + int i; + for(i = 1; i < argc; i++){ + if(argv[i][0] == '-'){ + if(strcmp(argv[i], "--inetd") == 0 || strcmp(argv[i], "-i") == 0){ + run_inetd = CMTRUE; + }else if(strcmp(argv[i], "--auth") == 0 || strcmp(argv[i], "-a") == 0){ + i++; + if(argv[i] == NULL){ + fprintf(stderr, "Missing argument\n"); + return 1; + } + }else{ + fprintf(stderr, "Unknown option: %s\n", argv[i]); + return 1; + } + }else{ + } + } + if(!rbs_server_init()){ + fprintf(stderr, "Failed to initialize\n"); + return 1; + } + if(!rbs_server_loop()){ + return 1; + } + return 0; } diff --git a/Server/rbs_server.h b/Server/rbs_server.h new file mode 100644 index 0000000..a20fc2e --- /dev/null +++ b/Server/rbs_server.h @@ -0,0 +1,11 @@ +/* $Id$ */ + +#ifndef __RBS_SERVER_H__ +#define __RBS_SERVER_H__ + +#include + +CMBOOL rbs_server_init(void); +CMBOOL rbs_server_loop(void); + +#endif diff --git a/Server/server.c b/Server/server.c new file mode 100644 index 0000000..8a56461 --- /dev/null +++ b/Server/server.c @@ -0,0 +1,126 @@ +/* $Id$ */ + +#include "rbs_server.h" + +#include +#include +#include +#include +#include + +#ifdef __MINGW32__ +#define WINSOCK +#include +#include +#else +#include +#include +#include +#include +#endif + +#include + +CMBOOL run_inetd = CMFALSE; +int server_socket; +int port = 7980; + +CMBOOL rbs_server_init(void){ + if(run_inetd){ + return CMTRUE; + }else{ + struct sockaddr_in server_address; + int yes = 1; +#ifdef WINSOCK + WSADATA wsa; + WSAStartup(MAKEWORD(2, 0), &wsa); +#endif + server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); +#ifdef WINSOCK + if(server_socket == INVALID_SOCKET) return CMFALSE; +#else + if(server_socket < 0) return CMFALSE; +#endif + if(setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0){ + close(server_socket); + return CMFALSE; + } + memset(&server_address, 0, sizeof(server_address)); + server_address.sin_family = AF_INET; + server_address.sin_addr.s_addr = INADDR_ANY; + server_address.sin_port = htons(port); + if(bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0){ + close(server_socket); + fprintf(stderr, "Bind fail\n"); + return CMFALSE; + } + if(listen(server_socket, 128) < 0){ + close(server_socket); + fprintf(stderr, "Listen fail\n"); + return CMFALSE; + } + printf("Ready\n"); + return CMTRUE; + } +} + +void rbs_write(int sock, unsigned char* data, unsigned int size){ + if(run_inetd){ + fwrite(data, 1, size, stdout); + fflush(stdout); + }else{ + send(sock, data, size, 0); + } +} + +void rbs_close(int sock){ +#ifdef WINSOCK + closesocket(sock); +#else + close(sock); +#endif +} + +void rbs_server_handler(void* sockptr){ + int sock = -1; + if(sockptr != NULL){ + sock = *(int*)sockptr; + free(sockptr); + } + rbs_write(sock, "Hello\n", 6); + + rbs_close(sock); +#ifdef WINSOCK + _endthread(); +#endif +} + +CMBOOL rbs_server_loop(void){ + if(run_inetd){ + rbs_server_handler(NULL); + }else{ +#ifndef WINSOCK + signal(SIGCHLD, SIG_IGN); +#endif + while(1){ + struct sockaddr_in claddr; + int clen = sizeof(claddr); + int sock = accept(server_socket, (struct sockaddr*)&claddr, &clen); +#ifdef WINSOCK + int* sockptr = malloc(sizeof(*sockptr)); + *sockptr = sock; + _beginthread(rbs_server_handler, 0, sockptr); +#else + pid_t p = fork(); + if(p == 0){ + int* sockptr = malloc(sizeof(*sockptr)); + *sockptr = sock; + rbs_server_handler(sockptr); + _exit(0); + }else{ + rbs_close(sock); + } +#endif + } + } +} -- 2.43.0