示例#1
0
static int handle_lossy_packet(void *object, int number, const uint8_t *data, uint16_t length, void *userdata)
{
    if (length == 0) {
        return -1;
    }

    Friend_Connections *const fr_c = (Friend_Connections *)object;
    const Friend_Conn *friend_con = get_conn(fr_c, number);

    if (!friend_con) {
        return -1;
    }

    for (unsigned i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
        if (friend_con->callbacks[i].lossy_data_callback) {
            friend_con->callbacks[i].lossy_data_callback(
                friend_con->callbacks[i].callback_object,
                friend_con->callbacks[i].callback_id, data, length, userdata);
        }

        friend_con = get_conn(fr_c, number);

        if (!friend_con) {
            return -1;
        }
    }

    return 0;
}
示例#2
0
void
EngineBase::delete_from(const Table &table, const Keys &keys)
{
    if (get_mode() == READ_ONLY)
        throw BadOperationInMode(
                _T("Using DELETE operation in read-only mode"));
    if (!keys.size())
        return;
    touch();
    String sql;
    TypeCodes type_codes;
    SqlGeneratorOptions options(NO_QUOTES,
            get_dialect()->has_for_update(),
            true,
            get_conn()->get_driver()->numbered_params(),
            (Yb::SqlPagerModel)get_dialect()->pager_model());
    gen_sql_delete(sql, type_codes, table, options);
    auto_ptr<SqlCursor> cursor = get_conn()->new_cursor();
    cursor->prepare(sql);
    cursor->bind_params(type_codes);
    Values params(type_codes.size());
    Keys::const_iterator k = keys.begin(), kend = keys.end();
    for (; k != kend; ++k) {
        for (size_t i = 0; i < k->second.size(); ++i)
            params[i] = k->second[i].second;
        cursor->exec(params);
    }
}
示例#3
0
void
EngineBase::update(const Table &table, const RowsData &rows)
{
    if (get_mode() == READ_ONLY)
        throw BadOperationInMode(
                _T("Using UPDATE operation in read-only mode"));
    if (!rows.size())
        return;
    touch();
    String sql;
    TypeCodes type_codes;
    ParamNums param_nums;
    SqlGeneratorOptions options(NO_QUOTES,
            get_dialect()->has_for_update(),
            true,
            get_conn()->get_driver()->numbered_params(),
            (Yb::SqlPagerModel)get_dialect()->pager_model());
    gen_sql_update(sql, type_codes, param_nums, table, options);
    auto_ptr<SqlCursor> cursor = get_conn()->new_cursor();
    cursor->prepare(sql);
    cursor->bind_params(type_codes);
    Values params(type_codes.size());
    RowsData::const_iterator r = rows.begin(), rend = rows.end();
    for (; r != rend; ++r) {
        ParamNums::const_iterator f = param_nums.begin(),
            fend = param_nums.end();
        for (; f != fend; ++f)
            params[f->second] = (**r)[table.idx_by_name(f->first)];
        cursor->exec(params);
    }
}
示例#4
0
static int handle_lossy_packet(void *object, int number, const uint8_t *data, uint16_t length)
{
    if (length == 0)
        return -1;

    Friend_Connections *fr_c = object;
    Friend_Conn *friend_con = get_conn(fr_c, number);

    if (!friend_con)
        return -1;

    unsigned int i;

    for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
        if (friend_con->callbacks[i].lossy_data_callback)
            friend_con->callbacks[i].lossy_data_callback(friend_con->callbacks[i].lossy_data_callback_object,
                    friend_con->callbacks[i].lossy_data_callback_id, data, length);

        friend_con = get_conn(fr_c, number);

        if (!friend_con)
            return -1;
    }

    return 0;
}
示例#5
0
static int handle_packet(void *object, int number, const uint8_t *data, uint16_t length, void *userdata)
{
    if (length == 0) {
        return -1;
    }

    Friend_Connections *const fr_c = (Friend_Connections *)object;
    Friend_Conn *friend_con = get_conn(fr_c, number);

    if (!friend_con) {
        return -1;
    }

    if (data[0] == PACKET_ID_FRIEND_REQUESTS) {
        if (fr_c->fr_request_callback) {
            fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length, userdata);
        }

        return 0;
    }

    if (data[0] == PACKET_ID_ALIVE) {
        friend_con->ping_lastrecv = mono_time_get(fr_c->mono_time);
        return 0;
    }

    if (data[0] == PACKET_ID_SHARE_RELAYS) {
        Node_format nodes[MAX_SHARED_RELAYS];
        const int n = unpack_nodes(nodes, MAX_SHARED_RELAYS, nullptr, data + 1, length - 1, 1);

        if (n == -1) {
            return -1;
        }

        for (int j = 0; j < n; ++j) {
            friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key);
        }

        return 0;
    }

    for (unsigned i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
        if (friend_con->callbacks[i].data_callback) {
            friend_con->callbacks[i].data_callback(
                friend_con->callbacks[i].callback_object,
                friend_con->callbacks[i].callback_id, data, length, userdata);
        }

        friend_con = get_conn(fr_c, number);

        if (!friend_con) {
            return -1;
        }
    }

    return 0;
}
示例#6
0
static int handle_packet(void *object, int number, uint8_t *data, uint16_t length, void *userdata)
{
    if (length == 0)
        return -1;

    Friend_Connections *fr_c = object;
    Friend_Conn *friend_con = get_conn(fr_c, number);

    if (!friend_con)
        return -1;

    if (data[0] == PACKET_ID_FRIEND_REQUESTS) {
        if (fr_c->fr_request_callback)
            fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length);

        return 0;
    } else if (data[0] == PACKET_ID_ALIVE) {
        friend_con->ping_lastrecv = unix_time();
        return 0;
    } else if (data[0] == PACKET_ID_SHARE_RELAYS) {
        Node_format nodes[MAX_SHARED_RELAYS];
        int n;

        if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data + 1, length - 1, 1)) == -1)
            return -1;

        int j;

        for (j = 0; j < n; j++) {
            friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key);
        }

        return 0;
    }

    unsigned int i;

    for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
        if (friend_con->callbacks[i].data_callback)
            friend_con->callbacks[i].data_callback(
                friend_con->callbacks[i].data_callback_object,
                friend_con->callbacks[i].data_callback_id, data, length, userdata);

        friend_con = get_conn(fr_c, number);

        if (!friend_con)
            return -1;
    }

    return 0;
}
示例#7
0
CAMLprim value PQprepare_stub(value v_conn, value v_stm_name, value v_query)
{
  CAMLparam1(v_conn);
  PGconn *conn = get_conn(v_conn);
  np_callback *np_cb = get_conn_cb(v_conn);
  PGresult *res;
  size_t stm_name_len = caml_string_length(v_stm_name) + 1;
  size_t query_len = caml_string_length(v_query) + 1;
  char *stm_name = caml_stat_alloc(stm_name_len);
  char *query = caml_stat_alloc(query_len);
  memcpy(stm_name, String_val(v_stm_name), stm_name_len);
  memcpy(query, String_val(v_query), query_len);
  caml_enter_blocking_section();
    res = PQprepare(conn, stm_name, query, 0, NULL);
    free(stm_name);
    free(query);
  caml_leave_blocking_section();
  CAMLreturn(alloc_result(res, np_cb));
#else
CAMLprim value PQprepare_stub(
  value __unused v_conn, value __unused v_stm_name, value __unused v_query)
{
  caml_failwith("Postgresql.prepare: not supported");
  return Val_unit;
#endif
}
示例#8
0
static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
{
    Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);

    if (!friend_con) {
        return 0;
    }

    Node_format nodes[MAX_SHARED_RELAYS];
    uint8_t data[1024];

    const int n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS);

    for (int i = 0; i < n; ++i) {
        /* Associated the relays being sent with this connection.
           On receiving the peer will do the same which will establish the connection. */
        friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key);
    }

    int length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n);

    if (length <= 0) {
        return 0;
    }

    data[0] = PACKET_ID_SHARE_RELAYS;
    ++length;

    if (write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, data, length, 0) != -1) {
        friend_con->share_relays_lastsent = mono_time_get(fr_c->mono_time);
        return 1;
    }

    return 0;
}
示例#9
0
/* Callback for dht public key changes. */
static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key, void *userdata)
{
    Friend_Connections *const fr_c = (Friend_Connections *)object;
    Friend_Conn *const friend_con = get_conn(fr_c, number);

    if (!friend_con) {
        return;
    }

    if (public_key_cmp(friend_con->dht_temp_pk, dht_public_key) == 0) {
        return;
    }

    change_dht_pk(fr_c, number, dht_public_key);

    /* if pk changed, create a new connection.*/
    if (friend_con->crypt_connection_id != -1) {
        crypto_kill(fr_c->net_crypto, friend_con->crypt_connection_id);
        friend_con->crypt_connection_id = -1;
        handle_status(object, number, 0, userdata); /* Going offline. */
    }

    friend_new_connection(fr_c, number);
    onion_set_friend_DHT_pubkey(fr_c->onion_c, friend_con->onion_friendnum, dht_public_key);
}
示例#10
0
CAMLprim value PQgetlineAsync_stub(
  value v_conn, value v_buf, value v_pos, value v_len)
{
  return Val_int(PQgetlineAsync(get_conn(v_conn),
                                String_val(v_buf) + Long_val(v_pos),
                                Long_val(v_len)));
}
示例#11
0
int update_filesize(struct inode * e, off_t newsize) {
    bson cond, doc;
    mongo * conn = get_conn();
    int res;

    if(newsize < e->size)
        return 0;

    e->size = newsize;

    bson_init(&cond);
    bson_append_oid(&cond, "_id", &e->oid);
    bson_finish(&cond);

    bson_init(&doc);
    bson_append_start_object(&doc, "$set");
    bson_append_long(&doc, "size", newsize);
    bson_append_finish_object(&doc);
    bson_finish(&doc);

    res = mongo_update(conn, inodes_name, &cond, &doc, 0, NULL);
    bson_destroy(&cond);
    bson_destroy(&doc);

    if(res != 0)
        return -EIO;
    return 0;
}
示例#12
0
/* Set the callbacks for the friend connection.
 * index is the index (0 to (MAX_FRIEND_CONNECTION_CALLBACKS - 1)) we want the callback to set in the array.
 *
 * return 0 on success.
 * return -1 on failure
 */
int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsigned int index,
                                fc_status_cb *status_callback,
                                fc_data_cb *data_callback,
                                fc_lossy_data_cb *lossy_data_callback,
                                void *object, int number)
{
    Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);

    if (!friend_con) {
        return -1;
    }

    if (index >= MAX_FRIEND_CONNECTION_CALLBACKS) {
        return -1;
    }

    friend_con->callbacks[index].status_callback = status_callback;
    friend_con->callbacks[index].data_callback = data_callback;
    friend_con->callbacks[index].lossy_data_callback = lossy_data_callback;

    friend_con->callbacks[index].callback_object = object;
    friend_con->callbacks[index].callback_id = number;

    return 0;
}
示例#13
0
CAMLprim value PQsetNoticeProcessor_stub(value v_conn, value v_cb)
{
  np_decr_refcount(get_conn_cb(v_conn));
  set_conn_cb(v_conn, np_new(v_cb));
  PQsetNoticeProcessor(get_conn(v_conn), &notice_ml, get_conn_cb(v_conn));
  return Val_unit;
}
示例#14
0
static int handle_new_connections(void *object, New_Connection *n_c)
{
    Friend_Connections *fr_c = object;
    int friendcon_id = getfriend_conn_id_pk(fr_c, n_c->public_key);
    Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);

    if (friend_con) {

        if (friend_con->crypt_connection_id != -1)
            return -1;

        int id = accept_crypto_connection(fr_c->net_crypto, n_c);
        connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id);
        connection_data_handler(fr_c->net_crypto, id, &handle_packet, fr_c, friendcon_id);
        connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id);
        friend_con->crypt_connection_id = id;

        if (n_c->source.ip.family != AF_INET && n_c->source.ip.family != AF_INET6) {
            set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port);
        } else {
            friend_con->dht_ip_port = n_c->source;
            friend_con->dht_ip_port_lastrecv = unix_time();
        }

        dht_pk_callback(fr_c, friendcon_id, n_c->dht_public_key);

        nc_dht_pk_callback(fr_c->net_crypto, id, &dht_pk_callback, fr_c, friendcon_id);
        return 0;
    }

    return -1;
}
示例#15
0
/**
 * Invoked when a TCP listening socket fd is ready
 * to accept a new client. Accepts the client, initializes
 * the connection buffers, and prepares to start listening
 * for client data
 */
static void handle_new_client(int listen_fd, worker_ev_userdata* data) {
    // Accept the client connection
    struct sockaddr_in client_addr;
    int client_addr_len = sizeof(client_addr);
    int client_fd = accept(listen_fd,
                        (struct sockaddr*)&client_addr,
                        &client_addr_len);

    // Check for an error
    if (client_fd == -1) {
        syslog(LOG_ERR, "Failed to accept() connection! %s.", strerror(errno));
        return;
    }

    // Setup the socket
    if (set_client_sockopts(client_fd)) {
        return;
    }

    // Debug info
    syslog(LOG_DEBUG, "Accepted client connection: %s %d [%d]",
            inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), client_fd);

    // Get the associated conn object
    conn_info *conn = get_conn(data->netconf);

    // Initialize the libev stuff
    ev_io_init(&conn->client, prepare_event, client_fd, EV_READ);
    ev_io_init(&conn->write_client, prepare_event, client_fd, EV_WRITE);

    // Schedule the new client
    schedule_async(data->netconf, SCHEDULE_WATCHER, &conn->client);
}
示例#16
0
static int handle_status(void *object, int number, uint8_t status)
{
    Friend_Connections *fr_c = object;
    Friend_Conn *friend_con = get_conn(fr_c, number);

    if (!friend_con)
        return -1;

    if (status) {  /* Went online. */
        friend_con->status = FRIENDCONN_STATUS_CONNECTED;
        friend_con->ping_lastrecv = unix_time();
        onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status);
    } else {  /* Went offline. */
        friend_con->status = FRIENDCONN_STATUS_CONNECTING;
        friend_con->crypt_connection_id = -1;
        friend_con->dht_ping_lastrecv = unix_time();
    }

    unsigned int i;

    for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
        if (friend_con->callbacks[i].status_callback)
            friend_con->callbacks[i].status_callback(friend_con->callbacks[i].status_callback_object,
                    friend_con->callbacks[i].status_callback_id, status);
    }

    return 0;
}
示例#17
0
static int handle_packet(void *object, int number, uint8_t *data, uint16_t length)
{
    if (length == 0)
        return -1;

    Friend_Connections *fr_c = object;
    Friend_Conn *friend_con = get_conn(fr_c, number);

    if (!friend_con)
        return -1;

    if (data[0] == PACKET_ID_ALIVE) {
        friend_con->ping_lastrecv = unix_time();
        return 0;
    }

    unsigned int i;

    for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
        if (friend_con->callbacks[i].data_callback)
            friend_con->callbacks[i].data_callback(friend_con->callbacks[i].data_callback_object,
                                                   friend_con->callbacks[i].data_callback_id, data, length);
    }

    return 0;
}
示例#18
0
/* Callback for dht public key changes. */
static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key)
{
    Friend_Connections *fr_c = object;
    Friend_Conn *friend_con = get_conn(fr_c, number);

    if (!friend_con)
        return;

    friend_con->dht_ping_lastrecv = unix_time();

    if (memcmp(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0)
        return;

    if (friend_con->dht_lock) {
        if (DHT_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock) != 0) {
            printf("a. Could not delete dht peer. Please report this.\n");
            return;
        }

        friend_con->dht_lock = 0;
    }

    DHT_addfriend(fr_c->dht, dht_public_key, dht_ip_callback, object, number, &friend_con->dht_lock);

    if (friend_con->crypt_connection_id == -1) {
        friend_new_connection(fr_c, number);
    }

    set_connection_dht_public_key(fr_c->net_crypto, friend_con->crypt_connection_id, dht_public_key);
    onion_set_friend_DHT_pubkey(fr_c->onion_c, friend_con->onion_friendnum, dht_public_key);

    memcpy(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES);
}
示例#19
0
int configure()
{

	struct conn * conn = get_conn();
	cw_send_request(conn,CW_MSG_CONFIGURATION_STATUS_REQUEST);



/*	struct wtpinfo * wtpinfo = get_wtpinfo();

	struct radioinfo *rip = &(wtpinfo->radioinfo[0]);

	cw_prepare_configuration_status_request(conn,rip,wtpinfo);
	struct cwrmsg * cwrmsg = conn_send_request(conn);

	int rc = cw_readmsg_configuration_status_response(cwrmsg->msgelems,cwrmsg->msgelems_len);

	printf("Chage Sate\n");
	cw_prepare_change_state_event_request(conn,rip,wtpinfo);
	cwrmsg = conn_send_request(conn);


	printf("Got change resp %p\n",cwrmsg);
*/
	return 0;
}
示例#20
0
void
req_authenResvPort(struct batch_request *preq)
{
	pbs_net_t	req_addr;
	conn_t		*cp;
	uint		authrequest_port = preq->rq_ind.rq_authen_resvport.rq_port;

	cp = get_conn(preq->rq_conn);
	if (!cp) {
		req_reject(PBSE_SYSTEM, 0, preq);
		return;
	}

	req_addr = cp->cn_addr;

	/*
	 * find the socket whose client side is bound to the port named
	 * in the request
	 */

	for (cp = (conn_t *)GET_NEXT(svr_allconns); cp; cp = GET_NEXT(cp->cn_link)) {
		if (authrequest_port == cp->cn_port && req_addr == cp->cn_addr) {
			if ((cp->cn_authen & (PBS_NET_CONN_AUTHENTICATED | PBS_NET_CONN_FROM_PRIVIL)) == 0) {
				(void) strcpy(cp->cn_username, preq->rq_user);
				(void)strcpy(cp->cn_hostname, preq->rq_host);
				/* time stamp just for the record */
				cp->cn_timestamp = time_now;
				cp->cn_authen |= PBS_NET_CONN_AUTHENTICATED;
			}
			reply_ack(preq);
			return;
		}
	}
	req_reject(PBSE_BADCRED, 0, preq);
}
示例#21
0
CAMLprim value PQexecParams_stub(
  value v_conn, value v_query, value v_params, value v_binary_params)
{
  CAMLparam1(v_conn);
  PGconn *conn = get_conn(v_conn);
  np_callback *np_cb = get_conn_cb(v_conn);
  PGresult *res;
  size_t len = caml_string_length(v_query) + 1;
  char *query = caml_stat_alloc(len);
  size_t nparams = Wosize_val(v_params);
  const char * const *params = copy_params(v_params, nparams);
  int *formats, *lengths;
  copy_binary_params(v_params, v_binary_params, nparams, &formats, &lengths);
  memcpy(query, String_val(v_query), len);
  caml_enter_blocking_section();
    res =
      (nparams == 0)
        ? PQexec(conn, query)
        : PQexecParams(conn, query, nparams, NULL, params, lengths, formats, 0);
    free_binary_params(formats, lengths);
    free_params(params, nparams);
    free(query);
  caml_leave_blocking_section();
  CAMLreturn(alloc_result(res, np_cb));
}
示例#22
0
文件: cfg.c 项目: Benyjuice/actube
int cfg_json_put_radios(char *dst, const char *name, mbag_item_t * i, int n)
{
	struct conn *conn = get_conn();
	char *d = dst;


	memset(d, '\t', n);
	d += n;
	d += sprintf(d, "\"radios\":{\n");

	MAVLITER_DEFINE(radios, conn->radios);
	const char *comma = "";
	mavliter_foreach(&radios) {
		mbag_item_t *i = mavliter_get(&radios);
//		int rid = i->iid;
		//mbag_t radio = i->data;

		d += sprintf(d, "%s", comma);
		comma = ",\n";
		memset(d, '\t', n + 1);
		d += n + 1;
		d += sprintf(d, "\"%d\":", i->iid);

		//d += mbag_tojson(d, i->data, radio_cfg, n + 1);
	}

	d += sprintf(d, "\n");
	memset(d, '\t', n);
	d += n;
	d += sprintf(d, "}");
	return d - dst;
}
示例#23
0
static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id)
{
    Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);

    if (!friend_con) {
        return -1;
    }

    if (friend_con->crypt_connection_id != -1) {
        return -1;
    }

    /* If dht_temp_pk does not contains a pk. */
    if (!friend_con->dht_lock) {
        return -1;
    }

    const int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key, friend_con->dht_temp_pk);

    if (id == -1) {
        return -1;
    }

    friend_con->crypt_connection_id = id;
    connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id);
    connection_data_handler(fr_c->net_crypto, id, &handle_packet, fr_c, friendcon_id);
    connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id);
    nc_dht_pk_callback(fr_c->net_crypto, id, &dht_pk_callback, fr_c, friendcon_id);

    return 0;
}
示例#24
0
/* Set the callbacks for the friend connection.
 * index is the index (0 to (MAX_FRIEND_CONNECTION_CALLBACKS - 1)) we want the callback to set in the array.
 *
 * return 0 on success.
 * return -1 on failure
 */
int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsigned int index,
                                int (*status_callback)(void *object, int id, uint8_t status), int (*data_callback)(void *object, int id, uint8_t *data,
                                        uint16_t length), int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length), void *object,
                                int number)
{
    Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);

    if (!friend_con)
        return -1;

    if (index >= MAX_FRIEND_CONNECTION_CALLBACKS)
        return -1;

    friend_con->callbacks[index].status_callback = status_callback;
    friend_con->callbacks[index].data_callback = data_callback;
    friend_con->callbacks[index].lossy_data_callback = lossy_data_callback;

    friend_con->callbacks[index].status_callback_object =
        friend_con->callbacks[index].data_callback_object =
            friend_con->callbacks[index].lossy_data_callback_object = object;

    friend_con->callbacks[index].status_callback_id =
        friend_con->callbacks[index].data_callback_id =
            friend_con->callbacks[index].lossy_data_callback_id = number;
    return 0;
}
示例#25
0
/* Send a Friend request packet.
 *
 *  return -1 if failure.
 *  return  0 if it sent the friend request directly to the friend.
 *  return the number of peers it was routed through if it did not send it directly.
 */
int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *data,
                               uint16_t length)
{
    if (1 + sizeof(nospam_num) + length > ONION_CLIENT_MAX_DATA_SIZE || length == 0) {
        return -1;
    }

    const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);

    if (!friend_con) {
        return -1;
    }

    VLA(uint8_t, packet, 1 + sizeof(nospam_num) + length);
    memcpy(packet + 1, &nospam_num, sizeof(nospam_num));
    memcpy(packet + 1 + sizeof(nospam_num), data, length);

    if (friend_con->status == FRIENDCONN_STATUS_CONNECTED) {
        packet[0] = PACKET_ID_FRIEND_REQUESTS;
        return write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, packet, SIZEOF_VLA(packet), 0) != -1;
    }

    packet[0] = CRYPTO_PACKET_FRIEND_REQ;
    const int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, SIZEOF_VLA(packet));

    if (num <= 0) {
        return -1;
    }

    return num;
}
示例#26
0
/**
 * Invoked when a TCP listening socket fd is ready
 * to accept a new client. Accepts the client, initializes
 * the connection buffers, and stars to listening for data
 */
static void handle_new_client(ev_io *watcher, int ready_events) {
    // Accept the client connection
    int listen_fd = watcher->fd;
    struct sockaddr_in client_addr;
    int client_addr_len = sizeof(client_addr);
    int client_fd = accept(listen_fd,
                        (struct sockaddr*)&client_addr,
                        &client_addr_len);

    // Check for an error
    if (client_fd == -1) {
        syslog(LOG_ERR, "Failed to accept() connection! %s.", strerror(errno));
        return;
    }

    // Setup the socket
    if (set_client_sockopts(client_fd)) {
        return;
    }

    // Debug info
    syslog(LOG_DEBUG, "Accepted client connection: %s %d [%d]",
            inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), client_fd);

    // Get the associated conn object
    conn_info *conn = get_conn();

    // Initialize the libev stuff
    ev_io_init(&conn->client, invoke_event_handler, client_fd, EV_READ);
    ev_io_start(&conn->client);
}
示例#27
0
SqlResultSet
EngineBase::select_iter(const Expression &select_expr)
{
    SqlGeneratorOptions options(NO_QUOTES,
            get_dialect()->has_for_update(),
            true,
            get_conn()->get_driver()->numbered_params(),
            (Yb::SqlPagerModel)get_dialect()->pager_model());
    SqlGeneratorContext ctx;
    String sql = select_expr.generate_sql(options, &ctx);
    auto_ptr<SqlCursor> cursor = get_conn()->new_cursor();
    cursor->prepare(sql);
    SqlResultSet rs = cursor->exec(ctx.params_);
    rs.own(cursor);
    return rs;
}
示例#28
0
/* Add a TCP relay associated to the friend.
 *
 * return -1 on failure.
 * return 0 on success.
 */
int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key)
{
    Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);

    if (!friend_con) {
        return -1;
    }

    /* Local ip and same pk means that they are hosting a TCP relay. */
    if (ip_is_local(ip_port.ip) && public_key_cmp(friend_con->dht_temp_pk, public_key) == 0) {
        if (!net_family_is_unspec(friend_con->dht_ip_port.ip.family)) {
            ip_port.ip = friend_con->dht_ip_port.ip;
        } else {
            friend_con->hosting_tcp_relay = 0;
        }
    }

    const uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS;

    for (unsigned i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) {
        if (!net_family_is_unspec(friend_con->tcp_relays[i].ip_port.ip.family)
                && public_key_cmp(friend_con->tcp_relays[i].public_key, public_key) == 0) {
            memset(&friend_con->tcp_relays[i], 0, sizeof(Node_format));
        }
    }

    friend_con->tcp_relays[index].ip_port = ip_port;
    memcpy(friend_con->tcp_relays[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
    ++friend_con->tcp_relay_counter;

    return add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, public_key);
}
示例#29
0
int inode_exists(const char * path) {
    bson query, fields;
    mongo * conn = get_conn();
    mongo_cursor curs;
    int res;

    bson_init(&query);
    bson_append_string(&query, "dirents", path);
    bson_finish(&query);

    bson_init(&fields);
    bson_append_int(&fields, "dirents", 1);
    bson_append_int(&fields, "_id", 0);
    bson_finish(&fields);

    mongo_cursor_init(&curs, conn, inodes_name);
    mongo_cursor_set_query(&curs, &query);
    mongo_cursor_set_fields(&curs, &fields);
    mongo_cursor_set_limit(&curs, 1);

    res = mongo_cursor_next(&curs);
    bson_destroy(&query);
    bson_destroy(&fields);
    mongo_cursor_destroy(&curs);

    if(res == 0)
        return 0;
    if(curs.err != MONGO_CURSOR_EXHAUSTED)
        return -EIO;
    return -ENOENT;
}
示例#30
0
CAMLprim value PQexecPrepared_stub(
  value v_conn, value v_stm_name, value v_params, value v_binary_params)
{
  CAMLparam1(v_conn);
  PGconn *conn = get_conn(v_conn);
  np_callback *np_cb = get_conn_cb(v_conn);
  PGresult *res;
  size_t len = caml_string_length(v_stm_name) + 1;
  char *stm_name = caml_stat_alloc(len);
  size_t nparams = Wosize_val(v_params);
  const char * const *params = copy_params(v_params, nparams);
  int *formats, *lengths;
  copy_binary_params(v_params, v_binary_params, nparams, &formats, &lengths);
  memcpy(stm_name, String_val(v_stm_name), len);
  caml_enter_blocking_section();
    res = PQexecPrepared(conn, stm_name, nparams, params, lengths, formats, 0);
    free(stm_name);
    free_binary_params(formats, lengths);
    free_params(params, nparams);
  caml_leave_blocking_section();
  CAMLreturn(alloc_result(res, np_cb));
#else
CAMLprim value PQexecPrepared_stub(
  value __unused v_conn, value __unused v_stm_name, value __unused v_params,
  value __unused v_binary_params)
{
  caml_failwith("Postgresql.exec_prepared: not supported");
  return Val_unit;
#endif
}