-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
yc_poll.c
172 lines (146 loc) · 5.16 KB
/
yc_poll.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* yc_poll - a yoctochat server using a classic poll() IO loop */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <errno.h>
/* number of pollfds in our array. because of the wey we're implementing this,
* that's roughly the maximum number of connections we can handle. */
#define NUM_POLLFDS (128)
int main(int argc, char **argv) {
if (argc < 2) {
printf("usage: %s <port>\n", argv[0]);
exit(1);
}
int port = atoi(argv[1]);
if (port <= 0) {
printf("'%s' not a valid port number\n", argv[1]);
exit(1);
}
/* create the server socket */
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("socket");
exit(1);
}
/* arrange for the listening address to be reusable. This makes TCP
* marginally "less safe" (for a whole bunch of obscure reasons) but allows
* us to kill and restart the program with ease */
int onoff = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &onoff, sizeof(onoff)) < 0) {
perror("setsockopt");
exit(1);
}
/* set up the address structure for binding, which is *:<port> */
struct sockaddr_in sin = {
.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr = {
.s_addr = htonl(INADDR_ANY)
}
};
/* bind the server socket to the wanted address */
if (bind(server_fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
perror("bind");
exit(1);
}
/* and open it for connections! */
if (listen(server_fd, 10) < 0) {
perror("listen");
exit(1);
}
printf("listening on port %d\n", port);
/* create an array of pollfd structs. each one carries a file descriptor, a
* set of wanted events, and after the poll() call, a set of events that
* occurred. for our own convenience we keep a static list and use the file
* descriptor as the index. */
struct pollfd pollfds[NUM_POLLFDS];
for (int fd = 0; fd < NUM_POLLFDS; fd++) {
/* setting -1 fd disables */
pollfds[fd].fd = -1;
}
/* add the server socket, and request read/input events */
pollfds[server_fd].fd = server_fd;
pollfds[server_fd].events = POLLIN;
/* wait forever for something to happen */
while (poll(pollfds, NUM_POLLFDS, -1) >= 0) {
/* if the server socket has activity, someone connected */
if (pollfds[server_fd].revents & POLLIN) {
/* create storage for their address */
struct sockaddr_in sin;
socklen_t sinlen = sizeof(sin);
/* let them in! */
int new_fd = accept(server_fd, (struct sockaddr *) &sin, &sinlen);
if (new_fd < 0) {
perror("accept");
}
else {
/* hello */
printf("[%d] connect from %s:%d\n", new_fd, inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
/* make them non-blocking. this is necessary, because a disconnect will
* cause a descriptor to become readable, but reading will block
* forever (because they're disconnected. non-blocking will cause
* read() to return 0 on a disconnected descriptor, so we can take the
* right action */
int onoff = 1;
if (ioctl(new_fd, FIONBIO, &onoff) < 0) {
printf("fcntl(%d): %s\n", new_fd, strerror(errno));
close(new_fd);
continue;
}
/* enable the pollfd for this fd, and request read events */
pollfds[new_fd].fd = new_fd;
pollfds[new_fd].events = POLLIN;
}
}
for (int fd = 0; fd < NUM_POLLFDS; fd++) {
if (!(pollfds[fd].revents & POLLIN) || fd == server_fd)
continue;
printf("[%d] activity\n", fd);
/* create a buffer to read into */
char buf[1024];
int nread = read(fd, buf, sizeof(buf));
/* see how much we read */
if (nread < 0) {
/* less then zero is some error. disconnect them */
fprintf(stderr, "read(%d): %s\n", fd, strerror(errno));
close(fd);
pollfds[fd].fd = -1;
}
else if (nread > 0) {
/* we got some stuff from them! */
printf("[%d] read: %.*s\n", fd, nread, buf);
/* loop over all our connections, and send stuff onto them! */
for (int dest_fd = 0; dest_fd < NUM_POLLFDS; dest_fd++) {
/* take active connections, but not ourselves */
if (pollfds[dest_fd].fd >= 0 && pollfds[dest_fd].fd != fd && pollfds[dest_fd].fd != server_fd) {
/* write to them */
if (write(dest_fd, buf, nread) < 0) {
/* disconnect if it fails; they might have legitimately gone away without telling us */
fprintf(stderr, "write(%d): %s\n", dest_fd, strerror(errno));
close(dest_fd);
pollfds[dest_fd].fd = -1;
}
}
}
}
/* zero byes read */
else {
/* so they gracefully disconnected and we should forget them */
printf("[%d] closed\n", fd);
close(fd);
pollfds[fd].fd = -1;
}
}
}
/* poll failed. in a real server you might actually need to handle
* non-error cases like EINTR, but it complicates this example so we won't
* bother */
perror("poll");
exit(1);
}