void *incoming_connection(void *arg) { char readline[MAXLINE]; struct thread_argument *ta = (struct thread_argument *)arg; struct context *context = context_new(true, true); context->find = ta->find; for (;;) { bzero(readline, sizeof(readline)); int n; if (((n = CyaSSL_read(ta->ssl, readline, MAXLINE)) <= 0)) { fprintf(stderr, "client closed connection\n"); goto free_ssl; } fprintf(stderr, "%d bytes received: %s\n", n, readline); struct byte_array *raw_message = byte_array_new_size(n); raw_message->data = (uint8_t*)readline; int32_t raw_message_length = serial_decode_int(raw_message); assert_message(raw_message_length < MAXLINE, "todo: handle long messages"); struct variable *message = variable_deserialize(context, raw_message); struct variable *listener = (struct variable *)map_get(server_listeners, (void*)(VOID_INT)ta->fd); vm_call(context, listener, message); } free_ssl: CyaSSL_free(ta->ssl); // Free CYASSL object free(ta); context_del(context); return NULL; }
struct byte_array *byte_array_from_string(const char* str) { int len = (int)strlen(str); struct byte_array* ba = byte_array_new_size(len); memcpy(ba->data, str, len); ba->length = len; //DEBUGPRINT("byte_array_from_string %s %p->%p\n", str, ba, ba->data); return ba; }
struct byte_array *byte_array_copy(const struct byte_array* original) { if (original == NULL) return NULL; struct byte_array* copy = byte_array_new_size(original->size); memcpy(copy->data, original->data, original->length); copy->length = original->length; copy->current = copy->data + (original->current - original->data); return copy; }
struct byte_array* serial_decode_string(struct byte_array* buf) { null_check(buf); int32_t len = serial_decode_int(buf); assert_message(len>=0, "negative malloc"); struct byte_array* ba = byte_array_new_size(len); ba->data = ba->current = (uint8_t*)malloc(len); null_check(ba->data); memcpy(ba->data, buf->current, len); buf->current += len; return ba; }
struct byte_array *read_file(const struct byte_array *filename_ba, uint32_t offset, long size) { FILE * file; char *str; const char *filename_str = byte_array_to_string(filename_ba); if (!(file = fopen(filename_str, "rb"))) goto no_file; long available; if ((available = fsize(file)) < 0) goto no_file; available -= offset; //printf("read_file %s @%d size=%ld available=%ld\n", filename_str, offset, size, available); size = size ? MIN(size, available) : available; if (size <= 0) goto no_file; if (size > INPUT_MAX_LEN) goto no_file; if (!(str = (char*)malloc((size_t)size + 1))) goto no_file; if (fseek(file, offset, SEEK_SET)) goto no_file; if (fread(str, 1, (size_t)size, file) == EOF) goto no_file; if (ferror(file)) goto no_file; if (fclose(file)) goto no_file; struct byte_array* ba = byte_array_new_size((uint32_t)size); ba->length = (uint32_t)size; memcpy(ba->data, str, size); //free(filename_str); free(str); return ba; no_file: //free(filename_str); DEBUGPRINT("\nCould not read file %s\n", filename_str); return NULL; }
struct byte_array *byte_array_replace(struct byte_array *within, struct byte_array *replacement, uint32_t start, int32_t length) { null_check(within); null_check(replacement); uint32_t ws = within->length; assert_message(start < ws, "index out of bounds"); if (length < 0) length = ws - start; int32_t new_length = within->length - length + replacement->length; struct byte_array *replaced = byte_array_new_size(new_length); replaced->length = new_length; memcpy(replaced->data, within->data, start); memcpy(replaced->data + start, replacement->data, replacement->length); memcpy(replaced->data + start + replacement->length, within->data + start + length, within->length - start - length); //DEBUGPRINT("byte_array_replace %p->%p\n", replaced, replaced->data); return replaced; }
struct byte_array *byte_array_new() { return byte_array_new_size(0); }