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
#include <assert.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include "client_thread.h"
/* Maximum number of connections to queue up */
#define LISTENQ 1024
static int open_listen_fd(int port) {
/* Create a socket descriptor */
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0) {
return -1;
}
/* Eliminates "Address already in use" error from bind. */
int value = 1;
if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)) < 0) {
return -1;
}
/* listen_fd will be an endpoint for all requests to port
on any IP address for this host */
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(port);
if (bind(listen_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
return -1;
}
/* Make it a listening socket ready to accept connection requests */
if (listen(listen_fd, LISTENQ) < 0) {
return -1;
}
return listen_fd;
}
static void cleanup(void) {
}
static void sigint_handler(int sig) {
(void) sig;
exit(0);
}
static void usage(char *program) {
printf("Usage: %s <port>\n", program);
exit(1);
}
int main(int argc, char *argv[]) {
/* Ignore broken pipes */
signal(SIGPIPE, SIG_IGN);
/* Stop process when CTRL+C is pressed */
signal(SIGINT, sigint_handler);
if (argc != 2) {
usage(argv[0]);
}
int port = atoi(argv[1]);
if (port <= 0 || port > 65535) {
usage(argv[0]);
}
/* Open listen socket */
int listen_fd = open_listen_fd(port);
if (listen_fd < 0) {
perror("Listen error");
return 1;
}
/* Register cleanup code to run at exit */
if (atexit(cleanup) != 0) {
printf("Could not register clean up function\n");
return 1;
}
printf("Proxy listening on port %d\n", port);
while (true) {
int *cfd = malloc(sizeof(int));
assert(cfd != NULL);
*cfd = accept(listen_fd, NULL, NULL);
if (*cfd == -1) {
perror("Accept error");
free(cfd);
continue;
}
handle_request(cfd);
}
}