1 /* 2 * These are helping routines for the main module of CNTLM 3 * 4 * CNTLM is free software; you can redistribute it and/or modify it under the 5 * terms of the GNU General Public License as published by the Free Software 6 * Foundation; either version 2 of the License, or (at your option) any later 7 * version. 8 * 9 * CNTLM is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 12 * details. 13 * 14 * You should have received a copy of the GNU General Public License along with 15 * this program; if not, write to the Free Software Foundation, Inc., 51 Franklin 16 * St, Fifth Floor, Boston, MA 02110-1301, USA. 17 * 18 * Copyright (c) 2007 David Kubicek 19 * 20 */ 21 22 #ifndef _UTILS_H 23 #define _UTILS_H 24 25 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 26 # include <sys/param.h> 27 #endif 28 #include <pthread.h> 29 #include <netinet/in.h> 30 31 #include "config/config.h" 32 33 #define BUFSIZE 4096 34 #define MINIBUF_SIZE 50 35 #define VAL(var, type, offset) *((type *)(var+offset)) 36 #define MEM(var, type, offset) (type *)(var+offset) 37 38 #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) 39 # define MIN(a, b) ((a) < (b) ? (a) : (b)) 40 #endif 41 42 /* 43 #define isalnum(c) (isalpha(c) || isdigit(c)) 44 #define isspace(c) ((c) == ' ' || (c) == '\f' || (c) == '\t' || (c) == '\r' || (c) == '\n') 45 */ 46 47 /* 48 * Solaris doesn't have LOG_PERROR 49 */ 50 #ifndef LOG_PERROR 51 # define LOG_PERROR LOG_CONS 52 #endif 53 54 /* 55 * Two single-linked list types. First is for storing headers, 56 * second keeps a list of finished threads or cached connections. 57 * Each has a different set of manipulation routines. 58 */ 59 typedef struct hlist_s *hlist_t; 60 struct hlist_s { 61 char *key; 62 char *value; 63 int islist; 64 struct hlist_s *next; 65 }; 66 67 typedef struct plist_s *plist_t; 68 struct plist_s { 69 unsigned long key; 70 void *aux; 71 struct plist_s *next; 72 }; 73 74 typedef enum { 75 HLIST_NOALLOC = 0, 76 HLIST_ALLOC 77 } hlist_add_t; 78 79 /* 80 * Request/response data structure. Complete and parsed req/res is 81 * kept in this. See below for (de)allocation routines. 82 */ 83 typedef struct rr_data_s *rr_data_t; 84 struct rr_data_s { 85 int req; 86 hlist_t headers; 87 int code; 88 int skip_http; 89 int body_len; 90 int empty; 91 int port; 92 char *method; 93 char *url; 94 char *rel_url; 95 char *hostname; 96 char *http; 97 char *msg; 98 char *body; 99 char *errmsg; 100 }; 101 102 /* 103 * This is used in main() for passing arguments to the thread. 104 */ 105 struct thread_arg_s { 106 int fd; 107 char *target; 108 struct sockaddr_in addr; 109 }; 110 111 extern void myexit(int rc); 112 extern void croak(const char *msg, int console); 113 114 extern plist_t plist_add(plist_t list, unsigned long key, void *aux); 115 extern plist_t plist_del(plist_t list, unsigned long key); 116 extern int plist_in(plist_t list, unsigned long key); 117 extern void plist_dump(plist_t list); 118 extern char *plist_get(plist_t list, int key); 119 extern int plist_pop(plist_t *list, void **aux); 120 extern int plist_count(plist_t list); 121 extern plist_t plist_free(plist_t list); 122 123 extern hlist_t hlist_add(hlist_t list, char *key, char *value, hlist_add_t allockey, hlist_add_t allocvalue); 124 extern hlist_t hlist_dup(hlist_t list); 125 extern hlist_t hlist_del(hlist_t list, const char *key); 126 extern hlist_t hlist_mod(hlist_t list, char *key, char *value, int add); 127 extern int hlist_in(hlist_t list, const char *key); 128 extern int hlist_count(hlist_t list); 129 extern char *hlist_get(hlist_t list, const char *key); 130 extern int hlist_subcmp(hlist_t list, const char *key, const char *substr); 131 extern int hlist_subcmp_all(hlist_t list, const char *key, const char *substr); 132 extern hlist_t hlist_free(hlist_t list); 133 extern void hlist_dump(hlist_t list); 134 135 extern char *substr(const char *src, int pos, int len); 136 extern size_t strlcpy(char *dst, const char *src, size_t siz); 137 extern size_t strlcat(char *dst, const char *src, size_t siz); 138 extern char *trimr(char *buf); 139 extern char *lowercase(char *str); 140 extern char *uppercase(char *str); 141 extern int unicode(char **dst, char *src); 142 extern char *new(size_t size); 143 extern char *urlencode(const char *str); 144 145 extern rr_data_t new_rr_data(void); 146 extern rr_data_t copy_rr_data(rr_data_t dst, rr_data_t src); 147 extern rr_data_t dup_rr_data(rr_data_t data); 148 extern rr_data_t reset_rr_data(rr_data_t data); 149 extern void free_rr_data(rr_data_t data); 150 151 extern char *printmem(char *src, size_t len, int bitwidth); 152 extern char *scanmem(char *src, int bitwidth); 153 154 extern void to_base64(unsigned char *out, const unsigned char *in, size_t len, size_t olen); 155 extern int from_base64(char *out, const char *in); 156 157 extern long int random(void); 158 #if config_gethostname == 1 159 extern int gethostname(char *name, size_t len); 160 #endif 161 #ifndef strdup 162 extern char *strdup(const char *src); 163 #endif 164 165 #endif /* _UTILS_H */ 166