first commit

This commit is contained in:
Jose Caban
2025-06-07 01:59:34 -04:00
commit 388ac241f0
3558 changed files with 9116289 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
l33t_server.vcproj.CRYSTAL.AssKoala.user
l33t_server.suo
Debug
Release
l33t_server.ncb
bin

View File

@@ -0,0 +1,6 @@
D/src////
/.cvsignore/1.1/Tue Feb 21 03:42:45 2006//
/l33t_server.sln/1.1/Thu Jan 26 02:43:32 2006//
/l33t_server.vcproj/1.1/Sat Feb 18 00:35:20 2006//
/Makefile/1.10/Tue Mar 28 01:11:53 2006//
/l33t_server.conf/1.4/Tue Mar 28 01:11:53 2006//

View File

@@ -0,0 +1,6 @@
D/src///////
/.cvsignore////*///
/l33t_server.sln////*///
/l33t_server.vcproj////*///
/Makefile////*///
/l33t_server.conf////*///

View File

@@ -0,0 +1,6 @@
D/src///////
/.cvsignore////*///
/l33t_server.sln////*///
/l33t_server.vcproj////*///
/l33t_server.conf////*///
/Makefile////*///

View File

@@ -0,0 +1,6 @@
D/src////
/.cvsignore/1.1/Tue Feb 21 03:42:45 2006//
/l33t_server.sln/1.1/Thu Jan 26 02:43:32 2006//
/l33t_server.vcproj/1.1/Sat Feb 18 00:35:20 2006//
/l33t_server.conf/1.2/Thu Mar 9 05:28:29 2006//
/Makefile/1.9/Tue Mar 28 00:07:59 2006//

View File

@@ -0,0 +1 @@
CS4210/Project 2/server

View File

@@ -0,0 +1 @@
:ext:asskoala@192.168.0.3:/usr/_CVS

View File

@@ -0,0 +1,43 @@
CC = gcc
OPT = -O2 -fomit-frame-pointer
LIBS = -lpthread
TARGET = l33t_server
MISC = Makefile
INCDIR = src/include
SRCDIR = src
BINDIR = ../bin/server
COMMONDIR = ../common
MAKE = make
all: $(TARGET)
$(TARGET): $(BINDIR)/parser.o $(BINDIR)/settings.o $(BINDIR)/thread_pool.o $(BINDIR)/networking.o $(BINDIR)/sock_lib.o $(BINDIR)/http.o $(BINDIR)/shared_memory.o $(BINDIR)/main.o $(COMMONDIR)/defs.h
$(CC) $(OPT) $(LIBS) -o $(BINDIR)/$(TARGET) $(BINDIR)/*.o
$(BINDIR)/parser.o: $(COMMONDIR)/config_parser/parser.h $(COMMONDIR)/config_parser/parser.c
-mkdir -p $(BINDIR) > /dev/null
$(CC) $(OPT) -c -o $(BINDIR)/parser.o $(COMMONDIR)/config_parser/parser.c
$(BINDIR)/settings.o: $(SRCDIR)/settings.c $(INCDIR)/settings.h
$(CC) $(OPT) -c -o $(BINDIR)/settings.o $(SRCDIR)/settings.c
$(BINDIR)/thread_pool.o: $(COMMONDIR)/thread_pool/thread_pool.h $(COMMONDIR)/thread_pool/thread_pool.c
$(CC) $(OPT) -c -o $(BINDIR)/thread_pool.o $(COMMONDIR)/thread_pool/thread_pool.c
$(BINDIR)/networking.o: $(COMMONDIR)/networking/networking.h $(COMMONDIR)/networking/networking.c
$(CC) $(OPT) -c -o $(BINDIR)/networking.o $(COMMONDIR)/networking/networking.c
$(BINDIR)/sock_lib.o: $(SRCDIR)/sock_lib.c $(INCDIR)/sock_lib.h
$(CC) $(OPT) -c -o $(BINDIR)/sock_lib.o $(SRCDIR)/sock_lib.c
$(BINDIR)/http.o: $(COMMONDIR)/http/http.c $(COMMONDIR)/http/http.h
$(CC) $(OPT) -c -o $(BINDIR)/http.o $(COMMONDIR)/http/http.c
$(BINDIR)/shared_memory.o: $(COMMONDIR)/shared_memory/shared_memory.c $(COMMONDIR)/shared_memory/shared_memory.h
$(CC) $(OPT) -c -o $(BINDIR)/shared_memory.o $(COMMONDIR)/shared_memory/shared_memory.c
$(BINDIR)/main.o: $(SRCDIR)/main.c
$(CC) $(OPT) -c -o $(BINDIR)/main.o $(SRCDIR)/main.c
clean:
rm -f $(BINDIR)/*.o $(BINDIR)/$(TARGET)

View File

@@ -0,0 +1,19 @@
# Lines beginning with # will be ignored
#
# l33t_server config file
#
# This file will override internal static compiled
# default configurations for the server.
#
# Any settings in this file will be overwritten by any command line
# arguments passed to the application.
#
# Load Dir
LOAD_DIR:../
# MAX_THREADS
MAX_THREADS:16
# Port Number
PORT_NUMBER:1337

View File

@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "l33t_server", "l33t_server.vcproj", "{F3D5791C-A793-4705-849B-9923D1A9A1F7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F3D5791C-A793-4705-849B-9923D1A9A1F7}.Debug|Win32.ActiveCfg = Debug|Win32
{F3D5791C-A793-4705-849B-9923D1A9A1F7}.Debug|Win32.Build.0 = Debug|Win32
{F3D5791C-A793-4705-849B-9923D1A9A1F7}.Release|Win32.ActiveCfg = Release|Win32
{F3D5791C-A793-4705-849B-9923D1A9A1F7}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,266 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="8.00"
Name="l33t_server"
ProjectGUID="{F3D5791C-A793-4705-849B-9923D1A9A1F7}"
RootNamespace="l33t_server"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="&quot;$(PROJECTDIR)\src\pthreads\include&quot;"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="pthreadVC2.lib pthreadVSE2.lib wsock32.lib"
AdditionalLibraryDirectories="&quot;$(PROJECTDIR)\src\pthreads\lib&quot;"
GenerateDebugInformation="true"
AssemblyDebug="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\src\dirent.c"
>
</File>
<File
RelativePath=".\src\http.c"
>
</File>
<File
RelativePath=".\src\main.c"
>
</File>
<File
RelativePath=".\src\parser.c"
>
</File>
<File
RelativePath=".\src\settings.c"
>
</File>
<File
RelativePath=".\src\snprintf.c"
>
</File>
<File
RelativePath=".\src\sock_lib.c"
>
</File>
<File
RelativePath=".\src\strtok_r.c"
>
</File>
<File
RelativePath=".\src\sync.c"
>
</File>
<File
RelativePath=".\src\tester.c"
>
</File>
<File
RelativePath=".\src\thread_pool.c"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\src\include\defs.h"
>
</File>
<File
RelativePath=".\src\include\dirent.h"
>
</File>
<File
RelativePath=".\src\include\http.h"
>
</File>
<File
RelativePath=".\src\include\parser.h"
>
</File>
<File
RelativePath=".\src\include\settings.h"
>
</File>
<File
RelativePath=".\src\include\snprintf.h"
>
</File>
<File
RelativePath=".\src\include\sock_lib.h"
>
</File>
<File
RelativePath=".\src\include\strtok_r.h"
>
</File>
<File
RelativePath=".\src\include\sync.h"
>
</File>
<File
RelativePath=".\src\include\tester.h"
>
</File>
<File
RelativePath=".\src\include\thread_pool.h"
>
</File>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
<File
RelativePath=".\l33t_server.conf"
>
</File>
<File
RelativePath=".\makefile"
>
</File>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,4 @@
D/include////
/main.c/1.9/Tue Mar 28 00:07:59 2006//
/settings.c/1.7/Tue Mar 28 00:07:59 2006//
/sock_lib.c/1.17/Tue Mar 28 02:40:31 2006//

View File

@@ -0,0 +1,4 @@
D/include///////
/main.c////*///
/settings.c////*///
/sock_lib.c////*///

View File

@@ -0,0 +1,5 @@
D/include///////
/main.c////*///
/settings.c////*///
/sock_lib.c////*///
D/pthreads///////

View File

@@ -0,0 +1,5 @@
D/include////
/main.c/1.9/Tue Mar 28 00:07:59 2006//
/settings.c/1.7/Tue Mar 28 00:07:59 2006//
/sock_lib.c/1.17/Tue Mar 28 02:40:31 2006//
D/pthreads////

View File

@@ -0,0 +1 @@
CS4210/Project 2/server/src

View File

@@ -0,0 +1 @@
:ext:asskoala@192.168.0.3:/usr/_CVS

View File

@@ -0,0 +1,3 @@
/settings.h/1.2/Tue Mar 28 00:07:59 2006//
/sock_lib.h/1.5/Tue Mar 28 00:07:59 2006//
D

View File

@@ -0,0 +1,2 @@
/settings.h////*///
/sock_lib.h////*///

View File

@@ -0,0 +1,2 @@
/settings.h////*///
/sock_lib.h////*///

View File

@@ -0,0 +1,3 @@
/settings.h/1.1/Fri Feb 17 02:20:35 2006//
/sock_lib.h/1.4/Sun Mar 12 16:43:56 2006//
D

View File

@@ -0,0 +1 @@
CS4210/Project 2/server/src/include

View File

@@ -0,0 +1 @@
:ext:asskoala@192.168.0.3:/usr/_CVS

View File

@@ -0,0 +1,21 @@
#ifndef _SETTINGS_H_
#define _SETTINGS_H_
/////////////
// Globals //
/////////////
typedef struct {
char* LOAD_DIR; // Server home directory
char* CONFIG_FILE; // Config File, should be constant
int MAX_THREADS; // Maximum number of threads to spawn
unsigned short PORT_NUMBER; // Port number to use
char useSHMEM;
} settings_t;
///////////////
// Functions //
///////////////
int init_settings(settings_t *s, int, char**);
#endif

View File

@@ -0,0 +1,11 @@
/* Puyan Lotfi
* Berkeley Sockets Abstaction
*/
void *consumer(void*);
void *producer(void* v_port_number);
int ContructTCPSocket(unsigned short port);
int AcceptConnection(int server_socket);

View File

@@ -0,0 +1,82 @@
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include "include/sock_lib.h"
#include "include/settings.h"
#include "../../common/defs.h"
#include "../../common/thread_pool/thread_pool.h"
#include "../../common/shared_memory/shared_memory.h"
/* signal handler for SIGINT */
void catch_int(int sig_num)
{
// TODO: Insert memory freeing stuff here
printf("Signal %d received\n", sig_num);
printf("Destroying the Shared Memory Segments...\n");
destroy_shared_memory();
printf("Done!\n");
exit(1);
}
int main(int argc, char** argv) {
settings_t settings;
int c = '\0'; //character to get
#ifndef LINUX
sigset_t newmask;
sigset_t oldmask;
sigemptyset(&newmask);
sigaddset(&newmask, SIGPIPE);
if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
{
fprintf(stderr, "can't set signal mask\n");
exit(1);
}
#endif
printf("* Beginning l33t_server Initialization *\n\n");
init_settings(&settings, argc, argv);
printf("** Using %d threads\n",settings.MAX_THREADS);
printf("** Using home_dir: %s\n",settings.LOAD_DIR);
printf( (settings.useSHMEM == 0 ? "** Not " : "** "));
printf( "Using Shared Memory.\n");
if (settings.useSHMEM) {
// Mask signals (fancy, isn't it?)
signal(SIGINT, catch_int);
signal(SIGABRT, catch_int);
signal(SIGSEGV, catch_int);
signal(SIGTERM, catch_int);
signal(SIGBUS, catch_int);
init_shared_memory();
}
if (chdir(settings.LOAD_DIR) < 0) {
printf("*** Error setting home directory..Exiting\n");
exit(1);
}
printf("* l33t_server started *\n\n");
init_thread_pool(&(settings.PORT_NUMBER), settings.MAX_THREADS,
producer, consumer, &settings);
while (getchar() != 'q');
if (settings.useSHMEM) {
catch_int(SIGINT);
}
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,93 @@
#include "include/settings.h"
#include "../../common/config_parser/parser.h"
#include "../../common/defs.h"
#include <stdio.h>
#include <string.h>
/*
* Set the default values for all the variables
*/
static void setDefaults(settings_t *s) {
s->LOAD_DIR = ".";
s->CONFIG_FILE = "l33t_server.conf";
s->MAX_THREADS = 7;
s->PORT_NUMBER = 1337; // l33t
s->useSHMEM = 0;
}
/*
* Initialize internal server parameters
*/
int init_settings(settings_t *s, int argc, char** argv) {
ConfigFile* configFile;
VPRINTF(("** Reading Config File **\n\n"));
setDefaults(s);
switch (argc) {
case 3:
s->CONFIG_FILE = argv[2];
s->useSHMEM = atoi(argv[1]);
break;
case 2:
s->useSHMEM = atoi(argv[1]);
break;
default:
cmdline_error:
printf("Proper Usage: \n");
printf(" ./l33t_server use_shared_memory [configFile]\n");
printf(" e.g. ./l33t_server 1\n");
printf(" e.g. ./l33t_server 0 l33t_server2.conf\n");
exit(1);
}
if (s->useSHMEM != 0 && s->useSHMEM != 1) goto cmdline_error;
configFile = loadFile(s->CONFIG_FILE);
if (!configFile) {
printf("*** configuration file \"%s\" not found.\n",s->CONFIG_FILE);
exit(1);
}
while (nextSet(configFile)) {
if (!strcmp(configFile->currentParamter, "LOAD_DIR")) {
VPRINTF(("*** Read LOAD_FILE Value: %s\n",configFile->currentValue));
s->LOAD_DIR = malloc(strlen(configFile->currentValue)+1);
strcpy(s->LOAD_DIR, configFile->currentValue);
} else if(!strcmp(configFile->currentParamter, "CONFIG_FILE")) {
VPRINTF(("*** Read CONFIG_FILE Value: %s\n",configFile->currentValue));
s->CONFIG_FILE = malloc(strlen(configFile->currentValue)+1);
strcpy(s->CONFIG_FILE, configFile->currentValue);
} else if(!strcmp(configFile->currentParamter, "MAX_THREADS")) {
s->MAX_THREADS = atoi(configFile->currentValue);
// In case some idiot tries to set the MAX_THREADS <=0
if (s->MAX_THREADS < 1) {
s->MAX_THREADS = 1;
}
VPRINTF(("*** Read MAX_THREADS Value: %d\n",s->MAX_THREADS));
} else if(!strcmp(configFile->currentParamter, "PORT_NUMBER")) {
s->PORT_NUMBER = atoi(configFile->currentValue);
VPRINTF(("*** Read PORT_NUMBER Value: %d\n",s->PORT_NUMBER));
} else {
printf("**** Illegal Parameter Exception in Config File:\n");
printf("**** \"%s:%s\"\n", configFile->currentParamter, configFile->currentValue);
printf("**** is not a legal Configuration Parameter.\n");
}
}
VPRINTF(("\n** Done Reading Config File **\n\n"));
return 0;
}

View File

@@ -0,0 +1,202 @@
#ifdef _WIN32
#include <winsock.h>
#else
#include <unistd.h> /* close() */
#include <sys/socket.h> /* Socket Functions */
#include <sys/types.h> /* Socket Datatypes */
#include <netinet/in.h> /* IP Datatypes */
#include <arpa/inet.h> /* Address Structs */
#include <sys/ipc.h>
#include <sys/sem.h>
#endif
#include <stdio.h> /* Standard IO */
#include <stdlib.h> /* Standard Lib */
#include <string.h> /* String Libs */
#include "include/sock_lib.h" /* My Functions */
#include "include/settings.h"
#include "../../common/defs.h" /* Definitions */
#include "../../common/http/http.h"
#include "../../common/networking/networking.h"
#include "../../common/shared_memory/shared_memory.h"
/*
* Producer function
*/
void *producer(void* v_port_number) {
int clntSock; /* client sock descriptor. */
int servSock; /* Server sock descriptor. */
unsigned short port_number = *((unsigned short*)v_port_number);
#ifdef _WIN32
DWORD threadID; /* ThreadID from CreateThread().*/
WSADATA wsaData; /* Winsock struct. */
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)/* Winsock 2.0 DLL. */
DieWithError("WSAStartup() failed");
#endif
VPRINTF((" Producer Port %d\n", port_number));
servSock = ContructTCPSocket(port_number);
#if _LOG_
printf("[PRODUCER] Producer Initialized, listening for connections\n");
fflush(stdout);
#endif
for(;;) /* Loop forever */
{
clntSock = AcceptConnection(servSock);
if (clntSock == -1) {
continue;
}
addSocket(clntSock);
}
/* NEVER REACHED... */
}
/*
* Request looks as follows (it is assumed to be this way):
* LOCAL_GET file shmem_buff_num \0
* e.g.
* LOCAL_GET /index.html 2
* LOCAL_GET /files/panda.png 3
*
* Notes:
* - LOCAL_GET does not support directory listing
* - LOCAL_GET will send a simple 404 page
*/
void handleLocal(char *received) {
char* fileName;
int shmem_buff_num, i;
FILE* fp;
if (!received) {
return;
}
//printf("Got: %s\n", received);
for (; *received != ' '; received++);
received++;
for (i=0; received[i] != ' '; i++);
received[i] = '\0';
fileName = received;
shmem_buff_num = atoi(received+i+1);
//printf("Got request for %s on shmem %d\n", fileName, shmem_buff_num);
fp = fopen(fileName, "r");
if (fp == NULL) {
printf("File Not Found, sock_lib.c, %d\n",__LINE__);
// Put 404
return;
}
//printf("Work on: %d\n",shmem_buff_num);
for (;;)
{
char buff[BUFFSIZE-sizeof(int)], *pc;
int temp;
struct sembuf sembuffer;
for (i=0; i<BUFFSIZE-sizeof(int); i++) {
temp = fgetc(fp);
if (temp == EOF) {
//printf("Got EOF\n");
break;
}
buff[i] = temp;
}
// Put stuff on the shared memory segment
sembuffer.sem_num = shmem_buff_num;
sembuffer.sem_op = -1;
sembuffer.sem_flg = 0;
//printf("Waiting for semserv to be 1\n");
semop(semserv, &sembuffer, 1);
// Now semserv[num] == 0
pc = buffptr[shmem_buff_num];
if (i == 0) {
//Last packet
*((int*)pc) = -1;
sembuffer.sem_op = 1;
semop(semprox, &sembuffer, 1);
break;
} else {
*((int*)pc) = i; // Number of Bytes in this packet
pc += sizeof(int);
for (i=0; i<BUFFSIZE-sizeof(int); i++, pc++) {
*pc = buff[i];
}
sembuffer.sem_op = 1;
semop(semprox, &sembuffer, 1);
}
}
//printf("Done on: %d\n",shmem_buff_num);
return;
}
/*
* Consumer Function
*/
void *consumer(void* dumb) {
#if _LOG_
#ifndef _WIN32
printf("[CONSUMER] Consumer %ld started\n", (long)pthread_self());
fflush(stdout);
#endif
#endif
settings_t *settings = (settings_t*)dumb;
for (;;) {
char *received;
int clntSocket;
clntSocket = removeSocket(); //This is where it waits!!!
uber_recv(clntSocket, BUFFERSIZE, "\0", &received);
if (strstr(received, LOCAL_GET) && settings->useSHMEM) {
handleLocal(received);
close(clntSocket);
} else {
http_proto(clntSocket, received);
// Note: The socket isn't being close here.
// This is because I use it as a FILE* and then close it.
// close(clntSocket);
// Don't forget to free()
}
VPRINTF(("Socket %d says goodbye.\n", (int)clntSocket));
fflush(stdout);
free(received);
} //end main loop
return NULL; //never reached
}