1 #define _GNU_SOURCE /* See feature_test_macros(7) */
9 #include "signal_exit.h"
14 char *line = (char *)malloc((LWS_PRE + INPUT_LINE_LENGTH)*sizeof(char));
19 return line + LWS_PRE;
22 char *copy_line(char *line) {
23 char *copy = new_line();
27 strncpy(copy, line, INPUT_LINE_LENGTH);
30 void free_line(char *line)
35 node *node_init(char *line)
37 node *res = (node *)malloc(sizeof(node));
49 list *res = (list *)calloc(1, sizeof(list));
56 int list_add(list *in, char *line)
58 node *new = node_init(line);
71 void list_remove(list *in)
73 node *tmp = in->first;
75 in->first = tmp->next;
84 void list_deinit(list *in)
92 static const struct lws_http_mount mount = {
93 .mount_next = NULL, /* linked-list "next" */
94 .mountpoint = HTTP_MOUNTPOINT,
95 .origin = HTTP_ORIGIN, /* serve from dir */
96 .def = HTTP_DEFAULT, /* default filename */
99 .extra_mimetypes = NULL,
105 .cache_revalidate = 0,
106 .cache_intermediaries = 0,
107 .origin_protocol = LWSMPRO_FILE, /* files in a dir */
108 .mountpoint_len = 1, /* char count */
109 .basic_auth_login_file = NULL,
112 typedef struct per_vhost_data__merica_terminal {
113 struct lws_context *context;
114 struct lws_vhost *vhost;
115 const struct lws_protocols *protocol;
116 } per_vhost_data__merica_terminal;
119 typedef struct per_session_data__merica_terminal {
121 } per_session_data__merica_terminal;
125 PROTOCOL_HTTP = 0, // always first
126 PROTOCOL_MERICA_TERMINAL
129 static int callback_merica_terminal(struct lws *wsi,
130 enum lws_callback_reasons reason,
131 void *user, void *in, size_t len);
133 // list of supported protocols and callbacks
134 static struct lws_protocols protocols[] = {
135 // first protocol must always be HTTP handler
136 {"http", lws_callback_http_dummy, 0, 0},
138 "merica-terminal-protocol",
139 callback_merica_terminal,
140 0, //sizeof(struct per_session_data__merica_terminal),
141 MT_PROTOCOL_RX_BUFFER_SIZE
143 {NULL, NULL, 0, 0} // terminator
146 static int callback_merica_terminal(struct lws *wsi,
147 enum lws_callback_reasons reason,
148 void *user, void *in, size_t len)
150 struct lws_context *context = lws_get_context(wsi);
151 struct lws_vhost *vhost = lws_get_vhost(wsi);
152 const struct lws_protocols *prot = lws_get_protocol(wsi);
154 per_session_data__merica_terminal *pss =
155 (per_session_data__merica_terminal *)user;
157 per_vhost_data__merica_terminal *vhd =
158 (per_vhost_data__merica_terminal *)
159 lws_protocol_vh_priv_get(vhost, prot);
163 list *lines = (list *)lws_context_user(context);
167 case LWS_CALLBACK_PROTOCOL_INIT:
168 vhd = lws_protocol_vh_priv_zalloc(
170 sizeof(struct per_vhost_data__merica_terminal)
172 vhd->context = context;
173 vhd->protocol = prot;
177 case LWS_CALLBACK_PROTOCOL_DESTROY:
183 case LWS_CALLBACK_ESTABLISHED:
186 case LWS_CALLBACK_SERVER_WRITEABLE:
188 line = lines->first->line;
190 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
193 lws_callback_on_writable_all_protocol(context,
194 &protocols[PROTOCOL_MERICA_TERMINAL]);
197 line = copy_line(JSON_EMPTY);
200 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
207 fprintf(stderr, "ERROR %d writing to di socket\n", n);
212 case LWS_CALLBACK_RECEIVE:
213 if (strcmp((const char *)in, "reset") == 0) {
214 line = copy_line(JSON_EMPTY);
216 if (list_add(lines, line) == 0) {
217 lws_callback_on_writable_all_protocol(context,
218 &protocols[PROTOCOL_MERICA_TERMINAL]);
223 } else if (strcmp((const char *)in, "close") == 0) {
224 fprintf(stderr, "closing websocket\n");
225 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
226 (unsigned char *)"seeya", 5);
238 static void fd_cb(EV_P_ ev_io *w_, int revents)
240 ev_io_ws *w = (ev_io_ws *)w_;
242 char *pos = w->pos++;
244 read(w->w.fd, pos, 1);
246 if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
251 syslog(LOG_INFO, w->text);
252 char *line = new_line();
254 if (list_add(w->lines, w->text) == 0) {
257 lws_callback_on_writable_all_protocol(w->context,
258 &protocols[PROTOCOL_MERICA_TERMINAL]);
263 fprintf(stderr, "cannot malloc new line\n");
267 int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
269 struct lws_context_creation_info info;
271 list *l = list_init();
276 openlog(program_invocation_short_name, LOG_PID | LOG_PERROR, LOG_DAEMON);
278 memset(&info, 0, sizeof(info));
279 info.port = HTTP_PORT;
280 info.mounts = &mount;
281 info.protocols = protocols;
282 info.max_http_header_pool = 1;
283 info.options |= LWS_SERVER_OPTION_LIBEV;
286 struct lws_context *context = lws_create_context(&info);
288 fprintf(stderr, "lws_create_context failed\n");
293 self->context = context;
295 ev_io_ws *w = &(self->fd_watcher);
296 w->context = context;
299 char *line = new_line();
306 ev_io_init(&(w->w), fd_cb, fd, EV_READ);
307 ev_io_start(loop, (ev_io *)w);
309 return lws_ev_initloop(context, loop, 0);
312 void mt_server_deinit(mt_server_t *self)
314 free_line(self->fd_watcher.text);
315 list_deinit(self->fd_watcher.lines);
316 lws_context_destroy(self->context);
321 int main(int argc, const char **argv)
323 struct ev_loop *loop = EV_DEFAULT;
326 set_signal_exit(loop);
328 if (mt_server_init(&server, loop, STDIN_FILENO) != 0) {
334 mt_server_deinit(&server);
335 ev_loop_destroy(loop);