Skip to content

Commit

Permalink
Request Data Connection Safety
Browse files Browse the repository at this point in the history
md5sum a.out
4e3c201511515682ab53f0fdccd8a9ac  a.out

Requests being processed in the same thread allowed incoming requests to
take over the buffer causing it to be rewritten while another request
was using it. The buffer has been removed and each thread's data now
handles the request instead.

Should look into changing buff into an array of buffs to prevent
concurreny issues there as well.
  • Loading branch information
EdgeCaseBerg committed Jan 13, 2014
1 parent a3d70b7 commit c19d51f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
4 changes: 1 addition & 3 deletions green-serv.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ int green_serv(int argc, const char* argv[]) {
_shared_campaign_id = campaign.id;
(void)_shared_campaign_id; /*http://stackoverflow.com/a/394568/1808164*/

char buff[8000];
bzero(buff,8000);
/*What a silly cast we have to make...*/
BOOT_LOG_STR("Running Network Interface:", "");
run_network(buff,8000,(void*(*)(void*))&doNetWork);
run_network((void*(*)(void*))&doNetWork);
BOOT_LOG_STR("Finished Network Interface.","");
/*Clean Up database connection*/
mysql_library_end();
Expand Down
5 changes: 3 additions & 2 deletions headers/network/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#define METHOD_LENGTH 7

#define FIRSTLINEBUFFSIZE 512
#define THREAD_DATA_MAX_SIZE 16384
#define NUMTHREADS 100
#define DETACHED_THREADS
#undef DETACHED_THREADS /* Using detacthed threads runs the risk of leaving fd's open */
/* Simple struct to contain data to be sent to worker threads */
struct threadData{
char msg[16384];
char msg[THREAD_DATA_MAX_SIZE];
int clientfd;
};

Expand Down Expand Up @@ -64,7 +65,7 @@
*/
void* doNetWork(struct threadData* td);

int run_network(char * buffer, int bufferLength, void*( *func )(void*) );
int run_network(void*( *func )(void*) );



Expand Down
12 changes: 5 additions & 7 deletions src/network/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void stop_server(int signum){
}


int run_network(char * buffer, int bufferLength, void*(*func)(void*)){
int run_network(void*(*func)(void*)){
struct sockaddr_in sockserv,sockclient;
int clientfd;
int socketfd;
Expand Down Expand Up @@ -387,21 +387,20 @@ int run_network(char * buffer, int bufferLength, void*(*func)(void*)){
else
readAmount = 0;
}else{
if(totalRead + readAmount > bufferLength){
if(totalRead + readAmount > THREAD_DATA_MAX_SIZE){
NETWORK_LOG_LEVEL_1("Warning Too much content in request. Possible Truncation");
readAmount = 0;
}else
strncat(buffer, buff ,bufferLength);
strncat(data[i].msg, buff ,THREAD_DATA_MAX_SIZE);
totalRead += readAmount;
}
}
if(totalRead > bufferLength)
if(totalRead > THREAD_DATA_MAX_SIZE)
NETWORK_LOG_LEVEL_1("Warning: Total Read Greater than Buffer Length");
buffer[totalRead] = '\0';
data[i].msg[totalRead] = '\0';


/* A thread pool would be intelligent here */
sprintf(data[i].msg, "%s", buffer);
data[i].clientfd = clientfd;
#ifndef DETACHED_THREADS
pthread_create(&children[i],NULL,func,&data[i]);
Expand All @@ -410,7 +409,6 @@ int run_network(char * buffer, int bufferLength, void*(*func)(void*)){
pthread_create(&children[i],&attr,func,&data[i]);
#endif
bzero(buff,BUFSIZ);
bzero(buffer,bufferLength);
}else{
NETWORK_LOG_LEVEL_1("Connection shutdown.");
NETWORK_LOG_LEVEL_2("Invalid file descriptor from client connection.");
Expand Down

0 comments on commit c19d51f

Please sign in to comment.