Permalink
Browse files

First commit

  • Loading branch information...
Twisol committed Nov 25, 2010
0 parents commit c3f08bda316adc8071e3577f3327dafe25e3c240
Showing with 734 additions and 0 deletions.
  1. +13 −0 Rakefile
  2. +2 −0 ext/.gitignore
  3. +187 −0 ext/Makefile
  4. +140 −0 ext/anachronism.c
  5. +4 −0 ext/extconf.rb
  6. +116 −0 ext/mud_nvt.c
  7. +99 −0 ext/telnet_common.rl
  8. +73 −0 ext/telnet_nvt.h
  9. +100 −0 ext/telnet_nvt.rl
@@ -0,0 +1,13 @@
namespace :ragel do
desc 'View a Ragel state machine visualization'
task :graph, :file do |t, args|
name = args['file']
exec "ragel #{name}.rl -V -p | dot -Tpng > #{name}.png && eog #{name}.png && rm #{name}.png"
end
desc 'Build a Ragel state machine'
task :build, :file do |t, args|
name = args['file']
exec "ragel -C -G2 #{name}.rl"
end
end
@@ -0,0 +1,2 @@
*.o
*.so
@@ -0,0 +1,187 @@
SHELL = /bin/sh
#### Start of system configuration section. ####
srcdir = .
topdir = /home/jonathan/.rvm/rubies/ruby-1.9.2-p0/include/ruby-1.9.1
hdrdir = /home/jonathan/.rvm/rubies/ruby-1.9.2-p0/include/ruby-1.9.1
arch_hdrdir = /home/jonathan/.rvm/rubies/ruby-1.9.2-p0/include/ruby-1.9.1/$(arch)
VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
prefix = $(DESTDIR)/home/jonathan/.rvm/rubies/ruby-1.9.2-p0
rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
exec_prefix = $(prefix)
vendorhdrdir = $(rubyhdrdir)/vendor_ruby
sitehdrdir = $(rubyhdrdir)/site_ruby
rubyhdrdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
vendordir = $(rubylibprefix)/vendor_ruby
sitedir = $(rubylibprefix)/site_ruby
ridir = $(datarootdir)/$(RI_BASE_NAME)
mandir = $(datarootdir)/man
localedir = $(datarootdir)/locale
libdir = $(exec_prefix)/lib
psdir = $(docdir)
pdfdir = $(docdir)
dvidir = $(docdir)
htmldir = $(docdir)
infodir = $(datarootdir)/info
docdir = $(datarootdir)/doc/$(PACKAGE)
oldincludedir = $(DESTDIR)/usr/include
includedir = $(prefix)/include
localstatedir = $(prefix)/var
sharedstatedir = $(prefix)/com
sysconfdir = $(prefix)/etc
datadir = $(datarootdir)
datarootdir = $(prefix)/share
libexecdir = $(exec_prefix)/libexec
sbindir = $(exec_prefix)/sbin
bindir = $(exec_prefix)/bin
rubylibdir = $(rubylibprefix)/$(ruby_version)
archdir = $(rubylibdir)/$(arch)
sitelibdir = $(sitedir)/$(ruby_version)
sitearchdir = $(sitelibdir)/$(sitearch)
vendorlibdir = $(vendordir)/$(ruby_version)
vendorarchdir = $(vendorlibdir)/$(sitearch)
CC = gcc
CXX = g++
LIBRUBY = $(LIBRUBY_SO)
LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
LIBRUBYARG_SHARED = -Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)
LIBRUBYARG_STATIC = -Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static
OUTFLAG = -o
COUTFLAG = -o
RUBY_EXTCONF_H =
cflags = $(optflags) $(debugflags) $(warnflags)
optflags = -O3
debugflags = -ggdb
warnflags = -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long
CFLAGS = -fPIC $(cflags) -fPIC
INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
DEFS = -D_FILE_OFFSET_BITS=64
CPPFLAGS = $(DEFS) $(cppflags)
CXXFLAGS = $(CFLAGS) $(cxxflags)
ldflags = -L. -rdynamic -Wl,-export-dynamic
dldflags =
ARCH_FLAG =
DLDFLAGS = $(ldflags) $(dldflags)
LDSHARED = $(CC) -shared
LDSHAREDXX = $(CXX) -shared
AR = ar
EXEEXT =
RUBY_BASE_NAME = ruby
RUBY_INSTALL_NAME = ruby
RUBY_SO_NAME = ruby
arch = i686-linux
sitearch = $(arch)
ruby_version = 1.9.1
ruby = /home/jonathan/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
RUBY = $(ruby)
RM = rm -f
RM_RF = $(RUBY) -run -e rm -- -rf
RMDIRS = $(RUBY) -run -e rmdir -- -p
MAKEDIRS = /bin/mkdir -p
INSTALL = /usr/bin/install -c
INSTALL_PROG = $(INSTALL) -m 0755
INSTALL_DATA = $(INSTALL) -m 644
COPY = cp
#### End of system configuration section. ####
preload =
libpath = . $(libdir)
LIBPATH = -L. -L$(libdir) -Wl,-R$(libdir)
DEFFILE =
CLEANFILES = mkmf.log
DISTCLEANFILES =
DISTCLEANDIRS =
extout =
extout_prefix =
target_prefix =
LOCAL_LIBS =
LIBS = $(LIBRUBYARG_SHARED) -lpthread -lrt -ldl -lcrypt -lm -lc
SRCS = mud_nvt.c chains.c anachronism.c
OBJS = mud_nvt.o chains.o anachronism.o
TARGET = anachronism
DLLIB = $(TARGET).so
EXTSTATIC =
STATIC_LIB =
BINDIR = $(bindir)
RUBYCOMMONDIR = $(sitedir)$(target_prefix)
RUBYLIBDIR = $(sitelibdir)$(target_prefix)
RUBYARCHDIR = $(sitearchdir)$(target_prefix)
HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
TARGET_SO = $(DLLIB)
CLEANLIBS = $(TARGET).so
CLEANOBJS = *.o *.bak
all: $(DLLIB)
static: $(STATIC_LIB)
.PHONY: all install static install-so install-rb
.PHONY: clean clean-so clean-rb
clean-rb-default::
clean-rb::
clean-so::
clean: clean-so clean-rb-default clean-rb
@-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
distclean-rb-default::
distclean-rb::
distclean-so::
distclean: clean distclean-so distclean-rb-default distclean-rb
@-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
@-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
@-$(RMDIRS) $(DISTCLEANDIRS)
realclean: distclean
install: install-so install-rb
install-so: $(RUBYARCHDIR)
install-so: $(RUBYARCHDIR)/$(DLLIB)
$(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
@-$(MAKEDIRS) $(@D)
$(INSTALL_PROG) $(DLLIB) $(@D)
install-rb: pre-install-rb install-rb-default
install-rb-default: pre-install-rb-default
pre-install-rb: Makefile
pre-install-rb-default: Makefile
$(RUBYARCHDIR):
$(MAKEDIRS) $@
site-install: site-install-so site-install-rb
site-install-so: install-so
site-install-rb: install-rb
.SUFFIXES: .c .m .cc .cxx .cpp .C .o
.cc.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
.cxx.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
.cpp.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
.C.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
.c.o:
$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
$(DLLIB): $(OBJS) Makefile
@-$(RM) $(@)
$(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
$(OBJS): $(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h $(arch_hdrdir)/ruby/config.h
@@ -0,0 +1,140 @@
#include "ruby.h"
#include "telnet_nvt.h"
#define NVT(nvt) ((telnet_nvt*)(nvt))
#define RB_NVT(nvt) ((rb_telnet_nvt*)(nvt))
#define STR2SYM(str) ID2SYM(rb_intern(str))
static VALUE cEvent;
typedef struct rb_telnet_nvt
{
telnet_nvt base;
unsigned char subneg; // boolean
VALUE object;
} rb_telnet_nvt;
static VALUE make_telnet_data(const char* type, VALUE data)
{
VALUE hash = rb_hash_new();
rb_hash_aset(hash, ID2SYM(rb_intern(type)), data);
return hash;
}
static void got_text(telnet_nvt* nvt, telnet_byte chr)
{
const char* type = (RB_NVT(nvt)->subneg) ? "subnegotiation" : "text";
VALUE data = rb_ary_new3(1, rb_str_new(&chr, 1));
rb_yield(make_telnet_data(type, data));
}
static void got_command(telnet_nvt* nvt, telnet_byte command)
{
VALUE data = rb_ary_new3(1, INT2FIX(command));
rb_yield(make_telnet_data("command", data));
}
static void got_option(telnet_nvt* nvt, telnet_byte command, telnet_byte option)
{
const char* strCommand = NULL;
switch (command)
{
case IAC_WILL: strCommand = "WILL"; break;
case IAC_WONT: strCommand = "WONT"; break;
case IAC_DO: strCommand = "DO"; break;
case IAC_DONT: strCommand = "DONT"; break;
default:
assert("Got something other than WILL/WONT/DO/DONT!");
}
VALUE data = rb_ary_new3(2, INT2FIX(option), STR2SYM(strCommand));
rb_yield(make_telnet_data("option", data));
}
static void got_mode(telnet_nvt* nvt, telnet_mode mode, telnet_byte extra)
{
RB_NVT(nvt)->subneg = (mode == TELNET_SUBNEG);
}
static void got_error(telnet_nvt* nvt, int fatal, const char* message, size_t position)
{
VALUE type = STR2SYM(fatal ? "fatal" : "warning");
VALUE data = rb_ary_new3(2, type, rb_str_new_cstr(message));
rb_yield(make_telnet_data("error", data));
}
static VALUE parser_mark(telnet_nvt* nvt)
{
}
static VALUE parser_free(telnet_nvt* nvt)
{
free(nvt);
}
static VALUE parser_allocate(VALUE klass)
{
rb_telnet_nvt* nvt = malloc(sizeof(rb_telnet_nvt));
VALUE object = Data_Wrap_Struct(klass, parser_mark, parser_free, nvt);
NVT(nvt)->text_callback = &got_text;
NVT(nvt)->command_callback = &got_command;
NVT(nvt)->option_callback = &got_option;
NVT(nvt)->mode_callback = &got_mode;
NVT(nvt)->error_callback = &got_error;
nvt->subneg = 0;
nvt->object = object;
return object;
}
static VALUE parser_process(VALUE self, VALUE data)
{
return rb_ary_new3(1, rb_struct_new(cEvent, STR2SYM("text"), data));
/*
telnet_nvt* nvt = NULL;
Data_Get_Struct(self, telnet_nvt, nvt);
return data;
*/
}
void Init_anachronism()
{
VALUE mAnachronism = rb_define_module("Anachronism");
VALUE cParser = rb_define_class_under(mAnachronism, "Parser", rb_cObject);
rb_define_alloc_func(cParser, parser_allocate);
rb_define_method(cParser, "process", parser_process, 1);
cEvent = rb_struct_define("Event", "type", "data", NULL);
rb_define_const(mAnachronism, "Event", cEvent);
}
/*
grammar Telnet
rule protocol
(text | iac_sequence)*
end
rule text
~(iac)
end
rule iac
"\255"
end
rule iac_sequence
iac (command|option|subnegotiation)
end
rule command
"\241" | "\242" | "\243" |
"\244" | "\245" | "\246" |
"\247" | "\248" | "\249"
end
end
*/
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require 'mkmf'
create_makefile('anachronism')
Oops, something went wrong.

0 comments on commit c3f08bd

Please sign in to comment.