#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/md5.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <time.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>

#define BUFSIZE	8192

char *logfile;

void getthebabe(int sock, struct sockaddr_in *sin, socklen_t addrlen) {
    FILE *logf;
    time_t now;
    int outfile;
    char tmpname[strlen(logfile) + 11], md5name[strlen(logfile) + 34];
    unsigned char inbuf[BUFSIZE];
    int len, totlen = 0;
    char md5sum[33];

    sprintf(tmpname, "%sXXXXXXXXXX", logfile);

    time(&now);

    if ((outfile = mkstemp(tmpname)) == -1) {
	perror("mkstemp()");
	exit(1);
    }

    for (;;) {
	len = recv(sock, inbuf, sizeof(inbuf), 0);
	if (len > 0) {
	    write(outfile, inbuf, len);
	    totlen += len;
	}
	if (len <= 0)
	    break;
    }

    close(outfile);

    MD5File(tmpname, md5sum);

    sprintf(md5name, "%s.%s", logfile, md5sum);

    outfile = open(md5name, O_WRONLY | O_CREAT | O_EXCL);

    if (outfile != -1) {    /* no file exists */
	rename(tmpname, md5name);
	chmod(md5name, 0644);
    } else
	unlink(tmpname);

    if (!(logf = fopen(logfile, "a"))) {
	perror("wah. can't write to logfile");
	exit(1);
    }

    fprintf(logf, "%24.24s from: %s:%i len: %i MD5: %s (%s)\n", ctime(&now), inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), totlen, md5sum, outfile == -1 ? "already known" : "new");

    if (outfile != -1)
	close(outfile);

    fclose(logf);

    close(sock);
}

void sighandler(int param) {
    int stat;
    while (wait4(-1, &stat, WNOHANG, NULL));
}

int main(int argc, char *argv[]) {
    int acceptsock, readsock;
    struct sockaddr_in addr_me, addr_he;
    socklen_t addrlen = sizeof(addr_he);

    signal(SIGCHLD, sighandler);
    signal(SIGHUP, sighandler);

    if (argc != 2) {
	fprintf(stderr, "usage: %s <dest>\n", argv[0]);
	exit(1);
    }

    logfile = argv[1];

    if ((readsock = open(logfile, O_WRONLY | O_APPEND | O_CREAT, 0644)) == -1) {
	perror("open()");
	exit(1);
    }

    close(readsock);

    if ((acceptsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
	perror("socket()");
	exit(1);
    }

    bzero(&addr_me, sizeof(addr_me));
    addr_me.sin_port = htons(80);
    addr_me.sin_family = AF_INET;
    addr_me.sin_addr.s_addr = 0;
    addr_me.sin_len = sizeof(addr_me);

    if (bind(acceptsock, (struct sockaddr *)&addr_me, sizeof(addr_me))) {
	perror("bind()");
	exit(1);
    }

    if (listen(acceptsock, 5)) {
	perror("listen()");
	exit(1);
    }

    for (;;) {
	readsock = accept(acceptsock, (struct sockaddr *)&addr_he, &addrlen);
	if (readsock == -1 && errno == ECONNABORTED)
	    continue;
	if (readsock == -1) {
	    perror("accept()");
	    exit(1);
	}
	if (!fork()) {
	    close(acceptsock);
	    getthebabe(readsock, &addr_he, addrlen);
	    exit(0);
	} else
	    close(readsock);
    }

    return 0;
}