|
| 1 | +#include "helpers.h" |
| 2 | +#include <netinet/in.h> |
| 3 | +#include <arpa/inet.h> |
| 4 | +#include <netdb.h> |
| 5 | +#include <pthread.h> |
| 6 | + |
| 7 | +// Connection structure |
| 8 | +#define CONN_BUFSIZ 1024 |
| 9 | +typedef struct conn { |
| 10 | + FILE* f; |
| 11 | + char buf[CONN_BUFSIZ]; |
| 12 | + struct conn* next; |
| 13 | + struct conn* prev; |
| 14 | +} conn; |
| 15 | +#define MAX_CONNS 10240 |
| 16 | +conn* conns = NULL; |
| 17 | + |
| 18 | +int make_listen(int port); |
| 19 | +void handle_connection(conn* c); |
| 20 | +void close_connection(conn* c); |
| 21 | +int remove_trailing_whitespace(char* buf); |
| 22 | + |
| 23 | +int main(int argc, char** argv) { |
| 24 | + // Usage: ./serviceserver [PORT] |
| 25 | + int port = 6168; |
| 26 | + if (argc >= 2) { |
| 27 | + port = strtol(argv[1], NULL, 0); |
| 28 | + assert(port > 0 && port <= 65535); |
| 29 | + } |
| 30 | + |
| 31 | + // Prepare listening socket |
| 32 | + int lfd = make_listen(port); |
| 33 | + make_nonblocking(lfd); |
| 34 | + |
| 35 | + // Prepare connection array |
| 36 | + raise_file_limit(); |
| 37 | + |
| 38 | + while (1) { |
| 39 | + // Prepare set of relevant connections |
| 40 | + fd_set rfds; |
| 41 | + FD_ZERO(&rfds); |
| 42 | + FD_SET(lfd, &rfds); // New connection attempts |
| 43 | + int max_fd = lfd; |
| 44 | + for (conn* c = conns; c; c = c->next) { |
| 45 | + int fd = fileno(c->f); |
| 46 | + FD_SET(fd, &rfds); |
| 47 | + max_fd = (fd > max_fd ? fd : max_fd); |
| 48 | + } |
| 49 | + |
| 50 | + // Select to block until we're ready |
| 51 | + (void) select(max_fd + 1, &rfds, NULL, NULL, NULL); |
| 52 | + |
| 53 | + // Maybe accept an incoming connection |
| 54 | + if (FD_ISSET(lfd, &rfds)) { |
| 55 | + int cfd = accept(lfd, NULL, NULL); |
| 56 | + if (cfd < 0 && errno == EMFILE) { |
| 57 | + for (int i = 0; i < 10 && conns; ++i) |
| 58 | + close_connection(conns); |
| 59 | + continue; |
| 60 | + } else if (cfd < 0) { |
| 61 | + perror("accept"); |
| 62 | + exit(1); |
| 63 | + } |
| 64 | + make_nonblocking(cfd); |
| 65 | + |
| 66 | + conn* c = (conn*) malloc(sizeof(conn)); |
| 67 | + c->f = fdopen(cfd, "a+"); |
| 68 | + c->buf[0] = 0; // empty string |
| 69 | + c->next = conns; |
| 70 | + if (conns) |
| 71 | + conns->prev = c; |
| 72 | + c->prev = NULL; |
| 73 | + conns = c; |
| 74 | + } |
| 75 | + |
| 76 | + // Handle data from existing connections |
| 77 | + for (conn* c = conns; c; ) { |
| 78 | + conn* next = c->next; |
| 79 | + if (FD_ISSET(fileno(c->f), &rfds)) |
| 80 | + handle_connection(c); /* might close `c` */ |
| 81 | + c = next; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void handle_connection(conn* c) { |
| 87 | + size_t len = strlen(c->buf); |
| 88 | + |
| 89 | + // Attempt to load more data into the buffer. |
| 90 | + // If you fail, maybe try again later |
| 91 | + int ch = EOF; |
| 92 | + while (len != CONN_BUFSIZ - 1 |
| 93 | + && (ch = fgetc(c->f)) != EOF |
| 94 | + && ch != '\n') { |
| 95 | + c->buf[len] = ch; |
| 96 | + ++len; |
| 97 | + } |
| 98 | + c->buf[len] = 0; |
| 99 | + |
| 100 | + // Maybe the user ran out of data to send. Just try again later |
| 101 | + if (len != CONN_BUFSIZ - 1 && ch == EOF |
| 102 | + && ferror(c->f) && errno == EAGAIN) |
| 103 | + return; |
| 104 | + |
| 105 | + // At this point we have a line |
| 106 | + |
| 107 | + // Remove trailing whitespace |
| 108 | + if (remove_trailing_whitespace(c->buf)) { |
| 109 | + struct servent* service = getservbyname(c->buf, "tcp"); |
| 110 | + int port = service ? ntohs(service->s_port) : 0; |
| 111 | + fprintf(c->f, "%s,%d\n", c->buf, port); |
| 112 | + fflush(c->f); |
| 113 | + } |
| 114 | + |
| 115 | + c->buf[0] = 0; |
| 116 | + |
| 117 | + // If the file is done, remove the connection |
| 118 | + if (feof(c->f) || ferror(c->f)) |
| 119 | + close_connection(c); |
| 120 | +} |
| 121 | + |
| 122 | +void close_connection(conn* c) { |
| 123 | + fclose(c->f); |
| 124 | + if (c->prev) |
| 125 | + c->prev->next = c->next; |
| 126 | + else |
| 127 | + conns = c->next; |
| 128 | + if (c->next) |
| 129 | + c->next->prev = c->prev; |
| 130 | + free(c); |
| 131 | +} |
| 132 | + |
| 133 | +int make_listen(int port) { |
| 134 | + // Create socket |
| 135 | + int fd = socket(AF_INET, SOCK_STREAM, 0); |
| 136 | + assert(fd >= 0); |
| 137 | + |
| 138 | + // Useful options: close-on-exec, reuse-address |
| 139 | + int r = fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 140 | + assert(r >= 0); |
| 141 | + |
| 142 | + int yes = 1; |
| 143 | + r = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); |
| 144 | + assert(r >= 0); |
| 145 | + |
| 146 | + // Bind to port |
| 147 | + struct sockaddr_in address; |
| 148 | + memset(&address, 0, sizeof(address)); |
| 149 | + address.sin_family = AF_INET; |
| 150 | + address.sin_port = htons(port); |
| 151 | + address.sin_addr.s_addr = INADDR_ANY; |
| 152 | + r = bind(fd, (struct sockaddr*) &address, sizeof(address)); |
| 153 | + assert(r >= 0); |
| 154 | + |
| 155 | + // Actually start listening |
| 156 | + r = listen(fd, 100); |
| 157 | + assert(r >= 0); |
| 158 | + |
| 159 | + return fd; |
| 160 | +} |
| 161 | + |
| 162 | +int remove_trailing_whitespace(char* buf) { |
| 163 | + int len = strlen(buf); |
| 164 | + while (len > 0 && isspace((unsigned char) buf[len - 1])) { |
| 165 | + --len; |
| 166 | + buf[len] = 0; |
| 167 | + } |
| 168 | + return len; |
| 169 | +} |
0 commit comments