summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 103ca2d6227ed6e76889a37bcdd85aa0fffca9b6 (plain) (blame)
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
#include <netinet/in.h>
#include <poll.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>

// 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) {
    perror("could not open socket");
    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) {
    perror("could not bind socket");
    close(sfd);
    return -1;
  }
  int listen_addr = listen(sfd, 100);
  if (listen_addr == -1) {
    perror("could not listen");
    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) {
  int conn = accept(sfd, NULL, NULL);
  if (conn == -1)
    return -1;
  fds[*nfds].fd = conn;
  fds[*nfds].events = POLLIN;
  (*nfds)++;
  return 0;
}

// process message to all the connected clients.
// returns 0 on success and -1 on failure.
int process_message(struct pollfd *fds, nfds_t *nfds) {
  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 of the client returns an error */
      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--;
        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;
}

int main() {
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;         /* IPv4 */
  addr.sin_port = htons(8000);       /* Port */
  addr.sin_addr.s_addr = INADDR_ANY; /* equals to 0.0.0.0 */
  struct pollfd fds[256];
  int sfd = establish_connection(addr);
  if (sfd == -1) {
    perror("could not establish connection");
    return 1;
  }

  fds[0].fd = sfd;        /* the listening socket */
  fds[0].events = POLLIN; /* data to read */
  nfds_t nfds = 1; /* 1 = listener alone, grows by 1 per connected client */
  while (1) {
    /* we start the polling here */
    int ready =
        poll(fds, nfds, -1); /* poll to accept many conn simultaneously */
    if (ready == -1) {
      perror("could not run poll");
      return 1;
    }
    if (fds[0].revents & POLLIN) {
      if (handle_new_connection(sfd, fds, &nfds) == -1) {
        perror("could not accept connection");
        continue;
      }
    }
    if (process_message(fds, &nfds) == -1) {
      perror("could not process message");
      continue;
    }
  }
  return 0;
}