static void run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { void *addr; size_t alen; struct sockaddr_in *v4; struct sockaddr_in expect; asock = GNUNET_CONNECTION_create_from_accept (NULL, NULL, ls); GNUNET_assert (asock != NULL); GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock)); GNUNET_assert (GNUNET_OK == GNUNET_CONNECTION_get_address (asock, &addr, &alen)); GNUNET_assert (alen == sizeof (struct sockaddr_in)); v4 = addr; memset (&expect, 0, sizeof (expect)); #if HAVE_SOCKADDR_IN_SIN_LEN expect.sin_len = sizeof (expect); #endif expect.sin_family = AF_INET; expect.sin_port = v4->sin_port; expect.sin_addr.s_addr = htonl (INADDR_LOOPBACK); GNUNET_assert (0 == memcmp (&expect, v4, alen)); GNUNET_free (addr); GNUNET_CONNECTION_destroy (lsock); GNUNET_CONNECTION_receive (asock, 1024, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, cls); }
/** * Read from the service. * * @param client the service * @param handler function to call with the message * @param handler_cls closure for handler * @param timeout how long to wait until timing out */ void GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client, GNUNET_CLIENT_MessageHandler handler, void *handler_cls, struct GNUNET_TIME_Relative timeout) { if (NULL == client->connection) { /* already disconnected, fail instantly! */ GNUNET_break (0); /* this should not happen in well-written code! */ if (NULL != handler) handler (handler_cls, NULL); return; } client->receiver_handler = handler; client->receiver_handler_cls = handler_cls; client->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); if (GNUNET_YES == client->msg_complete) { GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->receive_task); client->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, client); } else { LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n"); GNUNET_assert (GNUNET_NO == client->in_receive); client->in_receive = GNUNET_YES; GNUNET_CONNECTION_receive (client->connection, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, timeout, &receive_helper, client); } }
static void receive_check (void *cls, const void *buf, size_t available, const struct sockaddr *addr, socklen_t addrlen, int errCode) { int *ok = cls; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive validates incoming data\n"); GNUNET_assert (buf != NULL); /* no timeout */ if (0 == memcmp (&"Hello World"[sofar], buf, available)) sofar += available; if (sofar < 12) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive needs more data\n"); GNUNET_CONNECTION_receive (asock, 1024, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, cls); } else { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive closes accepted socket\n"); *ok = 0; GNUNET_CONNECTION_destroy (asock); GNUNET_CONNECTION_destroy (csock); } }
/** * Register callback to read from the SOCKS5 proxy. * * @param client the service * @param handler function to call with the message * @param handler_cls closure for @a handler */ void register_reciever (struct GNUNET_SOCKS_Handshake *ih, int want) { GNUNET_CONNECTION_receive (ih->socks5_connection, want, GNUNET_TIME_relative_get_minute_ (), &reciever, ih); }
static void run_accept (void *cls) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test accepts connection\n"); asock = GNUNET_CONNECTION_create_from_accept (NULL, NULL, ls); GNUNET_assert (asock != NULL); GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock)); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test destroys listen socket\n"); GNUNET_CONNECTION_destroy (lsock); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test asks to receive on accepted socket\n"); GNUNET_CONNECTION_receive (asock, 1024, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, cls); }
static void receive_check (void *cls, const void *buf, size_t available, const struct sockaddr *addr, socklen_t addrlen, int errCode) { int *ok = cls; GNUNET_assert (buf != NULL); /* no timeout */ if (0 == memcmp (&"Hello World"[sofar], buf, available)) sofar += available; if (sofar < 12) { GNUNET_CONNECTION_receive (asock, 1024, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check, cls); } else { *ok = 0; GNUNET_CONNECTION_destroy (csock); GNUNET_CONNECTION_destroy (asock); } }