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 fprintf(stderr, "ERROR %d writing to di socket\n", n);
203 case LWS_CALLBACK_RECEIVE:
204 if (strcmp((const char *)in, "close") == 0) {
205 fprintf(stderr, "closing websocket\n");
206 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
207 (unsigned char *)"seeya", 5);
219 static void fd_cb(EV_P_ ev_io *w_, int revents)
221 ev_io_ws *w = (ev_io_ws *)w_;
223 char *pos = w->pos++;
225 read(w->w.fd, pos, 1);
227 if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
232 syslog(LOG_INFO, "%s", w->text);
233 char *line = new_line();
235 if (list_add(w->lines, w->text) == 0) {
238 lws_callback_on_writable_all_protocol(w->context,
239 &protocols[PROTOCOL_MERICA_TERMINAL]);
244 fprintf(stderr, "cannot malloc new line\n");
248 int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
250 struct lws_context_creation_info info;
252 list *l = list_init();
257 openlog(program_invocation_short_name, LOG_PID | LOG_PERROR, LOG_DAEMON);
259 memset(&info, 0, sizeof(info));
260 info.port = HTTP_PORT;
261 info.mounts = &mount;
262 info.protocols = protocols;
263 info.max_http_header_pool = 1;
264 info.options |= LWS_SERVER_OPTION_LIBEV;
267 struct lws_context *context = lws_create_context(&info);
269 fprintf(stderr, "lws_create_context failed\n");
274 self->context = context;
276 ev_io_ws *w = &(self->fd_watcher);
277 w->context = context;
280 char *line = new_line();
287 ev_io_init(&(w->w), fd_cb, fd, EV_READ);
288 ev_io_start(loop, (ev_io *)w);
290 return lws_ev_initloop(context, loop, 0);
293 void mt_server_deinit(mt_server_t *self)
295 free_line(self->fd_watcher.text);
296 list_deinit(self->fd_watcher.lines);
297 lws_context_destroy(self->context);
302 int main(int argc, const char **argv)
304 struct ev_loop *loop = EV_DEFAULT;
307 set_signal_exit(loop);
309 if (mt_server_init(&server, loop, STDIN_FILENO) != 0) {
315 mt_server_deinit(&server);
316 ev_loop_destroy(loop);