Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 1503ea236540d9e950d19379ad5d7d5fb8228db1 @thejh committed May 28, 2012
Showing with 1,029 additions and 0 deletions.
  1. +3 −0 .gitmodules
  2. +17 −0 conf/domains.blacklist
  3. +1 −0 deps/http-parser
  4. +4 −0 src/.gitignore
  5. +12 −0 src/Makefile
  6. +47 −0 src/blacklist.c
  7. +2 −0 src/blacklist.h
  8. +40 −0 src/easy_hashtable.c
  9. +13 −0 src/easy_hashtable.h
  10. +14 −0 src/ev_helpers.c
  11. +3 −0 src/ev_helpers.h
  12. +54 −0 src/headers.c
  13. +12 −0 src/headers.h
  14. +6 −0 src/helpers.c
  15. +19 −0 src/helpers.h
  16. +613 −0 src/main.c
  17. +15 −0 src/memory.c
  18. +4 −0 src/memory.h
  19. +111 −0 src/outstream.c
  20. +39 −0 src/outstream.h
@@ -0,0 +1,3 @@
+[submodule "deps/http-parser"]
+ path = deps/http-parser
+ url = git://github.com/joyent/http-parser.git
@@ -0,0 +1,17 @@
+platform.twitter.com
+platform.linkedin.com
+facebook.net
+chartbeat.net
+crwdcntrl.net
+google-analytics.com
+scorecardresearch.com
+contextly.com
+widgets.twimg.com
+quantserve.com
+doubleclick.net
+atdmt.com
+badge.stumbleupon.com
+api.tweetmeme.com
+statcounter.com
+yadro.ru
+widgets.digg.com
Submodule http-parser added at 2ec48f
@@ -0,0 +1,4 @@
+*.d
+*.o
+core.*
+inceptroxy
@@ -0,0 +1,12 @@
+LIBS = -lev `pkg-config --libs glib-2.0`
+FLAGS = -g -Wall -Werror -Wno-strict-aliasing -Wno-unused-variable -Wno-unused-function -std=c99 -Wno-missing-braces `pkg-config --cflags glib-2.0`
+OBJS = headers.o memory.o main.o ev_helpers.o outstream.o helpers.o easy_hashtable.o blacklist.o
+
+inceptroxy: $(OBJS)
+ $(CC) $(FLAGS) -o inceptroxy $(OBJS) $(LIBS) ../deps/http-parser/http_parser.o
+
+-include $(OBJS:.o=.d)
+
+%.o: %.c
+ gcc $(FLAGS) -c $*.c -o $*.o
+ gcc $(FLAGS) -MM $*.c > $*.d
@@ -0,0 +1,47 @@
+#define _XOPEN_SOURCE 500
+
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+
+char **blacklist = NULL;
+int blacklist_size = 0;
+
+void reload_domain_blacklist() {
+ FILE *f = fopen("../conf/domains.blacklist", "r");
+ char line[1024];
+ blacklist_size = 0;
+ while (fgets(line, 1024, f) != NULL) {
+ if (strlen(line) < 3 || line[0] == '#') continue;
+ blacklist_size++;
+ }
+ blacklist = malloc(sizeof(char *) * blacklist_size);
+ rewind(f);
+ int i = 0;
+ while (fgets(line, 1024, f) != NULL) {
+ if (strlen(line) < 3 || line[0] == '#') continue;
+ char *r_i = strchr(line, '\r');
+ char *n_i = strchr(line, '\n');
+ if (r_i != NULL) *r_i = '\0';
+ if (n_i != NULL) *n_i = '\0';
+ blacklist[i++] = strdup(line);
+ }
+ assert(i == blacklist_size);
+ fclose(f);
+}
+
+int bl_check(char *host) {
+ char predotted_bl_entry[1030];
+ predotted_bl_entry[0] = '.';
+
+ for (int i=0; i<blacklist_size; i++) {
+ if (strcmp(host, blacklist[i]) == 0) return 1;
+
+ strcpy(predotted_bl_entry+1, blacklist[i]);
+ if (memcmp(host + (strlen(host) - strlen(predotted_bl_entry)), predotted_bl_entry, strlen(predotted_bl_entry)+1) == 0) return 1;
+ }
+
+ return 0;
+}
@@ -0,0 +1,2 @@
+void reload_domain_blacklist();
+int bl_check(char *host);
@@ -0,0 +1,40 @@
+#define _XOPEN_SOURCE 500
+
+#include <string.h>
+#include <glib.h>
+#include <stdlib.h>
+#include <assert.h>
+
+
+GHashTable *ht_create() {
+ return g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
+}
+
+void ht_free(GHashTable *ht) {
+ g_hash_table_destroy(ht);
+}
+
+void *ht_lookup_or_insert(GHashTable *ht, char *key, void *value) {
+ void *retval = g_hash_table_lookup(ht, key);
+ if (retval != NULL) return retval;
+ key = strdup(key);
+ g_hash_table_insert(ht, key, value);
+ return retval;
+}
+
+void ht_update(GHashTable *ht, char *key, void *value) {
+ if (value == NULL) {
+ g_hash_table_remove(ht, key);
+ } else {
+ key = strdup(key);
+ g_hash_table_insert(ht, key, value);
+ }
+}
+
+void ht_remove(GHashTable *ht, char *key) {
+ g_hash_table_remove(ht, key);
+}
+
+void *ht_lookup(GHashTable *ht, char *key) {
+ return g_hash_table_lookup(ht, key);
+}
@@ -0,0 +1,13 @@
+typedef struct _GHashTable GHashTable;
+
+GHashTable *ht_create();
+void ht_free(GHashTable *);
+
+// Gives value ownership if retval is NULL.
+void *ht_lookup_or_insert(GHashTable *, char *key, void *value);
+
+void ht_update(GHashTable *, char *key, void *value);
+
+void ht_remove(GHashTable *, char *key);
+
+void *ht_lookup(GHashTable *, char *key);
@@ -0,0 +1,14 @@
+#include "ev_helpers.h"
+
+void alter_ev_io_events(struct ev_io *w, int add, int events_change) {
+ struct ev_loop *loop = ev_default_loop(0);
+ ev_io_stop(loop, w);
+ int events = w->events;
+ if (add) {
+ events |= events_change;
+ } else {
+ events &= ~events_change;
+ }
+ ev_io_set(w, w->fd, events);
+ ev_io_start(loop, w);
+}
@@ -0,0 +1,3 @@
+#include <libev/ev.h>
+
+void alter_ev_io_events(struct ev_io *w, int add, int events_change);
@@ -0,0 +1,54 @@
+#include <string.h>
+#include <stdlib.h>
+
+#include "headers.h"
+#include "memory.h"
+
+void free_headers(struct http_header **header) {
+ struct http_header *h = *header;
+ struct http_header *next;
+ while (h != NULL) {
+ next = h->next;
+ free(h->key);
+ free(h->value);
+ free(h);
+ h = next;
+ }
+ *header = NULL;
+}
+
+#define header (*header_ptr)
+
+void headers_append_key(struct http_header **header_ptr, const char *buf, size_t len) {
+ if (header == NULL || header->value != NULL) {
+ // likely case: new key
+ struct http_header *new_header = safe_malloc(sizeof(struct http_header));
+ new_header->key = safe_malloc(len+1);
+ memcpy(new_header->key, buf, len);
+ new_header->key[len] = '\0';
+ new_header->value = NULL;
+ new_header->next = header;
+ header = new_header; // this is why we need a double-pointer
+ } else {
+ // Oh, we're completing an existing header? well, in this unlikely case, we
+ // can use strlen() and stuff without feeling too bad.
+ size_t old_len = strlen(header->key);
+ header->key = safe_realloc(header->key, old_len + len + 1);
+ memcpy(header->key + old_len, buf, len);
+ header->key[old_len + len] = '\0';
+ }
+}
+
+void headers_append_value(struct http_header **header_ptr, const char *buf, size_t len) {
+ size_t old_len;
+ if (header->value == NULL) {
+ old_len = 0;
+ } else {
+ old_len = strlen(header->value);
+ }
+ header->value = safe_realloc(header->value, old_len + len + 1);
+ memcpy(header->value + old_len, buf, len);
+ header->value[old_len + len] = '\0';
+}
+
+#undef header
@@ -0,0 +1,12 @@
+#include <sys/types.h>
+
+struct http_header {
+ char *key;
+ char *value;
+ struct http_header *next;
+};
+
+void free_headers(struct http_header **header);
+
+void headers_append_key(struct http_header **header_ptr, const char *buf, size_t len);
+void headers_append_value(struct http_header **header_ptr, const char *buf, size_t len);
@@ -0,0 +1,6 @@
+#include <unistd.h>
+#include <fcntl.h>
+
+int unblock_fd(int fd) {
+ return fcntl(fd, F_SETFL, O_NONBLOCK);
+}
@@ -0,0 +1,19 @@
+#ifndef helpers_h
+#define helpers_h
+
+#include <stddef.h>
+
+#define BUF_APPEND(buf, data, len) { memcpy(buf, data, len); buf += len; }
+#define BUF_APPEND_STR(buf, str) BUF_APPEND(buf, str, strlen(str))
+
+// Looks complicated, but should actually be even faster than the `void *user_data` method.
+// Should compile to a fixed-size decrement.
+#define CASTUP(pointer, supertype, member) ( \
+ (supertype *) (((char *) pointer) - offsetof(supertype, member)) \
+ )
+
+#define YADA assert(0/* XXX NOT IMPLEMENTED YET XXX */);
+
+int unblock_fd(int fd);
+
+#endif
Oops, something went wrong.

0 comments on commit 1503ea2

Please sign in to comment.