pconn-banger.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 
11 #if HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif
14 #if HAVE_FCNTL_H
15 #include <fcntl.h>
16 #endif
17 #if HAVE_STRING_H
18 #include <string.h>
19 #endif
20 #if HAVE_STRINGS_H
21 #include <strings.h>
22 #endif
23 #if HAVE_BSTRING_H
24 #include <bstring.h>
25 #endif
26 #if HAVE_SIGNAL_H
27 #include <signal.h>
28 #endif
29 #if HAVE_TIME_H
30 #include <time.h>
31 #endif
32 #if HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
34 #endif
35 #if HAVE_NETINET_IN_H
36 #include <netinet/in.h>
37 #endif
38 #if HAVE_ARPA_INET_H
39 #include <arpa/inet.h>
40 #endif
41 #if HAVE_ERRNO_H
42 #include <errno.h>
43 #endif
44 #if HAVE_CTYPE_H
45 #include <ctype.h>
46 #endif
47 #if HAVE_ASSERT_H
48 #include <assert.h>
49 #endif
50 #if HAVE_SYS_STAT_H
51 #include <sys/stat.h>
52 #endif
53 
54 #define PROXY_PORT "3128"
55 #define PROXY_ADDR "127.0.0.1"
56 #define MAX_FDS 1024
57 #define READ_BUF_SZ 4096
58 #define min(x,y) ((x)<(y)? (x) : (y))
59 
60 static int proxy_port = PROXY_PORT;
61 static char *proxy_addr = PROXY_ADDR;
62 static char *progname;
63 static int noutstanding = 0;
64 static int done_reading_urls = 0;
65 static int opt_ims = 0;
66 static int opt_checksum = 0;
67 static int opt_reopen = 1;
68 static int max_outstanding = 10;
69 static time_t lifetime = 60;
70 static const char *const crlf = "\r\n";
71 static int trace_fd = -1;
72 static int total_bytes_read = 0;
73 
74 #define REPLY_HDR_SZ 8192
75 
76 struct _r {
77  char url[1024];
83  struct _r *next;
84  long sum;
85  long validsize;
86  long validsum;
87 };
88 
89 static struct _r *Requests;
90 
91 char *
93 time_t *t;
94 {
95  static char buf[128];
96  struct tm *gmt = gmtime(t);
97  buf[0] = '\0';
98  (void) strftime(buf, 127, "%A, %d-%b-%y %H:%M:%S GMT", gmt);
99  return buf;
100 }
101 
102 char *
103 mime_headers_end(const char *mime)
104 {
105  const char *p1, *p2;
106  const char *end = NULL;
107 
108  p1 = strstr(mime, "\n\r\n");
109  p2 = strstr(mime, "\n\n");
110 
111  if (p1 && p2)
112  end = p1 < p2 ? p1 : p2;
113  else
114  end = p1 ? p1 : p2;
115  if (end)
116  end += (end == p1 ? 3 : 2);
117 
118  return (char *) end;
119 }
120 
121 void
122 sig_intr(int sig)
123 {
124  fprintf(stderr, "\rWaiting for open connections to finish...\n");
125  signal(sig, SIG_DFL);
126  done_reading_urls = 1;
127 }
128 
129 int
131 {
132  int s;
133  struct addrinfo *AI = NULL;
134  struct addrinfo hints;
135 
136  memset(&hints, '\0', sizeof(struct addrinfo));
138  hints.ai_family = AF_UNSPEC;
139  hints.ai_socktype = SOCK_STREAM;
140 
141  getaddrinfo(proxy_addr, proxy_port, &hints, AI);
142 
143  if ((s = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol)) < 0) {
144  perror("socket");
145  s = -1;
146  } else if (connect(s, AI->ai_addr, AI->ai_addrlen) < 0) {
147  close(s);
148  perror("connect");
149  s = -1;
150  }
151 
152  freeaddrinfo(AI);
153  return s;
154 }
155 
156 int
157 send_request(int fd, const char *data)
158 {
159  char msg[4096], buf[4096];
160  int len;
161  time_t w;
162  struct _r *r;
163  struct _r **R;
164  char *method, *url, *file, *size, *checksum;
165  char *tmp = xstrdup(data);
166  struct stat st;
167  int file_fd = -1;
168  method = strtok(tmp, " ");
169  url = strtok(NULL, " ");
170  file = strtok(NULL, " ");
171  size = strtok(NULL, " ");
172  checksum = strtok(NULL, " ");
173  if (!url) {
174  url = method;
175  method = "GET";
176  }
177  if (file && strcmp(file, "-") == 0)
178  file = NULL;
179  if (size && strcmp(size, "-") == 0)
180  size = NULL;
181  if (checksum && strcmp(checksum, "-") == 0)
182  checksum = NULL;
183  msg[0] = '\0';
184  snprintf(buf, sizeof(buf)-1, "%s %s HTTP/1.0\r\n", method, url);
185  strcat(msg, buf);
186  strcat(msg, "Accept: */*\r\n");
187  strcat(msg, "Proxy-Connection: Keep-Alive\r\n");
188  if (opt_ims && (lrand48() & 0x03) == 0) {
189  w = time(NULL) - (lrand48() & 0x3FFFF);
190  snprintf(buf, sizeof(buf)-1, "If-Modified-Since: %s\r\n", mkrfc850(&w));
191  strcat(msg, buf);
192  }
193  if (file) {
194  if ((file_fd = open(file, O_RDONLY)) < 0) {
195  perror("open");
196  return -1;
197  }
198  if (fstat(file_fd, &st)) {
199  perror("fstat");
200  close(file_fd);
201  return -1;
202  }
203  snprintf(buf, sizeof(buf)-1, "Content-length: %d\r\n", st.st_size);
204  strcat(msg, buf);
205  }
206  strcat(msg, "\r\n");
207  len = strlen(msg);
208  if (write(fd, msg, len) < 0) {
209  close(fd);
210  perror("request write");
211  close(file_fd);
212  return -1;
213  }
214  if (file) {
215  while ((len = read(file_fd, buf, sizeof buf)) > 0) {
216  if (write(fd, buf, len) < 0) {
217  close(fd);
218  perror("body write");
219  close(file_fd);
220  return -1;
221  }
222  }
223  if (len < 0) {
224  perror("file read");
225  close(file_fd);
226  return -1;
227  }
228  close(file_fd);
229  }
230  r = calloc(1, sizeof(struct _r));
231  strcpy(r->url, url);
232  if (size)
233  r->validsize = atoi(size);
234  else
235  r->validsize = -1;
236  if (checksum)
237  r->validsum = atoi(checksum);
238  for (R = &Requests; *R; R = &(*R)->next);
239  *R = r;
240  /* fprintf(stderr, "REQUESTED %s\n", url); */
241  noutstanding++;
242  return 0;
243 }
244 
245 static int
246 get_header_int_value(const char *hdr, const char *buf, const char *end)
247 {
248  const char *t;
249  for (t = buf; t < end; t += strcspn(t, crlf), t += strspn(t, crlf)) {
250  if (strncasecmp(t, hdr, strlen(hdr)) == 0) {
251  t += strlen(hdr);
252  while (xisspace(*t))
253  t++;
254  return atoi(t);
255  }
256  }
257  return -1;
258 }
259 
260 static const char *
261 get_header_string_value(const char *hdr, const char *buf, const char *end)
262 {
263  const char *t;
264  static char result[8192];
265  for (t = buf; t < end; t += strcspn(t, crlf), t += strspn(t, crlf)) {
266  if (strncasecmp(t, hdr, strlen(hdr)) == 0) {
267  t += strlen(hdr);
268  while (xisspace(*t))
269  t++;
270  strcpy(result, "");
271  strncat(result, t, strcspn(t, crlf));
272  return result;
273  }
274  }
275  return NULL;
276 }
277 
278 void
279 request_done(struct _r *r)
280 {
281  if (r->content_length != r->bytes_read)
282  fprintf(stderr, "ERROR! Short reply, expected %d bytes got %d\n",
283  r->content_length, r->bytes_read);
284  else if (r->validsize >= 0) {
285  if (r->validsize != r->bytes_read)
286  fprintf(stderr, "WARNING: %s Object size mismatch, expected %d got %d\n",
287  r->url, r->validsize, r->bytes_read);
288  else if (opt_checksum && r->sum != r->validsum)
289  fprintf(stderr, "WARNING: %s Checksum error. Expected %d got %d\n",
290  r->url, r->validsum, r->sum);
291  }
292 }
293 int
294 handle_read(char *inbuf, int len)
295 {
296  struct _r *r = Requests;
297  const char *end;
298  const char *url;
299  static char buf[READ_BUF_SZ];
300  int hlen, blen;
301  if (len < 0) {
302  perror("read");
303  Requests = r->next;
304  request_done(r);
305  free(r);
306  noutstanding--;
307  if (trace_fd >= 0)
308  write(trace_fd, "\n[CLOSED]\n", 10);
309  return -1;
310  }
311  total_bytes_read += len;
312  memcpy(buf, inbuf, len);
313  if (len == 0) {
314  fprintf(stderr, "WARNING: %s, server closed socket after %d+%d bytes\n", r->url, r->hdr_offset, r->bytes_read);
315  /* XXX, If no data was received and it isn't the first request on this
316  * connection then the request should be restarted rather than aborted
317  * but this is a simple test program an not a full blown HTTP client.
318  */
319  request_done(r);
320  Requests = r->next;
321  free(r);
322  noutstanding--;
323  return -1;
324  }
325  if (trace_fd > 0)
326  write(trace_fd, buf, len);
327  while (len > 0) {
328  /* Build headers */
329  if (r->hdr_length == 0) {
330  hlen = min(len, REPLY_HDR_SZ - r->hdr_offset - 1);
331  memcpy(r->reply_hdrs + r->hdr_offset, buf, hlen);
332  r->hdr_offset += hlen;
333  r->reply_hdrs[r->hdr_offset] = '\0';
334  len -= hlen;
335  /* Save any remaining read data */
336  memmove(buf, buf + hlen, len);
337  }
338  /* Process headers */
339  if (r->hdr_length == 0 && (end = mime_headers_end(r->reply_hdrs)) != NULL) {
340  r->hdr_length = end - r->reply_hdrs;
341  /* "unread" any body contents received */
342  blen = r->hdr_offset - r->hdr_length;
343  assert(blen >= 0);
344  if (blen > 0) {
345  memmove(buf + blen, buf, len);
346  memcpy(buf, r->reply_hdrs + r->hdr_length, blen);
347  len += blen;
348  }
349  r->reply_hdrs[r->hdr_length] = '\0'; /* Null terminate headers */
350  /* Parse headers */
351  r->content_length = get_header_int_value("content-length:", r->reply_hdrs, end);
352  /* fprintf(stderr, "CONTENT_LENGTH = %d\n", r->content_length); */
353  url = get_header_string_value("X-Request-URI:", r->reply_hdrs, end);
354  if (url != NULL && strcmp(r->url, url) != 0)
355  fprintf(stderr, "WARNING: %s got reply %s\n", r->url, url);
356 #if XREQUESTURI || 0
357  fprintf(stderr, "LOCATION = %s\n", get_header_string_value("X-Request-URI:", r->reply_hdrs, end));
358 #endif
359  }
360  if (!(len == 0 || r->hdr_length > 0)) {
361  fprintf(stderr, "ERROR!!!\n");
362  assert((len == 0 || r->hdr_length > 0));
363  }
364  /* Process body */
365  if (r->hdr_length != 0) {
366  int i;
367  int bytes_left, bytes_used;
368  if (r->content_length >= 0) {
369  bytes_left = r->content_length - r->bytes_read;
370  assert(bytes_left >= 0);
371  bytes_used = len < bytes_left ? len : bytes_left;
372  } else {
373  bytes_left = len + 1; /* Unknown end... */
374  bytes_used = len;
375  }
376  if (opt_checksum) {
377  for (i = 0; i < bytes_used; i++)
378  r->sum += (int) buf[i] & 0xFF;
379  }
380  r->bytes_read += bytes_used;
381  len -= bytes_used;
382  if (bytes_left == bytes_used) {
383  request_done(r);
384  Requests = r->next;
385  free(r);
386  noutstanding--;
387  r = Requests;
388  } else if (r->content_length > -1) {
390  }
391  memmove(buf, buf + bytes_used, len);
392  }
393  }
394  return 0;
395 }
396 
397 int
398 read_reply(int fd)
399 {
400  static char buf[READ_BUF_SZ];
401  int len;
402  int x;
403  len = read(fd, buf, READ_BUF_SZ);
404  x = handle_read(buf, len);
405  if (x < 0) {
406  perror("read reply");
407  close(fd);
408  }
409  return x;
410 }
411 
412 void
414 {
415  static int pconn_fd = -1;
416  static char buf[8192];
417  struct timeval to;
418  struct timeval now, last, start;
419  fd_set R;
420  struct _r *r;
421  struct _r *nextr;
422  int x;
423  int timeouts;
424  int nrequests = 0, rrequests = 0, reqpersec = 0;
425 
426  gettimeofday(&start, NULL);
427  last = start;
428 
429  pconn_fd = open_http_socket();
430  if (pconn_fd < 0) {
431  perror("socket");
432  exit(1);
433  }
434  while (!done_reading_urls || noutstanding) {
435  if (!opt_reopen && pconn_fd < 0) {
436  fprintf(stderr, "TERMINATED: Connection closed\n");
437  break;
438  }
439  if (pconn_fd < 0) {
440  pconn_fd = open_http_socket();
441  if (pconn_fd < 0) {
442  perror("socket");
443  exit(1);
444  }
445  nextr = Requests;
446  Requests = NULL;
447  noutstanding = 0;
448  while ((r = nextr) != NULL) {
449  nextr = r->next;
450  if (send_request(pconn_fd, r->url) != 0) {
451  close(pconn_fd);
452  pconn_fd = -1;
453  nextr = r;
454  for (r = Requests; r != NULL && r->next; r = r->next);
455  if (r != NULL)
456  r->next = nextr;
457  else
458  Requests = nextr;
459  break;
460  }
461  free(r);
462  }
463  timeouts = 0;
464  if (pconn_fd < 0)
465  continue;
466  }
467  if (timeouts == 200) {
468  close(pconn_fd);
469  pconn_fd = -1;
470  r = Requests;
472  fprintf(stderr, "ABORT %s\n", Requests->url);
473  free(r);
474  noutstanding--;
475  }
476  if (pconn_fd >= 0 && noutstanding < max_outstanding && !done_reading_urls) {
477  char *t;
478  if (fgets(buf, 8191, stdin) == NULL) {
479  fprintf(stderr, "Done Reading URLS\n");
480  done_reading_urls = 1;
481  continue;
482  }
483  rrequests++;
484  if ((t = strchr(buf, '\n')))
485  *t = '\0';
486  if (send_request(pconn_fd, buf) != 0) {
487  close(pconn_fd);
488  pconn_fd = -1;
489  continue;
490  }
491  nrequests++;
492  reqpersec++;
493  timeouts = 0;
494  }
495  FD_ZERO(&R);
496  to.tv_sec = 1;
497  to.tv_usec = 0;
498  FD_SET(pconn_fd, &R);
499  x = select(pconn_fd + 1, &R, NULL, NULL, &to);
500  if (x < 0) {
501  if (errno != EINTR)
502  perror("select");
503  continue;
504  } else if (x == 0) {
505  assert(Requests != NULL);
506  fprintf(stderr, "TIMEOUT %s; %d, %d\n", Requests->url,
508  continue;
509  }
510  if (FD_ISSET(pconn_fd, &R)) {
511  timeouts = 0;
512  if (read_reply(pconn_fd) != 0)
513  pconn_fd = -1;
514  }
515  gettimeofday(&now, NULL);
516  if (now.tv_sec > last.tv_sec) {
517  int dt;
518  int nreq;
519  last = now;
520  dt = (int) (now.tv_sec - start.tv_sec);
521  nreq = 0;
522  for (r = Requests; r; r = r->next)
523  nreq++;
524  printf("T+ %6d: %9d req (%+4d), %4d pend, %3d/sec avg, %dmb, %dkb/sec avg\n",
525  dt,
526  nrequests,
527  reqpersec,
528  nreq,
529  (int) (nrequests / dt),
530  (int) total_bytes_read / 1024 / 1024,
531  (int) total_bytes_read / 1024 / dt);
532  reqpersec = 0;
533  }
534  }
535 }
536 
537 void
538 usage(void)
539 {
540  fprintf(stderr, "usage: %s: -p port -h host -n max -t tracefile -i -c -l lifetime\n", progname);
541 }
542 
543 int
544 main(argc, argv)
545 int argc;
546 char *argv[];
547 {
548  int c;
549  setbuf(stdout, NULL);
550  setbuf(stderr, NULL);
551  progname = xstrdup(argv[0]);
552  while ((c = getopt(argc, argv, "p:h:n:t:icl:r")) != -1) {
553  switch (c) {
554  case 'p':
555  proxy_port = atoi(optarg);
556  break;
557  case 'h':
559  break;
560  case 'n':
561  max_outstanding = atoi(optarg);
562  break;
563  case 'i':
564  opt_ims = 1;
565  break;
566  case 'c':
567  opt_checksum = 1;
568  break;
569  case 'l':
570  lifetime = (time_t) atoi(optarg);
571  break;
572  case 't':
573  trace_fd = open(optarg, O_WRONLY | O_CREAT | O_TRUNC, 0666);
574  break;
575  case 'r':
577  break;
578  default:
579  usage();
580  return 1;
581  }
582  }
583  signal(SIGINT, sig_intr);
584  signal(SIGPIPE, SIG_IGN);
585  main_loop();
586  return 0;
587 }
588 
static int trace_fd
Definition: pconn-banger.c:71
static int reqpersec
Definition: tcp-banger2.c:89
static int done_reading_urls
Definition: pconn-banger.c:64
static int opt_ims
Definition: pconn-banger.c:65
static char * progname
Definition: pconn-banger.c:62
static int opt_checksum
Definition: pconn-banger.c:66
int getaddrinfo()
char * mkrfc850(time_t *t)
Definition: pconn-banger.c:92
static struct timeval now
Definition: tcp-banger2.c:97
#define xstrdup
char * optarg
Definition: getopt.c:51
static int max_outstanding
Definition: pconn-banger.c:68
#define PROXY_ADDR
Definition: pconn-banger.c:55
static char last
Definition: parse.c:451
static const char * get_header_string_value(const char *hdr, const char *buf, const char *end)
Definition: pconn-banger.c:261
socklen_t ai_addrlen
int main(int argc, argv)
Definition: pconn-banger.c:544
int hdr_offset
Definition: pconn-banger.c:80
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:62
int hdr_length
Definition: pconn-banger.c:79
static int noutstanding
Definition: pconn-banger.c:63
struct sockaddr * ai_addr
#define READ_BUF_SZ
Definition: pconn-banger.c:57
struct _r * next
Definition: pconn-banger.c:83
int size
Definition: ModDevPoll.cc:75
long validsize
Definition: pconn-banger.c:85
#define NULL
Definition: types.h:166
int content_length
Definition: pconn-banger.c:78
#define PROXY_PORT
Definition: pconn-banger.c:54
void sig_intr(int sig)
Definition: pconn-banger.c:122
int send_request(int fd, const char *data)
Definition: pconn-banger.c:157
int handle_read(char *inbuf, int len)
Definition: pconn-banger.c:294
void usage(void)
Definition: pconn-banger.c:538
static int total_bytes_read
Definition: pconn-banger.c:72
static int nrequests
Definition: tcp-banger2.c:90
#define assert(EX)
Definition: assert.h:19
static char * proxy_addr
Definition: pconn-banger.c:61
long sum
Definition: pconn-banger.c:84
long validsum
Definition: pconn-banger.c:86
char * mime_headers_end(const char *mime)
Definition: pconn-banger.c:103
static const char *const crlf
Definition: pconn-banger.c:70
#define min(x, y)
Definition: pconn-banger.c:58
static int proxy_port
Definition: pconn-banger.c:60
int read_reply(int fd)
Definition: pconn-banger.c:398
void request_done(struct _r *r)
Definition: pconn-banger.c:279
char url[1024]
Definition: pconn-banger.c:77
int open_http_socket(void)
Definition: pconn-banger.c:130
char reply_hdrs[REPLY_HDR_SZ]
Definition: pconn-banger.c:82
void main_loop(void)
Definition: pconn-banger.c:413
static int get_header_int_value(const char *hdr, const char *buf, const char *end)
Definition: pconn-banger.c:246
#define AI_NUMERICSERV
int timeouts
Definition: peer_select.cc:66
static time_t lifetime
Definition: pconn-banger.c:69
int bytes_read
Definition: pconn-banger.c:81
void freeaddrinfo()
#define xisspace(x)
Definition: xis.h:17
static struct _r * Requests
Definition: pconn-banger.c:89
static int opt_reopen
Definition: pconn-banger.c:67
#define AI_NUMERICHOST
int unsigned int
Definition: stub_fd.cc:19
#define REPLY_HDR_SZ
Definition: pconn-banger.c:74

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors