Skip to content

Commit

Permalink
Patches for 0.0.1 (#6)
Browse files Browse the repository at this point in the history
Fix versioning, add to readme, and fix arg parsing as well as some minor code improvements
  • Loading branch information
Nick Ashley committed Sep 28, 2016
1 parent 07096c5 commit f9a05bb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Araneus
A simple, stateless HTTP server written in C
A simple, stateless echo server written in C

## Compilation
Run `make` to compile

## Usage Example
Run `./araneus -p 80` to run the echo server on port 80
38 changes: 23 additions & 15 deletions src/general.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,39 @@

void process_args(int *argc, char *argv[])
{
port = PORT_HTTP;
port = PORT_ANY;
for (int i=0; i < *argc; i++){
if (argv[i][0]=='-'){
if (argv[i][1]=='h')
print_help(argv[0]);
if (argv[i][1]=='v')
print_version();
if (argv[i][1]=='p')
//printf("\nPort: %s\n", extract_arg(argv, i, 2)); // start off by trying the same argv (but starting with the character after 'p')
port = extract_arg(argv, i, 2, argc); // if the argument doesn't have a number following "-p" (e.g., "-p letters"), then the port is assigned to 0 (i.e., any available port)
switch (argv[i][0]){
case '-':
switch (argv[i][1]) {
case 'h':
print_help(argv[0]);
break;
case 'v':
print_version();
break;
case 'p':
port = extract_arg(argv, i, 2, argc); // if the argument doesn't have a number following "-p" (e.g., "-p letters"), then the port is assigned to 0 (i.e., any available port)
break;
}
break;
}
}
}

void print_help(char *prog)
{
printf("Usage: %s [-h] [-v]\n", prog);
printf("\t-h\tPrint this help message and exit\n");
printf("\t-v\tPrint version information and exit\n");
exit(0);
printf("Usage: %s [-h] [-v] [-p port]\n", prog);
printf("\t[ -h ]\t\tPrint this help message and exit\n");
printf("\t[ -v ]\t\tPrint version information and exit\n");
printf("\t[ -p port ]\tSpecify the port to listen on; the default is 80\n");
exit(EXIT_SUCCESS);
}

void print_version()
{
printf("Araneus Version %hu.%hu\n", VERSION_MAJOR, VERSION_MINOR);
printf("Araneus Version %hu.%hu.%hu\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
exit(EXIT_SUCCESS);
}

/*
Expand All @@ -43,7 +51,7 @@ int extract_arg(char *s[], int i, int j, int *argc)
return strtoul(&s[i][j], NULL, 0);
j++;
}
return 0;
return PORT_ANY;
} else { // argument immediately follows "-p"
return strtoul(&s[i][j], NULL, 0);
}
Expand Down
16 changes: 14 additions & 2 deletions src/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@
#include <stdlib.h>
#include <string.h>

/*
Taken from semver.org:
Given a version number MAJOR.MINOR.PATCH, increment the:
MAJOR version when you make incompatible API changes,
MINOR version when you add functionality in a backwards-compatible manner, and
PATCH version when you make backwards-compatible bug fixes.
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
*/
#define VERSION_MAJOR 0
#define VERSION_MINOR 0
#define VERSION_MINOR 1
#define VERSION_PATCH 1

#define PORT_HTTP 80
#define PORT_ANY 0

short unsigned port;

Expand Down
4 changes: 1 addition & 3 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int network_init(int port)

if (listen(sfd, BACKLOG_LIMIT) == -1)
return network_error_listening;
return 0; // no error
return network_error_none; // no error
}

int network_process()
Expand All @@ -38,8 +38,6 @@ int network_process()
if (message_length == -1) // check to see if actual message_length is larger than specified message_length
return network_error_recieving; // error receiving from remote

printf("Received message of size %d:\n\t'%.*s'\n", message_length, message_length, (char *)message_buffer);

if (send(remote_sfd, message_buffer, message_length, 0) == -1) // send a message to the remote: send(socket, message, message_length, flags);
return network_error_sending; // error sending to remote

Expand Down
3 changes: 1 addition & 2 deletions src/network.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <sys/socket.h> // for opening and connecting to network sockets
#include <sys/types.h> // not required for Linux; may be required for BSD or embedded systems
#include <sys/types.h> // not required for Linux; may be required for BSD or embedded systems; see man 2 bind
#include <netinet/ip.h> // for IPPROTO_*, the socket type
#include <unistd.h> // for closing (socket) file descriptors
#include <stdio.h> // for printing output
#include <stdlib.h> // for malloc/free

#define BACKLOG_LIMIT 10 // the maximum number of connections sitting in the listen queue (before they're accepted)
Expand Down

0 comments on commit f9a05bb

Please sign in to comment.