From: Jann Horn Date: Wed, 22 May 2013 22:48:26 +0000 (+0200) Subject: initial commit X-Git-Url: http://git.thejh.net/?p=roguegraph.git;a=commitdiff_plain;h=a77f8731ad64dd8e5f92b0a57edaa865baba0f86;ds=sidebyside initial commit --- a77f8731ad64dd8e5f92b0a57edaa865baba0f86 diff --git a/rg.c b/rg.c new file mode 100644 index 0000000..1c286fc --- /dev/null +++ b/rg.c @@ -0,0 +1,76 @@ +// Copyright (C) Jann Horn (2013) +// You can redistribute this code under the terms of the GPLv2 or GPLv3. +// THIS PROGRAM IS FOR EDUCATIONAL PURPOSES ONLY! +// I AM NOT RESPONSIBLE FOR WHAT YOU DO WITH THIS PROGRAM OR THE IMPACT THIS +// PROGRAM MIGHT HAVE ON YOUR SYSTEMS! YOU HAVE BEEN WARNED! + +#include +#include +#include +#include +#include +#include +#include +#include + +char *target; + +void run(void) { + int pipefds[2]; + if (pipe(pipefds)) perror("can't create pipe"), exit(1); + int parent_fd = pipefds[0]; + int child_fd = pipefds[1]; + + pid_t pid = fork(); + if (pid < 0) perror("can't fork"), exit(1); + + if (pid == 0) { + int nullfd = open("/dev/null", O_RDWR); + dup2(nullfd, 0); + dup2(nullfd, 2); + close(nullfd); + close(parent_fd); + dup2(child_fd, 1); + close(child_fd); + execlp("hping3", "hping3", "-c", "20", "-p", "80", "-i", "u100000", "--syn", target, NULL); + perror("can't exec"), exit(1); + } + close(child_fd); + + char indata[4096]; + int indata_written = 0; + int rres; + while ((rres=read(parent_fd, indata+indata_written, 4095-indata_written)) > 0) { + indata_written += rres; + if (indata_written >= 4095) fputs("too much information", stderr), exit(1); + } + if (rres < 0) perror("failure reading from child"), exit(1); + indata[indata_written] = '\0'; + close(parent_fd); + + char *s = indata; + int last_id = -1; + int sum = 0; + while ((s=strstr(s, " id=")) != NULL) { + s += 4; + int id = atoi(s); + if (last_id != -1) { + int diff = id - last_id; + if (diff < 0) diff += (256*256); + sum += diff; + } + last_id = id; + } + + printf("%i %i\n", (int)time(NULL), sum); +} + +int main(int argc, char **argv) { + if (argc != 2) fputs("bad invocation\n", stderr), exit(1); + target = argv[1]; + + while (1) { + run(); + sleep(300); + } +}