This commit was generated by cvs2svn to compensate for changes in r2,
which included commits to RCS files with non-trunk default branches.
This commit is contained in:
1 parent
a094587fb0
commit
37e63909c0
243 files changed
+45839
No files matched your search
@@ -0,0 +1,19 @@
|
||||
bin_PROGRAMS = tinyproxy
|
||||
tinyproxy_SOURCES = config.h \
|
||||
buffer.c buffer.h \
|
||||
conns.c conns.h \
|
||||
dnscache.c dnscache.h \
|
||||
log.c log.h \
|
||||
reqs.c reqs.h \
|
||||
sock.c sock.h \
|
||||
tinyproxy.c tinyproxy.h \
|
||||
uri.c uri.h \
|
||||
utils.c utils.h \
|
||||
regexp.h
|
||||
|
||||
tinyproxy_LDADD = @LIBOBJS@
|
||||
|
||||
EXTRA_DIST = gnuregex.c gnuregex.h \
|
||||
filter.c filter.h
|
||||
|
||||
LDADD = @LIBOBJS
|
||||
+340
@@ -0,0 +1,340 @@
|
||||
# Makefile.in generated automatically by automake 1.4 from Makefile.am
|
||||
|
||||
# Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
|
||||
# This Makefile.in is free software; the Free Software Foundation
|
||||
# gives unlimited permission to copy and/or distribute it,
|
||||
# with or without modifications, as long as this notice is preserved.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
|
||||
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
# PARTICULAR PURPOSE.
|
||||
|
||||
|
||||
SHELL = @SHELL@
|
||||
|
||||
srcdir = @srcdir@
|
||||
top_srcdir = @top_srcdir@
|
||||
VPATH = @srcdir@
|
||||
prefix = @prefix@
|
||||
exec_prefix = @exec_prefix@
|
||||
|
||||
bindir = @bindir@
|
||||
sbindir = @sbindir@
|
||||
libexecdir = @libexecdir@
|
||||
datadir = @datadir@
|
||||
sysconfdir = @sysconfdir@
|
||||
sharedstatedir = @sharedstatedir@
|
||||
localstatedir = @localstatedir@
|
||||
libdir = @libdir@
|
||||
infodir = @infodir@
|
||||
mandir = @mandir@
|
||||
includedir = @includedir@
|
||||
oldincludedir = /usr/include
|
||||
|
||||
DESTDIR =
|
||||
|
||||
pkgdatadir = $(datadir)/@PACKAGE@
|
||||
pkglibdir = $(libdir)/@PACKAGE@
|
||||
pkgincludedir = $(includedir)/@PACKAGE@
|
||||
|
||||
top_builddir = ..
|
||||
|
||||
ACLOCAL = @ACLOCAL@
|
||||
AUTOCONF = @AUTOCONF@
|
||||
AUTOMAKE = @AUTOMAKE@
|
||||
AUTOHEADER = @AUTOHEADER@
|
||||
|
||||
INSTALL = @INSTALL@
|
||||
INSTALL_PROGRAM = @INSTALL_PROGRAM@ $(AM_INSTALL_PROGRAM_FLAGS)
|
||||
INSTALL_DATA = @INSTALL_DATA@
|
||||
INSTALL_SCRIPT = @INSTALL_SCRIPT@
|
||||
transform = @program_transform_name@
|
||||
|
||||
NORMAL_INSTALL = :
|
||||
PRE_INSTALL = :
|
||||
POST_INSTALL = :
|
||||
NORMAL_UNINSTALL = :
|
||||
PRE_UNINSTALL = :
|
||||
POST_UNINSTALL = :
|
||||
CC = @CC@
|
||||
CFLAGS = @CFLAGS@ -I. -DHAVE_CONFIG_H
|
||||
LIBOBJS = @LIBOBJS@
|
||||
LIBS = @LIBS@
|
||||
MAKEINFO = @MAKEINFO@
|
||||
PACKAGE = @PACKAGE@
|
||||
VERSION = @VERSION@
|
||||
|
||||
bin_PROGRAMS = tinyproxy
|
||||
tinyproxy_SOURCES = config.h buffer.c buffer.h conns.c conns.h dnscache.c dnscache.h log.c log.h reqs.c reqs.h sock.c sock.h tinyproxy.c tinyproxy.h uri.c uri.h utils.c utils.h regexp.h
|
||||
|
||||
|
||||
tinyproxy_LDADD = @LIBOBJS@
|
||||
|
||||
EXTRA_DIST = gnuregex.c gnuregex.h filter.c filter.h
|
||||
|
||||
|
||||
LDADD = @LIBOBJS
|
||||
mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
|
||||
CONFIG_HEADER = defines.h
|
||||
CONFIG_CLEAN_FILES =
|
||||
PROGRAMS = $(bin_PROGRAMS)
|
||||
|
||||
|
||||
DEFS = @DEFS@ -I. -I$(srcdir) -I.
|
||||
CPPFLAGS = @CPPFLAGS@
|
||||
LDFLAGS = @LDFLAGS@
|
||||
tinyproxy_OBJECTS = buffer.o conns.o dnscache.o log.o reqs.o sock.o \
|
||||
tinyproxy.o uri.o utils.o
|
||||
tinyproxy_DEPENDENCIES = @LIBOBJS@
|
||||
tinyproxy_LDFLAGS =
|
||||
COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
|
||||
CCLD = $(CC)
|
||||
LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@
|
||||
DIST_COMMON = ./stamp-h.in Makefile.am Makefile.in defines.h.in \
|
||||
filter.c gnuregex.c
|
||||
|
||||
|
||||
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
|
||||
|
||||
TAR = tar
|
||||
GZIP_ENV = --best
|
||||
DEP_FILES = .deps/buffer.P .deps/conns.P .deps/dnscache.P \
|
||||
.deps/filter.P .deps/gnuregex.P .deps/log.P .deps/reqs.P .deps/sock.P \
|
||||
.deps/tinyproxy.P .deps/uri.P .deps/utils.P
|
||||
SOURCES = $(tinyproxy_SOURCES)
|
||||
OBJECTS = $(tinyproxy_OBJECTS)
|
||||
|
||||
all: all-redirect
|
||||
.SUFFIXES:
|
||||
.SUFFIXES: .S .c .o .s
|
||||
$(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4)
|
||||
cd $(top_srcdir) && $(AUTOMAKE) --gnu src/Makefile
|
||||
|
||||
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status $(BUILT_SOURCES)
|
||||
cd $(top_builddir) \
|
||||
&& CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status
|
||||
|
||||
|
||||
defines.h: stamp-h
|
||||
@if test ! -f $@; then \
|
||||
rm -f stamp-h; \
|
||||
$(MAKE) stamp-h; \
|
||||
else :; fi
|
||||
stamp-h: $(srcdir)/defines.h.in $(top_builddir)/config.status
|
||||
cd $(top_builddir) \
|
||||
&& CONFIG_FILES= CONFIG_HEADERS=src/defines.h \
|
||||
$(SHELL) ./config.status
|
||||
@echo timestamp > stamp-h 2> /dev/null
|
||||
$(srcdir)/defines.h.in: $(srcdir)/stamp-h.in
|
||||
@if test ! -f $@; then \
|
||||
rm -f $(srcdir)/stamp-h.in; \
|
||||
$(MAKE) $(srcdir)/stamp-h.in; \
|
||||
else :; fi
|
||||
$(srcdir)/stamp-h.in: $(top_srcdir)/configure.in $(ACLOCAL_M4)
|
||||
cd $(top_srcdir) && $(AUTOHEADER)
|
||||
@echo timestamp > $(srcdir)/stamp-h.in 2> /dev/null
|
||||
|
||||
mostlyclean-hdr:
|
||||
|
||||
clean-hdr:
|
||||
|
||||
distclean-hdr:
|
||||
-rm -f defines.h
|
||||
|
||||
maintainer-clean-hdr:
|
||||
|
||||
mostlyclean-binPROGRAMS:
|
||||
|
||||
clean-binPROGRAMS:
|
||||
-test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS)
|
||||
|
||||
distclean-binPROGRAMS:
|
||||
|
||||
maintainer-clean-binPROGRAMS:
|
||||
|
||||
install-binPROGRAMS: $(bin_PROGRAMS)
|
||||
@$(NORMAL_INSTALL)
|
||||
$(mkinstalldirs) $(DESTDIR)$(bindir)
|
||||
@list='$(bin_PROGRAMS)'; for p in $$list; do \
|
||||
if test -f $$p; then \
|
||||
echo " $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`"; \
|
||||
$(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`; \
|
||||
else :; fi; \
|
||||
done
|
||||
|
||||
uninstall-binPROGRAMS:
|
||||
@$(NORMAL_UNINSTALL)
|
||||
list='$(bin_PROGRAMS)'; for p in $$list; do \
|
||||
rm -f $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`; \
|
||||
done
|
||||
|
||||
.s.o:
|
||||
$(COMPILE) -c $<
|
||||
|
||||
.S.o:
|
||||
$(COMPILE) -c $<
|
||||
|
||||
mostlyclean-compile:
|
||||
-rm -f *.o core *.core
|
||||
|
||||
clean-compile:
|
||||
|
||||
distclean-compile:
|
||||
-rm -f *.tab.c
|
||||
|
||||
maintainer-clean-compile:
|
||||
|
||||
tinyproxy: $(tinyproxy_OBJECTS) $(tinyproxy_DEPENDENCIES)
|
||||
@rm -f tinyproxy
|
||||
$(LINK) $(tinyproxy_LDFLAGS) $(tinyproxy_OBJECTS) $(tinyproxy_LDADD) $(LIBS)
|
||||
|
||||
tags: TAGS
|
||||
|
||||
ID: $(HEADERS) $(SOURCES) $(LISP)
|
||||
list='$(SOURCES) $(HEADERS)'; \
|
||||
unique=`for i in $$list; do echo $$i; done | \
|
||||
awk ' { files[$$0] = 1; } \
|
||||
END { for (i in files) print i; }'`; \
|
||||
here=`pwd` && cd $(srcdir) \
|
||||
&& mkid -f$$here/ID $$unique $(LISP)
|
||||
|
||||
TAGS: $(HEADERS) $(SOURCES) defines.h.in $(TAGS_DEPENDENCIES) $(LISP)
|
||||
tags=; \
|
||||
here=`pwd`; \
|
||||
list='$(SOURCES) $(HEADERS)'; \
|
||||
unique=`for i in $$list; do echo $$i; done | \
|
||||
awk ' { files[$$0] = 1; } \
|
||||
END { for (i in files) print i; }'`; \
|
||||
test -z "$(ETAGS_ARGS)defines.h.in$$unique$(LISP)$$tags" \
|
||||
|| (cd $(srcdir) && etags $(ETAGS_ARGS) $$tags defines.h.in $$unique $(LISP) -o $$here/TAGS)
|
||||
|
||||
mostlyclean-tags:
|
||||
|
||||
clean-tags:
|
||||
|
||||
distclean-tags:
|
||||
-rm -f TAGS ID
|
||||
|
||||
maintainer-clean-tags:
|
||||
|
||||
distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
|
||||
|
||||
subdir = src
|
||||
|
||||
distdir: $(DISTFILES)
|
||||
here=`cd $(top_builddir) && pwd`; \
|
||||
top_distdir=`cd $(top_distdir) && pwd`; \
|
||||
distdir=`cd $(distdir) && pwd`; \
|
||||
cd $(top_srcdir) \
|
||||
&& $(AUTOMAKE) --include-deps --build-dir=$$here --srcdir-name=$(top_srcdir) --output-dir=$$top_distdir --gnu doc/Makefile
|
||||
@for file in $(DISTFILES); do \
|
||||
d=$(srcdir); \
|
||||
if test -d $$d/$$file; then \
|
||||
cp -pr $$/$$file $(distdir)/$$file; \
|
||||
else \
|
||||
test -f $(distdir)/$$file \
|
||||
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
|
||||
|| cp -p $$d/$$file $(distdir)/$$file || :; \
|
||||
fi; \
|
||||
done
|
||||
buffer.o: buffer.c defines.h utils.h conns.h tinyproxy.h config.h log.h \
|
||||
buffer.h
|
||||
conns.o: conns.c defines.h config.h log.h utils.h conns.h tinyproxy.h \
|
||||
buffer.h dnscache.h
|
||||
dnscache.o: dnscache.c defines.h utils.h conns.h tinyproxy.h config.h \
|
||||
dnscache.h
|
||||
log.o: log.c defines.h tinyproxy.h config.h log.h
|
||||
reqs.o: reqs.c defines.h config.h tinyproxy.h sock.h buffer.h utils.h \
|
||||
conns.h log.h reqs.h filter.h uri.h regexp.h
|
||||
sock.o: sock.c defines.h tinyproxy.h config.h sock.h buffer.h log.h \
|
||||
utils.h conns.h dnscache.h
|
||||
tinyproxy.o: tinyproxy.c defines.h config.h tinyproxy.h utils.h conns.h \
|
||||
log.h sock.h buffer.h reqs.h filter.h
|
||||
uri.o: uri.c defines.h uri.h utils.h conns.h tinyproxy.h config.h log.h \
|
||||
regexp.h
|
||||
utils.o: utils.c defines.h config.h tinyproxy.h utils.h conns.h log.h \
|
||||
buffer.h
|
||||
|
||||
info-am:
|
||||
info: info-am
|
||||
dvi-am:
|
||||
dvi: dvi-am
|
||||
check-am: all-am
|
||||
check: check-am
|
||||
installcheck-am:
|
||||
installcheck: installcheck-am
|
||||
all-recursive-am: defines.h
|
||||
$(MAKE) $(AM_MAKEFLAGS) all-recursive
|
||||
|
||||
install-exec-am: install-binPROGRAMS
|
||||
install-exec: install-exec-am
|
||||
|
||||
install-data-am:
|
||||
install-data: install-data-am
|
||||
|
||||
install-am: all-am
|
||||
@$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
|
||||
install: install-am
|
||||
uninstall-am: uninstall-binPROGRAMS
|
||||
uninstall: uninstall-am
|
||||
all-am: Makefile $(PROGRAMS) defines.h
|
||||
all-redirect: all-am
|
||||
install-strip:
|
||||
$(MAKE) $(AM_MAKEFLAGS) AM_INSTALL_PROGRAM_FLAGS=-s install
|
||||
installdirs:
|
||||
$(mkinstalldirs) $(DESTDIR)$(bindir)
|
||||
|
||||
|
||||
mostlyclean-generic:
|
||||
|
||||
clean-generic:
|
||||
|
||||
distclean-generic:
|
||||
-rm -f Makefile $(CONFIG_CLEAN_FILES)
|
||||
-rm -f config.cache config.log stamp-h stamp-h[0-9]*
|
||||
|
||||
maintainer-clean-generic:
|
||||
mostlyclean-am: mostlyclean-hdr mostlyclean-binPROGRAMS \
|
||||
mostlyclean-compile mostlyclean-tags mostlyclean-depend \
|
||||
mostlyclean-generic
|
||||
|
||||
mostlyclean: mostlyclean-am
|
||||
|
||||
clean-am: clean-hdr clean-binPROGRAMS clean-compile clean-tags \
|
||||
clean-depend clean-generic mostlyclean-am
|
||||
|
||||
clean: clean-am
|
||||
|
||||
distclean-am: distclean-hdr distclean-binPROGRAMS distclean-compile \
|
||||
distclean-tags distclean-depend distclean-generic \
|
||||
clean-am
|
||||
|
||||
distclean: distclean-am
|
||||
|
||||
maintainer-clean-am: maintainer-clean-hdr maintainer-clean-binPROGRAMS \
|
||||
maintainer-clean-compile maintainer-clean-tags \
|
||||
maintainer-clean-depend maintainer-clean-generic \
|
||||
distclean-am
|
||||
@echo "This command is intended for maintainers to use;"
|
||||
@echo "it deletes files that may require special tools to rebuild."
|
||||
|
||||
maintainer-clean: maintainer-clean-am
|
||||
|
||||
.PHONY: mostlyclean-hdr distclean-hdr clean-hdr maintainer-clean-hdr \
|
||||
mostlyclean-binPROGRAMS distclean-binPROGRAMS clean-binPROGRAMS \
|
||||
maintainer-clean-binPROGRAMS uninstall-binPROGRAMS install-binPROGRAMS \
|
||||
mostlyclean-compile distclean-compile clean-compile \
|
||||
maintainer-clean-compile tags mostlyclean-tags distclean-tags \
|
||||
clean-tags maintainer-clean-tags distdir mostlyclean-depend \
|
||||
distclean-depend clean-depend maintainer-clean-depend info-am info \
|
||||
dvi-am dvi check check-am installcheck-am installcheck all-recursive-am \
|
||||
install-exec-am install-exec install-data-am install-data install-am \
|
||||
install uninstall-am uninstall all-redirect all-am all installdirs \
|
||||
mostlyclean-generic distclean-generic clean-generic \
|
||||
maintainer-clean-generic clean mostlyclean distclean maintainer-clean
|
||||
|
||||
|
||||
# Tell versions [3.59,3.63) of GNU make to not export all variables.
|
||||
# Otherwise a system limit (for SysV at least) may be exceeded.
|
||||
.NOEXPORT:
|
||||
+308
@@ -0,0 +1,308 @@
|
||||
/* $Id: buffer.c,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $
|
||||
*
|
||||
* The buffer used in each connection is a linked list of lines. As the lines
|
||||
* are read in and written out the buffer expands and contracts. Basically,
|
||||
* by using this method we can increase the buffer size dynamicly. However,
|
||||
* we have a hard limit of 64 KB for the size of the buffer. The buffer can be
|
||||
* thought of as a queue were we act on both the head and tail. The various
|
||||
* functions act on each end (the names are taken from what Perl uses to act on
|
||||
* the ends of an array. :)
|
||||
*
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "log.h"
|
||||
#include "tinyproxy.h"
|
||||
#include "buffer.h"
|
||||
|
||||
/*
|
||||
* Take a string of data and a length and make a new line which can be added
|
||||
* to the buffer
|
||||
*/
|
||||
static struct bufline_s *makenewline(unsigned char *data, unsigned int length)
|
||||
{
|
||||
struct bufline_s *newline;
|
||||
|
||||
assert(data);
|
||||
assert(length > 0);
|
||||
|
||||
if (!(newline = xmalloc(sizeof(struct bufline_s))))
|
||||
return NULL;
|
||||
|
||||
newline->string = data;
|
||||
newline->next = NULL;
|
||||
newline->length = length;
|
||||
newline->pos = 0;
|
||||
|
||||
return newline;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a new buffer
|
||||
*/
|
||||
struct buffer_s *new_buffer(void)
|
||||
{
|
||||
struct buffer_s *buffptr;
|
||||
|
||||
if (!(buffptr = xmalloc(sizeof(struct buffer_s))))
|
||||
return NULL;
|
||||
|
||||
buffptr->head = buffptr->tail = NULL;
|
||||
buffptr->size = 0;
|
||||
|
||||
return buffptr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete all the lines in the buffer and the buffer itself
|
||||
*/
|
||||
void delete_buffer(struct buffer_s *buffptr)
|
||||
{
|
||||
struct bufline_s *next;
|
||||
|
||||
assert(buffptr);
|
||||
|
||||
while (buffer_head(buffptr)) {
|
||||
next = buffer_head(buffptr)->next;
|
||||
free_line(buffer_head(buffptr));
|
||||
buffer_head(buffptr) = next;
|
||||
}
|
||||
buffer_head(buffptr) = NULL;
|
||||
buffer_tail(buffptr) = NULL;
|
||||
|
||||
safefree(buffptr);
|
||||
}
|
||||
|
||||
/*
|
||||
* Free the allocated buffer line
|
||||
*/
|
||||
void free_line(struct bufline_s *line)
|
||||
{
|
||||
if (!line)
|
||||
return;
|
||||
|
||||
if (line->string) {
|
||||
safefree(line->string);
|
||||
}
|
||||
|
||||
safefree(line);
|
||||
}
|
||||
|
||||
/*
|
||||
* Push a new line on to the end of the buffer
|
||||
*/
|
||||
struct bufline_s *push_buffer(struct buffer_s *buffptr, unsigned char *data,
|
||||
unsigned int length)
|
||||
{
|
||||
struct bufline_s *newline;
|
||||
|
||||
assert(buffptr);
|
||||
assert(data);
|
||||
assert(length > 0);
|
||||
|
||||
if (!(newline = makenewline(data, length)))
|
||||
return NULL;
|
||||
|
||||
if (!buffer_head(buffptr) && !buffer_tail(buffptr))
|
||||
buffer_head(buffptr) = buffer_tail(buffptr) = newline;
|
||||
else
|
||||
buffer_tail(buffptr) = (buffer_tail(buffptr)->next = newline);
|
||||
|
||||
buffptr->size += length;
|
||||
|
||||
return newline;
|
||||
}
|
||||
|
||||
/*
|
||||
* Pop a buffer line off the end of the buffer
|
||||
*/
|
||||
struct bufline_s *pop_buffer(struct buffer_s *buffptr)
|
||||
{
|
||||
struct bufline_s *line, *newend;
|
||||
|
||||
assert(buffptr);
|
||||
|
||||
if (buffer_head(buffptr) == buffer_tail(buffptr)) {
|
||||
line = buffer_head(buffptr);
|
||||
buffer_head(buffptr) = buffer_tail(buffptr) = NULL;
|
||||
buffptr->size = 0;
|
||||
return line;
|
||||
}
|
||||
|
||||
line = buffer_tail(buffptr);
|
||||
newend = buffer_head(buffptr);
|
||||
|
||||
while (newend->next != line && newend->next)
|
||||
newend = newend->next;
|
||||
|
||||
buffer_tail(buffptr) = newend;
|
||||
buffptr->size -= line->length;
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
/*
|
||||
* Unshift a buffer line from the top of the buffer (meaning add a new line
|
||||
* to the top of the buffer)
|
||||
*/
|
||||
struct bufline_s *unshift_buffer(struct buffer_s *buffptr, unsigned char *data,
|
||||
unsigned int length)
|
||||
{
|
||||
struct bufline_s *newline;
|
||||
|
||||
assert(buffptr);
|
||||
assert(data);
|
||||
assert(length > 0);
|
||||
|
||||
if (!(newline = makenewline(data, length)))
|
||||
return NULL;
|
||||
|
||||
if (!buffer_head(buffptr) && buffer_tail(buffptr)) {
|
||||
buffer_head(buffptr) = buffer_tail(buffptr) = newline;
|
||||
} else {
|
||||
newline->next = buffer_head(buffptr);
|
||||
buffer_head(buffptr) = newline;
|
||||
if (!buffer_tail(buffptr))
|
||||
buffer_tail(buffptr) = newline;
|
||||
}
|
||||
|
||||
buffptr->size += length;
|
||||
|
||||
return newline;
|
||||
}
|
||||
|
||||
/*
|
||||
* Shift a line off the top of the buffer (remove the line from the top of
|
||||
* the buffer)
|
||||
*/
|
||||
struct bufline_s *shift_buffer(struct buffer_s *buffptr)
|
||||
{
|
||||
struct bufline_s *line;
|
||||
|
||||
assert(buffptr);
|
||||
|
||||
if (!buffer_head(buffptr) && !buffer_tail(buffptr)) {
|
||||
line = buffer_head(buffptr);
|
||||
buffer_head(buffptr) = buffer_tail(buffptr) = NULL;
|
||||
buffptr->size = 0;
|
||||
return line;
|
||||
}
|
||||
|
||||
line = buffer_head(buffptr);
|
||||
buffer_head(buffptr) = line->next;
|
||||
|
||||
if (!buffer_head(buffptr))
|
||||
buffer_tail(buffptr) = NULL;
|
||||
|
||||
buffptr->size -= line->length;
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reads the bytes from the socket, and adds them to the buffer.
|
||||
* Takes a connection and returns the number of bytes read.
|
||||
*/
|
||||
int readbuff(int fd, struct buffer_s *buffptr)
|
||||
{
|
||||
int bytesin;
|
||||
unsigned char inbuf[BUFFER];
|
||||
unsigned char *buffer;
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(buffptr);
|
||||
|
||||
bytesin = recv(fd, inbuf, BUFFER, 0);
|
||||
|
||||
if (bytesin > 0) {
|
||||
if (!(buffer = xmalloc(bytesin)))
|
||||
return 0;
|
||||
|
||||
memcpy(buffer, inbuf, bytesin);
|
||||
push_buffer(buffptr, buffer, bytesin);
|
||||
return bytesin;
|
||||
} else if (bytesin == 0) {
|
||||
/* connection was closed by client */
|
||||
return -1;
|
||||
} else {
|
||||
switch (errno) {
|
||||
#ifdef EWOULDBLOCK
|
||||
case EWOULDBLOCK:
|
||||
#else
|
||||
# ifdef EAGAIN
|
||||
case EAGAIN:
|
||||
# endif
|
||||
#endif
|
||||
case EINTR:
|
||||
return 0;
|
||||
case ECONNRESET:
|
||||
return -1;
|
||||
default:
|
||||
log("ERROR readbuff: recv (%s)", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Write the bytes in the buffer to the socket.
|
||||
* Takes a connection and returns the number of bytes written.
|
||||
*/
|
||||
int writebuff(int fd, struct buffer_s *buffptr)
|
||||
{
|
||||
int bytessent;
|
||||
struct bufline_s *line = buffer_head(buffptr);
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(buffptr);
|
||||
|
||||
bytessent = send(fd, line->string + line->pos,
|
||||
(size_t) (line->length - line->pos), 0);
|
||||
|
||||
if (bytessent >= 0) {
|
||||
/* bytes sent, adjust buffer */
|
||||
line->pos += bytessent;
|
||||
if (line->pos == line->length)
|
||||
free_line(shift_buffer(buffptr));
|
||||
return bytessent;
|
||||
} else {
|
||||
switch (errno) {
|
||||
#ifdef EWOULDBLOCK
|
||||
case EWOULDBLOCK:
|
||||
#else
|
||||
# ifdef EAGAIN
|
||||
case EAGAIN:
|
||||
# endif
|
||||
#endif
|
||||
case ENOBUFS:
|
||||
case EINTR:
|
||||
case ENOMEM:
|
||||
return 0;
|
||||
default:
|
||||
log("ERROR writebuff: send (%s)", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/* $Id: buffer.h,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $
|
||||
*
|
||||
* See 'buffer.c' for a detailed description.
|
||||
*
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef __BUFFER_H_
|
||||
#define __BUFFER_H_ 1
|
||||
|
||||
#define MAXBUFFSIZE 20480
|
||||
|
||||
struct bufline_s {
|
||||
unsigned char *string; /* the actual string of data */
|
||||
struct bufline_s *next; /* pointer to next in linked list */
|
||||
unsigned int length; /* length of the string of data */
|
||||
unsigned int pos; /* start sending from this offset */
|
||||
};
|
||||
|
||||
struct buffer_s {
|
||||
struct bufline_s *head; /* top of the buffer */
|
||||
struct bufline_s *tail; /* bottom of the buffer */
|
||||
unsigned long int size; /* total size of the buffer */
|
||||
};
|
||||
|
||||
#define buffer_head(x) (((struct buffer_s *)(x))->head)
|
||||
#define buffer_tail(x) (((struct buffer_s *)(x))->tail)
|
||||
#define buffer_size(x) (((struct buffer_s *)(x))->size)
|
||||
|
||||
/* Create and delete buffers */
|
||||
extern struct buffer_s *new_buffer(void);
|
||||
extern void delete_buffer(struct buffer_s *buffptr);
|
||||
|
||||
/* Operate on the end of the list */
|
||||
extern struct bufline_s *push_buffer(struct buffer_s *buffptr,
|
||||
unsigned char *data, unsigned int length);
|
||||
extern struct bufline_s *pop_buffer(struct buffer_s *buffptr);
|
||||
|
||||
/* Operate on the head of the list */
|
||||
extern struct bufline_s *unshift_buffer(struct buffer_s *buffptr,
|
||||
unsigned char *data,
|
||||
unsigned int length);
|
||||
extern struct bufline_s *shift_buffer(struct buffer_s *buffptr);
|
||||
|
||||
/* Free a buffer line */
|
||||
extern void free_line(struct bufline_s *line);
|
||||
|
||||
/* Read/Write the buffer to a socket */
|
||||
extern int readbuff(int fd, struct buffer_s *buffptr);
|
||||
extern int writebuff(int fd, struct buffer_s *buffptr);
|
||||
|
||||
#endif /* __BUFFER_H_ */
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
/* $Id: config.h,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $
|
||||
*
|
||||
* Contains all the tune-able variables which are used by tinyproxy.
|
||||
* Modifications made to these variables WILL change the default behaviour
|
||||
* of tinyproxy. Please read the comments to better understand what each
|
||||
* variable does.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _CONFIG_H_
|
||||
#define _CONFIG_H_ 1
|
||||
|
||||
/* Change these if you want */
|
||||
|
||||
/* Default log file */
|
||||
#ifndef DEFAULT_LOG
|
||||
#define DEFAULT_LOG "/usr/local/var/log/tinyproxy.log"
|
||||
#endif
|
||||
|
||||
/* Default port tinyproxy listens on */
|
||||
#ifndef DEFAULT_PORT
|
||||
#define DEFAULT_PORT 8080
|
||||
#endif
|
||||
|
||||
/* Default user to change to after */
|
||||
#ifndef DEFAULT_USER
|
||||
#define DEFAULT_USER "nobody"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Define this if you want to have all requests sent to a particular machine.
|
||||
* This is useful if you have another proxy further upstream which you must
|
||||
* send your HTTP requests through.
|
||||
*/
|
||||
#undef UPSTREAM
|
||||
|
||||
#ifdef UPSTREAM
|
||||
#define UPSTREAM_PROXY_NAME "name.of.upstream.machine"
|
||||
#define UPSTREAM_PROXY_PORT 80
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Define this if you want tinyproxy to use /proc/loadavg to determine
|
||||
* system load (Linux only, I think)
|
||||
*/
|
||||
#define USE_PROC
|
||||
|
||||
#ifndef USE_PROC
|
||||
/*
|
||||
* Path to uptime to determin system load. This path doesn't have to be
|
||||
* valid if DEFAULT_CUTOFFLOAD is 0
|
||||
*/
|
||||
#define UPTIME_PATH "/usr/bin/uptime"
|
||||
#endif /* !USE_PROC */
|
||||
|
||||
/*
|
||||
* The default load at which tinyproxy will start refusing connections.
|
||||
* 0 == disabled by default
|
||||
*/
|
||||
#define DEFAULT_CUTOFFLOAD 0
|
||||
|
||||
/*
|
||||
* NOTE: for DEFAULT_STATHOST: this controls remote proxy stats display.
|
||||
* for example, the default DEFAULT_STATHOST of "tinyproxy.stats" will
|
||||
* mean that when you use the proxy to access http://tinyproxy.stats/",
|
||||
* you will be shown the proxy stats. Set this to something obscure
|
||||
* if you don't want random people to be able to see them, or set it to
|
||||
* "" to disable. In the future, I figure maybe some sort of auth
|
||||
* might be desirable, but that would involve a major simplicity
|
||||
* sacrifice.
|
||||
*/
|
||||
|
||||
/* The "hostname" for getting tinyproxy stats. "" = disabled by default */
|
||||
#define DEFAULT_STATHOST "tinyproxy.stats"
|
||||
|
||||
/*
|
||||
* NOTE: change these if you know what you're doing
|
||||
*/
|
||||
#define SOCK_TIMEOUT 5
|
||||
/* Recalculate load avery 30 seconds */
|
||||
#define LOAD_RECALCTIMER 30
|
||||
/* Default HTTP port */
|
||||
#define HTTP_PORT 80
|
||||
/* Every time conncoll is called, nuke connections idle for > 60 seconds */
|
||||
#define STALECONN_TIME (60 * 15)
|
||||
/* Every 100 times through run the garbage collection */
|
||||
#define GARBCOLL_INTERVAL 10
|
||||
|
||||
#endif
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
/* $Id: conns.c,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $
|
||||
*
|
||||
* These functions handle the various stages a connection will go through in
|
||||
* the course of its life in tinyproxy. New connections are initialized and
|
||||
* added to the linked list of active connections. As these connections are
|
||||
* completed, they are closed, and the a garbage collection process removes
|
||||
* them from the list.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sysexits.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <adns.h>
|
||||
|
||||
extern adns_state adns;
|
||||
|
||||
#include "config.h"
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
#include "conns.h"
|
||||
#include "buffer.h"
|
||||
#include "dnscache.h"
|
||||
|
||||
struct conn_s *connections = NULL;
|
||||
|
||||
/*
|
||||
* Add a new connection to the linked list
|
||||
*/
|
||||
struct conn_s *new_conn(int fd)
|
||||
{
|
||||
struct conn_s **rpConnptr = &connections;
|
||||
struct conn_s *connptr = connections;
|
||||
struct conn_s *newconn;
|
||||
|
||||
assert(fd >= 0);
|
||||
|
||||
while (connptr) {
|
||||
rpConnptr = &connptr->next;
|
||||
connptr = connptr->next;
|
||||
}
|
||||
|
||||
if (!(newconn = xmalloc(sizeof(struct conn_s)))) {
|
||||
log("ERROR new_conn: could not allocate memory for conn");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Allocate the new buffer */
|
||||
newconn->cbuffer = NULL;
|
||||
newconn->sbuffer = NULL;
|
||||
if (!(newconn->cbuffer = new_buffer())
|
||||
|| !(newconn->sbuffer = new_buffer())) {
|
||||
log("ERROR new_conn: could not allocate memory for buffer");
|
||||
safefree(newconn->cbuffer);
|
||||
safefree(newconn->sbuffer);
|
||||
|
||||
newconn->next = NULL;
|
||||
safefree(newconn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
newconn->client_fd = fd;
|
||||
newconn->server_fd = -1;
|
||||
newconn->type = NEWCONN;
|
||||
newconn->inittime = newconn->actiontime = time(NULL);
|
||||
|
||||
newconn->clientheader = newconn->serverheader = FALSE;
|
||||
newconn->simple_req = FALSE;
|
||||
|
||||
*rpConnptr = newconn;
|
||||
newconn->next = connptr;
|
||||
|
||||
stats.num_cons++;
|
||||
|
||||
return newconn;
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a connection from the linked list
|
||||
*/
|
||||
int del_conn(struct conn_s *delconn)
|
||||
{
|
||||
struct conn_s **rpConnptr = &connections;
|
||||
struct conn_s *connptr = connections;
|
||||
|
||||
assert(delconn);
|
||||
|
||||
if (delconn->cbuffer) {
|
||||
delete_buffer(delconn->cbuffer);
|
||||
delconn->cbuffer = NULL;
|
||||
}
|
||||
if (delconn->sbuffer) {
|
||||
delete_buffer(delconn->sbuffer);
|
||||
delconn->sbuffer = NULL;
|
||||
}
|
||||
|
||||
close(delconn->client_fd);
|
||||
close(delconn->server_fd);
|
||||
|
||||
while (connptr && (connptr != delconn)) {
|
||||
rpConnptr = &connptr->next;
|
||||
connptr = connptr->next;
|
||||
}
|
||||
|
||||
if (connptr == delconn) {
|
||||
*rpConnptr = delconn->next;
|
||||
safefree(delconn);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for connections that have been idle too long
|
||||
*/
|
||||
void conncoll(void)
|
||||
{
|
||||
struct conn_s *connptr = connections;
|
||||
|
||||
while (connptr) {
|
||||
if (
|
||||
(difftime(time(NULL), connptr->actiontime) >
|
||||
STALECONN_TIME) && connptr->type != FINISHCONN) {
|
||||
connptr->type = FINISHCONN;
|
||||
stats.num_idles++;
|
||||
}
|
||||
connptr = connptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Actually remove all entries in the linked list that have been marked for
|
||||
* deletion.
|
||||
*/
|
||||
void garbcoll(void)
|
||||
{
|
||||
struct conn_s *connptr = connections;
|
||||
struct conn_s *tmp;
|
||||
|
||||
static unsigned int dnscount = 0;
|
||||
|
||||
#ifdef __DEBUG__
|
||||
log("Garbage collecting (%lu)", stats.num_garbage);
|
||||
#endif
|
||||
|
||||
stats.num_garbage++;
|
||||
|
||||
while (connptr) {
|
||||
tmp = connptr->next;
|
||||
if (connptr->type == FINISHCONN) {
|
||||
#ifdef __DEBUG__
|
||||
log("Deleting connection: %p", connptr);
|
||||
#endif
|
||||
del_conn(connptr);
|
||||
}
|
||||
connptr = tmp;
|
||||
}
|
||||
|
||||
if (dnscount++ > DNS_GARBAGE_COL) {
|
||||
dnscount = 0;
|
||||
dnsclean();
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/* $Id: conns.h,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $
|
||||
*
|
||||
* See 'conns.c' for a detailed description.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _CONN_H_
|
||||
#define _CONN_H_ 1
|
||||
|
||||
#include "tinyproxy.h"
|
||||
|
||||
#include <adns.h>
|
||||
|
||||
/* Different connection types */
|
||||
enum conn_type {
|
||||
NEWCONN,
|
||||
WAITCONN,
|
||||
DNS_WAITCONN,
|
||||
RELAYCONN,
|
||||
CLOSINGCONN,
|
||||
FINISHCONN
|
||||
};
|
||||
|
||||
struct conn_s {
|
||||
struct conn_s *next;
|
||||
int client_fd, server_fd;
|
||||
enum conn_type type;
|
||||
struct buffer_s *cbuffer;
|
||||
struct buffer_s *sbuffer;
|
||||
time_t inittime, actiontime;
|
||||
flag clientheader, serverheader;
|
||||
flag simple_req;
|
||||
|
||||
adns_query adns_qu;
|
||||
char *domain;
|
||||
int port_no;
|
||||
};
|
||||
|
||||
extern struct conn_s *connections;
|
||||
|
||||
/* Handle the creation and deletion of connections */
|
||||
extern struct conn_s *new_conn(int fd);
|
||||
extern int del_conn(struct conn_s *connptr);
|
||||
extern void conncoll(void);
|
||||
extern void garbcoll(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,89 @@
|
||||
/* src/defines.h.in. Generated automatically from configure.in by autoheader. */
|
||||
|
||||
/* Define to empty if the keyword does not work. */
|
||||
#undef const
|
||||
|
||||
/* Define if you don't have vprintf but do have _doprnt. */
|
||||
#undef HAVE_DOPRNT
|
||||
|
||||
/* Define if you have the strftime function. */
|
||||
#undef HAVE_STRFTIME
|
||||
|
||||
/* Define if you have the vprintf function. */
|
||||
#undef HAVE_VPRINTF
|
||||
|
||||
/* Define as __inline if that's what the C compiler calls it. */
|
||||
#undef inline
|
||||
|
||||
/* Define as the return type of signal handlers (int or void). */
|
||||
#undef RETSIGTYPE
|
||||
|
||||
/* Define to `unsigned' if <sys/types.h> doesn't define. */
|
||||
#undef size_t
|
||||
|
||||
/* Define if you have the ANSI C header files. */
|
||||
#undef STDC_HEADERS
|
||||
|
||||
/* Define if you can safely include both <sys/time.h> and <time.h>. */
|
||||
#undef TIME_WITH_SYS_TIME
|
||||
|
||||
/*
|
||||
* Define if you want to have the peer's IP address to be included in a
|
||||
* XTinyproxy header sent to the server.
|
||||
*/
|
||||
#undef XTINYPROXY
|
||||
|
||||
/* chris - undef'd here so that they can be set up in the configure script */
|
||||
#undef DEFAULT_LOG
|
||||
#undef DEFAULT_PORT
|
||||
#undef DEFAULT_USER
|
||||
|
||||
/*
|
||||
* Define if you would like to include filtering code.
|
||||
*/
|
||||
#undef FILTER_ENABLE
|
||||
|
||||
/*
|
||||
* Define if you want to use the included GNU regex routine
|
||||
*/
|
||||
#undef USE_GNU_REGEX
|
||||
|
||||
/* Define if you have the regcomp function. */
|
||||
#undef HAVE_REGCOMP
|
||||
|
||||
/* Define if you have the select function. */
|
||||
#undef HAVE_SELECT
|
||||
|
||||
/* Define if you have the socket function. */
|
||||
#undef HAVE_SOCKET
|
||||
|
||||
/* Define if you have the strdup function. */
|
||||
#undef HAVE_STRDUP
|
||||
|
||||
/* Define if you have the strerror function. */
|
||||
#undef HAVE_STRERROR
|
||||
|
||||
/* Define if you have the vsnprintf function. */
|
||||
#undef HAVE_VSNPRINTF
|
||||
|
||||
/* Define if you have the vsyslog function. */
|
||||
#undef HAVE_VSYSLOG
|
||||
|
||||
/* Define if you have the <fcntl.h> header file. */
|
||||
#undef HAVE_FCNTL_H
|
||||
|
||||
/* Define if you have the <sys/time.h> header file. */
|
||||
#undef HAVE_SYS_TIME_H
|
||||
|
||||
/* Define if you have the <syslog.h> header file. */
|
||||
#undef HAVE_SYSLOG_H
|
||||
|
||||
/* Define if you have the <unistd.h> header file. */
|
||||
#undef HAVE_UNISTD_H
|
||||
|
||||
/* Name of package */
|
||||
#undef PACKAGE
|
||||
|
||||
/* Version number of package */
|
||||
#undef VERSION
|
||||
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
/* $Id: dnscache.c,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $
|
||||
*
|
||||
* This is a caching DNS system. When a host name is needed we look it up here
|
||||
* and see if there is already an answer for it. The domains are placed in a
|
||||
* hashed linked list. If the name is not here, then we need to look it up and
|
||||
* add it to the system. This really speeds up the connection to servers since
|
||||
* the DNS name does not need to be looked up each time. It's kind of cool. :)
|
||||
*
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
* Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <netdb.h>
|
||||
#include <time.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "dnscache.h"
|
||||
|
||||
#define HASH_BOX 25
|
||||
#define DNSEXPIRE (5 * 60)
|
||||
|
||||
struct dnscache_s {
|
||||
struct in_addr ipaddr;
|
||||
char *domain;
|
||||
time_t expire;
|
||||
struct dnscache_s *next;
|
||||
};
|
||||
|
||||
struct dnscache_s *cache[HASH_BOX];
|
||||
|
||||
static unsigned int hash(unsigned char *keystr, unsigned int box)
|
||||
{
|
||||
unsigned long hashc = 0;
|
||||
unsigned char ch;
|
||||
|
||||
assert(keystr);
|
||||
assert(box > 0);
|
||||
|
||||
while ((ch = *keystr++))
|
||||
hashc += tolower(ch);
|
||||
|
||||
return hashc % box;
|
||||
}
|
||||
|
||||
int lookup(struct in_addr *addr, char *domain)
|
||||
{
|
||||
unsigned int box = hash(domain, HASH_BOX);
|
||||
struct dnscache_s **rptr = &cache[box];
|
||||
struct dnscache_s *ptr = cache[box];
|
||||
|
||||
assert(addr);
|
||||
assert(domain);
|
||||
|
||||
while (ptr && strcasecmp(ptr->domain, domain)) {
|
||||
rptr = &ptr->next;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
|
||||
if (ptr && !strcasecmp(ptr->domain, domain)) {
|
||||
/* Woohoo... found it. Make sure it hasn't expired */
|
||||
if (difftime(time(NULL), ptr->expire) > DNSEXPIRE) {
|
||||
/* Oops... expired */
|
||||
*rptr = ptr->next;
|
||||
safefree(ptr->domain);
|
||||
safefree(ptr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* chris - added this so that the routine can be used to just
|
||||
* look stuff up.
|
||||
*/
|
||||
if (addr) *addr = ptr->ipaddr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int insert(struct in_addr *addr, char *domain)
|
||||
{
|
||||
unsigned int box = hash(domain, HASH_BOX);
|
||||
struct dnscache_s **rptr = &cache[box];
|
||||
struct dnscache_s *ptr = cache[box];
|
||||
struct dnscache_s *newptr;
|
||||
|
||||
assert(addr);
|
||||
assert(domain);
|
||||
|
||||
while (ptr) {
|
||||
rptr = &ptr->next;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
|
||||
if (!(newptr = xmalloc(sizeof(struct dnscache_s)))) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(newptr->domain = xstrdup(domain))) {
|
||||
safefree(newptr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
newptr->ipaddr = *addr;
|
||||
|
||||
newptr->expire = time(NULL);
|
||||
|
||||
*rptr = newptr;
|
||||
newptr->next = ptr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dnscache(struct in_addr *addr, char *domain)
|
||||
{
|
||||
struct hostent *resolv;
|
||||
|
||||
assert(addr);
|
||||
assert(domain);
|
||||
|
||||
if (inet_aton(domain, (struct in_addr *) addr) != 0)
|
||||
return 0;
|
||||
|
||||
/* Well, we're not dotted-decimal so we need to look it up */
|
||||
if (lookup(addr, domain) == 0)
|
||||
return 0;
|
||||
|
||||
/* Okay, so not in the list... need to actually look it up. */
|
||||
if (!(resolv = gethostbyname(domain)))
|
||||
return -1;
|
||||
|
||||
memcpy(addr, resolv->h_addr_list[0], resolv->h_length);
|
||||
insert(addr, domain);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void dnsdelete(unsigned int c, struct dnscache_s *del)
|
||||
{
|
||||
struct dnscache_s **rptr;
|
||||
struct dnscache_s *ptr;
|
||||
|
||||
assert(c > 0);
|
||||
assert(del);
|
||||
|
||||
rptr = &cache[c];
|
||||
ptr = cache[c];
|
||||
|
||||
while (ptr && (ptr != del)) {
|
||||
rptr = &ptr->next;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
|
||||
if (ptr == del) {
|
||||
*rptr = ptr->next;
|
||||
safefree(ptr->domain);
|
||||
safefree(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
void dnsclean(void)
|
||||
{
|
||||
unsigned int c;
|
||||
struct dnscache_s *ptr, *tmp;
|
||||
|
||||
for (c = 0; c < HASH_BOX; c++) {
|
||||
ptr = cache[c];
|
||||
|
||||
while (ptr) {
|
||||
tmp = ptr->next;
|
||||
|
||||
if (difftime(time(NULL), ptr->expire) > DNSEXPIRE)
|
||||
dnsdelete(c, ptr);
|
||||
|
||||
ptr = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/* $Id: dnscache.h,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $
|
||||
*
|
||||
* See 'dnscache.c' for a detailed description.
|
||||
*
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _DNSCACHE_H_
|
||||
#define _DNSCACHE_H_ 1
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define DNS_GARBAGE_COL 100
|
||||
|
||||
extern int dnscache(struct in_addr *addr, char *domain);
|
||||
extern void dnsclean(void);
|
||||
|
||||
/* chris - Access these from reqs.c because of ADNS. Ugly. */
|
||||
extern int lookup(struct in_addr *addr, char *domain);
|
||||
extern int insert(struct in_addr *addr, char *domain);
|
||||
|
||||
#endif
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
/* $Id: filter.c,v 1.1.1.1 2000-02-16 17:32:18 sdyoung Exp $
|
||||
*
|
||||
* Copyright (c) 1999 George Talusan (gstalusan@uwaterloo.ca)
|
||||
*
|
||||
* A substring of the domain to be filtered goes into the file
|
||||
* pointed at by DEFAULT_FILTER.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "utils.h"
|
||||
#include "tinyproxy.h"
|
||||
#include "filter.h"
|
||||
|
||||
#include "regexp.h"
|
||||
#include <sysexits.h>
|
||||
|
||||
static int err;
|
||||
|
||||
struct filter_list {
|
||||
struct filter_list *next;
|
||||
char *pat;
|
||||
regex_t *cpat;
|
||||
};
|
||||
|
||||
|
||||
static struct filter_list *fl = NULL;
|
||||
static int already_init = 0;
|
||||
|
||||
/* initializes a linked list of strings containing hosts to be filtered */
|
||||
void filter_init(void)
|
||||
{
|
||||
FILE *fd;
|
||||
struct filter_list *p;
|
||||
char buf[255];
|
||||
char *s;
|
||||
|
||||
if (!fl && !already_init) {
|
||||
fd = fopen(config.filter, "r");
|
||||
if (fd) {
|
||||
p = NULL;
|
||||
|
||||
while (fgets(buf, 255, (FILE *) fd)) {
|
||||
s = buf;
|
||||
if (!p) /* head of list */
|
||||
fl = p = (struct filter_list *)
|
||||
xmalloc(sizeof
|
||||
(struct filter_list));
|
||||
else { /* next entry */
|
||||
p->next = (struct filter_list *)
|
||||
xmalloc(sizeof
|
||||
(struct filter_list));
|
||||
p = p->next;
|
||||
}
|
||||
/* initialize space to zero */
|
||||
memset(p, 0, sizeof(struct filter_list));
|
||||
|
||||
/* replace first whitespace with \0 */
|
||||
while (*s++)
|
||||
if (isspace((int) *s))
|
||||
*s = '\0';
|
||||
|
||||
p->pat = xstrdup(buf);
|
||||
p->cpat = xmalloc(sizeof(regex_t));
|
||||
if (
|
||||
(err =
|
||||
regcomp(p->cpat, p->pat,
|
||||
REG_NEWLINE | REG_NOSUB)) != 0) {
|
||||
fprintf(stderr,
|
||||
"Bad regex in %s: %s\n",
|
||||
config.filter, p->pat);
|
||||
exit(EX_DATAERR);
|
||||
}
|
||||
}
|
||||
already_init = 1;
|
||||
fclose(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* unlink the list */
|
||||
void filter_destroy(void)
|
||||
{
|
||||
struct filter_list *p, *q;
|
||||
|
||||
if (already_init) {
|
||||
for (p = q = fl; p; p = q) {
|
||||
regfree(p->cpat);
|
||||
safefree(p->cpat);
|
||||
safefree(p->pat);
|
||||
q = p->next;
|
||||
safefree(p);
|
||||
}
|
||||
fl = NULL;
|
||||
already_init = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* returns 0 if host is not an element of filter list, non-zero otherwise */
|
||||
int filter_host(char *host)
|
||||
{
|
||||
struct filter_list *p;
|
||||
char *s, *port;
|
||||
int result;
|
||||
|
||||
assert(host);
|
||||
|
||||
if (!fl || !already_init)
|
||||
return (0);
|
||||
|
||||
/* strip off the port number */
|
||||
s = xstrdup(host);
|
||||
port = strchr(s, ':');
|
||||
if (port)
|
||||
*port = '\0';
|
||||
|
||||
result = 0;
|
||||
|
||||
for (p = fl; p; p = p->next) {
|
||||
result = !regexec(p->cpat, s, (size_t) 0, (regmatch_t *) 0, 0);
|
||||
|
||||
if (result)
|
||||
break;
|
||||
}
|
||||
safefree(s);
|
||||
return (result);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/* $Id: filter.h,v 1.1.1.1 2000-02-16 17:32:24 sdyoung Exp $
|
||||
*
|
||||
* See 'filter.c' for a detailed description.
|
||||
*
|
||||
* Copyright (c) 1999 George Talusan (gstalusan@uwaterloo.ca)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef __FILTER_H_
|
||||
#define __FILTER_H_ 1
|
||||
|
||||
extern void filter_init(void);
|
||||
extern void filter_destroy(void);
|
||||
extern int filter_host(char *host);
|
||||
|
||||
#endif
|
||||
+5880
File diff suppressed because it is too large.
Load diff
+542
@@ -0,0 +1,542 @@
|
||||
/* Definitions for data structures and routines for the regular
|
||||
expression library, version 0.12.
|
||||
Copyright (C) 1985,89,90,91,92,93,95,96,97,98 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of the GNU C Library. Its master source is NOT part of
|
||||
the C library, however. The master source lives in /gd/gnu/lib.
|
||||
|
||||
The GNU C Library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public License as
|
||||
published by the Free Software Foundation; either version 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
The GNU C Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with the GNU C Library; see the file COPYING.LIB. If not,
|
||||
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
Boston, MA 02111-1307, USA. */
|
||||
|
||||
#ifndef _REGEX_H
|
||||
#define _REGEX_H 1
|
||||
|
||||
/* Allow the use in C++ code. */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* POSIX says that <sys/types.h> must be included (by the caller) before
|
||||
<regex.h>. */
|
||||
|
||||
#if !defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE && defined VMS
|
||||
/* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
|
||||
should be there. */
|
||||
# include <stddef.h>
|
||||
#endif
|
||||
|
||||
/* The following two types have to be signed and unsigned integer type
|
||||
wide enough to hold a value of a pointer. For most ANSI compilers
|
||||
ptrdiff_t and size_t should be likely OK. Still size of these two
|
||||
types is 2 for Microsoft C. Ugh... */
|
||||
typedef long int s_reg_t;
|
||||
typedef unsigned long int active_reg_t;
|
||||
|
||||
/* The following bits are used to determine the regexp syntax we
|
||||
recognize. The set/not-set meanings are chosen so that Emacs syntax
|
||||
remains the value 0. The bits are given in alphabetical order, and
|
||||
the definitions shifted by one from the previous bit; thus, when we
|
||||
add or remove a bit, only one other definition need change. */
|
||||
typedef unsigned long int reg_syntax_t;
|
||||
|
||||
/* If this bit is not set, then \ inside a bracket expression is literal.
|
||||
If set, then such a \ quotes the following character. */
|
||||
#define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)
|
||||
|
||||
/* If this bit is not set, then + and ? are operators, and \+ and \? are
|
||||
literals.
|
||||
If set, then \+ and \? are operators and + and ? are literals. */
|
||||
#define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
|
||||
|
||||
/* If this bit is set, then character classes are supported. They are:
|
||||
[:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
|
||||
[:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
|
||||
If not set, then character classes are not supported. */
|
||||
#define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
|
||||
|
||||
/* If this bit is set, then ^ and $ are always anchors (outside bracket
|
||||
expressions, of course).
|
||||
If this bit is not set, then it depends:
|
||||
^ is an anchor if it is at the beginning of a regular
|
||||
expression or after an open-group or an alternation operator;
|
||||
$ is an anchor if it is at the end of a regular expression, or
|
||||
before a close-group or an alternation operator.
|
||||
|
||||
This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
|
||||
POSIX draft 11.2 says that * etc. in leading positions is undefined.
|
||||
We already implemented a previous draft which made those constructs
|
||||
invalid, though, so we haven't changed the code back. */
|
||||
#define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
|
||||
|
||||
/* If this bit is set, then special characters are always special
|
||||
regardless of where they are in the pattern.
|
||||
If this bit is not set, then special characters are special only in
|
||||
some contexts; otherwise they are ordinary. Specifically,
|
||||
* + ? and intervals are only special when not after the beginning,
|
||||
open-group, or alternation operator. */
|
||||
#define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
|
||||
|
||||
/* If this bit is set, then *, +, ?, and { cannot be first in an re or
|
||||
immediately after an alternation or begin-group operator. */
|
||||
#define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
|
||||
|
||||
/* If this bit is set, then . matches newline.
|
||||
If not set, then it doesn't. */
|
||||
#define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
|
||||
|
||||
/* If this bit is set, then . doesn't match NUL.
|
||||
If not set, then it does. */
|
||||
#define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
|
||||
|
||||
/* If this bit is set, nonmatching lists [^...] do not match newline.
|
||||
If not set, they do. */
|
||||
#define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
|
||||
|
||||
/* If this bit is set, either \{...\} or {...} defines an
|
||||
interval, depending on RE_NO_BK_BRACES.
|
||||
If not set, \{, \}, {, and } are literals. */
|
||||
#define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
|
||||
|
||||
/* If this bit is set, +, ? and | aren't recognized as operators.
|
||||
If not set, they are. */
|
||||
#define RE_LIMITED_OPS (RE_INTERVALS << 1)
|
||||
|
||||
/* If this bit is set, newline is an alternation operator.
|
||||
If not set, newline is literal. */
|
||||
#define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
|
||||
|
||||
/* If this bit is set, then `{...}' defines an interval, and \{ and \}
|
||||
are literals.
|
||||
If not set, then `\{...\}' defines an interval. */
|
||||
#define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
|
||||
|
||||
/* If this bit is set, (...) defines a group, and \( and \) are literals.
|
||||
If not set, \(...\) defines a group, and ( and ) are literals. */
|
||||
#define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
|
||||
|
||||
/* If this bit is set, then \<digit> matches <digit>.
|
||||
If not set, then \<digit> is a back-reference. */
|
||||
#define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
|
||||
|
||||
/* If this bit is set, then | is an alternation operator, and \| is literal.
|
||||
If not set, then \| is an alternation operator, and | is literal. */
|
||||
#define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
|
||||
|
||||
/* If this bit is set, then an ending range point collating higher
|
||||
than the starting range point, as in [z-a], is invalid.
|
||||
If not set, then when ending range point collates higher than the
|
||||
starting range point, the range is ignored. */
|
||||
#define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
|
||||
|
||||
/* If this bit is set, then an unmatched ) is ordinary.
|
||||
If not set, then an unmatched ) is invalid. */
|
||||
#define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
|
||||
|
||||
/* If this bit is set, succeed as soon as we match the whole pattern,
|
||||
without further backtracking. */
|
||||
#define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
|
||||
|
||||
/* If this bit is set, do not process the GNU regex operators.
|
||||
If not set, then the GNU regex operators are recognized. */
|
||||
#define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)
|
||||
|
||||
/* If this bit is set, turn on internal regex debugging.
|
||||
If not set, and debugging was on, turn it off.
|
||||
This only works if regex.c is compiled -DDEBUG.
|
||||
We define this bit always, so that all that's needed to turn on
|
||||
debugging is to recompile regex.c; the calling code can always have
|
||||
this bit set, and it won't affect anything in the normal case. */
|
||||
#define RE_DEBUG (RE_NO_GNU_OPS << 1)
|
||||
|
||||
/* This global variable defines the particular regexp syntax to use (for
|
||||
some interfaces). When a regexp is compiled, the syntax used is
|
||||
stored in the pattern buffer, so changing this does not affect
|
||||
already-compiled regexps. */
|
||||
extern reg_syntax_t re_syntax_options;
|
||||
|
||||
/* Define combinations of the above bits for the standard possibilities.
|
||||
(The [[[ comments delimit what gets put into the Texinfo file, so
|
||||
don't delete them!) */
|
||||
/* [[[begin syntaxes]]] */
|
||||
#define RE_SYNTAX_EMACS 0
|
||||
|
||||
#define RE_SYNTAX_AWK \
|
||||
(RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
|
||||
| RE_NO_BK_PARENS | RE_NO_BK_REFS \
|
||||
| RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
|
||||
| RE_DOT_NEWLINE | RE_CONTEXT_INDEP_ANCHORS \
|
||||
| RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
|
||||
|
||||
#define RE_SYNTAX_GNU_AWK \
|
||||
((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DEBUG) \
|
||||
& ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS))
|
||||
|
||||
#define RE_SYNTAX_POSIX_AWK \
|
||||
(RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \
|
||||
| RE_INTERVALS | RE_NO_GNU_OPS)
|
||||
|
||||
#define RE_SYNTAX_GREP \
|
||||
(RE_BK_PLUS_QM | RE_CHAR_CLASSES \
|
||||
| RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
|
||||
| RE_NEWLINE_ALT)
|
||||
|
||||
#define RE_SYNTAX_EGREP \
|
||||
(RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
|
||||
| RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
|
||||
| RE_NEWLINE_ALT | RE_NO_BK_PARENS \
|
||||
| RE_NO_BK_VBAR)
|
||||
|
||||
#define RE_SYNTAX_POSIX_EGREP \
|
||||
(RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
|
||||
|
||||
/* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
|
||||
#define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
|
||||
|
||||
#define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
|
||||
|
||||
/* Syntax bits common to both basic and extended POSIX regex syntax. */
|
||||
#define _RE_SYNTAX_POSIX_COMMON \
|
||||
(RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
|
||||
| RE_INTERVALS | RE_NO_EMPTY_RANGES)
|
||||
|
||||
#define RE_SYNTAX_POSIX_BASIC \
|
||||
(_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
|
||||
|
||||
/* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
|
||||
RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
|
||||
isn't minimal, since other operators, such as \`, aren't disabled. */
|
||||
#define RE_SYNTAX_POSIX_MINIMAL_BASIC \
|
||||
(_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
|
||||
|
||||
#define RE_SYNTAX_POSIX_EXTENDED \
|
||||
(_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
|
||||
| RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
|
||||
| RE_NO_BK_PARENS | RE_NO_BK_VBAR \
|
||||
| RE_UNMATCHED_RIGHT_PAREN_ORD)
|
||||
|
||||
/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
|
||||
replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */
|
||||
#define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
|
||||
(_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
|
||||
| RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
|
||||
| RE_NO_BK_PARENS | RE_NO_BK_REFS \
|
||||
| RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
|
||||
/* [[[end syntaxes]]] */
|
||||
|
||||
/* Maximum number of duplicates an interval can allow. Some systems
|
||||
(erroneously) define this in other header files, but we want our
|
||||
value, so remove any previous define. */
|
||||
#ifdef RE_DUP_MAX
|
||||
# undef RE_DUP_MAX
|
||||
#endif
|
||||
/* If sizeof(int) == 2, then ((1 << 15) - 1) overflows. */
|
||||
#define RE_DUP_MAX (0x7fff)
|
||||
|
||||
|
||||
/* POSIX `cflags' bits (i.e., information for `regcomp'). */
|
||||
|
||||
/* If this bit is set, then use extended regular expression syntax.
|
||||
If not set, then use basic regular expression syntax. */
|
||||
#define REG_EXTENDED 1
|
||||
|
||||
/* If this bit is set, then ignore case when matching.
|
||||
If not set, then case is significant. */
|
||||
#define REG_ICASE (REG_EXTENDED << 1)
|
||||
|
||||
/* If this bit is set, then anchors do not match at newline
|
||||
characters in the string.
|
||||
If not set, then anchors do match at newlines. */
|
||||
#define REG_NEWLINE (REG_ICASE << 1)
|
||||
|
||||
/* If this bit is set, then report only success or fail in regexec.
|
||||
If not set, then returns differ between not matching and errors. */
|
||||
#define REG_NOSUB (REG_NEWLINE << 1)
|
||||
|
||||
|
||||
/* POSIX `eflags' bits (i.e., information for regexec). */
|
||||
|
||||
/* If this bit is set, then the beginning-of-line operator doesn't match
|
||||
the beginning of the string (presumably because it's not the
|
||||
beginning of a line).
|
||||
If not set, then the beginning-of-line operator does match the
|
||||
beginning of the string. */
|
||||
#define REG_NOTBOL 1
|
||||
|
||||
/* Like REG_NOTBOL, except for the end-of-line. */
|
||||
#define REG_NOTEOL (1 << 1)
|
||||
|
||||
|
||||
/* If any error codes are removed, changed, or added, update the
|
||||
`re_error_msg' table in regex.c. */
|
||||
typedef enum
|
||||
{
|
||||
#ifdef _XOPEN_SOURCE
|
||||
REG_ENOSYS = -1, /* This will never happen for this implementation. */
|
||||
#endif
|
||||
|
||||
REG_NOERROR = 0, /* Success. */
|
||||
REG_NOMATCH, /* Didn't find a match (for regexec). */
|
||||
|
||||
/* POSIX regcomp return error codes. (In the order listed in the
|
||||
standard.) */
|
||||
REG_BADPAT, /* Invalid pattern. */
|
||||
REG_ECOLLATE, /* Not implemented. */
|
||||
REG_ECTYPE, /* Invalid character class name. */
|
||||
REG_EESCAPE, /* Trailing backslash. */
|
||||
REG_ESUBREG, /* Invalid back reference. */
|
||||
REG_EBRACK, /* Unmatched left bracket. */
|
||||
REG_EPAREN, /* Parenthesis imbalance. */
|
||||
REG_EBRACE, /* Unmatched \{. */
|
||||
REG_BADBR, /* Invalid contents of \{\}. */
|
||||
REG_ERANGE, /* Invalid range end. */
|
||||
REG_ESPACE, /* Ran out of memory. */
|
||||
REG_BADRPT, /* No preceding re for repetition op. */
|
||||
|
||||
/* Error codes we've added. */
|
||||
REG_EEND, /* Premature end. */
|
||||
REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
|
||||
REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
|
||||
} reg_errcode_t;
|
||||
|
||||
/* This data structure represents a compiled pattern. Before calling
|
||||
the pattern compiler, the fields `buffer', `allocated', `fastmap',
|
||||
`translate', and `no_sub' can be set. After the pattern has been
|
||||
compiled, the `re_nsub' field is available. All other fields are
|
||||
private to the regex routines. */
|
||||
|
||||
#ifndef RE_TRANSLATE_TYPE
|
||||
# define RE_TRANSLATE_TYPE char *
|
||||
#endif
|
||||
|
||||
struct re_pattern_buffer
|
||||
{
|
||||
/* [[[begin pattern_buffer]]] */
|
||||
/* Space that holds the compiled pattern. It is declared as
|
||||
`unsigned char *' because its elements are
|
||||
sometimes used as array indexes. */
|
||||
unsigned char *buffer;
|
||||
|
||||
/* Number of bytes to which `buffer' points. */
|
||||
unsigned long int allocated;
|
||||
|
||||
/* Number of bytes actually used in `buffer'. */
|
||||
unsigned long int used;
|
||||
|
||||
/* Syntax setting with which the pattern was compiled. */
|
||||
reg_syntax_t syntax;
|
||||
|
||||
/* Pointer to a fastmap, if any, otherwise zero. re_search uses
|
||||
the fastmap, if there is one, to skip over impossible
|
||||
starting points for matches. */
|
||||
char *fastmap;
|
||||
|
||||
/* Either a translate table to apply to all characters before
|
||||
comparing them, or zero for no translation. The translation
|
||||
is applied to a pattern when it is compiled and to a string
|
||||
when it is matched. */
|
||||
RE_TRANSLATE_TYPE translate;
|
||||
|
||||
/* Number of subexpressions found by the compiler. */
|
||||
size_t re_nsub;
|
||||
|
||||
/* Zero if this pattern cannot match the empty string, one else.
|
||||
Well, in truth it's used only in `re_search_2', to see
|
||||
whether or not we should use the fastmap, so we don't set
|
||||
this absolutely perfectly; see `re_compile_fastmap' (the
|
||||
`duplicate' case). */
|
||||
unsigned can_be_null : 1;
|
||||
|
||||
/* If REGS_UNALLOCATED, allocate space in the `regs' structure
|
||||
for `max (RE_NREGS, re_nsub + 1)' groups.
|
||||
If REGS_REALLOCATE, reallocate space if necessary.
|
||||
If REGS_FIXED, use what's there. */
|
||||
#define REGS_UNALLOCATED 0
|
||||
#define REGS_REALLOCATE 1
|
||||
#define REGS_FIXED 2
|
||||
unsigned regs_allocated : 2;
|
||||
|
||||
/* Set to zero when `regex_compile' compiles a pattern; set to one
|
||||
by `re_compile_fastmap' if it updates the fastmap. */
|
||||
unsigned fastmap_accurate : 1;
|
||||
|
||||
/* If set, `re_match_2' does not return information about
|
||||
subexpressions. */
|
||||
unsigned no_sub : 1;
|
||||
|
||||
/* If set, a beginning-of-line anchor doesn't match at the
|
||||
beginning of the string. */
|
||||
unsigned not_bol : 1;
|
||||
|
||||
/* Similarly for an end-of-line anchor. */
|
||||
unsigned not_eol : 1;
|
||||
|
||||
/* If true, an anchor at a newline matches. */
|
||||
unsigned newline_anchor : 1;
|
||||
|
||||
/* [[[end pattern_buffer]]] */
|
||||
};
|
||||
|
||||
typedef struct re_pattern_buffer regex_t;
|
||||
|
||||
/* Type for byte offsets within the string. POSIX mandates this. */
|
||||
typedef int regoff_t;
|
||||
|
||||
|
||||
/* This is the structure we store register match data in. See
|
||||
regex.texinfo for a full description of what registers match. */
|
||||
struct re_registers
|
||||
{
|
||||
unsigned num_regs;
|
||||
regoff_t *start;
|
||||
regoff_t *end;
|
||||
};
|
||||
|
||||
|
||||
/* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
|
||||
`re_match_2' returns information about at least this many registers
|
||||
the first time a `regs' structure is passed. */
|
||||
#ifndef RE_NREGS
|
||||
# define RE_NREGS 30
|
||||
#endif
|
||||
|
||||
|
||||
/* POSIX specification for registers. Aside from the different names than
|
||||
`re_registers', POSIX uses an array of structures, instead of a
|
||||
structure of arrays. */
|
||||
typedef struct
|
||||
{
|
||||
regoff_t rm_so; /* Byte offset from string's start to substring's start. */
|
||||
regoff_t rm_eo; /* Byte offset from string's start to substring's end. */
|
||||
} regmatch_t;
|
||||
|
||||
/* Declarations for routines. */
|
||||
|
||||
/* To avoid duplicating every routine declaration -- once with a
|
||||
prototype (if we are ANSI), and once without (if we aren't) -- we
|
||||
use the following macro to declare argument types. This
|
||||
unfortunately clutters up the declarations a bit, but I think it's
|
||||
worth it. */
|
||||
|
||||
#if __STDC__
|
||||
|
||||
# define _RE_ARGS(args) args
|
||||
|
||||
#else /* not __STDC__ */
|
||||
|
||||
# define _RE_ARGS(args) ()
|
||||
|
||||
#endif /* not __STDC__ */
|
||||
|
||||
/* Sets the current default syntax to SYNTAX, and return the old syntax.
|
||||
You can also simply assign to the `re_syntax_options' variable. */
|
||||
extern reg_syntax_t re_set_syntax _RE_ARGS ((reg_syntax_t syntax));
|
||||
|
||||
/* Compile the regular expression PATTERN, with length LENGTH
|
||||
and syntax given by the global `re_syntax_options', into the buffer
|
||||
BUFFER. Return NULL if successful, and an error string if not. */
|
||||
extern const char *re_compile_pattern
|
||||
_RE_ARGS ((const char *pattern, size_t length,
|
||||
struct re_pattern_buffer *buffer));
|
||||
|
||||
|
||||
/* Compile a fastmap for the compiled pattern in BUFFER; used to
|
||||
accelerate searches. Return 0 if successful and -2 if was an
|
||||
internal error. */
|
||||
extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer));
|
||||
|
||||
|
||||
/* Search in the string STRING (with length LENGTH) for the pattern
|
||||
compiled into BUFFER. Start searching at position START, for RANGE
|
||||
characters. Return the starting position of the match, -1 for no
|
||||
match, or -2 for an internal error. Also return register
|
||||
information in REGS (if REGS and BUFFER->no_sub are nonzero). */
|
||||
extern int re_search
|
||||
_RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
|
||||
int length, int start, int range, struct re_registers *regs));
|
||||
|
||||
|
||||
/* Like `re_search', but search in the concatenation of STRING1 and
|
||||
STRING2. Also, stop searching at index START + STOP. */
|
||||
extern int re_search_2
|
||||
_RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
|
||||
int length1, const char *string2, int length2,
|
||||
int start, int range, struct re_registers *regs, int stop));
|
||||
|
||||
|
||||
/* Like `re_search', but return how many characters in STRING the regexp
|
||||
in BUFFER matched, starting at position START. */
|
||||
extern int re_match
|
||||
_RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
|
||||
int length, int start, struct re_registers *regs));
|
||||
|
||||
|
||||
/* Relates to `re_match' as `re_search_2' relates to `re_search'. */
|
||||
extern int re_match_2
|
||||
_RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
|
||||
int length1, const char *string2, int length2,
|
||||
int start, struct re_registers *regs, int stop));
|
||||
|
||||
|
||||
/* Set REGS to hold NUM_REGS registers, storing them in STARTS and
|
||||
ENDS. Subsequent matches using BUFFER and REGS will use this memory
|
||||
for recording register information. STARTS and ENDS must be
|
||||
allocated with malloc, and must each be at least `NUM_REGS * sizeof
|
||||
(regoff_t)' bytes long.
|
||||
|
||||
If NUM_REGS == 0, then subsequent matches should allocate their own
|
||||
register data.
|
||||
|
||||
Unless this function is called, the first search or match using
|
||||
PATTERN_BUFFER will allocate its own register data, without
|
||||
freeing the old data. */
|
||||
extern void re_set_registers
|
||||
_RE_ARGS ((struct re_pattern_buffer *buffer, struct re_registers *regs,
|
||||
unsigned num_regs, regoff_t *starts, regoff_t *ends));
|
||||
|
||||
#if defined _REGEX_RE_COMP || defined _LIBC
|
||||
# ifndef _CRAY
|
||||
/* 4.2 bsd compatibility. */
|
||||
extern char *re_comp _RE_ARGS ((const char *));
|
||||
extern int re_exec _RE_ARGS ((const char *));
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* POSIX compatibility. */
|
||||
extern int regcomp _RE_ARGS ((regex_t *__preg, const char *__pattern,
|
||||
int __cflags));
|
||||
|
||||
extern int regexec _RE_ARGS ((const regex_t *__preg,
|
||||
const char *__string, size_t __nmatch,
|
||||
regmatch_t __pmatch[], int __eflags));
|
||||
|
||||
extern size_t regerror _RE_ARGS ((int __errcode, const regex_t *__preg,
|
||||
char *__errbuf, size_t __errbuf_size));
|
||||
|
||||
extern void regfree _RE_ARGS ((regex_t *__preg));
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* C++ */
|
||||
|
||||
#endif /* regex.h */
|
||||
|
||||
/*
|
||||
Local variables:
|
||||
make-backup-files: t
|
||||
version-control: t
|
||||
trim-versions-without-asking: nil
|
||||
End:
|
||||
*/
|
||||
@@ -0,0 +1,90 @@
|
||||
/* $Id: log.c,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $
|
||||
*
|
||||
* Logs the various messages which tinyproxy produces to either a log file or
|
||||
* the syslog daemon. Not much to it...
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* log.c - For the manipulation of log files.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
#include <syslog.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "tinyproxy.h"
|
||||
#include "log.h"
|
||||
|
||||
#define LENGTH 16
|
||||
|
||||
/*
|
||||
* This routine logs messages to either the log file or the syslog function.
|
||||
*/
|
||||
void log(char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
time_t nowtime;
|
||||
FILE *cf;
|
||||
|
||||
#if defined(HAVE_SYSLOG_H) && !defined(HAVE_VSYSLOG_H)
|
||||
static char str[800];
|
||||
#endif
|
||||
static char time_string[LENGTH];
|
||||
|
||||
assert(fmt);
|
||||
|
||||
va_start(args, fmt);
|
||||
|
||||
#ifdef HAVE_SYSLOG_H
|
||||
if (config.syslog == FALSE) {
|
||||
#endif
|
||||
nowtime = time(NULL);
|
||||
/* Format is month day hour:minute:second (24 time) */
|
||||
strftime(time_string, LENGTH, "%b %d %H:%M:%S",
|
||||
localtime(&nowtime));
|
||||
|
||||
if (!(cf = config.logf))
|
||||
cf = stderr;
|
||||
|
||||
fprintf(cf, "%s [%d]: ", time_string, getpid());
|
||||
vfprintf(cf, fmt, args);
|
||||
fprintf(cf, "\n");
|
||||
fflush(cf);
|
||||
#ifdef HAVE_SYSLOG_H
|
||||
} else {
|
||||
# ifdef HAVE_VSYSLOG_H
|
||||
vsyslog(LOG_INFO, fmt, args);
|
||||
# else
|
||||
# ifdef HAVE_VSNPRINTF
|
||||
vsnprintf(str, 800, fmt, args);
|
||||
# else
|
||||
# ifdef HAVE_VPRINTF
|
||||
vsprintf(str, fmt, args);
|
||||
# endif
|
||||
# endif
|
||||
syslog(LOG_INFO, str);
|
||||
# endif
|
||||
}
|
||||
#endif
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/* $Id: log.h,v 1.1.1.1 2000-02-16 17:32:22 sdyoung Exp $
|
||||
*
|
||||
* See 'log.c' for a detailed description.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _LOG_H_
|
||||
#define _LOG_H_ 1
|
||||
|
||||
extern void log(char *fmt, ...);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
/* $Id: regexp.h,v 1.1.1.1 2000-02-16 17:32:24 sdyoung Exp $
|
||||
*
|
||||
* We need this little header to help distinguish whether to use the REGEX
|
||||
* library installed in the system, or to include our own version (the GNU
|
||||
* version to be exact.)
|
||||
*
|
||||
* Copyright (C) 2000 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef USE_GNU_REGEX
|
||||
# include "gnuregex.h"
|
||||
#else
|
||||
# include <regex.h>
|
||||
#endif
|
||||
+832
@@ -0,0 +1,832 @@
|
||||
/* $Id: reqs.c,v 1.1.1.1 2000-02-16 17:32:23 sdyoung Exp $
|
||||
*
|
||||
* This is where all the work in tinyproxy is actually done. Incoming
|
||||
* connections are added to the active list of connections and then the header
|
||||
* is processed to determine what is being requested. tinyproxy then connects
|
||||
* to the remote server and begins to relay the bytes back and forth between
|
||||
* the client and the remote server. Basically, we sit there and sling bytes
|
||||
* back and forth. Optionally, we can weed out headers we do not want to send,
|
||||
* and also add a header of our own.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
* Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sysexits.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "tinyproxy.h"
|
||||
#include "sock.h"
|
||||
#include "utils.h"
|
||||
#include "conns.h"
|
||||
#include "log.h"
|
||||
#include "reqs.h"
|
||||
#include "buffer.h"
|
||||
#include "filter.h"
|
||||
#include "uri.h"
|
||||
#include "regexp.h"
|
||||
|
||||
/* chris - for asynchronous DNS */
|
||||
#include "dnscache.h"
|
||||
#include <adns.h>
|
||||
extern adns_state adns;
|
||||
|
||||
#ifdef XTINYPROXY
|
||||
|
||||
static void Add_XTinyproxy_Header(struct conn_s *connptr)
|
||||
{
|
||||
char *header_line;
|
||||
char ipaddr[PEER_IP_LENGTH];
|
||||
int length;
|
||||
|
||||
assert(connptr);
|
||||
|
||||
if (!(header_line = xmalloc(sizeof(char) * 32)))
|
||||
return;
|
||||
|
||||
length = sprintf(header_line, "X-Tinyproxy: %s\r\n",
|
||||
getpeer_ip(connptr->client_fd, ipaddr));
|
||||
|
||||
unshift_buffer(connptr->cbuffer, header_line, length);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#define HTTPPATTERN "^([a-z]+)[ \t]+([^ \t]+)([ \t]+(HTTP/[0-9]+\\.[0-9]+))?"
|
||||
#define NMATCH 4
|
||||
#define METHOD_IND 1
|
||||
#define URI_IND 2
|
||||
#define VERSION_MARK 3
|
||||
#define VERSION_IND 4
|
||||
|
||||
#define HTTP400ERROR "Unrecognizable request. Only HTTP is allowed."
|
||||
#define HTTP500ERROR "Unable to connect to remote server."
|
||||
#define HTTP503ERROR "Internal server error."
|
||||
|
||||
/*
|
||||
* Parse a client HTTP request and then establish connection.
|
||||
*/
|
||||
static int clientreq(struct conn_s *connptr)
|
||||
{
|
||||
URI *uri = NULL;
|
||||
char *inbuf, *buffer, *request, *port;
|
||||
char *inbuf_ptr;
|
||||
|
||||
regex_t preg;
|
||||
regmatch_t pmatch[NMATCH];
|
||||
|
||||
long len;
|
||||
int fd, port_no;
|
||||
|
||||
char peer_ipaddr[PEER_IP_LENGTH];
|
||||
|
||||
assert(connptr);
|
||||
|
||||
getpeer_ip(connptr->client_fd, peer_ipaddr);
|
||||
/* chris - getpeer_string could block, so for the moment take it out */
|
||||
|
||||
if ((len
|
||||
= readline(connptr->client_fd, connptr->cbuffer, &inbuf)) <= 0) {
|
||||
return len;
|
||||
}
|
||||
|
||||
inbuf_ptr = inbuf + len - 1;
|
||||
while (*inbuf_ptr == '\r' || *inbuf_ptr == '\n')
|
||||
*inbuf_ptr-- = '\0';
|
||||
|
||||
/* Log the incoming connection */
|
||||
if (!config.restricted) {
|
||||
log("Connect: %s", peer_ipaddr);
|
||||
log("Request: %s", inbuf);
|
||||
}
|
||||
|
||||
if (regcomp(&preg, HTTPPATTERN, REG_EXTENDED | REG_ICASE) != 0) {
|
||||
log("ERROR clientreq: regcomp");
|
||||
return 0;
|
||||
}
|
||||
if (regexec(&preg, inbuf, NMATCH, pmatch, 0) != 0) {
|
||||
log("ERROR clientreq: regexec");
|
||||
regfree(&preg);
|
||||
return -1;
|
||||
}
|
||||
regfree(&preg);
|
||||
|
||||
if (pmatch[VERSION_MARK].rm_so == -1)
|
||||
connptr->simple_req = TRUE;
|
||||
|
||||
if (pmatch[METHOD_IND].rm_so == -1 || pmatch[URI_IND].rm_so == -1) {
|
||||
log("ERROR clientreq: Incomplete line from %s (%s)",
|
||||
peer_ipaddr, inbuf);
|
||||
httperr(connptr, 400, HTTP400ERROR);
|
||||
goto COMMON_EXIT;
|
||||
}
|
||||
|
||||
len = pmatch[URI_IND].rm_eo - pmatch[URI_IND].rm_so;
|
||||
if (!(buffer = xmalloc(len + 1))) {
|
||||
log("ERROR clientreq: Cannot allocate buffer for request from %s",
|
||||
peer_ipaddr);
|
||||
httperr(connptr, 503, HTTP503ERROR);
|
||||
goto COMMON_EXIT;
|
||||
}
|
||||
memcpy(buffer, inbuf + pmatch[URI_IND].rm_so, len);
|
||||
buffer[len] = '\0';
|
||||
if (!(uri = explode_uri(buffer))) {
|
||||
safefree(buffer);
|
||||
log("ERROR clientreq: Problem with explode_uri");
|
||||
httperr(connptr, 503, HTTP503ERROR);
|
||||
goto COMMON_EXIT;
|
||||
}
|
||||
safefree(buffer);
|
||||
|
||||
if (strcasecmp(uri->scheme, "http") != 0) {
|
||||
char *error_string = xmalloc(strlen(uri->scheme) + 64);
|
||||
sprintf(error_string, "Invalid scheme (%s). Only HTTP is allowed.",
|
||||
uri->scheme);
|
||||
httperr(connptr, 400, error_string);
|
||||
safefree(error_string);
|
||||
goto COMMON_EXIT;
|
||||
}
|
||||
|
||||
if ((strlen(config.stathost) > 0) &&
|
||||
strcasecmp(uri->authority, config.stathost) == 0) {
|
||||
showstats(connptr);
|
||||
goto COMMON_EXIT;
|
||||
}
|
||||
|
||||
port_no = 80;
|
||||
if ((port = strchr(uri->authority, ':'))) {
|
||||
*port++ = '\0';
|
||||
if (strlen(port) > 0)
|
||||
port_no = atoi(port);
|
||||
}
|
||||
|
||||
/* chris - so this can be passed on to clientreq_dnscomplete. */
|
||||
connptr->port_no = port_no;
|
||||
|
||||
#ifdef FILTER_ENABLE
|
||||
/* Filter domains out */
|
||||
if (config.filter) {
|
||||
if (filter_host(uri->authority)) {
|
||||
log("ERROR clientreq: Filtered connection (%s)",
|
||||
peer_ipaddr);
|
||||
httperr(connptr, 404,
|
||||
"Unable to connect to filtered host.");
|
||||
goto COMMON_EXIT;
|
||||
}
|
||||
}
|
||||
#endif /* FILTER_ENABLE */
|
||||
|
||||
/* Build a new request from the first line of the header */
|
||||
if (!(request = xmalloc(strlen(inbuf) + 1))) {
|
||||
log("ERROR clientreq: cannot allocate buffer for request from %s",
|
||||
peer_ipaddr);
|
||||
httperr(connptr, 503, HTTP503ERROR);
|
||||
goto COMMON_EXIT;
|
||||
}
|
||||
|
||||
/* We need to set the version number WE support */
|
||||
memcpy(request, inbuf, pmatch[METHOD_IND].rm_eo);
|
||||
request[pmatch[METHOD_IND].rm_eo] = '\0';
|
||||
strcat(request, " ");
|
||||
strcat(request, uri->path);
|
||||
if (uri->query) {
|
||||
strcat(request, "?");
|
||||
strcat(request, uri->query);
|
||||
}
|
||||
strcat(request, " HTTP/1.0\r\n");
|
||||
|
||||
/* chris - If domain is in dotted-quad format or is already in the
|
||||
* DNS cache, then go straight to WAITCONN.
|
||||
*/
|
||||
if (inet_aton(uri->authority, NULL) || lookup(NULL, uri->authority) == 0) {
|
||||
if ((fd = opensock(uri->authority, port_no)) < 0) {
|
||||
safefree(request);
|
||||
httperr(connptr, 500,
|
||||
"Unable to connect to host (cannot create sock)");
|
||||
stats.num_badcons++;
|
||||
goto COMMON_EXIT;
|
||||
}
|
||||
|
||||
connptr->server_fd = fd;
|
||||
connptr->type = WAITCONN;
|
||||
}
|
||||
/* Otherwise submit a DNS request and hope for the best. */
|
||||
else {
|
||||
if (adns_submit(adns, uri->authority, adns_r_a, adns_qf_quoteok_cname | adns_qf_cname_loose, connptr, &(connptr->adns_qu))) {
|
||||
safefree(request);
|
||||
httperr(connptr, 500, "Resolver error connecting to host");
|
||||
stats.num_badcons++;
|
||||
goto COMMON_EXIT;
|
||||
} else {
|
||||
#ifdef __DEBUG__
|
||||
log("DNS request submitted");
|
||||
#endif
|
||||
/* copy domain for later caching */
|
||||
connptr->domain = strdup(uri->authority);
|
||||
connptr->type = DNS_WAITCONN;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef XTINYPROXY
|
||||
/* Add a X-Tinyproxy header which contains our IP address */
|
||||
if (config.my_domain
|
||||
&& xstrstr(uri->authority, config.my_domain,
|
||||
strlen(uri->authority), FALSE)) {
|
||||
Add_XTinyproxy_Header(connptr);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Add the rewritten request to the buffer */
|
||||
unshift_buffer(connptr->cbuffer, request, strlen(request));
|
||||
|
||||
/*
|
||||
* HACK HACK HACK: When we're sending a POST there is no restriction
|
||||
* on the length of the header. If we don't let all the header lines
|
||||
* through, the POST will not work. This _definitely_ needs to be
|
||||
* fixed. - rjkaes
|
||||
*/
|
||||
if (!xstrstr(inbuf, "POST ", 5, FALSE)) {
|
||||
connptr->clientheader = TRUE;
|
||||
}
|
||||
|
||||
COMMON_EXIT:
|
||||
safefree(inbuf);
|
||||
free_uri(uri);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* chris - added this to move a connection from the DNS_WAITCONN state
|
||||
* to the WAITCONN state by connecting it to the server, once the name
|
||||
* has been resolved.
|
||||
*/
|
||||
static int clientreq_dnscomplete(struct conn_s *connptr, struct in_addr *inaddr) {
|
||||
int fd;
|
||||
|
||||
fd = opensock_inaddr(inaddr, connptr->port_no);
|
||||
|
||||
if (fd < 0) {
|
||||
#ifdef __DEBUG__
|
||||
log("Failed to open connection to server");
|
||||
#endif
|
||||
httperr(connptr, 500,
|
||||
"Unable to connect to host (cannot create sock)");
|
||||
stats.num_badcons++;
|
||||
|
||||
return 0;
|
||||
} else {
|
||||
#ifdef __DEBUG__
|
||||
log("Connected to server");
|
||||
#endif
|
||||
connptr->server_fd = fd;
|
||||
connptr->type = WAITCONN;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Finish the client request
|
||||
*/
|
||||
static int clientreq_finish(struct conn_s *connptr)
|
||||
{
|
||||
int sockopt, len = sizeof(sockopt);
|
||||
|
||||
assert(connptr);
|
||||
|
||||
if (getsockopt
|
||||
(connptr->server_fd, SOL_SOCKET, SO_ERROR, &sockopt, &len) < 0) {
|
||||
log("ERROR clientreq_finish: getsockopt error (%s)",
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sockopt != 0) {
|
||||
if (sockopt == EINPROGRESS)
|
||||
return 0;
|
||||
else if (sockopt == ETIMEDOUT) {
|
||||
httperr(connptr, 408, "Connect Timed Out");
|
||||
return 0;
|
||||
} else {
|
||||
log("ERROR clientreq_finish: could not create connection (%s)",
|
||||
strerror(sockopt));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
connptr->type = RELAYCONN;
|
||||
stats.num_opens++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check to see if the line is allowed or not depending on the anonymous
|
||||
* headers which are to be allowed.
|
||||
*/
|
||||
static int anonheader(char *line)
|
||||
{
|
||||
struct allowedhdr_s *allowedptr = allowedhdrs;
|
||||
|
||||
assert(line);
|
||||
assert(allowedhdrs);
|
||||
|
||||
if (!xstrstr(line, "GET ", 4, FALSE)
|
||||
|| !xstrstr(line, "POST ", 5, FALSE)
|
||||
|| !xstrstr(line, "HEAD ", 5, FALSE))
|
||||
return 1;
|
||||
|
||||
for (allowedptr = allowedhdrs; allowedptr;
|
||||
allowedptr = allowedptr->next) {
|
||||
if (!strncasecmp
|
||||
(line, allowedptr->hdrname, strlen(allowedptr->hdrname))) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Used to read in the lines from the header (client side) when we're doing
|
||||
* the anonymous header reduction.
|
||||
*/
|
||||
static int readanonconn(struct conn_s *connptr)
|
||||
{
|
||||
char *line = NULL;
|
||||
int retv;
|
||||
|
||||
assert(connptr);
|
||||
|
||||
if ((retv = readline(connptr->client_fd, connptr->cbuffer, &line)) <=
|
||||
0) {
|
||||
return retv;
|
||||
}
|
||||
|
||||
if ((line[0] == '\n') || (strncmp(line, "\r\n", 2) == 0)) {
|
||||
connptr->clientheader = TRUE;
|
||||
} else if (!anonheader(line)) {
|
||||
safefree(line);
|
||||
return 0;
|
||||
}
|
||||
|
||||
push_buffer(connptr->cbuffer, line, strlen(line));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read in the bytes from the socket
|
||||
*/
|
||||
static int readconn(int fd, struct buffer_s *buffptr)
|
||||
{
|
||||
int bytesin;
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(buffptr);
|
||||
|
||||
if ((bytesin = readbuff(fd, buffptr)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
#ifdef __DEBUG__
|
||||
log("readconn [%d]: %d", fd, bytesin);
|
||||
#endif
|
||||
|
||||
stats.num_rx += bytesin;
|
||||
return bytesin;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write the bytes from the buffer to the socket
|
||||
*/
|
||||
static int writeconn(int fd, struct buffer_s *buffptr)
|
||||
{
|
||||
int bytessent;
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(buffptr);
|
||||
|
||||
if ((bytessent = writebuff(fd, buffptr)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
stats.num_tx += bytessent;
|
||||
return bytessent;
|
||||
}
|
||||
|
||||
/*
|
||||
* Factored out the common function to read from the client. It was used in
|
||||
* two different places with no change, so no point in having the same code
|
||||
* twice.
|
||||
*/
|
||||
static int read_from_client(struct conn_s *connptr, fd_set * readfds)
|
||||
{
|
||||
assert(connptr);
|
||||
assert(readfds);
|
||||
|
||||
if (FD_ISSET(connptr->client_fd, readfds)) {
|
||||
if (config.anonymous && !connptr->clientheader) {
|
||||
if (readanonconn(connptr) < 0) {
|
||||
shutdown(connptr->client_fd, 2);
|
||||
shutdown(connptr->server_fd, 2);
|
||||
connptr->type = FINISHCONN;
|
||||
return -1;
|
||||
}
|
||||
} else if (readconn(connptr->client_fd, connptr->cbuffer) < 0) {
|
||||
shutdown(connptr->client_fd, 2);
|
||||
shutdown(connptr->server_fd, 2);
|
||||
connptr->type = FINISHCONN;
|
||||
return -1;
|
||||
}
|
||||
connptr->actiontime = time(NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Factored out the common write to client function since, again, it was used
|
||||
* in two different places with no changes.
|
||||
*/
|
||||
static int write_to_client(struct conn_s *connptr, fd_set * writefds)
|
||||
{
|
||||
assert(connptr);
|
||||
assert(writefds);
|
||||
|
||||
if (FD_ISSET(connptr->client_fd, writefds)) {
|
||||
if (writeconn(connptr->client_fd, connptr->sbuffer) < 0) {
|
||||
shutdown(connptr->client_fd, 2);
|
||||
shutdown(connptr->server_fd, 2);
|
||||
connptr->type = FINISHCONN;
|
||||
return -1;
|
||||
}
|
||||
connptr->actiontime = time(NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* All of the *_req functions handle the various stages a connection can go
|
||||
* through. I moved them out from getreqs because they handle a lot of error
|
||||
* code and it was indenting too far in. As you can see they are very simple,
|
||||
* and are only called once from getreqs, hence the inlining.
|
||||
*/
|
||||
|
||||
inline static void newconn_req(struct conn_s *connptr, fd_set * readfds)
|
||||
{
|
||||
assert(connptr);
|
||||
assert(readfds);
|
||||
|
||||
if (FD_ISSET(connptr->client_fd, readfds)) {
|
||||
if (clientreq(connptr) < 0) {
|
||||
shutdown(connptr->client_fd, 2);
|
||||
connptr->type = FINISHCONN;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!connptr)
|
||||
abort();
|
||||
|
||||
connptr->actiontime = time(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
inline static void waitconn_req(struct conn_s *connptr, fd_set * readfds,
|
||||
fd_set * writefds)
|
||||
{
|
||||
assert(connptr);
|
||||
assert(readfds);
|
||||
assert(writefds);
|
||||
|
||||
if (read_from_client(connptr, readfds) < 0)
|
||||
return;
|
||||
|
||||
if (FD_ISSET(connptr->server_fd, readfds)
|
||||
|| FD_ISSET(connptr->server_fd, writefds)) {
|
||||
if (clientreq_finish(connptr) < 0) {
|
||||
shutdown(connptr->server_fd, 2);
|
||||
shutdown(connptr->client_fd, 2);
|
||||
connptr->type = FINISHCONN;
|
||||
return;
|
||||
}
|
||||
connptr->actiontime = time(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
inline static void relayconn_req(struct conn_s *connptr, fd_set * readfds,
|
||||
fd_set * writefds)
|
||||
{
|
||||
assert(connptr);
|
||||
assert(readfds);
|
||||
assert(writefds);
|
||||
|
||||
if (read_from_client(connptr, readfds) < 0)
|
||||
return;
|
||||
|
||||
if (FD_ISSET(connptr->server_fd, readfds)) {
|
||||
if (connptr->serverheader) {
|
||||
if (readconn(connptr->server_fd, connptr->sbuffer) < 0) {
|
||||
shutdown(connptr->server_fd, 2);
|
||||
connptr->type = CLOSINGCONN;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* We need to read in the first line to rewrite the
|
||||
* version back down to HTTP/1.0 (if needed)
|
||||
*/
|
||||
char *line = NULL, *ptr, *newline;
|
||||
int retv;
|
||||
|
||||
if (
|
||||
(retv =
|
||||
readline(connptr->server_fd, connptr->sbuffer,
|
||||
&line)) < 0) {
|
||||
shutdown(connptr->server_fd, 2);
|
||||
httperr(connptr, 500, "Server Closed Early");
|
||||
return;
|
||||
} else if (retv == 0)
|
||||
return;
|
||||
|
||||
connptr->serverheader = TRUE;
|
||||
|
||||
if (strncasecmp(line, "HTTP/1.0", 8)) {
|
||||
/* Okay, we need to rewrite it then */
|
||||
if (!(ptr = strchr(line, ' '))) {
|
||||
shutdown(connptr->server_fd, 2);
|
||||
httperr(connptr, 500,
|
||||
"There was Server Error");
|
||||
return;
|
||||
}
|
||||
ptr++;
|
||||
|
||||
if (!(newline = xmalloc(strlen(line) + 1))) {
|
||||
shutdown(connptr->server_fd, 2);
|
||||
httperr(connptr, 503,
|
||||
"No Memory Available");
|
||||
return;
|
||||
}
|
||||
|
||||
sprintf(newline, "HTTP/1.0 %s", ptr);
|
||||
safefree(line);
|
||||
line = newline;
|
||||
}
|
||||
|
||||
push_buffer(connptr->sbuffer, line, strlen(line));
|
||||
}
|
||||
connptr->actiontime = time(NULL);
|
||||
}
|
||||
|
||||
if (write_to_client(connptr, writefds) < 0)
|
||||
return;
|
||||
|
||||
if (FD_ISSET(connptr->server_fd, writefds)) {
|
||||
if (writeconn(connptr->server_fd, connptr->cbuffer) < 0) {
|
||||
shutdown(connptr->server_fd, 2);
|
||||
connptr->type = CLOSINGCONN;
|
||||
return;
|
||||
}
|
||||
connptr->actiontime = time(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
inline static void closingconn_req(struct conn_s *connptr, fd_set * writefds)
|
||||
{
|
||||
assert(connptr);
|
||||
assert(writefds);
|
||||
|
||||
write_to_client(connptr, writefds);
|
||||
}
|
||||
|
||||
/*
|
||||
* Check against the valid subnet to see if we should allow the access
|
||||
*/
|
||||
static int validuser(int fd)
|
||||
{
|
||||
char ipaddr[PEER_IP_LENGTH];
|
||||
|
||||
assert(fd >= 0);
|
||||
|
||||
if (config.subnet == NULL)
|
||||
return 1;
|
||||
|
||||
if (!strncmp(config.subnet, getpeer_ip(fd, ipaddr),
|
||||
strlen(config.subnet))) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Loop that checks for new connections, dispatches to the correct
|
||||
* routines if bytes are pending, checks to see if it's time for a
|
||||
* garbage collect.
|
||||
*/
|
||||
int getreqs(void)
|
||||
{
|
||||
static unsigned int garbc = 0;
|
||||
fd_set readfds, writefds, exceptfds; /* chris - ADNS expects exceptfds */
|
||||
struct conn_s *connptr;
|
||||
int fd;
|
||||
struct timeval tv, now; /* chris - for ADNS timeouts */
|
||||
|
||||
char peer_ipaddr[PEER_IP_LENGTH];
|
||||
|
||||
if (setup_fd < 0) {
|
||||
log("ERROR getreqs: setup_fd not a socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Garbage collect the dead connections and close any idle ones */
|
||||
if (garbc++ >= GARBCOLL_INTERVAL) {
|
||||
garbcoll();
|
||||
garbc = 0;
|
||||
}
|
||||
conncoll();
|
||||
|
||||
FD_ZERO(&readfds);
|
||||
FD_ZERO(&writefds);
|
||||
FD_SET(setup_fd, &readfds);
|
||||
|
||||
for (connptr = connections; connptr; connptr = connptr->next) {
|
||||
#ifdef __DEBUG__
|
||||
log("Connptr: %p - %d / client %d server %d", connptr,
|
||||
connptr->type, connptr->client_fd, connptr->server_fd);
|
||||
#endif
|
||||
switch (connptr->type) {
|
||||
case NEWCONN:
|
||||
if (buffer_size(connptr->cbuffer) < MAXBUFFSIZE)
|
||||
FD_SET(connptr->client_fd, &readfds);
|
||||
else {
|
||||
httperr(connptr, 414,
|
||||
"Your Request is way too long.");
|
||||
}
|
||||
break;
|
||||
|
||||
/* no case here for DNS_WAITCONN */
|
||||
|
||||
case WAITCONN:
|
||||
FD_SET(connptr->server_fd, &readfds);
|
||||
FD_SET(connptr->server_fd, &writefds);
|
||||
|
||||
if (buffer_size(connptr->cbuffer) < MAXBUFFSIZE)
|
||||
FD_SET(connptr->client_fd, &readfds);
|
||||
break;
|
||||
|
||||
case RELAYCONN:
|
||||
if (buffer_size(connptr->sbuffer) > 0)
|
||||
FD_SET(connptr->client_fd, &writefds);
|
||||
if (buffer_size(connptr->sbuffer) < MAXBUFFSIZE)
|
||||
FD_SET(connptr->server_fd, &readfds);
|
||||
|
||||
if (buffer_size(connptr->cbuffer) > 0)
|
||||
FD_SET(connptr->server_fd, &writefds);
|
||||
if (buffer_size(connptr->cbuffer) < MAXBUFFSIZE)
|
||||
FD_SET(connptr->client_fd, &readfds);
|
||||
|
||||
break;
|
||||
|
||||
case CLOSINGCONN:
|
||||
if (buffer_size(connptr->sbuffer) > 0)
|
||||
FD_SET(connptr->client_fd, &writefds);
|
||||
else {
|
||||
shutdown(connptr->client_fd, 2);
|
||||
shutdown(connptr->server_fd, 2);
|
||||
connptr->type = FINISHCONN;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Set a 60 second time out */
|
||||
tv.tv_sec = 1;//60;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
/* chris - Make ADNS do its stuff, too. */
|
||||
{
|
||||
struct timeval *tv_mod = &tv;
|
||||
int foo = FD_SETSIZE;
|
||||
gettimeofday(&now, NULL);
|
||||
FD_ZERO(&exceptfds);
|
||||
adns_beforeselect(adns, &foo, &readfds, &writefds, &exceptfds, &tv_mod, NULL, &now);
|
||||
}
|
||||
|
||||
if (select(FD_SETSIZE, &readfds, &writefds, &exceptfds, &tv) < 0) {
|
||||
#ifdef __DEBUG__
|
||||
log("select error: %s", strerror(errno));
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* chris - see whether any ADNS lookups have completed */
|
||||
gettimeofday(&now, NULL);
|
||||
adns_afterselect(adns, FD_SETSIZE, &readfds, &writefds, &exceptfds, &now);
|
||||
|
||||
for (connptr = connections; connptr; connptr = connptr->next) {
|
||||
adns_answer *ans;
|
||||
|
||||
if (connptr->type == DNS_WAITCONN &&
|
||||
adns_check(adns, &(connptr->adns_qu), &ans, (void**)&connptr) == 0) {
|
||||
|
||||
if (ans->status == adns_s_ok) {
|
||||
if (connptr->domain) {
|
||||
insert(ans->rrs.inaddr, connptr->domain);
|
||||
free(connptr->domain);
|
||||
}
|
||||
|
||||
clientreq_dnscomplete(connptr, ans->rrs.inaddr);
|
||||
free(ans);
|
||||
|
||||
/* hack */
|
||||
FD_SET(connptr->server_fd, &readfds);
|
||||
#ifdef __DEBUG__
|
||||
log("DNS resolution successful");
|
||||
#endif
|
||||
} else {
|
||||
free(ans);
|
||||
|
||||
httperr(connptr, 500, "Unable to resolve hostname in URL");
|
||||
#ifdef __DEBUG__
|
||||
log("DNS resolution failed");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Check to see if there are new connections pending */
|
||||
if (FD_ISSET(setup_fd, &readfds) && (fd = listen_sock()) >= 0) {
|
||||
new_conn(fd); /* make a connection from the FD */
|
||||
|
||||
if (validuser(fd)) {
|
||||
if (config.cutoffload && (load > config.cutoffload)) {
|
||||
stats.num_refused++;
|
||||
httperr(connptr, 503,
|
||||
"tinyproxy is not accepting connections due to high system load");
|
||||
}
|
||||
} else {
|
||||
httperr(connptr, 403,
|
||||
"You are not authorized to use the service.");
|
||||
log("AUTH Rejected connection from %s",
|
||||
getpeer_ip(fd, peer_ipaddr));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Loop through the connections and dispatch them to the appropriate
|
||||
* handler
|
||||
*/
|
||||
for (connptr = connections; connptr; connptr = connptr->next) {
|
||||
switch (connptr->type) {
|
||||
case NEWCONN:
|
||||
newconn_req(connptr, &readfds);
|
||||
break;
|
||||
|
||||
case WAITCONN:
|
||||
waitconn_req(connptr, &readfds, &writefds);
|
||||
break;
|
||||
|
||||
case RELAYCONN:
|
||||
relayconn_req(connptr, &readfds, &writefds);
|
||||
break;
|
||||
|
||||
case CLOSINGCONN:
|
||||
closingconn_req(connptr, &writefds);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
/* $Id: reqs.h,v 1.1.1.1 2000-02-16 17:32:23 sdyoung Exp $
|
||||
*
|
||||
* See 'reqs.c' for a detailed description.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _REQS_H_
|
||||
#define _REQS_H_ 1
|
||||
|
||||
extern int getreqs(void);
|
||||
|
||||
#endif
|
||||
+362
@@ -0,0 +1,362 @@
|
||||
/* $Id: sock.c,v 1.1.1.1 2000-02-16 17:32:23 sdyoung Exp $
|
||||
*
|
||||
* Sockets are created and destroyed here. When a new connection comes in from
|
||||
* a client, we need to copy the socket and the create a second socket to the
|
||||
* remote server the client is trying to connect to. Also, the listening
|
||||
* socket is created and destroyed here. Sounds more impressive than it
|
||||
* actually is.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
* Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "tinyproxy.h"
|
||||
#include "sock.h"
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
#include "dnscache.h"
|
||||
|
||||
/* This routine is so old I can't even remember writing it. But I do
|
||||
* remember that it was an .h file because I didn't know putting code in a
|
||||
* header was bad magic yet. anyway, this routine opens a connection to a
|
||||
* system and returns the fd.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Cleaned up some of the code to use memory routines which are now the
|
||||
* default. Also, the routine first checks to see if the address is in
|
||||
* dotted-decimal form before it does a name lookup. Finally, the newly
|
||||
* created socket is made non-blocking.
|
||||
* - rjkaes
|
||||
*/
|
||||
int opensock(char *ip_addr, int port)
|
||||
{
|
||||
int sock_fd, flags;
|
||||
struct sockaddr_in port_info;
|
||||
|
||||
assert(ip_addr);
|
||||
assert(port > 0);
|
||||
|
||||
memset((struct sockaddr *) &port_info, 0, sizeof(port_info));
|
||||
|
||||
port_info.sin_family = AF_INET;
|
||||
|
||||
/* chris - Could block; neet to ensure that this is never called
|
||||
* before a non-blocking DNS query happens for this address. Not
|
||||
* relevant in the code as it stands.
|
||||
*/
|
||||
if (dnscache(&port_info.sin_addr, ip_addr) < 0) {
|
||||
log("ERROR opensock: Could not lookup address: %s", ip_addr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
port_info.sin_port = htons(port);
|
||||
|
||||
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
||||
log("ERROR opensock: socket (%s)", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
flags = fcntl(sock_fd, F_GETFL, 0);
|
||||
fcntl(sock_fd, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
if (connect
|
||||
(sock_fd, (struct sockaddr *) &port_info, sizeof(port_info)) < 0) {
|
||||
if (errno != EINPROGRESS) {
|
||||
log("ERROR opensock: connect (%s)", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return sock_fd;
|
||||
}
|
||||
|
||||
/* chris - added this to open a socket given a struct in_addr */
|
||||
int opensock_inaddr(struct in_addr *inaddr, int port)
|
||||
{
|
||||
int sock_fd, flags;
|
||||
struct sockaddr_in port_info;
|
||||
|
||||
assert(inaddr);
|
||||
assert(port > 0);
|
||||
|
||||
memset((struct sockaddr *) &port_info, 0, sizeof(port_info));
|
||||
|
||||
port_info.sin_family = AF_INET;
|
||||
|
||||
memcpy(&port_info.sin_addr, inaddr, sizeof(struct in_addr));
|
||||
|
||||
port_info.sin_port = htons(port);
|
||||
|
||||
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
||||
log("ERROR opensock_inaddr: socket (%s)", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
flags = fcntl(sock_fd, F_GETFL, 0);
|
||||
fcntl(sock_fd, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
if (connect
|
||||
(sock_fd, (struct sockaddr *) &port_info, sizeof(port_info)) < 0) {
|
||||
if (errno != EINPROGRESS) {
|
||||
log("ERROR opensock: connect (%s)", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return sock_fd;
|
||||
}
|
||||
|
||||
int setup_fd;
|
||||
static struct sockaddr listen_sock_addr;
|
||||
|
||||
/*
|
||||
* Start listening to a socket.
|
||||
*/
|
||||
int init_listen_sock(int port)
|
||||
{
|
||||
struct sockaddr_in laddr;
|
||||
int i = 1;
|
||||
|
||||
assert(port > 0);
|
||||
|
||||
if ((setup_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
log("ERROR init_listen_sock: socket (%s)", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (setsockopt(setup_fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0) {
|
||||
log("ERROR init_listen_sock: setsockopt (%s)",
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&listen_sock_addr, 0, sizeof(listen_sock_addr));
|
||||
memset(&laddr, 0, sizeof(laddr));
|
||||
laddr.sin_family = AF_INET;
|
||||
laddr.sin_port = htons(port);
|
||||
|
||||
if (config.ipAddr) {
|
||||
laddr.sin_addr.s_addr = inet_addr(config.ipAddr);
|
||||
} else {
|
||||
laddr.sin_addr.s_addr = inet_addr("0.0.0.0");
|
||||
}
|
||||
if (bind(setup_fd, (struct sockaddr *) &laddr, sizeof(laddr)) < 0) {
|
||||
log("ERROR init_listen_sock: bind (%s)", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if ((listen(setup_fd, MAXLISTEN)) != 0) {
|
||||
log("ERROR init_listen_sock: listen (%s)", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Grab a pending connection
|
||||
*/
|
||||
int listen_sock(void)
|
||||
{
|
||||
static int sock;
|
||||
int sz = sizeof(listen_sock_addr);
|
||||
|
||||
if ((sock = accept(setup_fd, &listen_sock_addr, &sz)) < 0) {
|
||||
if (errno != ECONNABORTED
|
||||
#ifdef EPROTO
|
||||
&& errno != EPROTO
|
||||
#endif
|
||||
#ifdef EWOULDBLOCK
|
||||
&& errno != EWOULDBLOCK
|
||||
#endif
|
||||
&& errno != EINTR)
|
||||
log("ERROR listen_sock: accept (%s)", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
stats.num_listens++;
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
/*
|
||||
* Stop listening on a socket.
|
||||
*/
|
||||
void de_init_listen_sock(void)
|
||||
{
|
||||
close(setup_fd);
|
||||
}
|
||||
|
||||
/*
|
||||
* Takes a socket descriptor and returns the string contain the peer's
|
||||
* IP address.
|
||||
*/
|
||||
char *getpeer_ip(int fd, char *ipaddr)
|
||||
{
|
||||
struct sockaddr_in name;
|
||||
int namelen = sizeof(name);
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(ipaddr);
|
||||
|
||||
memset(ipaddr, '\0', PEER_IP_LENGTH);
|
||||
|
||||
if (getpeername(fd, (struct sockaddr *) &name, &namelen) != 0) {
|
||||
log("ERROR Connect: 'could not get peer name'");
|
||||
} else {
|
||||
strncpy(ipaddr,
|
||||
inet_ntoa(*(struct in_addr *) &name.sin_addr.s_addr),
|
||||
PEER_IP_LENGTH - 1);
|
||||
}
|
||||
|
||||
return ipaddr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Takes a socket descriptor and returns the string containing the peer's
|
||||
* address.
|
||||
*/
|
||||
char *getpeer_string(int fd, char *string)
|
||||
{
|
||||
struct sockaddr_in name;
|
||||
int namelen = sizeof(name);
|
||||
struct hostent *peername;
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(string);
|
||||
|
||||
memset(string, '\0', PEER_STRING_LENGTH);
|
||||
|
||||
if (getpeername(fd, (struct sockaddr *) &name, &namelen) != 0) {
|
||||
log("ERROR Connect: 'could not get peer name'");
|
||||
} else {
|
||||
if (
|
||||
(peername =
|
||||
gethostbyaddr((char *) &name.sin_addr.s_addr,
|
||||
sizeof(name.sin_addr.s_addr),
|
||||
AF_INET)) != NULL) {
|
||||
strncpy(string, peername->h_name,
|
||||
PEER_STRING_LENGTH - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Read one line of the header
|
||||
*/
|
||||
int readline(int fd, struct buffer_s *buffer, char **line)
|
||||
{
|
||||
char inbuf[BUFFER];
|
||||
int bytesin;
|
||||
char *endline = NULL;
|
||||
char *newline;
|
||||
struct bufline_s *oldline;
|
||||
unsigned long len = 0, length;
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(buffer);
|
||||
assert(line);
|
||||
|
||||
*line = NULL;
|
||||
|
||||
if ((bytesin = recv(fd, inbuf, BUFFER - 1, MSG_PEEK)) <= 0) {
|
||||
goto CONN_ERROR;
|
||||
}
|
||||
|
||||
endline = xstrstr(inbuf, "\n", bytesin, FALSE);
|
||||
|
||||
if (endline) {
|
||||
endline++;
|
||||
*endline = '\0';
|
||||
|
||||
length = strlen(inbuf);
|
||||
memset(inbuf, '\0', BUFFER);
|
||||
|
||||
/* Actually pull it off the queue */
|
||||
if ((bytesin = recv(fd, inbuf, length, 0)) <= 0) {
|
||||
goto CONN_ERROR;
|
||||
}
|
||||
|
||||
*line = xstrdup(inbuf);
|
||||
|
||||
if (buffer_size(buffer) > 0) {
|
||||
if (!(newline = xmalloc(buffer_size(buffer)
|
||||
+ length + 1))) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
newline[0] = '\0';
|
||||
|
||||
while ((oldline = shift_buffer(buffer))) {
|
||||
memcpy(newline + len, oldline->string,
|
||||
oldline->length);
|
||||
|
||||
len += oldline->length;
|
||||
free_line(oldline);
|
||||
}
|
||||
|
||||
memcpy(newline + len, *line, strlen(*line) + 1);
|
||||
safefree(*line);
|
||||
|
||||
*line = newline;
|
||||
}
|
||||
|
||||
return strlen(*line);
|
||||
}
|
||||
|
||||
/*
|
||||
* We didn't find a '\r\n', but we've filled the buffer.. pull it off
|
||||
* and try again later...
|
||||
*/
|
||||
if ((bytesin = recv(fd, inbuf, bytesin, 0)) <= 0) {
|
||||
goto CONN_ERROR;
|
||||
}
|
||||
|
||||
if (!(*line = xmalloc(bytesin + 1))) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(*line, inbuf, bytesin);
|
||||
(*line)[bytesin] = '\0';
|
||||
|
||||
push_buffer(buffer, *line, strlen(*line));
|
||||
|
||||
return 0;
|
||||
|
||||
CONN_ERROR:
|
||||
if (bytesin == 0 || (errno != EAGAIN && errno != EINTR)) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
/* $Id: sock.h,v 1.1.1.1 2000-02-16 17:32:23 sdyoung Exp $
|
||||
*
|
||||
* See 'sock.c' for a detailed description.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _SOCK_H_
|
||||
#define _SOCK_H_ 1
|
||||
|
||||
#include "buffer.h"
|
||||
|
||||
#define PEER_IP_LENGTH 16
|
||||
#define PEER_STRING_LENGTH 256
|
||||
|
||||
extern int setup_fd;
|
||||
|
||||
extern int opensock(char *ip_addr, int port);
|
||||
extern int opensock_inaddr(struct in_addr *inaddr, int port);
|
||||
extern int init_listen_sock(int port);
|
||||
extern int listen_sock(void);
|
||||
extern void de_init_listen_sock(void);
|
||||
extern int setsocketopt(int *sock_fd, int options, int flip);
|
||||
|
||||
extern char *getpeer_ip(int fd, char *ipaddr);
|
||||
extern char *getpeer_string(int fd, char *string);
|
||||
extern int readline(int fd, struct buffer_s *buffer, char **line);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1 @@
|
||||
timestamp
|
||||
+478
@@ -0,0 +1,478 @@
|
||||
/* $Id: tinyproxy.c,v 1.1.1.1 2000-02-16 17:32:23 sdyoung Exp $
|
||||
*
|
||||
* The initialize routine. Basically sets up all the initial stuff (logfile,
|
||||
* listening socket, config options, etc.) and then sits there and loops
|
||||
* over the new connections until the daemon is closed. Also has additional
|
||||
* functions to handle the "user friendly" aspects of a program (usage,
|
||||
* stats, etc.) Like any good program, most of the work is actually done
|
||||
* elsewhere.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
* Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <sysexits.h>
|
||||
#include <pwd.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <syslog.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
/* chris - need this for asynchronous DNS resolution */
|
||||
#include <adns.h>
|
||||
|
||||
adns_state adns;
|
||||
|
||||
#include "config.h"
|
||||
#include "tinyproxy.h"
|
||||
#include "utils.h"
|
||||
#include "log.h"
|
||||
#include "sock.h"
|
||||
#include "conns.h"
|
||||
#include "reqs.h"
|
||||
#include "buffer.h"
|
||||
#include "filter.h"
|
||||
|
||||
void takesig(int sig);
|
||||
|
||||
/*
|
||||
* Global Structures
|
||||
*/
|
||||
struct config_s config = {
|
||||
NULL, /* Log file handle */
|
||||
DEFAULT_LOG, /* Logfile name */
|
||||
FALSE, /* Use syslog instead? */
|
||||
DEFAULT_CUTOFFLOAD, /* Cut off load */
|
||||
DEFAULT_PORT, /* Listen on this port */
|
||||
DEFAULT_STATHOST, /* URL of stats host */
|
||||
FALSE, /* Quit? */
|
||||
DEFAULT_USER, /* Name of user to change to */
|
||||
FALSE, /* Run anonymous by default? */
|
||||
NULL, /* String containing the subnet allowed */
|
||||
NULL, /* IP address to listen on */
|
||||
#ifdef FILTER_ENABLE
|
||||
NULL, /* Location of filter file */
|
||||
#endif /* FILTER_ENABLE */
|
||||
FALSE, /* Restrict the log to only errors */
|
||||
#ifdef XTINYPROXY
|
||||
NULL /* The name of this domain */
|
||||
#endif
|
||||
};
|
||||
|
||||
struct stat_s stats;
|
||||
float load = 0.00;
|
||||
struct allowedhdr_s *allowedhdrs = NULL;
|
||||
|
||||
/*
|
||||
* Dump info to the logfile
|
||||
*/
|
||||
static void dumpdebug(void)
|
||||
{
|
||||
struct conn_s *connptr = connections;
|
||||
long clients = 0, waiting = 0, relaying = 0, closing = 0, finished = 0;
|
||||
|
||||
log("SIGUSR1 received, debug dump follows.");
|
||||
|
||||
while (connptr) {
|
||||
switch (connptr->type) {
|
||||
case NEWCONN:
|
||||
clients++;
|
||||
break;
|
||||
case WAITCONN:
|
||||
waiting++;
|
||||
break;
|
||||
case RELAYCONN:
|
||||
relaying++;
|
||||
break;
|
||||
case CLOSINGCONN:
|
||||
closing++;
|
||||
break;
|
||||
case FINISHCONN:
|
||||
finished++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
connptr = connptr->next;
|
||||
}
|
||||
log("clients: %d, waiting: %d, relaying: %d," \
|
||||
"closing: %d, finished: %d",
|
||||
clients, waiting, relaying, closing, finished);
|
||||
log("total requests handled: %lu", stats.num_reqs);
|
||||
log("total connections handled: %lu", stats.num_cons);
|
||||
log("total sockets listened: %lu", stats.num_listens);
|
||||
log("total sockets opened: %lu", stats.num_opens);
|
||||
log("total bad opens: %lu", stats.num_badcons);
|
||||
log("total bytes tx: %lu", stats.num_tx);
|
||||
log("total bytes rx: %lu", stats.num_rx);
|
||||
log("connections refused due to load: %lu", stats.num_refused);
|
||||
log("garbage collections: %lu", stats.num_garbage);
|
||||
log("idle connections killed: %lu", stats.num_idles);
|
||||
log("end debug dump.");
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle a signal
|
||||
*/
|
||||
void takesig(int sig)
|
||||
{
|
||||
switch (sig) {
|
||||
case SIGUSR1:
|
||||
dumpdebug();
|
||||
break;
|
||||
case SIGHUP:
|
||||
if (config.logf)
|
||||
ftruncate(fileno(config.logf), 0);
|
||||
|
||||
log("SIGHUP received, cleaning up...");
|
||||
conncoll();
|
||||
garbcoll();
|
||||
|
||||
#ifdef FILTER_ENABLE
|
||||
if (config.filter) {
|
||||
filter_destroy();
|
||||
filter_init();
|
||||
}
|
||||
log("Re-reading filter file.");
|
||||
#endif /* FILTER_ENABLE */
|
||||
log("Finished cleaning memory/connections.");
|
||||
break;
|
||||
case SIGTERM:
|
||||
#ifdef FILTER_ENABLE
|
||||
if (config.filter)
|
||||
filter_destroy();
|
||||
#endif /* FILTER_ENABLE */
|
||||
config.quit = TRUE;
|
||||
break;
|
||||
case SIGALRM:
|
||||
calcload();
|
||||
alarm(LOAD_RECALCTIMER);
|
||||
break;
|
||||
}
|
||||
if (sig != SIGTERM)
|
||||
signal(sig, takesig);
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
}
|
||||
|
||||
/*
|
||||
* Display usage to the user on stderr.
|
||||
*/
|
||||
static void usagedisp(void)
|
||||
{
|
||||
printf("tinyproxy version " VERSION "\n");
|
||||
printf("Copyright 1998 Steven Young (sdyoung@well.com)\n");
|
||||
printf
|
||||
("Copyright 1998-1999 Robert James Kaes (rjkaes@flarenet.com)\n\n");
|
||||
printf("Copyright 2000 Chris Lightfoot (chris@ex-parrot.com)\n");
|
||||
|
||||
printf
|
||||
("This software is licensed under the GNU General Public License (GPL).\n");
|
||||
printf("See the file 'COPYING' included with tinyproxy source.\n\n");
|
||||
|
||||
printf("Compiled with Ian Jackson's adns:\n");
|
||||
printf(" http://www.chiark.greenend.org.uk/~ian/adns/\n\n");
|
||||
|
||||
printf("Usage: tinyproxy [args]\n");
|
||||
printf("Options:\n");
|
||||
printf("\t-v\t\tdisplay version number\n");
|
||||
printf("\t-h\t\tdisplay usage\n");
|
||||
printf("\t-d\t\tdo not daemonize\n");
|
||||
printf
|
||||
("\t-n ip_address\tallow access from only this subnet. (i.e. 192.168.0.)\n");
|
||||
printf("\t-i ip_address\tonly listen on this address\n");
|
||||
printf("\t-p port\t\tlisten on 'port'\n");
|
||||
printf("\t-l logfile\tlog to 'logfile'\n");
|
||||
#ifdef HAVE_SYSLOG_H
|
||||
printf("\t-S\t\tlog using the syslog instead\n");
|
||||
#endif
|
||||
printf("\t-r\t\trestrict the log to only errors\n");
|
||||
printf
|
||||
("\t-w load\t\tstop accepting new connections at 'load'. 0 disables\n");
|
||||
printf("\t-s stathost\tset stathost to 'stathost'\n");
|
||||
printf("\t-u user\t\tchange to user after startup. \"\" disables\n");
|
||||
printf("\t-a header\tallow 'header' through the anon block\n");
|
||||
#ifdef FILTER_ENABLE
|
||||
printf("\t-f filterfile\tblock sites specified in filterfile\n");
|
||||
#endif /* FILTER_ENABLE */
|
||||
#ifdef XTINYPROXY
|
||||
printf
|
||||
("\t-x domain\tAdd a XTinyproxy header with the peer's IP address\n");
|
||||
#endif
|
||||
|
||||
/* Display the modes compiled into tinyproxy */
|
||||
printf("\nFeatures Compiled In:\n");
|
||||
#ifdef XTINYPROXY
|
||||
printf(" XTinyproxy Header\n");
|
||||
#endif /* XTINYPROXY */
|
||||
#ifdef FILTER_ENABLE
|
||||
printf(" Filtering\n");
|
||||
printf(" * with Regular Expression support\n");
|
||||
#endif /* FILTER_ENABLE */
|
||||
#ifndef NDEBUG
|
||||
printf(" Debuggin code\n");
|
||||
#endif /* NDEBUG */
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int optch;
|
||||
flag usage = FALSE, godaemon = TRUE, changeid = FALSE;
|
||||
struct passwd *thisuser = NULL;
|
||||
|
||||
struct allowedhdr_s **rpallowedptr = &allowedhdrs;
|
||||
struct allowedhdr_s *allowedptr = allowedhdrs, *newallowed;
|
||||
|
||||
while ((optch = getopt(argc, argv, "vh?dp:l:Sa:w:s:u:n:i:rx:f:")) !=
|
||||
EOF) {
|
||||
switch (optch) {
|
||||
case 'v':
|
||||
fprintf(stderr, "tinyproxy version " VERSION "\n");
|
||||
exit(EX_OK);
|
||||
break;
|
||||
case 'p':
|
||||
if (!(config.port = atoi(optarg))) {
|
||||
log
|
||||
("bad port on commandline, defaulting to %d",
|
||||
DEFAULT_PORT);
|
||||
config.port = DEFAULT_PORT;
|
||||
}
|
||||
break;
|
||||
case 'l':
|
||||
if (!(config.logf_name = xstrdup(optarg))) {
|
||||
log("bad log file, defaulting to %s",
|
||||
DEFAULT_LOG);
|
||||
config.logf_name = DEFAULT_LOG;
|
||||
}
|
||||
break;
|
||||
#ifdef HAVE_SYSLOG_H
|
||||
case 'S': /* Use the syslog function to handle logging */
|
||||
config.syslog = TRUE;
|
||||
break;
|
||||
#endif
|
||||
case 'd':
|
||||
godaemon = FALSE;
|
||||
break;
|
||||
case 'w':
|
||||
sscanf(optarg, "%f", &config.cutoffload);
|
||||
break;
|
||||
case 's':
|
||||
if (!(config.stathost = xstrdup(optarg))) {
|
||||
log("bad stathost, defaulting to %s",
|
||||
DEFAULT_STATHOST);
|
||||
config.stathost = DEFAULT_STATHOST;
|
||||
}
|
||||
break;
|
||||
case 'u':
|
||||
if (!(config.changeuser = xstrdup(optarg))) {
|
||||
log("bad user name, defaulting to %s",
|
||||
DEFAULT_USER);
|
||||
config.changeuser = DEFAULT_USER;
|
||||
}
|
||||
break;
|
||||
case 'a':
|
||||
config.anonymous = TRUE;
|
||||
|
||||
while (allowedptr) {
|
||||
rpallowedptr = &allowedptr->next;
|
||||
allowedptr = allowedptr->next;
|
||||
}
|
||||
|
||||
if (!
|
||||
(newallowed =
|
||||
xmalloc(sizeof(struct allowedhdr_s)))) {
|
||||
log("tinyproxy: cannot allocate headers");
|
||||
exit(EX_SOFTWARE);
|
||||
}
|
||||
|
||||
if (!(newallowed->hdrname = xstrdup(optarg))) {
|
||||
log("tinyproxy: cannot duplicate string");
|
||||
exit(EX_SOFTWARE);
|
||||
}
|
||||
|
||||
*rpallowedptr = newallowed;
|
||||
newallowed->next = allowedptr;
|
||||
|
||||
break;
|
||||
case 'n':
|
||||
if (!(config.subnet = xstrdup(optarg))) {
|
||||
log("tinyproxy: could not allocate memory");
|
||||
exit(EX_SOFTWARE);
|
||||
}
|
||||
break;
|
||||
case 'i':
|
||||
if (!(config.ipAddr = xstrdup(optarg))) {
|
||||
log("tinyproxy: could not allocate memory");
|
||||
exit(EX_SOFTWARE);
|
||||
}
|
||||
break;
|
||||
#ifdef FILTER_ENABLE
|
||||
case 'f':
|
||||
if (!(config.filter = xstrdup(optarg))) {
|
||||
log("tinyproxy: could not allocate memory");
|
||||
}
|
||||
break;
|
||||
#endif /* FILTER_ENABLE */
|
||||
case 'r':
|
||||
config.restricted = TRUE;
|
||||
break;
|
||||
#ifdef XTINYPROXY
|
||||
case 'x':
|
||||
if (!(config.my_domain = xstrdup(optarg))) {
|
||||
log("tinyproxy: could not allocate memory");
|
||||
exit(EX_SOFTWARE);
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
case '?':
|
||||
case 'h':
|
||||
default:
|
||||
usage = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (usage == TRUE) {
|
||||
usagedisp();
|
||||
exit(EX_OK);
|
||||
}
|
||||
|
||||
/* chris - Initialise asynchronous DNS */
|
||||
if (adns_init(&adns, 0, 0)) {
|
||||
log("tinyproxy: could not initialise ADNS");
|
||||
exit(EX_SOFTWARE);
|
||||
}
|
||||
|
||||
/* Open the log file if not using syslog */
|
||||
if (config.syslog == FALSE) {
|
||||
if (!(config.logf = fopen(config.logf_name, "a"))) {
|
||||
fprintf(stderr,
|
||||
"Unable to open logfile %s for appending!\n",
|
||||
config.logf_name);
|
||||
exit(EX_CANTCREAT);
|
||||
}
|
||||
} else {
|
||||
if (godaemon == TRUE)
|
||||
openlog("tinyproxy", LOG_PID, LOG_DAEMON);
|
||||
else
|
||||
openlog("tinyproxy", LOG_PID, LOG_USER);
|
||||
}
|
||||
|
||||
log(PACKAGE " " VERSION " starting...");
|
||||
|
||||
if (strlen(config.changeuser)) {
|
||||
if ((getuid() != 0) && (geteuid() != 0)) {
|
||||
log
|
||||
("not running as root, therefore not changing uid/gid.");
|
||||
} else {
|
||||
changeid = TRUE;
|
||||
if (!(thisuser = getpwnam(config.changeuser))) {
|
||||
log("unable to find user \"%s\"!",
|
||||
config.changeuser);
|
||||
exit(EX_NOUSER);
|
||||
}
|
||||
log("changing to user \"%s\" (%d/%d).",
|
||||
config.changeuser, thisuser->pw_uid,
|
||||
thisuser->pw_gid);
|
||||
}
|
||||
}
|
||||
#ifdef NDEBUG
|
||||
if (godaemon == TRUE)
|
||||
makedaemon();
|
||||
#else
|
||||
printf("Debugging is enabled, so you can not go daemon.\n");
|
||||
#endif
|
||||
|
||||
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
|
||||
fprintf(stderr, "Could not set SIGPIPE\n");
|
||||
exit(EX_OSERR);
|
||||
}
|
||||
if (signal(SIGUSR1, takesig) == SIG_ERR) {
|
||||
fprintf(stderr, "Could not set SIGUSR1\n");
|
||||
exit(EX_OSERR);
|
||||
}
|
||||
if (signal(SIGTERM, takesig) == SIG_ERR) {
|
||||
fprintf(stderr, "Could not set SIGTERM\n");
|
||||
exit(EX_OSERR);
|
||||
}
|
||||
if (signal(SIGHUP, takesig) == SIG_ERR) {
|
||||
fprintf(stderr, "Could not set SIGHUP\n");
|
||||
exit(EX_OSERR);
|
||||
}
|
||||
if (signal(SIGALRM, takesig) == SIG_ERR) {
|
||||
fprintf(stderr, "Could not set SIGALRM\n");
|
||||
exit(EX_OSERR);
|
||||
}
|
||||
alarm(LOAD_RECALCTIMER);
|
||||
calcload();
|
||||
|
||||
if (init_listen_sock(config.port) < 0) {
|
||||
log("unable to bind port %d!", config.port);
|
||||
exit(EX_UNAVAILABLE);
|
||||
}
|
||||
if (changeid == TRUE) {
|
||||
setuid(thisuser->pw_uid);
|
||||
setgid(thisuser->pw_gid);
|
||||
}
|
||||
log("now accepting connections.");
|
||||
|
||||
#ifdef FILTER_ENABLE
|
||||
if (config.filter)
|
||||
filter_init();
|
||||
#endif /* FILTER_ENABLE */
|
||||
|
||||
while (config.quit == FALSE) {
|
||||
if (getreqs() < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef FILTER_ENABLE
|
||||
if (config.filter)
|
||||
filter_destroy();
|
||||
#endif /* FILTER_ENABLE */
|
||||
|
||||
log("shutting down.");
|
||||
de_init_listen_sock();
|
||||
|
||||
if (config.syslog == FALSE)
|
||||
fclose(config.logf);
|
||||
else
|
||||
closelog();
|
||||
|
||||
allowedptr = allowedhdrs;
|
||||
while (allowedptr) {
|
||||
struct allowedhdr_s *delptr = NULL;
|
||||
delptr = allowedptr;
|
||||
safefree(delptr->hdrname);
|
||||
allowedptr = delptr->next;
|
||||
safefree(delptr);
|
||||
}
|
||||
|
||||
/* finsih up ADNS */
|
||||
adns_finish(adns);
|
||||
|
||||
exit(EX_OK);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/* $Id: tinyproxy.h,v 1.1.1.1 2000-02-16 17:32:23 sdyoung Exp $
|
||||
*
|
||||
* See 'tinyproxy.c' for a detailed description.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _TINYPROXY_H_
|
||||
#define _TINYPROXY_H_ 1
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
/* Global variables for the main controls of the program */
|
||||
#define BUFFER (1024 * 2) /* Size of buffer for reading */
|
||||
#define MAXLISTEN 128 /* Max number of connections to listen for */
|
||||
|
||||
/* Make a new type: flag */
|
||||
typedef char flag;
|
||||
|
||||
/* Other stuff */
|
||||
#define FALSE (0)
|
||||
#define TRUE (!FALSE)
|
||||
|
||||
struct config_s {
|
||||
FILE *logf;
|
||||
char *logf_name;
|
||||
flag syslog;
|
||||
float cutoffload;
|
||||
int port;
|
||||
char *stathost;
|
||||
flag quit;
|
||||
char *changeuser;
|
||||
flag anonymous;
|
||||
char *subnet;
|
||||
char *ipAddr;
|
||||
#ifdef FILTER_ENABLE
|
||||
char *filter;
|
||||
#endif /* FILTER_ENABLE */
|
||||
flag restricted;
|
||||
#ifdef XTINYPROXY
|
||||
char *my_domain;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct stat_s {
|
||||
unsigned long int num_reqs;
|
||||
unsigned long int num_cons;
|
||||
unsigned long int num_badcons;
|
||||
unsigned long int num_opens;
|
||||
unsigned long int num_listens;
|
||||
unsigned long int num_tx;
|
||||
unsigned long int num_rx;
|
||||
unsigned long int num_garbage;
|
||||
unsigned long int num_idles;
|
||||
unsigned long int num_refused;
|
||||
};
|
||||
|
||||
struct allowedhdr_s {
|
||||
char *hdrname;
|
||||
struct allowedhdr_s *next;
|
||||
};
|
||||
|
||||
/* Global Structures used in the program */
|
||||
extern struct config_s config;
|
||||
extern struct stat_s stats;
|
||||
extern struct allowedhdr_s *allowedhdrs;
|
||||
extern float load;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,124 @@
|
||||
/* $Id: uri.c,v 1.1.1.1 2000-02-16 17:32:23 sdyoung Exp $
|
||||
*
|
||||
* This borrows the REGEX from RFC2396 to split a URI string into the five
|
||||
* primary components. The components are:
|
||||
* scheme the uri method (like "http", "ftp", "gopher")
|
||||
* authority the domain and optional ":" port
|
||||
* path path to the document/resource
|
||||
* query an optional query (separated with a "?")
|
||||
* fragment an optional fragement (separated with a "#")
|
||||
*
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "uri.h"
|
||||
#include "utils.h"
|
||||
#include "log.h"
|
||||
#include "regexp.h"
|
||||
|
||||
#define NMATCH 10
|
||||
|
||||
#define URIPATTERN "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"
|
||||
|
||||
#define SCHEME 2
|
||||
#define AUTHORITY 4
|
||||
#define PATH 5
|
||||
#define QUERY_MARK 6
|
||||
#define QUERY 7
|
||||
#define FRAGMENT_MARK 8
|
||||
#define FRAGMENT 9
|
||||
|
||||
static int extract_uri(regmatch_t pmatch[], const char *buffer, char **section,
|
||||
int substring)
|
||||
{
|
||||
int len = pmatch[substring].rm_eo - pmatch[substring].rm_so;
|
||||
if ((*section = xmalloc(len + 1)) == NULL)
|
||||
return -1;
|
||||
|
||||
memset(*section, '\0', len + 1);
|
||||
memcpy(*section, buffer + pmatch[substring].rm_so, len);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void free_uri(URI * uri)
|
||||
{
|
||||
safefree(uri->scheme);
|
||||
safefree(uri->authority);
|
||||
safefree(uri->path);
|
||||
safefree(uri->query);
|
||||
safefree(uri->fragment);
|
||||
safefree(uri);
|
||||
}
|
||||
|
||||
URI *explode_uri(const char *string)
|
||||
{
|
||||
URI *uri;
|
||||
regmatch_t pmatch[NMATCH];
|
||||
regex_t preg;
|
||||
|
||||
if (!(uri = xmalloc(sizeof(URI))))
|
||||
return NULL;
|
||||
memset(uri, 0, sizeof(URI));
|
||||
|
||||
if (regcomp(&preg, URIPATTERN, REG_EXTENDED) != 0) {
|
||||
log("ERROR explode_uri: regcomp");
|
||||
goto ERROR_EXIT;
|
||||
}
|
||||
|
||||
if (regexec(&preg, string, NMATCH, pmatch, 0) != 0) {
|
||||
log("ERROR explode_uri: regexec");
|
||||
goto ERROR_EXIT;
|
||||
}
|
||||
|
||||
regfree(&preg);
|
||||
|
||||
if (pmatch[SCHEME].rm_so != -1) {
|
||||
if (extract_uri(pmatch, string, &uri->scheme, SCHEME) < 0)
|
||||
goto ERROR_EXIT;
|
||||
}
|
||||
|
||||
if (pmatch[AUTHORITY].rm_so != -1) {
|
||||
if (extract_uri(pmatch, string, &uri->authority, AUTHORITY) <
|
||||
0) goto ERROR_EXIT;
|
||||
}
|
||||
|
||||
if (pmatch[PATH].rm_so != -1) {
|
||||
if (extract_uri(pmatch, string, &uri->path, PATH) < 0)
|
||||
goto ERROR_EXIT;
|
||||
}
|
||||
|
||||
if (pmatch[QUERY_MARK].rm_so != -1) {
|
||||
if (extract_uri(pmatch, string, &uri->query, QUERY) < 0)
|
||||
goto ERROR_EXIT;
|
||||
}
|
||||
|
||||
if (pmatch[FRAGMENT_MARK].rm_so != -1) {
|
||||
if (extract_uri(pmatch, string, &uri->fragment, FRAGMENT) < 0)
|
||||
goto ERROR_EXIT;
|
||||
}
|
||||
|
||||
return uri;
|
||||
|
||||
ERROR_EXIT:
|
||||
free_uri(uri);
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/* $Id: uri.h,v 1.1.1.1 2000-02-16 17:32:24 sdyoung Exp $
|
||||
*
|
||||
* See 'uri.c' for a detailed description.
|
||||
*
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef __URI_H_
|
||||
#define __URI_H_
|
||||
|
||||
typedef struct {
|
||||
char *scheme;
|
||||
char *authority;
|
||||
char *path;
|
||||
char *query;
|
||||
char *fragment;
|
||||
} URI;
|
||||
|
||||
extern URI *explode_uri(const char *string);
|
||||
extern void free_uri(URI * uri);
|
||||
|
||||
#endif
|
||||
+264
@@ -0,0 +1,264 @@
|
||||
/* $Id: utils.c,v 1.1.1.1 2000-02-16 17:32:24 sdyoung Exp $
|
||||
*
|
||||
* Misc. routines which are used by the various functions to handle strings
|
||||
* and memory allocation and pretty much anything else we can think of. Also,
|
||||
* the load cutoff routine is in here, along with the HTML show stats
|
||||
* function. Could not think of a better place for it, so it's in here.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <defines.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <ctype.h>
|
||||
#include <sysexits.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <sys/stat.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "tinyproxy.h"
|
||||
#include "utils.h"
|
||||
#include "log.h"
|
||||
#include "conns.h"
|
||||
#include "buffer.h"
|
||||
|
||||
char *xstrdup(char *st)
|
||||
{
|
||||
char *p;
|
||||
|
||||
assert(st);
|
||||
|
||||
if (!(p = strdup(st))) {
|
||||
log("ERROR xstrdup: out of memory (%s)", strerror(errno));
|
||||
return NULL;
|
||||
} else {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the start of the needle in the haystack. Limits the search to less
|
||||
* than "length" characters. Returns NULL if the needle is not found.
|
||||
*/
|
||||
char *xstrstr(char *haystack, char *needle, unsigned int length,
|
||||
int case_sensitive)
|
||||
{
|
||||
unsigned int i;
|
||||
/* Used to specify which function to use... need the decl. */
|
||||
int (*fn) (const char *s1, const char *s2, unsigned int n);
|
||||
|
||||
assert(haystack);
|
||||
assert(needle);
|
||||
assert(length > 0);
|
||||
assert(case_sensitive == FALSE || case_sensitive == TRUE);
|
||||
|
||||
if (case_sensitive)
|
||||
fn = strncmp;
|
||||
else
|
||||
fn = strncasecmp;
|
||||
|
||||
if (strlen(needle) > length)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i <= length - strlen(needle); i++) {
|
||||
if ((*fn) (haystack + i, needle, strlen(needle)) == 0)
|
||||
return haystack + i;
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* for-sure malloc
|
||||
*/
|
||||
void *xmalloc(unsigned long int sz)
|
||||
{
|
||||
void *p;
|
||||
|
||||
assert(sz > 0);
|
||||
|
||||
if (!(p = malloc((size_t) sz))) {
|
||||
log("ERROR xmalloc: out of memory (%s)", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
#ifdef USE_PROC
|
||||
int calcload(void)
|
||||
{
|
||||
char buf[BUFFER], *p;
|
||||
FILE *f;
|
||||
|
||||
if (!config.cutoffload) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(f = fopen("/proc/loadavg", "rt"))) {
|
||||
log("unable to read /proc/loadavg");
|
||||
config.cutoffload = 0.0;
|
||||
return -1;
|
||||
}
|
||||
fgets(buf, BUFFER, f);
|
||||
p = strchr(buf, ' ');
|
||||
*p = '\0';
|
||||
load = atof(buf);
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
int calcload(void)
|
||||
{
|
||||
FILE *f;
|
||||
char buf[BUFFER];
|
||||
char *p, *y;
|
||||
|
||||
if (!config.cutoffload) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(f = popen(UPTIME_PATH, "r"))) {
|
||||
log("calcload: unable to exec uptime");
|
||||
config.cutoffload = 0.0;
|
||||
return -1;
|
||||
}
|
||||
fgets(buf, BUFFER, f);
|
||||
p = strrchr(buf, ':');
|
||||
p += 2;
|
||||
y = strchr(p, ',');
|
||||
*y = '\0';
|
||||
load = atof(p);
|
||||
pclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Delete the server's buffer and replace it with a premade message which will
|
||||
* be sent to the client.
|
||||
*/
|
||||
static void update_output_buffer(struct conn_s *connptr, char *outbuf)
|
||||
{
|
||||
assert(connptr);
|
||||
assert(outbuf);
|
||||
|
||||
delete_buffer(connptr->sbuffer);
|
||||
connptr->sbuffer = new_buffer();
|
||||
|
||||
push_buffer(connptr->sbuffer, outbuf, strlen(outbuf));
|
||||
shutdown(connptr->server_fd, 2);
|
||||
connptr->type = CLOSINGCONN;
|
||||
}
|
||||
|
||||
/*
|
||||
* Display the statics of the tinyproxy server.
|
||||
*/
|
||||
int showstats(struct conn_s *connptr)
|
||||
{
|
||||
char *outbuf;
|
||||
static char *msg = "HTTP/1.0 200 OK\r\n" \
|
||||
"Content-type: text/html\r\n\r\n" \
|
||||
"<html><head><title>%s stats</title></head>\r\n" \
|
||||
"<body>\r\n" \
|
||||
"<center><h2>%s run-time statistics</h2></center><hr>\r\n" \
|
||||
"<blockquote>\r\n" \
|
||||
"Number of requests: %lu<br>\r\n" \
|
||||
"Number of connections: %lu<br>\r\n" \
|
||||
"Number of bad connections: %lu<br>\r\n" \
|
||||
"Number of opens: %lu<br>\r\n" \
|
||||
"Number of listens: %lu<br>\r\n" \
|
||||
"Number of bytes (tx): %lu<br>\r\n" \
|
||||
"Number of bytes (rx): %lu<br>\r\n" \
|
||||
"Number of garbage collects:%lu<br>\r\n" \
|
||||
"Number of idle connection kills:%lu<br>\r\n" \
|
||||
"Number of refused connections due to high load:%lu<br>\r\n" \
|
||||
"Current system load average:%.2f" \
|
||||
"(recalculated every % lu seconds)<br>\r\n" \
|
||||
"</blockquote>\r\n</body></html>\r\n";
|
||||
|
||||
assert(connptr);
|
||||
|
||||
outbuf = xmalloc(BUFFER);
|
||||
|
||||
sprintf(outbuf, msg, VERSION, VERSION, stats.num_reqs,
|
||||
stats.num_cons, stats.num_badcons, stats.num_opens,
|
||||
stats.num_listens, stats.num_tx, stats.num_rx,
|
||||
stats.num_garbage, stats.num_idles, stats.num_refused, load,
|
||||
LOAD_RECALCTIMER);
|
||||
|
||||
update_output_buffer(connptr, outbuf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Display an error to the client.
|
||||
*/
|
||||
int httperr(struct conn_s *connptr, int err, char *msg)
|
||||
{
|
||||
char *outbuf;
|
||||
static char *premsg = "HTTP/1.0 %d %s\r\n" \
|
||||
"Content-type: text/html\r\n\r\n" \
|
||||
"<html><head><title>%s</title></head>\r\n" \
|
||||
"<body>\r\n" \
|
||||
"<font size=\"+2\">Cache Error!</font><br>\r\n" \
|
||||
"An error of type %d occurred: %s\r\n" \
|
||||
"<hr>\r\n" \
|
||||
"<font size=\"-1\"><em>Generated by %s</em></font>\r\n" \
|
||||
"</body></html>\r\n";
|
||||
|
||||
assert(connptr);
|
||||
assert(err > 0);
|
||||
assert(msg);
|
||||
|
||||
outbuf = xmalloc(BUFFER);
|
||||
sprintf(outbuf, premsg, err, msg, msg, err, msg, VERSION);
|
||||
|
||||
update_output_buffer(connptr, outbuf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void makedaemon(void)
|
||||
{
|
||||
if (fork() != 0)
|
||||
exit(0);
|
||||
|
||||
setsid();
|
||||
signal(SIGHUP, SIG_IGN);
|
||||
|
||||
if (fork() != 0)
|
||||
exit(0);
|
||||
|
||||
chdir("/");
|
||||
umask(0);
|
||||
|
||||
close(0);
|
||||
close(1);
|
||||
close(2);
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/* $Id: utils.h,v 1.1.1.1 2000-02-16 17:32:24 sdyoung Exp $
|
||||
*
|
||||
* See 'utils.h' for a detailed description.
|
||||
*
|
||||
* Copyright (C) 1998 Steven Young
|
||||
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef __UTILS_H_
|
||||
#define __UTILS_H_ 1
|
||||
|
||||
#include "conns.h"
|
||||
|
||||
#define safefree(x) free(x); x = NULL
|
||||
|
||||
extern char *xstrdup(char *st);
|
||||
extern void *xmalloc(unsigned long int sz);
|
||||
extern char *xstrstr(char *haystack, char *needle, unsigned int length,
|
||||
int case_sensitive);
|
||||
|
||||
extern int showstats(struct conn_s *connptr);
|
||||
extern int httperr(struct conn_s *connptr, int err, char *msg);
|
||||
|
||||
extern int calcload(void);
|
||||
|
||||
extern void makedaemon(void);
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user