1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/event.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>

static int maxfiles = 65536;

int
main(int argc, char *argv[])
{
    struct sockaddr_in sa_local, sa_remote;
    socklen_t sin_size = sizeof(struct sockaddr_in);
    socklen_t optval, optlen = sizeof(socklen_t);
    struct timespec timeout;
    int fd_listen, i;
    int readme_fd;

    if ((fd_listen = socket(PF_INET, SOCK_STREAM, 0)) == -1)
        err(EX_OSERR, "socket(2) failure");

    optval = 1;
    if (setsockopt(fd_listen, SOL_SOCKET, SO_REUSEADDR, (void *)&optval, optlen) == -1)
        err(EX_OSERR, "setsockopt(2) failure");

    /* Make socket non-blocking */
    if ((i = fcntl(fd_listen, F_GETFL)) == -1)
        i = 0;
    if (fcntl(fd_listen, F_SETFL, i | O_NONBLOCK) == -1)
        err(EX_OSERR, "fcntl(2) failure");

    sa_local.sin_family = AF_INET;
    sa_local.sin_port = htons(8080);
    sa_local.sin_addr.s_addr = htonl(INADDR_ANY);
    memset(&(sa_local.sin_zero), 0, sizeof(sa_local.sin_zero));

    if (bind(fd_listen, (struct sockaddr *)&sa_local, sizeof(struct sockaddr)) == -1)
        err(EX_OSERR, "bind(2) failure");

    if ((i = listen(fd_listen, -1)) < 128)
        i = 128;
    if (listen(fd_listen, i) == -1)
        err(EX_OSERR, "listen(2) failure");

    readme_fd = open("/usr/src/README", O_RDONLY);
    if (readme_fd == -1)
        err(EX_OSERR, "open(2) failure");

for (i = 0; i < 4; ++i) {
if (fork() == 0) {
    do {
        struct kevent changes[maxfiles], events[maxfiles];
        struct sockaddr_in sa_remote;
        socklen_t remote_size = sizeof(sa_remote);
        int kq, nchanges, nevents;
        int conns[maxfiles];

        if ((kq = kqueue()) == -1)
            err(EX_OSERR, "kqueue(2) failure");

//        EV_SET(&changes[0], fd_listen, EVFILT_READ, EV_ADD, 0, 0, 0);
        EV_SET(&changes[0], fd_listen, EVFILT_READ, EV_ADD|EV_WAKEONE, 0, 0, 0);
        nchanges = 1;

        while (1) {
            nevents = kevent(kq, (struct kevent *)&changes, nchanges,
                             (struct kevent *)&events, maxfiles, NULL);
            nchanges = 0;

            for (i = 0; i < nevents; ++i) {
                int fd = events[i].ident;

                /* New connection */
                if (fd == fd_listen) {
                    int fd_new = accept(fd_listen, (struct sockaddr *)&sa_remote, &remote_size);
// printf("new connection: %d\n", fd_new);
                    if (fd_new == -1) {
                        warn("could not accept(2) new connection\n");
                    } else {
                        EV_SET(&changes[nchanges], fd_new, EVFILT_READ, EV_ADD, 0, 0, 0);
                        ++nchanges;
                        conns[fd_new] = 0;
                    }

                /* Existing connection */
                } else {
                    /* Already completed */
                    if (conns[fd] == -1) continue;

                    if (events[i].filter == EVFILT_READ) {
                        char rbuf[8192];
                        ssize_t rb = recv(fd, &rbuf, 8192, 0);
// printf("sucked down %d\n", rb);
                        /*
                         * Just blatantly assume after pulling some data
                         * off the socket that we can respond, h0h0
                         */

                        EV_SET(&changes[nchanges], fd, EVFILT_READ, EV_DELETE, 0, 0, 0);
                        ++nchanges;
                        EV_SET(&changes[nchanges], fd, EVFILT_WRITE, EV_ADD, 0, 0, 0);
                        ++nchanges;
                        goto write_ready;
                    } else {
                        /* EVFILT_WRITE */
                        off_t sbytes;
write_ready:

// printf("doing sendfile!\n");
                        sendfile(readme_fd, fd, conns[fd], 2559-conns[fd], NULL, &sbytes, 0);
                        conns[fd] += sbytes;

// printf("conns[fd] for fd %d is %d\n", fd, conns[fd]);
                        if (conns[fd] >= 2559) {
                            close(fd); /* Cleans up KQ for us */
                            conns[fd] = -1;
                        }
                    }
                }
            }
        }
    } while (0);
}
}

    return (0);
}