summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcashmere <cashmere@autistici.org>2026-07-01 14:25:15 +0200
committercashmere <cashmere@autistici.org>2026-07-01 14:25:15 +0200
commitb3639402e86834b9e69ebb371256d4b9e67ed9f7 (patch)
tree45bf22152e0fc8e4419e97e5d1008b56e1ac0990 /src
parent054c71c8fdd91c07db0a50394a966755ab57f2f0 (diff)
downloadtelnet-b3639402e86834b9e69ebb371256d4b9e67ed9f7.tar.gz
src/main.c: include "telnet.h"
Diffstat (limited to 'src')
-rw-r--r--src/main.c87
1 files changed, 1 insertions, 86 deletions
diff --git a/src/main.c b/src/main.c
index 137d954..4fb15c5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,89 +1,4 @@
-#include <netinet/in.h>
-#include <poll.h>
-#include <stdio.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <unistd.h>
-
-struct client {
- char nickname[32];
- int has_nickname;
-};
-
-// returns a socket file descriptor.
-// if connection could not be established, -1 gets returned
-int establish_connection(struct sockaddr_in addr) {
- int sfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sfd == -1) {
- return -1;
- }
- int opt = -1;
- setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
- int bind_addr = bind(sfd, (struct sockaddr *)&addr, sizeof(addr));
- if (bind_addr == -1) {
- close(sfd);
- return -1;
- }
- int listen_addr = listen(sfd, 100);
- if (listen_addr == -1) {
- close(sfd);
- return -1;
- }
- return sfd;
-}
-
-// handles new telnet connections.
-// returns 0 on success and -1 on failure.
-int handle_new_connection(int sfd, struct pollfd *fds, nfds_t *nfds,
- struct client *clients) {
- int conn = accept(sfd, NULL, NULL);
- if (conn == -1)
- return -1;
- fds[*nfds].fd = conn;
- fds[*nfds].events = POLLIN;
- clients[*nfds].has_nickname = 0;
- char *message = "Enter your nickname:\n";
- write(fds[*nfds].fd, message, strlen(message));
- (*nfds)++;
- return 0;
-}
-
-// process message to all the connected clients.
-// returns 0 on success and -1 on failure.
-// TODO: add struct client *clients
-int process_message(struct pollfd *fds, nfds_t *nfds, struct client *clients) {
- for (nfds_t i = 1; i < *nfds; i++) {
- /* if client has something to say */
- if (fds[i].revents & POLLIN) {
- char buffer[1024]; /* create a new empty buffer */
- ssize_t message =
- read(fds[i].fd, buffer,
- sizeof(buffer)); /* read the message which the client sent */
- if (message == 0) {
- /* close the connection if client disappeared or disconnected */
- close(fds[i].fd);
- fds[i] = fds[*nfds - 1]; /* move back last slot */
- (*nfds)--; /* size down the current array */
- i--;
- } else if (message == -1) {
- /* if message of the client returns an error */
- close(fds[i].fd);
- fds[i] = fds[*nfds - 1]; /* move back last slot */
- (*nfds)--; /* size down the current array */
- i--;
- return -1;
- } else {
- /* send every message bytes to the other clients */
- for (nfds_t j = 1; j < *nfds; j++) {
- if (j != i) {
- write(fds[j].fd, buffer, message);
- }
- }
- }
- }
- }
- return 0;
-}
+#include "telnet.h"
int main() {
struct sockaddr_in addr;