aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/facemgr/examples/updownsrv/updownsrv.c
blob: 57661d42737b07fe843ac47bdd6d38a33f989a81 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Dummy server sending alternating bytes to all clients.
 *
 * This is used by the face manager to illustrate the creation of interfaces
 * using unix domains that sets a face up and down.
 */

#include <arpa/inet.h>   // inet_ntop
#include <errno.h>       // EINTR,. ..
#include <netinet/in.h>  // INET_ADDRSTRLEN, INET6_ADDRSTRLEN
#include <stdio.h>
#include <inttypes.h>

#include <stdlib.h>
#include <sys/socket.h>
#include <sys/timerfd.h>
#include <sys/un.h>  // sockaddr_un
#include <unistd.h>  // fcntl
#include <fcntl.h>   // fcntl

#include <hicn/util/sstrncpy.h>

/**
 * \brief Default unix socket path (the leading \0 means using the abstract
 * namespace instead of the filesystem).
 */
#define UNIX_PATH "\0updownsrv"

/**
 * \brief Default interval (in seconds) between timer events */
#define DEFAULT_INTERVAL_SEC 5
#define DEFAULT_INTERVAL_NSEC 0

/**
 * \brief Maximum allowed number of connected clients
 */
#define MAX_CLIENTS 5

/**
 * \brief Maximum backlog of listening unix socket
 */
#define LISTEN_BACKLOG MAX_CLIENTS

/**
 * \brief Creates a unix server socket
 * \param [in] path - string representing the path on which to listen for
 *      connections
 * \return int - fd associated to the socket
 */
int create_unix_server(char* path) {
  struct sockaddr_un addr;
  int fd;

  fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd == -1) {
    perror("socket error");
    return -1;
  }

  if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
    perror("fcntl");
    return -1;
  }

  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  if (*path == '\0') {
    *addr.sun_path = '\0';
    strcpy_s(addr.sun_path + 1, sizeof(addr.sun_path) - 2, path + 1);
  } else {
    strcpy_s(addr.sun_path, sizeof(addr.sun_path) - 1, path);
    unlink(path);
  }

  if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
    perror("bind error");
    return -1;
  }

  if (listen(fd, LISTEN_BACKLOG) == -1) {
    perror("listen error");
    return -1;
  }

  return fd;
}

/**
 * \brief Main function
 */
int main() {
  int fd, tfd;
  int rc;

  /* Alternating state of the server : 0 / 1 */
  unsigned state = 0;

  /*
   * This server has to send a signal to all connected clients at periodic
   * intervals. Since we don't expect a large number of connected clients for
   * such a simple program, we simply use a statically allocated array.
   */
  int clients[MAX_CLIENTS];
  size_t num_clients = 0;

  fd_set active_fd_set, read_fd_set;
  FD_ZERO(&active_fd_set);

  /* Create listening unix socket */
  fd = create_unix_server(UNIX_PATH);
  if (fd < 0) exit(EXIT_FAILURE);

  if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
    perror("fcntl");
    exit(EXIT_FAILURE);
  }

  FD_SET(fd, &active_fd_set);

  /* Create timer */
  tfd = timerfd_create(CLOCK_MONOTONIC, 0);
  if (tfd == -1) {
    perror("timer error");
    exit(EXIT_FAILURE);
  }

  if (fcntl(tfd, F_SETFL, O_NONBLOCK) < 0) {
    perror("fcntl");
    exit(EXIT_FAILURE);
  }

  FD_SET(tfd, &active_fd_set);

  struct itimerspec ts = {.it_interval =
                              {
                                  .tv_sec = DEFAULT_INTERVAL_SEC,
                                  .tv_nsec = DEFAULT_INTERVAL_NSEC,
                              },
                          .it_value = {
                              .tv_sec = DEFAULT_INTERVAL_SEC,
                              .tv_nsec = DEFAULT_INTERVAL_NSEC,
                          }};
  rc = timerfd_settime(tfd, 0, &ts, NULL);
  if (rc == -1) {
    perror("timerfd_settime");
    exit(EXIT_FAILURE);
  }

  printf("Waiting for clients...\n");

  for (;;) {
    /* Block until input arrives on one or more active sockets. */
    read_fd_set = active_fd_set;
    rc = select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL);
    if (rc < 0) {
      if (rc == EINTR) break;
      perror("select");
      exit(EXIT_FAILURE);
    }

    /* Service all the sockets with input pending. */
    for (int i = 0; i < FD_SETSIZE; ++i) {
      if (!FD_ISSET(i, &read_fd_set)) continue;
      if (i == fd) {
        /* Connection request on original socket. */
        int client_fd = accept(fd, NULL, NULL);
        if (client_fd < 0) {
          perror("accept");
          continue;
        }

        fprintf(stderr, "Server: connect from new client\n");
        clients[num_clients++] = client_fd;
        FD_SET(client_fd, &active_fd_set);
      } else if (i == tfd) {
        /* Timer event */
        uint64_t res;

        read(tfd, &res, sizeof(res));
        //                while (read(fd, &missed, sizeof(missed)) > 0)
        //                    ;
        for (unsigned j = 0; j < num_clients; j++) {
          write(clients[j], state ? "\1" : "\0", 1);
        }
        printf("STATE=%d\n", state);
        state = 1 - state;
      } else {
        char buf[1024];
        rc = read(i, buf, sizeof(buf));
        /* Client event : we close the connection on any event... */
        for (unsigned j = 0; j < num_clients; j++) {
          if (i == clients[j]) {
            clients[j] = clients[num_clients--];
            break;
          }
        }
        close(i);
        FD_CLR(i, &active_fd_set);
      }
    }
  }

  int ret = EXIT_SUCCESS;

  /* Close all active client connections */
  for (unsigned i = 0; i < num_clients; i++) {
    rc = close(clients[i]);
    if (rc == -1) {
      perror("close");
      ret = EXIT_FAILURE;
    }
  }

  /* Close server */
  rc = close(fd);
  if (rc == -1) {
    perror("close");
    ret = EXIT_FAILURE;
  }

  /* Terminate timer */
  ts.it_value.tv_sec = 0;
  rc = timerfd_settime(tfd, 0, &ts, NULL);
  if (rc == -1) {
    perror("timerfd_settime");
    exit(EXIT_FAILURE);
  }

  exit(ret);
}