diff options
author | gayot_o <gayot_o@localhost> | 2013-11-19 02:43:27 +0000 |
---|---|---|
committer | gayot_o <gayot_o@localhost> | 2013-11-19 02:45:46 +0000 |
commit | 459e2685158f1dc5a9cd3da1c3125889bc02f9d6 (patch) | |
tree | d4a205fed05e69d1184da0e76f1fb9ac49e06e43 /callbacks.c | |
parent | b0155666fbbb6c4105c36ffcddb894d7d21e38ae (diff) |
mplayer_server: first version of the server
Diffstat (limited to 'callbacks.c')
-rw-r--r-- | callbacks.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/callbacks.c b/callbacks.c new file mode 100644 index 0000000..e41fe8b --- /dev/null +++ b/callbacks.c @@ -0,0 +1,108 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "mplayer_server.h" +#include "request.h" + +#define CB(_name) int callback_##_name(const byte *buffer __attribute__((unused)), int size __attribute__((unused))) + +CB(load_url); +CB(pause); +CB(quit); +CB(snd_down); +CB(snd_up); +CB(fullscreen); +CB(mute); + +extern FILE *stream_g; + +typedef struct { + int opcode; + int (*cb)(const byte *, int); +} callback_t; + +static callback_t callbacks_g[] = { + {0, callback_load_url}, + {1, callback_pause}, + {2, callback_quit}, + {3, callback_snd_up}, + {4, callback_snd_down}, + {5, callback_fullscreen}, + {6, callback_mute}, +}; + +/* + * returns a new malloced() null terminated escaped string + * if the string is considered dangerous, then the function returns NULL + */ + +char *real_escape_string(const byte *buf, int size); + +void *get_assoc_cb(int opcode) +{ + for (size_t i = 0; i < sizeof(callbacks_g) / sizeof(callbacks_g[0]); ++i) { + if (callbacks_g[i].opcode == opcode) { + return callbacks_g[i].cb; + } + } + + return NULL; +} + +#define SEND_CMD(_fmt, ...) \ + do { \ + _log(_fmt, ##__VA_ARGS__); \ + fprintf(stream_g, _fmt "\n", ##__VA_ARGS__); \ + } while (0) + +/* TODO refuse the execution of commands with arguments when they do not need */ +CB(load_url) +{ + /* XXX beware, someone may perform arbitrary code injection */ + char *escaped = real_escape_string(buffer, size); + + if (escaped == NULL) { + return -1; + } + + SEND_CMD("loadfile '%s'", escaped); + + free(escaped); + return 0; +} + +CB(pause) +{ + SEND_CMD("pause"); + return 0; +} + +CB(quit) +{ + SEND_CMD("quit"); + return 0; +} + +CB(snd_down) +{ + SEND_CMD("volume -5"); + return 0; +} + +CB(snd_up) +{ + SEND_CMD("volume +5"); + return 0; +} + +CB(fullscreen) +{ + SEND_CMD("vo_fullscreen"); + return 0; +} + +CB(mute) +{ + SEND_CMD("mute"); + return 0; +} |