diff --git a/guacamole/proxy/Makefile b/guacamole/proxy/Makefile index 34b3ddfb2..13710eea9 100644 --- a/guacamole/proxy/Makefile +++ b/guacamole/proxy/Makefile @@ -3,12 +3,15 @@ all: guacd -guacd: daemon.o proxy.o guacio.o - $(CC) -O2 daemon.o proxy.o guacio.o -l png -l vncclient -o guacd +guacd: daemon.o proxy.o guacio.o protocol.o + $(CC) -O2 daemon.o proxy.o protocol.o guacio.o -l png -l vncclient -o guacd proxy.o: proxy.c proxy.h guacio.h $(CC) -O2 -c -ansi -pedantic proxy.c +protocol.o: protocol.c protocol.h guacio.h + $(CC) -O2 -c -ansi -pedantic protocol.c + guacio.o: guacio.c guacio.h $(CC) -O2 -c -ansi -pedantic guacio.c diff --git a/guacamole/proxy/guacio.c b/guacamole/proxy/guacio.c index f04dffc6d..f61fe8df7 100644 --- a/guacamole/proxy/guacio.c +++ b/guacamole/proxy/guacio.c @@ -3,6 +3,8 @@ #include #include +#include + #include "guacio.h" char __GUACIO_BAS64_CHARACTERS[64] = { diff --git a/guacamole/proxy/protocol.c b/guacamole/proxy/protocol.c new file mode 100644 index 000000000..9a16ae387 --- /dev/null +++ b/guacamole/proxy/protocol.c @@ -0,0 +1,169 @@ +#include +#include +#include +#include + +#include "guacio.h" +#include "protocol.h" + +char* guac_escape_string(const char* str) { + + char* escaped; + char* current; + + int i; + int length = 0; + + /* Determine length */ + for (i=0; str[i] != '\0'; i++) { + switch (str[i]) { + + case ';': + length += 2; + break; + + case ',': + length += 2; + break; + + default: + length++; + + } + } + + /* Allocate new */ + escaped = malloc(length+1); + + current = escaped; + for (i=0; str[i] != '\0'; i++) { + switch (str[i]) { + + case ';': + *(current++) = '\\'; + *(current++) = 's'; + break; + + case ',': + *(current++) = '\\'; + *(current++) = 'c'; + break; + + default: + *(current++) = str[i]; + } + + } + + return escaped; + +} + +void guac_send_name(GUACIO* io, const char* name) { + guac_write_string(io, "name:"); + guac_write_string(io, name); + guac_write_string(io, ";"); +} + +void guac_send_size(GUACIO* io, int w, int h) { + guac_write_string(io, "size:"); + guac_write_int(io, w); + guac_write_string(io, ","); + guac_write_int(io, h); + guac_write_string(io, ";"); +} + +void guac_send_copy(GUACIO* io, int srcx, int srcy, int w, int h, int dstx, int dsty) { + guac_write_string(io, "copy:"); + guac_write_int(io, srcx); + guac_write_string(io, ","); + guac_write_int(io, srcy); + guac_write_string(io, ","); + guac_write_int(io, w); + guac_write_string(io, ","); + guac_write_int(io, h); + guac_write_string(io, ","); + guac_write_int(io, dstx); + guac_write_string(io, ","); + guac_write_int(io, dsty); + guac_write_string(io, ";"); +} + +void __guac_write_png(png_structp png, png_bytep data, png_size_t length) { + + if (guac_write_base64((GUACIO*) png->io_ptr, data, length) < 0) { + perror("Error writing PNG"); + png_error(png, "Error writing PNG"); + return; + } + +} + +void __guac_write_flush(png_structp png) { +} + +void guac_send_png(GUACIO* io, int x, int y, png_byte** png_rows, int w, int h) { + + png_structp png; + png_infop png_info; + png_byte* row; + + /* Write image */ + + /* Set up PNG writer */ + png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (!png) { + perror("Error initializing libpng write structure"); + return; + } + + png_info = png_create_info_struct(png); + if (!png_info) { + perror("Error initializing libpng info structure"); + png_destroy_write_struct(&png, NULL); + return; + } + + /* Set error handler */ + if (setjmp(png_jmpbuf(png))) { + perror("Error setting handler"); + png_destroy_write_struct(&png, &png_info); + return; + } + + png_set_write_fn(png, io, __guac_write_png, __guac_write_flush); + + /* Set PNG IHDR */ + png_set_IHDR( + png, + png_info, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT + ); + + guac_write_string(io, "png:"); + guac_write_int(io, x); + guac_write_string(io, ","); + guac_write_int(io, y); + guac_write_string(io, ","); + png_set_rows(png, png_info, png_rows); + png_write_png(png, png_info, PNG_TRANSFORM_IDENTITY, NULL); + + if (guac_flush_base64(io) < 0) { + perror("Error flushing PNG"); + png_error(png, "Error flushing PNG"); + return; + } + + png_destroy_write_struct(&png, &png_info); + + guac_write_string(io, ";"); + +} + + diff --git a/guacamole/proxy/protocol.h b/guacamole/proxy/protocol.h new file mode 100644 index 000000000..edf432809 --- /dev/null +++ b/guacamole/proxy/protocol.h @@ -0,0 +1,16 @@ + +#ifndef __PROTOCOL_H +#define __PROTOCOL_H + +#include + +#include "guacio.h" + +char* guac_escape_string(const char* str); +void guac_send_name(GUACIO* io, const char* name); +void guac_send_size(GUACIO* io, int w, int h); +void guac_send_copy(GUACIO* io, int srcx, int srcy, int w, int h, int dstx, int dsty); +void guac_send_png(GUACIO* io, int x, int y, png_byte** png_rows, int w, int h); + +#endif + diff --git a/guacamole/proxy/proxy.c b/guacamole/proxy/proxy.c index 0838a8af5..0de577d19 100644 --- a/guacamole/proxy/proxy.c +++ b/guacamole/proxy/proxy.c @@ -6,6 +6,7 @@ #include #include "guacio.h" +#include "protocol.h" #include "proxy.h" char __guac_password[] = "potato"; @@ -13,166 +14,6 @@ char __guac_password[] = "potato"; char* __GUAC_VNC_TAG_IO = "GUACIO"; char* __GUAC_VNC_TAG_PNG_ROWS = "PNG_ROWS"; -char* guac_escape_string(const char* str) { - - char* escaped; - char* current; - - int i; - int length = 0; - - /* Determine length */ - for (i=0; str[i] != '\0'; i++) { - switch (str[i]) { - - case ';': - length += 2; - break; - - case ',': - length += 2; - break; - - default: - length++; - - } - } - - /* Allocate new */ - escaped = malloc(length+1); - - current = escaped; - for (i=0; str[i] != '\0'; i++) { - switch (str[i]) { - - case ';': - *(current++) = '\\'; - *(current++) = 's'; - break; - - case ',': - *(current++) = '\\'; - *(current++) = 'c'; - break; - - default: - *(current++) = str[i]; - } - - } - - return escaped; - -} - -void guac_write_png(png_structp png, png_bytep data, png_size_t length) { - - if (guac_write_base64((GUACIO*) png->io_ptr, data, length) < 0) { - perror("Error writing PNG"); - png_error(png, "Error writing PNG"); - return; - } - -} - -void guac_write_flush(png_structp png) { -} - -void guac_send_name(GUACIO* io, const char* name) { - guac_write_string(io, "name:"); - guac_write_string(io, name); - guac_write_string(io, ";"); -} - -void guac_send_size(GUACIO* io, int w, int h) { - guac_write_string(io, "size:"); - guac_write_int(io, w); - guac_write_string(io, ","); - guac_write_int(io, h); - guac_write_string(io, ";"); -} - -void guac_send_copy(GUACIO* io, int srcx, int srcy, int w, int h, int dstx, int dsty) { - guac_write_string(io, "copy:"); - guac_write_int(io, srcx); - guac_write_string(io, ","); - guac_write_int(io, srcy); - guac_write_string(io, ","); - guac_write_int(io, w); - guac_write_string(io, ","); - guac_write_int(io, h); - guac_write_string(io, ","); - guac_write_int(io, dstx); - guac_write_string(io, ","); - guac_write_int(io, dsty); - guac_write_string(io, ";"); -} - -void guac_send_png(GUACIO* io, int x, int y, png_byte** png_rows, int w, int h) { - - png_structp png; - png_infop png_info; - png_byte* row; - - /* Write image */ - - /* Set up PNG writer */ - png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (!png) { - perror("Error initializing libpng write structure"); - return; - } - - png_info = png_create_info_struct(png); - if (!png_info) { - perror("Error initializing libpng info structure"); - png_destroy_write_struct(&png, NULL); - return; - } - - /* Set error handler */ - if (setjmp(png_jmpbuf(png))) { - perror("Error setting handler"); - png_destroy_write_struct(&png, &png_info); - return; - } - - png_set_write_fn(png, io, guac_write_png, guac_write_flush); - - /* Set PNG IHDR */ - png_set_IHDR( - png, - png_info, - w, - h, - 8, - PNG_COLOR_TYPE_RGB, - PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_DEFAULT, - PNG_FILTER_TYPE_DEFAULT - ); - - guac_write_string(io, "png:"); - guac_write_int(io, x); - guac_write_string(io, ","); - guac_write_int(io, y); - guac_write_string(io, ","); - png_set_rows(png, png_info, png_rows); - png_write_png(png, png_info, PNG_TRANSFORM_IDENTITY, NULL); - - if (guac_flush_base64(io) < 0) { - perror("Error flushing PNG"); - png_error(png, "Error flushing PNG"); - return; - } - - png_destroy_write_struct(&png, &png_info); - - guac_write_string(io, ";"); - -} - void guac_vnc_update(rfbClient* client, int x, int y, int w, int h) { int dx, dy;