Pound-3.0.2/0000775000175000017500000000000014150724211011620 5ustar rosegrosegPound-3.0.2/CMakeLists.txt0000664000175000017500000000576114150724211014371 0ustar rosegroseg# tunable variables - regenerate pound.h for it to take effect set (VERSION "3.0.2") set (MAXBUF 8192) set (MAXHEADERS 128) set (RESURRECT_CYCLE 60) set (RESURRECT_TO 15) set (HASH_FUNCTION HASH_FNV) set (LOG_FACILITY LOG_DAEMON) set (H2TABSIZE 4096) set (H2FRAMESIZE 16384) # from here on there should be nothing to change cmake_minimum_required(VERSION 3.0) project(Pound C) if (CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "GCC" OR CMAKE_C_COMPILER_ID STREQUAL "GNU") set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-result -Wno-cast-qual -Wno-discarded-qualifiers -D_GNU_SOURCE -O2") endif() include_directories(include) file(GLOB SOURCES "src/*.c") add_executable(pound ${SOURCES}) set(CMAKE_THREAD_PREFER_PTHREAD TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) include(CheckIncludeFiles) CHECK_INCLUDE_FILES("stdio.h;pthread.h;yaml.h;nanomsg/nn.h;nanomsg/inproc.h;nanomsg/pipeline.h;nanomsg/pair.h;nanomsg/reqrep.h;stdlib.h;unistd.h;fcntl.h;ctype.h;getopt.h;string.h;syslog.h;sys/types.h;sys/socket.h;netdb.h;sys/stat.h;time.h;poll.h;semaphore.h;pwd.h;grp.h;signal.h;setjmp.h;mbedtls/config.h;mbedtls/certs.h;mbedtls/oid.h;mbedtls/asn1.h;mbedtls/x509.h;mbedtls/entropy.h;mbedtls/ctr_drbg.h;mbedtls/ssl.h;mbedtls/error.h" HAVE_MANDATORY_INCLUDES LANGUAGE C) if(NOT HAVE_MANDATORY_INCLUDES) message(FATAL_ERROR "Missing mandatory header files!") endif() CHECK_INCLUDE_FILES(pcre2posix.h HAVE_PCRE2POSIX_H) CHECK_INCLUDE_FILES(pcre/pcre2posix.h HAVE_PCRE_PCRE2POSIX_H) CHECK_INCLUDE_FILES(pcreposix.h HAVE_PCREPOSIX_H) CHECK_INCLUDE_FILES(pcre/pcreposix.h HAVE_PCRE_PCREPOSIX_H) CHECK_INCLUDE_FILES(regex.h HAVE_REGEX_H) CHECK_INCLUDE_FILES(stdarg.h HAVE_STDARG_H) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/include/pound.h.in ${CMAKE_CURRENT_SOURCE_DIR}/include/pound.h) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/MKDIST.in ${CMAKE_CURRENT_SOURCE_DIR}/MKDIST) target_link_libraries(pound -lyaml) target_link_libraries(pound -lnanomsg) target_link_libraries(pound -lpthread) target_link_libraries(pound Threads::Threads) FIND_LIBRARY(TCMALLOC tcmalloc ) if(TCMALLOC) target_link_libraries(pound -ltcmalloc) endif() FIND_LIBRARY(MBEDCRYPTO mbedcrypto) if(MBEDCRYPTO) target_link_libraries(pound -lmbedcrypto) endif() FIND_LIBRARY(MBEDX509 mbedx509) if(MBEDX509) target_link_libraries(pound -lmbedx509) endif() FIND_LIBRARY(MBEDTLS mbedtls) if(MBEDTLS) target_link_libraries(pound -lmbedtls) endif() if(NOT MBEDCRYPTO OR NOT MBEDX509 OR NOT MBEDTLS) message(FATAL_ERROR "Missing mandatory library MbedTLS!") endif() if(HAVE_PCRE2POSIX_H OR HAVE_PCRE_PCRE2POSIX_H) FIND_LIBRARY(PCRE pcre2-posix) if(PCRE) target_link_libraries(pound -lpcre2-posix -lpcre2-8) else() message(FATAL_ERROR "pcre2posix.h found but libpcre2-posix is not available!") endif() else() if(HAVE_PCREPOSIX_H OR HAVE_PCRE_PCREPOSIX_H) FIND_LIBRARY(PCRE pcreposix) if(PCRE) target_link_libraries(pound -lpcreposix) else() message(FATAL_ERROR "pcreposix.h found but libpcreposix is not available!") endif() endif() endif()Pound-3.0.2/build/0000775000175000017500000000000014150724211012717 5ustar rosegrosegPound-3.0.2/build/.keep0000664000175000017500000000000014150724211013632 0ustar rosegrosegPound-3.0.2/pound/0000775000175000017500000000000014150724211012745 5ustar rosegrosegPound-3.0.2/pound/pound.png0000664000175000017500000000116314150724211014601 0ustar rosegrosegPNG  IHDRd pHYs  %IDATh홱nA T(yt*HȖԩ(BMLx,!R͜4罳F:Y;3#g/2],sW,fߴ&|\^ش~\-6a_AUD !cchl2ct1^>50w&XPXW*\z괥mpH79],ZP,-)!`%wySgJ52n?d} |Vvm_?y~*筊ͣlt%) s<䰏v x -{|Lw_O(00P$r0!nkؖSau(%T_Y/+ csX}(gl]7ES68NJh}B-teSI1V`iXe׵[̝@JB4eRtFR^\>uUp 0Gjx nBąIENDB`Pound-3.0.2/pound/UTHASH.txt0000644000175000017500000000216114150724211014500 0ustar rosegrosegCopyright (c) 2005-2018, Troy D. Hanson http://troydhanson.github.com/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.Pound-3.0.2/include/0000775000175000017500000000000014150724211013243 5ustar rosegrosegPound-3.0.2/include/utarray.h0000664000175000017500000003217514150724211015113 0ustar rosegroseg/* Copyright (c) 2008-2018, Troy D. Hanson http://troydhanson.github.com/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* a dynamic array implementation using macros */ #ifndef UTARRAY_H #define UTARRAY_H #define UTARRAY_VERSION 2.1.0 #include /* size_t */ #include /* memset, etc */ #include /* exit */ #ifdef __GNUC__ #define UTARRAY_UNUSED __attribute__((__unused__)) #else #define UTARRAY_UNUSED #endif #ifdef oom #error "The name of macro 'oom' has been changed to 'utarray_oom'. Please update your code." #define utarray_oom() oom() #endif #ifndef utarray_oom #define utarray_oom() exit(-1) #endif typedef void (ctor_f)(void *dst, const void *src); typedef void (dtor_f)(void *elt); typedef void (init_f)(void *elt); typedef struct { size_t sz; init_f *init; ctor_f *copy; dtor_f *dtor; } UT_icd; typedef struct { unsigned i,n;/* i: index of next available slot, n: num slots */ UT_icd icd; /* initializer, copy and destructor functions */ char *d; /* n slots of size icd->sz*/ } UT_array; #define utarray_init(a,_icd) do { \ memset(a,0,sizeof(UT_array)); \ (a)->icd = *(_icd); \ } while(0) #define utarray_done(a) do { \ if ((a)->n) { \ if ((a)->icd.dtor) { \ unsigned _ut_i; \ for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \ (a)->icd.dtor(utarray_eltptr(a,_ut_i)); \ } \ } \ free((a)->d); \ } \ (a)->n=0; \ } while(0) #define utarray_new(a,_icd) do { \ (a) = (UT_array*)malloc(sizeof(UT_array)); \ if ((a) == NULL) { \ utarray_oom(); \ } \ utarray_init(a,_icd); \ } while(0) #define utarray_free(a) do { \ utarray_done(a); \ free(a); \ } while(0) #define utarray_reserve(a,by) do { \ if (((a)->i+(by)) > (a)->n) { \ char *utarray_tmp; \ while (((a)->i+(by)) > (a)->n) { (a)->n = ((a)->n ? (2*(a)->n) : 8); } \ utarray_tmp=(char*)realloc((a)->d, (a)->n*(a)->icd.sz); \ if (utarray_tmp == NULL) { \ utarray_oom(); \ } \ (a)->d=utarray_tmp; \ } \ } while(0) #define utarray_push_back(a,p) do { \ utarray_reserve(a,1); \ if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,(a)->i++), p); } \ else { memcpy(_utarray_eltptr(a,(a)->i++), p, (a)->icd.sz); }; \ } while(0) #define utarray_pop_back(a) do { \ if ((a)->icd.dtor) { (a)->icd.dtor( _utarray_eltptr(a,--((a)->i))); } \ else { (a)->i--; } \ } while(0) #define utarray_extend_back(a) do { \ utarray_reserve(a,1); \ if ((a)->icd.init) { (a)->icd.init(_utarray_eltptr(a,(a)->i)); } \ else { memset(_utarray_eltptr(a,(a)->i),0,(a)->icd.sz); } \ (a)->i++; \ } while(0) #define utarray_len(a) ((a)->i) #define utarray_eltptr(a,j) (((j) < (a)->i) ? _utarray_eltptr(a,j) : NULL) #define _utarray_eltptr(a,j) ((void*)((a)->d + ((a)->icd.sz * (j)))) #define utarray_insert(a,p,j) do { \ if ((j) > (a)->i) utarray_resize(a,j); \ utarray_reserve(a,1); \ if ((j) < (a)->i) { \ memmove( _utarray_eltptr(a,(j)+1), _utarray_eltptr(a,j), \ ((a)->i - (j))*((a)->icd.sz)); \ } \ if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,j), p); } \ else { memcpy(_utarray_eltptr(a,j), p, (a)->icd.sz); }; \ (a)->i++; \ } while(0) #define utarray_inserta(a,w,j) do { \ if (utarray_len(w) == 0) break; \ if ((j) > (a)->i) utarray_resize(a,j); \ utarray_reserve(a,utarray_len(w)); \ if ((j) < (a)->i) { \ memmove(_utarray_eltptr(a,(j)+utarray_len(w)), \ _utarray_eltptr(a,j), \ ((a)->i - (j))*((a)->icd.sz)); \ } \ if ((a)->icd.copy) { \ unsigned _ut_i; \ for(_ut_i=0;_ut_i<(w)->i;_ut_i++) { \ (a)->icd.copy(_utarray_eltptr(a, (j) + _ut_i), _utarray_eltptr(w, _ut_i)); \ } \ } else { \ memcpy(_utarray_eltptr(a,j), _utarray_eltptr(w,0), \ utarray_len(w)*((a)->icd.sz)); \ } \ (a)->i += utarray_len(w); \ } while(0) #define utarray_resize(dst,num) do { \ unsigned _ut_i; \ if ((dst)->i > (unsigned)(num)) { \ if ((dst)->icd.dtor) { \ for (_ut_i = (num); _ut_i < (dst)->i; ++_ut_i) { \ (dst)->icd.dtor(_utarray_eltptr(dst, _ut_i)); \ } \ } \ } else if ((dst)->i < (unsigned)(num)) { \ utarray_reserve(dst, (num) - (dst)->i); \ if ((dst)->icd.init) { \ for (_ut_i = (dst)->i; _ut_i < (unsigned)(num); ++_ut_i) { \ (dst)->icd.init(_utarray_eltptr(dst, _ut_i)); \ } \ } else { \ memset(_utarray_eltptr(dst, (dst)->i), 0, (dst)->icd.sz*((num) - (dst)->i)); \ } \ } \ (dst)->i = (num); \ } while(0) #define utarray_concat(dst,src) do { \ utarray_inserta(dst, src, utarray_len(dst)); \ } while(0) #define utarray_erase(a,pos,len) do { \ if ((a)->icd.dtor) { \ unsigned _ut_i; \ for (_ut_i = 0; _ut_i < (len); _ut_i++) { \ (a)->icd.dtor(utarray_eltptr(a, (pos) + _ut_i)); \ } \ } \ if ((a)->i > ((pos) + (len))) { \ memmove(_utarray_eltptr(a, pos), _utarray_eltptr(a, (pos) + (len)), \ ((a)->i - ((pos) + (len))) * (a)->icd.sz); \ } \ (a)->i -= (len); \ } while(0) #define utarray_renew(a,u) do { \ if (a) utarray_clear(a); \ else utarray_new(a, u); \ } while(0) #define utarray_clear(a) do { \ if ((a)->i > 0) { \ if ((a)->icd.dtor) { \ unsigned _ut_i; \ for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \ (a)->icd.dtor(_utarray_eltptr(a, _ut_i)); \ } \ } \ (a)->i = 0; \ } \ } while(0) #define utarray_sort(a,cmp) do { \ qsort((a)->d, (a)->i, (a)->icd.sz, cmp); \ } while(0) #define utarray_find(a,v,cmp) bsearch((v),(a)->d,(a)->i,(a)->icd.sz,cmp) #define utarray_front(a) (((a)->i) ? (_utarray_eltptr(a,0)) : NULL) #define utarray_next(a,e) (((e)==NULL) ? utarray_front(a) : (((a)->i != utarray_eltidx(a,e)+1) ? _utarray_eltptr(a,utarray_eltidx(a,e)+1) : NULL)) #define utarray_prev(a,e) (((e)==NULL) ? utarray_back(a) : ((utarray_eltidx(a,e) != 0) ? _utarray_eltptr(a,utarray_eltidx(a,e)-1) : NULL)) #define utarray_back(a) (((a)->i) ? (_utarray_eltptr(a,(a)->i-1)) : NULL) #define utarray_eltidx(a,e) (((char*)(e) - (a)->d) / (a)->icd.sz) /* last we pre-define a few icd for common utarrays of ints and strings */ static void utarray_str_cpy(void *dst, const void *src) { char **_src = (char**)src, **_dst = (char**)dst; *_dst = (*_src == NULL) ? NULL : strdup(*_src); } static void utarray_str_dtor(void *elt) { char **eltc = (char**)elt; if (*eltc != NULL) free(*eltc); } static const UT_icd ut_str_icd UTARRAY_UNUSED = {sizeof(char*),NULL,utarray_str_cpy,utarray_str_dtor}; static const UT_icd ut_int_icd UTARRAY_UNUSED = {sizeof(int),NULL,NULL,NULL}; static const UT_icd ut_ptr_icd UTARRAY_UNUSED = {sizeof(void*),NULL,NULL,NULL}; #endif /* UTARRAY_H */ Pound-3.0.2/include/pound.h.in0000664000175000017500000001274214150724211015154 0ustar rosegroseg/* * Pound - the reverse-proxy load-balancer * Copyright (C) 2002-2020 Apsis GmbH * * This file is part of Pound. * * Pound 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 3 of the License, or * (at your option) any later version. * * Pound 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Contact information: * Apsis GmbH * P.O.Box * 8707 Uetikon am See * Switzerland * EMail: roseg@apsis.ch */ #cmakedefine HAVE_MANDATORY_INCLUDES #cmakedefine HAVE_PCRE2POSIX_H #cmakedefine HAVE_PCRE_PCRE2POSIX_H #cmakedefine HAVE_PCREPOSIX_H #cmakedefine HAVE_PCRE_PCREPOSIX_H #cmakedefine HAVE_REGEX_H #cmakedefine HAVE_STDARG_H #cmakedefine VERSION "@VERSION@" #cmakedefine MAXBUF @MAXBUF@ #cmakedefine MAXHEADERS @MAXHEADERS@ #cmakedefine RESURRECT_TO @RESURRECT_TO@ #cmakedefine RESURRECT_CYCLE @RESURRECT_CYCLE@ #cmakedefine HASH_FUNCTION @HASH_FUNCTION@ #cmakedefine LOG_FACILITY @LOG_FACILITY@ #cmakedefine H2TABSIZE @H2TABSIZE@ #cmakedefine H2FRAMESIZE @H2FRAMESIZE@ #ifdef HAVE_MANDATORY_INCLUDES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #endif #ifdef HAVE_STDARG_H #include #else #include #endif #ifdef HAVE_PCRE2POSIX_H #include #else #ifdef HAVE_PCRE_PCRE2POSIX_H #include #else #ifdef HAVE_PCREPOSIX_H #include #else #ifdef HAVE_PCRE_PCREPOSIX_H #include #else #ifdef HAVE_REGEX_H #include #endif #endif #endif #endif #endif #include "uthash.h" #include "utarray.h" #include "hpack.h" #define F_CONF "/etc/pound/pound.yaml" #define F_PID "/var/run/pound.pid" #ifndef NI_MAXHOST #define NI_MAXHOST 1025 #endif #ifndef NI_MAXSERV #define NI_MAXSERV 32 #endif /* Data structures */ typedef struct { char *root_jail; uid_t user; gid_t group; char *pid; char *err404; char *err405; char *err500; char *http2_preamble[5]; int log_facility; int log_level; } GLOBAL; extern GLOBAL global; typedef struct _backend_ { yaml_node_item_t id; struct addrinfo *addr; /* int rewriteLocation; */ /* int rewriteDestination; */ char *add_header; int alive; int timeout; int threads; int is_dead; int sock_in; int sock; } BACKEND; extern BACKEND *backends; extern int backends_len; typedef struct _service_ { regex_t *url; int session; regex_t *head_require; regex_t *head_deny; BACKEND **backends; int backends_len; int sock_in; int sock; } SERVICE; typedef struct { mbedtls_x509_crt certificate; mbedtls_pk_context key; regex_t *host; int host_len; } SNI; typedef struct _http_listener_ { struct addrinfo *addr; int client; int threads; SERVICE **services; int services_len; mbedtls_ssl_config *conf; SNI **sni; int sock_in; int sock_fan; } HTTP_LISTENER; extern HTTP_LISTENER *http_listeners; extern int http_len; extern mbedtls_ctr_drbg_context tls_ctr_drbg; extern regex_t rex_Response, rex_Expect, rex_ContentLength, rex_Chunked, rex_Connection_Closed, rex_Connection_HTTP2, rex_Upgrade_HTTP2; extern sem_t sem_start; /* config.c */ extern void config(const int, char **); /* util.c */ extern void logmsg(int, const char *, ...); extern void *thr_resurrect(void *); extern void time_stamp(char *); extern int do_sni(void *, mbedtls_ssl_context *, const unsigned char *, size_t); /* backend.c */ extern void *thr_backend(void *); /* http.c */ extern void *thr_service(void *); extern int get_be(HTTP_LISTENER *, char *, char *, char *[], struct hpack_headerblock *); extern void *thr_http(void *); /* http2.c */ extern void do_http2(HTTP_LISTENER *, FILE *, char *, char *, int);Pound-3.0.2/include/uthash.h0000664000175000017500000022031014150724211014706 0ustar rosegroseg/* Copyright (c) 2003-2018, Troy D. Hanson http://troydhanson.github.com/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef UTHASH_H #define UTHASH_H #define UTHASH_VERSION 2.1.0 #include /* memcmp, memset, strlen */ #include /* ptrdiff_t */ #include /* exit */ /* These macros use decltype or the earlier __typeof GNU extension. As decltype is only available in newer compilers (VS2010 or gcc 4.3+ when compiling c++ source) this code uses whatever method is needed or, for VS2008 where neither is available, uses casting workarounds. */ #if !defined(DECLTYPE) && !defined(NO_DECLTYPE) #if defined(_MSC_VER) /* MS compiler */ #if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */ #define DECLTYPE(x) (decltype(x)) #else /* VS2008 or older (or VS2010 in C mode) */ #define NO_DECLTYPE #endif #elif defined(__BORLANDC__) || defined(__ICCARM__) || defined(__LCC__) || defined(__WATCOMC__) #define NO_DECLTYPE #else /* GNU, Sun and other compilers */ #define DECLTYPE(x) (__typeof(x)) #endif #endif #ifdef NO_DECLTYPE #define DECLTYPE(x) #define DECLTYPE_ASSIGN(dst,src) \ do { \ char **_da_dst = (char**)(&(dst)); \ *_da_dst = (char*)(src); \ } while (0) #else #define DECLTYPE_ASSIGN(dst,src) \ do { \ (dst) = DECLTYPE(dst)(src); \ } while (0) #endif /* a number of the hash function use uint32_t which isn't defined on Pre VS2010 */ #if defined(_WIN32) #if defined(_MSC_VER) && _MSC_VER >= 1600 #include #elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__) #include #else typedef unsigned int uint32_t; typedef unsigned char uint8_t; #endif #elif defined(__GNUC__) && !defined(__VXWORKS__) #include #else typedef unsigned int uint32_t; typedef unsigned char uint8_t; #endif #ifndef uthash_malloc #define uthash_malloc(sz) malloc(sz) /* malloc fcn */ #endif #ifndef uthash_free #define uthash_free(ptr,sz) free(ptr) /* free fcn */ #endif #ifndef uthash_bzero #define uthash_bzero(a,n) memset(a,'\0',n) #endif #ifndef uthash_strlen #define uthash_strlen(s) strlen(s) #endif #ifdef uthash_memcmp /* This warning will not catch programs that define uthash_memcmp AFTER including uthash.h. */ #warning "uthash_memcmp is deprecated; please use HASH_KEYCMP instead" #else #define uthash_memcmp(a,b,n) memcmp(a,b,n) #endif #ifndef HASH_KEYCMP #define HASH_KEYCMP(a,b,n) uthash_memcmp(a,b,n) #endif #ifndef uthash_noexpand_fyi #define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */ #endif #ifndef uthash_expand_fyi #define uthash_expand_fyi(tbl) /* can be defined to log expands */ #endif #ifndef HASH_NONFATAL_OOM #define HASH_NONFATAL_OOM 0 #endif #if HASH_NONFATAL_OOM /* malloc failures can be recovered from */ #ifndef uthash_nonfatal_oom #define uthash_nonfatal_oom(obj) do {} while (0) /* non-fatal OOM error */ #endif #define HASH_RECORD_OOM(oomed) do { (oomed) = 1; } while (0) #define IF_HASH_NONFATAL_OOM(x) x #else /* malloc failures result in lost memory, hash tables are unusable */ #ifndef uthash_fatal #define uthash_fatal(msg) exit(-1) /* fatal OOM error */ #endif #define HASH_RECORD_OOM(oomed) uthash_fatal("out of memory") #define IF_HASH_NONFATAL_OOM(x) #endif /* initial number of buckets */ #define HASH_INITIAL_NUM_BUCKETS 32U /* initial number of buckets */ #define HASH_INITIAL_NUM_BUCKETS_LOG2 5U /* lg2 of initial number of buckets */ #define HASH_BKT_CAPACITY_THRESH 10U /* expand when bucket count reaches */ /* calculate the element whose hash handle address is hhp */ #define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho))) /* calculate the hash handle from element address elp */ #define HH_FROM_ELMT(tbl,elp) ((UT_hash_handle*)(void*)(((char*)(elp)) + ((tbl)->hho))) #define HASH_ROLLBACK_BKT(hh, head, itemptrhh) \ do { \ struct UT_hash_handle *_hd_hh_item = (itemptrhh); \ unsigned _hd_bkt; \ HASH_TO_BKT(_hd_hh_item->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \ (head)->hh.tbl->buckets[_hd_bkt].count++; \ _hd_hh_item->hh_next = NULL; \ _hd_hh_item->hh_prev = NULL; \ } while (0) #define HASH_VALUE(keyptr,keylen,hashv) \ do { \ HASH_FCN(keyptr, keylen, hashv); \ } while (0) #define HASH_FIND_BYHASHVALUE(hh,head,keyptr,keylen,hashval,out) \ do { \ (out) = NULL; \ if (head) { \ unsigned _hf_bkt; \ HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _hf_bkt); \ if (HASH_BLOOM_TEST((head)->hh.tbl, hashval) != 0) { \ HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], keyptr, keylen, hashval, out); \ } \ } \ } while (0) #define HASH_FIND(hh,head,keyptr,keylen,out) \ do { \ (out) = NULL; \ if (head) { \ unsigned _hf_hashv; \ HASH_VALUE(keyptr, keylen, _hf_hashv); \ HASH_FIND_BYHASHVALUE(hh, head, keyptr, keylen, _hf_hashv, out); \ } \ } while (0) #ifdef HASH_BLOOM #define HASH_BLOOM_BITLEN (1UL << HASH_BLOOM) #define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8UL) + (((HASH_BLOOM_BITLEN%8UL)!=0UL) ? 1UL : 0UL) #define HASH_BLOOM_MAKE(tbl,oomed) \ do { \ (tbl)->bloom_nbits = HASH_BLOOM; \ (tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \ if (!(tbl)->bloom_bv) { \ HASH_RECORD_OOM(oomed); \ } else { \ uthash_bzero((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \ (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \ } \ } while (0) #define HASH_BLOOM_FREE(tbl) \ do { \ uthash_free((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \ } while (0) #define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8U] |= (1U << ((idx)%8U))) #define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8U] & (1U << ((idx)%8U))) #define HASH_BLOOM_ADD(tbl,hashv) \ HASH_BLOOM_BITSET((tbl)->bloom_bv, ((hashv) & (uint32_t)((1UL << (tbl)->bloom_nbits) - 1U))) #define HASH_BLOOM_TEST(tbl,hashv) \ HASH_BLOOM_BITTEST((tbl)->bloom_bv, ((hashv) & (uint32_t)((1UL << (tbl)->bloom_nbits) - 1U))) #else #define HASH_BLOOM_MAKE(tbl,oomed) #define HASH_BLOOM_FREE(tbl) #define HASH_BLOOM_ADD(tbl,hashv) #define HASH_BLOOM_TEST(tbl,hashv) (1) #define HASH_BLOOM_BYTELEN 0U #endif #define HASH_MAKE_TABLE(hh,head,oomed) \ do { \ (head)->hh.tbl = (UT_hash_table*)uthash_malloc(sizeof(UT_hash_table)); \ if (!(head)->hh.tbl) { \ HASH_RECORD_OOM(oomed); \ } else { \ uthash_bzero((head)->hh.tbl, sizeof(UT_hash_table)); \ (head)->hh.tbl->tail = &((head)->hh); \ (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \ (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \ (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \ (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \ HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \ (head)->hh.tbl->signature = HASH_SIGNATURE; \ if (!(head)->hh.tbl->buckets) { \ HASH_RECORD_OOM(oomed); \ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ } else { \ uthash_bzero((head)->hh.tbl->buckets, \ HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \ HASH_BLOOM_MAKE((head)->hh.tbl, oomed); \ IF_HASH_NONFATAL_OOM( \ if (oomed) { \ uthash_free((head)->hh.tbl->buckets, \ HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ } \ ) \ } \ } \ } while (0) #define HASH_REPLACE_BYHASHVALUE_INORDER(hh,head,fieldname,keylen_in,hashval,add,replaced,cmpfcn) \ do { \ (replaced) = NULL; \ HASH_FIND_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, replaced); \ if (replaced) { \ HASH_DELETE(hh, head, replaced); \ } \ HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, &((add)->fieldname), keylen_in, hashval, add, cmpfcn); \ } while (0) #define HASH_REPLACE_BYHASHVALUE(hh,head,fieldname,keylen_in,hashval,add,replaced) \ do { \ (replaced) = NULL; \ HASH_FIND_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, replaced); \ if (replaced) { \ HASH_DELETE(hh, head, replaced); \ } \ HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, add); \ } while (0) #define HASH_REPLACE(hh,head,fieldname,keylen_in,add,replaced) \ do { \ unsigned _hr_hashv; \ HASH_VALUE(&((add)->fieldname), keylen_in, _hr_hashv); \ HASH_REPLACE_BYHASHVALUE(hh, head, fieldname, keylen_in, _hr_hashv, add, replaced); \ } while (0) #define HASH_REPLACE_INORDER(hh,head,fieldname,keylen_in,add,replaced,cmpfcn) \ do { \ unsigned _hr_hashv; \ HASH_VALUE(&((add)->fieldname), keylen_in, _hr_hashv); \ HASH_REPLACE_BYHASHVALUE_INORDER(hh, head, fieldname, keylen_in, _hr_hashv, add, replaced, cmpfcn); \ } while (0) #define HASH_APPEND_LIST(hh, head, add) \ do { \ (add)->hh.next = NULL; \ (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \ (head)->hh.tbl->tail->next = (add); \ (head)->hh.tbl->tail = &((add)->hh); \ } while (0) #define HASH_AKBI_INNER_LOOP(hh,head,add,cmpfcn) \ do { \ do { \ if (cmpfcn(DECLTYPE(head)(_hs_iter), add) > 0) { \ break; \ } \ } while ((_hs_iter = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->next)); \ } while (0) #ifdef NO_DECLTYPE #undef HASH_AKBI_INNER_LOOP #define HASH_AKBI_INNER_LOOP(hh,head,add,cmpfcn) \ do { \ char *_hs_saved_head = (char*)(head); \ do { \ DECLTYPE_ASSIGN(head, _hs_iter); \ if (cmpfcn(head, add) > 0) { \ DECLTYPE_ASSIGN(head, _hs_saved_head); \ break; \ } \ DECLTYPE_ASSIGN(head, _hs_saved_head); \ } while ((_hs_iter = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->next)); \ } while (0) #endif #if HASH_NONFATAL_OOM #define HASH_ADD_TO_TABLE(hh,head,keyptr,keylen_in,hashval,add,oomed) \ do { \ if (!(oomed)) { \ unsigned _ha_bkt; \ (head)->hh.tbl->num_items++; \ HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \ HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], hh, &(add)->hh, oomed); \ if (oomed) { \ HASH_ROLLBACK_BKT(hh, head, &(add)->hh); \ HASH_DELETE_HH(hh, head, &(add)->hh); \ (add)->hh.tbl = NULL; \ uthash_nonfatal_oom(add); \ } else { \ HASH_BLOOM_ADD((head)->hh.tbl, hashval); \ HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \ } \ } else { \ (add)->hh.tbl = NULL; \ uthash_nonfatal_oom(add); \ } \ } while (0) #else #define HASH_ADD_TO_TABLE(hh,head,keyptr,keylen_in,hashval,add,oomed) \ do { \ unsigned _ha_bkt; \ (head)->hh.tbl->num_items++; \ HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \ HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], hh, &(add)->hh, oomed); \ HASH_BLOOM_ADD((head)->hh.tbl, hashval); \ HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \ } while (0) #endif #define HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh,head,keyptr,keylen_in,hashval,add,cmpfcn) \ do { \ IF_HASH_NONFATAL_OOM( int _ha_oomed = 0; ) \ (add)->hh.hashv = (hashval); \ (add)->hh.key = (char*) (keyptr); \ (add)->hh.keylen = (unsigned) (keylen_in); \ if (!(head)) { \ (add)->hh.next = NULL; \ (add)->hh.prev = NULL; \ HASH_MAKE_TABLE(hh, add, _ha_oomed); \ IF_HASH_NONFATAL_OOM( if (!_ha_oomed) { ) \ (head) = (add); \ IF_HASH_NONFATAL_OOM( } ) \ } else { \ void *_hs_iter = (head); \ (add)->hh.tbl = (head)->hh.tbl; \ HASH_AKBI_INNER_LOOP(hh, head, add, cmpfcn); \ if (_hs_iter) { \ (add)->hh.next = _hs_iter; \ if (((add)->hh.prev = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->prev)) { \ HH_FROM_ELMT((head)->hh.tbl, (add)->hh.prev)->next = (add); \ } else { \ (head) = (add); \ } \ HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->prev = (add); \ } else { \ HASH_APPEND_LIST(hh, head, add); \ } \ } \ HASH_ADD_TO_TABLE(hh, head, keyptr, keylen_in, hashval, add, _ha_oomed); \ HASH_FSCK(hh, head, "HASH_ADD_KEYPTR_BYHASHVALUE_INORDER"); \ } while (0) #define HASH_ADD_KEYPTR_INORDER(hh,head,keyptr,keylen_in,add,cmpfcn) \ do { \ unsigned _hs_hashv; \ HASH_VALUE(keyptr, keylen_in, _hs_hashv); \ HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, keyptr, keylen_in, _hs_hashv, add, cmpfcn); \ } while (0) #define HASH_ADD_BYHASHVALUE_INORDER(hh,head,fieldname,keylen_in,hashval,add,cmpfcn) \ HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, &((add)->fieldname), keylen_in, hashval, add, cmpfcn) #define HASH_ADD_INORDER(hh,head,fieldname,keylen_in,add,cmpfcn) \ HASH_ADD_KEYPTR_INORDER(hh, head, &((add)->fieldname), keylen_in, add, cmpfcn) #define HASH_ADD_KEYPTR_BYHASHVALUE(hh,head,keyptr,keylen_in,hashval,add) \ do { \ IF_HASH_NONFATAL_OOM( int _ha_oomed = 0; ) \ (add)->hh.hashv = (hashval); \ (add)->hh.key = (char*) (keyptr); \ (add)->hh.keylen = (unsigned) (keylen_in); \ if (!(head)) { \ (add)->hh.next = NULL; \ (add)->hh.prev = NULL; \ HASH_MAKE_TABLE(hh, add, _ha_oomed); \ IF_HASH_NONFATAL_OOM( if (!_ha_oomed) { ) \ (head) = (add); \ IF_HASH_NONFATAL_OOM( } ) \ } else { \ (add)->hh.tbl = (head)->hh.tbl; \ HASH_APPEND_LIST(hh, head, add); \ } \ HASH_ADD_TO_TABLE(hh, head, keyptr, keylen_in, hashval, add, _ha_oomed); \ HASH_FSCK(hh, head, "HASH_ADD_KEYPTR_BYHASHVALUE"); \ } while (0) #define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \ do { \ unsigned _ha_hashv; \ HASH_VALUE(keyptr, keylen_in, _ha_hashv); \ HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, keyptr, keylen_in, _ha_hashv, add); \ } while (0) #define HASH_ADD_BYHASHVALUE(hh,head,fieldname,keylen_in,hashval,add) \ HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, add) #define HASH_ADD(hh,head,fieldname,keylen_in,add) \ HASH_ADD_KEYPTR(hh, head, &((add)->fieldname), keylen_in, add) #define HASH_TO_BKT(hashv,num_bkts,bkt) \ do { \ bkt = ((hashv) & ((num_bkts) - 1U)); \ } while (0) /* delete "delptr" from the hash table. * "the usual" patch-up process for the app-order doubly-linked-list. * The use of _hd_hh_del below deserves special explanation. * These used to be expressed using (delptr) but that led to a bug * if someone used the same symbol for the head and deletee, like * HASH_DELETE(hh,users,users); * We want that to work, but by changing the head (users) below * we were forfeiting our ability to further refer to the deletee (users) * in the patch-up process. Solution: use scratch space to * copy the deletee pointer, then the latter references are via that * scratch pointer rather than through the repointed (users) symbol. */ #define HASH_DELETE(hh,head,delptr) \ HASH_DELETE_HH(hh, head, &(delptr)->hh) #define HASH_DELETE_HH(hh,head,delptrhh) \ do { \ struct UT_hash_handle *_hd_hh_del = (delptrhh); \ if ((_hd_hh_del->prev == NULL) && (_hd_hh_del->next == NULL)) { \ HASH_BLOOM_FREE((head)->hh.tbl); \ uthash_free((head)->hh.tbl->buckets, \ (head)->hh.tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ (head) = NULL; \ } else { \ unsigned _hd_bkt; \ if (_hd_hh_del == (head)->hh.tbl->tail) { \ (head)->hh.tbl->tail = HH_FROM_ELMT((head)->hh.tbl, _hd_hh_del->prev); \ } \ if (_hd_hh_del->prev != NULL) { \ HH_FROM_ELMT((head)->hh.tbl, _hd_hh_del->prev)->next = _hd_hh_del->next; \ } else { \ DECLTYPE_ASSIGN(head, _hd_hh_del->next); \ } \ if (_hd_hh_del->next != NULL) { \ HH_FROM_ELMT((head)->hh.tbl, _hd_hh_del->next)->prev = _hd_hh_del->prev; \ } \ HASH_TO_BKT(_hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \ HASH_DEL_IN_BKT((head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \ (head)->hh.tbl->num_items--; \ } \ HASH_FSCK(hh, head, "HASH_DELETE_HH"); \ } while (0) /* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */ #define HASH_FIND_STR(head,findstr,out) \ do { \ unsigned _uthash_hfstr_keylen = (unsigned)uthash_strlen(findstr); \ HASH_FIND(hh, head, findstr, _uthash_hfstr_keylen, out); \ } while (0) #define HASH_ADD_STR(head,strfield,add) \ do { \ unsigned _uthash_hastr_keylen = (unsigned)uthash_strlen((add)->strfield); \ HASH_ADD(hh, head, strfield[0], _uthash_hastr_keylen, add); \ } while (0) #define HASH_REPLACE_STR(head,strfield,add,replaced) \ do { \ unsigned _uthash_hrstr_keylen = (unsigned)uthash_strlen((add)->strfield); \ HASH_REPLACE(hh, head, strfield[0], _uthash_hrstr_keylen, add, replaced); \ } while (0) #define HASH_FIND_INT(head,findint,out) \ HASH_FIND(hh,head,findint,sizeof(int),out) #define HASH_ADD_INT(head,intfield,add) \ HASH_ADD(hh,head,intfield,sizeof(int),add) #define HASH_REPLACE_INT(head,intfield,add,replaced) \ HASH_REPLACE(hh,head,intfield,sizeof(int),add,replaced) #define HASH_FIND_PTR(head,findptr,out) \ HASH_FIND(hh,head,findptr,sizeof(void *),out) #define HASH_ADD_PTR(head,ptrfield,add) \ HASH_ADD(hh,head,ptrfield,sizeof(void *),add) #define HASH_REPLACE_PTR(head,ptrfield,add,replaced) \ HASH_REPLACE(hh,head,ptrfield,sizeof(void *),add,replaced) #define HASH_DEL(head,delptr) \ HASH_DELETE(hh,head,delptr) /* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined. * This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined. */ #ifdef HASH_DEBUG #include /* fprintf, stderr */ #define HASH_OOPS(...) do { fprintf(stderr, __VA_ARGS__); exit(-1); } while (0) #define HASH_FSCK(hh,head,where) \ do { \ struct UT_hash_handle *_thh; \ if (head) { \ unsigned _bkt_i; \ unsigned _count = 0; \ char *_prev; \ for (_bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; ++_bkt_i) { \ unsigned _bkt_count = 0; \ _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \ _prev = NULL; \ while (_thh) { \ if (_prev != (char*)(_thh->hh_prev)) { \ HASH_OOPS("%s: invalid hh_prev %p, actual %p\n", \ (where), (void*)_thh->hh_prev, (void*)_prev); \ } \ _bkt_count++; \ _prev = (char*)(_thh); \ _thh = _thh->hh_next; \ } \ _count += _bkt_count; \ if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \ HASH_OOPS("%s: invalid bucket count %u, actual %u\n", \ (where), (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \ } \ } \ if (_count != (head)->hh.tbl->num_items) { \ HASH_OOPS("%s: invalid hh item count %u, actual %u\n", \ (where), (head)->hh.tbl->num_items, _count); \ } \ _count = 0; \ _prev = NULL; \ _thh = &(head)->hh; \ while (_thh) { \ _count++; \ if (_prev != (char*)_thh->prev) { \ HASH_OOPS("%s: invalid prev %p, actual %p\n", \ (where), (void*)_thh->prev, (void*)_prev); \ } \ _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \ _thh = (_thh->next ? HH_FROM_ELMT((head)->hh.tbl, _thh->next) : NULL); \ } \ if (_count != (head)->hh.tbl->num_items) { \ HASH_OOPS("%s: invalid app item count %u, actual %u\n", \ (where), (head)->hh.tbl->num_items, _count); \ } \ } \ } while (0) #else #define HASH_FSCK(hh,head,where) #endif /* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to * the descriptor to which this macro is defined for tuning the hash function. * The app can #include to get the prototype for write(2). */ #ifdef HASH_EMIT_KEYS #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \ do { \ unsigned _klen = fieldlen; \ write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \ write(HASH_EMIT_KEYS, keyptr, (unsigned long)fieldlen); \ } while (0) #else #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) #endif /* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */ #ifdef HASH_FUNCTION #define HASH_FCN HASH_FUNCTION #else #define HASH_FCN HASH_JEN #endif /* The Bernstein hash function, used in Perl prior to v5.6. Note (x<<5+x)=x*33. */ #define HASH_BER(key,keylen,hashv) \ do { \ unsigned _hb_keylen = (unsigned)keylen; \ const unsigned char *_hb_key = (const unsigned char*)(key); \ (hashv) = 0; \ while (_hb_keylen-- != 0U) { \ (hashv) = (((hashv) << 5) + (hashv)) + *_hb_key++; \ } \ } while (0) /* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */ #define HASH_SAX(key,keylen,hashv) \ do { \ unsigned _sx_i; \ const unsigned char *_hs_key = (const unsigned char*)(key); \ hashv = 0; \ for (_sx_i=0; _sx_i < keylen; _sx_i++) { \ hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \ } \ } while (0) /* FNV-1a variation */ #define HASH_FNV(key,keylen,hashv) \ do { \ unsigned _fn_i; \ const unsigned char *_hf_key = (const unsigned char*)(key); \ (hashv) = 2166136261U; \ for (_fn_i=0; _fn_i < keylen; _fn_i++) { \ hashv = hashv ^ _hf_key[_fn_i]; \ hashv = hashv * 16777619U; \ } \ } while (0) #define HASH_OAT(key,keylen,hashv) \ do { \ unsigned _ho_i; \ const unsigned char *_ho_key=(const unsigned char*)(key); \ hashv = 0; \ for(_ho_i=0; _ho_i < keylen; _ho_i++) { \ hashv += _ho_key[_ho_i]; \ hashv += (hashv << 10); \ hashv ^= (hashv >> 6); \ } \ hashv += (hashv << 3); \ hashv ^= (hashv >> 11); \ hashv += (hashv << 15); \ } while (0) #define HASH_JEN_MIX(a,b,c) \ do { \ a -= b; a -= c; a ^= ( c >> 13 ); \ b -= c; b -= a; b ^= ( a << 8 ); \ c -= a; c -= b; c ^= ( b >> 13 ); \ a -= b; a -= c; a ^= ( c >> 12 ); \ b -= c; b -= a; b ^= ( a << 16 ); \ c -= a; c -= b; c ^= ( b >> 5 ); \ a -= b; a -= c; a ^= ( c >> 3 ); \ b -= c; b -= a; b ^= ( a << 10 ); \ c -= a; c -= b; c ^= ( b >> 15 ); \ } while (0) #define HASH_JEN(key,keylen,hashv) \ do { \ unsigned _hj_i,_hj_j,_hj_k; \ unsigned const char *_hj_key=(unsigned const char*)(key); \ hashv = 0xfeedbeefu; \ _hj_i = _hj_j = 0x9e3779b9u; \ _hj_k = (unsigned)(keylen); \ while (_hj_k >= 12U) { \ _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \ + ( (unsigned)_hj_key[2] << 16 ) \ + ( (unsigned)_hj_key[3] << 24 ) ); \ _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \ + ( (unsigned)_hj_key[6] << 16 ) \ + ( (unsigned)_hj_key[7] << 24 ) ); \ hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \ + ( (unsigned)_hj_key[10] << 16 ) \ + ( (unsigned)_hj_key[11] << 24 ) ); \ \ HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ \ _hj_key += 12; \ _hj_k -= 12U; \ } \ hashv += (unsigned)(keylen); \ switch ( _hj_k ) { \ case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); /* FALLTHROUGH */ \ case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); /* FALLTHROUGH */ \ case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); /* FALLTHROUGH */ \ case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); /* FALLTHROUGH */ \ case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); /* FALLTHROUGH */ \ case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); /* FALLTHROUGH */ \ case 5: _hj_j += _hj_key[4]; /* FALLTHROUGH */ \ case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); /* FALLTHROUGH */ \ case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); /* FALLTHROUGH */ \ case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); /* FALLTHROUGH */ \ case 1: _hj_i += _hj_key[0]; \ } \ HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ } while (0) /* The Paul Hsieh hash function */ #undef get16bits #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) #define get16bits(d) (*((const uint16_t *) (d))) #endif #if !defined (get16bits) #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \ +(uint32_t)(((const uint8_t *)(d))[0]) ) #endif #define HASH_SFH(key,keylen,hashv) \ do { \ unsigned const char *_sfh_key=(unsigned const char*)(key); \ uint32_t _sfh_tmp, _sfh_len = (uint32_t)keylen; \ \ unsigned _sfh_rem = _sfh_len & 3U; \ _sfh_len >>= 2; \ hashv = 0xcafebabeu; \ \ /* Main loop */ \ for (;_sfh_len > 0U; _sfh_len--) { \ hashv += get16bits (_sfh_key); \ _sfh_tmp = ((uint32_t)(get16bits (_sfh_key+2)) << 11) ^ hashv; \ hashv = (hashv << 16) ^ _sfh_tmp; \ _sfh_key += 2U*sizeof (uint16_t); \ hashv += hashv >> 11; \ } \ \ /* Handle end cases */ \ switch (_sfh_rem) { \ case 3: hashv += get16bits (_sfh_key); \ hashv ^= hashv << 16; \ hashv ^= (uint32_t)(_sfh_key[sizeof (uint16_t)]) << 18; \ hashv += hashv >> 11; \ break; \ case 2: hashv += get16bits (_sfh_key); \ hashv ^= hashv << 11; \ hashv += hashv >> 17; \ break; \ case 1: hashv += *_sfh_key; \ hashv ^= hashv << 10; \ hashv += hashv >> 1; \ } \ \ /* Force "avalanching" of final 127 bits */ \ hashv ^= hashv << 3; \ hashv += hashv >> 5; \ hashv ^= hashv << 4; \ hashv += hashv >> 17; \ hashv ^= hashv << 25; \ hashv += hashv >> 6; \ } while (0) /* iterate over items in a known bucket to find desired item */ #define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,hashval,out) \ do { \ if ((head).hh_head != NULL) { \ DECLTYPE_ASSIGN(out, ELMT_FROM_HH(tbl, (head).hh_head)); \ } else { \ (out) = NULL; \ } \ while ((out) != NULL) { \ if ((out)->hh.hashv == (hashval) && (out)->hh.keylen == (keylen_in)) { \ if (HASH_KEYCMP((out)->hh.key, keyptr, keylen_in) == 0) { \ break; \ } \ } \ if ((out)->hh.hh_next != NULL) { \ DECLTYPE_ASSIGN(out, ELMT_FROM_HH(tbl, (out)->hh.hh_next)); \ } else { \ (out) = NULL; \ } \ } \ } while (0) /* add an item to a bucket */ #define HASH_ADD_TO_BKT(head,hh,addhh,oomed) \ do { \ UT_hash_bucket *_ha_head = &(head); \ _ha_head->count++; \ (addhh)->hh_next = _ha_head->hh_head; \ (addhh)->hh_prev = NULL; \ if (_ha_head->hh_head != NULL) { \ _ha_head->hh_head->hh_prev = (addhh); \ } \ _ha_head->hh_head = (addhh); \ if ((_ha_head->count >= ((_ha_head->expand_mult + 1U) * HASH_BKT_CAPACITY_THRESH)) \ && !(addhh)->tbl->noexpand) { \ HASH_EXPAND_BUCKETS(addhh,(addhh)->tbl, oomed); \ IF_HASH_NONFATAL_OOM( \ if (oomed) { \ HASH_DEL_IN_BKT(head,addhh); \ } \ ) \ } \ } while (0) /* remove an item from a given bucket */ #define HASH_DEL_IN_BKT(head,delhh) \ do { \ UT_hash_bucket *_hd_head = &(head); \ _hd_head->count--; \ if (_hd_head->hh_head == (delhh)) { \ _hd_head->hh_head = (delhh)->hh_next; \ } \ if ((delhh)->hh_prev) { \ (delhh)->hh_prev->hh_next = (delhh)->hh_next; \ } \ if ((delhh)->hh_next) { \ (delhh)->hh_next->hh_prev = (delhh)->hh_prev; \ } \ } while (0) /* Bucket expansion has the effect of doubling the number of buckets * and redistributing the items into the new buckets. Ideally the * items will distribute more or less evenly into the new buckets * (the extent to which this is true is a measure of the quality of * the hash function as it applies to the key domain). * * With the items distributed into more buckets, the chain length * (item count) in each bucket is reduced. Thus by expanding buckets * the hash keeps a bound on the chain length. This bounded chain * length is the essence of how a hash provides constant time lookup. * * The calculation of tbl->ideal_chain_maxlen below deserves some * explanation. First, keep in mind that we're calculating the ideal * maximum chain length based on the *new* (doubled) bucket count. * In fractions this is just n/b (n=number of items,b=new num buckets). * Since the ideal chain length is an integer, we want to calculate * ceil(n/b). We don't depend on floating point arithmetic in this * hash, so to calculate ceil(n/b) with integers we could write * * ceil(n/b) = (n/b) + ((n%b)?1:0) * * and in fact a previous version of this hash did just that. * But now we have improved things a bit by recognizing that b is * always a power of two. We keep its base 2 log handy (call it lb), * so now we can write this with a bit shift and logical AND: * * ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0) * */ #define HASH_EXPAND_BUCKETS(hh,tbl,oomed) \ do { \ unsigned _he_bkt; \ unsigned _he_bkt_i; \ struct UT_hash_handle *_he_thh, *_he_hh_nxt; \ UT_hash_bucket *_he_new_buckets, *_he_newbkt; \ _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \ 2UL * (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \ if (!_he_new_buckets) { \ HASH_RECORD_OOM(oomed); \ } else { \ uthash_bzero(_he_new_buckets, \ 2UL * (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \ (tbl)->ideal_chain_maxlen = \ ((tbl)->num_items >> ((tbl)->log2_num_buckets+1U)) + \ ((((tbl)->num_items & (((tbl)->num_buckets*2U)-1U)) != 0U) ? 1U : 0U); \ (tbl)->nonideal_items = 0; \ for (_he_bkt_i = 0; _he_bkt_i < (tbl)->num_buckets; _he_bkt_i++) { \ _he_thh = (tbl)->buckets[ _he_bkt_i ].hh_head; \ while (_he_thh != NULL) { \ _he_hh_nxt = _he_thh->hh_next; \ HASH_TO_BKT(_he_thh->hashv, (tbl)->num_buckets * 2U, _he_bkt); \ _he_newbkt = &(_he_new_buckets[_he_bkt]); \ if (++(_he_newbkt->count) > (tbl)->ideal_chain_maxlen) { \ (tbl)->nonideal_items++; \ if (_he_newbkt->count > _he_newbkt->expand_mult * (tbl)->ideal_chain_maxlen) { \ _he_newbkt->expand_mult++; \ } \ } \ _he_thh->hh_prev = NULL; \ _he_thh->hh_next = _he_newbkt->hh_head; \ if (_he_newbkt->hh_head != NULL) { \ _he_newbkt->hh_head->hh_prev = _he_thh; \ } \ _he_newbkt->hh_head = _he_thh; \ _he_thh = _he_hh_nxt; \ } \ } \ uthash_free((tbl)->buckets, (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \ (tbl)->num_buckets *= 2U; \ (tbl)->log2_num_buckets++; \ (tbl)->buckets = _he_new_buckets; \ (tbl)->ineff_expands = ((tbl)->nonideal_items > ((tbl)->num_items >> 1)) ? \ ((tbl)->ineff_expands+1U) : 0U; \ if ((tbl)->ineff_expands > 1U) { \ (tbl)->noexpand = 1; \ uthash_noexpand_fyi(tbl); \ } \ uthash_expand_fyi(tbl); \ } \ } while (0) /* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */ /* Note that HASH_SORT assumes the hash handle name to be hh. * HASH_SRT was added to allow the hash handle name to be passed in. */ #define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn) #define HASH_SRT(hh,head,cmpfcn) \ do { \ unsigned _hs_i; \ unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \ struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \ if (head != NULL) { \ _hs_insize = 1; \ _hs_looping = 1; \ _hs_list = &((head)->hh); \ while (_hs_looping != 0U) { \ _hs_p = _hs_list; \ _hs_list = NULL; \ _hs_tail = NULL; \ _hs_nmerges = 0; \ while (_hs_p != NULL) { \ _hs_nmerges++; \ _hs_q = _hs_p; \ _hs_psize = 0; \ for (_hs_i = 0; _hs_i < _hs_insize; ++_hs_i) { \ _hs_psize++; \ _hs_q = ((_hs_q->next != NULL) ? \ HH_FROM_ELMT((head)->hh.tbl, _hs_q->next) : NULL); \ if (_hs_q == NULL) { \ break; \ } \ } \ _hs_qsize = _hs_insize; \ while ((_hs_psize != 0U) || ((_hs_qsize != 0U) && (_hs_q != NULL))) { \ if (_hs_psize == 0U) { \ _hs_e = _hs_q; \ _hs_q = ((_hs_q->next != NULL) ? \ HH_FROM_ELMT((head)->hh.tbl, _hs_q->next) : NULL); \ _hs_qsize--; \ } else if ((_hs_qsize == 0U) || (_hs_q == NULL)) { \ _hs_e = _hs_p; \ if (_hs_p != NULL) { \ _hs_p = ((_hs_p->next != NULL) ? \ HH_FROM_ELMT((head)->hh.tbl, _hs_p->next) : NULL); \ } \ _hs_psize--; \ } else if ((cmpfcn( \ DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl, _hs_p)), \ DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl, _hs_q)) \ )) <= 0) { \ _hs_e = _hs_p; \ if (_hs_p != NULL) { \ _hs_p = ((_hs_p->next != NULL) ? \ HH_FROM_ELMT((head)->hh.tbl, _hs_p->next) : NULL); \ } \ _hs_psize--; \ } else { \ _hs_e = _hs_q; \ _hs_q = ((_hs_q->next != NULL) ? \ HH_FROM_ELMT((head)->hh.tbl, _hs_q->next) : NULL); \ _hs_qsize--; \ } \ if ( _hs_tail != NULL ) { \ _hs_tail->next = ((_hs_e != NULL) ? \ ELMT_FROM_HH((head)->hh.tbl, _hs_e) : NULL); \ } else { \ _hs_list = _hs_e; \ } \ if (_hs_e != NULL) { \ _hs_e->prev = ((_hs_tail != NULL) ? \ ELMT_FROM_HH((head)->hh.tbl, _hs_tail) : NULL); \ } \ _hs_tail = _hs_e; \ } \ _hs_p = _hs_q; \ } \ if (_hs_tail != NULL) { \ _hs_tail->next = NULL; \ } \ if (_hs_nmerges <= 1U) { \ _hs_looping = 0; \ (head)->hh.tbl->tail = _hs_tail; \ DECLTYPE_ASSIGN(head, ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \ } \ _hs_insize *= 2U; \ } \ HASH_FSCK(hh, head, "HASH_SRT"); \ } \ } while (0) /* This function selects items from one hash into another hash. * The end result is that the selected items have dual presence * in both hashes. There is no copy of the items made; rather * they are added into the new hash through a secondary hash * hash handle that must be present in the structure. */ #define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \ do { \ unsigned _src_bkt, _dst_bkt; \ void *_last_elt = NULL, *_elt; \ UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \ ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \ if ((src) != NULL) { \ for (_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \ for (_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \ _src_hh != NULL; \ _src_hh = _src_hh->hh_next) { \ _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \ if (cond(_elt)) { \ IF_HASH_NONFATAL_OOM( int _hs_oomed = 0; ) \ _dst_hh = (UT_hash_handle*)(void*)(((char*)_elt) + _dst_hho); \ _dst_hh->key = _src_hh->key; \ _dst_hh->keylen = _src_hh->keylen; \ _dst_hh->hashv = _src_hh->hashv; \ _dst_hh->prev = _last_elt; \ _dst_hh->next = NULL; \ if (_last_elt_hh != NULL) { \ _last_elt_hh->next = _elt; \ } \ if ((dst) == NULL) { \ DECLTYPE_ASSIGN(dst, _elt); \ HASH_MAKE_TABLE(hh_dst, dst, _hs_oomed); \ IF_HASH_NONFATAL_OOM( \ if (_hs_oomed) { \ uthash_nonfatal_oom(_elt); \ (dst) = NULL; \ continue; \ } \ ) \ } else { \ _dst_hh->tbl = (dst)->hh_dst.tbl; \ } \ HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \ HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt], hh_dst, _dst_hh, _hs_oomed); \ (dst)->hh_dst.tbl->num_items++; \ IF_HASH_NONFATAL_OOM( \ if (_hs_oomed) { \ HASH_ROLLBACK_BKT(hh_dst, dst, _dst_hh); \ HASH_DELETE_HH(hh_dst, dst, _dst_hh); \ _dst_hh->tbl = NULL; \ uthash_nonfatal_oom(_elt); \ continue; \ } \ ) \ HASH_BLOOM_ADD(_dst_hh->tbl, _dst_hh->hashv); \ _last_elt = _elt; \ _last_elt_hh = _dst_hh; \ } \ } \ } \ } \ HASH_FSCK(hh_dst, dst, "HASH_SELECT"); \ } while (0) #define HASH_CLEAR(hh,head) \ do { \ if ((head) != NULL) { \ HASH_BLOOM_FREE((head)->hh.tbl); \ uthash_free((head)->hh.tbl->buckets, \ (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket)); \ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ (head) = NULL; \ } \ } while (0) #define HASH_OVERHEAD(hh,head) \ (((head) != NULL) ? ( \ (size_t)(((head)->hh.tbl->num_items * sizeof(UT_hash_handle)) + \ ((head)->hh.tbl->num_buckets * sizeof(UT_hash_bucket)) + \ sizeof(UT_hash_table) + \ (HASH_BLOOM_BYTELEN))) : 0U) #ifdef NO_DECLTYPE #define HASH_ITER(hh,head,el,tmp) \ for(((el)=(head)), ((*(char**)(&(tmp)))=(char*)((head!=NULL)?(head)->hh.next:NULL)); \ (el) != NULL; ((el)=(tmp)), ((*(char**)(&(tmp)))=(char*)((tmp!=NULL)?(tmp)->hh.next:NULL))) #else #define HASH_ITER(hh,head,el,tmp) \ for(((el)=(head)), ((tmp)=DECLTYPE(el)((head!=NULL)?(head)->hh.next:NULL)); \ (el) != NULL; ((el)=(tmp)), ((tmp)=DECLTYPE(el)((tmp!=NULL)?(tmp)->hh.next:NULL))) #endif /* obtain a count of items in the hash */ #define HASH_COUNT(head) HASH_CNT(hh,head) #define HASH_CNT(hh,head) ((head != NULL)?((head)->hh.tbl->num_items):0U) typedef struct UT_hash_bucket { struct UT_hash_handle *hh_head; unsigned count; /* expand_mult is normally set to 0. In this situation, the max chain length * threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If * the bucket's chain exceeds this length, bucket expansion is triggered). * However, setting expand_mult to a non-zero value delays bucket expansion * (that would be triggered by additions to this particular bucket) * until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH. * (The multiplier is simply expand_mult+1). The whole idea of this * multiplier is to reduce bucket expansions, since they are expensive, in * situations where we know that a particular bucket tends to be overused. * It is better to let its chain length grow to a longer yet-still-bounded * value, than to do an O(n) bucket expansion too often. */ unsigned expand_mult; } UT_hash_bucket; /* random signature used only to find hash tables in external analysis */ #define HASH_SIGNATURE 0xa0111fe1u #define HASH_BLOOM_SIGNATURE 0xb12220f2u typedef struct UT_hash_table { UT_hash_bucket *buckets; unsigned num_buckets, log2_num_buckets; unsigned num_items; struct UT_hash_handle *tail; /* tail hh in app order, for fast append */ ptrdiff_t hho; /* hash handle offset (byte pos of hash handle in element */ /* in an ideal situation (all buckets used equally), no bucket would have * more than ceil(#items/#buckets) items. that's the ideal chain length. */ unsigned ideal_chain_maxlen; /* nonideal_items is the number of items in the hash whose chain position * exceeds the ideal chain maxlen. these items pay the penalty for an uneven * hash distribution; reaching them in a chain traversal takes >ideal steps */ unsigned nonideal_items; /* ineffective expands occur when a bucket doubling was performed, but * afterward, more than half the items in the hash had nonideal chain * positions. If this happens on two consecutive expansions we inhibit any * further expansion, as it's not helping; this happens when the hash * function isn't a good fit for the key domain. When expansion is inhibited * the hash will still work, albeit no longer in constant time. */ unsigned ineff_expands, noexpand; uint32_t signature; /* used only to find hash tables in external analysis */ #ifdef HASH_BLOOM uint32_t bloom_sig; /* used only to test bloom exists in external analysis */ uint8_t *bloom_bv; uint8_t bloom_nbits; #endif } UT_hash_table; typedef struct UT_hash_handle { struct UT_hash_table *tbl; void *prev; /* prev element in app order */ void *next; /* next element in app order */ struct UT_hash_handle *hh_prev; /* previous hh in bucket order */ struct UT_hash_handle *hh_next; /* next hh in bucket order */ void *key; /* ptr to enclosing struct's key */ unsigned keylen; /* enclosing struct's key len */ unsigned hashv; /* result of hash-fcn(key) */ } UT_hash_handle; #endif /* UTHASH_H */ Pound-3.0.2/include/hpack.h0000664000175000017500000006111714150724211014510 0ustar rosegroseg/* $OpenBSD$ */ /* * Copyright (c) 2019 Reyk Floeter * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #ifndef HPACK_H #define HPACK_H struct hpack_table; enum hpack_header_index { HPACK_NO_INDEX = 0, HPACK_NEVER_INDEX, HPACK_INDEX, }; struct hpack_header { char *hdr_name; char *hdr_value; enum hpack_header_index hdr_index; TAILQ_ENTRY(hpack_header) hdr_entry; }; TAILQ_HEAD(hpack_headerblock, hpack_header); int hpack_init(void); struct hpack_table *hpack_table_new(size_t); void hpack_table_free(struct hpack_table *); size_t hpack_table_size(struct hpack_table *); struct hpack_headerblock *hpack_decode(unsigned char *, size_t, struct hpack_table *); unsigned char *hpack_encode(struct hpack_headerblock *, size_t *, struct hpack_table *); struct hpack_header *hpack_header_new(void); struct hpack_header *hpack_header_add(struct hpack_headerblock *, const char *, const char *, enum hpack_header_index); void hpack_header_free(struct hpack_header *); struct hpack_headerblock *hpack_headerblock_new(void); void hpack_headerblock_free(struct hpack_headerblock *); unsigned char *hpack_huffman_decode(unsigned char *, size_t, size_t *); char *hpack_huffman_decode_str(unsigned char *, size_t); unsigned char *hpack_huffman_encode(unsigned char *, size_t, size_t *); #ifdef HPACK_INTERNAL #ifndef DEBUG #define DPRINTF(x...) do{} while(0) #else #define DPRINTF(fmt...) logmsg(3, fmt) #endif /* from sys/param.h */ #define MAX(a,b) (((a)>(b))?(a):(b)) #define HPACK_HUFFMAN_BUFSZ 256 #define HPACK_MAX_TABLE_SIZE 4096 struct hpack_huffman_node { struct hpack_huffman_node *hpn_zero; struct hpack_huffman_node *hpn_one; int hpn_sym; }; struct hpack { struct hpack_huffman_node *hpack_huffman; }; struct hpack_table { struct hpack_headerblock *htb_dynamic; long htb_dynamic_size; long htb_dynamic_entries; long htb_table_size; long htb_max_table_size; struct hpack_headerblock *htb_headers; struct hpack_header *htb_next; }; /* Simple internal buffer API */ struct hbuf { unsigned char *data; /* data pointer */ size_t size; /* data size */ size_t rpos; /* read position */ size_t wpos; /* write position */ size_t wbsz; /* realloc buf size */ }; /* Masks, flags, and prefixes of the field types */ #define HPACK_M_INDEX 0x80 /* 7-bit prefix */ #define HPACK_F_INDEX 0x80 /* index flag */ #define HPACK_M_LITERAL_INDEX 0xc0 /* 6-bit prefix */ #define HPACK_F_LITERAL_INDEX 0x40 /* literal index flag */ #define HPACK_M_LITERAL_NO_INDEX 0xf0 /* 4-bit prefix */ #define HPACK_F_LITERAL_NO_INDEX 0x00 /* no index flag */ #define HPACK_M_LITERAL_NEVER_INDEX 0xf0 /* 4-bit prefix */ #define HPACK_F_LITERAL_NEVER_INDEX 0x10 /* never index flag */ #define HPACK_M_TABLE_SIZE_UPDATE 0xe0 /* 5-bit prefix */ #define HPACK_F_TABLE_SIZE_UPDATE 0x20 /* dynamic table size flag */ #define HPACK_M_LITERAL 0x80 /* 7-bit index */ #define HPACK_F_LITERAL 0x00 /* literal encoding */ #define HPACK_F_LITERAL_HUFFMAN 0x80 /* huffman encoding */ /* * Appendix A. Static Table Definition */ struct hpack_index { long hpi_id; /* Index */ const char *hpi_name; /* Header Name */ const char *hpi_value; /* Value */ }; #define HPACK_STATIC_SIZE (sizeof(static_table) / sizeof(static_table[0])) static struct hpack_index static_table[] = { { 1, ":authority", NULL }, \ { 2, ":method", "GET" }, \ { 3, ":method", "POST" }, \ { 4, ":path", "/" }, \ { 5, ":path", "/index.html"}, \ { 6, ":scheme", "http" }, \ { 7, ":scheme", "https" }, \ { 8, ":status", "200" }, \ { 9, ":status", "204" }, \ { 10, ":status", "206" }, \ { 11, ":status", "304" }, \ { 12, ":status", "400" }, \ { 13, ":status", "404" }, \ { 14, ":status", "500" }, \ { 15, "accept-charset", NULL }, \ { 16, "accept-encoding", "gzip, deflate" }, \ { 17, "accept-language", NULL }, \ { 18, "accept-ranges", NULL }, \ { 19, "accept", NULL }, \ { 20, "access-control-allow-origin", NULL }, \ { 21, "age", NULL }, \ { 22, "allow", NULL }, \ { 23, "authorization", NULL }, \ { 24, "cache-control", NULL }, \ { 25, "content-disposition", NULL }, \ { 26, "content-encoding", NULL }, \ { 27, "content-language", NULL }, \ { 28, "content-length", NULL }, \ { 29, "content-location", NULL }, \ { 30, "content-range", NULL }, \ { 31, "content-type", NULL }, \ { 32, "cookie", NULL }, \ { 33, "date", NULL }, \ { 34, "etag", NULL }, \ { 35, "expect", NULL }, \ { 36, "expires", NULL }, \ { 37, "from", NULL }, \ { 38, "host", NULL }, \ { 39, "if-match", NULL }, \ { 40, "if-modified-since", NULL }, \ { 41, "if-none-match", NULL }, \ { 42, "if-range", NULL }, \ { 43, "if-unmodified-since", NULL }, \ { 44, "last-modified", NULL }, \ { 45, "link", NULL }, \ { 46, "location", NULL }, \ { 47, "max-forwards", NULL }, \ { 48, "proxy-authenticate", NULL }, \ { 49, "proxy-authorization", NULL }, \ { 50, "range", NULL }, \ { 51, "referer", NULL }, \ { 52, "refresh", NULL }, \ { 53, "retry-after", NULL }, \ { 54, "server", NULL }, \ { 55, "set-cookie", NULL }, \ { 56, "strict-transport-security", NULL }, \ { 57, "transfer-encoding", NULL }, \ { 58, "user-agent", NULL }, \ { 59, "vary", NULL }, \ { 60, "via", NULL }, \ { 61, "www-authenticate", NULL }, \ }; /* * Appendix B. Huffman Code */ struct hpack_huffman { /* (comment) */ /* ASCII symbol */ /* (comment) */ /* code as bits aligned to MSB */ unsigned int hph_code; /* code as hex aligned to LSB */ unsigned int hph_length; /* len in bits */ }; #define HPACK_HUFFMAN_SIZE (sizeof(huffman_table) / sizeof(huffman_table[0])) static struct hpack_huffman huffman_table[] = { { /* ( 0) |11111111|11000 */ 0x1ff8, 13 }, { /* ( 1) |11111111|11111111|1011000 */ 0x7fffd8, 23 }, { /* ( 2) |11111111|11111111|11111110|0010 */ 0xfffffe2, 28 }, { /* ( 3) |11111111|11111111|11111110|0011 */ 0xfffffe3, 28 }, { /* ( 4) |11111111|11111111|11111110|0100 */ 0xfffffe4, 28 }, { /* ( 5) |11111111|11111111|11111110|0101 */ 0xfffffe5, 28 }, { /* ( 6) |11111111|11111111|11111110|0110 */ 0xfffffe6, 28 }, { /* ( 7) |11111111|11111111|11111110|0111 */ 0xfffffe7, 28 }, { /* ( 8) |11111111|11111111|11111110|1000 */ 0xfffffe8, 28 }, { /* ( 9) |11111111|11111111|11101010 */ 0xffffea, 24 }, { /* ( 10) |11111111|11111111|11111111|111100 */ 0x3ffffffc, 30 }, { /* ( 11) |11111111|11111111|11111110|1001 */ 0xfffffe9, 28 }, { /* ( 12) |11111111|11111111|11111110|1010 */ 0xfffffea, 28 }, { /* ( 13) |11111111|11111111|11111111|111101 */ 0x3ffffffd, 30 }, { /* ( 14) |11111111|11111111|11111110|1011 */ 0xfffffeb, 28 }, { /* ( 15) |11111111|11111111|11111110|1100 */ 0xfffffec, 28 }, { /* ( 16) |11111111|11111111|11111110|1101 */ 0xfffffed, 28 }, { /* ( 17) |11111111|11111111|11111110|1110 */ 0xfffffee, 28 }, { /* ( 18) |11111111|11111111|11111110|1111 */ 0xfffffef, 28 }, { /* ( 19) |11111111|11111111|11111111|0000 */ 0xffffff0, 28 }, { /* ( 20) |11111111|11111111|11111111|0001 */ 0xffffff1, 28 }, { /* ( 21) |11111111|11111111|11111111|0010 */ 0xffffff2, 28 }, { /* ( 22) |11111111|11111111|11111111|111110 */ 0x3ffffffe, 30 }, { /* ( 23) |11111111|11111111|11111111|0011 */ 0xffffff3, 28 }, { /* ( 24) |11111111|11111111|11111111|0100 */ 0xffffff4, 28 }, { /* ( 25) |11111111|11111111|11111111|0101 */ 0xffffff5, 28 }, { /* ( 26) |11111111|11111111|11111111|0110 */ 0xffffff6, 28 }, { /* ( 27) |11111111|11111111|11111111|0111 */ 0xffffff7, 28 }, { /* ( 28) |11111111|11111111|11111111|1000 */ 0xffffff8, 28 }, { /* ( 29) |11111111|11111111|11111111|1001 */ 0xffffff9, 28 }, { /* ( 30) |11111111|11111111|11111111|1010 */ 0xffffffa, 28 }, { /* ( 31) |11111111|11111111|11111111|1011 */ 0xffffffb, 28 }, { /* ' ' ( 32) |010100 */ 0x14, 6 }, { /* '!' ( 33) |11111110|00 */ 0x3f8, 10 }, { /* '"' ( 34) |11111110|01 */ 0x3f9, 10 }, { /* '#' ( 35) |11111111|1010 */ 0xffa, 12 }, { /* '$' ( 36) |11111111|11001 */ 0x1ff9, 13 }, { /* '%' ( 37) |010101 */ 0x15, 6 }, { /* '&' ( 38) |11111000 */ 0xf8, 8 }, { /* ''' ( 39) |11111111|010 */ 0x7fa, 11 }, { /* '(' ( 40) |11111110|10 */ 0x3fa, 10 }, { /* ')' ( 41) |11111110|11 */ 0x3fb, 10 }, { /* '*' ( 42) |11111001 */ 0xf9, 8 }, { /* '+' ( 43) |11111111|011 */ 0x7fb, 11 }, { /* ',' ( 44) |11111010 */ 0xfa, 8 }, { /* '-' ( 45) |010110 */ 0x16, 6 }, { /* '.' ( 46) |010111 */ 0x17, 6 }, { /* '/' ( 47) |011000 */ 0x18, 6 }, { /* '0' ( 48) |00000 */ 0x0, 5 }, { /* '1' ( 49) |00001 */ 0x1, 5 }, { /* '2' ( 50) |00010 */ 0x2, 5 }, { /* '3' ( 51) |011001 */ 0x19, 6 }, { /* '4' ( 52) |011010 */ 0x1a, 6 }, { /* '5' ( 53) |011011 */ 0x1b, 6 }, { /* '6' ( 54) |011100 */ 0x1c, 6 }, { /* '7' ( 55) |011101 */ 0x1d, 6 }, { /* '8' ( 56) |011110 */ 0x1e, 6 }, { /* '9' ( 57) |011111 */ 0x1f, 6 }, { /* ':' ( 58) |1011100 */ 0x5c, 7 }, { /* ';' ( 59) |11111011 */ 0xfb, 8 }, { /* '<' ( 60) |11111111|1111100 */ 0x7ffc, 15 }, { /* '=' ( 61) |100000 */ 0x20, 6 }, { /* '>' ( 62) |11111111|1011 */ 0xffb, 12 }, { /* '?' ( 63) |11111111|00 */ 0x3fc, 10 }, { /* '@' ( 64) |11111111|11010 */ 0x1ffa, 13 }, { /* 'A' ( 65) |100001 */ 0x21, 6 }, { /* 'B' ( 66) |1011101 */ 0x5d, 7 }, { /* 'C' ( 67) |1011110 */ 0x5e, 7 }, { /* 'D' ( 68) |1011111 */ 0x5f, 7 }, { /* 'E' ( 69) |1100000 */ 0x60, 7 }, { /* 'F' ( 70) |1100001 */ 0x61, 7 }, { /* 'G' ( 71) |1100010 */ 0x62, 7 }, { /* 'H' ( 72) |1100011 */ 0x63, 7 }, { /* 'I' ( 73) |1100100 */ 0x64, 7 }, { /* 'J' ( 74) |1100101 */ 0x65, 7 }, { /* 'K' ( 75) |1100110 */ 0x66, 7 }, { /* 'L' ( 76) |1100111 */ 0x67, 7 }, { /* 'M' ( 77) |1101000 */ 0x68, 7 }, { /* 'N' ( 78) |1101001 */ 0x69, 7 }, { /* 'O' ( 79) |1101010 */ 0x6a, 7 }, { /* 'P' ( 80) |1101011 */ 0x6b, 7 }, { /* 'Q' ( 81) |1101100 */ 0x6c, 7 }, { /* 'R' ( 82) |1101101 */ 0x6d, 7 }, { /* 'S' ( 83) |1101110 */ 0x6e, 7 }, { /* 'T' ( 84) |1101111 */ 0x6f, 7 }, { /* 'U' ( 85) |1110000 */ 0x70, 7 }, { /* 'V' ( 86) |1110001 */ 0x71, 7 }, { /* 'W' ( 87) |1110010 */ 0x72, 7 }, { /* 'X' ( 88) |11111100 */ 0xfc, 8 }, { /* 'Y' ( 89) |1110011 */ 0x73, 7 }, { /* 'Z' ( 90) |11111101 */ 0xfd, 8 }, { /* '[' ( 91) |11111111|11011 */ 0x1ffb, 13 }, { /* '\' ( 92) |11111111|11111110|000 */ 0x7fff0, 19 }, { /* '}' ( 93) |11111111|11100 */ 0x1ffc, 13 }, { /* '^' ( 94) |11111111|111100 */ 0x3ffc, 14 }, { /* '_' ( 95) |100010 */ 0x22, 6 }, { /* '`' ( 96) |11111111|1111101 */ 0x7ffd, 15 }, { /* 'a' ( 97) |00011 */ 0x3, 5 }, { /* 'b' ( 98) |100011 */ 0x23, 6 }, { /* 'c' ( 99) |00100 */ 0x4, 5 }, { /* 'd' (100) |100100 */ 0x24, 6 }, { /* 'e' (101) |00101 */ 0x5, 5 }, { /* 'f' (102) |100101 */ 0x25, 6 }, { /* 'g' (103) |100110 */ 0x26, 6 }, { /* 'h' (104) |100111 */ 0x27, 6 }, { /* 'i' (105) |00110 */ 0x6, 5 }, { /* 'j' (106) |1110100 */ 0x74, 7 }, { /* 'k' (107) |1110101 */ 0x75, 7 }, { /* 'l' (108) |101000 */ 0x28, 6 }, { /* 'm' (109) |101001 */ 0x29, 6 }, { /* 'n' (110) |101010 */ 0x2a, 6 }, { /* 'o' (111) |00111 */ 0x7, 5 }, { /* 'p' (112) |101011 */ 0x2b, 6 }, { /* 'q' (113) |1110110 */ 0x76, 7 }, { /* 'r' (114) |101100 */ 0x2c, 6 }, { /* 's' (115) |01000 */ 0x8, 5 }, { /* 't' (116) |01001 */ 0x9, 5 }, { /* 'u' (117) |101101 */ 0x2d, 6 }, { /* 'v' (118) |1110111 */ 0x77, 7 }, { /* 'w' (119) |1111000 */ 0x78, 7 }, { /* 'x' (120) |1111001 */ 0x79, 7 }, { /* 'y' (121) |1111010 */ 0x7a, 7 }, { /* 'z' (122) |1111011 */ 0x7b, 7 }, { /* '{' (123) |11111111|1111110 */ 0x7ffe, 15 }, { /* '|' (124) |11111111|100 */ 0x7fc, 11 }, { /* '}' (125) |11111111|111101 */ 0x3ffd, 14 }, { /* '~' (126) |11111111|11101 */ 0x1ffd, 13 }, { /* (127) |11111111|11111111|11111111|1100 */ 0xffffffc, 28 }, { /* (128) |11111111|11111110|0110 */ 0xfffe6, 20 }, { /* (129) |11111111|11111111|010010 */ 0x3fffd2, 22 }, { /* (130) |11111111|11111110|0111 */ 0xfffe7, 20 }, { /* (131) |11111111|11111110|1000 */ 0xfffe8, 20 }, { /* (132) |11111111|11111111|010011 */ 0x3fffd3, 22 }, { /* (133) |11111111|11111111|010100 */ 0x3fffd4, 22 }, { /* (134) |11111111|11111111|010101 */ 0x3fffd5, 22 }, { /* (135) |11111111|11111111|1011001 */ 0x7fffd9, 23 }, { /* (136) |11111111|11111111|010110 */ 0x3fffd6, 22 }, { /* (137) |11111111|11111111|1011010 */ 0x7fffda, 23 }, { /* (138) |11111111|11111111|1011011 */ 0x7fffdb, 23 }, { /* (139) |11111111|11111111|1011100 */ 0x7fffdc, 23 }, { /* (140) |11111111|11111111|1011101 */ 0x7fffdd, 23 }, { /* (141) |11111111|11111111|1011110 */ 0x7fffde, 23 }, { /* (142) |11111111|11111111|11101011 */ 0xffffeb, 24 }, { /* (143) |11111111|11111111|1011111 */ 0x7fffdf, 23 }, { /* (144) |11111111|11111111|11101100 */ 0xffffec, 24 }, { /* (145) |11111111|11111111|11101101 */ 0xffffed, 24 }, { /* (146) |11111111|11111111|010111 */ 0x3fffd7, 22 }, { /* (147) |11111111|11111111|1100000 */ 0x7fffe0, 23 }, { /* (148) |11111111|11111111|11101110 */ 0xffffee, 24 }, { /* (149) |11111111|11111111|1100001 */ 0x7fffe1, 23 }, { /* (150) |11111111|11111111|1100010 */ 0x7fffe2, 23 }, { /* (151) |11111111|11111111|1100011 */ 0x7fffe3, 23 }, { /* (152) |11111111|11111111|1100100 */ 0x7fffe4, 23 }, { /* (153) |11111111|11111110|11100 */ 0x1fffdc, 21 }, { /* (154) |11111111|11111111|011000 */ 0x3fffd8, 22 }, { /* (155) |11111111|11111111|1100101 */ 0x7fffe5, 23 }, { /* (156) |11111111|11111111|011001 */ 0x3fffd9, 22 }, { /* (157) |11111111|11111111|1100110 */ 0x7fffe6, 23 }, { /* (158) |11111111|11111111|1100111 */ 0x7fffe7, 23 }, { /* (159) |11111111|11111111|11101111 */ 0xffffef, 24 }, { /* (160) |11111111|11111111|011010 */ 0x3fffda, 22 }, { /* (161) |11111111|11111110|11101 */ 0x1fffdd, 21 }, { /* (162) |11111111|11111110|1001 */ 0xfffe9, 20 }, { /* (163) |11111111|11111111|011011 */ 0x3fffdb, 22 }, { /* (164) |11111111|11111111|011100 */ 0x3fffdc, 22 }, { /* (165) |11111111|11111111|1101000 */ 0x7fffe8, 23 }, { /* (166) |11111111|11111111|1101001 */ 0x7fffe9, 23 }, { /* (167) |11111111|11111110|11110 */ 0x1fffde, 21 }, { /* (168) |11111111|11111111|1101010 */ 0x7fffea, 23 }, { /* (169) |11111111|11111111|011101 */ 0x3fffdd, 22 }, { /* (170) |11111111|11111111|011110 */ 0x3fffde, 22 }, { /* (171) |11111111|11111111|11110000 */ 0xfffff0, 24 }, { /* (172) |11111111|11111110|11111 */ 0x1fffdf, 21 }, { /* (173) |11111111|11111111|011111 */ 0x3fffdf, 22 }, { /* (174) |11111111|11111111|1101011 */ 0x7fffeb, 23 }, { /* (175) |11111111|11111111|1101100 */ 0x7fffec, 23 }, { /* (176) |11111111|11111111|00000 */ 0x1fffe0, 21 }, { /* (177) |11111111|11111111|00001 */ 0x1fffe1, 21 }, { /* (178) |11111111|11111111|100000 */ 0x3fffe0, 22 }, { /* (179) |11111111|11111111|00010 */ 0x1fffe2, 21 }, { /* (180) |11111111|11111111|1101101 */ 0x7fffed, 23 }, { /* (181) |11111111|11111111|100001 */ 0x3fffe1, 22 }, { /* (182) |11111111|11111111|1101110 */ 0x7fffee, 23 }, { /* (183) |11111111|11111111|1101111 */ 0x7fffef, 23 }, { /* (184) |11111111|11111110|1010 */ 0xfffea, 20 }, { /* (185) |11111111|11111111|100010 */ 0x3fffe2, 22 }, { /* (186) |11111111|11111111|100011 */ 0x3fffe3, 22 }, { /* (187) |11111111|11111111|100100 */ 0x3fffe4, 22 }, { /* (188) |11111111|11111111|1110000 */ 0x7ffff0, 23 }, { /* (189) |11111111|11111111|100101 */ 0x3fffe5, 22 }, { /* (190) |11111111|11111111|100110 */ 0x3fffe6, 22 }, { /* (191) |11111111|11111111|1110001 */ 0x7ffff1, 23 }, { /* (192) |11111111|11111111|11111000|00 */ 0x3ffffe0, 26 }, { /* (193) |11111111|11111111|11111000|01 */ 0x3ffffe1, 26 }, { /* (194) |11111111|11111110|1011 */ 0xfffeb, 20 }, { /* (195) |11111111|11111110|001 */ 0x7fff1, 19 }, { /* (196) |11111111|11111111|100111 */ 0x3fffe7, 22 }, { /* (197) |11111111|11111111|1110010 */ 0x7ffff2, 23 }, { /* (198) |11111111|11111111|101000 */ 0x3fffe8, 22 }, { /* (199) |11111111|11111111|11110110|0 */ 0x1ffffec, 25 }, { /* (200) |11111111|11111111|11111000|10 */ 0x3ffffe2, 26 }, { /* (201) |11111111|11111111|11111000|11 */ 0x3ffffe3, 26 }, { /* (202) |11111111|11111111|11111001|00 */ 0x3ffffe4, 26 }, { /* (203) |11111111|11111111|11111011|110 */ 0x7ffffde, 27 }, { /* (204) |11111111|11111111|11111011|111 */ 0x7ffffdf, 27 }, { /* (205) |11111111|11111111|11111001|01 */ 0x3ffffe5, 26 }, { /* (206) |11111111|11111111|11110001 */ 0xfffff1, 24 }, { /* (207) |11111111|11111111|11110110|1 */ 0x1ffffed, 25 }, { /* (208) |11111111|11111110|010 */ 0x7fff2, 19 }, { /* (209) |11111111|11111111|00011 */ 0x1fffe3, 21 }, { /* (210) |11111111|11111111|11111001|10 */ 0x3ffffe6, 26 }, { /* (211) |11111111|11111111|11111100|000 */ 0x7ffffe0, 27 }, { /* (212) |11111111|11111111|11111100|001 */ 0x7ffffe1, 27 }, { /* (213) |11111111|11111111|11111001|11 */ 0x3ffffe7, 26 }, { /* (214) |11111111|11111111|11111100|010 */ 0x7ffffe2, 27 }, { /* (215) |11111111|11111111|11110010 */ 0xfffff2, 24 }, { /* (216) |11111111|11111111|00100 */ 0x1fffe4, 21 }, { /* (217) |11111111|11111111|00101 */ 0x1fffe5, 21 }, { /* (218) |11111111|11111111|11111010|00 */ 0x3ffffe8, 26 }, { /* (219) |11111111|11111111|11111010|01 */ 0x3ffffe9, 26 }, { /* (220) |11111111|11111111|11111111|1101 */ 0xffffffd, 28 }, { /* (221) |11111111|11111111|11111100|011 */ 0x7ffffe3, 27 }, { /* (222) |11111111|11111111|11111100|100 */ 0x7ffffe4, 27 }, { /* (223) |11111111|11111111|11111100|101 */ 0x7ffffe5, 27 }, { /* (224) |11111111|11111110|1100 */ 0xfffec, 20 }, { /* (225) |11111111|11111111|11110011 */ 0xfffff3, 24 }, { /* (226) |11111111|11111110|1101 */ 0xfffed, 20 }, { /* (227) |11111111|11111111|00110 */ 0x1fffe6, 21 }, { /* (228) |11111111|11111111|101001 */ 0x3fffe9, 22 }, { /* (229) |11111111|11111111|00111 */ 0x1fffe7, 21 }, { /* (230) |11111111|11111111|01000 */ 0x1fffe8, 21 }, { /* (231) |11111111|11111111|1110011 */ 0x7ffff3, 23 }, { /* (232) |11111111|11111111|101010 */ 0x3fffea, 22 }, { /* (233) |11111111|11111111|101011 */ 0x3fffeb, 22 }, { /* (234) |11111111|11111111|11110111|0 */ 0x1ffffee, 25 }, { /* (235) |11111111|11111111|11110111|1 */ 0x1ffffef, 25 }, { /* (236) |11111111|11111111|11110100 */ 0xfffff4, 24 }, { /* (237) |11111111|11111111|11110101 */ 0xfffff5, 24 }, { /* (238) |11111111|11111111|11111010|10 */ 0x3ffffea, 26 }, { /* (239) |11111111|11111111|1110100 */ 0x7ffff4, 23 }, { /* (240) |11111111|11111111|11111010|11 */ 0x3ffffeb, 26 }, { /* (241) |11111111|11111111|11111100|110 */ 0x7ffffe6, 27 }, { /* (242) |11111111|11111111|11111011|00 */ 0x3ffffec, 26 }, { /* (243) |11111111|11111111|11111011|01 */ 0x3ffffed, 26 }, { /* (244) |11111111|11111111|11111100|111 */ 0x7ffffe7, 27 }, { /* (245) |11111111|11111111|11111101|000 */ 0x7ffffe8, 27 }, { /* (246) |11111111|11111111|11111101|001 */ 0x7ffffe9, 27 }, { /* (247) |11111111|11111111|11111101|010 */ 0x7ffffea, 27 }, { /* (248) |11111111|11111111|11111101|011 */ 0x7ffffeb, 27 }, { /* (249) |11111111|11111111|11111111|1110 */ 0xffffffe, 28 }, { /* (250) |11111111|11111111|11111101|100 */ 0x7ffffec, 27 }, { /* (251) |11111111|11111111|11111101|101 */ 0x7ffffed, 27 }, { /* (252) |11111111|11111111|11111101|110 */ 0x7ffffee, 27 }, { /* (253) |11111111|11111111|11111101|111 */ 0x7ffffef, 27 }, { /* (254) |11111111|11111111|11111110|000 */ 0x7fffff0, 27 }, { /* (255) |11111111|11111111|11111011|10 */ 0x3ffffee, 26 }, { /* EOS (256) |11111111|11111111|11111111|111111 */ 0x3fffffff, 30 }, }; #endif /* HPACK_INTERNAL */ #endif /* HPACK_H */ Pound-3.0.2/README.md0000664000175000017500000004576714150724211013122 0ustar rosegroseg###POUND - REVERSE-PROXY AND LOAD-BALANCER### The Pound program is a reverse proxy, load balancer and HTTPS front-end for Web server(s). Pound was developed to enable distributing the load among several Web-servers and to allow for a convenient SSL wrapper for those Web servers that do not offer it natively. Pound is distributed under the GPL - no warranty, it's free to use, copy and give away. ###WHAT POUND IS:### 1. a reverse-proxy: it passes requests from client browsers to one or more back-end servers. 2. a load balancer: it will distribute the requests from the client browsers among several back-end servers, while keeping session information. 3. an SSL wrapper: Pound will decrypt HTTPS requests from client browsers and pass them as plain HTTP to the back-end servers. 4. an HTTP/HTTPS sanitizer: Pound will verify requests for correctness and accept only well-formed ones. 5. a fail over-server: should a back-end server fail, Pound will take note of the fact and stop passing requests to it until it recovers. Pound is a very small program, easily audited for security problems. It can run as setuid/setgid and/or in a chroot jail. Pound does not access the hard-disk at all (except for reading certificate file(s) on start, if required) and should thus pose no security threat to any machine. ###WHAT POUND IS NOT:### 1. Pound is not a Web server: by itself, Pound serves no content - it contacts the back-end server(s) for that purpose. 2. Pound is not a Web accelerator: no caching is done - every request is passed "as is" to a back-end server. ###STATUS### As of release 1.0 Pound is declared to be production-quality code. Quite a few people have reported using Pound successfully in production environments. The largest volume reported to date is a site with an average of about 30M requests per day, peaking at over 600 requests/sec. Pound was successfully used in production with a variety of Web servers, including Apache, IIS, Zope, WebLogic, Jakarta/Tomcat, iPlanet, etc. In general Pound passes requests and responses back and forth unchanged, so we have no reason to think that any web server would be incompatible. Client browsers that were tested: - IE 5.0 and up (Windows) HTTP/HTTPS - Netscape 4.7 (Windows/Linux) HTTP/HTTPS - Firefox (Windows/Linux) HTTP/HTTPS - Konqueror (Linux) HTTP/HTTPS - Galleon (Linux) HTTP/HTTPS - Opera (Linux/Windows) HTTP/HTTPS - Lynx (Linux) HTTP Given that Pound is in production and no problems were reported, we have no reason to believe that other browsers would present a problem. A few issues were observed with problematic SSL implementations, most notably with Opera 6, but these should be OK in the present version. ###INSTALLATION### Probably the easiest way to install Pound is to use a pre-compiled package if you can find one. While Apsis offers no such packages, they are available for quite a few systems (Suse, Debian and derivatives such as Ubuntu), as well as some private packages. Failing that you should install from sources. Required packages: - the [nanomsg](https://nanomsg.org/) library (available as a package on most distributions) - the [libyaml](https://pyyaml.org/wiki/LibYAML) library (available as a package on most distributions) - the [mbedSSL](https://tls.mbed.org/) library (the library previously known as PolarSSL, available as a package on most distributions) - a threads library (commonly libpthread) - the [uthash](http://troydhanson.github.io/uthash/) library. A copy is included in the Pound distribution, but you may want to download the most up-to-date version. The copyright notice is [here](pound/UTHASH.txt) - the [hpack](https://github.com/reyk/hpack) library. A copy is included in the Pound distribution, with adjustments for Linux compatability. The copyright is held by Reyk Floeter (BSD license). The uthash and hpack libraries are included as packages for them are usually not included in distributions. Optional, but recommended packages: - tcmalloc (previously part of the Google performance tools, available as a package on most distributions) - pcreposix (available as a package on most distributions) These packages will be automatically used by Pound if available. Download the latest version [Pound-3.0.2.tgz](https://www.apsis.ch/pound/Pound-3.0.2.tgz) file and unpack it. The archive is [signed](https://www.apsis.ch/pound/Pound-3.0.2.asc). My signature is available [here](https://www.apsis.ch/pound/roseg.asc). Alternately see below for stable versions. Pound uses CMake for its build, so just do the usual: cd build cmake .. make ###COPYRIGHT### Pound is copyrighted by Apsis GmbH and is distributed under the terms of the GNU Public License. Basically, this means that you can use it free of charge, copy it, distribute it (provided the copyright is maintained and the full package is distributed), modify it, or line a bird-cage with it. We would be happy to hear from you if you use it and suggestions and improvements are gladly accepted. ###CONTACT### [Robert Segall](mailto:roseg@apsis.ch) [Apsis GmbH](https://www.apsis.ch) P O Box CH-8707 Uetikon am See Switzerland +41-44-920 4904 ###MAILING LIST### Pound has its own mailing list now: go [here](https://admin.hostpoint.ch/mailman/listinfo/pound_apsis.ch) to subscribe. You will receive confirmation and instructions in the reply. All messages are available and indexed (searcheable) in the [archive](https://admin.hostpoint.ch/pipermail/pound_apsis.ch). The mailing list is the primary support forum for Pound - please post there any questions you may have. The developpers' address is given here for information purposes only. ###LOGO### We would be grateful if you would include the Pound logo ![Pound logo](pound/pound.png) with a link to [Apsis GmbH](https://www.apsis.ch) somewhere on your web page. ###SESSIONS### Pound has the ability to keep track of sessions between a client browser and a back-end server. Unfortunately, HTTP is defined as a stateless protocol, which complicates matters: many schemes have been invented to allow keeping track of sessions, none of which works perfectly. Even worse, sessions are critical in order to allow web-based applications to function correctly - it is vital that once a session is established all subsequent requests from the same browser be directed to the same back-end server. The way this is supported in Pound is by keeping track of the association between an originating IP address and a specific Backend. This is done at the Service level, and the directive Session defines for how long this association is kept. A note on *cookie injection*: some applications have no session-tracking mechanism at all but would still like to have the client always directed to the same back-end time after time. Some reverse proxies use a mechanism called "cookie injection" in order to achieve this: a cookie is added to the back-end responses and tracked by the reverse proxy. Pound was designed to be as transparent as possible, and this mechanism is not supported. ###THREADS AND LIMITS### A few people ran into problems when installing Pound because of the various threading models and how they interact with system-imposed limits. Please keep in mind the following requirements: - on most System V derived Unices (of which Linux up to 2.4 is one), a thread is a process. This means that when doing a 'ps' you will see as many processes with the name 'pound' as there are active threads. Each such process uses only two file descriptors, but the system needs to support the required number of processes, both in total and per user (possibly also per process group). In bash, this is 'ulimit -u', in csh this is 'limit maxproc'. - on BSD style systems all threads run in the same process space. Do a ps and you see a single 'pound' process. The process needs two file descriptors per active request (bash: 'ulimit -n', csh 'limit maxfiles'/'limit openfiles'). - on most systems the thread library comes with a built-in limit on the maximal number of concurrent threads allowed - on older systems it usually is 1024, on newer systems quite a bit higher. In very rare cases (very high load and long response times) you may run into this limitation - the symptom is log messages saying "can't create thread". Your only solution is to recompile the system threads library (and possibly the kernel itself) with a higher limit. Please note that your kernel needs to be configured to support the required resources - the above are just the shell commands. ###SIMILAR SYSTEMS### Quite a few people asked "What is wrong with Apache/Squid/ stunnel/your_favorite? Do we really need another proxy system?". The simple answer is that there is nothing wrong - they are all excellent systems that do their jobs very well. The reasoning behind Pound is however slightly different: - In my experience, a load-balancer may easily become a bottle-neck in itself. If you have a heavily loaded site, there are few things more depressing than seeing your "load-balancer" slow down the entire network. This means that the load-balancer should be kept as light-weight as possible. - Security: auditing a large system for security issues is a major undertaking for anybody (ask Bill Gates about it). This implies that in order to avoid introducing new vulnerabilities into a system (after all, your installation is only as secure as its weakest component) the proxy/load-balancer should be kept as small as possible. - Protection: I assume Pound will be the only component exposed to the Internet - your back-end servers will run in a protected network behind it. This means that Pound should filter requests and make sure only valid, correctly formed ones are passed to the back-end servers, thus protecting them from malicious clients. Taking these criteria into consideration, it is easy to see why the other systems mentioned above do not fit: - Apache (with mod_proxy and mod_backhand): great system, but very large. Imposes a significant load on the system, complex set-up procedure (and it is so easy to get it wrong: check how many Apache servers allow proxying from and to external hosts). While Apache has proven remarkably exploit free, I wouldn't wish to go into a security audit for the tens of thousands of lines of code involved, not to mention all the additional modules. - Squid: great caching proxy, but even should load-balancing features become available in the future, do you really need caching on the load-balancer? After all, Pound can easily run on a disk-less system, whereas with Squid you'd better prepare a high throughput RAID. Squid is still perfectly usable as a caching proxy between Pound and the actual Web server, should it lack its own cache (which Zope happily has). - stunnel: probably comes closest to my understanding of software design (does one job only and does it very well). However, it lacks the load balancing and HTTP filtering features that I considered necessary. Using stunnel in front of Pound (for HTTPS) would have made sense, except that integrating HTTPS into Pound proved to be so simple that it was not worth the trouble. - your favourite system: let me know how it looks in light of the above criteria - I am always interested in new ideas. ###OTHER ISSUES### The following problems were reported by various people who use pound: - Pound fails to start; HTTPS is enabled and the message "can't read private key from file xxx" appears in the log. Solution: make sure that the certificate file includes: - (optional) a chain of certificates from a known certificate authority to your server certificate - the server certificate - the private key; the key may NOT be password-protected The file should be in PEM format. The OpenSSL command to generate a self-signed certificate in the correct format would be something like:: openssl req -x509 -newkey rsa:1024 -keyout test.pem -out test.pem \ -days 365 -nodes Note the '-nodes' flag - it's important! - Pound fails to operate correctly with SSL when RootJail is specified. Solution: OpenSSL requires access to /dev/urandom, so make sure such a device is accessible from the root jail directory. Thus if your root jail is something like /var/pound: mkdir /var/pound/dev mknod /var/pound/dev/urandom c 1 9 or whatever major/minor number are appropriate for your system. - In chroot mode logging may stop functioning. Solution: make sure /dev and the root jail are on the same filesystem and create a hard link in the root jail to /dev/log: mkdir /chroot/jail/dev ln /dev/log /chroot/jail/dev/log Alternately you can have syslog (or syslog-ng) listen on another socket - see the man page for details. - In chroot mode name resolution (and especially redirects) may stop functioning. Solution: make sure your resolver works correctly in the jail. You probably need copies of /etc/resolv.conf and (at least part) of /etc/hosts. Depending on your system additional files may be required check your resolver man page for details. Should name resolution fail the translation of host names to IP addresses would fail, thereby defeating the mechanism Pound uses to identify when should a Redirect be rewritten. - Linux-specific: some people use various redundant Pound solutions for Linux which require Pound instances on separate machines to bind to the same address. The default configuration of Linux does not allow a program to bind() to non-local addresses, which may cause a problem. Solution: add echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind in your start-up script, or just set net.ipv4.ip_nonlocal_bind = 1 in /etc/sysctl.conf (if you have one). (Thanks to Rune Saetre for the suggestion). ###ACKNOWLEDGMENTS### - Albert (of Alacra) for investigating and writing the TCP_NODELAY code. - Luuk de Boer did some serious testing and debugging of the WebDAV code for Microsoft servers. - Alession Cervellin packages and makes available Solaris packages for various Pound versions. - David Couture found some nasty, lurking bugs, as well as contributing some serious testing on big hardware. - Frank Denis contributed a few excellent code patches and some good ideas. - Dmitriy Dvoinikov makes available a live-CD FreeBSD distribution that includes a Pound binary. - Abner G. Jacobsen did a lot of testing in a production environment and contributed some very nice ideas. - Akira Higuchi found a significant security issue in Pound and contributed the code to fix it. - Ken Lalonde contributed very useful remarks and suggestions, as well as correcting a few code errors. - Phil Lodwick contributed essential parts of the high-availability code and came up with some good ideas. In addition, did some serious testing under heavy loads. - Simon Matter packages and makes available RPMs for various Pound versions. - Jan-Piet Mens raised some interesting security points about the HTTPS implementation and brought the original idea for SSL header filtering. - Andreas Roedl for testing and some ideas about logging in root jails. - Gurkan Sengun tested Pound on Solaris, contributed the Solaris cc flags and makes a Solaris pre-compiled version available on his Web-site (www.linuks.mine.nu) - Shinji Tanaka contributed a patch for controlling logging to disk files. This is available at http://www.hatena-inc.co.jp/~stanaka/pound/ - Jim Washington contributed the code for WebDAV and tested it. - Maxime Yve discovered a nasty bug in the session tracking code and contributed the patch to fix it. All the others who tested Pound and told me about their results. ###STABLE VERSIONS### - [Pound-3.0.2.tgz](https:///www.apsis.ch/pound/Pound-3.0.2.tgz) ([signature](https:///www.apsis.ch/pound/Pound-3.0.2.asc)) - [Pound-3.0.1.tgz](https:///www.apsis.ch/pound/Pound-3.0.1.tgz) ([signature](https:///www.apsis.ch/pound/Pound-3.0.1.asc)) - [Pound-3.0.tgz](https:///www.apsis.ch/pound/Pound-3.0.tgz) ([signature](https:///www.apsis.ch/pound/Pound-3.0.asc)) - [Pound-2.8.tgz](https://www.apsis.ch/pound/Pound-2.8.tgz) ([signature](https://www.apsis.ch/pound/Pound-2.8.tgz)) - [Pound-2.7.tgz](https:///www.apsis.ch/pound/Pound-2.7.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.7.asc)) - [Pound-2.6.tgz](https:///www.apsis.ch/pound/Pound-2.6.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.6.asc)) - [Pound-2.5.tgz](https:///www.apsis.ch/pound/Pound-2.5.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.5.asc)) - [Pound-2.4.5.tgz](https:///www.apsis.ch/pound/Pound-2.4.5.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.4.5.asc)) - [Pound-2.4.4.tgz](https:///www.apsis.ch/pound/Pound-2.4.4.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.4.4.asc)) - [Pound-2.4.3.tgz](https:///www.apsis.ch/pound/Pound-2.4.3.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.4.3.asc)) - [Pound-2.4.2.tgz](https:///www.apsis.ch/pound/Pound-2.4.2.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.4.2.asc)) - [Pound-2.4.1.tgz](https:///www.apsis.ch/pound/Pound-2.4.1.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.4.1.asc)) - [Pound-2.4.tgz](https:///www.apsis.ch/pound/Pound-2.4.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.4.asc)) - [Pound-2.3.2.tgz](https:///www.apsis.ch/pound/Pound-2.3.2.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.3.2.asc)) - [Pound-2.3.1.tgz](https:///www.apsis.ch/pound/Pound-2.3.1.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.3.1.asc)) - [Pound-2.3.tgz](https:///www.apsis.ch/pound/Pound-2.3.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.3.asc)) - [Pound-2.2.tgz](https:///www.apsis.ch/pound/Pound-2.2.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.2.asc)) - [Pound-2.1.tgz](https:///www.apsis.ch/pound/Pound-2.1.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.1.asc)) - [Pound-2.0.tgz](https:///www.apsis.ch/pound/Pound-2.0.tgz) ([signature](https:///www.apsis.ch/pound/Pound-2.0.asc)) - [Pound-1.10.tgz](https:///www.apsis.ch/pound/Pound-1.10.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.10.asc)) - [Pound-1.9.tgz](https:///www.apsis.ch/pound/Pound-1.9.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.9.asc)) - [Pound-1.8.tgz](https:///www.apsis.ch/pound/Pound-1.8.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.8.asc)) - [Pound-1.7.tgz](https:///www.apsis.ch/pound/Pound-1.7.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.7.asc)) - [Pound-1.6.tgz](https:///www.apsis.ch/pound/Pound-1.6.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.6.asc)) - [Pound-1.5.tgz](https:///www.apsis.ch/pound/Pound-1.5.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.5.asc)) - [Pound-1.4.tgz](https:///www.apsis.ch/pound/Pound-1.4.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.4.asc)) - [Pound-1.3.tgz](https:///www.apsis.ch/pound/Pound-1.3.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.3.asc)) - [Pound-1.2.tgz](https:///www.apsis.ch/pound/Pound-1.2.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.2.asc)) - [Pound-1.1.tgz](https:///www.apsis.ch/pound/Pound-1.1.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.1.asc)) - [Pound-1.0.tgz](https:///www.apsis.ch/pound/Pound-1.0.tgz) ([signature](https:///www.apsis.ch/pound/Pound-1.0.asc)) ###EXPERIMENTAL VERSIONS### Pound-3.0.2/MKDIST.in0000755000175000017500000000115714150724211013150 0ustar rosegroseg#!/bin/sh # # Create archive # REL="Pound-@VERSION@" mkdir "$REL" mkdir "$REL"/build "$REL"/include "$REL"/man "$REL"/src "$REL"/pound cp CMakeLists.txt README.md MKDIST.in GPL.txt "$REL" cp build/.keep "$REL"/build cp include/pound.h.in include/utarray.h include/uthash.h include/hpack.h "$REL"/include cp man/pound.8 "$REL"/man cp src/backend.c src/config.c src/http.c src/http2.c src/pound.c src/util.c src/hpack.c "$REL"/src cp pound/pound.png pound/UTHASH.txt "$REL"/pound rm -f "$REL".tgz "$REL".asc tar -cvzf "$REL".tgz "$REL" rm -fr "$REL" # # Sign archive # gpg --output "$REL".asc --detach-sig --armor "$REL".tgz Pound-3.0.2/src/0000775000175000017500000000000014150724211012407 5ustar rosegrosegPound-3.0.2/src/hpack.c0000664000175000017500000006311414150724211013646 0ustar rosegroseg/* $OpenBSD$ */ /* * Copyright (c) 2019 Reyk Floeter * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #define HPACK_INTERNAL #include "pound.h" static const struct hpack_index * hpack_table_getbyid(long, struct hpack_index *, struct hpack_table *); static const struct hpack_index * hpack_table_getbyheader(struct hpack_header *, struct hpack_index *, struct hpack_table *); static int hpack_table_add(struct hpack_header *, struct hpack_table *); static int hpack_table_evict(long, long, struct hpack_table *); static int hpack_table_setsize(long, struct hpack_table *); static long hpack_decode_int(struct hbuf *, unsigned char); static char *hpack_decode_str(struct hbuf *, unsigned char); static int hpack_decode_buf(struct hbuf *, struct hpack_table *); static long hpack_decode_index(struct hbuf *, unsigned char, const struct hpack_index **, struct hpack_table *); static int hpack_decode_literal(struct hbuf *, unsigned char, struct hpack_table *); static int hpack_encode_int(struct hbuf *, long, unsigned char, unsigned char); static int hpack_encode_str(struct hbuf *, char *); static int hpack_huffman_init(void); static struct hpack_huffman_node * hpack_huffman_new(void); static void hpack_huffman_free(struct hpack_huffman_node *); static struct hbuf * hbuf_new(unsigned char *, size_t); static void hbuf_free(struct hbuf *); static int hbuf_writechar(struct hbuf *, unsigned char); static int hbuf_writebuf(struct hbuf *, unsigned char *, size_t); static unsigned char * hbuf_release(struct hbuf *, size_t *); static int hbuf_readchar(struct hbuf *, unsigned char *); static int hbuf_readbuf(struct hbuf *, unsigned char **, size_t); static int hbuf_advance(struct hbuf *, size_t); static size_t hbuf_left(struct hbuf *); static struct hpack hpack_global; #ifdef __GLIBC__ /* these functions are available on BSD, but not on Linux */ #include void * recallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size) { void *res; if(ptr == NULL) return calloc(nmemb, size); /* the code should really be like this, but the Linux reallocarray does not zero correctly the additional memory if((res = reallocarray(ptr, nmemb, size)) == NULL) return NULL; */ if((res = calloc(nmemb, size)) == NULL) return NULL; memcpy(res, ptr, size * (nmemb > oldnmemb? oldnmemb: nmemb)); return res; } void freezero(void *ptr, size_t size) { if(ptr == NULL) return; free(ptr); return; } #endif int hpack_init(void) { /* Initialize the huffman tree */ if (hpack_huffman_init() == -1) return (-1); return (0); } struct hpack_header * hpack_header_new(void) { return (calloc(1, sizeof(struct hpack_header))); } struct hpack_header * hpack_header_add(struct hpack_headerblock *hdrs, const char *name, const char *value, enum hpack_header_index index) { struct hpack_header *hdr; if ((hdr = hpack_header_new()) == NULL) return (NULL); hdr->hdr_name = strdup(name); hdr->hdr_value = strdup(value); hdr->hdr_index = index; if (hdr->hdr_name == NULL || hdr->hdr_value == NULL) { hpack_header_free(hdr); return (NULL); } TAILQ_INSERT_TAIL(hdrs, hdr, hdr_entry); return (hdr); } void hpack_header_free(struct hpack_header *hdr) { if (hdr == NULL) return; free(hdr->hdr_name); free(hdr->hdr_value); free(hdr); } struct hpack_headerblock * hpack_headerblock_new(void) { struct hpack_headerblock *hdrs; if ((hdrs = calloc(1, sizeof(*hdrs))) == NULL) return (NULL); TAILQ_INIT(hdrs); return (hdrs); } void hpack_headerblock_free(struct hpack_headerblock *hdrs) { struct hpack_header *hdr; if (hdrs == NULL) return; while ((hdr = TAILQ_FIRST(hdrs)) != NULL) { TAILQ_REMOVE(hdrs, hdr, hdr_entry); hpack_header_free(hdr); } } struct hpack_table * hpack_table_new(size_t max_table_size) { struct hpack_table *hpack; if ((hpack = calloc(1, sizeof(*hpack))) == NULL) return (NULL); if ((hpack->htb_dynamic = hpack_headerblock_new()) == NULL) { free(hpack); return (NULL); } hpack->htb_max_table_size = hpack->htb_table_size = max_table_size == 0 ? HPACK_MAX_TABLE_SIZE : max_table_size; return (hpack); } void hpack_table_free(struct hpack_table *hpack) { if (hpack == NULL) return; hpack_headerblock_free(hpack->htb_dynamic); free(hpack); } static const struct hpack_index * hpack_table_getbyid(long index, struct hpack_index *idbuf, struct hpack_table *hpack) { struct hpack_index *id = NULL; struct hpack_header *hdr; long dynidx = HPACK_STATIC_SIZE; if (index < 1 || index > dynidx + (long)hpack->htb_dynamic_size) return (NULL); if (index <= dynidx) { /* Static table */ id = &static_table[index - 1]; if (id->hpi_id != index) DPRINTF("corrupted HPACK static table %ld != %ld", id->hpi_id, index); } else { /* Dynamic table */ TAILQ_FOREACH_REVERSE(hdr, hpack->htb_dynamic, hpack_headerblock, hdr_entry) { dynidx++; if (dynidx == index) { idbuf->hpi_id = index; idbuf->hpi_name = hdr->hdr_name; idbuf->hpi_value = hdr->hdr_value; id = idbuf; break; } } } return (id); } static const struct hpack_index * hpack_table_getbyheader(struct hpack_header *key, struct hpack_index *idbuf, struct hpack_table *hpack) { struct hpack_index *id = NULL, *firstid = NULL; struct hpack_header *hdr; size_t i, dynidx = HPACK_STATIC_SIZE; if (key->hdr_name == NULL) return (NULL); /* * Search the static and dynamic tables for a perfect match * or the first match that only matches the name. */ /* Static table */ for (i = 0; i < dynidx; i++) { id = &static_table[i]; if (strcasecmp(id->hpi_name, key->hdr_name) != 0) continue; if (firstid == NULL) { memcpy(idbuf, id, sizeof(*id)); idbuf->hpi_value = NULL; firstid = idbuf; } if ((id->hpi_value != NULL && key->hdr_value != NULL) && strcasecmp(id->hpi_value, key->hdr_value) == 0) return (id); } /* Dynamic table */ TAILQ_FOREACH_REVERSE(hdr, hpack->htb_dynamic, hpack_headerblock, hdr_entry) { dynidx++; if (strcasecmp(hdr->hdr_name, key->hdr_name) != 0) continue; if (firstid == NULL) { idbuf->hpi_id = dynidx; idbuf->hpi_name = hdr->hdr_name; idbuf->hpi_value = NULL; firstid = idbuf; } if ((hdr->hdr_value != NULL && key->hdr_value != NULL) && strcasecmp(hdr->hdr_value, key->hdr_value) == 0) { idbuf->hpi_id = dynidx; idbuf->hpi_name = hdr->hdr_name; idbuf->hpi_value = hdr->hdr_value; id = idbuf; return (id); } } return (firstid); } static int hpack_table_add(struct hpack_header *hdr, struct hpack_table *hpack) { long newsize; if (hdr->hdr_index != HPACK_INDEX) return (0); /* * Following RFC 7451 section 4.1, * the additional 32 octets account for an estimated overhead * associated with an entry. */ newsize = strlen(hdr->hdr_name) + strlen(hdr->hdr_value) + 32; if (newsize > hpack->htb_table_size) { /* * An entry larger than the maximum size causes * the table to be emptied of all existing entries. */ hpack_table_evict(0, newsize, hpack); return (0); } else hpack_table_evict(hpack->htb_table_size, newsize, hpack); if (hpack_header_add(hpack->htb_dynamic, hdr->hdr_name, hdr->hdr_value, hdr->hdr_index) == NULL) return (-1); hpack->htb_dynamic_entries++; hpack->htb_dynamic_size += newsize; return (0); } static int hpack_table_evict(long size, long newsize, struct hpack_table *hpack) { struct hpack_header *hdr; while (size < (hpack->htb_dynamic_size + newsize) && (hdr = TAILQ_FIRST(hpack->htb_dynamic)) != NULL) { TAILQ_REMOVE(hpack->htb_dynamic, hdr, hdr_entry); hpack->htb_dynamic_entries--; hpack->htb_dynamic_size -= strlen(hdr->hdr_name) + strlen(hdr->hdr_value) + 32; hpack_header_free(hdr); } if (TAILQ_EMPTY(hpack->htb_dynamic) && hpack->htb_dynamic_entries != 0 && hpack->htb_dynamic_size != 0) DPRINTF("corrupted HPACK dynamic table"); return (0); } static int hpack_table_setsize(long size, struct hpack_table *hpack) { if (size > hpack->htb_max_table_size) return (-1); if (hpack_table_evict(size, 0, hpack) == -1) return (-1); hpack->htb_table_size = size; return (0); } size_t hpack_table_size(struct hpack_table *hpack) { return ((size_t)hpack->htb_dynamic_size); } struct hpack_headerblock * hpack_decode(unsigned char *data, size_t len, struct hpack_table *hpack) { struct hpack_headerblock *hdrs = NULL; struct hbuf *hbuf = NULL; struct hpack_table *ctx = NULL; int ret = -1; if (len == 0 || len > LONG_MAX) if (hpack == NULL && (hpack = ctx = hpack_table_new(0)) == NULL) goto fail; if ((hdrs = hpack_headerblock_new()) == NULL) goto fail; hpack->htb_headers = hdrs; hpack->htb_next = NULL; if ((hbuf = hbuf_new(data, len)) == NULL) goto fail; do { if (hpack_decode_buf(hbuf, hpack) == -1) goto fail; } while (hbuf_left(hbuf) > 0); ret = 0; fail: hbuf_free(hbuf); if (ret != 0) { hpack_headerblock_free(hdrs); hdrs = NULL; } else hdrs = hpack->htb_headers; hpack->htb_headers = NULL; hpack->htb_next = NULL; /* Free the local table (for single invocations) */ hpack_table_free(ctx); return (hdrs); } static long hpack_decode_int(struct hbuf *buf, unsigned char prefix) { unsigned long i = 0; unsigned char b = 0, m; if (hbuf_left(buf) == 0) return (-1); if (hbuf_readchar(buf, &b) == -1 || hbuf_advance(buf, 1) == -1) return (-1); /* Mask and remainder after the prefix of the first octet */ m = ~prefix; i = b & m; if (i >= m) { m = 0; /* Read varint bits while the 0x80 bit is set */ do { if (i > LONG_MAX) return (-1); if (hbuf_readchar(buf, &b) == -1 || hbuf_advance(buf, 1) == -1) return (-1); i += (b & ~0x80) << m; m += 7; } while (b & 0x80); } return ((long)i); } static long hpack_decode_index(struct hbuf *buf, unsigned char prefix, const struct hpack_index **idptr, struct hpack_table *hpack) { struct hpack_index idbuf; struct hpack_header *hdr = hpack->htb_next; const struct hpack_index *id; long i; int hasvalue; if (idptr != NULL) *idptr = NULL; if ((i = hpack_decode_int(buf, prefix)) == -1) return (-1); DPRINTF("%s: index %ld", __func__, i); if (i == 0) return (0); if ((id = hpack_table_getbyid(i, &idbuf, hpack)) == NULL) { DPRINTF("index not found: %ld\n", i); return (-1); } if (hdr == NULL || hdr->hdr_name != NULL || hdr->hdr_value != NULL) DPRINTF("invalid header"); if ((hdr->hdr_name = strdup(id->hpi_name)) == NULL) return (-1); hasvalue = id->hpi_value == NULL ? 0 : 1; if (hasvalue && (hdr->hdr_value = strdup(id->hpi_value)) == NULL) { free(hdr->hdr_name); hdr->hdr_name = NULL; return (-1); } DPRINTF("%s: index: %ld (%s%s%s)", __func__, i, id->hpi_name, hasvalue ? ": " : "", hasvalue ? id->hpi_value : ""); if (idptr != NULL) *idptr = id; return (i); } static char * hpack_decode_str(struct hbuf *buf, unsigned char prefix) { long i; unsigned char *ptr, c; char *str; if (hbuf_readchar(buf, &c) == -1) return (NULL); if ((i = hpack_decode_int(buf, prefix)) == -1) return (NULL); if (hbuf_readbuf(buf, &ptr, (size_t)i) == -1 || hbuf_advance(buf, (size_t)i) == -1) return (NULL); if ((c & HPACK_M_LITERAL) == HPACK_F_LITERAL_HUFFMAN) { DPRINTF("%s: decoding huffman code (size %ld)", __func__, i); if ((str = hpack_huffman_decode_str(ptr, (size_t)i)) == NULL) return (NULL); } else { if ((str = calloc(1, (size_t)i + 1)) == NULL) return (NULL); memcpy(str, ptr, (size_t)i); } return (str); } static int hpack_decode_literal(struct hbuf *buf, unsigned char prefix, struct hpack_table *hpack) { struct hpack_header *hdr = hpack->htb_next; const struct hpack_index *id; long i; char *str; if ((i = hpack_decode_index(buf, prefix, &id, hpack)) == -1) return (-1); if (i == 0) { if (hdr == NULL || hdr->hdr_name != NULL || hdr->hdr_value != NULL) DPRINTF("invalid header"); if ((str = hpack_decode_str(buf, HPACK_M_LITERAL)) == NULL) return (-1); DPRINTF("%s: name: %s", __func__, str); hdr->hdr_name = str; } /* The index might have set a default value */ if (hdr->hdr_value != NULL) { free(hdr->hdr_value); hdr->hdr_value = NULL; } if ((str = hpack_decode_str(buf, HPACK_M_LITERAL)) == NULL) return (-1); DPRINTF("%s: value: %s", __func__, str); hdr->hdr_value = str; return (0); } static int hpack_decode_buf(struct hbuf *buf, struct hpack_table *hpack) { struct hpack_header *hdr = NULL; unsigned char c; long i; if (hbuf_readchar(buf, &c) == -1) goto fail; if ((hdr = hpack_header_new()) == NULL) goto fail; hdr->hdr_index = HPACK_NO_INDEX; hpack->htb_next = hdr; /* 6.1 Indexed Header Field Representation */ if ((c & HPACK_M_INDEX) == HPACK_F_INDEX) { DPRINTF("%s: 0x%02x: 6.1 index", __func__, c); /* 7 bit index */ if ((i = hpack_decode_index(buf, HPACK_M_INDEX, NULL, hpack)) == -1) goto fail; /* No value means header with empty value */ if ((hdr->hdr_value == NULL) && (hdr->hdr_value = strdup("")) == NULL) goto fail; } /* 6.2.1. Literal Header Field with Incremental Indexing */ else if ((c & HPACK_M_LITERAL_INDEX) == HPACK_F_LITERAL_INDEX) { DPRINTF("%s: 0x%02x: 6.2.1 literal indexed", __func__, c); /* 6 bit index */ if (hpack_decode_literal(buf, HPACK_M_LITERAL_INDEX, hpack) == -1) goto fail; hdr->hdr_index = HPACK_INDEX; } /* 6.2.2. Literal Header Field without Indexing */ else if ((c & HPACK_M_LITERAL_NO_INDEX) == HPACK_F_LITERAL_NO_INDEX) { DPRINTF("%s: 0x%02x: 6.2.2 literal", __func__, c); /* 4 bit index */ if (hpack_decode_literal(buf, HPACK_M_LITERAL_NO_INDEX, hpack) == -1) goto fail; } /* 6.2.3. Literal Header Field Never Indexed */ else if ((c & HPACK_M_LITERAL_NO_INDEX) == HPACK_F_LITERAL_NO_INDEX) { DPRINTF("%s: 0x%02x: 6.2.3 literal never indexed", __func__, c); /* 4 bit index */ if (hpack_decode_literal(buf, HPACK_M_LITERAL_NO_INDEX, hpack) == -1) goto fail; hdr->hdr_index = HPACK_NEVER_INDEX; } /* 6.3. Dynamic Table Size Update */ else if ((c & HPACK_M_TABLE_SIZE_UPDATE) == HPACK_F_TABLE_SIZE_UPDATE) { DPRINTF("%s: 0x%02x: 6.3 dynamic table update", __func__, c); /* 5 bit index */ if ((i = hpack_decode_int(buf, HPACK_M_TABLE_SIZE_UPDATE)) == -1) goto fail; if (hpack_table_setsize(i, hpack) == -1) goto fail; return (0); } /* unknown index */ else { DPRINTF("%s: 0x%02x: unknown index", __func__, c); goto fail; } if (hdr->hdr_name == NULL || hdr->hdr_value == NULL) goto fail; /* Optionally add to index */ if (hpack_table_add(hdr, hpack) == -1) goto fail; /* Add header to the list */ TAILQ_INSERT_TAIL(hpack->htb_headers, hdr, hdr_entry); hpack->htb_next = NULL; return (0); fail: DPRINTF("%s: failed", __func__); hpack_header_free(hdr); hpack->htb_next = NULL; return (-1); } unsigned char * hpack_encode(struct hpack_headerblock *hdrs, size_t *encoded_len, struct hpack_table *hpack) { const struct hpack_index *id; struct hpack_index idbuf; struct hpack_table *ctx = NULL; struct hpack_header *hdr; struct hbuf *hbuf = NULL; unsigned char mask, flag; if (hpack == NULL && (hpack = ctx = hpack_table_new(0)) == NULL) goto fail; if ((hbuf = hbuf_new(NULL, BUFSIZ)) == NULL) goto fail; TAILQ_FOREACH(hdr, hdrs, hdr_entry) { DPRINTF("%s: header %s: %s (index %d)", __func__, hdr->hdr_name, hdr->hdr_value == NULL ? "(null)" : hdr->hdr_value, hdr->hdr_index); switch (hdr->hdr_index) { case HPACK_INDEX: mask = HPACK_M_LITERAL_INDEX; flag = HPACK_F_LITERAL_INDEX; break; case HPACK_NO_INDEX: mask = HPACK_M_LITERAL_NO_INDEX; flag = HPACK_F_LITERAL_NO_INDEX; break; case HPACK_NEVER_INDEX: mask = HPACK_M_LITERAL_NEVER_INDEX; flag = HPACK_F_LITERAL_NEVER_INDEX; break; } id = hpack_table_getbyheader(hdr, &idbuf, hpack); /* 6.1 Indexed Header Field Representation */ if (id != NULL && id->hpi_value != NULL) { DPRINTF("%s: index %zu (%s: %s)", __func__, id->hpi_id, id->hpi_name, id->hpi_value == NULL ? "(null)" : id->hpi_value); if (hpack_encode_int(hbuf, id->hpi_id, HPACK_M_INDEX, HPACK_F_INDEX) == -1) goto fail; continue; } /* 6.2 Literal Header Field Representation */ else if (id != NULL) { DPRINTF("%s: index+name %zu, %s", __func__, id->hpi_id, hdr->hdr_value); if (hpack_encode_int(hbuf, id->hpi_id, mask, flag) == -1) goto fail; } else { DPRINTF("%s: literal %s: %s", __func__, hdr->hdr_name, hdr->hdr_value); if (hpack_encode_int(hbuf, 0, mask, flag) == -1) goto fail; /* name */ if (hpack_encode_str(hbuf, hdr->hdr_name) == -1) goto fail; } /* value */ if (hpack_encode_str(hbuf, hdr->hdr_value) == -1) goto fail; /* Optionally add to index */ if (hpack_table_add(hdr, hpack) == -1) goto fail; } return (hbuf_release(hbuf, encoded_len)); fail: hpack_table_free(ctx); hbuf_free(hbuf); return (NULL); } static int hpack_encode_int(struct hbuf *buf, long i, unsigned char prefix, unsigned char type) { unsigned char b, m; if (i < 0) return (-1); /* The first octet encodes up to prefix length bits */ m = ~prefix; if (i <= m) b = (i & m) | type; else b = m | type; if (hbuf_writechar(buf, b) == -1) return (-1); i -= m; /* Encode the remainder as a varint */ for (m = 0x80; i >= m; i /= m) { b = i % m + m; /* Set the continuation bit if there are steps left */ if (i >= m) b |= m; if (hbuf_writechar(buf, b) == -1) return (-1); } if (i > 0 && hbuf_writechar(buf, (unsigned char)i) == -1) return (-1); return (0); } static int hpack_encode_str(struct hbuf *buf, char *str) { unsigned char *data = NULL; size_t len, slen; int ret = -1; /* * We have to decide if the string should be encoded with huffman * encoding or as literal string. There could be better heuristics * to do this... */ slen = strlen(str); if ((data = hpack_huffman_encode(str, slen, &len)) == NULL) goto done; if (len > 0 && len < slen) { DPRINTF("%s: encoded huffman code (size %ld, from %ld)", __func__, len, slen); if (hpack_encode_int(buf, len, HPACK_M_LITERAL, HPACK_F_LITERAL_HUFFMAN) == -1) goto done; if (hbuf_writebuf(buf, data, len) == -1) goto done; } else { if (hpack_encode_int(buf, slen, HPACK_M_LITERAL, HPACK_F_LITERAL) == -1) goto done; if (hbuf_writebuf(buf, str, slen) == -1) goto done; } ret = 0; done: free(data); return (ret); } static int hpack_huffman_init(void) { struct hpack_huffman *hph; struct hpack_huffman_node *root, *cur, *node; unsigned int i, j; /* Create new Huffman tree */ if ((root = hpack_huffman_new()) == NULL) return (-1); for (i = 0; i < HPACK_HUFFMAN_SIZE; i++) { hph = &huffman_table[i]; cur = root; /* Create branch for each symbol */ for (j = hph->hph_length; j > 0; j--) { if ((hph->hph_code >> (j - 1)) & 1) { if (cur->hpn_one == NULL) { if ((node = hpack_huffman_new()) == NULL) goto fail; cur->hpn_one = node; } cur = cur->hpn_one; } else { if (cur->hpn_zero == NULL) { if ((node = hpack_huffman_new()) == NULL) goto fail; cur->hpn_zero = node; } cur = cur->hpn_zero; } } /* The leaf node contains the (8-bit ASCII) symbol */ cur->hpn_sym = i; } hpack_global.hpack_huffman = root; return (0); fail: hpack_huffman_free(root); hpack_global.hpack_huffman = NULL; return (-1); } unsigned char * hpack_huffman_decode(unsigned char *buf, size_t len, size_t *decoded_len) { struct hpack_huffman_node *node, *root; unsigned int i, j, code; struct hbuf *hbuf = NULL; if ((root = node = hpack_global.hpack_huffman) == NULL) DPRINTF("hpack not initialized"); if ((hbuf = hbuf_new(NULL, len)) == NULL) return (NULL); for (i = 0; i < len; i++) { code = buf[i]; /* Walk the Huffman tree for each bit in the encoded input */ for (j = 8; j > 0; j--) { if ((code >> (j - 1)) & 1) node = node->hpn_one; else node = node->hpn_zero; if (node->hpn_sym == -1) continue; /* Leaf node of the next (8-bit ASCII) symbol */ if (hbuf_writechar(hbuf, (unsigned char)node->hpn_sym) == -1) { DPRINTF("%s: failed to add '%c'", __func__, node->hpn_sym); goto fail; } node = root; } } return (hbuf_release(hbuf, decoded_len)); fail: *decoded_len = 0; hbuf_free(hbuf); return (NULL); } char * hpack_huffman_decode_str(unsigned char *buf, size_t len) { unsigned char *data; char *str; size_t data_len; if ((data = hpack_huffman_decode(buf, len, &data_len)) == NULL) return (NULL); /* Allocate with an extra NUL character */ if ((str = recallocarray(data, data_len, data_len + 1, 1)) == NULL) { freezero(data, data_len); return (NULL); } /* Check if this is an actual string (no matter of the encoding) */ if (strlen(str) != data_len) { freezero(str, data_len + 1); str = NULL; } return (str); } unsigned char * hpack_huffman_encode(unsigned char *data, size_t len, size_t *encoded_len) { struct hbuf *hbuf; struct hpack_huffman *hph; unsigned int code, i, j; unsigned char o, obits; if ((hbuf = hbuf_new(NULL, len)) == NULL) return (NULL); for (i = 0, o = 0, obits = 8; i < len; i++) { /* Get Huffman code for each (8-bit ASCII) symbol */ hph = &huffman_table[data[i]]; for (code = hph->hph_code, j = hph->hph_length; j > 0;) { if (j > obits) { /* More bits to encode for this symbol */ j -= obits; o |= (code >> j) & 0xff; obits = 0; } else { /* * Remaining bits to encode for this input * symbol. The current output octet will * include bits from the next symbol or padding. */ obits -= j; o |= (code << obits) & 0xff; j = 0; } if (obits == 0) { if (hbuf_writechar(hbuf, o) == -1) { DPRINTF("%s: failed to add '%c'", __func__, o); goto fail; } o = 0; obits = 8; } } } if (len && obits > 0 && obits < 8) { /* Pad last octet with ones (EOS) */ o |= (1 << obits) - 1; if (hbuf_writechar(hbuf, o) == -1) { DPRINTF("%s: failed to add '%c'", __func__, o); goto fail; } } return (hbuf_release(hbuf, encoded_len)); fail: *encoded_len = 0; hbuf_free(hbuf); return (NULL); } static struct hpack_huffman_node * hpack_huffman_new(void) { struct hpack_huffman_node *node; if ((node = calloc(1, sizeof(*node))) == NULL) return (NULL); node->hpn_sym = -1; return (node); } static void hpack_huffman_free(struct hpack_huffman_node *root) { if (root == NULL) return; hpack_huffman_free(root->hpn_zero); hpack_huffman_free(root->hpn_one); free(root); } static struct hbuf * hbuf_new(unsigned char *data, size_t len) { struct hbuf *buf; size_t size = len; if ((buf = calloc(1, sizeof(*buf))) == NULL) return (NULL); size = MAX(HPACK_HUFFMAN_BUFSZ, len); if ((buf->data = calloc(1, size)) == NULL) { free(buf); return (NULL); } if (data != NULL) { memcpy(buf->data, data, len); buf->wpos = len; } buf->size = buf->wbsz = size; return (buf); } static void hbuf_free(struct hbuf *buf) { if (buf == NULL) return; freezero(buf->data, buf->size); free(buf); } static int hbuf_realloc(struct hbuf *buf, size_t len) { unsigned char *ptr; size_t newsize; /* Allocate a multiple of the initial write buffer size */ newsize = (buf->size + len + (buf->wbsz - 1)) & ~(buf->wbsz - 1); DPRINTF("%s: size %zu -> %zu", __func__, buf->size, newsize); if ((ptr = recallocarray(buf->data, buf->size, newsize, 1)) == NULL) return (-1); buf->data = ptr; buf->size = newsize; return (0); } static int hbuf_writechar(struct hbuf *buf, unsigned char c) { return (hbuf_writebuf(buf, &c, 1)); } static int hbuf_writebuf(struct hbuf *buf, unsigned char *data, size_t len) { if ((buf->wpos + len > buf->size) && hbuf_realloc(buf, len) == -1) return (-1); memcpy(buf->data + buf->wpos, data, len); buf->wpos += len; return (0); } static unsigned char * hbuf_release(struct hbuf *buf, size_t *len) { unsigned char *data; /* * Adjust (shrink) buffer to the used size. This allows to * safely call recallocarray() or freezero() later. */ if (buf->wpos != buf->size) { if ((data = recallocarray(buf->data, buf->size, buf->wpos, 1)) == NULL) { hbuf_free(buf); return (NULL); } buf->data = data; } *len = buf->wpos; data = buf->data; free(buf); return (data); } static int hbuf_readchar(struct hbuf *buf, unsigned char *c) { if (buf->rpos + 1 > buf->size) return (-1); *c = *(buf->data + buf->rpos); return (0); } static int hbuf_readbuf(struct hbuf *buf, unsigned char **ptr, size_t len) { if (buf->rpos + len > buf->size) return (-1); *ptr = buf->data + buf->rpos; return (0); } static int hbuf_advance(struct hbuf *buf, size_t len) { if (buf->rpos + len > buf->size) return (-1); buf->rpos += len; return (0); } static size_t hbuf_left(struct hbuf *buf) { if (buf->rpos >= buf->wpos) return (0); return (buf->wpos - buf->rpos); } Pound-3.0.2/src/http2.c0000664000175000017500000007152114150724211013622 0ustar rosegroseg/* * Pound - the reverse-proxy load-balancer * Copyright (C) 2002-2020 Apsis GmbH * * This file is part of Pound. * * Pound 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 3 of the License, or * (at your option) any later version. * * Pound 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Contact information: * Apsis GmbH * P.O.Box * 8707 Uetikon am See * Switzerland * EMail: roseg@apsis.ch */ #include "pound.h" /* frame types */ #define F_DATA 0x0 #define F_HEADERS 0x1 #define F_PRIORITY 0x2 #define F_RST 0x3 #define F_SETTINGS 0x4 #define F_PUSH 0x5 #define F_PING 0x6 #define F_GOAWAY 0x7 #define F_WINUPD 0x8 #define F_CONT 0x9 /* error flags */ #define E_NO_ERROR 0x0 #define E_PROTOCOL_ERROR 0x1 #define E_INTERNAL_ERROR 0x2 #define E_FLOW_CONTROL_ERROR 0x3 #define E_SETTINGS_TIMEOUT 0x4 #define E_STREAM_CLOSED 0x5 #define E_FRAME_SIZE_ERROR 0x6 #define E_REFUSED_STREAM 0x7 #define E_CANCEL 0x8 #define E_COMPRESSION_ERROR 0x9 #define E_CONNECT_ERROR 0xa #define E_ENHANCE_YOUR_CALM 0xb #define E_INADEQUATE_SECURITY 0xc #define E_HTTP_1_1_REQUIRED 0xd /* settings types */ #define S_TABSIZE 0x1 #define S_MAXSTREAMS 0x3 #define S_MAXFRAME 0x5 typedef struct { int length; unsigned char type; unsigned char flags; unsigned int stream_id; } FRAME; typedef struct { unsigned int id; unsigned int val; } SETTINGS; typedef struct { int stream_id; UT_hash_handle hh; } CLOSED_STREAM; typedef enum { HEADERS, DATA, TRAILERS, REPLY } STREAM_STATE; typedef struct { int stream_id; STREAM_STATE state; int at_eos; struct hpack_table *h_tab; struct hpack_headerblock *h_headers, *h_trailers; int s_be; UT_hash_handle hh; } ACTIVE_STREAM; static int read_int(FILE *f, int n, jmp_buf *jmp_err) { int cin, res; for(res = 0; n > 0; n--) { if((cin = getc(f)) == EOF) longjmp(*jmp_err, 1); res = (res << 8) | (cin & 0xFF); } return res; } static void write_int(FILE *f, int val, int n, jmp_buf *jmp_err) { int byte; while(n > 0) { byte = (val >> ((n - 1) * 8)) & 0xFF; if(putc(byte, f) == EOF) longjmp(*jmp_err, 1); n--; } return; } #define get_frame(FH, F, E) { memset((FH), '\0', sizeof(FRAME)); (FH)->length = read_int((F), 3, (E)); (FH)->type = read_int((F), 1, (E)); (FH)->flags = read_int((F), 1, (E)); (FH)->stream_id = read_int((F), 4, (E)); } #define put_frame(FH, F, E) { write_int((F), (FH)->length, 3, (E)); write_int((F), (FH)->type, 1, (E)); write_int((F), (FH)->flags, 1, (E)); write_int((F), (FH)->stream_id, 4, (E)); } #define get_settings(S, F, E) { memset((S), '\0', sizeof(SETTINGS)); (S)->id = read_int((F), 2, (E)); (S)->val = read_int((F), 4, (E)); } #define put_settings(S, F, E) { write_int((F), (S)->id, 2, (E)); write_int((F), (S)->val, 4, (E)); } #define get_rst(C, F, E) { *(C) = read_int((F), 4, (E)); } #define put_rst(C, F, E) { write_int((F), *(C), 4, (E)); } static void close_be(int s_be) { struct timespec t_wait; if(s_be < 0) return; nn_send(s_be, "", 0, 0); /* sleep to make sure all messages are through before closing the channel */ t_wait.tv_sec = 0; t_wait.tv_nsec = 1000000; nanosleep(&t_wait, NULL); nn_close(s_be); return; } static int put_msg(FILE *f_client, int stream_id, char *reason, char *body, int TABSIZE, int FRAMESIZE, jmp_buf *jmp_err) { FRAME rep; unsigned char *content; struct hpack_table *tab; struct hpack_headerblock *headers; int total; size_t length; memset(&rep, '\0', sizeof(rep)); rep.stream_id = stream_id; rep.type = F_HEADERS; rep.flags = 0x04; if(body == NULL) rep.flags |= 0x01; headers = hpack_headerblock_new(); if(hpack_header_add(headers, ":status", reason, HPACK_INDEX) == NULL) { hpack_headerblock_free(headers); return 0; } tab = hpack_table_new(TABSIZE); if((content = hpack_encode(headers, &length, tab)) == NULL) { hpack_table_free(tab); hpack_headerblock_free(headers); return 0; } rep.length = length; put_frame(&rep, f_client, jmp_err); fwrite(content, 1, rep.length, f_client); total = 9 + rep.length; hpack_table_free(tab); hpack_headerblock_free(headers); free(content); if(body != NULL) { memset(&rep, '\0', sizeof(rep)); rep.stream_id = stream_id + 1; rep.type = F_DATA; rep.flags = 0x01; rep.length = strlen(body); put_frame(&rep, f_client, jmp_err); fwrite(body, 1, rep.length, f_client); total += 9 + rep.length; } return total; } static void put_reject(FILE *f_client, int stream_id, int code, jmp_buf *jmp_err) { FRAME rep; rep.stream_id = stream_id; rep.length = 0; rep.flags = 0; rep.type = F_RST; put_frame(&rep, f_client, jmp_err); write_int(f_client, code, 4, jmp_err); return; } static ACTIVE_STREAM * add_active(ACTIVE_STREAM **as, int stream_id, int TABSIZE) { ACTIVE_STREAM *s; if((s = malloc(sizeof(ACTIVE_STREAM))) == NULL) return NULL; s->stream_id = stream_id; s->h_headers = s->h_trailers = NULL; s->h_tab = hpack_table_new(TABSIZE); s->s_be = -1; s->state = HEADERS; s->at_eos = 0; HASH_ADD_INT(*as, stream_id, s); logmsg(3, "%lX added %d to active %s:%d", pthread_self(), stream_id, __FILE__, __LINE__); return s; } static void add_closed(CLOSED_STREAM **cs, int stream_id) { CLOSED_STREAM *s; if((s = malloc(sizeof(CLOSED_STREAM))) == NULL) return; s->stream_id = stream_id; HASH_ADD_INT(*cs, stream_id, s); logmsg(3, "%lX added %d to closed %s:%d", pthread_self(), stream_id, __FILE__, __LINE__); return; } static void del_active(ACTIVE_STREAM **active_streams, CLOSED_STREAM **closed_streams, int stream_id) { ACTIVE_STREAM *s; HASH_FIND_INT(*active_streams, &stream_id, s); if(s != NULL) { if(s->h_headers != NULL) hpack_headerblock_free(s->h_headers); if(s->h_trailers != NULL) hpack_headerblock_free(s->h_trailers); hpack_table_free(s->h_tab); close_be(s->s_be); HASH_DEL(*active_streams, s); logmsg(3, "%lX removed %d from active %s:%d", pthread_self(), stream_id, __FILE__, __LINE__); free(s); } add_closed(closed_streams, stream_id); return; } #define cleanup() {\ if(active_streams != NULL) \ HASH_ITER(hh, active_streams, as, tas) { \ hpack_headerblock_free(as->h_headers); \ hpack_table_free(as->h_tab); \ close_be(as->s_be); \ HASH_DEL(active_streams, as); \ free(as); \ } \ if(closed_streams != NULL) \ HASH_ITER(hh, closed_streams, cs, tcs) { \ HASH_DEL(closed_streams, cs); \ free(cs); \ } \ if(content != NULL) \ free(content); \ } void do_http2(HTTP_LISTENER *http, FILE *f_client, char *peer_name, char *crt_buf, int upgrade_h2) { FRAME header; SETTINGS settings; unsigned char *content, *buf; char *msg; int n, err, last_stream, v1, v2, h_size; int TABSIZE, FRAMESIZE, MAXSTREAMS; jmp_buf err_jmp; ACTIVE_STREAM *active_streams, *as, *tas; CLOSED_STREAM *closed_streams, *cs, *tcs; struct hpack_headerblock *h_cont; struct timespec t_wait; TABSIZE = H2TABSIZE; FRAMESIZE = H2FRAMESIZE; MAXSTREAMS = 0; active_streams = NULL; closed_streams = NULL; content = NULL; logmsg(1, "%lX start do_http2 %s:%d", pthread_self(), __FILE__, __LINE__); if(setjmp(err_jmp)) { logmsg(0, "%lX Unexpected error %s:%d", pthread_self(), __FILE__, __LINE__); cleanup(); return; } get_frame(&header, f_client, &err_jmp); if(header.type == F_SETTINGS && !(header.flags & 0x01)) { for(n = header.length; n > 0; n -= 6) { get_settings(&settings, f_client, &err_jmp); if(settings.id == S_TABSIZE) TABSIZE = settings.val; else if(settings.id == S_MAXFRAME) FRAMESIZE = settings.val; else if(settings.id == S_MAXSTREAMS) MAXSTREAMS = settings.val; else logmsg(4, "%lX ignored settings %d/%d %s:%d", pthread_self(), settings.id, settings.val, __FILE__, __LINE__); } } logmsg(3, "%lX TABSIZE %d FRAMESIZE %d MAXSTREAMS %d %s:%d", pthread_self(), TABSIZE, FRAMESIZE, MAXSTREAMS, __FILE__, __LINE__); if(MAXSTREAMS <= 0) MAXSTREAMS = 4; if((content = malloc(FRAMESIZE)) == NULL) { logmsg(0, "%lX HTTP2 content: out of memory %s:%d", pthread_self(), __FILE__, __LINE__); return; } /* my settings - same as client */ header.length = 18; header.type = 0x04; header.flags = 0; header.stream_id = 0; put_frame(&header, f_client, &err_jmp); settings.id = S_TABSIZE; settings.val = TABSIZE; put_settings(&settings, f_client, &err_jmp); settings.id = S_MAXFRAME; settings.val = FRAMESIZE; put_settings(&settings, f_client, &err_jmp); settings.id = S_MAXSTREAMS; settings.val = MAXSTREAMS; put_settings(&settings, f_client, &err_jmp); logmsg(3, "%lX sent my SETTINGS %s:%d", pthread_self(), __FILE__, __LINE__); /* ACK */ header.length = 0; header.type = F_SETTINGS; header.flags = 0x01; header.stream_id = 0; put_frame(&header, f_client, &err_jmp); logmsg(3, "%lX ACK SETTINGS %s:%d", pthread_self(), __FILE__, __LINE__); /* close stream 1 if needed - only for plain upgraded connections */ if(upgrade_h2) { if((cs = malloc(sizeof(CLOSED_STREAM))) == NULL) { logmsg(2, "%lX http2: out of memory for closed streams %s:%d", pthread_self(), __FILE__, __LINE__); cleanup(); return; } cs->stream_id = 1; HASH_ADD_INT(closed_streams, stream_id, cs); put_reject(f_client, cs->stream_id, E_REFUSED_STREAM, &err_jmp); logmsg(3, "%lX REJECT stream 1 %s:%d", pthread_self(), __FILE__, __LINE__); } /* Frames we care about: HEADERS CONTINUATION DATA RST_STREAM SETTINGS ==> only changes in the table size PING GOAWAY Frames we must react to but otherwise ignore: PUSH_PROMISE ==> RST_STREAM, set as closed, ignore further frames Frames we can ignore: WINDOW_UPDATE PRIORITY Frames we send: HEADERS CONTINUATION DATA GOAWAY */ for(;;) { get_frame(&header, f_client, &err_jmp); switch(header.type) { case F_HEADERS: logmsg(4, "%lX received HEADER %d length %d flags %x %s:%d", pthread_self(), header.stream_id, header.length, header.flags, __FILE__, __LINE__); HASH_FIND_INT(closed_streams, &header.stream_id, cs); if(cs != NULL) { /* header for a closed stream -> RST/PRTOCOL_ERROR */ put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); logmsg(2, "%lX stream %d already closed %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); continue; } HASH_FIND_INT(active_streams, &header.stream_id, as); if(as == NULL) { if((as = add_active(&active_streams, header.stream_id, TABSIZE)) == NULL) { logmsg(2, "%lX stream %d failed to add to active %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); put_msg(f_client, header.stream_id + 1, "500", global.err500, TABSIZE, FRAMESIZE, &err_jmp); add_closed(&closed_streams, header.stream_id); continue; } } else { if(as->state == DATA) { as->state = TRAILERS; } else { /* second HEADER frame for an already open stream -> RST/PRTOCOL_ERROR */ logmsg(2, "%lX stream %d repeated HEADERS %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); del_active(&active_streams, &closed_streams, header.stream_id); put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); continue; } } if(header.flags & 0x08) { /* padding */ v1 = read_int(f_client, 1, &err_jmp); v2 = 1; } else v1 = v2 = 0; if(header.flags & 0x20) { /* stream dependency - ignored */ n = read_int(f_client, 4, &err_jmp); logmsg(4, "%lX %d depends on %d %s:%d", pthread_self(), header.stream_id, n, __FILE__, __LINE__); /* weight - ignored */ (void)read_int(f_client, 1, &err_jmp); v2 += 5; } logmsg(4, "%lX length %d v1 %d v2 %d %s:%d", pthread_self(), header.length, v1, v2, __FILE__, __LINE__); if((buf = malloc(header.length - v1 - v2)) == NULL) { logmsg(2, "%lX stream %d out of memory %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); del_active(&active_streams, &closed_streams, header.stream_id); put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); continue; } h_size = header.length - v1 - v2; if(fread(buf, 1, h_size, f_client) != h_size) { logmsg(2, "%lX stream %d failed to read content %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); free(buf); cleanup(); return; } if(v1 > 0) fread(content, 1, v1, f_client); as->at_eos = (header.flags & 0x01); if(as->state == TRAILERS && !as->at_eos) { logmsg(2, "%lX stream %d trailers but nor end-of-stream %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); put_reject(f_client, as->stream_id, E_PROTOCOL_ERROR, &err_jmp); del_active(&active_streams, &closed_streams, as->stream_id); free(buf); continue; } while(!(header.flags & 0x04)) { get_frame(&header, f_client, &err_jmp); if(header.type != F_CONT || header.stream_id != as->stream_id) { logmsg(2, "%lX stream %d HEADER not followed by CONTINUATION %s:%d", pthread_self(), as->stream_id, __FILE__, __LINE__); put_reject(f_client, as->stream_id, E_PROTOCOL_ERROR, &err_jmp); if(header.stream_id != as->stream_id) { put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); del_active(&active_streams, &closed_streams, header.stream_id); } del_active(&active_streams, &closed_streams, as->stream_id); free(buf); buf = NULL; break; } if((buf = realloc(buf, h_size + header.length)) == NULL) { logmsg(2, "%lX stream %d out of memory %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); put_reject(f_client, as->stream_id, E_PROTOCOL_ERROR, &err_jmp); del_active(&active_streams, &closed_streams, as->stream_id); break; } if(fread(buf + h_size, 1, header.length, f_client) != header.length) { logmsg(2, "%lX stream %d failed to read content %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); free(buf); cleanup(); return; } h_size += header.length; } if(as->state == HEADERS) { if((as->h_headers = hpack_decode(buf, h_size, as->h_tab)) == NULL) { logmsg(2, "%lX stream %d failed to decode %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); free(buf); cleanup(); return; } } else { if((as->h_trailers = hpack_decode(buf, h_size, as->h_tab)) == NULL) { logmsg(2, "%lX stream %d failed to decode %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); free(buf); cleanup(); return; } } free(buf); if(as->state == HEADERS) { if((as->s_be = get_be(http, peer_name, NULL, NULL, as->h_headers)) < 0) { logmsg(2, "%lX stream %d no back-end %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); put_msg(f_client, header.stream_id + 1, "500", global.err500, TABSIZE, FRAMESIZE, &err_jmp); del_active(&active_streams, &closed_streams, header.stream_id); continue; } if(crt_buf[0]) hpack_header_add(as->h_headers, "X-Pound-Cert", crt_buf, HPACK_INDEX); v1 = 2; nn_send(as->s_be, &v1, sizeof(int), 0); nn_send(as->s_be, peer_name, strlen(peer_name) + 1, 0); nn_send(as->s_be, &FRAMESIZE, sizeof(int), 0); nn_send(as->s_be, &TABSIZE, sizeof(int), 0); nn_send(as->s_be, &as->h_headers, sizeof(as->h_headers), 0); if(as->at_eos) { nn_send(as->s_be, "", 0, 0); /* no content */ nn_send(as->s_be, "", 0, 0); /* no trailers */ as->state = REPLY; } else as->state = DATA; } else { nn_send(as->s_be, &as->h_trailers, sizeof(as->h_trailers), 0); as->state = REPLY; } break; case F_CONT: logmsg(4, "%lX received CONT not following HEAD %d %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); del_active(&active_streams, &closed_streams, header.stream_id); break; case F_DATA: logmsg(4, "%lX received DATA %d %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); HASH_FIND_INT(active_streams, &header.stream_id, as); if(as == NULL || as->state != DATA) { /* DATA without previous HEADER/CONT frame without END -> RST/PRTOCOL_ERROR */ logmsg(0, "http2: CONT without previous HEADERS frame or after END"); del_active(&active_streams, &closed_streams, header.stream_id); put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); continue; } if(header.flags & 0x08) { /* padding */ v1 = read_int(f_client, 1, &err_jmp); v2 = 1; } else v1 = v2 = 0; if(fread(content, 1, header.length - v2, f_client) != header.length) { put_reject(f_client, header.stream_id, E_PROTOCOL_ERROR, &err_jmp); del_active(&active_streams, &closed_streams, header.stream_id); logmsg(0, "%lX http2: premature EOF %s:%d", pthread_self(), __FILE__, __LINE__); return; } nn_send(as->s_be, content, header.length - v2 - v1, 0); if(v1 > 0) fread(content, 1, v1, f_client); if(as->at_eos = (header.flags & 0x01)) { nn_send(as->s_be, "", 0, 0); /* no trailers */ as->state = REPLY; } break; case F_RST: logmsg(4, "%lX received RST %d %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); break; case F_SETTINGS: logmsg(4, "%lX received SETTINGS %d %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); if(header.flags & 0x1) { /* ACK for what we sent */ logmsg(4, "%lX received ACK %d type %d length %d %s:%d", pthread_self(), header.stream_id, header.type, header.flags, __FILE__, __LINE__); continue; } if(header.stream_id != 0) { logmsg(4, "%lX non-zero stream_id %d %d %s:%d", pthread_self(), __FILE__, __LINE__); last_stream = header.stream_id; header.stream_id = 0; header.type = F_GOAWAY; header.flags = 0; header.length = 8; put_frame(&header, f_client, &err_jmp); write_int(f_client, last_stream, 4, &err_jmp); err = 0x1; /*protocol error */ write_int(f_client, err, 4, &err_jmp); cleanup(); return; } for(n = header.length; n > 0; n -= 6) { get_settings(&settings, f_client, &err_jmp); if(settings.id == S_TABSIZE) TABSIZE = settings.val; else if(settings.id == S_MAXFRAME) { FRAMESIZE = settings.val; if((content = realloc(content, FRAMESIZE)) == NULL) { logmsg(0, "HTTP2: Out of memory"); cleanup(); return; } } else if(settings.id == S_MAXSTREAMS) MAXSTREAMS = settings.val; else logmsg(4, "%lX ignored settings %d/%d %s:%d", pthread_self(), settings.id, settings.val, __FILE__, __LINE__); } logmsg(3, "%lX TABSIZE %d FRAMESIZE %d MAXSTREAMS %d %s:%d", pthread_self(), TABSIZE, FRAMESIZE, MAXSTREAMS, __FILE__, __LINE__); /* ACK */ header.length = 0; header.type = 0x04; header.flags = 0x01; header.stream_id = 0; put_frame(&header, f_client, &err_jmp); logmsg(3, "%lX ACK SETTINGS %s:%d", pthread_self(), __FILE__, __LINE__); break; case F_PING: logmsg(4, "%lX received PING %d %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); if(header.stream_id != 0 || header.length != 8) { last_stream = header.stream_id; header.stream_id = 0; header.type = F_GOAWAY; header.flags = 0; header.length = 8; put_frame(&header, f_client, &err_jmp); write_int(f_client, last_stream, 4, &err_jmp); err = 0x1; /*protocol error */ write_int(f_client, err, 4, &err_jmp); cleanup(); return; } v1 = read_int(f_client, 4, &err_jmp); v2 = read_int(f_client, 4, &err_jmp); header.flags = 0x1; /* ACK */ put_frame(&header, f_client, &err_jmp); write_int(f_client, v1, 4, &err_jmp); write_int(f_client, v2, 4, &err_jmp); break; case F_GOAWAY: logmsg(4, "%lX received GOAWAY %d %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); /* should really send a GOAWAY back */ cleanup(); return; // break; /* rejected - no push promise */ case F_PUSH: logmsg(4, "%lX received PUSH %d %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); add_closed(&closed_streams, header.stream_id); put_reject(f_client, header.stream_id, E_REFUSED_STREAM, &err_jmp); break; /* acknowledged but otherwise ignored */ case F_PRIORITY: logmsg(4, "%lX received PRIORITY %d length %d %s:%d", pthread_self(), header.stream_id, header.length, __FILE__, __LINE__); if(header.length != 5 || header.stream_id == 0) { cleanup(); return; } v1 = read_int(f_client, 4, &err_jmp); v2 = read_int(f_client, 1, &err_jmp); logmsg(4, "%lX PRIORITY %d stream %d weight %d %s:%d", pthread_self(), header.stream_id, v1 & 0x7FFFFFFF, v2, __FILE__, __LINE__); if(header.stream_id == 0) put_reject(f_client, 0, E_PROTOCOL_ERROR, &err_jmp); break; case F_WINUPD: logmsg(4, "%lX received WINUPD %d %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); if(header.length != 4) { cleanup(); return; } v1 = read_int(f_client, 4, &err_jmp); break; default: logmsg(4, "%lX received UNKNOWN %d stream_id %d length %d flags %d %s:%d", pthread_self(), header.type, header.stream_id, header.length, header.flags, __FILE__, __LINE__); cleanup(); return; } HASH_ITER(hh, active_streams, as, tas) { if(as->state == REPLY) { for(v1 = 1; (header.length = nn_recv(as->s_be, &msg, NN_MSG, 0)) > 0; v1 = 0) { header.stream_id = as->stream_id; header.flags = 0; header.type = (v1? F_HEADERS: F_CONT); put_frame(&header, f_client, &err_jmp); fwrite(msg, 1, header.length, f_client); nn_freemsg(msg); logmsg(4, "%lX write %d:%s -> %d %s:%d", pthread_self(), header.stream_id, v1? "HEADER": "CONT", header.length, __FILE__, __LINE__); } header.stream_id = as->stream_id; header.length = 0; header.flags = 0x04; header.type = (v1? F_HEADERS: F_CONT); put_frame(&header, f_client, &err_jmp); logmsg(4, "%lX write %d -> end-headers %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); while((header.length = nn_recv(as->s_be, &msg, NN_MSG, 0)) > 0) { header.stream_id = as->stream_id; header.flags = 0; header.type = F_DATA; put_frame(&header, f_client, &err_jmp); fwrite(msg, 1, header.length, f_client); nn_freemsg(msg); logmsg(4, "%lX write %d:DATA -> %d %s:%d", pthread_self(), header.stream_id, header.length, __FILE__, __LINE__); } for(v1 = 1; (header.length = nn_recv(as->s_be, &msg, NN_MSG, 0)) > 0; v1 = 0) { header.stream_id = as->stream_id; header.flags = (v1? 0x01: 0); header.type = (v1? F_HEADERS: F_CONT); put_frame(&header, f_client, &err_jmp); fwrite(msg, 1, header.length, f_client); nn_freemsg(msg); logmsg(4, "%lX write trailer %d:%s -> %d %s:%d", pthread_self(), header.stream_id, v1? "HEADER": "CONT", header.length, __FILE__, __LINE__); } header.stream_id = as->stream_id; header.length = 0; header.flags = (v1? 0x01 | 0x04: 0x04); header.type = (v1? F_HEADERS: F_CONT); put_frame(&header, f_client, &err_jmp); logmsg(4, "%lX write trailer %d -> end-headers %s:%d", pthread_self(), header.stream_id, __FILE__, __LINE__); /* end of comms; has to be done from this side, as no LINGER available */ nn_send(as->s_be, "", 0, 0); /* sleep to make sure all messages are through before closing the channel */ t_wait.tv_sec = 0; t_wait.tv_nsec = 1000000; nanosleep(&t_wait, NULL); nn_close(as->s_be); del_active(&active_streams, &closed_streams, as->stream_id); } } } }Pound-3.0.2/src/http.c0000664000175000017500000006053614150724211013544 0ustar rosegroseg/* * Pound - the reverse-proxy load-balancer * Copyright (C) 2002-2020 Apsis GmbH * * This file is part of Pound. * * Pound 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 3 of the License, or * (at your option) any later version. * * Pound 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Contact information: * Apsis GmbH * P.O.Box * 8707 Uetikon am See * Switzerland * EMail: roseg@apsis.ch */ #include "pound.h" void * thr_service(void *arg) { SERVICE *svc; struct SESSION { char *addr; BACKEND *be; time_t last_access; UT_hash_handle hh; } *sessions, *cur, *tmp; time_t lastscan, now; struct nn_pollfd s_poll; char *msg, addr[NI_MAXHOST]; int n, i; logmsg(1, "%lX start service %s:%d", pthread_self(), __FILE__, __LINE__); svc = (SERVICE *)arg; sessions = NULL; lastscan = time(NULL); sem_post(&sem_start); for(;;) { if(svc->session == 0) { logmsg(4, "%lX Null session: %s:%d", pthread_self(), __FILE__, __LINE__); /* accept a listener request, return an appropriate backend */ if(nn_recv(svc->sock, &msg, NN_MSG, 0) <= 0) { logmsg(0, "Session: bad receive %s", nn_strerror(nn_errno())); continue; } nn_freemsg(msg); if(svc->backends_len > 1) { n = random(); for(i = 0; i < svc->backends_len; i++) { n = (n + i) % svc->backends_len; if(!svc->backends[n]->is_dead) break; } } else n = 0; if(!svc->backends[n]->is_dead) { nn_send(svc->sock, &svc->backends[n], sizeof(svc->backends[n]), 0); logmsg(4, "%lX Null session returns %d %s:%d", pthread_self(), n, __FILE__, __LINE__); } else { nn_send(svc->sock, "", 0, 0); logmsg(4, "%lX Null session no backend %s:%d", pthread_self(), __FILE__, __LINE__); } } now = time(NULL); if(now >= (lastscan + svc->session)) { /* prune expired sessions */ HASH_ITER(hh, sessions, cur, tmp) if((cur->last_access + svc->session) < now) { logmsg(4, "%lX prune %s %s:%d", pthread_self(), cur->addr, __FILE__, __LINE__); HASH_DEL(sessions, cur); free(cur->addr); free(cur); } lastscan = time(NULL); } s_poll.fd = svc->sock; s_poll.events = NN_POLLIN; if(nn_poll(&s_poll, 1, (svc->session - (now - lastscan)) * 1000) != 1) continue; /* accept a listener request, return an appropriate backend */ if(nn_recv(svc->sock, &msg, NN_MSG, 0) <= 0) { logmsg(0, "Session: bad receive %s", nn_strerror(nn_errno())); continue; } strcpy(addr, msg); nn_freemsg(msg); logmsg(4, "%lX find %s %s:%d", pthread_self(), addr, __FILE__, __LINE__); HASH_FIND_STR(sessions, msg, cur); if(cur && !cur->be->is_dead) { logmsg(4, "%lX found %s session %s:%d", pthread_self(), addr, __FILE__, __LINE__); nn_send(svc->sock, &cur->be, sizeof(cur->be), 0); cur->last_access = time(NULL); continue; } if(svc->backends_len > 1) { n = random(); for(i = 0; i < svc->backends_len; i++) { n = (n + i) % svc->backends_len; if(!svc->backends[n]->is_dead) break; } } else n = 0; if(!svc->backends[n]->is_dead) { if((cur = (struct SESSION *)malloc(sizeof(struct SESSION))) == NULL) logmsg(0, "Session: out of memory"); else { cur->addr = strdup(addr); cur->be = svc->backends[i]; cur->last_access = time(NULL); HASH_ADD_KEYPTR(hh, sessions, cur->addr, strlen(cur->addr), cur); logmsg(4, "%lX added %s %s:%d", pthread_self(), addr, __FILE__, __LINE__); } nn_send(svc->sock, &svc->backends[n], sizeof(svc->backends[n]), 0); } else { nn_send(svc->sock, "", 0, 0); logmsg(4, "%lX no backend %s:%d", pthread_self(), __FILE__, __LINE__); } } } int get_be(HTTP_LISTENER *http, char *peer_name, char *request, char *headers[], struct hpack_headerblock *h2) { int i, j, found, s_private; char *msg, private_name[NI_MAXHOST], buf[MAXBUF], *method, *path; BACKEND *be; struct nn_pollfd private_poll; struct hpack_header *header; logmsg(1, "%lX start get_be %s:%d", pthread_self(), __FILE__, __LINE__); if(request != NULL) { logmsg(2, "%lX get_be HTTP/1.1 %s:%d", pthread_self(), __FILE__, __LINE__); for(i = 0; i < http->services_len; i++) { if(http->services[i]->url != NULL && regexec(http->services[i]->url, request, 0, NULL, REG_ICASE)) continue; found = (http->services[i]->head_require == NULL); for(j = 0; j < MAXHEADERS; j++) { if(headers[j] == NULL) continue; if(http->services[i]->head_deny != NULL && !regexec(http->services[i]->head_deny, headers[j], 0, NULL, REG_ICASE)) { found = 0; break; } if(http->services[i]->head_require != NULL && !regexec(http->services[i]->head_require, headers[j], 0, NULL, REG_ICASE)) found = 1; } if(found) break; } } else { logmsg(2, "%lX get_be HTTP/2 %s:%d", pthread_self(), __FILE__, __LINE__); for(i = 0; i < http->services_len; i++) { found = (http->services[i]->head_require == NULL); method = path = NULL; TAILQ_FOREACH(header, h2, hdr_entry) { if(!strcasecmp(header->hdr_name, ":method")) method = header->hdr_value; else if(!strcasecmp(header->hdr_name, ":path")) path = header->hdr_value; else if(strcasecmp(header->hdr_name, ":scheme")) { /* we don't care about :scheme */ if(!strcasecmp(header->hdr_name, ":authority")) snprintf(buf, MAXBUF, "Host: %s\r\n", header->hdr_value); else snprintf(buf, MAXBUF, "%s: %s\r\n", header->hdr_name, header->hdr_value); logmsg(4, "%lX check %d header %s %s:%d", pthread_self(), i, buf, __FILE__, __LINE__); if(http->services[i]->head_require != NULL && !regexec(http->services[i]->head_require, buf, 0, NULL, REG_ICASE)) found = 1; if(http->services[i]->head_deny != NULL && !regexec(http->services[i]->head_deny, buf, 0, NULL, REG_ICASE)) { found = 0; break; } } } if(!found) continue; if(http->services[i]->url == NULL) break; snprintf(buf, MAXBUF, "%s %s HTTP/1.1\r\n", method, path); logmsg(4, "%lX check %d request %s %s:%d", pthread_self(), i, buf, __FILE__, __LINE__); if(!regexec(http->services[i]->url, buf, 0, NULL, REG_ICASE)) break; } } logmsg(2, "%lX %sfound %d %s:%d", pthread_self(), i >= http->services_len? "not ": "", i, __FILE__, __LINE__); if(i >= http->services_len) return -1; nn_send(http->services[i]->sock_in, peer_name, strlen(peer_name) + 1, 0); if(nn_recv(http->services[i]->sock_in, &msg, NN_MSG, 0) == sizeof(be)) memcpy(&be, msg, sizeof(be)); else be = NULL; nn_freemsg(msg); if(be == NULL || (s_private = nn_socket(AF_SP, NN_PAIR)) < 0) return -1; do { snprintf(private_name, NI_MAXHOST, "inproc://HTTP_%ld", random() % 10000); } while(nn_bind(s_private, private_name) < 0); if(nn_send(be->sock_in, private_name, strlen(private_name) + 1, 0) < 0) { nn_close(s_private); return -1; } private_poll.fd = s_private; private_poll.events = NN_POLLOUT; if(nn_poll(&private_poll, 1, http->client * 1000) != 1) { nn_close(s_private); return -1; } logmsg(2, "%lX done get_be %s:%d", pthread_self(), __FILE__, __LINE__); return s_private; } static int put_err(FILE *f_client, int code, char *reason, char *body) { static char *fmt_body = "HTTP/1.1 %d %s\r\nContent-length: %d\r\n\r\n"; static char *fmt_empty = "HTTP/1.1 %d %s\r\r\n"; if(body != NULL) { fprintf(f_client, fmt_body, code, reason, body); return strlen(fmt_body) + 1 + strlen(reason) + strlen(body); } else { fprintf(f_client, fmt_empty, code, reason); return strlen(fmt_empty) + 1 + strlen(reason); } } static void do_request(HTTP_LISTENER *http, FILE *f_client, char *peer_name, char *crt_buf) { BACKEND *be; char *msg, *headers[MAXHEADERS], private_name[NI_MAXHOST], request[MAXBUF], buf[MAXBUF]; int i, is_closed, close_at_end, upgrade_h2, is_chunked, is_expect, s_private, header_found; long content_length; struct timespec t_wait; regmatch_t match[2]; logmsg(1, "%lX start do_request %s:%d", pthread_self(), __FILE__, __LINE__); for(;;) { logmsg(2, "%lX start loop %s:%d", pthread_self(), __FILE__, __LINE__); is_closed = 0; close_at_end = 0; content_length = 0L; is_chunked = 0; is_expect = 0; upgrade_h2 = 0; for(i = 0; i < MAXHEADERS; i++) headers[i] = NULL; while(!(is_closed = (fgets(buf, MAXBUF - 1, f_client) == NULL))) { if(buf[0] != '\r' && buf[0] != '\n') break; } if(is_closed) { logmsg(2, "%lX client closed %s:%d", pthread_self(), __FILE__, __LINE__); return; } strcpy(request, buf); if(!strncasecmp(request, "CONNECT", strlen("CONNECT"))) { /* CONNECT is the only refused HTTP request type */ time_stamp(buf); logmsg(0, "%s - - [%s] \"%s\" 405 %d CONNECT not allowed", peer_name, buf, request, put_err(f_client, 405, "CONNECT not allowed", global.err405)); return; } logmsg(4, "%lX request %s %s:%d", pthread_self(), request, __FILE__, __LINE__); if(!strcasecmp(request, global.http2_preamble[0])) { /* if the request looks like PRI * HTTP/2.0\r\n followed by \r\n SM\r\n \r\n then this is a direct HTTP/2 connection or a HTTP/2 over TLS connection so call do_http2() and return */ for(i = 1; global.http2_preamble[i]; i++) { if(fgets(buf, MAXBUF - 1, f_client) == NULL) { time_stamp(buf); logmsg(0, "%s - - [%s] \"%s\" 405 %d HTTP/2 preamble premature EOF", peer_name, buf, request, put_err(f_client, 405, "HTTP/2 preamble premature EOF", global.err405)); return; } logmsg(4, "%lX preamble %d -> %s %s:%d", pthread_self(), i, buf, __FILE__, __LINE__); if(strcasecmp(buf, global.http2_preamble[i])) { fwrite(global.err405, 1, strlen(global.err405), f_client); time_stamp(buf); logmsg(0, "%s - - [%s] \"%s\" 405 %d HTTP/2 partial preamble", peer_name, buf, request, put_err(f_client, 405, "HTTP/2 partial preamble", global.err405)); return; } } do_http2(http, f_client, peer_name, crt_buf, upgrade_h2); return; } for(i = 0; i < MAXHEADERS && !(is_closed = ((fgets(buf, MAXBUF - 1, f_client) == NULL))); ) { logmsg(4, "%lX header %s %s:%d", pthread_self(), buf, __FILE__, __LINE__); if(buf[0] == '\r' || buf[0] == '\n') break; if(!strncasecmp(buf, "Upgrade:", strlen("Upgrade:"))) { /* Upgrade: h2c ==> next request will be HTTP2 */ upgrade_h2 = upgrade_h2 || !regexec(&rex_Upgrade_HTTP2, buf, 0, NULL, 0); continue; } else if(!strncasecmp(buf, "Connection:", strlen("Connection:"))) { /* Connection: Upgrade in conjunction with Upgrade: h2c ==> next request will be HTTP2 Response will be: Connection: Upgrade Upgrade: h2c so call do_http2() on receipt of the preamble do_http2() will issue a RST_STREAM/REFUSED_STREAM on stream 1 so the client will re-issue the request in HTTP/2 */ upgrade_h2 = upgrade_h2 || !regexec(&rex_Connection_HTTP2, buf, 0, NULL, 0); close_at_end = !regexec(&rex_Connection_Closed, buf, 0, NULL, 0); logmsg(3, "%lX upgrade_h2 %d close_at_end %d %s:%d", pthread_self(), upgrade_h2, close_at_end, __FILE__, __LINE__); continue; } else if(!regexec(&rex_ContentLength, buf, 2, match, REG_ICASE)) sscanf(buf + match[1].rm_so, "%ld", &content_length); else if(!regexec(&rex_Chunked, buf, 0, NULL, REG_ICASE)) is_chunked = 1; else is_expect = !regexec(&rex_Expect, buf, 0, NULL, REG_ICASE); if((headers[i++] = strdup(buf)) == NULL) { logmsg(0, "Out of memory"); time_stamp(buf); logmsg(0, "%s - - [%s] \"%s\" 500 %d Out of memory", peer_name, buf, request, put_err(f_client, 500, "Out of memory", global.err500)); is_closed = 1; break; } } logmsg(3, "%lX content_length %d is_chunked %d is_expect %d %s:%d", pthread_self(), content_length, is_chunked, is_expect, __FILE__, __LINE__); if(is_closed) { for(i = 0; i < MAXHEADERS; i++) if(headers[i]) free(headers[i]); return; } if(content_length > 0L && is_chunked) { time_stamp(buf); logmsg(0, "%s - - [%s] \"%s\" 405 %d Chunked and Content-length", peer_name, buf, request, put_err(f_client, 405, "Chunked and Content-length", global.err405)); for(i = 0; i < MAXHEADERS; i++) if(headers[i]) free(headers[i]); return; } if(is_expect) put_err(f_client, 100, "Continue", NULL); if(upgrade_h2) { logmsg(2, "%lX upgrade HTTP/2 consume request %s:%d", pthread_self(), __FILE__, __LINE__); if(content_length > 0L) while(content_length > 0L) { i = fread(buf, sizeof(char), content_length >= MAXBUF? MAXBUF - 1: content_length, f_client); if(i <= 0) content_length = -1L; } else if(is_chunked) while(fgets(buf, MAXBUF - 1, f_client) != NULL) { sscanf(buf, "%ld", &content_length); if(content_length > 0L) { while(content_length > 0L) { i = fread(buf, sizeof(char), content_length >= MAXBUF? MAXBUF - 1: content_length, f_client); if(i <= 0) content_length = -1L; } fgets(buf, MAXBUF, f_client); } else { while(fgets(buf, MAXBUF, f_client) != NULL) { if(buf[0] == '\r' || buf[0] == '\n') break; } } } put_err(f_client, 101, "Switching protocols\r\nConnection: Upgrade\r\nUpgrade: h2c", NULL); continue; } if((s_private = get_be(http, peer_name, request, headers, NULL)) < 0) { time_stamp(buf); logmsg(0, "%s - - [%s] \"%s\" 500 %d No backend", peer_name, buf, request, put_err(f_client, 500, "No backend", global.err500)); return; } logmsg(2, "%lX got backend %s:%d", pthread_self(), __FILE__, __LINE__); i = 1; nn_send(s_private, &i, sizeof(int), 0); nn_send(s_private, peer_name, strlen(peer_name) + 1, 0); nn_send(s_private, request, strlen(request) + 1, 0); if(crt_buf[0]) { logmsg(3, "%lX send cert %s:%d", pthread_self(), __FILE__, __LINE__); nn_send(s_private, "X-Pound-Cert: ", 15, 0); nn_send(s_private, crt_buf, strlen(crt_buf) + 1, 0); nn_send(s_private, "\r\n", 3, 0); } logmsg(3, "%lX send headers %s:%d", pthread_self(), __FILE__, __LINE__); for(i = 0; i < MAXHEADERS; i++) if(headers[i]) { nn_send(s_private, headers[i], strlen(headers[i]) + 1, 0); free(headers[i]); } nn_send(s_private, "\r\n", 3, 0); if(content_length > 0L) { logmsg(3, "%lX send content_length %s:%d", pthread_self(), __FILE__, __LINE__); while(content_length > 0L) { memset(buf, '\0', MAXBUF); i = fread(buf, sizeof(char), content_length >= MAXBUF? MAXBUF - 1: content_length, f_client); if(i > 0) { nn_send(s_private, buf, i, 0); content_length -= i; } else content_length = -1L; } } else if(is_chunked) { logmsg(3, "%lX send is_chunked %s:%d", pthread_self(), __FILE__, __LINE__); while(fgets(buf, MAXBUF - 1, f_client) != NULL) { sscanf(buf, "%ld", &content_length); if(content_length > 0L) { while(content_length > 0L) { memset(buf, '\0', MAXBUF); i = fread(buf, sizeof(char), content_length >= MAXBUF? MAXBUF - 1: content_length, f_client); if(i > 0) { nn_send(s_private, buf, i, 0); content_length -= i; } else content_length = -1L; } if(fgets(buf, MAXBUF, f_client) != NULL) nn_send(s_private, buf, strlen(buf) + 1, 0); } else { while(fgets(buf, MAXBUF, f_client) != NULL) { if(regexec(&rex_ContentLength, buf, 2, match, REG_ICASE)) nn_send(s_private, buf, strlen(buf) + 1, 0); if(buf[0] == '\r' || buf[0] == '\n') break; } } } } nn_send(s_private, "", 0, 0); logmsg(2, "%lX pass response %s:%d", pthread_self(), __FILE__, __LINE__); while((i = nn_recv(s_private, &msg, NN_MSG, 0)) > 0) { fwrite(msg, i, 1, f_client); nn_freemsg(msg); } /* end of comms; has to be done from this side, as no LINGER available */ nn_send(s_private, "", 0, 0); /* sleep to make sure all messages are through before closing the channel */ t_wait.tv_sec = 0; t_wait.tv_nsec = 1000000; nanosleep(&t_wait, NULL); nn_close(s_private); logmsg(2, "%lX loop done %s:%d", pthread_self(), __FILE__, __LINE__); if(close_at_end) return ; } } typedef struct cookie { mbedtls_ssl_context *fd; } COOKIE; static size_t c_read(void *cv, char *buf, size_t size) { COOKIE *c; int n; size_t n_read; c = (COOKIE *)cv; n_read = 0; while(n_read < size && (n = mbedtls_ssl_read(c->fd, buf + n_read, size - n_read)) > 0) n_read += n; return n_read; } static size_t c_write(void *cv, char *buf, size_t size) { COOKIE *c; c = (COOKIE *)cv; return mbedtls_ssl_write(c->fd, buf, size); } static int c_close(void *cv) { COOKIE *c; int res; mbedtls_net_context *ssl_fd; c = (COOKIE *)cv; res = mbedtls_ssl_close_notify(c->fd); ssl_fd = c->fd->p_bio; mbedtls_ssl_free(c->fd); mbedtls_net_free(ssl_fd); return res; } void * thr_http(void *arg) { HTTP_LISTENER *http; BACKEND *be; char *msg, peer_name[NI_MAXHOST], crt_buf[MAXBUF]; struct sockaddr peer_addr; int sock_client, n; FILE *f_client; struct linger s_linger; struct timeval s_time; mbedtls_ssl_context ssl; mbedtls_net_context ssl_client; COOKIE c; cookie_io_functions_t cio; logmsg(1, "%lX thr_http start %s:%d", pthread_self(), __FILE__, __LINE__); http = (HTTP_LISTENER *)arg; sem_post(&sem_start); for(;;) { logmsg(2, "%lX start loop %s:%d", pthread_self(), __FILE__, __LINE__); if(nn_recv(http->sock_fan, &msg, NN_MSG, 0) <= 0) { logmsg(0, "HTTP: bad receive %s", nn_strerror(nn_errno())); continue; } memcpy(&sock_client, msg, sizeof(sock_client)); nn_freemsg(msg); n = sizeof(peer_addr); getpeername(sock_client, &peer_addr, &n); getnameinfo(&peer_addr, sizeof(peer_addr), peer_name, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); logmsg(4, "%lX peer address %s %s:%d", pthread_self(), peer_name, __FILE__, __LINE__); if(http->conf == NULL) { s_linger.l_onoff = 1; s_linger.l_linger = 0; setsockopt(sock_client, SOL_SOCKET, SO_LINGER, &s_linger, sizeof(s_linger)); s_time.tv_sec = http->client; s_time.tv_usec = 0; setsockopt(sock_client, SOL_SOCKET, SO_RCVTIMEO, &s_time, sizeof(s_time)); setsockopt(sock_client, SOL_SOCKET, SO_SNDTIMEO, &s_time, sizeof(s_time)); f_client = fdopen(sock_client, "r+"); } else { mbedtls_net_init(&ssl_client); ssl_client.fd = sock_client; mbedtls_ssl_init(&ssl); mbedtls_ssl_setup(&ssl, http->conf); mbedtls_ssl_set_bio(&ssl, &sock_client, mbedtls_net_send, NULL, mbedtls_net_recv_timeout); if(n = mbedtls_ssl_handshake(&ssl)) { mbedtls_strerror(n, crt_buf, MAXBUF); logmsg(0, "Failed handshake from %s: %d - %s", peer_name, n, crt_buf); mbedtls_ssl_free(&ssl); close(sock_client); continue; } logmsg(2, "%lX handshake OK %s:%d", pthread_self(), __FILE__, __LINE__); if(mbedtls_ssl_get_peer_cert(&ssl) != NULL) { mbedtls_x509_crt_info(crt_buf, MAXBUF, "\t", mbedtls_ssl_get_peer_cert(&ssl)); logmsg(4, "%lX peer certificate %s %s:%d", pthread_self(), crt_buf, __FILE__, __LINE__); for(n = 0; n < MAXBUF && crt_buf[n]; n++) ; crt_buf[--n] = '\0'; } else crt_buf[0] = '\0'; /* for HTTP2: !strcmp(mbedtls_ssl_get_alpn_protocol(&ssl), "h2"), but we don't really need it */ c.fd = &ssl; cio.read = (cookie_read_function_t *)c_read; cio.write = (cookie_write_function_t *)c_write; cio.seek = NULL; cio.close = (cookie_close_function_t *)c_close; if((f_client = fopencookie(&c, "w+", cio)) == NULL) { logmsg(0, "fopencookie failed"); mbedtls_ssl_free(&ssl); mbedtls_net_free(&ssl_client); close(sock_client); continue; } setvbuf(f_client, NULL, _IONBF, 0); } do_request(http, f_client, peer_name, crt_buf); fclose(f_client); logmsg(2, "%lX done loop %s:%d", pthread_self(), __FILE__, __LINE__); } }Pound-3.0.2/src/config.c0000664000175000017500000010423414150724211014024 0ustar rosegroseg/* * Pound - the reverse-proxy load-balancer * Copyright (C) 2002-2020 Apsis GmbH * * This file is part of Pound. * * Pound 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 3 of the License, or * (at your option) any later version. * * Pound 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Contact information: * Apsis GmbH * P.O.Box * 8707 Uetikon am See * Switzerland * EMail: roseg@apsis.ch */ #include "pound.h" static UT_array *ut_backends; static UT_array *ut_http; void fatal(const char *fmt, ...) { char buf[MAXBUF + 1]; va_list ap; buf[MAXBUF] = '\0'; va_start(ap, fmt); vsnprintf(buf, MAXBUF, fmt, ap); va_end(ap); global.log_level = 1; logmsg(0, buf); exit(3); } static char * file2str(const char *fname) { char *res; struct stat st; FILE *fin; if(stat(fname, &st) || (fin = fopen(fname, "r")) == NULL || (res = malloc(st.st_size + 1)) == NULL || fread(res, 1, st.st_size, fin) != st.st_size) fatal("YAML can't access file %s", fname); res[st.st_size] = '\0'; fclose(fin); return res; } static void get_global(yaml_document_t *document, yaml_node_t *root) { yaml_node_pair_t *cur_pair; yaml_node_t *node; struct passwd *pwent; struct group *grent; logmsg(1, "start get_global %s:%d", __FILE__, __LINE__); for(cur_pair = root->data.mapping.pairs.start; cur_pair < root->data.mapping.pairs.top; cur_pair++) if(!strcasecmp("User", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { if((pwent = getpwnam(yaml_document_get_node(document, cur_pair->value)->data.scalar.value)) == NULL) fatal("User %s not found", yaml_document_get_node(document, cur_pair->value)->data.scalar.value); global.user = pwent->pw_uid; logmsg(4, "user %d %s:%d", global.user, __FILE__, __LINE__); } else if(!strcasecmp("Group", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { if((grent = getgrnam(yaml_document_get_node(document, cur_pair->value)->data.scalar.value)) == NULL) fatal("Group %s not found", yaml_document_get_node(document, cur_pair->value)->data.scalar.value); global.group = grent->gr_gid; logmsg(4, "group %d %s:%d", global.group, __FILE__, __LINE__); } else if(!strcasecmp("RootJail", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { if((global.root_jail = strdup(yaml_document_get_node(document, cur_pair->value)->data.scalar.value)) == NULL) fatal("Out of memory at %ld", yaml_document_get_node(document, cur_pair->value)->start_mark.line); logmsg(4, "RootJail %s %s:%d", global.root_jail, __FILE__, __LINE__); } else if(!strcasecmp("Err404", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { global.err404 = file2str(yaml_document_get_node(document, cur_pair->value)->data.scalar.value); logmsg(4, "err404 %s %s:%d", global.err404, __FILE__, __LINE__); } else if(!strcasecmp("Err405", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { global.err405 = file2str(yaml_document_get_node(document, cur_pair->value)->data.scalar.value); logmsg(4, "err405 %s %s:%d", global.err405, __FILE__, __LINE__); } else if(!strcasecmp("Err500", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { global.err500 = file2str(yaml_document_get_node(document, cur_pair->value)->data.scalar.value); logmsg(4, "err500 %s %s:%d", global.err500, __FILE__, __LINE__); } else fatal("Unknown directive %s at line %ld", yaml_document_get_node(document, cur_pair->key)->data.scalar.value, yaml_document_get_node(document, cur_pair->key)->start_mark.line); if(global.err404 == NULL) global.err404 = "Not Found

Page not found

\r\n"; if(global.err405 == NULL) global.err405 = "Not Allowed

Method not allowed

\r\n"; if(global.err500 == NULL) global.err500 = "Error

Internal error

\r\n"; return; } static void get_backends(yaml_document_t *document, yaml_node_t *root) { yaml_node_pair_t *cur_pair; yaml_node_t *vals; yaml_node_item_t *cur; BACKEND res; char addr[NI_MAXHOST], port[NI_MAXSERV], redirect[NI_MAXHOST]; struct addrinfo hints; logmsg(1, "start get_backends %s:%d", __FILE__, __LINE__); memset(&hints, '\0', sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; for(cur = root->data.sequence.items.start; cur < root->data.sequence.items.top; cur++) { memset(&res, '\0', sizeof(res)); res.id = *cur; vals = yaml_document_get_node(document, *cur); if(vals->type != YAML_MAPPING_NODE) fatal("Backend at %ld: not a mapping", vals->start_mark.line); addr[0] = '\0'; port[0] = '\0'; redirect[0] = '\0'; for(cur_pair = vals->data.mapping.pairs.start; cur_pair < vals->data.mapping.pairs.top; cur_pair++) if(!strcasecmp("Address", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { strncpy(addr, yaml_document_get_node(document, cur_pair->value)->data.scalar.value, NI_MAXHOST); logmsg(4, "addr %s %s:%d", addr, __FILE__, __LINE__); } else if(!strcasecmp("Port", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { strncpy(port, yaml_document_get_node(document, cur_pair->value)->data.scalar.value, NI_MAXSERV); logmsg(4, "port %s %s:%d", port, __FILE__, __LINE__); } else if(!strcasecmp("Timeout", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { res.timeout = atoi(yaml_document_get_node(document, cur_pair->value)->data.scalar.value); logmsg(4, "timeout %d %s:%d", res.timeout, __FILE__, __LINE__); } else if(!strcasecmp("Threads", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { res.threads = atoi(yaml_document_get_node(document, cur_pair->value)->data.scalar.value); logmsg(4, "threads %d %s:%d", res.threads, __FILE__, __LINE__); } else if(!strcasecmp("HeadAdd", yaml_document_get_node(document, cur_pair->key)->data.scalar.value)) { if((res.add_header = strdup(yaml_document_get_node(document, cur_pair->value)->data.scalar.value)) == NULL) fatal("HeadAdd out of memory at line %ld", yaml_document_get_node(document, cur_pair->key)->start_mark.line); if(strlen(res.add_header) == 0) fatal("HeadAdd at line %ld: header may not be empty", yaml_document_get_node(document, cur_pair->key)->start_mark.line); logmsg(4, "HeadAdd %s %s:%d", res.add_header, __FILE__, __LINE__); } else fatal("Unknown directive %s at line %ld", yaml_document_get_node(document, cur_pair->key)->data.scalar.value, yaml_document_get_node(document, cur_pair->key)->start_mark.line); if(addr[0]) { if(res.threads <= 0) res.threads = 8; if(res.timeout <= 0) res.timeout = 15; if(getaddrinfo(addr, port, &hints, &res.addr)) fatal("Backend at %ld: unknown server %s:%s", vals->start_mark.line, addr, port); } else fatal("Backend at %ld: missing data", vals->start_mark.line); logmsg(2, "push %s:%d", __FILE__, __LINE__); utarray_push_back(ut_backends, &res); } } static UT_array * get_svc_backends(yaml_document_t *document, yaml_node_t *root) { yaml_node_item_t *cur; UT_array *res; UT_icd backend_icd = {sizeof(BACKEND *), NULL, NULL, NULL }; int i; BACKEND *be; if(root->type != YAML_SEQUENCE_NODE) fatal("YAML Services not sequence (%ld)", root->start_mark.line); utarray_new(res, &backend_icd); for(cur = root->data.sequence.items.start; cur < root->data.sequence.items.top; cur++) { for(i = 0; i < backends_len; i++) if(backends[i].id == *cur) { be = &backends[i]; utarray_push_back(res, &be); break; } } return res; } static UT_array * get_services(yaml_document_t *document, yaml_node_t *root) { yaml_node_pair_t *map_pair; yaml_node_item_t *cur; yaml_node_t *vals; UT_array *res, *bes; BACKEND *be; UT_icd service_icd = {sizeof(SERVICE), NULL, NULL, NULL }; SERVICE *svc; int i; char pat[MAXBUF]; logmsg(1, "start get_services %s:%d", __FILE__, __LINE__); if(root->type != YAML_SEQUENCE_NODE) fatal("YAML Services not sequence (%ld)", root->start_mark.line); utarray_new(res, &service_icd); bes = NULL; for(cur = root->data.sequence.items.start; cur < root->data.sequence.items.top; cur++) { vals = yaml_document_get_node(document, *cur); if(vals->type != YAML_MAPPING_NODE) fatal("Service at %ld: not a mapping", vals->start_mark.line); if((svc = calloc(1, sizeof(SERVICE))) == NULL) fatal("Service at %ld: out of memory", vals->start_mark.line); for(map_pair = vals->data.mapping.pairs.start; map_pair < vals->data.mapping.pairs.top; map_pair++) { if(!strcasecmp("URL", yaml_document_get_node(document, map_pair->key)->data.scalar.value)) { if((svc->url = malloc(sizeof(regex_t))) == NULL) fatal("Service at %ld: out of memory", vals->start_mark.line); snprintf(pat, MAXBUF, "[^ \t]+[ \t]%s[ \t].*", yaml_document_get_node(document, map_pair->value)->data.scalar.value); if(regcomp(svc->url, pat, REG_ICASE)) fatal("Service at %ld: bad URL pattern %s", vals->start_mark.line, pat); logmsg(4, "URL %s %s:%d", yaml_document_get_node(document, map_pair->value)->data.scalar.value, __FILE__, __LINE__); } else if(!strcasecmp("Session", yaml_document_get_node(document, map_pair->key)->data.scalar.value)) { svc->session = atoi(yaml_document_get_node(document, map_pair->value)->data.scalar.value); logmsg(4, "session %d %s:%d", svc->session, __FILE__, __LINE__); } else if(!strcasecmp("HeadRequire", yaml_document_get_node(document, map_pair->key)->data.scalar.value)) { if((svc->head_require = malloc(sizeof(regex_t))) == NULL) fatal("Service at %ld: out of memory", vals->start_mark.line); if(regcomp(svc->head_require, yaml_document_get_node(document, map_pair->value)->data.scalar.value, REG_ICASE)) fatal("Service at %ld: bad HeadRequire pattern %s", vals->start_mark.line, yaml_document_get_node(document, map_pair->value)->data.scalar.value); logmsg(4, "HeadRequire %s %s:%d", yaml_document_get_node(document, map_pair->value)->data.scalar.value, __FILE__, __LINE__); } else if(!strcasecmp("HeadDeny", yaml_document_get_node(document, map_pair->key)->data.scalar.value)) { if((svc->head_deny = malloc(sizeof(regex_t))) == NULL) fatal("Service at %ld: out of memory", vals->start_mark.line); if(regcomp(svc->head_deny, yaml_document_get_node(document, map_pair->value)->data.scalar.value, REG_ICASE)) fatal("Service at %ld: bad HeadDeny pattern %s", vals->start_mark.line, yaml_document_get_node(document, map_pair->value)->data.scalar.value); logmsg(4, "HeadDeny %s %s:%d", yaml_document_get_node(document, map_pair->value)->data.scalar.value, __FILE__, __LINE__); } else if(!strcasecmp("Backends", yaml_document_get_node(document, map_pair->key)->data.scalar.value)) { bes = get_svc_backends(document, yaml_document_get_node(document, map_pair->value)); if(utarray_len(bes) <= 0) fatal("Service at %ld: no backends defined", vals->start_mark.line); svc->backends_len = utarray_len(bes); if((svc->backends = calloc(svc->backends_len, sizeof(BACKEND *))) == NULL) fatal("Service at %ld: out of memory", vals->start_mark.line); for(i = 0; i < svc->backends_len; i++) svc->backends[i] = *(BACKEND **)utarray_eltptr(bes, i); utarray_free(bes); } else fatal("Unknown directive %s at line %ld", yaml_document_get_node(document, map_pair->key)->data.scalar.value, yaml_document_get_node(document, map_pair->key)->start_mark.line); } logmsg(2, "push %s:%d", __FILE__, __LINE__); utarray_push_back(res, &svc); } return res; } static void get_http(yaml_document_t *document, yaml_node_t *root) { yaml_node_pair_t *map_pairs; yaml_node_item_t *cur; yaml_node_t *vals; HTTP_LISTENER res; char addr[NI_MAXHOST], port[NI_MAXSERV]; struct addrinfo hints; UT_array *services; int i; logmsg(1, "start get_http %s:%d", __FILE__, __LINE__); memset(&hints, '\0', sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; memset(&res, '\0', sizeof(res)); for(cur = root->data.sequence.items.start; cur < root->data.sequence.items.top; cur++) { vals = yaml_document_get_node(document, *cur); if(vals->type != YAML_MAPPING_NODE) fatal("HTTPListener at %ld: not a mapping", vals->start_mark.line); addr[0] ='\0'; port[0] = '\0'; for(map_pairs = vals->data.mapping.pairs.start; map_pairs < vals->data.mapping.pairs.top; map_pairs++) if(!strcasecmp("Address", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { strncpy(addr, yaml_document_get_node(document, map_pairs->value)->data.scalar.value, NI_MAXHOST); logmsg(4, "addr %s %s:%d", addr, __FILE__, __LINE__); } else if(!strcasecmp("Port", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { strncpy(port, yaml_document_get_node(document, map_pairs->value)->data.scalar.value, NI_MAXSERV); logmsg(4, "port %s %s:%d", port, __FILE__, __LINE__); } else if(!strcasecmp("Client", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { res.client = atoi(yaml_document_get_node(document, map_pairs->value)->data.scalar.value); logmsg(4, "client %d %s:%d", res.client, __FILE__, __LINE__); } else if(!strcasecmp("Threads", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { res.threads = atoi(yaml_document_get_node(document, map_pairs->value)->data.scalar.value); logmsg(4, "threads %d %s:%d", res.threads, __FILE__, __LINE__); } else if(!strcasecmp("Services", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { services = get_services(document, yaml_document_get_node(document, map_pairs->value)); } else fatal("Unknown directive %s at line %ld", yaml_document_get_node(document, map_pairs->key)->data.scalar.value, yaml_document_get_node(document, map_pairs->key)->start_mark.line); if(res.threads <= 0) res.threads = 8; if(res.client <= 0) res.client = 5; if(getaddrinfo(addr, port, &hints, &res.addr)) fatal("HTTPListener at %ld: unknown server %s:%s", vals->start_mark.line, addr, port); if(utarray_len(services) == 0) fatal("HTTPListener at %ld: no services defined", vals->start_mark.line); res.services_len = utarray_len(services); if((res.services = calloc(res.services_len, sizeof(SERVICE *))) == NULL) fatal("HTTPListener at %ld: out of memory", vals->start_mark.line); for(i = 0; i < res.services_len; i++) res.services[i] = *(SERVICE **)utarray_eltptr(services, i); utarray_free(services); logmsg(2, "push %s:%d", __FILE__, __LINE__); utarray_push_back(ut_http, &res); } return; } static int * get_ciphers(yaml_document_t *document, yaml_node_t *root) { yaml_node_item_t *cur; yaml_node_t *vals; UT_array *list; int *res, c; logmsg(1, "start get_ciphers %s:%d", __FILE__, __LINE__); utarray_new(list, &ut_int_icd); if(root->type == YAML_SCALAR_NODE) { logmsg(4, "cipher %s %s:%d", root->data.scalar.value, __FILE__, __LINE__); if((c = mbedtls_ssl_get_ciphersuite_id(root->data.scalar.value)) <= 0) fatal("Unknown cipher %s", root->data.scalar.value); utarray_push_back(list, &c); } else if(root->type == YAML_SEQUENCE_NODE) { for(cur = root->data.sequence.items.start; cur < root->data.sequence.items.top; cur++) { vals = yaml_document_get_node(document, *cur); if(vals->type != YAML_SCALAR_NODE) fatal("Ciphers at %ld: not a sequence", vals->start_mark.line); logmsg(4, "cipher %s %s:%d", vals->data.scalar.value, __FILE__, __LINE__); if((c = mbedtls_ssl_get_ciphersuite_id(vals->data.scalar.value)) <= 0) fatal("Unknown cipher %s", vals->data.scalar.value); utarray_push_back(list, &c); } } else fatal("Syntax error at line %ld", root->start_mark.line); if((res = calloc(utarray_len(list) + 1, sizeof(int))) == NULL) fatal("Ciphers at %ld: out of memory", root->start_mark.line); for(c = 0; c < utarray_len(list); c++) res[c] = *((int *)utarray_eltptr(list, c)); res[utarray_len(list)] = 0; utarray_free(list); return res; } static SNI * get_one(char *filename) { SNI *res; mbedtls_x509_crt *cur; mbedtls_x509_name *nd; mbedtls_asn1_buf data; mbedtls_x509_sequence *san; regex_t *one_host; UT_array *hosts; UT_icd regex_icd = {sizeof(regex_t), NULL, NULL, NULL}; char buf[NI_MAXHOST]; int i, j; logmsg(1, "start get_one(%s) %s:%d", filename, __FILE__, __LINE__); if((res = malloc(sizeof(SNI))) == NULL) fatal("SNI: out of memory"); mbedtls_x509_crt_init(&res->certificate); if(mbedtls_x509_crt_parse_file(&res->certificate, filename)) fatal("SNI: can't read certificate %s", filename); mbedtls_pk_init(&res->key); if(mbedtls_pk_parse_keyfile(&res->key, filename, NULL)) fatal("SNI: can't read key %s", filename); utarray_new(hosts, ®ex_icd); for(cur = &res->certificate; cur != NULL; cur = cur->next) { if(mbedtls_pk_check_pair(&cur->pk, &res->key)) continue; for(nd = &cur->subject; nd != NULL; nd = nd->next) if(MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &nd->oid) == 0) { data = nd->val; memset(buf, '\0', NI_MAXHOST); for(i = j = 0; i < data.len; i++) if(data.p[i] == '*') { buf[j++] = '['; buf[j++] = '^'; buf[j++] = '.'; buf[j++] = ']'; buf[j++] = '+'; } else if(data.p[i] == '.') { buf[j++] = '\\'; buf[j++] = '.'; } else buf[j++] = data.p[i]; buf[j] = '\0'; logmsg(4, "get_one add pattern %s %s:%d", buf, __FILE__, __LINE__); if((one_host = (regex_t *)malloc(sizeof(regex_t))) == NULL) fatal("Can't alloc regex for %s",buf); if(regcomp(one_host, buf, REG_ICASE | REG_EXTENDED)) fatal("Certificate in %s: bad host name pattern %s",filename, buf); utarray_push_back(hosts, one_host); } for(san = &cur->subject_alt_names; san != NULL; san = san->next) if(san->buf.tag == 130) { /* 130 seems to be the OID for DNS entries */ data = san->buf; memset(buf, '\0', NI_MAXHOST); for(i = j = 0; i < data.len; i++) if(data.p[i] == '*') { buf[j++] = '['; buf[j++] = '^'; buf[j++] = '.'; buf[j++] = ']'; buf[j++] = '+'; } else if(data.p[i] == '.') { buf[j++] = '\\'; buf[j++] = '.'; } else buf[j++] = data.p[i]; buf[j] = '\0'; logmsg(4, "get_one add pattern %s %s:%d", buf, __FILE__, __LINE__); if((one_host = (regex_t *)malloc(sizeof(regex_t))) == NULL) fatal("Can't alloc regex for %s",buf); if(regcomp(one_host, buf, REG_ICASE | REG_EXTENDED)) fatal("Certificate in %s: bad host name pattern %s",filename, buf); utarray_push_back(hosts, one_host); } if((res->host = calloc(utarray_len(hosts), sizeof(regex_t))) == NULL) fatal("SNI: out of memory"); res->host_len = utarray_len(hosts); for(i = 0; i < res->host_len; i++) res->host[i] = *((regex_t *)utarray_eltptr(hosts, i)); utarray_free(hosts); } logmsg(4, "get_one: added %d patterns %s:%d", res->host_len, __FILE__, __LINE__); return res; } static SNI ** get_certificates(yaml_document_t *document, yaml_node_t *root) { yaml_node_item_t *cur; yaml_node_t *vals; UT_array *list; UT_icd ut_sni_icd = {sizeof(SNI *), NULL, NULL, NULL}; SNI **res; int i; SNI *sni; logmsg(1, "start get_certificates %s:%d", __FILE__, __LINE__); utarray_new(list, &ut_sni_icd); if(root->type == YAML_SCALAR_NODE) { sni = get_one(root->data.scalar.value); utarray_push_back(list, &sni); } else if(root->type == YAML_SEQUENCE_NODE) { for(cur = root->data.sequence.items.start; cur < root->data.sequence.items.top; cur++) { vals = yaml_document_get_node(document, *cur); if(vals->type != YAML_SCALAR_NODE) fatal("Certificates at %ld: not a file name", vals->start_mark.line); sni = get_one(vals->data.scalar.value); utarray_push_back(list, &sni); } } else fatal("Syntax error at line %ld", root->start_mark.line); if((res = calloc(utarray_len(list) + 1, sizeof(SNI *))) == NULL) fatal("SNI at %ld: out of memory", root->start_mark.line); for(i = 0; i < utarray_len(list); i++) res[i] = *(SNI **)utarray_eltptr(list, i); res[utarray_len(list)] = NULL; utarray_free(list); return res; } const static char *alpn_protocols[] = { "h2", "http/1.1", NULL}; static void get_https(yaml_document_t *document, yaml_node_t *root) { yaml_node_pair_t *map_pairs; yaml_node_item_t *cur; yaml_node_t *vals; HTTP_LISTENER res; char addr[NI_MAXHOST], port[NI_MAXSERV]; struct addrinfo hints; UT_array *services; int i, *ciphers; logmsg(1, "start get_https %s:%d", __FILE__, __LINE__); memset(&hints, '\0', sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; memset(&res, '\0', sizeof(res)); if((res.conf = malloc(sizeof(mbedtls_ssl_config))) == NULL) fatal("HTTPSListener: out of memory"); mbedtls_ssl_config_init(res.conf); mbedtls_ssl_conf_rng(res.conf, mbedtls_ctr_drbg_random, &tls_ctr_drbg); mbedtls_ssl_config_defaults(res.conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT); mbedtls_ssl_conf_alpn_protocols(res.conf, alpn_protocols); mbedtls_ssl_conf_authmode(res.conf, MBEDTLS_SSL_VERIFY_NONE); for(cur = root->data.sequence.items.start; cur < root->data.sequence.items.top; cur++) { vals = yaml_document_get_node(document, *cur); if(vals->type != YAML_MAPPING_NODE) fatal("HTTPSListener at %ld: not a mapping", vals->start_mark.line); addr[0] = '\0'; port[0] = '\0'; for(map_pairs = vals->data.mapping.pairs.start; map_pairs < vals->data.mapping.pairs.top; map_pairs++) if(!strcasecmp("Address", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { strncpy(addr, yaml_document_get_node(document, map_pairs->value)->data.scalar.value, NI_MAXHOST); logmsg(4, "address %s %s:%d", addr, __FILE__, __LINE__); } else if(!strcasecmp("Port", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { strncpy(port, yaml_document_get_node(document, map_pairs->value)->data.scalar.value, NI_MAXSERV); logmsg(4, "port %s %s:%d", port, __FILE__, __LINE__); } else if(!strcasecmp("Client", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { res.client = atoi(yaml_document_get_node(document, map_pairs->value)->data.scalar.value); logmsg(4, "client %d %s:%d", res.client, __FILE__, __LINE__); } else if(!strcasecmp("Threads", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { res.threads = atoi(yaml_document_get_node(document, map_pairs->value)->data.scalar.value); logmsg(4, "threads %d %s:%d", res.threads, __FILE__, __LINE__); } else if(!strcasecmp("Services", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { services = get_services(document, yaml_document_get_node(document, map_pairs->value)); } else if(!strcasecmp("Certificates", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { res.sni = get_certificates(document, yaml_document_get_node(document, map_pairs->value)); } else if(!strcasecmp("Ciphers", yaml_document_get_node(document, map_pairs->key)->data.scalar.value)) { ciphers = get_ciphers(document, yaml_document_get_node(document, map_pairs->value)); } else fatal("Unknown directive %s at line %ld", yaml_document_get_node(document, map_pairs->key)->data.scalar.value, yaml_document_get_node(document, map_pairs->key)->start_mark.line); if(res.threads <= 0) res.threads = 8; if(res.client <= 0) res.client = 5; if(getaddrinfo(addr, port, &hints, &res.addr)) fatal("HTTPSListener at %ld: unknown server %s:%s", vals->start_mark.line, addr, port); if(utarray_len(services) == 0) fatal("HTTPSListener at %ld: no services defined", vals->start_mark.line); res.services_len = utarray_len(services); if((res.services = calloc(res.services_len, sizeof(SERVICE *))) == NULL) fatal("HTTPSListener at %ld: out of memory", vals->start_mark.line); for(i = 0; i < res.services_len; i++) res.services[i] = *(SERVICE **)utarray_eltptr(services, i); utarray_free(services); if(ciphers != NULL) mbedtls_ssl_conf_ciphersuites(res.conf, ciphers); if(res.sni == NULL) fatal("HTTPSListener at %ld: no certificates defined", vals->start_mark.line); mbedtls_ssl_conf_sni(res.conf, do_sni, res.sni); mbedtls_ssl_conf_own_cert(res.conf, &res.sni[0]->certificate, &res.sni[0]->key); if(res.sni[0]->certificate.next != NULL) mbedtls_ssl_conf_ca_chain(res.conf, res.sni[0]->certificate.next, NULL ); mbedtls_ssl_conf_read_timeout(res.conf, (uint32_t)res.client * 1000); logmsg(2, "push %s:%d", __FILE__, __LINE__); utarray_push_back(ut_http, &res); } return; } static void get_others(yaml_document_t *document, yaml_node_t *root) { yaml_node_pair_t *cur_pair; yaml_node_t *node; logmsg(1, "start get_others %s:%d", __FILE__, __LINE__); if(root->type != YAML_MAPPING_NODE) fatal("YAML root not mapping (%ld)", root->start_mark.line); for(cur_pair = root->data.mapping.pairs.start; cur_pair < root->data.mapping.pairs.top; cur_pair++) { node = yaml_document_get_node(document, cur_pair->key); if(node->type != YAML_SCALAR_NODE) fatal("YAML key not scalar (%ld)", node->start_mark.line); if(strcasecmp("Global", node->data.scalar.value) && strcasecmp("Backends", node->data.scalar.value) && strcasecmp("HTTPListeners", node->data.scalar.value) && strcasecmp("HTTPSListeners", node->data.scalar.value)) fatal("YAML unknown directive %s (%ld)", node->data.scalar.value, node->start_mark.line); } return; } static yaml_node_t * get_base(yaml_document_t *document, yaml_node_t *root, char *key, int type) { yaml_node_pair_t *cur_pair; yaml_node_t *node; for(cur_pair = root->data.mapping.pairs.start; cur_pair < root->data.mapping.pairs.top; cur_pair++) { node = yaml_document_get_node(document, cur_pair->key); if(!strcasecmp(key, node->data.scalar.value)) { node = yaml_document_get_node(document, cur_pair->value); if(node->type != type) return NULL; return node; } } return NULL; } void config(const int argc, char **argv) { int c_opt, o_check = 0, o_version = 0, is_key, i; char *f_conf = F_CONF; FILE *f_in; yaml_parser_t parser; yaml_document_t document; yaml_node_t *root, *sub; UT_icd backend_icd = {sizeof(BACKEND), NULL, NULL, NULL }; BACKEND *be; UT_icd http_icd = {sizeof(HTTP_LISTENER), NULL, NULL, NULL }; HTTP_LISTENER *http; memset(&global, '\0', sizeof(global)); opterr = 0; global.pid = "/var/run/pound.pid"; global.log_level = 0; while((c_opt = getopt(argc, argv, "f:cvd:p:")) > 0) switch(c_opt) { case 'f': /* configuration file specified on the commend line */ f_conf = optarg; logmsg(4, "config file option %s %s:%d", f_conf, __FILE__, __LINE__); break; case 'p': /* configuration file specified on the commend line */ global.pid = optarg; logmsg(4, "pid file option %s %s:%d", global.pid, __FILE__, __LINE__); break; case 'd': /* debug mode: run in foreground, messages to stdout */ global.log_level = atoi(optarg); logmsg(4, "debug option %d %s:%d", global.log_level, __FILE__, __LINE__); break; case 'c': /* check only: parse configuration and exit */ o_check = 1; break; case 'v': /* print version and exit */ o_version = 1; break; default: global.log_level = 1; logmsg(0, "Unknown flag %c", c_opt); exit(1); } if(o_version) { global.log_level = 1; logmsg(0, "Pound version %s", VERSION); exit(0); } if((f_in = fopen(f_conf, "r")) == NULL) fatal("File %s not found", f_conf); if(!yaml_parser_initialize(&parser)) fatal("Cannot initialise parser"); yaml_parser_set_input_file(&parser, f_in); if(!yaml_parser_load(&parser, &document)) fatal("Can't load configuration - YAML error \"%s\" at line %ld", parser.problem, parser.problem_mark.line); yaml_parser_delete(&parser); fclose(f_in); if((root = yaml_document_get_root_node(&document)) == NULL) fatal("YAML can't get root"); utarray_new(ut_backends, &backend_icd); utarray_new(ut_http, &http_icd); get_others(&document, root); if((sub = get_base(&document, root, "Global", YAML_MAPPING_NODE)) != NULL) get_global(&document, sub); if((sub = get_base(&document, root, "Backends", YAML_SEQUENCE_NODE)) == NULL) fatal("Missing Backends section"); get_backends(&document, sub); backends_len = utarray_len(ut_backends); if(backends_len == 0) fatal("No backends defined"); if((backends = calloc(backends_len, sizeof(BACKEND))) == NULL) fatal("Backends: out of memory"); for(i = 0, be = utarray_front(ut_backends); be != NULL; i++, be = utarray_next(ut_backends, be)) memcpy(&backends[i], be, sizeof(BACKEND)); utarray_free(ut_backends); if(get_base(&document, root, "HTTPListeners", YAML_SCALAR_NODE) == NULL) { if((sub = get_base(&document, root, "HTTPListeners", YAML_SEQUENCE_NODE)) == NULL) fatal("Missing HTTPListeners section"); get_http(&document, sub); } if(get_base(&document, root, "HTTPSListeners", YAML_SCALAR_NODE) == NULL) { if((sub = get_base(&document, root, "HTTPSListeners", YAML_SEQUENCE_NODE)) == NULL) fatal("Missing HTTPSListeners section"); get_https(&document, sub); } http_len = utarray_len(ut_http); if(http_len > 0) { if((http_listeners = calloc(http_len, sizeof(HTTP_LISTENER))) == NULL) fatal("Listeners: out of memory"); for(i = 0, http = utarray_front(ut_http); http != NULL; i++, http = utarray_next(ut_http, http)) memcpy(&http_listeners[i], http, sizeof(HTTP_LISTENER)); } utarray_free(ut_http); if(http_len == 0) fatal("No listeners defined"); yaml_document_delete(&document); if(o_check) exit(0); return; }Pound-3.0.2/src/backend.c0000664000175000017500000005406214150724211014151 0ustar rosegroseg/* * Pound - the reverse-proxy load-balancer * Copyright (C) 2002-2020 Apsis GmbH * * This file is part of Pound. * * Pound 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 3 of the License, or * (at your option) any later version. * * Pound 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Contact information: * Apsis GmbH * P.O.Box * 8707 Uetikon am See * Switzerland * EMail: roseg@apsis.ch */ #include "pound.h" #define EOF_LISTENER(S) {\ char *m;\ int f;\ \ nn_send((S), "", 0, 0);\ f = 100;\ nn_setsockopt(S, NN_SOL_SOCKET, NN_RCVTIMEO, &f, sizeof(f));\ if(nn_recv((S), &m, NN_MSG, 0) >= 0)\ nn_freemsg(m);\ nn_close(S);\ } static int backend_1(BACKEND *be, int s_listener, FILE *f_be, char *client_addr) { char buf[MAXBUF + 1], request[MAXBUF + 1], *msg; int no_content, is_chunked, reply_code, n; long content_length, total; regmatch_t match[2]; logmsg(1, "%lX start backend_1 %s:%d", pthread_self(), __FILE__, __LINE__); no_content = is_chunked = 0; content_length = total = 0L; /* receive the request from the listener and keep it for logging */ if(nn_recv(s_listener, &msg, NN_MSG, 0) <= 0) { logmsg(2, "%lX can't get request %s:%d", pthread_self(), __FILE__, __LINE__); EOF_LISTENER(s_listener); return -1; } strcpy(request, msg); nn_freemsg(msg); no_content = !strncasecmp("HEAD", request, 4); fputs(request, f_be); for(n = 0; n < MAXBUF && request[n]; n++) if(request[n] == '\r' || request[n] == '\n') { request[n] = '\0'; break; } logmsg(4, "%lX request:%s %s:%d", pthread_self(), request, __FILE__, __LINE__); /* receive the other headers from the listener and pass them to the back-end */ while(nn_recv(s_listener, &msg, NN_MSG, 0) > 0) { logmsg(4, "%lX header:%s %s:%d", pthread_self(), msg, __FILE__, __LINE__); fputs(msg, f_be); nn_freemsg(msg); } nn_freemsg(msg); logmsg(2, "%lX end request %s:%d", pthread_self(), __FILE__, __LINE__); fflush(f_be); total = 0L; if(fgets(buf, MAXBUF - 1, f_be) == NULL) { logmsg(2, "%lX no reply from backend %s:%d", pthread_self(), __FILE__, __LINE__); close(fileno(f_be)); fclose(f_be); EOF_LISTENER(s_listener); return -1; } nn_send(s_listener, buf, strlen(buf), 0); logmsg(4, "%lX reply %s %s:%d", pthread_self(), buf, __FILE__, __LINE__); for(;;) { if(regexec(&rex_Response, buf, 2, match, REG_ICASE)) continue; sscanf(buf + match[1].rm_so, "%d", &reply_code); if(reply_code >= 200) break; /* a 1xx reply - pass and skip */ while(fgets(buf, MAXBUF - 1, f_be) != NULL) { logmsg(4, "%lX 100 pass through %s:%d", pthread_self(), buf, __FILE__, __LINE__); nn_send(s_listener, buf, strlen(buf), 0); if(buf[0] == '\r' || buf[0] == '\n') break; } if(fgets(buf, MAXBUF, f_be) == NULL) { close(fileno(f_be)); fclose(f_be); EOF_LISTENER(s_listener); time_stamp(buf); logmsg(0, "%s - - [%s] \"%s\" premature EOF", client_addr, buf, request); return -1; } logmsg(4, "%lX %s %s:%d", pthread_self(), buf, __FILE__, __LINE__); nn_send(s_listener, buf, strlen(buf), 0); } total += strlen(buf); /* * receive the reply headers * * WARNING: we do not check here for malicious combinations, as we assume our servers to be OK! */ if(be->add_header) { nn_send(s_listener, be->add_header, strlen(be->add_header), 0); logmsg(4, "%lX add header %s %s:%d", pthread_self(), be->add_header, __FILE__, __LINE__); nn_send(s_listener, "\r\n", 2, 0); total += strlen(be->add_header) + 2; } while(fgets(buf, MAXBUF, f_be) != NULL) { logmsg(4, "%lX header %s %s:%d", pthread_self, buf, __FILE__, __LINE__); nn_send(s_listener, buf, strlen(buf), 0); total += strlen(buf); if(buf[0] == '\r' || buf[0] == '\n') break; if(!no_content) { if(!regexec(&rex_Chunked, buf, 0, NULL, REG_ICASE)) is_chunked = 1; else if(!regexec(&rex_ContentLength, buf, 2, match, REG_ICASE)) sscanf(buf + match[1].rm_so, "%ld", &content_length); } } logmsg(3, "%lX no_content %d, is_chunked %d, content_length %d %s:%d", pthread_self, no_content, is_chunked, content_length, __FILE__, __LINE__); if(!no_content) { if(is_chunked) { for(;;) { if(fgets(buf, MAXBUF, f_be) == NULL) break; nn_send(s_listener, buf, strlen(buf), 0); total += strlen(buf); sscanf(buf, "%lx", &content_length); logmsg(4, "%lX chunk %d %s:%d", pthread_self(), content_length, __FILE__, __LINE__); if(content_length == 0L) break; while(content_length > 0L) { if((n = fread(buf, sizeof(char), content_length > MAXBUF? MAXBUF: content_length, f_be)) > 0) { logmsg(4, "%lX read %d %s:%d", pthread_self(), n, __FILE__, __LINE__); nn_send(s_listener, buf, n, 0); content_length -= n; total += n; } else { content_length = -1L; logmsg(4, "%lX read return %d %s:%d", pthread_self(), n, __FILE__, __LINE__); } } /* CR/LF separator */ fgets(buf, MAXBUF, f_be); } /* possibly some more headers */ while(fgets(buf, MAXBUF, f_be) != NULL) { nn_send(s_listener, buf, strlen(buf), 0); logmsg(4, "%lX additional header %s %s:%d", pthread_self(), buf, __FILE__, __LINE__); total += strlen(buf); if(buf[0] == '\r' || buf[0] == '\n') break; } } else while(content_length > 0L) { memset(buf, '\0', MAXBUF + 1); if((n = fread(buf, sizeof(char), content_length > MAXBUF? MAXBUF: content_length, f_be)) > 0) { logmsg(4, "%lX read %d %s:%d", pthread_self(), n, __FILE__, __LINE__); nn_send(s_listener, buf, n, 0); content_length -= n; total += n; } else { logmsg(4, "%lX read return %d %s:%d", pthread_self(), n, __FILE__, __LINE__); content_length = -1L; } } } EOF_LISTENER(s_listener); time_stamp(buf); logmsg(0, "%s - - [%s] \"%s\" %d %ld", client_addr, buf, request, reply_code, total); return fileno(f_be); } static int backend_2(BACKEND *be, int s_listener, FILE *f_be, char *client_addr) { struct hpack_headerblock *headers, *r_headers; struct hpack_header *h; struct hpack_table *tab; int TABSIZE, FRAMESIZE, n, no_content, is_chunked, reply_code; long content_length, total; size_t h_len; char buf[MAXBUF], *method, *path, *authority, *cp, *tp, *ttp, request[MAXBUF]; unsigned char *msg, *content; regmatch_t match[2]; logmsg(1, "%lX start backend_2 %s:%d", pthread_self(), __FILE__, __LINE__); if(nn_recv(s_listener, &msg, NN_MSG, 0) != sizeof(int)) { logmsg(2, "%lX can't get FRAMESIZE %s:%d", pthread_self(), __FILE__, __LINE__); EOF_LISTENER(s_listener); return -1; } memcpy(&FRAMESIZE, msg, sizeof(int)); nn_freemsg(msg); if(nn_recv(s_listener, &msg, NN_MSG, 0) != sizeof(int)) { logmsg(2, "%lX can't get TABSIZE %s:%d", pthread_self(), __FILE__, __LINE__); EOF_LISTENER(s_listener); return -1; } memcpy(&TABSIZE, msg, sizeof(int)); nn_freemsg(msg); if(nn_recv(s_listener, &msg, NN_MSG, 0) != sizeof(struct hpack_headerblock *)) { logmsg(2, "%lX can't get headers %s:%d", pthread_self(), __FILE__, __LINE__); EOF_LISTENER(s_listener); return -1; } memcpy(&headers, msg, sizeof(struct hpack_headerblock *)); nn_freemsg(msg); logmsg(3, "%lX FRAMESIZE %d, TABSIZE %d %s:%d", pthread_self(), FRAMESIZE, TABSIZE, __FILE__, __LINE__); method = path = authority = NULL; TAILQ_FOREACH(h, headers, hdr_entry) { if(!strcmp(h->hdr_name, ":method")) method = h->hdr_value; else if(!strcmp(h->hdr_name, ":authority")) authority = h->hdr_value; else if(!strcmp(h->hdr_name, ":path")) path = h->hdr_value; } if(method == NULL || path == NULL || authority == NULL) { logmsg(2, "%lX can't find method/path/authority %s:%d", pthread_self(), __FILE__, __LINE__); EOF_LISTENER(s_listener); return -1; } is_chunked = 0; snprintf(request, MAXBUF, "%s %s HTTP/1.1", method, path); fprintf(f_be, "%s\r\n", request); logmsg(4, "%lX %s %s HTTP/1.1 %s:%d", pthread_self(), method, path, __FILE__, __LINE__); fprintf(f_be, "host: %s\r\n", authority); logmsg(4, "%lX host: %s %s:%d", pthread_self(), authority, __FILE__, __LINE__); TAILQ_FOREACH(h, headers, hdr_entry) { if(!strcasecmp(h->hdr_name, ":method") || !strcasecmp(h->hdr_name, ":authority") || !strcasecmp(h->hdr_name, ":path") || !strcasecmp(h->hdr_name, ":scheme")) continue; if(!strcmp(h->hdr_name, "te") || !strcasecmp(h->hdr_name, "transfer-encoding")) { fprintf(f_be, "%s: %s, chunked\r\n", h->hdr_name, h->hdr_value); logmsg(4, "%lX %s: %s, chunked %s:%d", pthread_self(), h->hdr_name, h->hdr_value, __FILE__, __LINE__); is_chunked = 1; } if(!strcasecmp(h->hdr_name, "upgrade") || !strcasecmp(h->hdr_name, "upgrade-insecure-requests")) { logmsg(4, "%lX %s: skipped %s:%d", pthread_self(), h->hdr_name, __FILE__, __LINE__); } else { fprintf(f_be, "%s: %s\r\n", h->hdr_name, h->hdr_value); logmsg(4, "%lX %s: %s %s:%d", pthread_self(), h->hdr_name, h->hdr_value, __FILE__, __LINE__); } } if(!is_chunked) { fprintf(f_be, "te: chunked\r\n"); logmsg(4, "%lX te: chunked %s:%d", pthread_self(), __FILE__, __LINE__); } fprintf(f_be, "\r\n"); logmsg(4, "%lX ---end of headers--- %s:%d", pthread_self(), __FILE__, __LINE__); while((n = nn_recv(s_listener, &msg, NN_MSG, 0)) > 0) { logmsg(4, "%lX chunk %d %s:%d", pthread_self(), n, __FILE__, __LINE__); fprintf(f_be, "%X\r\n", n); fwrite(msg, 1, n, f_be); fwrite("\r\n", 1, 2, f_be); nn_freemsg(msg); } fprintf(f_be, "0\r\n"); logmsg(4, "%lX ---end of content--- %s:%d", pthread_self(), __FILE__, __LINE__); if(nn_recv(s_listener, &msg, NN_MSG, 0) == sizeof(struct hpack_headerblock *)) { memcpy(&headers, msg, sizeof(struct hpack_headerblock *)); TAILQ_FOREACH(h, headers, hdr_entry) { if(!strcasecmp(h->hdr_name, ":method") || !strcasecmp(h->hdr_name, ":authority") || !strcasecmp(h->hdr_name, ":path") || !strcasecmp(h->hdr_name, ":scheme")) continue; fprintf(f_be, "%s: %s\r\n", h->hdr_name, h->hdr_value); logmsg(4, "%lX trailing header %s: %s %s:%d", pthread_self(), h->hdr_name, h->hdr_value, __FILE__, __LINE__); } } else { logmsg(2, "%lX no trailing headers %s:%d", pthread_self(), __FILE__, __LINE__); } nn_freemsg(msg); fprintf(f_be, "\r\n"); logmsg(4, "%lX ---end of trailing headers --- %s:%d", pthread_self(), __FILE__, __LINE__); no_content = !strncasecmp("HEAD", method, 4); total = 0L; r_headers = hpack_headerblock_new(); tab = hpack_table_new(TABSIZE); for(;;) { do { if(fgets(buf, MAXBUF - 1, f_be) == NULL) { logmsg(2, "%lX no reply from backend %s:%d", pthread_self(), __FILE__, __LINE__); hpack_headerblock_free(r_headers); hpack_table_free(tab); close(fileno(f_be)); fclose(f_be); EOF_LISTENER(s_listener); return -1; } else logmsg(4, "%lX read %s %s:%d", pthread_self(), buf, __FILE__, __LINE__); } while(regexec(&rex_Response, buf, 2, match, REG_ICASE)); sscanf(buf + match[1].rm_so, "%d", &reply_code); logmsg(4, "%lX reply code %d %s:%d", pthread_self(), reply_code, __FILE__, __LINE__); if(reply_code >= 200) { sprintf(buf, "%d", reply_code); hpack_header_add(r_headers, ":status", buf, HPACK_INDEX); logmsg(4, "%lX reply header :status %s %s:%d", pthread_self(), buf, __FILE__, __LINE__); total += strlen(":status") + strlen(buf); break; } } if(be->add_header) { strncpy(buf, be->add_header, MAXBUF); cp = strtok_r(buf, ":", &tp); if((ttp = strtok_r(NULL, "\r", &tp)) != NULL) while(*ttp == ' ') ttp++; else ttp = ""; hpack_header_add(r_headers, cp, ttp, HPACK_INDEX); logmsg(4, "%lX add header %s %s:%d", pthread_self(), be->add_header, __FILE__, __LINE__); total += strlen(cp) + strlen(ttp); } is_chunked = 0; content_length = 0L; while(fgets(buf, MAXBUF, f_be) != NULL) { if(buf[0] == '\r' || buf[0] == '\n') break; if(!no_content) { if(!regexec(&rex_Chunked, buf, 0, NULL, REG_ICASE)) is_chunked = 1; else if(!regexec(&rex_ContentLength, buf, 2, match, REG_ICASE)) sscanf(buf + match[1].rm_so, "%ld", &content_length); } cp = strtok_r(buf, ":", &tp); if(!strcasecmp(cp, "connection") || !strcasecmp(cp, "keep-alive") || !strcasecmp(cp, "proxy-connection") || !strcasecmp(cp, "transfer-encoding") || !strcasecmp(cp, "te") || !strcasecmp(cp, "content-length") || !strcasecmp(cp, "upgrade")) { logmsg(4, "%lX header %s skipped %s:%d", pthread_self(), buf, __FILE__, __LINE__); continue; } if((ttp = strtok_r(NULL, "\r", &tp)) != NULL) while(*ttp == ' ') ttp++; else ttp = ""; hpack_header_add(r_headers, cp, ttp, HPACK_INDEX); logmsg(4, "%lX header %s => %s: %s %s:%d", pthread_self(), buf, cp, ttp, __FILE__, __LINE__); total += strlen(cp) + strlen(ttp); } if((content = hpack_encode(r_headers, &h_len, tab)) == NULL) { logmsg(4, "%lX failed encode %s:%d", pthread_self(), __FILE__, __LINE__); hpack_headerblock_free(r_headers); hpack_table_free(tab); close(fileno(f_be)); fclose(f_be); EOF_LISTENER(s_listener); return -1; } for(n = 0; h_len > 0; ) { nn_send(s_listener, content + n, h_len > FRAMESIZE? FRAMESIZE: h_len, 0); n += (h_len > FRAMESIZE? FRAMESIZE: h_len); h_len -= (h_len > FRAMESIZE? FRAMESIZE: h_len); } free(content); hpack_headerblock_free(r_headers); /* end of headers */ nn_send(s_listener, "", 0, 0); logmsg(3, "%lX no_content %d, is_chunked %d, content_length %d %s:%d", pthread_self(), no_content, is_chunked, content_length, __FILE__, __LINE__); if(!no_content) { if((content = malloc(FRAMESIZE)) == NULL) { logmsg(4, "%lX out of memory for content %s:%d", pthread_self(), __FILE__, __LINE__); hpack_table_free(tab); close(fileno(f_be)); fclose(f_be); EOF_LISTENER(s_listener); return -1; } if(is_chunked) { for(;;) { if(fgets(buf, MAXBUF, f_be) == NULL) break; sscanf(buf, "%lx", &content_length); logmsg(4, "%lX chunk %d (%s) %s:%d", pthread_self(), content_length, buf, __FILE__, __LINE__); if(content_length == 0L) break; while(content_length > 0L) { if((n = fread(content, sizeof(char), content_length > FRAMESIZE? FRAMESIZE: content_length, f_be)) > 0) { logmsg(4, "%lX read %d %s:%d", pthread_self(), n, __FILE__, __LINE__); nn_send(s_listener, content, n, 0); content_length -= n; total += n; } else { content_length = -1L; logmsg(4, "%lX read return %d %s:%d", pthread_self(), n, __FILE__, __LINE__); } } /* CR/LF separator */ fgets(buf, MAXBUF, f_be); } } else while(content_length > 0L) { if((n = fread(content, sizeof(char), content_length > FRAMESIZE? FRAMESIZE: content_length, f_be)) > 0) { logmsg(4, "%lX read %d %s:%d", pthread_self(), n, __FILE__, __LINE__); nn_send(s_listener, content, n, 0); content_length -= n; total += n; } else { logmsg(4, "%lX read return %d %s:%d", pthread_self(), n, __FILE__, __LINE__); content_length = -1L; } } free(content); } /* end of data */ nn_send(s_listener, "", 0, 0); logmsg(4, "%lX sent end-of-content %s:%d", pthread_self(), __FILE__, __LINE__); EOF_LISTENER(s_listener); time_stamp(buf); logmsg(0, "%s - - [%s] \"%s\" %d %ld", client_addr, buf, request, reply_code, total); return fileno(f_be); } void * thr_backend(void *arg) { BACKEND *be; int s_listener, s_be, res, flags, http_ver; char *msg, client_addr[NI_MAXHOST], *cp, *saveptr; struct pollfd be_poll; struct linger s_linger; struct timeval s_time; FILE *f_be; regmatch_t match[2]; be = (BACKEND *)arg; sem_post(&sem_start); s_be = -1; for(;;) { msg = NULL; if(nn_recv(be->sock, &msg, NN_MSG, 0) <= 0) { logmsg(0, "Backend: can't receive from queue socket"); continue; } if((s_listener = nn_socket(AF_SP, NN_PAIR)) < 0) { logmsg(0, "Backend: can't create direct socket"); continue; } if(nn_connect(s_listener, msg) < 0) { logmsg(0, "Backend: can't connect to direct socket"); continue; } nn_freemsg(msg); /* prepare the back-end socket */ if(s_be >= 0) { be_poll.fd = s_be; be_poll.events = POLLIN; if(poll(&be_poll, 1, 1) == 1) { /* only EOF can be read! */ close(s_be); s_be = -1; } } if(s_be < 0) { /* open the socket to the backend */ if((s_be = socket(be->addr->ai_family, SOCK_STREAM, 0)) < 0) { logmsg(0, "Backend: can't create socket"); s_be = -1; EOF_LISTENER(s_listener); continue; } /* wait at most 'timeout' seconds for the connection */ flags = fcntl(s_be, F_GETFL, &flags); fcntl(s_be, F_SETFL, flags | O_NONBLOCK); if(connect(s_be, be->addr->ai_addr, be->addr->ai_addrlen)) { if(errno != EINPROGRESS) { logmsg(0, "Backend: can't connect socket"); close(s_be); s_be = -1; EOF_LISTENER(s_listener); continue; } be_poll.fd = s_be; be_poll.events = POLLOUT; if(poll(&be_poll, 1, be->timeout * 1000) != 1) { logmsg(0, "Backend: can't connect"); close(s_be); be->is_dead = 1; s_be = -1; EOF_LISTENER(s_listener); continue; } } fcntl(s_be, F_SETFL, flags); s_linger.l_onoff = 1; s_linger.l_linger = 0; setsockopt(s_be, SOL_SOCKET, SO_LINGER, &s_linger, sizeof(s_linger)); s_time.tv_sec = be->timeout; s_time.tv_usec = 0; setsockopt(s_be, SOL_SOCKET, SO_RCVTIMEO, &s_time, sizeof(s_time)); setsockopt(s_be, SOL_SOCKET, SO_SNDTIMEO, &s_time, sizeof(s_time)); f_be = fdopen(s_be, "r+"); } /* received the protocol information from the listener */ if(nn_recv(s_listener, &msg, NN_MSG, 0) != sizeof(int)) { logmsg(2, "%lX can't get protocol version %s:%d", pthread_self(), __FILE__, __LINE__); EOF_LISTENER(s_listener); continue; } memcpy(&http_ver, msg, sizeof(int)); nn_freemsg(msg); logmsg(4, "%lX http_ver %d %s:%d", pthread_self(), http_ver, __FILE__, __LINE__); /* receive the client address from the listener and keep it for logging */ if(nn_recv(s_listener, &msg, NN_MSG, 0) <= 0) { logmsg(2, "thr_backend: can't get client address %s:%d", pthread_self(), __FILE__, __LINE__); EOF_LISTENER(s_listener); continue; } strcpy(client_addr, msg); nn_freemsg(msg); logmsg(4, "%lX client_addr:%s %s:%d", pthread_self(), client_addr, __FILE__, __LINE__); /* receive the reply from the back-end and pass it to the listener in the appropriate protocol */ if(http_ver == 2) s_be = backend_2(be, s_listener, f_be, client_addr); else s_be = backend_1(be, s_listener, f_be, client_addr); } } Pound-3.0.2/src/util.c0000664000175000017500000001257514150724211013542 0ustar rosegroseg/* * Pound - the reverse-proxy load-balancer * Copyright (C) 2002-2020 Apsis GmbH * * This file is part of Pound. * * Pound 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 3 of the License, or * (at your option) any later version. * * Pound 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Contact information: * Apsis GmbH * P.O.Box * 8707 Uetikon am See * Switzerland * EMail: roseg@apsis.ch */ #include "pound.h" #ifdef HAVE_STDARG_H void logmsg(int level, const char *fmt, ...) { char buf[MAXBUF]; va_list ap; if(level > global.log_level) return; buf[MAXBUF - 1] = '\0'; va_start(ap, fmt); vsnprintf(buf, MAXBUF - 1, fmt, ap); va_end(ap); if(global.log_level > 0) fprintf(stderr, "%s\n", buf); else syslog(global.log_facility | LOG_INFO, "%s", buf); return; } #else void logmsg(int level, const char *fmt, va_alist) va_dcl { char buf[MAXBUF]; va_list ap; struct tm *t_now, t_res; if(level > global.log_level) return; buf[MAXBUF - 1] = '\0'; va_start(ap); vsnprintf(buf, MAXBUF - 1, fmt, ap); va_end(ap); if(global.log_level > 0) fprintf(stderr, "%s\n", buf); else syslog(global.log_facility | LOG_INFO, "%s", buf); return; } #endif void * thr_resurrect(void *arg) { BACKEND **dead_be, *t_be; int n_be, i, j, flags; struct pollfd *dead_poll; char be_addr[NI_MAXHOST]; logmsg(1, "Starting resurrector thread %s:%d", __FILE__, __LINE__); for(;;) { sleep(RESURRECT_CYCLE - RESURRECT_TO); logmsg(2, "Resurrect: wake-up %s:%d", __FILE__, __LINE__); for(n_be = 0, i = 0; i < backends_len; i++) if(backends[i].is_dead) n_be++; if(n_be == 0) continue; if((dead_be = calloc(n_be, sizeof(BACKEND *))) == NULL) { logmsg(0, "resurrect: out of memory"); continue; } if((dead_poll = calloc(n_be, sizeof(struct pollfd))) == NULL) { free(dead_be); logmsg(0, "resurrect: out of memory"); continue; } for(i = 0, j = 0; i < backends_len && j < n_be; i++) if(backends[i].is_dead) { dead_poll[j].fd = socket(backends[i].addr->ai_family, SOCK_STREAM, 0); flags = fcntl(dead_poll[j].fd, F_GETFL, &flags); fcntl(dead_poll[j].fd, F_SETFL, flags | O_NONBLOCK); if(!connect(dead_poll[j].fd, backends[i].addr->ai_addr, backends[i].addr->ai_addrlen)) { logmsg(2, "Resurrect: backend %d immediate connect %s:%d", i, __FILE__, __LINE__); backends[i].is_dead = 0; close(dead_poll[j].fd); continue; } if(errno != EINPROGRESS) { backends[i].is_dead = 1; close(dead_poll[j].fd); continue; } dead_poll[j].events = POLLOUT; dead_be[j++] = &backends[i]; } n_be = j; sleep(RESURRECT_TO); logmsg(2, "Resurrect: check backends %s:%d", i, __FILE__, __LINE__); poll(dead_poll, n_be, 1); for(i = 0; i < n_be; i++) { if(dead_poll[i].revents & POLLOUT) { dead_be[i]->is_dead = 0; getnameinfo(dead_be[i]->addr->ai_addr, dead_be[i]->addr->ai_addrlen, be_addr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); logmsg(0, "Backend %s resurrected", be_addr); } close(dead_poll[i].fd); } free(dead_be); free(dead_poll); } } void time_stamp(char *buf) { time_t t_now; struct tm *s_now; t_now = time(NULL); s_now = localtime(&t_now); if(s_now == NULL) { *buf = '\0'; return; } strftime(buf, MAXBUF, "%c", s_now); return; } int do_sni(void *data, mbedtls_ssl_context *ctx, const unsigned char *host_name, size_t host_len) { char *name; int i, j; SNI **sni; logmsg(1, "%lX start sni %s:%d", pthread_self(), __FILE__, __LINE__); sni = (SNI **)data; if((name = malloc(host_len + 1)) == NULL) { logmsg(0, "SNI: out of memory!"); return 0; } memset(name, '\0', host_len + 1); strncpy(name, host_name, host_len); logmsg(4, "%lX sni for %s %s:%d", pthread_self(), name, __FILE__, __LINE__); for(i = 0; sni[i] != NULL; i++) for(j = 0; j < sni[i]->host_len; j++) if(!regexec(&sni[i]->host[j], name, 0, NULL, REG_ICASE)) { logmsg(2, "%lX: found match at %d %s:%d", pthread_self(), j, __FILE__, __LINE__); return mbedtls_ssl_set_hs_own_cert(ctx, &sni[i]->certificate, &sni[i]->key); } /* if no match found: use the default certificate (from conf, aka the first sni) and let the client figure it out */ logmsg(2, "%lX %s: no match found %s:%d", pthread_self(), name, __FILE__, __LINE__); return 0; } Pound-3.0.2/src/pound.c0000664000175000017500000002436614150724211013713 0ustar rosegroseg/* * Pound - the reverse-proxy load-balancer * Copyright (C) 2002-2020 Apsis GmbH * * This file is part of Pound. * * Pound 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 3 of the License, or * (at your option) any later version. * * Pound 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Contact information: * Apsis GmbH * P.O.Box * 8707 Uetikon am See * Switzerland * EMail: roseg@apsis.ch */ #include "pound.h" GLOBAL global; BACKEND *backends; int backends_len; HTTP_LISTENER *http_listeners; int http_len; mbedtls_ctr_drbg_context tls_ctr_drbg; regex_t rex_Expect, rex_Response, rex_ContentLength, rex_Chunked, rex_Connection_Closed, rex_Connection_HTTP2, rex_Upgrade_HTTP2; sem_t sem_start; int main(const int argc, char **argv) { int i, j, n, s_in, s_listener; HTTP_LISTENER **listeners; struct pollfd *listener_poll; struct addrinfo *listener_addr; BACKEND *be; char name[NI_MAXHOST], port[NI_MAXSERV], *msg; pthread_t thr; pthread_attr_t attr; FILE *f_pid; mbedtls_entropy_context tls_entropy; memset(&global, 0, sizeof(global)); global.log_facility = LOG_FACILITY; openlog("pound", LOG_CONS | LOG_NDELAY, global.log_facility); mbedtls_entropy_init(&tls_entropy); mbedtls_ctr_drbg_init(&tls_ctr_drbg); getentropy(name, 16); mbedtls_ctr_drbg_seed(&tls_ctr_drbg, mbedtls_entropy_func, &tls_entropy, (const unsigned char *)name, 16); hpack_init(); config(argc, argv); /* These are handled elsewhere */ (void)signal(SIGHUP, SIG_IGN); (void)signal(SIGPIPE, SIG_IGN); /* Common regex */ regcomp(&rex_Response, "HTTP[^ \t]*[ \t]+([0-9]+).*", REG_ICASE | REG_EXTENDED); regcomp(&rex_Expect, "Expect:[ \t]*100-continue", REG_ICASE | REG_EXTENDED); regcomp(&rex_ContentLength, "Content-Length:[ \t]*([0-9]+)", REG_ICASE | REG_EXTENDED); regcomp(&rex_Chunked, "Transfer-Encoding:.*chunked", REG_ICASE | REG_EXTENDED | REG_NOSUB); regcomp(&rex_Connection_Closed, "Connection:.*close", REG_ICASE | REG_EXTENDED | REG_NOSUB); regcomp(&rex_Connection_HTTP2, "Connection:.*upgrade", REG_ICASE | REG_EXTENDED | REG_NOSUB); regcomp(&rex_Upgrade_HTTP2, "Upgrade:.*h2c", REG_ICASE | REG_EXTENDED | REG_NOSUB); /* preamble */ global.http2_preamble[0] = "PRI * HTTP/2.0\r\n"; global.http2_preamble[1] = "\r\n"; global.http2_preamble[2] = "SM\r\n"; global.http2_preamble[3] = "\r\n"; global.http2_preamble[4] = NULL; /* Daemonize if needed */ if(global.log_level == 0) { switch(fork()) { case 0: close(0); close(1); close(2); if(setsid() < 0) { logmsg(0, "Failed setsid"); exit(1); } break; case -1: logmsg(0, "Can't fork"); exit(1); break; default: exit(0); } switch(fork()) { case 0: break; case -1: logmsg(0, "Can't fork"); exit(1); break; default: exit(0); } } if((f_pid = fopen(global.pid, "w")) == NULL) logmsg(0, "Can't open pid file %s", global.pid); else { fprintf(f_pid, "%d", getpid()); fclose(f_pid); } /* chroot, set[ug]id */ if(global.root_jail) if(chroot(global.root_jail)) { logmsg(0, "Can't chroot to %s", global.root_jail); exit(1); } if(global.group > 0) if(setgid(global.group)) { logmsg(0, "Can't setgid %d", global.group); exit(1); } if(global.user > 0) if(setuid(global.user)) { logmsg(0, "Can't setuid %d", global.user); exit(1); } /* prepare threads */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); /* resurrector */ if(pthread_create(&thr, &attr, thr_resurrect, NULL)) { logmsg(0, "Resurrector: can't start thread"); exit(1); } logmsg(2, "Prepare backends %s:%d", __FILE__, __LINE__); /* prepare back-ends */ for(i = 0; i < backends_len; i++) { snprintf(name, NI_MAXHOST, "inproc://BE_%d", i); if((backends[i].sock = nn_socket(AF_SP, NN_PULL)) < 0) { logmsg(0, "Backend %d: can't create queue socket", i); exit(1); } if(nn_bind(backends[i].sock, name) < 0) { logmsg(0, "Backend %d: can't bind queue socket", i); exit(1); } if((backends[i].sock_in = nn_socket(AF_SP, NN_PUSH)) < 0) { logmsg(0, "Backend %d: can't create queue socket", i); exit(1); } if(nn_connect(backends[i].sock_in, name) < 0) { logmsg(0, "Backend %d: can't connect queue socket", i); exit(1); } sem_init(&sem_start, 0, 0); for(j = 0; j < backends[i].threads; j++) if(pthread_create(&thr, &attr, thr_backend, (void *)&backends[i])) { logmsg(0, "Backend %d: can't start threads %d", i); exit(1); } for(j = 0; j < backends[i].threads; j++) sem_wait(&sem_start); sem_destroy(&sem_start); } /* prepare listeners */ logmsg(2, "Prepare listeners %s:%d", __FILE__, __LINE__); for(i = 0, n = 0; i < http_len; i++) { /* prepare the services */ logmsg(3, "Prepare services for listener %d %s:%d", i, __FILE__, __LINE__); for(j = 0; j < http_listeners[i].services_len; j++) { snprintf(name, NI_MAXHOST, "inproc://SVC_%d_%d", i, j); if((http_listeners[i].services[j]->sock = nn_socket(AF_SP, NN_REP)) < 0) { logmsg(0, "Service %d/%d: can't create queue socket", i, j); exit(1); } if(nn_bind(http_listeners[i].services[j]->sock, name) < 0) { logmsg(0, "Service %d/%d: can't bind queue socket", i, j); exit(1); } if((http_listeners[i].services[j]->sock_in = nn_socket(AF_SP, NN_REQ)) < 0) { logmsg(0, "Service %d/%d: can't create queue socket", i, j); exit(1); } if(nn_connect(http_listeners[i].services[j]->sock_in, name) < 0) { logmsg(0, "Service %d/%d: can't connect queue socket", i, j); exit(1); } sem_init(&sem_start, 0, 0); if(pthread_create(&thr, &attr, thr_service, (void *)http_listeners[i].services[j])) { logmsg(0, "Service %d/%d: can't start thread", i, j); exit(1); } sem_wait(&sem_start); sem_destroy(&sem_start); } /* prepare the listeners */ snprintf(name, NI_MAXHOST, "inproc://HTTP%d", i); if((http_listeners[i].sock_fan = nn_socket(AF_SP, NN_PULL)) < 0) { logmsg(0, "Listener %d: can't create fan socket", i); exit(1); } if(nn_bind(http_listeners[i].sock_fan, name) < 0) { logmsg(0, "Listener %d: can't bind fan socket", i); exit(1); } if((http_listeners[i].sock_in = nn_socket(AF_SP, NN_PUSH)) < 0) { logmsg(0, "Listener %d: can't create in socket", i); exit(1); } if(nn_connect(http_listeners[i].sock_in, name) < 0) { logmsg(0, "Listener %d: can't connect in socket", i); exit(1); } sem_init(&sem_start, 0, 0); for(j = 0; j < http_listeners[i].threads; j++) if(pthread_create(&thr, &attr, thr_http, (void *)&http_listeners[i])) { logmsg(0, "Http %d: can't start listener threads %d", i, j); exit(1); } for(j = 0; j < http_listeners[i].threads; j++) sem_wait(&sem_start); sem_destroy(&sem_start); for(listener_addr = http_listeners[i].addr; listener_addr != NULL; listener_addr = listener_addr->ai_next) n++; } if((listeners = calloc(n, sizeof(HTTP_LISTENER *))) == NULL || (listener_poll = calloc(n, sizeof(struct pollfd))) == NULL) { logmsg(0, "Listener poll: out of memory"); exit(1); } for(i = 0, n = 0; i < http_len; i++) for(listener_addr = http_listeners[i].addr; listener_addr != NULL; listener_addr = listener_addr->ai_next) { getnameinfo(listener_addr->ai_addr, listener_addr->ai_addrlen, name, NI_MAXHOST, port, NI_MAXSERV, NI_NUMERICHOST); if((listener_poll[n].fd = socket(listener_addr->ai_family, SOCK_STREAM, 0)) < 0) { logmsg(0, "Listener %s:%s: can't open socket", name, port); continue; } if(bind(listener_poll[n].fd, listener_addr->ai_addr, listener_addr->ai_addrlen)) { close(listener_poll[n].fd); logmsg(0, "Listener %s:%s: can't bind socket", name, port); continue; } if(listen(listener_poll[n].fd, http_listeners[i].threads * 2)) { close(listener_poll[n].fd); logmsg(0, "Listener %s:%s: can't listen to socket", name, port); continue; } listener_poll[n].events = POLLIN; listeners[n++] = &http_listeners[i]; } for(;;) { poll(listener_poll, n, -1); for(i = 0; i < n; i++) if(listener_poll[i].revents & POLLIN) { listener_poll[i].revents = 0; if((s_in = accept(listener_poll[i].fd, NULL, NULL)) < 0) { logmsg(0, "Bad accept"); continue; } nn_send(listeners[i]->sock_in, &s_in, sizeof(s_in), 0); } } } Pound-3.0.2/man/0000775000175000017500000000000014150724211012373 5ustar rosegrosegPound-3.0.2/man/pound.80000664000175000017500000003346514150724211013624 0ustar rosegroseg.TH POUND "8" "Jan 2010" "pound" "System Manager's Manual" .SH NAME pound \- HTTP/HTTPS reverse-proxy and load-balancer .SH SYNOPSIS .TP .B pound [\fI-v\fR] [\fI-c\fR] [\fI-d level\fR] [\fI-f config_file\fR] [\fI-p pid_file\fR] .SH DESCRIPTION .PP .B Pound is a reverse-proxy load balancing server. It accepts requests from HTTP/HTTPS clients and distributes them to one or more Web servers. The HTTPS requests are decrypted and passed to the back-ends as plain HTTP. .PP If more than one back-end server is defined, .B Pound chooses one of them randomly. By default, .B Pound keeps track of associations between clients and back-end servers (sessions). .SH GENERAL PRINCIPLES .P In general .B Pound needs three types of objects defined in order to function: .IR listeners , .I services and .IR back-ends . .TP \fBListeners\fR A .I listener is a definition of how .B Pound receives requests from the clients (browsers). Two types of .I listeners may be defined: regular HTTP .I listeners and HTTPS (HTTP over SSL/TLS) .IR listeners . At the very least a .I listener must define the address and port to listen on, with additional requirements for HTTPS .IR listeners . .TP \fBServices\fR A .I service is the definition of how the requests are answered. When a request is received .B Pound attempts to match them to each .I service in turn. The .I services may define their own conditions as to which requests they can answer: typically this involves certain URLs (images only, or a certain path) or specific headers (such as the Host header). .TP \fBBack-ends\fR The .I back-ends are the actual servers for the content requested. By itself, .B Pound supplies no responses - all contents must be received from a "real" web server. The .I back-end defines how the server should be contacted. .IP Multiple .I back-ends may be used within a .IR service , in which case .B Pound will load-balance between the available .IR back-ends . .IP If a .I back-end fails to respond it will be considered "dead", in which case .B Pound will stop sending requests to it. Dead .I back-ends are periodically checked for availability, and once they respond again they are "resurected" and requests are sent again their way. If no .I back-ends are available (none were defined, or all are "dead") then .B Pound will reply with "503 Service Unavailable", without checking additional .IR services . .IP The connection between .B Pound and the .I back-ends is always via HTTP, regardless of the actual protocol used between .B Pound and the client. .SH OPTIONS Options available (see also below for configuration file options): .TP \fB\-v\fR Print version: .B Pound will exit immediately after printing the current version. .TP \fB\-c\fR Check only: .B Pound will exit immediately after parsing the configuration file. This may be used for running a quick syntax check before actually activating a server. .TP \fB\-d\fR level Debug mode: if level is greater than 0 error messages will be sent to stdout and .B Pound will stay in the foreground. Level 0 (default) are the regular log messages, level 1 and up will produce more detailed information. .TP \fB\-f\fR config_file Location of the configuration file (see below for a full description of the format). Default: .I /etc/pound/pound.yaml .TP \fB\-p\fR pid_file Location of the pid file. .B Pound will write its own pid into this file. Normally this is used for shell scripts that control starting and stopping of the daemon. Default: .I /var/run/pound.pid .PP One (or more) copies of .B Pound should be started at boot time. Use "big iron" if you expect heavy loads: while .B Pound is as light-weight as we know how to make it, with a lot of simultaneous requests it will use quite a bit of CPU and memory. Multiple CPUs are your friend. .SH "CONFIGURATION FILE" The configuration file is in standard YAML syntax. There are four blocks of directives: .B Global directives (they affect the settings for the entire program instance), .B Backends directives, defining the available backends, .B HTTPlisteners directives (they define which requests .B Pound will listen for), and .B HTTPSlisteners directives (same as HTTPlistener but via TLS). .SS "Global Directives" .TP \fBUser\fR: user_name Specify the user .B Pound will run as (must be defined in \fI/etc/passwd\fR). .TP \fBGroup\fR: group_name Specify the group .B Pound will run as (must be defined in \fI/etc/group\fR). .TP \fBRootJail\fR: directory_path_and_name Specify the directory that .B Pound will chroot to at runtime. Please note that SSL may require access to /dev/urandom, so make sure you create a device by that name, accessible from the root jail directory. .B Pound may also require access to .I /dev/syslog or similar. .TP \fBErr404\fR: path_to_file Specify a path to an HTML file to be returned in case of a 404 error. .TP \fBErr405\fR: path_to_file Specify a path to an HTML file to be returned in case of a 405 error. .TP \fBErr500\fR: path_to_file Specify a path to an HTML file to be returned in case of a 500 error. .SS "Backends" A back-end is a definition of a single back-end server .B Pound will use to reply to incoming requests. Each backend must be marked with an anchor. The following directives are available: .TP \fBAddress\fR: address The address that .B Pound will connect to. This can be a numeric IP address, or a symbolic host name that must be resolvable at run-time. This is a .B mandatory parameter. .TP \fBPort\fR: port The port number that .B Pound will connect to. This is a .B mandatory parameter. .TP \fBTimeout\fR: number How long to wait for a backend (server) to complete and operation. Default: 15 seconds. .TP \fBThreads\fR: number How many threads will be used to service requests to this backend. See also below for remarks on performance tuning. Default: 8 threads. .TP \fBHeadAdd\fR: header A header to add to each reply received from this backend. The header is a string. .SS "HTTPListeners" An HTTP listener defines an address and port that .B Pound will listen on for HTTP requests. The following directives are available: .TP \fBAddress\fR: address The address that .B Pound will listen on. This can be a numeric IP address, or a symbolic host name that must be resolvable at run-time. This is a .B mandatory parameter. The address 0.0.0.0 may be used as an alias for 'all available addresses on this machine', but this practice is strongly discouraged. .TP \fBPort\fR: port The port number that .B Pound will listen on. This is a .B mandatory parameter. .TP \fBClient\fR: value Define how long .B Pound will wait for client activity. Default: 5 seconds. .TP \fBThreads\fR: value Define how many threads .B Pound will use to service client requests. Default: 8 threads. .TP \fBServices\fR: This defines a service. This service will be used only by this listener. .SS "Services" The following directives are allowed in a service definition: .TP \fBURL\fR: pattern The service will only be used if the request URL matches the given pattern. .TP \fBHeadRequire\fR: pattern Use the service only if .I any of the request headers matches the given pattern. .TP \fBHeadDeny\fR: pattern Use the service only if .I none of the request headers matches the given pattern. .TP \fBSession\fR: number How long to keep the client sessions (in seconds). Sessions are a long term association between a client IP address and a specific backend in this service. A value of 0 seconds means no sessions are kept. Default: 0. .TP \fBBackEnds\fR: A list of references to previously defined backends. .SS "HTTPSListeners" All .I HTTPListeners directives are also available in the .I HTTPSListener blocks. The following additional directives are available: .TP \fBCertificates\fR: A file name or a list of file names. Each file must contain a certificate, optionally additional chained certificates up to a known certificate authority, and the private key corresponding to the certificate. \fINote\fR: the private key should probably not be password-protected, as .B Pound normally starts as a daemon and cannot ask for the password at start-up time. .TP \fBCiphers\fR: A list of acceptable cipher names for this listener. The negotiation with the client will result in one of these ciphers being used, or the hand-shake will fail. .SH "ADDITIONAL REMARKS" .SS "High-availability" .B Pound attempts to keep track of active back-end servers, and will temporarily disable servers that do not respond (though not necessarily dead: an overloaded server that .B Pound cannot establish a connection to will be considered dead). However, every 60 seconds (compile-time option), an attempt is made to connect to the dead servers in case they have become active again. If this attempt succeeds, connections will be initiated to them again. .PP The clients that happen upon a dead backend server will just receive a .I "503 Service Unavailable" message. .SH Security .PP In general, .B Pound does not read or write to the hard-disk. The exceptions are reading the configuration file and (possibly) the server certificate file(s) and error message(s), which are opened read-only on startup, read, and closed; secondly the pid file which is opened on start-up, written to and immediately closed. Following this there is no disk access whatsoever, so using a RootJail directive is only for extra security bonus points. .PP .B Pound tries to sanitise all HTTP/HTTPS requests: the request itself, the headers and the contents are checked for conformance to the RFC's and only valid requests are passed to the back-end servers. This is not absolutely fool-proof - as the recent Apache problem with chunked transfers demonstrated. However, given the current standards, this is the best that can be done - HTTP is an inherently weak protocol. .SS "Additional Notes" .B Pound uses the system log for messages (default facility LOG_DAEMON - compile-time option). The format is very similar to other web servers, so if you want to use a log tool: .RS .EX fgrep pound /var/log/messages | cut -d ':' -f 4- | your_log_tool .EE .RE (assuming .I messages is you log file; it may be .I syslog or something else, depending on your configuration). .PP .B Pound deals with (and sanitizes) HTTP/1.1 requests. Thus a single connection to an HTTP/1.1 client is kept, while the connection to the back-end server is (re)opened as necessary. .PP Unless you start .B Pound as root it won't be able to listen on privileged ports. That applies even if you do start it as root but set the .I User to something else. .PP There is no point in setting .I User to root: either you start as root, so you already are, or you are not allowed to setuid(0). .SS "Performance Tuning Considerations" The two important factors in tuning the performance are the number of threads for the backends end the number of threads for the listeners. .PP The number of backend threads defines how many requests may be issued in parallel to a specific backend server, but also backend priorities. Increasing it may overload the web server, but setting it too low will cause longer wating ques for servicing requests. Please note that you may define several backends for the same server in order to use them in separate services. .PP The number of listener threads defines how many client requests can be serviced in parallel. If this number is too low for your load clients may be faced with long waiting times even when the backends are almost idle. .SH "EXAMPLES" The simplest configuration, with .I Pound used strictly to sanitise requests: .RS .EX Backends: - &be Address: 10.1.1.100 Port: 80 HTTPListeners: - Address: 123.1.2.3 Port: 80 Services: - Backends: - *be HTTPSListeners: .EE .RE .PP The same thing, but with HTTPS: .RS .EX Backends: - &be Address: 10.1.1.100 Port: 80 HTTPListeners: HTTPSListeners: - Address: 123.1.2.3 Port: 443 Services: - Backends: - *be Certificates: "cert.pem" Client: 60 Ciphers: - TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384 - TLS-DHE-RSA-WITH-3DES-EDE-CBC-SHA - TLS-DHE-RSA-WITH-AES-128-CBC-SHA - TLS-RSA-WITH-CAMELLIA-128-CBC-SHA - TLS-RSA-WITH-AES-128-CCM - TLS-RSA-WITH-AES-256-GCM-SHA384 - TLS-RSA-WITH-RC4-128-MD5 - TLS-RSA-WITH-3DES-EDE-CBC-SHA .EE .RE .PP To distribute the HTTP/HTTPS requests to three Web servers, where the third one is a newer and faster machine: .RS .EX Backends: - &be0 Address: 10.1.1.100 Port: 80 Threads: 8 - &be1 Address: 10.1.1.101 Port: 80 Threads: 8 - &be2 Address: 10.1.1.102 Port: 80 Threads: 12 HTTPListeners: HTTPSListeners: - Address: 123.1.2.3 Port: 80 Threads: 32 Services: - Backends: - *be0 - *be1 - *be2 Certificates: - "cert1.pem" - "cert2.pem" .EE .RE .PP To separate between image requests and other Web content: .RS .EX Backends: - &text Address: 10.1.1.100 Port: 80 Threads: 16 - &images Address: 10.1.1.101 Port: 80 Threads: 16 HTTPListeners: - Address: 123.1.2.3 Port: 80 Threads: 32 Services: - URL: ".*\.(gif|jpg|png)" Backends: - *images - Session: 300 Backends: - *text HTTPSListeners: .EE .RE .SH FILES .TP \fI/var/run/pound.pid\fR this is where .B Pound will attempt to record its process id. .TP \fI/etc/pound/pound.yaml\fR the default configuration file (compile-time option). .SH "AUTHOR" Written by Robert Segall, Apsis GmbH. .SH "REPORTING BUGS" Report bugs to . .SH "COPYRIGHT" Copyright \(co 2002-2020 Apsis GmbH. .br This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.Pound-3.0.2/GPL.txt0000755000175000017500000010451314150724211013010 0ustar rosegroseg GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 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 3 of the License, 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. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read .