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,3 @@
D/src////
/Makefile/1.5/Tue Apr 25 02:31:00 2006//
/l33t_proxy.conf/1.7/Tue Apr 25 02:31:00 2006//

View File

@@ -0,0 +1,3 @@
D/src///////
/Makefile////*///
/l33t_proxy.conf////*///

View File

@@ -0,0 +1,3 @@
D/src///////
/Makefile////*///
/l33t_proxy.conf////*///

View File

@@ -0,0 +1,3 @@
D/src////
/Makefile/1.4/Wed Apr 19 03:54:01 2006//
/l33t_proxy.conf/1.4/Sat Apr 22 20:35:49 2006//

View File

@@ -0,0 +1 @@
CS4210/Project 3/proxy_server

View File

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

View File

@@ -0,0 +1,54 @@
CC = gcc
OPT = -O2 -fomit-frame-pointer
LIBS = -lpthread
TARGET = l33t_proxy
MISC = Makefile
INCDIR = src/include
SRCDIR = src
BINDIR = ../bin/proxy
COMMONDIR = ../common
MAKE = make
RPCDIR = ../jpeg_resizer/rpc_server
JPEGLIB = ../bin/rpc_server/lowres-write.o ../bin/rpc_server/lowres.o ../jpeg_resizer/jpeg-6b/libjpeg.a
all: $(TARGET)
$(TARGET): $(BINDIR)/parser.o $(BINDIR)/proxy_settings.o $(BINDIR)/http.o $(BINDIR)/thread_pool.o $(BINDIR)/networking.o $(BINDIR)/threads.o $(BINDIR)/shared_memory.o $(BINDIR)/jpeg_xdr.o $(BINDIR)/jpeg_clnt.o $(BINDIR)/main.o
$(CC) $(OPT) $(LIBS) -o $(BINDIR)/$(TARGET) $(BINDIR)/*.o $(JPEGLIB)
$(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)/proxy_settings.o: $(SRCDIR)/proxy_settings.c $(INCDIR)/proxy_settings.h
$(CC) $(OPT) -c -o $(BINDIR)/proxy_settings.o $(SRCDIR)/proxy_settings.c
$(BINDIR)/http.o: $(COMMONDIR)/http/http.h $(COMMONDIR)/http/http.c
$(CC) $(OPT) -c -o $(BINDIR)/http.o $(COMMONDIR)/http/http.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)/threads.o: $(SRCDIR)/threads.c $(INCDIR)/threads.h
$(CC) $(OPT) -c -o $(BINDIR)/threads.o $(SRCDIR)/threads.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)/jpeg_clnt.o: $(RPCDIR)/jpeg_clnt.c
$(CC) $(OPT) -c -o $(BINDIR)/jpeg_clnt.o $(RPCDIR)/jpeg_clnt.c
$(BINDIR)/jpeg_xdr.o: $(RPCDIR)/jpeg_xdr.c
$(CC) $(OPT) -c -o $(BINDIR)/jpeg_xdr.o $(RPCDIR)/jpeg_xdr.c
#$(BINDIR)/resize_client.o: $(RPCDIR)/resize_client.c
# $(CC) $(OPT) -c -o $(BINDIR)/resize_client.o $(RPCDIR)/resize_client.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,4 @@
MAX_THREADS:3
PORT_NUMBER:1338

View File

@@ -0,0 +1,5 @@
D/include////
/rpc.c/1.3/Sun Apr 16 00:19:29 2006//
/main.c/1.5/Wed Apr 19 03:54:01 2006//
/proxy_settings.c/1.5/Wed Apr 19 03:54:01 2006//
/threads.c/1.16/Tue Apr 25 02:31:00 2006//

View File

@@ -0,0 +1,5 @@
D/include///////
/rpc.c////*///
/main.c////*///
/proxy_settings.c////*///
/threads.c////*///

View File

@@ -0,0 +1,5 @@
D/include///////
/rpc.c////*///
/main.c////*///
/proxy_settings.c////*///
/threads.c////*///

View File

@@ -0,0 +1,5 @@
D/include////
/rpc.c/1.3/Sun Apr 16 00:19:29 2006//
/main.c/1.5/Wed Apr 19 03:54:01 2006//
/proxy_settings.c/1.5/Wed Apr 19 03:54:01 2006//
/threads.c/1.14/Sun Apr 23 20:49:04 2006//

View File

@@ -0,0 +1 @@
CS4210/Project 3/proxy_server/src

View File

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

View File

@@ -0,0 +1,4 @@
/threads.h/1.3/Sun Apr 2 10:08:48 2006//
/rpc.h/1.1/Sun Apr 16 00:11:42 2006//
/proxy_settings.h/1.5/Wed Apr 19 03:54:01 2006//
D

View File

@@ -0,0 +1,3 @@
/threads.h////*///
/rpc.h////*///
/proxy_settings.h////*///

View File

@@ -0,0 +1,3 @@
/threads.h////*///
/proxy_settings.h////*///
/rpc.h////*///

View File

@@ -0,0 +1,4 @@
/threads.h/1.3/Sun Apr 2 10:08:48 2006//
/proxy_settings.h/1.4/Sun Apr 16 04:05:17 2006//
/rpc.h/1.1/Sun Apr 16 00:11:42 2006//
D

View File

@@ -0,0 +1 @@
CS4210/Project 3/proxy_server/src/include

View File

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

View File

@@ -0,0 +1,24 @@
#ifndef _PROXY_SETTINGS_H_
#define _PROXY_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;
char useRPC;
char **hosts;
int numHosts;
} settings_t;
///////////////
// Functions //
///////////////
int init_settings(settings_t *s, int, char**);
#endif

View File

@@ -0,0 +1,4 @@
#ifndef _RPC_H_
#define _RPC_H_
#endif

View File

@@ -0,0 +1,9 @@
#ifndef _THREADS_H_
#define _THREADS_H_
void *consumer(void*);
void *producer(void* v_port_number);
void *consumerNoNoob(void* dumb);
#endif

View File

@@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include "../../common/defs.h"
#include "../../common/thread_pool/thread_pool.h"
#include "../../common/shared_memory/shared_memory.h"
#include "include/threads.h"
#include "include/proxy_settings.h"
settings_t settings;
/* 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");
detach_shared_memory();
printf("Done!\n");
exit(1);
}
int main(int argc, char** argv) {
int i;
// Mask SIGPIPE
#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
// Initialize proxy server settings
init_settings(&settings, argc, argv);
printf("** Using %d threads\n",settings.MAX_THREADS);
printf("** Using home_dir: %s\n",settings.LOAD_DIR);
printf( (settings.useRPC == 0 ? "** Not " : "** "));
printf( "Using RPC.\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);
attach_shared_memory();
}
if (settings.useRPC) {
printf("*** Using RPC Hosts: \n");
for (i=0; i<settings.numHosts; i++) {
printf("**** host %d: %s\n",i,settings.hosts[i]);
}
}
// Initialize threads
init_thread_pool(&(settings.PORT_NUMBER), settings.MAX_THREADS,
producer, consumerNoNoob, &settings);
// Wait for input
while (getchar() != 'q');
if (settings.useSHMEM) {
catch_int(SIGINT);
}
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,120 @@
#include "include/proxy_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_proxy.conf";
s->MAX_THREADS = 7;
s->PORT_NUMBER = 1338;
s->useSHMEM = 0;
s->useRPC = 0;
}
/*
* Initialize internal server parameters
*/
int init_settings(settings_t *s, int argc, char** argv) {
ConfigFile* configFile;
int i;
VPRINTF(("** Reading Config File **\n\n"));
setDefaults(s);
printf("* Proxy does not support Shared Memory right now\n");
if (argc < 2 || (atoi(argv[1]) == 1 && argc <= 2)) {
printf("Proper Usage: \n");
printf(" %s use_RPC [hosts]\n", argv[0]);
printf(" e.g. ./l33t_proxy 1 127.0.0.1 192.168.0.3\n");
printf(" e.g. ./l33t_proxy 0\n");
exit(1);
}
if (atoi(argv[1]) == 1 && argc > 2) {
s->useRPC = 1;
s->hosts = malloc(sizeof(char*)*(argc-2));
s->numHosts = argc-2;
for (i=2; i<argc; i++) {
s->hosts[i-2] = argv[i];
}
}
/*for (i=0; i<s->numHosts; i++) {
printf("%s\n",s->hosts[i]);
}*/
/* 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(" %s use_shared_memory [configFile]\n", argv[0]);
printf(" e.g. ./l33t_proxy 1\n");
printf(" e.g. ./l33t_proxy 0 l33t_proxy2.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,16 @@
/****************************
* This file contains all that fancy shmancy RPC stuff
* This is SOOO much 3xtr3m3r than Vlad's. You can tell
* because we didn't need to do any of that design crap.
*
* I mean, honestly, who needs that modularization stuff
* when you can just write it so that it works and
* refactor it when modifications are needed.
*
* Oh, sure its cleaner. But...its...got more stuff.
* More stuff is bad, that's what Smalltalk taught me.
*/
#include "include/rpc.h"

View File

@@ -0,0 +1,710 @@
#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 <netdb.h>
#include <stdio.h> /* Standard IO */
#include <stdlib.h> /* Standard Lib */
#include <sys/ipc.h>
#include <sys/sem.h>
#include <string.h> /* String Libs */
#include "include/threads.h" /* My Functions */
#include "include/proxy_settings.h"
#include "../../common/defs.h" /* Definitions */
#include "../../common/http/http.h"
#include "../../common/networking/networking.h"
#include "../../common/shared_memory/shared_memory.h"
#include "../../jpeg_resizer/rpc_server/jpeg.h"
#include <pthread.h>
#define HTTP_REQUEST_SIZE 1024
#define RECV_SIZE 64
#define PROTO "http://"
char *strfindr(char *dest, char *src, char *find, char *replace);
char *itoa(int);
char *strnstr(const char *, const char *, size_t);
/*
* Gcc has a crap linker and I don't feel like making this cleaner
*/
file_out* jpeg_resize_1_proxy(char *host, char *jpeg_file, unsigned int size)
{
CLIENT *clnt;
file_out *result_1;
file_in resize_1_arg;
//Set the argument
{
resize_1_arg.file_in_val = jpeg_file;
resize_1_arg.file_in_len = size;
}
#ifndef DEBUG
clnt = clnt_create(host, JPEGRESIZE, JPEGVER, "tcp");
if (clnt == (CLIENT *) NULL) {
clnt_pcreateerror(host);
exit(1);
}
#endif /* DEBUG */
result_1 = resize_1(&resize_1_arg, clnt);
if (result_1 == (file_out *) NULL) {
clnt_perror(clnt, "call failed");
}
#ifndef DEBUG
clnt_destroy(clnt);
#endif /* DEBUG */
return result_1;
}
/*
* Accepts a client request and returns the data received
*/
char *acceptClientRequest(int clntSocket) {
char* received;
char* temp;
int len, i;
uber_recv(clntSocket, BUFFERSIZE, "\0", &temp);
received = temp;
i=1;
while( len = uber_recv(clntSocket, BUFFERSIZE, "\0", &temp) && 0 > len ) {
received = realloc(received, (len + BUFFERSIZE)*sizeof(char));
memcpy(received+(BUFFERSIZE*i), temp, len);
free(temp);
}
return received;
}
/*
* Dispatch and handle a local request over a shmem thing
*/
void dispatchLocal(char *clntRequest, int clntSocket) {
char *get_local;
int i, length;
int servSocket;
shmemNode* shmemSegment;
get_local = malloc(sizeof(char) * (strlen(LOCAL_GET) + 1) );
strcpy(get_local, LOCAL_GET);
length = strlen(get_local);
get_local[length] = ' '; length++;
clntRequest = strstr(clntRequest, "localhost");
for (; *clntRequest != '/'; clntRequest++);
clntRequest++;
for (; *clntRequest != ' '; length++, clntRequest++) {
get_local = realloc(get_local, sizeof(char)*(length+1));
get_local[length] = *clntRequest;
}
get_local = realloc(get_local, sizeof(char) * (length+2));
get_local[length] = ' '; length++;
shmemSegment = getSharedMemorySegment();
//printf("semIndex: %d\n",shmemSegment->semIndex);
{
char *buff;
buff = itoa(shmemSegment->semIndex);
get_local = realloc(get_local, sizeof(char) * (length+strlen(buff)+1));
for (i=0; buff[i] != '\0'; i++) {
get_local[length+i] = buff[i];
}
get_local[length+i] = buff[i];
free(buff);
}
//printf("get_local: %s\n", get_local);
//printf("length: %d %d\n", length, strlen(get_local));
//printf("..Done\n");
servSocket = createTCPSocket("127.0.0.1", 1337);
if (servSocket < 0) {
return;
}
//Send to server
if (send(servSocket, get_local, strlen(get_local), 0) < 0) {
printf("Send error to server\n");
close(servSocket);
return;
}
// Send stuff Back to the client
{
struct sembuf sembuffer;
//printf("Receiving from server.\n");
// Get semaphore
sembuffer.sem_num = shmemSegment->semIndex;
sembuffer.sem_op = -1;
sembuffer.sem_flg = 0;
for (;;) {
// Wait for semprox to be 1
//printf("Waiting for semprox to be 1\n");
sembuffer.sem_op = -1;
semop(semprox, &sembuffer, 1);
if (*((int*)shmemSegment->buff) == -1) {
// Set semserv to 1
sembuffer.sem_op = 1;
semop(semserv, &sembuffer, 1);
break;
}
// Send back to client
if (send(clntSocket,
shmemSegment->buff+sizeof(int),
*((int*)shmemSegment->buff),
0) < 0)
{
printf("Send Error\n");
}
//printf("Setting semserv to 1\n");
// Set semserv to 1
sembuffer.sem_op = 1;
semop(semserv, &sembuffer, 1);
}
}
releaseSharedMemorySegment(shmemSegment);
}
/*
* Fixed Consumer Function
*/
void *consumerNoNoob(void* dumb) {
settings_t *settings = (settings_t*) dumb;
char isLocal = 0;
char isJPEG = 0;
#if _LOG_
printf("[CONSUMER] Consumer %ld started\n", (long)pthread_self());
fflush(stdout);
#endif
//Get stuff from the client
for (;;isLocal = 0, isJPEG = 0) {
int clntSocket, servSocket;
char *clntRequest = NULL, *servHostName;
struct addrinfo *addressInfo;
clntSocket = removeSocket();
//Receive from Client
{ //1
char buffer[RECV_SIZE];
int i = 1;
while (recv(clntSocket, buffer, RECV_SIZE, 0) > 0) {
//printf("receiving\n");
clntRequest = realloc(clntRequest, (i*RECV_SIZE)*sizeof(char));
memcpy(clntRequest+((i-1)*RECV_SIZE), buffer, RECV_SIZE);
if (strnstr(clntRequest, "\r\n\r\n\0",i*RECV_SIZE)) break; //FIXME: Fix my speed
i++;
}
// if Shared Memory is active, all localhost requests are
// routed to the shared memory modules
if (settings->useSHMEM && strstr(clntRequest, "localhost")) {
dispatchLocal(clntRequest, clntSocket);
close(clntSocket);
continue;
} else if (strstr(clntRequest, "localhost")) {
//We're going to assume that we're running l33t_server
// on port 1337 and thus skip the host resolution...stuff
isLocal = 1;
} else if (settings->useRPC) {
int i;
char * temp = strstr(clntRequest, PROTO);
if (temp) {
for (i=0; temp[i] != ' '; i++);
temp[i] = '\0';
if (strstr(temp,"jpg") || strstr(temp,"jpeg")) {
//Got Request for JPEG
printf("Requesting JPEG: %s\n",temp);
isJPEG = 1;
}
temp[i] = ' ';
}
}
} //1
//Figure out what to get
{ //2
char *request, *temp = NULL;
int i;
request = strstr(clntRequest, PROTO);
if (!request) {
printf("Bad Request on socket: %d\n",clntSocket);
free(clntRequest);
close(clntSocket);
continue;
}
request += strlen(PROTO);
for (i=0; request[i] != '/'; i++) {
temp = realloc(temp, (i+1)*sizeof(char));
temp[i] = request[i];
}
temp = realloc(temp, (i+1)*sizeof(char));
temp[i] = '\0';
//printf("temp: %s\n",temp);
servHostName = temp;
} //2
{ //5
char *temp;
temp = malloc(strlen(PROTO)+strlen(servHostName)+1);
sprintf(temp,"%s%s",PROTO,servHostName);
strfindr(clntRequest, clntRequest, temp, "");
free(temp);
//printf("clntRequest:\n%s\n", clntRequest);
} //5
//Get Website Address
if (!isLocal)
{ //3
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Get the Host Information
if (getaddrinfo(servHostName, "http", &hints, &addressInfo)) {
//Error
free(clntRequest);
free(servHostName);
close(clntSocket);
continue;
}
} //3
//Open Socket to the server
if ( !isLocal)
{ //4
servSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (servSocket < 0) {
free(clntRequest);
free(servHostName);
close(clntSocket);
freeaddrinfo(addressInfo);
continue;
}
//Connect to the server
if (connect(servSocket,
addressInfo->ai_addr, addressInfo->ai_addrlen) < 0)
{
free(clntRequest);
free(servHostName);
close(clntSocket);
close(servSocket);
freeaddrinfo(addressInfo);
continue;
}
} else {
servSocket = createTCPSocket("127.0.0.1",1337);
}
//printf("Connected to server\n");
//Send to server
if (send(servSocket, clntRequest, strlen(clntRequest), 0) < 0) {
printf("Send error to server\n");
goto error;
}
//printf("Sent to server\n");
if (!strstr(clntRequest, "\r\n\r\n\0")) printf("SHIT!!!\n");
//get stuff from server and send back to client
{ //6
char buffer[RECV_SIZE];
ssize_t got;
if (!isJPEG) {
while( (got = recv(servSocket, buffer, RECV_SIZE, 0) ) > 0 ) {
//Send back to client
if (send(clntSocket, buffer, got, 0) < 0) {
printf("Send Error\n");
break;
}
}
} else {
// Get the entire JPEG
char *jpeg_header = NULL, *jpeg_file = NULL;
unsigned int size = 0;
char headerDone = 0;
file_out *result = NULL;
printf("Handling the case for JPEG\n");
// Receive Entire JPEG
while( (got = recv(servSocket, buffer, RECV_SIZE, 0) ) > 0 ) {
//printf("Current Size %d\n",size);
jpeg_file = realloc(jpeg_file, sizeof(char)*(size+got));
memcpy(jpeg_file+size, buffer, got);
size += got;
}
if (!jpeg_file) {
goto error;
}
// Remove the HTTP OK Header, if its there
jpeg_header = jpeg_file;
jpeg_file = strnstr(jpeg_header, "\r\n\r\n", size);
jpeg_file+=4;
// Fix that large JPEG
// On a side note, I should say "TAKE THAT NETZERO"
// they make it sound like doing something like this
// is so amazing that it makes their farts sweet to smell.
// Bunch of damned noobs.
// The way this thing works is that it randomly selects a host
// between hosts 0 and 0
{
char* host = settings->hosts[rand()%settings->numHosts];
result = jpeg_resize_1_proxy(host, jpeg_file, size);
}
free(jpeg_header);
// Send the JPEG to the client
printf("Sending back to client\n");
if (!result || result->file_out_len == 0) {
//DO Nothing
} else if (send(clntSocket,
result->file_out_val,
result->file_out_len, 0) < 0)
{
printf("Send Error\n");
break;
}
//if(result) {
// Free the memory
xdr_free(xdr_file_out, (char*)result);
//}
}
}
error:
free(clntRequest);
free(servHostName);
close(clntSocket);
close(servSocket);
if (!isLocal) freeaddrinfo(addressInfo);
}
} //0
/*
* Produces the connections for the consumers
*/
void *producer(void* v_port_number) {
int clntSock; /* client sock descriptor. */
int servSock; /* Server sock descriptor. */
int i;
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);
} // End Producer Loop
/* NEVER REACHED... */
}
/*
* String Find & Replace
*
* This function works like it sounds. It finds instances of find
* in src and replaces them with replace in dest.
*
*
* Ver 1.1 - Corrected Issue with things like Hello and finding lo
*/
char *strfindr(char *dest, char *src, char *find, char *replace){
/* seems easy enough
* Too bad this one takes the second longest to code of
* all functions in this assignment, beaten only by strpig
*/
size_t i=0,x=0,j=0,a=0,b=0,bNotFound=0;
char *p, *p2;
/* Invalid Param Checks */
if(dest == NULL || src == NULL || find == NULL || replace == NULL){
return NULL;
}
/* Empty String */
if(*find == '\0'){
while(*(src+i) != '\0'){
*(dest+i) = *(src+i);
i++;
}
*(dest+i) = '\0';
return dest;
}
/* Important I'm sure */
p2 = src;
do{
/* The Cheap method to create an inline function */
{
/*
* This block of code finds where the
* the thing to be replaced is located
*/
for(; *(src+a) != '\0'; a++){
if(*(find+b) == '\0'){
break;
}else if(*(src+a) == *(find+b)){
b++;
}else{
if(*(find) != *(src+a)){
b=0;
p2=(src+a+1);
}else{
b=1;
p2=(src+a);
}
}
}
/*
* We didn't find it, oh poop
*/
if(*(src+a) == '\0' && *(find+b) != '\0'){
bNotFound = 1;
}
}
/* Reset b */
b=0;
/* Not found, we can just copy the strings over */
if(bNotFound == 1){
while(*(src+i) != '\0'){
*(dest+x) = *(src+i);
i++;x++;
}
break;
}
/* Also probably important */
p = p2;
/* Keep going until you hit the thang that needs fixin */
while((src+i) != p && *(src+i) != '\0'){
*(dest+x) = *(src+i);
i++;x++;
}
/*
* Time to replace
* Note the two counters
*/
if((src+i) == p){
for(j=0;*(find+j) != '\0';j++,i++);
for(j=0;*(replace+j) != '\0';x++,j++){
*(dest+x) = *(replace+j);
}
}
}while(p2 != NULL);
/* String Terminated */
*(dest+x) = '\0';
return dest;
}
/*
* finds the first occurrence of substring s2 within string s1
* not including '\0'
*
* Return: pointer to start of substring in s1
*
*
* Ver 1.1 - Corrected Issue with things like "Hello" and finding "lo"
* Ver 1.2 - Added to be strnstr since apparently Linux doesn't have it. Peachy.
*/
char *strnstr(const char *s1, const char *s2, size_t len){
char *p;
size_t x,i;
p = (char *)s1;
for(x=0,i=0; *(s1+x) != '\0' && x < len; x++){
if(*(s2+i) == '\0'){
return p;
}else if(*(s1+x) == *(s2+i)){
i++;
}else{
if(*(s2) != *(s1+x)){
i=0;
p = (char *)(s1+x+1);
}else{
i=1;
p = (char *)(s1+x);
}
}
}
return NULL;
}
/*--------------------------------------------------------------------+
| itoa() - manage the sign, compute the string equivalent, and call |
| memcpy(). |
+--------------------------------------------------------------------*/
char *itoa(int value)
{
int count, /* number of characters in string */
i, /* loop control variable */
sign; /* determine if the value is negative */
char *ptr, /* temporary pointer, index into string */
*string, /* return value */
*temp; /* temporary string array */
int INTSIZE = sizeof(int);
count = 0;
if ((sign = value) < 0) /* assign value to sign, if negative */
{ /* keep track and invert value */
value = -value;
count++; /* increment count */
}
/* allocate INTSIZE plus 2 bytes (sign and NULL) */
temp = (char *) malloc(INTSIZE + 2);
if (temp == NULL)
{
return(NULL);
}
memset(temp,'\0', INTSIZE + 2);
string = (char *) malloc(INTSIZE + 2);
if (string == NULL)
{
return(NULL);
}
memset(string,'\0', INTSIZE + 2);
ptr = string; /* set temporary ptr to string */
/*--------------------------------------------------------------------+
| NOTE: This process reverses the order of an integer, ie: |
| value = -1234 equates to: char [4321-] |
| Reorder the values using for {} loop below |
+--------------------------------------------------------------------*/
do {
*temp++ = value % 10 + '0'; /* obtain modulus and or with '0' */
count++; /* increment count, track iterations*/
} while (( value /= 10) >0);
if (sign < 0) /* add '-' when sign is negative */
*temp++ = '-';
*temp-- = '\0'; /* ensure null terminated and point */
/* to last char in array */
/*--------------------------------------------------------------------+
| reorder the resulting char *string: |
| temp - points to the last char in the temporary array |
| ptr - points to the first element in the string array |
+--------------------------------------------------------------------*/
for (i = 0; i < count; i++, temp--, ptr++)
{
memcpy(ptr,temp,sizeof(char));
}
return(string);
}