Esempio n. 1
0
struct Connection *Database_open(const char *filename, char mode)
{
	struct Connection *conn = malloc(sizeof(struct Connection));
	check_mem(conn);
    check_debug(conn, "Memory error");

	conn->db = malloc(sizeof(struct Database));
	check_mem(conn);
    check_debug(conn->db, "Memory error");

	if(mode == 'c') {
		conn->file = fopen(filename, "w");
	} else {
		conn->file = fopen(filename, "r+");

		if(conn->file) {
			Database_load(conn);
		}
	}

	check_debug(conn->file, "Failed to open file");
error:
   if(conn->db) free(conn->db);
   if(conn->file) fclose(conn->file);
}
Esempio n. 2
0
bool dl_del(list_t *list, char *new_word){
	check_debug(list != NULL, "List is NULL.");
	check_debug(list->prev != NULL && list->next != NULL,
		"List is empty.");
	check_debug(list->prev != list && list->next != list,
		"List is empty.");
	list_t *node = NULL;
	node = list->next;
	bool result = 0;
	while(node != list){
		list_t *next_node = node->next;
		check_debug(next_node != NULL && node->prev != NULL,
			"Invalid list, node: %s, node->prev: %s",
			next_node == NULL ? "NULL" : "VALID",
			node->prev == NULL ? "NULL" : "VALID");
		if(strcmp(node->word,new_word) == 0){
			check_debug(dl_remove(list, node) == 1, 
				"Could not delete node.");
			result = 1;
		}
		node = next_node;
	}

	return result;
error:
	return 0;
}
Esempio n. 3
0
int Dir_stream_file(FileRecord *file, Connection *conn)
{
    ssize_t sent = 0;
    size_t total = 0;
    off_t offset = 0;
    size_t block_size = MAX_SEND_BUFFER;

    // For the non-sendfile slowpath
    char *file_buffer = NULL;
    int nread = 0;
    int amt = 0;
    int tempfd = -1;

    int rc = Dir_send_header(file, conn);
    check_debug(rc, "Failed to write header to socket.");

    if(conn->ssl == NULL) {
        for(total = 0; fdwait(conn->fd, 'w') == 0 && total < file->sb.st_size;
            total += sent) {
            sent = Dir_send(conn->fd, file->fd, &offset, block_size);
            check_debug(sent > 0, "Failed to sendfile on socket: %d from "
                        "file %d", conn->fd, file->fd);
        }
    }
    else {
        // We have to reopen the file, so we don't get ourselves into seek
        // position trouble
        int tempfd = open((const char *)(file->full_path->data), O_RDONLY);
        check(tempfd >= 0, "Could not reopen open file");

        file_buffer = malloc(MAX_SEND_BUFFER);
        check_mem(file_buffer);

        while((nread = fdread(tempfd, file_buffer, MAX_SEND_BUFFER)) > 0) {
            for(amt = 0, sent = 0; sent < nread; sent += amt) {
                amt = conn->send(conn, file_buffer + sent, nread - sent);
                check_debug(amt > 0, "Failed to send on socket: %d from "
                            "file %d", conn->fd, tempfd);
            }
            total += nread;
        }
        free(file_buffer);
        close(tempfd); tempfd = -1;
    }
    
    check(total <= file->sb.st_size, 
            "Wrote way too much, wrote %d but size was %d",
            (int)total, (int)file->sb.st_size);

    check(total == file->sb.st_size,
            "Sent other than expected, sent: %d, but expected: %d", 
            (int)total, (int)file->sb.st_size);

    return total;

error:
    if(file_buffer) free(file_buffer);
    if(tempfd >= 0) close(tempfd);
    return -1;
}
Esempio n. 4
0
/*	---------------------------------------------------	*/
public	struct	xml_element * xsd_type( struct xml_element * xsd, char * nptr )
{
	struct	xml_element * wptr=(struct xml_element *) 0;
	struct	xml_element * tptr=(struct xml_element *) 0;
	struct	xml_atribut * aptr;
	char 		    * vptr;
	if ( check_debug() ) { printf("xsd:type( %s )\n",nptr); }
	for (	wptr = document_element( xsd, _XSD_COMPLEX );
		wptr != (struct xml_element *) 0;
		wptr = wptr->next )
	{
		if ( strcasecmp( wptr->name, _XSD_COMPLEX ) )
			continue;
		else if (!( aptr = document_atribut( wptr, _XSD_NAME ) ))
			continue;
		else if (!( aptr->value ))
			continue;
		else if (!( vptr = occi_unquoted_value( aptr->value ) ))
			continue;
		else if (!( strcmp( vptr, nptr ) ))
		{
			liberate( vptr );
			return( wptr );
		}
		else
		{
			liberate( vptr );
			continue;
		}
	}
	if ( check_debug() ) { printf("xsd:failure( attribute %s )\n",nptr); }
	return( wptr );
}
Esempio n. 5
0
int connection_proxy_req_parse(Connection *conn)
{
    int rc = 0;
    Host *target_host = conn->req->target_host;
    Backend *req_action = conn->req->action;

    check_debug(!IOBuf_closed(conn->iob), "Client closed, goodbye.");

    rc = Connection_read_header(conn, conn->req);

    check_debug(rc > 0, "Failed to read another header.");
    error_unless(Request_is_http(conn->req), conn, 400,
            "Someone tried to change the protocol on us from HTTP.");

    Backend *found = Host_match_backend(target_host, Request_path(conn->req), NULL);
    error_unless(found, conn, 404, 
            "Handler not found: %s", bdata(Request_path(conn->req)));

    // break out of PROXY if the actions don't match
    if(found != req_action) {
        Request_set_action(conn->req, found);
        return Connection_backend_event(found, conn);
    } else {
        return HTTP_REQ;
    }

    error_response(conn, 500, "Invalid code branch, tell Zed.");
error:
    return REMOTE_CLOSE;
}
Esempio n. 6
0
/**
 * Streams data out of the from IOBuf and into the to IOBuf
 * until it's moved total bytes between them.
 */
int IOBuf_stream(IOBuf *from, IOBuf *to, int total)
{
    int need = 0;
    int remain = total;
    int avail = 0;
    int rc = 0;
    char *data = NULL;

    if(from->len > to->len) IOBuf_resize(to, from->len);

    while(remain > 0) {
        need = remain <= from->len ? remain : from->len;

        data = IOBuf_read(from, need, &avail);
        check_debug(avail > 0, "Nothing in read buffer.");

        rc = IOBuf_send_all(to, IOBuf_start(from), avail);
        check_debug(rc == avail, "Failed to send all of the data: %d of %d", rc, avail)

        // commit whatever we just did
        check(IOBuf_read_commit(from, rc) != -1, "Final commit failed during streaming.");

        // reduce it by the given amount
        remain -= rc;
    }

    assert(remain == 0 && "Buffer math is wrong.");

    return total - remain;

error:
    return -1;
}
Esempio n. 7
0
FileRecord *Dir_resolve_file(Dir *dir, bstring path)
{
    FileRecord *file = NULL;
    bstring target = NULL;

    check(Dir_lazy_normalize_base(dir) == 0, "Failed to normalize base path when requesting %s",
            bdata(path));

    check(bstrncmp(path, dir->prefix, blength(dir->prefix)) == 0, 
            "Request for path %s does not start with %s prefix.", 
            bdata(path), bdata(dir->prefix));

    file = FileRecord_cache_check(dir, path);

    if(file) {
        // TODO: double check this gives the right users count
        file->users++;
        return file;
    }

    // We subtract one from the blengths below, because dir->prefix includes
    // a trailing '/'.  If we skip over this in path->data, we drop the '/'
    // from the URI, breaking the target path
    if(bchar(path, blength(path) - 1) == '/') {
        target = bformat("%s%s%s",
                    bdata(dir->normalized_base),
                    path->data + blength(dir->prefix) - 1,
                    bdata(dir->index_file));
    } else {
        target = bformat("%s%s",
                bdata(dir->normalized_base),
                path->data + blength(dir->prefix) - 1);
    }

    check(target, "Couldn't construct target path for %s", bdata(path));

    check_debug(normalize_path(target) == 0, "Failed to normalize target path.");
   
    check_debug(bstrncmp(target, dir->normalized_base, blength(dir->normalized_base)) == 0, 
            "Request for path %s does not start with %s base after normalizing.", 
            bdata(target), bdata(dir->base));

    // the FileRecord now owns the target
    file = Dir_find_file(target, dir->default_ctype);
    check_debug(file, "Error opening file: %s", bdata(target));

    // Increment the user count because we're adding it to the cache
    file->users++;
    file->request_path = bstrcpy(path);
    Cache_add(dir->fr_cache, file);


    return file;

error:
    bdestroy(target);
    FileRecord_release(file);
    return NULL;
}
Esempio n. 8
0
char *dl_get_front(list_t *list){
	check_debug(list != NULL, "List is NULL.");
	check_debug(list->next != NULL && list->next != list, 
		"List is empty.");
	check_debug(list->next->word != NULL,
		"list->next->word is NULL.");
	return list->next->word;
error:
	return NULL;
}
Esempio n. 9
0
bool dl_pop_back(list_t *list){
	check_debug(list != NULL, "List is NULL.");
	check_debug(list->prev != NULL && list->prev != list,
		"List is empty.");
	check_debug(dl_remove(list, list->prev) == 1, 
		"Delete from back was not successful.");
	return 1;
error:
	return 0;
}
Esempio n. 10
0
bool dl_pop_front(list_t *list){
	check_debug(list != NULL, "List is NULL.");
	check_debug(list->next != NULL && list->next != list,
		"List is empty.");
	check_debug(dl_remove(list, list->next) == 1, 
		"Delete from front was not successful.");
	return 1;
error:
	return 0;
}
Esempio n. 11
0
char *dl_get_back(list_t *list){
	check_debug(list != NULL, "List is NULL.");
	check_debug(list->prev != NULL && list->prev != list, 
		"List is empty.");
	check_debug(list->prev->word != NULL,
		"list->prev->word is NULL.");
	return list->prev->word;
error:
	return 0;
}
Esempio n. 12
0
int main(int argc, const char *argv[])
{
    fd_set allreads;
    fd_set readmask;

    int socket = 0;
    int rc = 0;
    RingBuffer *in_rb = RingBuffer_create(1024 * 10);
    RingBuffer *sock_rb = RingBuffer_create(1024 * 10);

    check(argc == 3, "USAGE: netclient HOST PORT");

    socket = client_connect(argv[1], argv[2]);
    check(socket >= 0, "Connect to %s:%s failed.", argv[1], argv[2]);

    FD_ZERO(&allreads);
    FD_SET(socket, &allreads);
    FD_SET(0, &allreads);

    while(1) {
        readmask = allreads;
        rc = select(socket + 1, &readmask, NULL, NULL, NULL);
        check(rc >= 0, "Select failed");

        if(FD_ISSET(0, &readmask)) {
            rc = read_some(in_rb, 0, 0);
            check_debug(rc != -1, "failed to read from stdin.");
        }

        if(FD_ISSET(socket, &readmask)) {
            rc = read_some(sock_rb, socket, 0);
            check_debug(rc != -1, "failed to read from socket.");
        }

        while(!RingBuffer_empty(sock_rb)) {
            rc = write_some(sock_rb, 1, 0);
            check_debug(rc != -1, "failed to write to stdout.");
        }

        while(!RingBuffer_empty(in_rb)) {
            rc = write_some(in_rb, socket, 1);
            check_debug(rc != -1, "failed to write to socket.");
        }

    }

    RingBuffer_destroy(in_rb);
    RingBuffer_destroy(sock_rb);

    return 0;

error:
    return -1;
}
Esempio n. 13
0
static inline int Connection_deliver_enqueue(Connection *conn, bstring b)
{
    check_debug(conn->deliverPost-conn->deliverAck < DELIVER_OUTSTANDING_MSGS, "Too many outstanding messages") ;
    check_debug(conn->deliverTaskStatus==DT_RUNNING, "Cannot enqueue, deliver task not running");
    conn->deliverRing[conn->deliverPost++%DELIVER_OUTSTANDING_MSGS]=b;
    taskwakeup(&conn->deliverRendez);
    return 0;

error:
    return -1;
}
Esempio n. 14
0
bool dl_sort(list_t *list){
	check_debug(list != NULL,
		"list is null.");
	check_debug(list->prev != NULL
		&& list->next != NULL
		&& list->prev != list
		&& list->next != list,
		"list is empty.");
	while(bubble_sort(list) != 1){}
	return 1;
error:
	return 0;	
}
Esempio n. 15
0
int Register_fd_for_id(uint32_t id)
{
    RMElement *el = RadixMap_find(REG_ID_TO_FD, id);

    check_debug(el != NULL, "Id %d not registered.", id);

    Registration *reg = darray_get(REGISTRATIONS, el->data.value);
    check_debug(Register_valid(reg), "Nothing registered under id %d.", id);

    return reg->fd;
error:
    return -1;
}
Esempio n. 16
0
int dl_get_wordcount(list_t *list, char *word){
	check_debug(list != NULL,
		"list is NULL.");
	check_debug(word != NULL,
		"word is NULL");
	list_t *node = NULL;
	node = dl_check(list, word);
	check_debug(node != NULL,
		"word '%s' not in list.", word);
	return node->word_count;
error:
	return 0;
}
Esempio n. 17
0
static VALUE method_get_next_event(VALUE self, VALUE blocking) {
  // dbg.h 
  check_debug(!is_closed(self), "we are closed, not trying to get event");

  char buf[64];
  FETCH_DATA_PTR(self, zk);

  for (;;) {
    check_debug(!is_closed(self), "we're closed in the middle of method_get_next_event, bailing");

    zkrb_event_t *event = zkrb_dequeue(zk->queue, 1);

    /* Wait for an event using rb_thread_select() on the queue's pipe */
    if (event == NULL) {
      if (NIL_P(blocking) || (blocking == Qfalse)) { 
        goto error;
      } 
      else {
        // if we're shutting down, don't enter this section, we don't want to block
        check_debug(!is_shutting_down(self), "method_get_next_event, we're shutting down, don't enter blocking section");

        int fd = zk->queue->pipe_read;
        ssize_t bytes_read = 0;

        // wait for an fd to become readable, opposite of rb_thread_fd_writable
        rb_thread_wait_fd(fd);

        // clear all bytes here, we'll catch all the events on subsequent calls
        // (until we run out of events)
        bytes_read = read(fd, buf, sizeof(buf));

        if (bytes_read == -1) {
          rb_raise(rb_eRuntimeError, "read failed: %d", errno);
        }

        zkrb_debug_inst(self, "read %zd bytes from the queue (%p)'s pipe", bytes_read, zk->queue);

        continue;
      }
    }

    VALUE hash = zkrb_event_to_ruby(event);
    zkrb_event_free(event);
    return hash;
  }

  error: 
    return Qnil;
}
Esempio n. 18
0
static ssize_t plain_stream_file(IOBuf *iob, int fd, off_t len)
{
    off_t sent = 0;
    off_t total = 0;
    off_t offset = 0;
    off_t block_size = MAX_SEND_BUFFER;
    int conn_fd = IOBuf_fd(iob);

    for(total = 0; fdwait(conn_fd, 'w') == 0 && total < len; total += sent) {

        block_size = (len - total) > block_size ? block_size : (len - total);
        sent = IOBuf_sendfile(conn_fd, fd, &offset, block_size);

        check(Register_write(iob->fd, sent) != -1, "Socket seems to be closed.");

        check_debug(sent > 0, "Client closed probably during sendfile on socket: %d from "
                    "file %d", conn_fd, fd);
    }
    
    check(total <= len,
            "Wrote way too much, wrote %d but size was %zd",
            (int)total, len);

    check(total == len,
            "Sent other than expected, sent: %d, but expected: %zd", 
            (int)total, len);

    return total;

error:
    return -1;
}
Esempio n. 19
0
int AST_walk_hash(tst_t *settings, Value *data, ast_hash_walk_cb cb)
{
    struct ASTScanData scan = {.settings = settings, .cb = cb, .error = 0};
    tst_traverse(data->as.hash, ast_hash_traverse_cb, &scan);
    return scan.error;
}


Value *AST_get(tst_t *settings, tst_t *fr, bstring name, ValueType type)
{
    Pair *pair = tst_search(fr, bdata(name), blength(name));
    check_debug(pair, "Couldn't find variable %s of type %s", bdata(name), Value_type_name(type));

    Value *val = Pair_value(pair);

    if(Value_is(val, REF)) {
        val = Value_resolve(settings, val);
        check(val, "Couldn't find variable %s of type %s",
            bdata(name), Value_type_name(type));
    }

    check(val->type == type, "Invalid type for %s, should be %s not %s",
            bdata(name), Value_type_name(type), Value_type_name(val->type));

    return val;

error:
    return NULL;
}
Esempio n. 20
0
 int main(int argc, char *argv[])
 {
     bstring url = NULL;
     bstring route = NULL;
     TSTree *routes = NULL;

     check(argc == 2, "USAGE: urlor <urlfile>");

     routes = load_routes(argv[1]);
     check(routes != NULL, "Your route file has an error.");

     while (1) {
         url = read_line("URL> ");
         check_debug(url != NULL, "goodbye.");

         route = match_url(routes, url);

         if (route) {
             printf("MATCH: %s == %s\n", bdata(url), bdata(route));
         } else {
             printf("FAIL: %s\n", bdata(url));
         }

         bdestroy(url);
     }

     destroy_routes(routes);
     return 0;

 error:
     destroy_routes(routes);
     return 1;
 }
Esempio n. 21
0
int		main(int ac, char **av)
{
  unsigned char	board[MEM_SIZE];
  t_vm		*vm;
  t_dlist	*list;
  t_dlist	*list_s;

  if (ac < 2)
    return (0);
  list = NULL;
  list_s = NULL;
  list = new_list(list);
  list_s = new_list(list_s);
  init_board(board);
  vm = NULL;
  vm = new_vm(vm);
  fill_list(list, av);
  check_debug(list, vm);
  syntax(list);
  find_dump(list, vm);
  find_champ(list, vm, board, 0);
  id_champ(vm);
  champ_id_reg(vm);
  init_alive(vm);
  start_vm(vm, board);
  winning(vm);
  return (0);
}
Esempio n. 22
0
int main(int argc , char *argv[])
{
/*just echo the prompt and wait for a line to be enterred in
 *then search the url as the key to find data
 * then output
 */
	bstring url = NULL ;
	bstring route = NULL;
	check(argc == 2 , "usage: urlor <urlfile>");

	TSTree *routes = load_routes(argv[1]);
	check(routes != NULL , "your route file has an errro.");

	while(1){
		url = read_line("URL>");
		check_debug(url != NULL , "goodbye");
		route = match_url(routes , url);

		if(route) {
			printf("match %s == %s\n" , bdata(url) , bdata(route));
		} else{
			printf("fail;%s \n" , bdata(url));
		}
		
		bdestroy(url);
	}

	destroy_routes(routes);
	return 0;
error:
	destroy_routes(routes);
	return 1;
}
Esempio n. 23
0
int connection_route_request(Connection *conn)
{
    Host *host = NULL;
    Route *route = NULL;

    bstring path = Request_path(conn->req);
    check_debug(path != NULL, "No path given, in request, ignoring.");

    Server *server = Server_queue_latest();
    check(server != NULL, "No server in the server queue, tell Zed.");

    if(conn->req->host_name) {
        host = Server_match_backend(server, conn->req->host_name);
    } else {
        host = server->default_host;
    }

    error_unless(host, conn, 404, "Request for a host we don't have registered: %s", bdata(conn->req->host_name));

    Backend *found = Host_match_backend(host, path, &route);
    error_unless(found, conn, 404, "Handler not found: %s", bdata(path));

    Request_set_action(conn->req, found);

    conn->req->target_host = host;
    conn->req->pattern = route->pattern;
    conn->req->prefix = route->prefix;

    return Connection_backend_event(found, conn);

error:
    return CLOSE;
}
Esempio n. 24
0
int test_check_debug() {
    int i = 0;
    check_debug(i != 0, "Oops, I was 0.");

    return 0;
error:
    return -1;
}
Esempio n. 25
0
/** Functions for loading
 * Loading database -> opening file and allocation of whole database into
 * memory with handlers for errors.
 */
int Database_load(struct Connection *conn)
{
    int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);
    check_debug(!rc, "Failed to load database");
    return 0;
error:
    return -1;
}
Esempio n. 26
0
int Dir_serve_file(Dir *dir, Request *req, Connection *conn)
{
    FileRecord *file = NULL;
    bstring resp = NULL;
    bstring path = Request_path(req);
    bstring pattern = req->pattern;
    int rc = 0;
    int is_get = biseq(req->request_method, &HTTP_GET);
    int is_head = is_get ? 0 : biseq(req->request_method, &HTTP_HEAD);

    check(path, "Request had not path. That's weird.");
    req->response_size = 0;

    if(!(is_get || is_head)) {
        req->status_code = 405;
        rc = Response_send_status(conn, &HTTP_405);
        check_debug(rc == blength(&HTTP_405), "Failed to send 405 to client.");
        return -1;
    } else {
        file = Dir_resolve_file(dir, pattern, path);
        resp = Dir_calculate_response(req, file);

        if(resp) {
            rc = Response_send_status(conn, resp);
            check_debug(rc == blength(resp), "Failed to send error response on file serving.");
        } else if(is_get) {
            rc = Dir_stream_file(file, conn);
            req->response_size = rc;
            check_debug(rc == file->sb.st_size, "Didn't send all of the file, sent %d of %s.", rc, bdata(path));
        } else if(is_head) {
            rc = Dir_send_header(file, conn);
            check_debug(rc, "Failed to write header to socket.");
        } else {
            sentinel("How the hell did you get to here. Tell Zed.");
        }

        FileRecord_release(file);
        return 0;
    }

    sentinel("Invalid code branch, Tell Zed you have magic.");
error:
    FileRecord_release(file);
    return -1;
}
Esempio n. 27
0
int dl_count(list_t *list){
	check_debug(list != NULL, "List is NULL.");
	check_debug(list->prev != NULL && list->next != NULL,
		"List is empty.");
	list_t *node = NULL;
	node = list->next;
	int count = 0;
	for(; node != list; node = node->next){
		check_debug(node != NULL && node->next != NULL,
			"Invalid list, node: %s, node->next: %s",
			node == NULL ? "NULL" : "VALID",
			node->next == NULL ? "NULL" : "VALID");
		count++;
	}
	return count;
error:
	return 0;
}
Esempio n. 28
0
int fs_create_dir(const char *path)
{
	errno = 0;
	mkdir(path, S_IRWXU);
	check_debug(errno == 0, "Could not create dir");
	return 0;

error:
	return errno;
}
Esempio n. 29
0
int test_check_debug()
{
	int i = 0;
	check_debug(i != 0, "It was 0");
	
	return 0;

error:
	return -1;
}
Esempio n. 30
0
static ssize_t ssl_stream_file(IOBuf *iob, int fd, off_t len)
{
    ssize_t sent = 0;
    off_t total = 0;
    ssize_t amt = 0;
    ssize_t tosend = 0;
    int conn_fd = IOBuf_fd(iob);
    char buff[1024];

    for(total = 0; fdwait(conn_fd, 'w') == 0 && total < len; total += tosend) {
        tosend = pread(fd, buff, sizeof(buff), total);
        check_debug(tosend > 0, "Came up short in reading file %d\n", fd);

        // We do this in case the file somehow lengthened on us.  In general,
        // it shouldn't happen.
        if(tosend + total > len)
            tosend = len - total;

        sent = 0;
        while(sent < tosend) {
            amt = ssl_send(iob, buff, tosend);
            check_debug(amt > 0, "ssl_send failed in ssl_stream_file with "
                        "return code %zd", amt);
            sent += amt;
        }

        check(Register_write(iob->fd, sent) != -1, "Failed to record write, must have died.");
    }
    
    check(total <= len,
            "Wrote way too much, wrote %d but size was %zd",
            (int)total, len);

    check(total == len,
            "Sent other than expected, sent: %d, but expected: %zd", 
            (int)total, len);

    return total;

error:
    return -1;
}