summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorcashmere <cashmere@autistici.org>2026-06-25 16:00:08 +0200
committercashmere <cashmere@autistici.org>2026-06-25 16:00:08 +0200
commitd5da8a2b2a5a4fdf7aa713cee221ba717fc4746a (patch)
treed4220aa64a1c45c9105c2a24813421f27a5c2813 /src/main.c
parentd772f279e4e3823d08a64a9a910125072bfa7e6f (diff)
downloadtelnet-d5da8a2b2a5a4fdf7aa713cee221ba717fc4746a.tar.gz
src/main.c: extract new connection into seperate fn
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/main.c b/src/main.c
index 85a884e..fc2815a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -27,6 +27,16 @@ int establish_connection(struct sockaddr_in addr) {
return sfd;
}
+int handle_new_connection(int sfd, struct pollfd *fds, nfds_t *nfds) {
+ int conn = accept(sfd, NULL, NULL);
+ if (conn == -1)
+ return -1;
+ fds[*nfds].fd = conn;
+ fds[*nfds].events = POLLIN;
+ (*nfds)++;
+ return 0;
+}
+
int main() {
struct sockaddr_in addr;
addr.sin_family = AF_INET; /* IPv4 */
@@ -50,19 +60,11 @@ int main() {
perror("could not run poll");
return 1;
}
- /* if new events are detected by the socket listener */
if (fds[0].revents & POLLIN) {
- /* we accept the connection */
- int conn = accept(sfd, NULL, NULL);
- if (conn == -1) {
+ if (handle_new_connection(sfd, fds, &nfds) == -1) {
perror("could not accept connection");
- continue; /* if one client is not able to connect the server shall not
- die */
+ continue;
}
- fds[nfds].fd = conn; /* read */
- fds[nfds].events =
- POLLIN; /* check if telnet client has something to say */
- nfds++;
}
for (nfds_t i = 1; i < nfds; i++) {
/* if client has something to say */