Skip to content

Commit

Permalink
start on configuration structs and such
Browse files Browse the repository at this point in the history
  • Loading branch information
pquerna committed Mar 28, 2012
1 parent a31eaff commit 4304108
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 11 deletions.
1 change: 1 addition & 0 deletions spedye.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'src/spedye_init.c',
'src/spedye_master.c',
'src/spedye_worker.c',
'src/spedye_conf.c',
],
'include_dirs': [
'src',
Expand Down
47 changes: 44 additions & 3 deletions src/spedye.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,55 @@

#include "uv.h"

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>

#define SPEDYE_OPENSSL_VERSION_NUMBER 0x0090812fL

typedef enum {
SPEDYE_WORKER_THREADED,
SPEDYE_WORKER_PROCESS,
} spedye_worker_type_e;


typedef struct spedye_vhost_t spedye_vhost_t;

struct spedye_vhost_t {
SSL_CTX *sslctx;
const char *name;
const char *certpath;
const char *keypath;
const char *destaddress;
int destport;
spedye_vhost_t *next;
};

typedef struct spedye_listener_t spedye_listener_t;

struct spedye_listener_t {
const char *address;
int port;
spedye_vhost_t *vhosts;
spedye_listener_t *next;
/* Bellow here are owned by spedye core */
SSL_CTX *sslctx;
};

typedef struct spedye_conn_t {
spedye_vhost_t *vhost;
SSL *ssl;
BIO *bio_read;
BIO *bio_write;
} spedye_conn_t;

typedef struct spedye_conf_t {
int worker_count;
spedye_worker_type_e worker_type;
/* TODO: array of certificates */
const char *certpath;
const char *keypath;
spedye_listener_t *listeners;
} spedye_conf_t;


Expand Down Expand Up @@ -63,6 +101,9 @@ typedef struct spedye_master_t {
void spedye_process_init();
void spedye_process_destroy();

int spedye_conf_create(spedye_conf_t **conf);
void spedye_conf_destroy(spedye_conf_t *conf);

int spedye_master_create(spedye_master_t **m, spedye_conf_t *conf, uv_loop_t *loop);
int spedye_master_run(spedye_master_t *m);
void spedye_master_destroy(spedye_master_t *m);
Expand Down
85 changes: 85 additions & 0 deletions src/spedye_conf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2012 The Spedye Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "spedye.h"

static void
init_blank_config(spedye_conf_t *conf)
{
spedye_vhost_t *vhost = calloc(1, sizeof(spedye_vhost_t));
spedye_listener_t *lrec = calloc(1, sizeof(spedye_listener_t));
conf->listeners = lrec;
lrec->port = 443;
lrec->vhosts = vhost;
}

int
spedye_conf_create(spedye_conf_t **p_conf)
{
spedye_conf_t *conf;

conf = calloc(1, sizeof(spedye_conf_t));

init_blank_config(conf);

*p_conf = conf;

return 0;
}

void
spedye_conf_destroy(spedye_conf_t *conf)
{
spedye_listener_t *lrec;
spedye_listener_t *tmplrec;
spedye_vhost_t *vhost;
spedye_vhost_t *tmpvhost;

for (lrec = conf->listeners; lrec != NULL; lrec = tmplrec) {
tmplrec = lrec->next;
for (vhost = lrec->vhosts; vhost != NULL; vhost = tmpvhost) {
tmpvhost = vhost->next;
if (vhost->sslctx) {
SSL_CTX_free(vhost->sslctx);
}
if (vhost->name) {
free((void*)vhost->name);
}
if (vhost->certpath) {
free((void*)vhost->certpath);
}
if (vhost->keypath) {
free((void*)vhost->keypath);
}
if (vhost->destaddress) {
free((void*)vhost->destaddress);
}
free(vhost);
}

if (lrec->sslctx) {
SSL_CTX_free(lrec->sslctx);
}

if (lrec->address) {
free((void*)lrec->address);
}

free(lrec);
}
}

6 changes: 6 additions & 0 deletions src/spedye_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
#include <openssl/x509.h>
#include <openssl/x509v3.h>

#if !USE_SYSTEM_SSL
#if OPENSSL_VERSION_NUMBER != SPEDYE_OPENSSL_VERSION_NUMBER
#error Invalid OpenSSL version number. Busted Include Paths?
#endif
#endif

static uv_rwlock_t* locks;

static unsigned long
Expand Down
55 changes: 47 additions & 8 deletions src/spedye_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,50 @@

#include "spedye.h"

static int long_arg(const char *sname, const char *lname, int i, int argc, char *argv[], char **arg){
*arg = NULL;

if (strcmp(sname, argv[i]) == 0 ||
strcmp(lname, argv[i]) == 0)
{
if (i + 1 < argc) {
*arg = argv[i+1];
return 1;
}
}

return 0;
}

static int
process_args(spedye_conf_t *conf, int argc, char *argv[])
{
char *arg = NULL;
char *p = NULL;
int i;
/* TODO: parse args */

/* TODO: support configuration file for complex configurations */

for (i = 0; i < argc; i++) {
printf("[%d] = %s\n", i, argv[i]);
if (long_arg("-b", "--bind", i, argc, argv, &arg)) {
i++;

p = strrchr(arg, ':');
if (p) {
conf->listeners->port = atoi(p+1);
*p = '\0';
}

if (strcmp("*", arg) == 0) {
conf->listeners->address = strdup("0.0.0.0");
}
else {
conf->listeners->address = strdup(arg);
}
/* TODO: proper parse IP:port, handle v6 */

continue;
}
}
conf->worker_count = 10;
return 0;
Expand All @@ -42,23 +79,23 @@ process_args(spedye_conf_t *conf, int argc, char *argv[])
int main(int argc, char *argv[])
{
int rv;
spedye_conf_t conf;
spedye_conf_t *conf;
spedye_master_t *master;
uv_loop_t *loop;

memset(&conf, 0, sizeof(spedye_conf_t));

spedye_process_init();

rv = process_args(&conf, argc, argv);
rv = spedye_conf_create(&conf);

rv = process_args(conf, argc, argv);

if (rv) {
return rv;
}

loop = uv_default_loop();

rv = spedye_master_create(&master, &conf, loop);
rv = spedye_master_create(&master, conf, loop);

if (rv) {
return rv;
Expand All @@ -67,11 +104,13 @@ int main(int argc, char *argv[])
spedye_master_run(master);

spedye_master_destroy(master);

/* TOOD: spawn loops/other threads */
/* TOOD: Create listeners */
/* TOOD: handle HTTPS 1:1 -> HTTP */
/* TODO: handle SPDY (m:n) -> HTTP*/

spedye_conf_destroy(conf);

spedye_process_destroy();

return 0;
Expand Down

0 comments on commit 4304108

Please sign in to comment.