summaryrefslogtreecommitdiff
path: root/src/server.c
blob: 188671248f832ac66ad642b3034a15e6a9b9324f (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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <openssl/sha.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <assert.h>
#include <unistd.h>

#define LOG_MODULE "server"
#include "logger/logger.h"

#define PORT 3456

#include "server/base64.c"
#include "server/ws_key.c"
#include "server/comms.c"
#include "ipc.c"

mqd_t logging, tx, rx;
int handle_client(int client);

int main(void) {
  logging = mq_open("/logs", O_WRONLY);
  tx = mq_open("/server_to_engine", O_WRONLY);
  rx = mq_open("/engine_to_server", O_RDONLY);

  int server_fd = socket(AF_INET, SOCK_STREAM, 0);
  int opt = 1;

  setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  struct sockaddr_in addr = {
      .sin_family = AF_INET,
      .sin_addr.s_addr = INADDR_ANY,
      .sin_port = htons(PORT)
  };

  if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind");
    return 1;
  }

  if (listen(server_fd, 16) < 0) {
    perror("listen");
    return 1;
  }

  log_infof("Listening on %d", PORT);

  while(1) {
    int client = accept(server_fd, NULL, NULL);

    if (client < 0) continue;
    char req[4096];

    int n = recv(client, req, sizeof(req) - 1, 0);
    if (n <= 0) {
      log_infof("Client close");
      close(client);
      continue;
    }

    req[n] = 0;

    char *key = find_websocket_key(req);

    if (!key) {
      close(client);
      continue;
    }

    char accept_key[128];

    websocket_accept_key(key, accept_key);
    char response[512];

    snprintf(
      response,
      sizeof(response),
      "HTTP/1.1 101 Switching Protocols\r\n"
      "Upgrade: websocket\r\n"
      "Connection: Upgrade\r\n"
      "Sec-WebSocket-Accept: %s\r\n"
      "\r\n",
      accept_key
    );

    send(client, response, strlen(response), 0);
    if (fork() == 0) {
      log_infof("{%d} WebSocket connected", client);
      while(1) {
        if (handle_client(client)) continue;
        else break;
      }
      log_infof("{%d} WebSocket disconnected", client);
      close(client);
    }
  }
}

int handle_client(int client) {
  unsigned char hdr[2];

  if (recv(client, hdr, 2, MSG_WAITALL) != 2) {
    log_warnf("{%d} recv close", client)
    return 0;
  }

  unsigned opcode = hdr[0] & 0x0F;
  unsigned len = hdr[1] & 0x7F;
  log_infof(
    "{%d} opcode=%u len=%u masked=%u",
    client,
    opcode,
    hdr[1] & 0x7F,
    !!(hdr[1] & 0x80)
  );
  if (opcode == 0x8) {
    unsigned char close_frame[2] = {0x88, 0x00};
    send(client, close_frame, 2, 0);
    log_warnf("{%d} opcode 0x8 close", client);
    return 0;
  }

  if (opcode == 0x9) {
    unsigned char pong[2] = {0x8A, 0x00};
    send(client, pong, 2, 0);
    return 1;
  }

  if (len >= 126) {
    log_infof("{%d} unsupported length %u", client, len);
    return 0;
  }

  unsigned char mask[4];
  recv(client, mask, 4, MSG_WAITALL);

  unsigned char payload[126];
  recv(client, payload, len, MSG_WAITALL);

  for (unsigned i = 0; i < len; i++)
    payload[i] ^= mask[i % 4];

  payload[len] = 0;
  char* payload_ptr = (char*)payload;
  struct strs response = handle_input(logging, tx, rx, payload_ptr, len);

  unsigned char out[1024];
  out[0] = 0x81;
  for (int i = 0; i < response.count; i++) {
    struct str item = response.data[i];
    out[1] = item.len;
    assert(item.len <= 1022);

    memcpy(out + 2, item.msg, item.len);
    send(client, out, item.len + 2, 0);
  }
  char end_transmission[] = "END-TRANSMISSION";
  int n = strlen(end_transmission);
  out[1] = n;

  memcpy(out + 2, end_transmission, n);
  send(client, out, n + 2, 0);
  return 1;
}