]> Nishi Git Mirror - rbuild.git/commitdiff
server can be launched
authorNishi <nishi@nishi.boats>
Fri, 11 Oct 2024 14:53:43 +0000 (14:53 +0000)
committerNishi <nishi@nishi.boats>
Fri, 11 Oct 2024 14:53:43 +0000 (14:53 +0000)
git-svn-id: file:///raid/svn-personal/rbuild/trunk@5 c68d3453-7f82-0740-9748-1d72386a946b

Common/cm_bool.h [new file with mode: 0644]
Platform/generic.mk
Platform/win32.mk [new file with mode: 0644]
Platform/win64.mk [new file with mode: 0644]
Server/Makefile
Server/main.c
Server/rbs_server.h [new file with mode: 0644]
Server/server.c [new file with mode: 0644]

diff --git a/Common/cm_bool.h b/Common/cm_bool.h
new file mode 100644 (file)
index 0000000..f5ee229
--- /dev/null
@@ -0,0 +1,11 @@
+/* $Id$ */
+
+#ifndef __CM_BOOL_H__
+#define __CM_BOOL_H__
+
+typedef unsigned char CMBOOL;
+
+#define CMTRUE 1
+#define CMFALSE 0
+
+#endif
index 3dfe0071ef481833ceaac2da84597d562510d4eb..d4470ef6d401d282c1778d7a57c6805c1ddb910e 100644 (file)
@@ -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 (file)
index 0000000..629662a
--- /dev/null
@@ -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 (file)
index 0000000..2e9a91d
--- /dev/null
@@ -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
index 92cc9cbf44f49885f7cc4d8fc8866620a4883352..10de3836080e09d3a33cbf27a50e038d7407f1c6 100644 (file)
@@ -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)
index 989daf6cafa9a778cb4e8d73296cd3d4f27e07ab..e01dc2ed69dc3aead73ab11d9df0b9d23fcecb5c 100644 (file)
@@ -1,4 +1,39 @@
 /* $Id$ */
 
-int main(){
+#include <stdio.h>
+#include <string.h>
+
+#include "rbs_server.h"
+
+#include <cm_bool.h>
+
+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 (file)
index 0000000..a20fc2e
--- /dev/null
@@ -0,0 +1,11 @@
+/* $Id$ */
+
+#ifndef __RBS_SERVER_H__
+#define __RBS_SERVER_H__
+
+#include <cm_bool.h>
+
+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 (file)
index 0000000..8a56461
--- /dev/null
@@ -0,0 +1,126 @@
+/* $Id$ */
+
+#include "rbs_server.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <signal.h>
+
+#ifdef __MINGW32__
+#define WINSOCK
+#include <winsock2.h>
+#include <process.h>
+#else
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#endif
+
+#include <cm_bool.h>
+
+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
+               }
+       }
+}